setup.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/sh
  2. if [ -z "$OPENSSL" ]; then
  3. OPENSSL=openssl
  4. fi
  5. export OPENSSL_CONF=$PWD/openssl.cnf
  6. PASS=whatever
  7. if [ -z "$DOMAIN" ]; then
  8. DOMAIN=w1.fi
  9. fi
  10. COMPANY=w1.fi
  11. OPER_ENG="engw1.fi TESTING USE"
  12. OPER_FI="finw1.fi TESTIKÄYTTÖ"
  13. CNR="Hotspot 2.0 Trust Root CA - 99"
  14. CNO="ocsp.$DOMAIN"
  15. CNV="osu-revoked.$DOMAIN"
  16. CNOC="osu-client.$DOMAIN"
  17. OSU_SERVER_HOSTNAME="osu.$DOMAIN"
  18. DEBUG=0
  19. OCSP_URI="http://$CNO:8888/"
  20. LOGO_URI="http://osu.w1.fi/w1fi_logo.png"
  21. LOGO_HASH256="4532f7ec36424381617c03c6ce87b55a51d6e7177ffafda243cebf280a68954d"
  22. LOGO_HASH1="5e1d5085676eede6b02da14d31c523ec20ffba0b"
  23. # Command line overrides
  24. USAGE=$( cat <<EOF
  25. Usage:\n
  26. # -c: Company name, used to generate Subject name CN for Intermediate CA\n
  27. # -C: Subject name CN of the Root CA ($CNR)\n
  28. # -D: Enable debugging (set -x, etc)\n
  29. # -g: Logo sha1 hash ($LOGO_HASH1)\n
  30. # -G: Logo sha256 hash ($LOGO_HASH256)\n
  31. # -h: Show this help message\n
  32. # -l: Logo URI ($LOGO_URI)\n
  33. # -m: Domain ($DOMAIN)\n
  34. # -o: Subject name CN for OSU-Client Server ($CNOC)\n
  35. # -O: Subject name CN for OCSP Server ($CNO)\n
  36. # -p: passphrase for private keys ($PASS)\n
  37. # -r: Operator-english ($OPER_ENG)\n
  38. # -R: Operator-finish ($OPER_FI)\n
  39. # -S: OSU Server name ($OSU_SERVER_HOSTNAME)\n
  40. # -u: OCSP-URI ($OCSP_URI)\n
  41. # -V: Subject name CN for OSU-Revoked Server ($CNV)\n
  42. EOF
  43. )
  44. while getopts "c:C:Dg:G:l:m:o:O:p:r:R:S:u:V:h" flag
  45. do
  46. case $flag in
  47. c) COMPANY=$OPTARG;;
  48. C) CNR=$OPTARG;;
  49. D) DEBUG=1;;
  50. g) LOGO_HASH1=$OPTARG;;
  51. G) LOGO_HASH256=$OPTARG;;
  52. h) echo -e $USAGE; exit 0;;
  53. l) LOGO_URI=$OPTARG;;
  54. m) DOMAIN=$OPTARG;;
  55. o) CNOC=$OPTARG;;
  56. O) CNO=$OPTARG;;
  57. p) PASS=$OPTARG;;
  58. r) OPER_ENG=$OPTARG;;
  59. R) OPER_FI=$OPTARG;;
  60. S) OSU_SERVER_HOSTNAME=$OPTARG;;
  61. u) OCSP_URI=$OPTARG;;
  62. V) CNV=$OPTARG;;
  63. *) echo "Unknown flag: $flag"; echo -e $USAGE; exit 1;;
  64. esac
  65. done
  66. fail()
  67. {
  68. echo "$*"
  69. exit 1
  70. }
  71. echo
  72. echo "---[ Root CA ]----------------------------------------------------------"
  73. echo
  74. if [ $DEBUG = 1 ]
  75. then
  76. set -x
  77. fi
  78. # Set the passphrase and some other common config accordingly.
  79. cat openssl-root.cnf | sed "s/@PASSWORD@/$PASS/" \
  80. > my-openssl-root.cnf
  81. cat openssl.cnf | sed "s/@PASSWORD@/$PASS/" |
  82. sed "s,@OCSP_URI@,$OCSP_URI," |
  83. sed "s,@LOGO_URI@,$LOGO_URI," |
  84. sed "s,@LOGO_HASH1@,$LOGO_HASH1," |
  85. sed "s,@LOGO_HASH256@,$LOGO_HASH256," |
  86. sed "s/@DOMAIN@/$DOMAIN/" \
  87. > my-openssl.cnf
  88. cat my-openssl-root.cnf | sed "s/#@CN@/commonName_default = $CNR/" > openssl.cnf.tmp
  89. mkdir -p rootCA/certs rootCA/crl rootCA/newcerts rootCA/private
  90. touch rootCA/index.txt
  91. if [ -e rootCA/private/cakey.pem ]; then
  92. echo " * Use existing Root CA"
  93. else
  94. echo " * Generate Root CA private key"
  95. $OPENSSL req -config openssl.cnf.tmp -batch -new -newkey rsa:4096 -keyout rootCA/private/cakey.pem -out rootCA/careq.pem || fail "Failed to generate Root CA private key"
  96. echo " * Sign Root CA certificate"
  97. $OPENSSL ca -config openssl.cnf.tmp -md sha256 -create_serial -out rootCA/cacert.pem -days 10957 -batch -keyfile rootCA/private/cakey.pem -passin pass:$PASS -selfsign -extensions v3_ca -outdir rootCA/newcerts -infiles rootCA/careq.pem || fail "Failed to sign Root CA certificate"
  98. $OPENSSL x509 -in rootCA/cacert.pem -out rootCA/cacert.der -outform DER || fail "Failed to create rootCA DER"
  99. sha256sum rootCA/cacert.der > rootCA/cacert.fingerprint || fail "Failed to create rootCA fingerprint"
  100. fi
  101. if [ ! -e rootCA/crlnumber ]; then
  102. echo 00 > rootCA/crlnumber
  103. fi
  104. echo
  105. echo "---[ Intermediate CA ]--------------------------------------------------"
  106. echo
  107. cat my-openssl.cnf | sed "s/#@CN@/commonName_default = $COMPANY Hotspot 2.0 Intermediate CA/" > openssl.cnf.tmp
  108. mkdir -p demoCA/certs demoCA/crl demoCA/newcerts demoCA/private
  109. touch demoCA/index.txt
  110. if [ -e demoCA/private/cakey.pem ]; then
  111. echo " * Use existing Intermediate CA"
  112. else
  113. echo " * Generate Intermediate CA private key"
  114. $OPENSSL req -config openssl.cnf.tmp -batch -new -newkey rsa:2048 -keyout demoCA/private/cakey.pem -out demoCA/careq.pem || fail "Failed to generate Intermediate CA private key"
  115. echo " * Sign Intermediate CA certificate"
  116. $OPENSSL ca -config openssl.cnf.tmp -md sha256 -create_serial -out demoCA/cacert.pem -days 3652 -batch -keyfile rootCA/private/cakey.pem -cert rootCA/cacert.pem -passin pass:$PASS -extensions v3_ca -infiles demoCA/careq.pem || fail "Failed to sign Intermediate CA certificate"
  117. # horrible from security view point, but for testing purposes since OCSP responder does not seem to support -passin
  118. openssl rsa -in demoCA/private/cakey.pem -out demoCA/private/cakey-plain.pem -passin pass:$PASS
  119. $OPENSSL x509 -in demoCA/cacert.pem -out demoCA/cacert.der -outform DER || fail "Failed to create demoCA DER."
  120. sha256sum demoCA/cacert.der > demoCA/cacert.fingerprint || fail "Failed to create demoCA fingerprint"
  121. fi
  122. if [ ! -e demoCA/crlnumber ]; then
  123. echo 00 > demoCA/crlnumber
  124. fi
  125. echo
  126. echo "OCSP responder"
  127. echo
  128. cat my-openssl.cnf | sed "s/#@CN@/commonName_default = $CNO/" > openssl.cnf.tmp
  129. $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -new -newkey rsa:2048 -nodes -out ocsp.csr -keyout ocsp.key -extensions v3_OCSP
  130. $OPENSSL ca -config $PWD/openssl.cnf.tmp -batch -md sha256 -keyfile demoCA/private/cakey.pem -passin pass:$PASS -in ocsp.csr -out ocsp.pem -days 730 -extensions v3_OCSP || fail "Could not generate ocsp.pem"
  131. echo
  132. echo "---[ Server - to be revoked ] ------------------------------------------"
  133. echo
  134. cat my-openssl.cnf | sed "s/#@CN@/commonName_default = $CNV/" > openssl.cnf.tmp
  135. $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -new -newkey rsa:2048 -nodes -out server-revoked.csr -keyout server-revoked.key
  136. $OPENSSL ca -config $PWD/openssl.cnf.tmp -batch -md sha256 -in server-revoked.csr -out server-revoked.pem -key $PASS -days 730 -extensions ext_server
  137. $OPENSSL ca -revoke server-revoked.pem -key $PASS
  138. echo
  139. echo "---[ Server - with client ext key use ] ---------------------------------"
  140. echo "---[ Only used for negative-testing for OSU-client implementation ] -----"
  141. echo
  142. cat my-openssl.cnf | sed "s/#@CN@/commonName_default = $CNOC/" > openssl.cnf.tmp
  143. $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -new -newkey rsa:2048 -nodes -out server-client.csr -keyout server-client.key || fail "Could not create server-client.key"
  144. $OPENSSL ca -config $PWD/openssl.cnf.tmp -batch -md sha256 -in server-client.csr -out server-client.pem -key $PASS -days 730 -extensions ext_client || fail "Could not create server-client.pem"
  145. echo
  146. echo "---[ User ]-------------------------------------------------------------"
  147. echo
  148. cat my-openssl.cnf | sed "s/#@CN@/commonName_default = User/" > openssl.cnf.tmp
  149. $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -new -newkey rsa:2048 -nodes -out user.csr -keyout user.key || fail "Could not create user.key"
  150. $OPENSSL ca -config $PWD/openssl.cnf.tmp -batch -md sha256 -in user.csr -out user.pem -key $PASS -days 730 -extensions ext_client || fail "Could not create user.pem"
  151. echo
  152. echo "---[ Server ]-----------------------------------------------------------"
  153. echo
  154. ALT="DNS:$OSU_SERVER_HOSTNAME"
  155. ALT="$ALT,otherName:1.3.6.1.4.1.40808.1.1.1;UTF8String:$OPER_ENG"
  156. ALT="$ALT,otherName:1.3.6.1.4.1.40808.1.1.1;UTF8String:$OPER_FI"
  157. cat my-openssl.cnf |
  158. sed "s/#@CN@/commonName_default = $OSU_SERVER_HOSTNAME/" |
  159. sed "s/^##organizationalUnitName/organizationalUnitName/" |
  160. sed "s/#@OU@/organizationalUnitName_default = Hotspot 2.0 Online Sign Up Server/" |
  161. sed "s/#@ALTNAME@/subjectAltName=critical,$ALT/" \
  162. > openssl.cnf.tmp
  163. echo $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -sha256 -new -newkey rsa:2048 -nodes -out server.csr -keyout server.key -reqexts v3_osu_server
  164. $OPENSSL req -config $PWD/openssl.cnf.tmp -batch -sha256 -new -newkey rsa:2048 -nodes -out server.csr -keyout server.key -reqexts v3_osu_server || fail "Failed to generate server request"
  165. $OPENSSL ca -config $PWD/openssl.cnf.tmp -batch -md sha256 -in server.csr -out server.pem -key $PASS -days 730 -extensions ext_server -policy policy_osu_server || fail "Failed to sign server certificate"
  166. #dump logotype details for debugging
  167. $OPENSSL x509 -in server.pem -out server.der -outform DER
  168. openssl asn1parse -in server.der -inform DER | grep HEX | tail -1 | sed 's/.*://' | xxd -r -p > logo.der
  169. openssl asn1parse -in logo.der -inform DER > logo.asn1
  170. echo
  171. echo "---[ CRL ]---------------------------------------------------------------"
  172. echo
  173. $OPENSSL ca -config $PWD/my-openssl.cnf -gencrl -md sha256 -out demoCA/crl/crl.pem -passin pass:$PASS
  174. echo
  175. echo "---[ Verify ]------------------------------------------------------------"
  176. echo
  177. $OPENSSL verify -CAfile rootCA/cacert.pem demoCA/cacert.pem
  178. $OPENSSL verify -CAfile rootCA/cacert.pem -untrusted demoCA/cacert.pem *.pem
  179. cat rootCA/cacert.pem demoCA/cacert.pem > ca.pem