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 : ${FILEPAT:="\.pem$|\.crt$|\.cer$|\.crl$|\.0$"}
36 ############################################################ GLOBALS
43 ############################################################ FUNCTIONS
49 if hash=$( openssl x509 -noout -subject_hash -in "$1" ); then
54 ERRORS=$(( $ERRORS + 1 ))
63 hash=$( do_hash "$1" ) || return
64 if [ -e "$BLACKLISTDESTDIR/$hash.0" ]; then
65 echo "Skipping blacklisted certificate $1 ($BLACKLISTDESTDIR/$hash.0)"
68 [ $VERBOSE -gt 0 ] && echo "Adding $hash.0 to trust store"
69 [ $NOOP -eq 0 ] && install ${INSTALLFLAGS} -lrs $(realpath "$1") "$CERTDESTDIR/$hash.0"
74 local hash srcfile filename
76 # If it exists as a file, we'll try that; otherwise, we'll scan
78 hash=$( do_hash "$1" ) || return
79 srcfile=$(realpath "$1")
81 elif [ -e "${CERTDESTDIR}/$1" ]; then
82 srcfile=$(realpath "${CERTDESTDIR}/$1")
87 [ $VERBOSE -gt 0 ] && echo "Adding $filename to blacklist"
88 [ $NOOP -eq 0 ] && install ${INSTALLFLAGS} -lrs "$srcfile" "$BLACKLISTDESTDIR/$filename"
93 local CFUNC CSEARCH CPATH CFILE
101 for CPATH in "$@"; do
102 [ -d "$CPATH" ] || continue
103 echo "Scanning $CPATH for certificates..."
104 for CFILE in $(ls -1 "${CPATH}" | grep -Ee "${FILEPAT}"); do
105 [ -e "$CPATH/$CFILE" -a $UNPRIV -eq 0 ] || continue
106 [ $VERBOSE -gt 0 ] && echo "Reading $CFILE"
107 "$CFUNC" "$CPATH/$CFILE"
119 if [ ! -s "$CFILE" ]; then
120 echo "Unable to read $CFILE" >&2
121 ERRORS=$(( $ERRORS + 1 ))
125 if [ $VERBOSE -eq 0 ]; then
126 subject=$( openssl x509 -noout -subject -nameopt multiline -in "$CFILE" |
127 sed -n '/commonName/s/.*= //p' )
130 subject=$( openssl x509 -noout -subject -in "$CFILE" )
131 printf "%s\t%s\n" "$CFILE" "$subject"
140 if [ $NOOP -eq 0 ]; then
141 if [ -e "$CERTDESTDIR" ]; then
142 find "$CERTDESTDIR" -type link -delete
144 mkdir -p "$CERTDESTDIR"
146 if [ -e "$BLACKLISTDESTDIR" ]; then
147 find "$BLACKLISTDESTDIR" -type link -delete
149 mkdir -p "$BLACKLISTDESTDIR"
153 do_scan create_blacklisted "$BLACKLISTPATH"
154 do_scan create_trusted_link "$TRUSTPATH"
159 echo "Listing Trusted Certificates:"
160 do_list "$CERTDESTDIR"
168 [ $NOOP -eq 0 ] && mkdir -p "$BLACKLISTDESTDIR"
169 for BFILE in "$@"; do
170 echo "Adding $BFILE to blacklist"
171 create_blacklisted "$BFILE"
180 for BFILE in "$@"; do
181 if [ -s "$BFILE" ]; then
182 hash=$( do_hash "$BFILE" )
183 echo "Removing $hash.0 from blacklist"
184 [ $NOOP -eq 0 ] && rm -f "$BLACKLISTDESTDIR/$hash.0"
185 elif [ -e "$BLACKLISTDESTDIR/$BFILE" ]; then
186 echo "Removing $BFILE from blacklist"
187 [ $NOOP -eq 0 ] && rm -f "$BLACKLISTDESTDIR/$BFILE"
189 echo "Cannot find $BFILE" >&2
190 ERRORS=$(( $ERRORS + 1 ))
197 echo "Listing Blacklisted Certificates:"
198 do_list "$BLACKLISTDESTDIR"
204 echo "Manage the TLS trusted certificates on the system"
205 echo " $SCRIPTNAME [-v] list"
206 echo " List trusted certificates"
207 echo " $SCRIPTNAME [-v] blacklisted"
208 echo " List blacklisted certificates"
209 echo " $SCRIPTNAME [-nUv] [-D <destdir>] [-M <metalog>] rehash"
210 echo " Generate hash links for all certificates"
211 echo " $SCRIPTNAME [-nv] blacklist <file>"
212 echo " Add <file> to the list of blacklisted certificates"
213 echo " $SCRIPTNAME [-nv] unblacklist <file>"
214 echo " Remove <file> from the list of blacklisted certificates"
218 ############################################################ MAIN
220 while getopts D:M:nUv flag; do
222 D) DESTDIR=${OPTARG} ;;
223 M) METALOG=${OPTARG} ;;
226 v) VERBOSE=$(( $VERBOSE + 1 )) ;;
229 shift $(( $OPTIND - 1 ))
231 : ${METALOG:=${DESTDIR}/METALOG}
233 [ $UNPRIV -eq 1 ] && INSTALLFLAGS=-U -M ${METALOG} -D ${DESTDIR}
234 : ${TRUSTPATH:=${DESTDIR}/usr/share/certs/trusted:${DESTDIR}/usr/local/share/certs:${DESTDIR}/usr/local/etc/ssl/certs}
235 : ${BLACKLISTPATH:=${DESTDIR}/usr/share/certs/blacklisted:${DESTDIR}/usr/local/etc/ssl/blacklisted}
236 : ${CERTDESTDIR:=${DESTDIR}/etc/ssl/certs}
237 : ${BLACKLISTDESTDIR:=${DESTDIR}/etc/ssl/blacklisted}
239 [ $# -gt 0 ] || usage
242 rehash) cmd_rehash ;;
243 blacklist) cmd_blacklist "$@" ;;
244 unblacklist) cmd_unblacklist "$@" ;;
245 blacklisted) cmd_blacklisted ;;
246 *) usage # NOTREACHED
250 [ $ERRORS -gt 0 ] && echo "Encountered $ERRORS errors" >&2
253 ################################################################################
255 ################################################################################