]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/certctl/certctl.sh
MFC r361022-r361023, r361148: certctl(8) fixes
[FreeBSD/FreeBSD.git] / usr.sbin / certctl / certctl.sh
1 #!/bin/sh
2 #-
3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 #
5 # Copyright 2018 Allan Jude <allanjude@freebsd.org>
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted providing that the following conditions 
9 # are met:
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.
15 #
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.
27 #
28 # $FreeBSD$
29
30 ############################################################ CONFIGURATION
31
32 : ${DESTDIR:=}
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$"}
38 : ${VERBOSE:=0}
39
40 ############################################################ GLOBALS
41
42 SCRIPTNAME="${0##*/}"
43 ERRORS=0
44 NOOP=0
45
46 ############################################################ FUNCTIONS
47
48 do_hash()
49 {
50         local hash
51
52         if hash=$( openssl x509 -noout -subject_hash -in "$1" ); then
53                 echo "$hash"
54                 return 0
55         else
56                 echo "Error: $1" >&2
57                 ERRORS=$(( $ERRORS + 1 ))
58                 return 1
59         fi
60 }
61
62 create_trusted_link()
63 {
64         local hash
65
66         hash=$( do_hash "$1" ) || return
67         if [ -e "$BLACKLISTDESTDIR/$hash.0" ]; then
68                 echo "Skipping blacklisted certificate $1 ($BLACKLISTDESTDIR/$hash.0)"
69                 return 1
70         fi
71         [ $VERBOSE -gt 0 ] && echo "Adding $hash.0 to trust store"
72         [ $NOOP -eq 0 ] && install -lrs $(realpath "$1") "$CERTDESTDIR/$hash.0"
73 }
74
75 create_blacklisted()
76 {
77         local hash srcfile filename
78
79         # If it exists as a file, we'll try that; otherwise, we'll scan
80         if [ -e "$1" ]; then
81                 hash=$( do_hash "$1" ) || return
82                 srcfile=$(realpath "$1")
83                 filename="$hash.0"
84         elif [ -e "${CERTDESTDIR}/$1" ];  then
85                 srcfile=$(realpath "${CERTDESTDIR}/$1")
86                 filename="$1"
87         else
88                 return
89         fi
90         [ $VERBOSE -gt 0 ] && echo "Adding $filename to blacklist"
91         [ $NOOP -eq 0 ] && install -lrs "$srcfile" "$BLACKLISTDESTDIR/$filename"
92 }
93
94 do_scan()
95 {
96         local CFUNC CSEARCH CPATH CFILE
97         local oldIFS="$IFS"
98         CFUNC="$1"
99         CSEARCH="$2"
100
101         IFS=:
102         set -- $CSEARCH
103         IFS="$oldIFS"
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"
111                 done
112         done
113 }
114
115 do_list()
116 {
117         local CFILE subject
118
119         if [ -e "$1" ]; then
120                 cd "$1"
121                 for CFILE in *.0; do
122                         if [ ! -s "$CFILE" ]; then
123                                 echo "Unable to read $CFILE" >&2
124                                 ERRORS=$(( $ERRORS + 1 ))
125                                 continue
126                         fi
127                         subject=
128                         if [ $VERBOSE -eq 0 ]; then
129                                 subject=$( openssl x509 -noout -subject -nameopt multiline -in "$CFILE" |
130                                     sed -n '/commonName/s/.*= //p' )
131                         fi
132                         [ "$subject" ] ||
133                             subject=$( openssl x509 -noout -subject -in "$CFILE" )
134                         printf "%s\t%s\n" "$CFILE" "$subject"
135                 done
136                 cd -
137         fi
138 }
139
140 cmd_rehash()
141 {
142
143         if [ $NOOP -eq 0 ]; then
144                 if [ -e "$CERTDESTDIR" ]; then
145                         find "$CERTDESTDIR" -type link -delete
146                 else
147                         mkdir -p "$CERTDESTDIR"
148                 fi
149                 if [ -e "$BLACKLISTDESTDIR" ]; then
150                         find "$BLACKLISTDESTDIR" -type link -delete
151                 else
152                         mkdir -p "$BLACKLISTDESTDIR"
153                 fi
154         fi
155
156         do_scan create_blacklisted "$BLACKLISTPATH"
157         do_scan create_trusted_link "$TRUSTPATH"
158 }
159
160 cmd_list()
161 {
162         echo "Listing Trusted Certificates:"
163         do_list "$CERTDESTDIR"
164 }
165
166 cmd_blacklist()
167 {
168         local BPATH
169
170         shift # verb
171         [ $NOOP -eq 0 ] && mkdir -p "$BLACKLISTDESTDIR"
172         for BFILE in "$@"; do
173                 echo "Adding $BFILE to blacklist"
174                 create_blacklisted "$BFILE"
175         done
176 }
177
178 cmd_unblacklist()
179 {
180         local BFILE hash
181
182         shift # verb
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"
191                 else
192                         echo "Cannot find $BFILE" >&2
193                         ERRORS=$(( $ERRORS + 1 ))
194                 fi
195         done
196 }
197
198 cmd_blacklisted()
199 {
200         echo "Listing Blacklisted Certificates:"
201         do_list "$BLACKLISTDESTDIR"
202 }
203
204 usage()
205 {
206         exec >&2
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"
218         exit 64
219 }
220
221 ############################################################ MAIN
222
223 while getopts nv flag; do
224         case "$flag" in
225         n) NOOP=1 ;;
226         v) VERBOSE=$(( $VERBOSE + 1 )) ;;
227         esac
228 done
229 shift $(( $OPTIND - 1 ))
230
231 [ $# -gt 0 ] || usage
232 case "$1" in
233 list)           cmd_list ;;
234 rehash)         cmd_rehash ;;
235 blacklist)      cmd_blacklist "$@" ;;
236 unblacklist)    cmd_unblacklist "$@" ;;
237 blacklisted)    cmd_blacklisted ;;
238 *)              usage # NOTREACHED
239 esac
240
241 retval=$?
242 [ $ERRORS -gt 0 ] && echo "Encountered $ERRORS errors" >&2
243 exit $retval
244
245 ################################################################################
246 # END
247 ################################################################################