12 # This option-parsing mechanism borrowed from a Autoconf-generated
13 # configure script under the following license:
15 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
16 # 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
17 # This configure script is free software; the Free Software Foundation
18 # gives unlimited permission to copy, distribute and modify it.
20 # If the previous option needs an argument, assign it.
21 if test -n "$prev"; then
27 *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
31 case $dashdash$option in
36 ofp-pki, for managing a simple OpenFlow public key infrastructure
37 usage: $0 [OPTION...] COMMAND [ARG...]
39 The valid stand-alone commands and their arguments are:
40 init Initialize the PKI
41 req NAME Create new private key and certificate request
42 named NAME-privkey.pem and NAME-req.pem, resp.
43 sign NAME [TYPE] Sign switch certificate request NAME-req.pem,
44 producing certificate NAME-cert.pem
45 req+sign NAME [TYPE] Combine the above two steps, producing all three files.
46 verify NAME [TYPE] Checks that NAME-cert.pem is a valid TYPE certificate
47 fingerprint FILE Prints the fingerprint for FILE
49 The following additional commands manage an online PKI:
50 ls [PREFIX] [TYPE] Lists incoming requests of the given TYPE, optionally
51 limited to those whose fingerprint begins with PREFIX
52 flush [TYPE] Rejects all incoming requests of the given TYPE
53 reject PREFIX [TYPE] Rejects the incoming request(s) whose fingerprint begins
54 with PREFIX and has the given TYPE
55 approve PREFIX [TYPE] Approves the incoming request whose fingerprint begins
56 with PREFIX and has the given TYPE
57 expire [AGE] Rejects all incoming requests older than AGE, in
58 one of the forms Ns, Nmin, Nh, Nday (default: 1day)
59 prompt [TYPE] Interactively prompts to accept or reject each incoming
60 request of the given TYPE
62 Each TYPE above is a certificate type: 'switch' (default) or 'controller'.
64 Options for 'init', 'req', and 'req+sign' only:
65 -k, --key=rsa|dsa Type of keys to use (default: rsa)
66 -B, --bits=NBITS Number of bits in keys (default: 2048). For DSA keys,
67 this has an effect only on 'init'.
68 -D, --dsaparam=FILE File with DSA parameters (DSA only)
69 (default: dsaparam.pem within PKI directory)
70 Options for use with the 'sign' and 'approve' commands:
71 -b, --batch Skip fingerprint verification
72 Options that apply to any command:
73 -d, --dir=DIR Directory where the PKI is located
75 -f, --force Continue even if file or directory already exists
76 -l, --log=FILE Log openssl output to FILE (default: ofp-log.log)
77 -h, --help Print this usage message.
118 echo "unrecognized option $option" >&2
122 if test -z "$command"; then
124 elif test -z "${arg1+set}"; then
126 elif test -z "${arg2+set}"; then
129 echo "$option: only two arguments may be specified" >&2
136 if test -n "$prev"; then
137 option=--`echo $prev | sed 's/_/-/g'`
138 { echo "$as_me: error: missing argument to $option" >&2
139 { (exit 1); exit 1; }; }
141 if test -z "$command"; then
142 echo "$0: missing command name; use --help for help" >&2
145 if test "$keytype" != rsa && test "$keytype" != dsa; then
146 echo "$0: argument to -k or --key must be rsa or dsa"
149 if test "$bits" -lt 1024; then
150 echo "$0: argument to -B or --bits must be at least 1024"
153 if test -z "$dsaparam"; then
154 dsaparam=$pkidir/dsaparam.pem
157 if test "$command" = "init"; then
158 if test -e "$pkidir" && test "$force" != "yes"; then
159 echo "$0: $pkidir already exists and --force not specified" >&2
163 if test ! -d "$pkidir"; then
169 if test $keytype = dsa && test ! -e dsaparam.pem; then
170 echo "Generating DSA parameters, please wait..." >&2
171 openssl dsaparam -out dsaparam.pem $bits 1>&3 2>&3
175 for ca in controllerca switchca; do
176 echo "Creating $ca..." >&2
181 mkdir -p certs crl newcerts
182 mkdir -p -m 0700 private
183 mkdir -p -m 0733 incoming
185 test -e crlnumber || echo 01 > crlnumber
186 test -e serial || echo 01 > serial
188 # Put DSA parameters in directory.
189 if test $keytype = dsa && test ! -e dsaparam.pem; then
193 # Write CA configuration file.
194 if test ! -e ca.cnf; then
195 sed "s/@ca@/$ca/g" > ca.cnf <<'EOF'
198 distinguished_name = req_distinguished_name
200 [ req_distinguished_name ]
206 CN = OpenFlow @ca@ CA Certificate
213 database = $dir/index.txt # index file.
214 new_certs_dir = $dir/newcerts # new certs dir
215 certificate = $dir/cacert.pem # The CA cert
216 serial = $dir/serial # serial no file
217 private_key = $dir/private/cakey.pem# CA private key
218 RANDFILE = $dir/private/.rand # random number file
219 default_days = 365 # how long to certify for
220 default_crl_days= 30 # how long before next CRL
221 default_md = md5 # md to use
222 policy = policy # default policy
223 email_in_dn = no # Don't add the email into cert DN
224 name_opt = ca_default # Subject name display option
225 cert_opt = ca_default # Certificate display option
226 copy_extensions = none # Don't copy extensions from request
230 countryName = optional
231 stateOrProvinceName = optional
232 organizationName = match
233 organizationalUnitName = optional
234 commonName = supplied
235 emailAddress = optional
239 # Create certificate authority.
240 if test $keytype = dsa; then
241 newkey=dsa:dsaparam.pem
245 openssl req -config ca.cnf -nodes \
246 -newkey $newkey -keyout private/cakey.pem -out careq.pem \
248 openssl ca -config ca.cnf -create_serial -out cacert.pem \
249 -days 1095 -batch -keyfile private/cakey.pem -selfsign \
250 -infiles careq.pem 1>&3 2>&3
251 chmod 0700 private/cakey.pem
259 if test -z "$arg1" || test -n "$arg2"; then
260 echo "$0: $command must have exactly one argument; use --help for help" >&2
266 if test -n "$arg2"; then
267 echo "$0: $command must have zero or one arguments; use --help for help" >&2
273 if test -z "$arg1"; then
274 echo "$0: $command must have one or two arguments; use --help for help" >&2
280 if test -e "$1" && test "$force" != "yes"; then
281 echo "$0: $1 already exists and --force not supplied" >&2
287 test -n "$type" || exit 123 # Forgot to call check_type?
293 echo "Prefix $arg1 is too short (less than 4 hex digits)"
298 fingerprint=$(cd "$pkidir/${type}ca/incoming" && echo "$1"*-req.pem
299 | sed 's/-req\.pem$//')
302 echo "No certificate requests matching $1"
306 echo "$1 matches more than one certificate request:"
307 echo $fingerprint | sed 's/ /\
315 req="$pkidir/${type}ca/incoming/$fingerprint-req.pem"
316 cert="$pkidir/${type}ca/certs/$fingerprint-cert.pem"
320 TMP=/tmp/ofp-pki.tmp$$
329 local date=$(date -r $file)
331 if grep -q -e '-BEGIN CERTIFICATE-' "$file"; then
332 fingerprint=$(openssl x509 -noout -in "$file" -fingerprint |
333 sed 's/SHA1 Fingerprint=//' | tr -d ':')
335 fingerprint=$(sha1sum "$file" | awk '{print $1}')
337 printf "$name\\t$date\\n"
340 printf "\\t(correct fingerprint in filename)\\n"
343 printf "\\tfingerprint $fingerprint\\n"
348 verify_fingerprint() {
350 if test $batch != yes; then
351 echo "Does fingerprint match? (yes/no)"
353 if test "$answer" != yes; then
354 echo "Match failure, aborting" >&2
361 if test x = x"$1"; then
363 elif test "$1" = switch || test "$1" = controller; then
366 echo "$0: type argument must be 'switch' or 'controller'" >&2
372 number=$(echo $1 | sed 's/^\([0-9]\+\)\([[:alpha:]]\+\)/\1/')
373 unit=$(echo $1 | sed 's/^\([0-9]\+\)\([[:alpha:]]\+\)/\2/')
388 echo "$1: age not in the form Ns, Nmin, Nh, Nday (e.g. 1day)" >&2
392 echo $(($number * $factor))
396 if test ! -e "$1"; then
397 echo "$0: $1 does not exist" >&2
402 pkidir_must_exist() {
403 if test ! -e "$pkidir"; then
404 echo "$0: $pkidir does not exist (need to run 'init' or use '--dir'?)" >&2
406 elif test ! -d "$pkidir"; then
407 echo "$0: $pkidir is not a directory" >&2
413 must_not_exist "$arg1-privkey.pem"
414 must_not_exist "$arg1-req.pem"
416 cat > "$TMP/req.cnf" <<EOF
419 distinguished_name = req_distinguished_name
421 [ req_distinguished_name ]
426 OU = OpenFlow certifier
427 CN = OpenFlow certificate for $arg1
429 if test $keytype = rsa; then
432 must_exist "$dsaparam"
435 openssl req -config "$TMP/req.cnf" -text -nodes \
436 -newkey $newkey -keyout "$1-privkey.pem" -out "$1-req.pem" 1>&3 2>&3
444 (cd "$pkidir/${type}ca" &&
445 openssl ca -config ca.cnf -batch -in /dev/stdin) \
446 < "$1" > "$2.tmp$$" 2>&3
451 local files=$(echo $1)
452 if test "$files" != "$1"; then
462 exec 3>>$pkidir/$log || true
466 if test "$command" = req; then
470 fingerprint "$arg1-req.pem"
471 elif test "$command" = sign; then
474 verify_fingerprint "$arg1-req.pem"
476 sign_request "$arg1-req.pem" "$arg2-cert.pem"
477 elif test "$command" = req+sign; then
483 sign_request "$arg1-req.pem" "$arg1-cert.pem"
484 fingerprint "$arg1-req.pem"
485 elif test "$command" = verify; then
487 must_exist "$arg1-cert.pem"
491 openssl verify -CAfile "$pkidir/${type}ca/cacert.pem" "$arg1-cert.pem"
492 elif test "$command" = fingerprint; then
496 elif test "$command" = ls; then
499 cd "$pkidir/${type}ca/incoming"
500 for file in $(glob "$arg1*-req.pem"); do
503 elif test "$command" = flush; then
506 rm -f "$pkidir/${type}ca/incoming/"*
507 elif test "$command" = reject; then
510 resolve_prefix "$arg1"
513 elif test "$command" = approve; then
516 resolve_prefix "$arg1"
519 cp "$req" "$TMP/$req"
520 verify_fingerprint "$TMP/$req"
521 sign_request "$TMP/$req"
522 rm -f "$req" "$TMP/$req"
523 elif test "$command" = prompt; then
528 cd "$pkidir/${type}ca/incoming"
529 for req in $(glob "*-req.pem"); do
530 cp "$req" "$TMP/$req"
532 cert=$(echo "$pkidir/${type}ca/certs/$req" |
533 sed 's/-req.pem/-cert.pem/')
534 if test -f $cert; then
535 echo "Request $req already approved--dropping duplicate request"
536 rm -f "$req" "$TMP/$req"
542 fingerprint "$TMP/$req" "$req"
543 printf "Disposition for this request (skip/approve/reject)? "
547 echo "Approving $req"
548 sign_request "$TMP/$req" "$cert"
549 rm -f "$req" "$TMP/$req"
552 echo "Rejecting $req"
553 rm -f "$req" "$TMP/$req"
560 elif test "$command" = expire; then
562 cutoff=$(($(date +%s) - $(parse_age ${arg1-1day})))
563 for type in switch controller; do
564 cd "$pkidir/${type}ca/incoming" || exit 1
565 for file in $(glob "*"); do
566 time=$(date -r "$file" +%s)
567 if test "$time" -lt "$cutoff"; then
573 echo "$0: $command command unknown; use --help for help" >&2