xref: /minix3/crypto/external/bsd/openssl/dist/demos/ssltest-ecc/ECCcertgen.sh (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc#!/bin/sh
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc# For a list of supported curves, use "apps/openssl ecparam -list_curves".
4*ebfedea0SLionel Sambuc
5*ebfedea0SLionel Sambuc# Path to the openssl distribution
6*ebfedea0SLionel SambucOPENSSL_DIR=../..
7*ebfedea0SLionel Sambuc# Path to the openssl program
8*ebfedea0SLionel SambucOPENSSL_CMD=$OPENSSL_DIR/apps/openssl
9*ebfedea0SLionel Sambuc# Option to find configuration file
10*ebfedea0SLionel SambucOPENSSL_CNF="-config $OPENSSL_DIR/apps/openssl.cnf"
11*ebfedea0SLionel Sambuc# Directory where certificates are stored
12*ebfedea0SLionel SambucCERTS_DIR=./Certs
13*ebfedea0SLionel Sambuc# Directory where private key files are stored
14*ebfedea0SLionel SambucKEYS_DIR=$CERTS_DIR
15*ebfedea0SLionel Sambuc# Directory where combo files (containing a certificate and corresponding
16*ebfedea0SLionel Sambuc# private key together) are stored
17*ebfedea0SLionel SambucCOMBO_DIR=$CERTS_DIR
18*ebfedea0SLionel Sambuc# cat command
19*ebfedea0SLionel SambucCAT=/bin/cat
20*ebfedea0SLionel Sambuc# rm command
21*ebfedea0SLionel SambucRM=/bin/rm
22*ebfedea0SLionel Sambuc# mkdir command
23*ebfedea0SLionel SambucMKDIR=/bin/mkdir
24*ebfedea0SLionel Sambuc# The certificate will expire these many days after the issue date.
25*ebfedea0SLionel SambucDAYS=1500
26*ebfedea0SLionel SambucTEST_CA_CURVE=secp160r1
27*ebfedea0SLionel SambucTEST_CA_FILE=secp160r1TestCA
28*ebfedea0SLionel SambucTEST_CA_DN="/C=US/ST=CA/L=Mountain View/O=Sun Microsystems, Inc./OU=Sun Microsystems Laboratories/CN=Test CA (Elliptic curve secp160r1)"
29*ebfedea0SLionel Sambuc
30*ebfedea0SLionel SambucTEST_SERVER_CURVE=secp160r2
31*ebfedea0SLionel SambucTEST_SERVER_FILE=secp160r2TestServer
32*ebfedea0SLionel SambucTEST_SERVER_DN="/C=US/ST=CA/L=Mountain View/O=Sun Microsystems, Inc./OU=Sun Microsystems Laboratories/CN=Test Server (Elliptic curve secp160r2)"
33*ebfedea0SLionel Sambuc
34*ebfedea0SLionel SambucTEST_CLIENT_CURVE=secp160r2
35*ebfedea0SLionel SambucTEST_CLIENT_FILE=secp160r2TestClient
36*ebfedea0SLionel SambucTEST_CLIENT_DN="/C=US/ST=CA/L=Mountain View/O=Sun Microsystems, Inc./OU=Sun Microsystems Laboratories/CN=Test Client (Elliptic curve secp160r2)"
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc# Generating an EC certificate involves the following main steps
39*ebfedea0SLionel Sambuc# 1. Generating curve parameters (if needed)
40*ebfedea0SLionel Sambuc# 2. Generating a certificate request
41*ebfedea0SLionel Sambuc# 3. Signing the certificate request
42*ebfedea0SLionel Sambuc# 4. [Optional] One can combine the cert and private key into a single
43*ebfedea0SLionel Sambuc#    file and also delete the certificate request
44*ebfedea0SLionel Sambuc
45*ebfedea0SLionel Sambuc$MKDIR -p $CERTS_DIR
46*ebfedea0SLionel Sambuc$MKDIR -p $KEYS_DIR
47*ebfedea0SLionel Sambuc$MKDIR -p $COMBO_DIR
48*ebfedea0SLionel Sambuc
49*ebfedea0SLionel Sambucecho "Generating self-signed CA certificate (on curve $TEST_CA_CURVE)"
50*ebfedea0SLionel Sambucecho "==============================================================="
51*ebfedea0SLionel Sambuc$OPENSSL_CMD ecparam -name $TEST_CA_CURVE -out $TEST_CA_CURVE.pem
52*ebfedea0SLionel Sambuc
53*ebfedea0SLionel Sambuc# Generate a new certificate request in $TEST_CA_FILE.req.pem. A
54*ebfedea0SLionel Sambuc# new ecdsa (actually ECC) key pair is generated on the parameters in
55*ebfedea0SLionel Sambuc# $TEST_CA_CURVE.pem and the private key is saved in $TEST_CA_FILE.key.pem
56*ebfedea0SLionel Sambuc# WARNING: By using the -nodes option, we force the private key to be
57*ebfedea0SLionel Sambuc# stored in the clear (rather than encrypted with a password).
58*ebfedea0SLionel Sambuc$OPENSSL_CMD req $OPENSSL_CNF -nodes -subj "$TEST_CA_DN" \
59*ebfedea0SLionel Sambuc    -keyout $KEYS_DIR/$TEST_CA_FILE.key.pem \
60*ebfedea0SLionel Sambuc    -newkey ec:$TEST_CA_CURVE.pem -new \
61*ebfedea0SLionel Sambuc    -out $CERTS_DIR/$TEST_CA_FILE.req.pem
62*ebfedea0SLionel Sambuc
63*ebfedea0SLionel Sambuc# Sign the certificate request in $TEST_CA_FILE.req.pem using the
64*ebfedea0SLionel Sambuc# private key in $TEST_CA_FILE.key.pem and include the CA extension.
65*ebfedea0SLionel Sambuc# Make the certificate valid for 1500 days from the time of signing.
66*ebfedea0SLionel Sambuc# The certificate is written into $TEST_CA_FILE.cert.pem
67*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -req -days $DAYS \
68*ebfedea0SLionel Sambuc    -in $CERTS_DIR/$TEST_CA_FILE.req.pem \
69*ebfedea0SLionel Sambuc    -extfile $OPENSSL_DIR/apps/openssl.cnf \
70*ebfedea0SLionel Sambuc    -extensions v3_ca \
71*ebfedea0SLionel Sambuc    -signkey $KEYS_DIR/$TEST_CA_FILE.key.pem \
72*ebfedea0SLionel Sambuc    -out $CERTS_DIR/$TEST_CA_FILE.cert.pem
73*ebfedea0SLionel Sambuc
74*ebfedea0SLionel Sambuc# Display the certificate
75*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_CA_FILE.cert.pem -text
76*ebfedea0SLionel Sambuc
77*ebfedea0SLionel Sambuc# Place the certificate and key in a common file
78*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_CA_FILE.cert.pem -issuer -subject \
79*ebfedea0SLionel Sambuc	 > $COMBO_DIR/$TEST_CA_FILE.pem
80*ebfedea0SLionel Sambuc$CAT $KEYS_DIR/$TEST_CA_FILE.key.pem >> $COMBO_DIR/$TEST_CA_FILE.pem
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc# Remove the cert request file (no longer needed)
83*ebfedea0SLionel Sambuc$RM $CERTS_DIR/$TEST_CA_FILE.req.pem
84*ebfedea0SLionel Sambuc
85*ebfedea0SLionel Sambucecho "GENERATING A TEST SERVER CERTIFICATE (on elliptic curve $TEST_SERVER_CURVE)"
86*ebfedea0SLionel Sambucecho "=========================================================================="
87*ebfedea0SLionel Sambuc# Generate parameters for curve $TEST_SERVER_CURVE, if needed
88*ebfedea0SLionel Sambuc$OPENSSL_CMD ecparam -name $TEST_SERVER_CURVE -out $TEST_SERVER_CURVE.pem
89*ebfedea0SLionel Sambuc
90*ebfedea0SLionel Sambuc# Generate a new certificate request in $TEST_SERVER_FILE.req.pem. A
91*ebfedea0SLionel Sambuc# new ecdsa (actually ECC) key pair is generated on the parameters in
92*ebfedea0SLionel Sambuc# $TEST_SERVER_CURVE.pem and the private key is saved in
93*ebfedea0SLionel Sambuc# $TEST_SERVER_FILE.key.pem
94*ebfedea0SLionel Sambuc# WARNING: By using the -nodes option, we force the private key to be
95*ebfedea0SLionel Sambuc# stored in the clear (rather than encrypted with a password).
96*ebfedea0SLionel Sambuc$OPENSSL_CMD req $OPENSSL_CNF -nodes -subj "$TEST_SERVER_DN" \
97*ebfedea0SLionel Sambuc    -keyout $KEYS_DIR/$TEST_SERVER_FILE.key.pem \
98*ebfedea0SLionel Sambuc    -newkey ec:$TEST_SERVER_CURVE.pem -new \
99*ebfedea0SLionel Sambuc    -out $CERTS_DIR/$TEST_SERVER_FILE.req.pem
100*ebfedea0SLionel Sambuc
101*ebfedea0SLionel Sambuc# Sign the certificate request in $TEST_SERVER_FILE.req.pem using the
102*ebfedea0SLionel Sambuc# CA certificate in $TEST_CA_FILE.cert.pem and the CA private key in
103*ebfedea0SLionel Sambuc# $TEST_CA_FILE.key.pem. Since we do not have an existing serial number
104*ebfedea0SLionel Sambuc# file for this CA, create one. Make the certificate valid for $DAYS days
105*ebfedea0SLionel Sambuc# from the time of signing. The certificate is written into
106*ebfedea0SLionel Sambuc# $TEST_SERVER_FILE.cert.pem
107*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -req -days $DAYS \
108*ebfedea0SLionel Sambuc    -in $CERTS_DIR/$TEST_SERVER_FILE.req.pem \
109*ebfedea0SLionel Sambuc    -CA $CERTS_DIR/$TEST_CA_FILE.cert.pem \
110*ebfedea0SLionel Sambuc    -CAkey $KEYS_DIR/$TEST_CA_FILE.key.pem \
111*ebfedea0SLionel Sambuc    -out $CERTS_DIR/$TEST_SERVER_FILE.cert.pem -CAcreateserial
112*ebfedea0SLionel Sambuc
113*ebfedea0SLionel Sambuc# Display the certificate
114*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_SERVER_FILE.cert.pem -text
115*ebfedea0SLionel Sambuc
116*ebfedea0SLionel Sambuc# Place the certificate and key in a common file
117*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_SERVER_FILE.cert.pem -issuer -subject \
118*ebfedea0SLionel Sambuc	 > $COMBO_DIR/$TEST_SERVER_FILE.pem
119*ebfedea0SLionel Sambuc$CAT $KEYS_DIR/$TEST_SERVER_FILE.key.pem >> $COMBO_DIR/$TEST_SERVER_FILE.pem
120*ebfedea0SLionel Sambuc
121*ebfedea0SLionel Sambuc# Remove the cert request file (no longer needed)
122*ebfedea0SLionel Sambuc$RM $CERTS_DIR/$TEST_SERVER_FILE.req.pem
123*ebfedea0SLionel Sambuc
124*ebfedea0SLionel Sambucecho "GENERATING A TEST CLIENT CERTIFICATE (on elliptic curve $TEST_CLIENT_CURVE)"
125*ebfedea0SLionel Sambucecho "=========================================================================="
126*ebfedea0SLionel Sambuc# Generate parameters for curve $TEST_CLIENT_CURVE, if needed
127*ebfedea0SLionel Sambuc$OPENSSL_CMD ecparam -name $TEST_CLIENT_CURVE -out $TEST_CLIENT_CURVE.pem
128*ebfedea0SLionel Sambuc
129*ebfedea0SLionel Sambuc# Generate a new certificate request in $TEST_CLIENT_FILE.req.pem. A
130*ebfedea0SLionel Sambuc# new ecdsa (actually ECC) key pair is generated on the parameters in
131*ebfedea0SLionel Sambuc# $TEST_CLIENT_CURVE.pem and the private key is saved in
132*ebfedea0SLionel Sambuc# $TEST_CLIENT_FILE.key.pem
133*ebfedea0SLionel Sambuc# WARNING: By using the -nodes option, we force the private key to be
134*ebfedea0SLionel Sambuc# stored in the clear (rather than encrypted with a password).
135*ebfedea0SLionel Sambuc$OPENSSL_CMD req $OPENSSL_CNF -nodes -subj "$TEST_CLIENT_DN" \
136*ebfedea0SLionel Sambuc	     -keyout $KEYS_DIR/$TEST_CLIENT_FILE.key.pem \
137*ebfedea0SLionel Sambuc	     -newkey ec:$TEST_CLIENT_CURVE.pem -new \
138*ebfedea0SLionel Sambuc	     -out $CERTS_DIR/$TEST_CLIENT_FILE.req.pem
139*ebfedea0SLionel Sambuc
140*ebfedea0SLionel Sambuc# Sign the certificate request in $TEST_CLIENT_FILE.req.pem using the
141*ebfedea0SLionel Sambuc# CA certificate in $TEST_CA_FILE.cert.pem and the CA private key in
142*ebfedea0SLionel Sambuc# $TEST_CA_FILE.key.pem. Since we do not have an existing serial number
143*ebfedea0SLionel Sambuc# file for this CA, create one. Make the certificate valid for $DAYS days
144*ebfedea0SLionel Sambuc# from the time of signing. The certificate is written into
145*ebfedea0SLionel Sambuc# $TEST_CLIENT_FILE.cert.pem
146*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -req -days $DAYS \
147*ebfedea0SLionel Sambuc    -in $CERTS_DIR/$TEST_CLIENT_FILE.req.pem \
148*ebfedea0SLionel Sambuc    -CA $CERTS_DIR/$TEST_CA_FILE.cert.pem \
149*ebfedea0SLionel Sambuc    -CAkey $KEYS_DIR/$TEST_CA_FILE.key.pem \
150*ebfedea0SLionel Sambuc    -out $CERTS_DIR/$TEST_CLIENT_FILE.cert.pem -CAcreateserial
151*ebfedea0SLionel Sambuc
152*ebfedea0SLionel Sambuc# Display the certificate
153*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_CLIENT_FILE.cert.pem -text
154*ebfedea0SLionel Sambuc
155*ebfedea0SLionel Sambuc# Place the certificate and key in a common file
156*ebfedea0SLionel Sambuc$OPENSSL_CMD x509 -in $CERTS_DIR/$TEST_CLIENT_FILE.cert.pem -issuer -subject \
157*ebfedea0SLionel Sambuc	 > $COMBO_DIR/$TEST_CLIENT_FILE.pem
158*ebfedea0SLionel Sambuc$CAT $KEYS_DIR/$TEST_CLIENT_FILE.key.pem >> $COMBO_DIR/$TEST_CLIENT_FILE.pem
159*ebfedea0SLionel Sambuc
160*ebfedea0SLionel Sambuc# Remove the cert request file (no longer needed)
161*ebfedea0SLionel Sambuc$RM $CERTS_DIR/$TEST_CLIENT_FILE.req.pem
162*ebfedea0SLionel Sambuc
163*ebfedea0SLionel Sambuc
164*ebfedea0SLionel Sambuc
165