3 # Copyright (c) 2008, 2009, 2010 Nicira Networks, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
24 log='@LOGDIR@/ovs-pki.log'
28 # This option-parsing mechanism borrowed from a Autoconf-generated
29 # configure script under the following license:
31 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
32 # 2002, 2003, 2004, 2005, 2006, 2009 Free Software Foundation, Inc.
33 # This configure script is free software; the Free Software Foundation
34 # gives unlimited permission to copy, distribute and modify it.
36 # If the previous option needs an argument, assign it.
37 if test -n "$prev"; then
43 *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
47 case $dashdash$option in
52 ovs-pki, for managing a simple OpenFlow public key infrastructure
53 usage: $0 [OPTION...] COMMAND [ARG...]
55 The valid stand-alone commands and their arguments are:
56 init Initialize the PKI
57 req NAME Create new private key and certificate request
58 named NAME-privkey.pem and NAME-req.pem, resp.
59 sign NAME [TYPE] Sign switch certificate request NAME-req.pem,
60 producing certificate NAME-cert.pem
61 req+sign NAME [TYPE] Combine the above two steps, producing all three files.
62 verify NAME [TYPE] Checks that NAME-cert.pem is a valid TYPE certificate
63 fingerprint FILE Prints the fingerprint for FILE
64 self-sign NAME Sign NAME-req.pem with NAME-privkey.pem,
65 producing self-signed certificate NAME-cert.pem
67 The following additional commands manage an online PKI:
68 ls [PREFIX] [TYPE] Lists incoming requests of the given TYPE, optionally
69 limited to those whose fingerprint begins with PREFIX
70 flush [TYPE] Rejects all incoming requests of the given TYPE
71 reject PREFIX [TYPE] Rejects the incoming request(s) whose fingerprint begins
72 with PREFIX and has the given TYPE
73 approve PREFIX [TYPE] Approves the incoming request whose fingerprint begins
74 with PREFIX and has the given TYPE
75 expire [AGE] Rejects all incoming requests older than AGE, in
76 one of the forms Ns, Nmin, Nh, Nday (default: 1day)
77 prompt [TYPE] Interactively prompts to accept or reject each incoming
78 request of the given TYPE
80 Each TYPE above is a certificate type: 'switch' (default) or 'controller'.
82 Options for 'init', 'req', and 'req+sign' only:
83 -k, --key=rsa|dsa Type of keys to use (default: rsa)
84 -B, --bits=NBITS Number of bits in keys (default: 2048). For DSA keys,
85 this has an effect only on 'init'.
86 -D, --dsaparam=FILE File with DSA parameters (DSA only)
87 (default: dsaparam.pem within PKI directory)
88 Options for use with the 'sign' and 'approve' commands:
89 -b, --batch Skip fingerprint verification
90 Options that apply to any command:
91 -d, --dir=DIR Directory where the PKI is located
93 -f, --force Continue even if file or directory already exists
94 -l, --log=FILE Log openssl output to FILE (default: ovs-log.log)
95 -h, --help Print this usage message.
136 echo "unrecognized option $option" >&2
140 if test -z "$command"; then
142 elif test -z "${arg1+set}"; then
144 elif test -z "${arg2+set}"; then
147 echo "$option: only two arguments may be specified" >&2
154 if test -n "$prev"; then
155 option=--`echo $prev | sed 's/_/-/g'`
156 { echo "$as_me: error: missing argument to $option" >&2
157 { (exit 1); exit 1; }; }
159 if test -z "$command"; then
160 echo "$0: missing command name; use --help for help" >&2
163 if test "$keytype" != rsa && test "$keytype" != dsa; then
164 echo "$0: argument to -k or --key must be rsa or dsa"
167 if test "$bits" -lt 1024; then
168 echo "$0: argument to -B or --bits must be at least 1024"
171 if test -z "$dsaparam"; then
172 dsaparam=$pkidir/dsaparam.pem
176 *) $log="$PWD/$log" ;;
179 if test "$command" = "init"; then
180 if test -e "$pkidir" && test "$force" != "yes"; then
181 echo "$0: $pkidir already exists and --force not specified" >&2
185 if test ! -d "$pkidir"; then
191 if test $keytype = dsa && test ! -e dsaparam.pem; then
192 echo "Generating DSA parameters, please wait..." >&2
193 openssl dsaparam -out dsaparam.pem $bits 1>&3 2>&3
196 # Get the current date to add some uniqueness to this certificate
197 curr_date=`date +"%Y %b %d %T"`
200 for ca in controllerca switchca; do
201 echo "Creating $ca..." >&2
206 mkdir -p certs crl newcerts
207 mkdir -p -m 0700 private
208 mkdir -p -m 0733 incoming
210 test -e crlnumber || echo 01 > crlnumber
211 test -e serial || echo 01 > serial
213 # Put DSA parameters in directory.
214 if test $keytype = dsa && test ! -e dsaparam.pem; then
218 # Write CA configuration file.
219 if test ! -e ca.cnf; then
220 sed "s/@ca@/$ca/g;s/@curr_date@/$curr_date/g" > ca.cnf <<'EOF'
223 distinguished_name = req_distinguished_name
225 [ req_distinguished_name ]
231 CN = OVS @ca@ CA Certificate (@curr_date@)
238 database = $dir/index.txt # index file.
239 new_certs_dir = $dir/newcerts # new certs dir
240 certificate = $dir/cacert.pem # The CA cert
241 serial = $dir/serial # serial no file
242 private_key = $dir/private/cakey.pem# CA private key
243 RANDFILE = $dir/private/.rand # random number file
244 default_days = 365 # how long to certify for
245 default_crl_days= 30 # how long before next CRL
246 default_md = md5 # md to use
247 policy = policy # default policy
248 email_in_dn = no # Don't add the email into cert DN
249 name_opt = ca_default # Subject name display option
250 cert_opt = ca_default # Certificate display option
251 copy_extensions = none # Don't copy extensions from request
255 countryName = optional
256 stateOrProvinceName = optional
257 organizationName = match
258 organizationalUnitName = optional
259 commonName = supplied
260 emailAddress = optional
264 # Create certificate authority.
265 if test $keytype = dsa; then
266 newkey=dsa:dsaparam.pem
270 openssl req -config ca.cnf -nodes \
271 -newkey $newkey -keyout private/cakey.pem -out careq.pem \
273 openssl ca -config ca.cnf -create_serial -out cacert.pem \
274 -days 2191 -batch -keyfile private/cakey.pem -selfsign \
275 -infiles careq.pem 1>&3 2>&3
276 chmod 0700 private/cakey.pem
284 if test -z "$arg1" || test -n "$arg2"; then
285 echo "$0: $command must have exactly one argument; use --help for help" >&2
291 if test -n "$arg2"; then
292 echo "$0: $command must have zero or one arguments; use --help for help" >&2
298 if test -z "$arg1"; then
299 echo "$0: $command must have one or two arguments; use --help for help" >&2
305 if test -e "$1" && test "$force" != "yes"; then
306 echo "$0: $1 already exists and --force not supplied" >&2
312 test -n "$type" || exit 123 # Forgot to call check_type?
318 echo "Prefix $arg1 is too short (less than 4 hex digits)"
323 fingerprint=$(cd "$pkidir/${type}ca/incoming" && echo "$1"*-req.pem | sed 's/-req\.pem$//')
326 echo "No certificate requests matching $1"
330 echo "$1 matches more than one certificate request:"
331 echo $fingerprint | sed 's/ /\
339 req="$pkidir/${type}ca/incoming/$fingerprint-req.pem"
340 cert="$pkidir/${type}ca/certs/$fingerprint-cert.pem"
344 TMP=/tmp/ovs-pki.tmp$$
353 date=$(date -r $file)
354 if grep -q -e '-BEGIN CERTIFICATE-' "$file"; then
355 fingerprint=$(openssl x509 -noout -in "$file" -fingerprint |
356 sed 's/SHA1 Fingerprint=//' | tr -d ':')
358 fingerprint=$(sha1sum "$file" | awk '{print $1}')
360 printf "$name\\t$date\\n"
363 printf "\\t(correct fingerprint in filename)\\n"
366 printf "\\tfingerprint $fingerprint\\n"
371 verify_fingerprint() {
373 if test $batch != yes; then
374 echo "Does fingerprint match? (yes/no)"
376 if test "$answer" != yes; then
377 echo "Match failure, aborting" >&2
384 if test x = x"$1"; then
386 elif test "$1" = switch || test "$1" = controller; then
389 echo "$0: type argument must be 'switch' or 'controller'" >&2
395 number=$(echo $1 | sed 's/^\([0-9]\+\)\([[:alpha:]]\+\)/\1/')
396 unit=$(echo $1 | sed 's/^\([0-9]\+\)\([[:alpha:]]\+\)/\2/')
411 echo "$1: age not in the form Ns, Nmin, Nh, Nday (e.g. 1day)" >&2
415 echo $(($number * $factor))
419 if test ! -e "$1"; then
420 echo "$0: $1 does not exist" >&2
425 pkidir_must_exist() {
426 if test ! -e "$pkidir"; then
427 echo "$0: $pkidir does not exist (need to run 'init' or use '--dir'?)" >&2
429 elif test ! -d "$pkidir"; then
430 echo "$0: $pkidir is not a directory" >&2
436 must_not_exist "$arg1-privkey.pem"
437 must_not_exist "$arg1-req.pem"
439 cat > "$TMP/req.cnf" <<EOF
442 distinguished_name = req_distinguished_name
444 [ req_distinguished_name ]
449 OU = Open vSwitch certifier
450 CN = Open vSwitch certificate for $arg1
452 if test $keytype = rsa; then
455 must_exist "$dsaparam"
458 openssl req -config "$TMP/req.cnf" -text -nodes \
459 -newkey $newkey -keyout "$1-privkey.pem" -out "$1-req.pem" 1>&3 2>&3
467 (cd "$pkidir/${type}ca" &&
468 openssl ca -config ca.cnf -batch -in /dev/stdin) \
469 < "$1" > "$2.tmp$$" 2>&3
475 if test "$files" != "$1"; then
481 if test "$command" = req; then
485 fingerprint "$arg1-req.pem"
486 elif test "$command" = sign; then
489 verify_fingerprint "$arg1-req.pem"
491 sign_request "$arg1-req.pem" "$arg2-cert.pem"
492 elif test "$command" = req+sign; then
498 sign_request "$arg1-req.pem" "$arg1-cert.pem"
499 fingerprint "$arg1-req.pem"
500 elif test "$command" = verify; then
502 must_exist "$arg1-cert.pem"
506 openssl verify -CAfile "$pkidir/${type}ca/cacert.pem" "$arg1-cert.pem"
507 elif test "$command" = fingerprint; then
511 elif test "$command" = self-sign; then
513 must_exist "$arg1-req.pem"
514 must_exist "$arg1-privkey.pem"
515 must_not_exist "$arg1-cert.pem"
517 openssl x509 -in "$arg1-req.pem" -out "$arg1-cert.pem" \
518 -signkey "$arg1-privkey.pem" -req -text 2>&3
519 elif test "$command" = ls; then
522 cd "$pkidir/${type}ca/incoming"
523 for file in $(glob "$arg1*-req.pem"); do
526 elif test "$command" = flush; then
529 rm -f "$pkidir/${type}ca/incoming/"*
530 elif test "$command" = reject; then
533 resolve_prefix "$arg1"
536 elif test "$command" = approve; then
539 resolve_prefix "$arg1"
542 cp "$req" "$TMP/$req"
543 verify_fingerprint "$TMP/$req"
544 sign_request "$TMP/$req"
545 rm -f "$req" "$TMP/$req"
546 elif test "$command" = prompt; then
551 cd "$pkidir/${type}ca/incoming"
552 for req in $(glob "*-req.pem"); do
553 cp "$req" "$TMP/$req"
555 cert=$(echo "$pkidir/${type}ca/certs/$req" |
556 sed 's/-req.pem/-cert.pem/')
557 if test -f $cert; then
558 echo "Request $req already approved--dropping duplicate request"
559 rm -f "$req" "$TMP/$req"
565 fingerprint "$TMP/$req" "$req"
566 printf "Disposition for this request (skip/approve/reject)? "
570 echo "Approving $req"
571 sign_request "$TMP/$req" "$cert"
572 rm -f "$req" "$TMP/$req"
575 echo "Rejecting $req"
576 rm -f "$req" "$TMP/$req"
583 elif test "$command" = expire; then
585 cutoff=$(($(date +%s) - $(parse_age ${arg1-1day})))
586 for type in switch controller; do
587 cd "$pkidir/${type}ca/incoming" || exit 1
588 for file in $(glob "*"); do
589 time=$(date -r "$file" +%s)
590 if test "$time" -lt "$cutoff"; then
596 echo "$0: $command command unknown; use --help for help" >&2