summaryrefslogtreecommitdiff
path: root/src/lib/libssl/src/apps/CA.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/src/apps/CA.sh')
-rw-r--r--src/lib/libssl/src/apps/CA.sh121
1 files changed, 90 insertions, 31 deletions
diff --git a/src/lib/libssl/src/apps/CA.sh b/src/lib/libssl/src/apps/CA.sh
index a0b20d85a9..7ad6b8c52e 100644
--- a/src/lib/libssl/src/apps/CA.sh
+++ b/src/lib/libssl/src/apps/CA.sh
@@ -5,10 +5,10 @@
5# things easier between now and when Eric is convinced to fix it :-) 5# things easier between now and when Eric is convinced to fix it :-)
6# 6#
7# CA -newca ... will setup the right stuff 7# CA -newca ... will setup the right stuff
8# CA -newreq ... will generate a certificate request 8# CA -newreq ... will generate a certificate request
9# CA -sign ... will sign the generated request and output 9# CA -sign ... will sign the generated request and output
10# 10#
11# At the end of that grab newreq.pem and newcert.pem (one has the key 11# At the end of that grab newreq.pem and newcert.pem (one has the key
12# and the other the certificate) and cat them together and that is what 12# and the other the certificate) and cat them together and that is what
13# you want/need ... I'll make even this a little cleaner later. 13# you want/need ... I'll make even this a little cleaner later.
14# 14#
@@ -16,8 +16,8 @@
16# 12-Jan-96 tjh Added more things ... including CA -signcert which 16# 12-Jan-96 tjh Added more things ... including CA -signcert which
17# converts a certificate to a request and then signs it. 17# converts a certificate to a request and then signs it.
18# 10-Jan-96 eay Fixed a few more bugs and added the SSLEAY_CONFIG 18# 10-Jan-96 eay Fixed a few more bugs and added the SSLEAY_CONFIG
19# environment variable so this can be driven from 19# environment variable so this can be driven from
20# a script. 20# a script.
21# 25-Jul-96 eay Cleaned up filenames some more. 21# 25-Jul-96 eay Cleaned up filenames some more.
22# 11-Jun-96 eay Fixed a few filename missmatches. 22# 11-Jun-96 eay Fixed a few filename missmatches.
23# 03-May-96 eay Modified to use 'ssleay cmd' instead of 'cmd'. 23# 03-May-96 eay Modified to use 'ssleay cmd' instead of 'cmd'.
@@ -29,52 +29,87 @@
29 29
30# default openssl.cnf file has setup as per the following 30# default openssl.cnf file has setup as per the following
31# demoCA ... where everything is stored 31# demoCA ... where everything is stored
32cp_pem() {
33 infile=$1
34 outfile=$2
35 bound=$3
36 flag=0
37 exec <$infile;
38 while read line; do
39 if [ $flag -eq 1 ]; then
40 echo $line|grep "^-----END.*$bound" 2>/dev/null 1>/dev/null
41 if [ $? -eq 0 ] ; then
42 echo $line >>$outfile
43 break
44 else
45 echo $line >>$outfile
46 fi
47 fi
48
49 echo $line|grep "^-----BEGIN.*$bound" 2>/dev/null 1>/dev/null
50 if [ $? -eq 0 ]; then
51 echo $line >$outfile
52 flag=1
53 fi
54 done
55}
56
57usage() {
58 echo "usage: $0 -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify" >&2
59}
32 60
33if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi 61if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi
34 62
35DAYS="-days 365" # 1 year 63if [ -z "$DAYS" ] ; then DAYS="-days 365" ; fi # 1 year
36CADAYS="-days 1095" # 3 years 64CADAYS="-days 1095" # 3 years
37REQ="$OPENSSL req $SSLEAY_CONFIG" 65REQ="$OPENSSL req $SSLEAY_CONFIG"
38CA="$OPENSSL ca $SSLEAY_CONFIG" 66CA="$OPENSSL ca $SSLEAY_CONFIG"
39VERIFY="$OPENSSL verify" 67VERIFY="$OPENSSL verify"
40X509="$OPENSSL x509" 68X509="$OPENSSL x509"
69PKCS12="openssl pkcs12"
41 70
42CATOP=./demoCA 71if [ -z "$CATOP" ] ; then CATOP=./demoCA ; fi
43CAKEY=./cakey.pem 72CAKEY=./cakey.pem
44CAREQ=./careq.pem 73CAREQ=./careq.pem
45CACERT=./cacert.pem 74CACERT=./cacert.pem
46 75
47for i 76RET=0
48do 77
49case $i in 78while [ "$1" != "" ] ; do
79case $1 in
50-\?|-h|-help) 80-\?|-h|-help)
51 echo "usage: CA -newcert|-newreq|-newca|-sign|-verify" >&2 81 usage
52 exit 0 82 exit 0
53 ;; 83 ;;
54-newcert) 84-newcert)
55 # create a certificate 85 # create a certificate
56 $REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS 86 $REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS
57 RET=$? 87 RET=$?
58 echo "Certificate is in newcert.pem, private key is in newkey.pem" 88 echo "Certificate is in newcert.pem, private key is in newkey.pem"
59 ;; 89 ;;
60-newreq) 90-newreq)
61 # create a certificate request 91 # create a certificate request
62 $REQ -new -keyout newkey.pem -out newreq.pem $DAYS 92 $REQ -new -keyout newkey.pem -out newreq.pem $DAYS
63 RET=$? 93 RET=$?
64 echo "Request is in newreq.pem, private key is in newkey.pem" 94 echo "Request is in newreq.pem, private key is in newkey.pem"
65 ;; 95 ;;
66-newca) 96-newreq-nodes)
97 # create a certificate request
98 $REQ -new -nodes -keyout newreq.pem -out newreq.pem $DAYS
99 RET=$?
100 echo "Request (and private key) is in newreq.pem"
101 ;;
102-newca)
67 # if explicitly asked for or it doesn't exist then setup the directory 103 # if explicitly asked for or it doesn't exist then setup the directory
68 # structure that Eric likes to manage things 104 # structure that Eric likes to manage things
69 NEW="1" 105 NEW="1"
70 if [ "$NEW" -o ! -f ${CATOP}/serial ]; then 106 if [ "$NEW" -o ! -f ${CATOP}/serial ]; then
71 # create the directory hierarchy 107 # create the directory hierarchy
72 mkdir ${CATOP} 108 mkdir -p ${CATOP}
73 mkdir ${CATOP}/certs 109 mkdir -p ${CATOP}/certs
74 mkdir ${CATOP}/crl 110 mkdir -p ${CATOP}/crl
75 mkdir ${CATOP}/newcerts 111 mkdir -p ${CATOP}/newcerts
76 mkdir ${CATOP}/private 112 mkdir -p ${CATOP}/private
77 echo "00" > ${CATOP}/serial
78 touch ${CATOP}/index.txt 113 touch ${CATOP}/index.txt
79 fi 114 fi
80 if [ ! -f ${CATOP}/private/$CAKEY ]; then 115 if [ ! -f ${CATOP}/private/$CAKEY ]; then
@@ -83,37 +118,60 @@ case $i in
83 118
84 # ask user for existing CA certificate 119 # ask user for existing CA certificate
85 if [ "$FILE" ]; then 120 if [ "$FILE" ]; then
86 cp $FILE ${CATOP}/private/$CAKEY 121 cp_pem $FILE ${CATOP}/private/$CAKEY PRIVATE
122 cp_pem $FILE ${CATOP}/$CACERT CERTIFICATE
87 RET=$? 123 RET=$?
124 if [ ! -f "${CATOP}/serial" ]; then
125 $X509 -in ${CATOP}/$CACERT -noout -next_serial \
126 -out ${CATOP}/serial
127 fi
88 else 128 else
89 echo "Making CA certificate ..." 129 echo "Making CA certificate ..."
90 $REQ -new -keyout ${CATOP}/private/$CAKEY \ 130 $REQ -new -keyout ${CATOP}/private/$CAKEY \
91 -out ${CATOP}/$CAREQ 131 -out ${CATOP}/$CAREQ
92 $CA -out ${CATOP}/$CACERT $CADAYS -batch \ 132 $CA -create_serial -out ${CATOP}/$CACERT $CADAYS -batch \
93 -keyfile ${CATOP}/private/$CAKEY -selfsign \ 133 -keyfile ${CATOP}/private/$CAKEY -selfsign \
94 -infiles ${CATOP}/$CAREQ 134 -extensions v3_ca \
135 -infiles ${CATOP}/$CAREQ
95 RET=$? 136 RET=$?
96 fi 137 fi
97 fi 138 fi
98 ;; 139 ;;
99-xsign) 140-xsign)
100 $CA -policy policy_anything -infiles newreq.pem 141 $CA -policy policy_anything -infiles newreq.pem
101 RET=$? 142 RET=$?
102 ;; 143 ;;
103-sign|-signreq) 144-pkcs12)
145 if [ -z "$2" ] ; then
146 CNAME="My Certificate"
147 else
148 CNAME="$2"
149 fi
150 $PKCS12 -in newcert.pem -inkey newreq.pem -certfile ${CATOP}/$CACERT \
151 -out newcert.p12 -export -name "$CNAME"
152 RET=$?
153 exit $RET
154 ;;
155-sign|-signreq)
104 $CA -policy policy_anything -out newcert.pem -infiles newreq.pem 156 $CA -policy policy_anything -out newcert.pem -infiles newreq.pem
105 RET=$? 157 RET=$?
106 cat newcert.pem 158 cat newcert.pem
107 echo "Signed certificate is in newcert.pem" 159 echo "Signed certificate is in newcert.pem"
108 ;; 160 ;;
109-signcert) 161-signCA)
162 $CA -policy policy_anything -out newcert.pem -extensions v3_ca -infiles newreq.pem
163 RET=$?
164 echo "Signed CA certificate is in newcert.pem"
165 ;;
166-signcert)
110 echo "Cert passphrase will be requested twice - bug?" 167 echo "Cert passphrase will be requested twice - bug?"
111 $X509 -x509toreq -in newreq.pem -signkey newreq.pem -out tmp.pem 168 $X509 -x509toreq -in newreq.pem -signkey newreq.pem -out tmp.pem
112 $CA -policy policy_anything -out newcert.pem -infiles tmp.pem 169 $CA -policy policy_anything -out newcert.pem -infiles tmp.pem
170 RET=$?
113 cat newcert.pem 171 cat newcert.pem
114 echo "Signed certificate is in newcert.pem" 172 echo "Signed certificate is in newcert.pem"
115 ;; 173 ;;
116-verify) 174-verify)
117 shift 175 shift
118 if [ -z "$1" ]; then 176 if [ -z "$1" ]; then
119 $VERIFY -CAfile $CATOP/$CACERT newcert.pem 177 $VERIFY -CAfile $CATOP/$CACERT newcert.pem
@@ -127,13 +185,14 @@ case $i in
127 fi 185 fi
128 done 186 done
129 fi 187 fi
130 exit 0 188 exit $RET
131 ;; 189 ;;
132*) 190*)
133 echo "Unknown arg $i"; 191 echo "Unknown arg $i" >&2
192 usage
134 exit 1 193 exit 1
135 ;; 194 ;;
136esac 195esac
196shift
137done 197done
138exit $RET 198exit $RET
139