makekeys.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/bin/sh
  2. # Helper script to create CA and server certificates.
  3. srcdir=${1-.}
  4. OPENSSL=@OPENSSL@
  5. CONF=${srcdir}/openssl.conf
  6. REQ="${OPENSSL} req -config ${CONF}"
  7. CA="${OPENSSL} ca -config ${CONF} -batch"
  8. # MKCERT makes a self-signed cert
  9. MKCERT="${REQ} -x509 -new -days 900"
  10. REQDN=reqDN
  11. STRMASK=default
  12. CADIR=./ca
  13. export REQDN STRMASK CADIR
  14. asn1date() {
  15. date -d "$1" "+%y%m%d%H%M%SZ"
  16. }
  17. openssl version 1>&2
  18. set -ex
  19. for i in ca ca1 ca2 ca3; do
  20. rm -rf $i
  21. mkdir $i
  22. touch $i/index.txt
  23. echo 01 > $i/serial
  24. ${OPENSSL} genrsa -rand ${srcdir}/../configure 2048 > $i/key.pem
  25. done
  26. ${OPENSSL} genrsa -rand ${srcdir}/../configure 2048 > client.key
  27. ${OPENSSL} genrsa -rand ${srcdir}/../configure 2048 > server.key
  28. ${OPENSSL} dsaparam -genkey -rand ${srcdir}/../configure 1024 > client.dsap
  29. ${OPENSSL} gendsa client.dsap > clientdsa.key
  30. ${MKCERT} -key ca/key.pem -out ca/cert.pem <<EOF
  31. US
  32. California
  33. Oakland
  34. Neosign
  35. Random Dept
  36. nowhere.example.com
  37. [email protected]
  38. EOF
  39. # Function to generate appropriate output for `openssl req'.
  40. csr_fields() {
  41. CN=${2-"localhost"}
  42. OU=${1-"Neon QA Dept"}
  43. Org=${3-"Neon Hackers Ltd"}
  44. Locality=${4-"Cambridge"}
  45. State=${5-"Cambridgeshire"}
  46. cat <<EOF
  47. GB
  48. ${State}
  49. ${Locality}
  50. ${Org}
  51. ${OU}
  52. ${CN}
  53. [email protected]
  54. .
  55. .
  56. EOF
  57. }
  58. # Create intermediary CA
  59. csr_fields IntermediaryCA | ${REQ} -new -key ca2/key.pem -out ca2.csr
  60. ${CA} -extensions caExt -days 3560 -in ca2.csr -out ca2/cert.pem
  61. csr_fields ExpiredCA | ${REQ} -new -key ca1/key.pem -out ca1/cert.csr
  62. csr_fields NotYetValidCA | ${REQ} -new -key ca3/key.pem -out ca3/cert.csr
  63. CADIR=./ca1 ${CA} -name neoncainit -extensions caExt -startdate `asn1date "2 days ago"` -enddate `asn1date "yesterday"` \
  64. -in ca1/cert.csr -keyfile ca1/key.pem -out ca1/cert.pem -selfsign
  65. CADIR=./ca3 ${CA} -name neoncainit -extensions caExt -startdate `asn1date "1 year"` -enddate `asn1date "2 years"` \
  66. -in ca3/cert.csr -keyfile ca3/key.pem -out ca3/cert.pem -selfsign
  67. csr_fields | ${REQ} -new -key server.key -out server.csr
  68. csr_fields | ${REQ} -new -key server.key -out expired.csr
  69. csr_fields | ${REQ} -new -key server.key -out notyet.csr
  70. csr_fields "Upper Case Dept" lOcALhost | \
  71. ${REQ} -new -key server.key -out caseless.csr
  72. csr_fields "Use AltName Dept" nowhere.example.com | \
  73. ${REQ} -new -key server.key -out altname1.csr
  74. csr_fields "Two AltName Dept" nowhere.example.com | \
  75. ${REQ} -new -key server.key -out altname2.csr
  76. csr_fields "Third AltName Dept" nowhere.example.com | \
  77. ${REQ} -new -key server.key -out altname3.csr
  78. csr_fields "Fourth AltName Dept" localhost | \
  79. ${REQ} -new -key server.key -out altname4.csr
  80. csr_fields "Good ipAddress altname Dept" nowhere.example.com | \
  81. ${REQ} -new -key server.key -out altname5.csr
  82. csr_fields "Bad ipAddress altname 1 Dept" nowhere.example.com | \
  83. ${REQ} -new -key server.key -out altname6.csr
  84. csr_fields "Bad ipAddress altname 2 Dept" nowhere.example.com | \
  85. ${REQ} -new -key server.key -out altname7.csr
  86. csr_fields "Bad ipAddress altname 3 Dept" nowhere.example.com | \
  87. ${REQ} -new -key server.key -out altname8.csr
  88. csr_fields "Wildcard Altname Dept 1" | \
  89. ${REQ} -new -key server.key -out altname9.csr
  90. csr_fields "Bad Hostname Department" nohost.example.com | \
  91. ${REQ} -new -key server.key -out wrongcn.csr
  92. csr_fields "Self-Signed" | \
  93. ${MKCERT} -key server.key -out ssigned.pem
  94. # default => T61String
  95. csr_fields "$(printf 'H%bllo World\n' '\0350')" localhost |
  96. ${REQ} -new -key server.key -out t61subj.csr
  97. STRMASK=pkix # => BMPString
  98. csr_fields "$(printf 'H%bllo World\n' '\0350')" localhost |
  99. ${REQ} -new -key server.key -out bmpsubj.csr
  100. STRMASK=utf8only # => UTF8String
  101. csr_fields "$(printf 'H%bllo World\n' '\0350')" localhost |
  102. ${REQ} -new -key server.key -out utf8subj.csr
  103. STRMASK=default
  104. ### produce a set of CA certs
  105. csr_fields "First Random CA" "first.example.com" "CAs Ltd." Lincoln Lincolnshire | \
  106. ${MKCERT} -key server.key -out ca1.pem
  107. csr_fields "Second Random CA" "second.example.com" "CAs Ltd." Falmouth Cornwall | \
  108. ${MKCERT} -key server.key -out ca2.pem
  109. csr_fields "Third Random CA" "third.example.com" "CAs Ltd." Ipswich Suffolk | \
  110. ${MKCERT} -key server.key -out ca3.pem
  111. csr_fields "Fourth Random CA" "fourth.example.com" "CAs Ltd." Norwich Norfolk | \
  112. ${MKCERT} -key server.key -out ca4.pem
  113. cat ca/cert.pem ca[1234].pem > calist.pem
  114. csr_fields "Wildcard Cert Dept" "*.example.com" | \
  115. ${REQ} -new -key server.key -out wildcard.csr
  116. csr_fields "Wildcard IP Cert" "*.0.0.1" | \
  117. ${REQ} -new -key server.key -out wildip.csr
  118. csr_fields "Neon Client Cert" ignored.example.com | \
  119. ${REQ} -new -key client.key -out client.csr
  120. csr_fields "Neon Client Cert" ignored.example.com | \
  121. ${REQ} -new -key clientdsa.key -out clientdsa.csr
  122. ### requests using special DN.
  123. REQDN=reqDN.doubleCN
  124. csr_fields "Double CN Dept" "nohost.example.com
  125. localhost" | ${REQ} -new -key server.key -out twocn.csr
  126. REQDN=reqDN.CNfirst
  127. echo localhost | ${REQ} -new -key server.key -out cnfirst.csr
  128. REQDN=reqDN.missingCN
  129. echo GB | ${REQ} -new -key server.key -out missingcn.csr
  130. REQDN=reqDN.justEmail
  131. echo [email protected] | ${REQ} -new -key server.key -out justmail.csr
  132. # presume AVAs will come out in least->most specific order still...
  133. REQDN=reqDN.twoOU
  134. csr_fields "Second OU Dept
  135. First OU Dept" | ${REQ} -new -key server.key -out twoou.csr
  136. ### don't put ${REQ} invocations after here
  137. for f in server client clientdsa twocn caseless cnfirst \
  138. t61subj bmpsubj utf8subj \
  139. missingcn justmail twoou wildcard wildip wrongcn; do
  140. ${CA} -days 900 -in ${f}.csr -out ${f}.cert
  141. done
  142. ${CA} -startdate `asn1date "2 days ago"` -enddate `asn1date "yesterday"` -in expired.csr -out expired.cert
  143. ${CA} -startdate `asn1date "tomorrow"` -enddate `asn1date "2 days"` -in notyet.csr -out notyet.cert
  144. for n in 1 2 3 4 5 6 9; do
  145. ${CA} -extensions altExt${n} -days 900 \
  146. -in altname${n}.csr -out altname${n}.cert
  147. done
  148. # Sign this CSR using the intermediary CA
  149. CADIR=./ca2 ${CA} -days 900 -in server.csr -out ca2server.cert
  150. # And create a file with the concatenation of both EE and intermediary
  151. # cert.
  152. cat ca2server.cert ca2/cert.pem > ca2server.pem
  153. # sign with expired CA
  154. CADIR=./ca1 ${CA} -days 3 -in server.csr -out ca1server.cert
  155. # sign with not yet valid CA
  156. CADIR=./ca3 ${CA} -days 3 -in server.csr -out ca3server.cert
  157. MKPKCS12="${OPENSSL} pkcs12 -export -passout stdin -in client.cert -inkey client.key"
  158. # generate a PKCS12 cert from the client cert: -passOUT because it's the
  159. # passphrase on the OUTPUT cert, confusing...
  160. echo foobar | ${MKPKCS12} -name "Just A Neon Client Cert" -out client.p12
  161. # generate a PKCS#12 cert with no password and a friendly name
  162. echo | ${MKPKCS12} -name "An Unencrypted Neon Client Cert" -out unclient.p12
  163. # PKCS#12 cert with DSA key
  164. echo | ${OPENSSL} pkcs12 -name "An Unencrypted Neon DSA Client Cert" \
  165. -export -passout stdin \
  166. -in clientdsa.cert -inkey clientdsa.key \
  167. -out dsaclient.p12
  168. # generate a PKCS#12 cert with no friendly name
  169. echo | ${MKPKCS12} -out noclient.p12
  170. # generate a PKCS#12 cert with no private keys
  171. echo | ${MKPKCS12} -nokeys -out nkclient.p12
  172. # generate a PKCS#12 cert without the cert
  173. echo | ${MKPKCS12} -nokeys -out ncclient.p12
  174. # generate an encoded PKCS#12 cert with no private keys
  175. echo foobar | ${MKPKCS12} -nokeys -out enkclient.p12
  176. # a PKCS#12 cert including a bundled CA cert
  177. echo foobar | ${MKPKCS12} -certfile ca/cert.pem -name "A Neon Client Cert With CA" -out clientca.p12
  178. ### a file containing a complete chain
  179. cat ca/cert.pem server.cert > chain.pem
  180. ### NSS database initialization, for testing PKCS#11.
  181. CERTUTIL=@CERTUTIL@
  182. PK12UTIL=@PK12UTIL@
  183. if [ ${CERTUTIL} != "notfound" -a ${PK12UTIL} != "notfound" ]; then
  184. rm -rf nssdb nssdb-dsa
  185. mkdir nssdb nssdb-dsa
  186. echo foobar > nssdb.pw
  187. ${CERTUTIL} -d nssdb -N -f nssdb.pw
  188. ${PK12UTIL} -d nssdb -K foobar -W '' -i unclient.p12
  189. ${CERTUTIL} -d nssdb-dsa -N -f nssdb.pw
  190. ${PK12UTIL} -d nssdb-dsa -K foobar -W '' -i dsaclient.p12
  191. rm -f nssdb.pw
  192. fi