big svn cleanup
[anytun.git] / src / openvpn / easy-rsa / 2.0 / pkitool
blob5f951622e1468fea03861646e50f90369c579e8d
1 #!/bin/sh
3 # OpenVPN -- An application to securely tunnel IP networks
4 # over a single TCP/UDP port, with support for SSL/TLS-based
5 # session authentication and key exchange,
6 # packet encryption, packet authentication, and
7 # packet compression.
9 # Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License version 2
13 # as published by the Free Software Foundation.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program (see the file COPYING included with this
22 # distribution); if not, write to the Free Software Foundation, Inc.,
23 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # pkitool is a front-end for the openssl tool.
27 # Calling scripts can set the certificate organizational
28 # unit with the KEY_OU environmental variable.
30 PROGNAME=pkitool
31 VERSION=2.0
32 DEBUG=0
34 die()
36 local m="$1"
38 echo "$m" >&2
39 exit 1
42 need_vars()
44 echo ' Please edit the vars script to reflect your configuration,'
45 echo ' then source it with "source ./vars".'
46 echo ' Next, to start with a fresh PKI configuration and to delete any'
47 echo ' previous certificates and keys, run "./clean-all".'
48 echo " Finally, you can run this tool ($PROGNAME) to build certificates/keys."
51 usage()
53 echo "$PROGNAME $VERSION"
54 echo "Usage: $PROGNAME [options...] [common-name]"
55 echo "Options:"
56 echo " --batch : batch mode (default)"
57 echo " --keysize : Set keysize"
58 echo " size : size (default=1024)"
59 echo " --interact : interactive mode"
60 echo " --server : build server cert"
61 echo " --initca : build root CA"
62 echo " --inter : build intermediate CA"
63 echo " --pass : encrypt private key with password"
64 echo " --csr : only generate a CSR, do not sign"
65 echo " --sign : sign an existing CSR"
66 echo " --pkcs12 : generate a combined PKCS#12 file"
67 echo " --pkcs11 : generate certificate on PKCS#11 token"
68 echo " lib : PKCS#11 library"
69 echo " slot : PKCS#11 slot"
70 echo " id : PKCS#11 object id (hex string)"
71 echo " label : PKCS#11 object label"
72 echo "Standalone options:"
73 echo " --pkcs11-slots : list PKCS#11 slots"
74 echo " lib : PKCS#11 library"
75 echo " --pkcs11-objects : list PKCS#11 token objects"
76 echo " lib : PKCS#11 library"
77 echo " slot : PKCS#11 slot"
78 echo " --pkcs11-init : initialize PKCS#11 token DANGEROUS!!!"
79 echo " lib : PKCS#11 library"
80 echo " slot : PKCS#11 slot"
81 echo " label : PKCS#11 token label"
82 echo "Notes:"
83 need_vars
84 echo " In order to use PKCS#11 interface you must have opensc-0.10.0 or higher."
85 echo "Generated files and corresponding OpenVPN directives:"
86 echo '(Files will be placed in the $KEY_DIR directory, defined in ./vars)'
87 echo " ca.crt -> root certificate (--ca)"
88 echo " ca.key -> root key, keep secure (not directly used by OpenVPN)"
89 echo " .crt files -> client/server certificates (--cert)"
90 echo " .key files -> private keys, keep secure (--key)"
91 echo " .csr files -> certificate signing request (not directly used by OpenVPN)"
92 echo " dh1024.pem or dh2048.pem -> Diffie Hellman parameters (--dh)"
93 echo "Examples:"
94 echo " $PROGNAME --initca -> Build root certificate"
95 echo " $PROGNAME --initca --pass -> Build root certificate with password-protected key"
96 echo " $PROGNAME --server server1 -> Build \"server1\" certificate/key"
97 echo " $PROGNAME client1 -> Build \"client1\" certificate/key"
98 echo " $PROGNAME --pass client2 -> Build password-protected \"client2\" certificate/key"
99 echo " $PROGNAME --pkcs12 client3 -> Build \"client3\" certificate/key in PKCS#12 format"
100 echo " $PROGNAME --csr client4 -> Build \"client4\" CSR to be signed by another CA"
101 echo " $PROGNAME --sign client4 -> Sign \"client4\" CSR"
102 echo " $PROGNAME --inter interca -> Build an intermediate key-signing certificate/key"
103 echo " Also see ./inherit-inter script."
104 echo " $PROGNAME --pkcs11 /usr/lib/pkcs11/lib1 0 010203 \"client5 id\" client5"
105 echo " -> Build \"client5\" certificate/key in PKCS#11 token"
106 echo "Typical usage for initial PKI setup. Build myserver, client1, and client2 cert/keys."
107 echo "Protect client2 key with a password. Build DH parms. Generated files in ./keys :"
108 echo " [edit vars with your site-specific info]"
109 echo " source ./vars"
110 echo " ./clean-all"
111 echo " ./build-dh -> takes a long time, consider backgrounding"
112 echo " ./$PROGNAME --initca"
113 echo " ./$PROGNAME --server myserver"
114 echo " ./$PROGNAME client1"
115 echo " ./$PROGNAME --pass client2"
116 echo "Typical usage for adding client cert to existing PKI:"
117 echo " source ./vars"
118 echo " ./$PROGNAME client-new"
121 # Set defaults
122 DO_REQ="1"
123 REQ_EXT=""
124 DO_CA="1"
125 CA_EXT=""
126 DO_P12="0"
127 DO_P11="0"
128 DO_ROOT="0"
129 NODES_REQ="-nodes"
130 NODES_P12=""
131 BATCH="-batch"
132 CA="ca"
133 # must be set or errors of openssl.cnf
134 PKCS11_MODULE_PATH="dummy"
135 PKCS11_PIN="dummy"
137 # Process options
138 while [ $# -gt 0 ]; do
139 case "$1" in
140 --keysize ) KEY_SIZE=$2
141 shift;;
142 --server ) REQ_EXT="$REQ_EXT -extensions server"
143 CA_EXT="$CA_EXT -extensions server" ;;
144 --batch ) BATCH="-batch" ;;
145 --interact ) BATCH="" ;;
146 --inter ) CA_EXT="$CA_EXT -extensions v3_ca" ;;
147 --initca ) DO_ROOT="1" ;;
148 --pass ) NODES_REQ="" ;;
149 --csr ) DO_CA="0" ;;
150 --sign ) DO_REQ="0" ;;
151 --pkcs12 ) DO_P12="1" ;;
152 --pkcs11 ) DO_P11="1"
153 PKCS11_MODULE_PATH="$2"
154 PKCS11_SLOT="$3"
155 PKCS11_ID="$4"
156 PKCS11_LABEL="$5"
157 shift 4;;
159 # standalone
160 --pkcs11-init)
161 PKCS11_MODULE_PATH="$2"
162 PKCS11_SLOT="$3"
163 PKCS11_LABEL="$4"
164 if [ -z "$PKCS11_LABEL" ]; then
165 die "Please specify library name, slot and label"
167 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --init-token --slot "$PKCS11_SLOT" \
168 --label "$PKCS11_LABEL" &&
169 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --init-pin --slot "$PKCS11_SLOT"
170 exit $?;;
171 --pkcs11-slots)
172 PKCS11_MODULE_PATH="$2"
173 if [ -z "$PKCS11_MODULE_PATH" ]; then
174 die "Please specify library name"
176 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --list-slots
177 exit 0;;
178 --pkcs11-objects)
179 PKCS11_MODULE_PATH="$2"
180 PKCS11_SLOT="$3"
181 if [ -z "$PKCS11_SLOT" ]; then
182 die "Please specify library name and slot"
184 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --list-objects --login --slot "$PKCS11_SLOT"
185 exit 0;;
187 # errors
188 --* ) die "$PROGNAME: unknown option: $1" ;;
189 * ) break ;;
190 esac
191 shift
192 done
194 if ! [ -z "$BATCH" ]; then
195 if $OPENSSL version | grep 0.9.6 > /dev/null; then
196 die "Batch mode is unsupported in openssl<0.9.7"
200 if [ $DO_P12 -eq 1 -a $DO_P11 -eq 1 ]; then
201 die "PKCS#11 and PKCS#12 cannot be specified together"
204 if [ $DO_P11 -eq 1 ]; then
205 if ! grep "^pkcs11.*=" "$KEY_CONFIG" > /dev/null; then
206 die "Please edit $KEY_CONFIG and setup PKCS#11 engine"
210 # If we are generating pkcs12, only encrypt the final step
211 if [ $DO_P12 -eq 1 ]; then
212 NODES_P12="$NODES_REQ"
213 NODES_REQ="-nodes"
216 if [ $DO_P11 -eq 1 ]; then
217 if [ -z "$PKCS11_LABEL" ]; then
218 die "PKCS#11 arguments incomplete"
222 # If undefined, set default key expiration intervals
223 if [ -z "$KEY_EXPIRE" ]; then
224 KEY_EXPIRE=3650
226 if [ -z "$CA_EXPIRE" ]; then
227 CA_EXPIRE=3650
230 # Set organizational unit to empty string if undefined
231 if [ -z "$KEY_OU" ]; then
232 KEY_OU=""
235 # Set KEY_CN
236 if [ $DO_ROOT -eq 1 ]; then
237 if [ -z "$KEY_CN" ]; then
238 if [ "$1" ]; then
239 KEY_CN="$1"
240 elif [ "$KEY_ORG" ]; then
241 KEY_CN="$KEY_ORG CA"
244 if [ $BATCH ] && [ "$KEY_CN" ]; then
245 echo "Using CA Common Name:" $KEY_CN
247 elif [ $BATCH ] && [ "$KEY_CN" ] && [ $# -eq 0 ]; then
248 echo "Using Common Name:" $KEY_CN
249 else
250 if [ $# -ne 1 ]; then
251 usage
252 exit 1
253 else
254 KEY_CN="$1"
258 export CA_EXPIRE KEY_EXPIRE KEY_OU KEY_CN PKCS11_MODULE_PATH PKCS11_PIN
260 # Show parameters (debugging)
261 if [ $DEBUG -eq 1 ]; then
262 echo DO_REQ $DO_REQ
263 echo REQ_EXT $REQ_EXT
264 echo DO_CA $DO_CA
265 echo CA_EXT $CA_EXT
266 echo NODES_REQ $NODES_REQ
267 echo NODES_P12 $NODES_P12
268 echo DO_P12 $DO_P12
269 echo KEY_CN $KEY_CN
270 echo BATCH $BATCH
271 echo DO_ROOT $DO_ROOT
272 echo KEY_EXPIRE $KEY_EXPIRE
273 echo CA_EXPIRE $CA_EXPIRE
274 echo KEY_OU $KEY_OU
275 echo DO_P11 $DO_P11
276 echo PKCS11_MODULE_PATH $PKCS11_MODULE_PATH
277 echo PKCS11_SLOT $PKCS11_SLOT
278 echo PKCS11_ID $PKCS11_ID
279 echo PKCS11_LABEL $PKCS11_LABEL
282 # Make sure ./vars was sourced beforehand
283 if [ -d "$KEY_DIR" ] && [ "$KEY_CONFIG" ]; then
284 cd "$KEY_DIR"
286 # Make sure $KEY_CONFIG points to the correct version
287 # of openssl.cnf
288 if $GREP -i 'easy-rsa version 2\.[0-9]' "$KEY_CONFIG" >/dev/null; then
290 else
291 echo "$PROGNAME: KEY_CONFIG (set by the ./vars script) is pointing to the wrong"
292 echo "version of openssl.cnf: $KEY_CONFIG"
293 echo "The correct version should have a comment that says: easy-rsa version 2.x";
294 exit 1;
297 # Build root CA
298 if [ $DO_ROOT -eq 1 ]; then
299 $OPENSSL req $BATCH -days $CA_EXPIRE $NODES_REQ -new -newkey rsa:$KEY_SIZE -sha1 \
300 -x509 -keyout "$CA.key" -out "$CA.crt" -config "$KEY_CONFIG" && \
301 chmod 0600 "$CA.key"
302 else
303 # Make sure CA key/cert is available
304 if [ $DO_CA -eq 1 ] || [ $DO_P12 -eq 1 ]; then
305 if [ ! -r "$CA.crt" ] || [ ! -r "$CA.key" ]; then
306 echo "$PROGNAME: Need a readable $CA.crt and $CA.key in $KEY_DIR"
307 echo "Try $PROGNAME --initca to build a root certificate/key."
308 exit 1
312 # Generate key for PKCS#11 token
313 PKCS11_ARGS=
314 if [ $DO_P11 -eq 1 ]; then
315 stty -echo
316 echo -n "User PIN: "
317 read -r PKCS11_PIN
318 stty echo
319 export PKCS11_PIN
321 echo "Generating key pair on PKCS#11 token..."
322 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --keypairgen \
323 --login --pin "$PKCS11_PIN" \
324 --key-type rsa:1024 \
325 --slot "$PKCS11_SLOT" --id "$PKCS11_ID" --label "$PKCS11_LABEL" || exit 1
326 PKCS11_ARGS="-engine pkcs11 -keyform engine -key $PKCS11_SLOT:$PKCS11_ID"
329 # Build cert/key
330 ( [ $DO_REQ -eq 0 ] || $OPENSSL req $BATCH -days $KEY_EXPIRE $NODES_REQ -new -newkey rsa:$KEY_SIZE \
331 -keyout "$KEY_CN.key" -out "$KEY_CN.csr" $REQ_EXT -config "$KEY_CONFIG" $PKCS11_ARGS ) && \
332 ( [ $DO_CA -eq 0 ] || $OPENSSL ca $BATCH -days $KEY_EXPIRE -out "$KEY_CN.crt" \
333 -in "$KEY_CN.csr" $CA_EXT -md sha1 -config "$KEY_CONFIG" ) && \
334 ( [ $DO_P12 -eq 0 ] || $OPENSSL pkcs12 -export -inkey "$KEY_CN.key" \
335 -in "$KEY_CN.crt" -certfile "$CA.crt" -out "$KEY_CN.p12" $NODES_P12 ) && \
336 ( [ $DO_CA -eq 0 -o $DO_P11 -eq 1 ] || chmod 0600 "$KEY_CN.key" ) && \
337 ( [ $DO_P12 -eq 0 ] || chmod 0600 "$KEY_CN.p12" )
339 # Load certificate into PKCS#11 token
340 if [ $DO_P11 -eq 1 ]; then
341 $OPENSSL x509 -in "$KEY_CN.crt" -inform PEM -out "$KEY_CN.crt.der" -outform DER && \
342 $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --write-object "$KEY_CN.crt.der" --type cert \
343 --login --pin "$PKCS11_PIN" \
344 --slot "$PKCS11_SLOT" --id "$PKCS11_ID" --label "$PKCS11_LABEL"
345 [ -e "$KEY_CN.crt.der" ]; rm "$KEY_CN.crt.der"
350 # Need definitions
351 else
352 need_vars