3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright 2018 Allan Jude <allanjude@freebsd.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted providing that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
30 ############################################################ CONFIGURATION
33 : ${TRUSTPATH:=${DESTDIR}/usr/share/certs/trusted:${DESTDIR}/usr/local/share/certs:${DESTDIR}/usr/local/etc/ssl/certs}
34 : ${BLACKLISTPATH:=${DESTDIR}/usr/share/certs/blacklisted:${DESTDIR}/usr/local/etc/ssl/blacklisted}
35 : ${CERTDESTDIR:=${DESTDIR}/etc/ssl/certs}
36 : ${BLACKLISTDESTDIR:=${DESTDIR}/etc/ssl/blacklisted}
37 : ${FILEPAT:="\.pem$|\.crt$|\.cer$|\.crl$|\.0$"}
40 ############################################################ GLOBALS
46 ############################################################ FUNCTIONS
52 if hash=$( openssl x509 -noout -subject_hash -in "$1" ); then
57 ERRORS=$(( $ERRORS + 1 ))
66 hash=$( do_hash "$1" ) || return
67 if [ -e "$BLACKLISTDESTDIR/$hash.0" ]; then
68 echo "Skipping blacklisted certificate $1 ($BLACKLISTDESTDIR/$hash.0)"
71 [ $VERBOSE -gt 0 ] && echo "Adding $hash.0 to trust store"
72 [ $NOOP -eq 0 ] && install -lrs $(realpath "$1") "$CERTDESTDIR/$hash.0"
77 local hash srcfile filename
79 # If it exists as a file, we'll try that; otherwise, we'll scan
81 hash=$( do_hash "$1" ) || return
82 srcfile=$(realpath "$1")
84 elif [ -e "${CERTDESTDIR}/$1" ]; then
85 srcfile=$(realpath "${CERTDESTDIR}/$1")
90 [ $VERBOSE -gt 0 ] && echo "Adding $filename to blacklist"
91 [ $NOOP -eq 0 ] && install -lrs "$srcfile" "$BLACKLISTDESTDIR/$filename"
96 local CFUNC CSEARCH CPATH CFILE
104 for CPATH in "$@"; do
105 [ -d "$CPATH" ] || continue
106 echo "Scanning $CPATH for certificates..."
107 for CFILE in $(ls -1 "${CPATH}" | grep -Ee "${FILEPAT}"); do
108 [ -e "$CPATH/$CFILE" ] || continue
109 [ $VERBOSE -gt 0 ] && echo "Reading $CFILE"
110 "$CFUNC" "$CPATH/$CFILE"
122 if [ ! -s "$CFILE" ]; then
123 echo "Unable to read $CFILE" >&2
124 ERRORS=$(( $ERRORS + 1 ))
128 if [ $VERBOSE -eq 0 ]; then
129 subject=$( openssl x509 -noout -subject -nameopt multiline -in "$CFILE" |
130 sed -n '/commonName/s/.*= //p' )
133 subject=$( openssl x509 -noout -subject -in "$CFILE" )
134 printf "%s\t%s\n" "$CFILE" "$subject"
143 if [ $NOOP -eq 0 ]; then
144 if [ -e "$CERTDESTDIR" ]; then
145 find "$CERTDESTDIR" -type link -delete
147 mkdir -p "$CERTDESTDIR"
149 if [ -e "$BLACKLISTDESTDIR" ]; then
150 find "$BLACKLISTDESTDIR" -type link -delete
152 mkdir -p "$BLACKLISTDESTDIR"
156 do_scan create_blacklisted "$BLACKLISTPATH"
157 do_scan create_trusted_link "$TRUSTPATH"
162 echo "Listing Trusted Certificates:"
163 do_list "$CERTDESTDIR"
171 [ $NOOP -eq 0 ] && mkdir -p "$BLACKLISTDESTDIR"
172 for BFILE in "$@"; do
173 echo "Adding $BFILE to blacklist"
174 create_blacklisted "$BFILE"
183 for BFILE in "$@"; do
184 if [ -s "$BFILE" ]; then
185 hash=$( do_hash "$BFILE" )
186 echo "Removing $hash.0 from blacklist"
187 [ $NOOP -eq 0 ] && rm -f "$BLACKLISTDESTDIR/$hash.0"
188 elif [ -e "$BLACKLISTDESTDIR/$BFILE" ]; then
189 echo "Removing $BFILE from blacklist"
190 [ $NOOP -eq 0 ] && rm -f "$BLACKLISTDESTDIR/$BFILE"
192 echo "Cannot find $BFILE" >&2
193 ERRORS=$(( $ERRORS + 1 ))
200 echo "Listing Blacklisted Certificates:"
201 do_list "$BLACKLISTDESTDIR"
207 echo "Manage the TLS trusted certificates on the system"
208 echo " $SCRIPTNAME [-v] list"
209 echo " List trusted certificates"
210 echo " $SCRIPTNAME [-v] blacklisted"
211 echo " List blacklisted certificates"
212 echo " $SCRIPTNAME [-nv] rehash"
213 echo " Generate hash links for all certificates"
214 echo " $SCRIPTNAME [-nv] blacklist <file>"
215 echo " Add <file> to the list of blacklisted certificates"
216 echo " $SCRIPTNAME [-nv] unblacklist <file>"
217 echo " Remove <file> from the list of blacklisted certificates"
221 ############################################################ MAIN
223 while getopts nv flag; do
226 v) VERBOSE=$(( $VERBOSE + 1 )) ;;
229 shift $(( $OPTIND - 1 ))
231 [ $# -gt 0 ] || usage
234 rehash) cmd_rehash ;;
235 blacklist) cmd_blacklist "$@" ;;
236 unblacklist) cmd_unblacklist "$@" ;;
237 blacklisted) cmd_blacklisted ;;
238 *) usage # NOTREACHED
242 [ $ERRORS -gt 0 ] && echo "Encountered $ERRORS errors" >&2
245 ################################################################################
247 ################################################################################