ocsp-stapling.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/bin/bash
  2. # ocsp-stapling.test
  3. ./examples/client/client -v 3 2>&1 | grep -- 'Bad SSL version'
  4. if [ $? -eq 0 ]; then
  5. echo "TLS 1.2 or lower required"
  6. echo "Skipped"
  7. exit 0
  8. fi
  9. # create a unique ready file ending in PID for the script instance ($$) to take
  10. # advantage of port zero solution
  11. WORKSPACE=`pwd`
  12. CERT_DIR="./certs/ocsp"
  13. resume_port=0
  14. ready_file=`pwd`/wolf_ocsp_s1_readyF$$
  15. ready_file2=`pwd`/wolf_ocsp_s1_readyF2$$
  16. printf '%s\n' "ready file: $ready_file"
  17. test_cnf="ocsp_s1.cnf"
  18. copy_originals() {
  19. cd $CERT_DIR
  20. cp intermediate1-ca-cert.pem bak-intermediate1-ca-cert.pem
  21. cp intermediate2-ca-cert.pem bak-intermediate2-ca-cert.pem
  22. cp intermediate3-ca-cert.pem bak-intermediate3-ca-cert.pem
  23. cp ocsp-responder-cert.pem bak-ocsp-responder-cert.pem
  24. cp root-ca-cert.pem bak-root-ca-cert.pem
  25. cp server1-cert.pem bak-server1-cert.pem
  26. cp server2-cert.pem bak-server2-cert.pem
  27. cp server3-cert.pem bak-server3-cert.pem
  28. cp server4-cert.pem bak-server4-cert.pem
  29. cp server5-cert.pem bak-server5-cert.pem
  30. cd $WORKSPACE
  31. }
  32. restore_originals() {
  33. cd $CERT_DIR
  34. mv bak-intermediate1-ca-cert.pem intermediate1-ca-cert.pem
  35. mv bak-intermediate2-ca-cert.pem intermediate2-ca-cert.pem
  36. mv bak-intermediate3-ca-cert.pem intermediate3-ca-cert.pem
  37. mv bak-ocsp-responder-cert.pem ocsp-responder-cert.pem
  38. mv bak-root-ca-cert.pem root-ca-cert.pem
  39. mv bak-server1-cert.pem server1-cert.pem
  40. mv bak-server2-cert.pem server2-cert.pem
  41. mv bak-server3-cert.pem server3-cert.pem
  42. mv bak-server4-cert.pem server4-cert.pem
  43. mv bak-server5-cert.pem server5-cert.pem
  44. }
  45. wait_for_readyFile(){
  46. counter=0
  47. while [ ! -s $1 -a "$counter" -lt 20 ]; do
  48. echo -e "waiting for ready file..."
  49. sleep 0.1
  50. counter=$((counter+ 1))
  51. done
  52. if test -e $1; then
  53. echo -e "found ready file, starting client..."
  54. else
  55. echo -e "NO ready file ending test..."
  56. exit 1
  57. fi
  58. }
  59. remove_single_rF(){
  60. if test -e $1; then
  61. printf '%s\n' "removing ready file: $1"
  62. rm $1
  63. fi
  64. }
  65. #create a configure file for cert generation with the port 0 solution
  66. create_new_cnf() {
  67. copy_originals
  68. printf '%s\n' "Random Port Selected: $RPORTSELECTED"
  69. printf '%s\n' "#" > $test_cnf
  70. printf '%s\n' "# openssl configuration file for OCSP certificates" >> $test_cnf
  71. printf '%s\n' "#" >> $test_cnf
  72. printf '%s\n' "" >> $test_cnf
  73. printf '%s\n' "# Extensions to add to a certificate request (intermediate1-ca)" >> $test_cnf
  74. printf '%s\n' "[ v3_req1 ]" >> $test_cnf
  75. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  76. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  77. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  78. printf '%s\n' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  79. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:$1" >> $test_cnf
  80. printf '%s\n' "" >> $test_cnf
  81. printf '%s\n' "# Extensions to add to a certificate request (intermediate2-ca)" >> $test_cnf
  82. printf '%s\n' "[ v3_req2 ]" >> $test_cnf
  83. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  84. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  85. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  86. printf '%s\n' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  87. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22222" >> $test_cnf
  88. printf '%s\n' "" >> $test_cnf
  89. printf '%s\n' "# Extensions to add to a certificate request (intermediate3-ca)" >> $test_cnf
  90. printf '%s\n' "[ v3_req3 ]" >> $test_cnf
  91. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  92. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  93. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  94. printf '%s\n' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  95. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22223" >> $test_cnf
  96. printf '%s\n' "" >> $test_cnf
  97. printf '%s\n' "# Extensions for a typical CA" >> $test_cnf
  98. printf '%s\n' "[ v3_ca ]" >> $test_cnf
  99. printf '%s\n' "basicConstraints = CA:true" >> $test_cnf
  100. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  101. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  102. printf '%s\n' "keyUsage = keyCertSign, cRLSign" >> $test_cnf
  103. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22220" >> $test_cnf
  104. printf '%s\n' "" >> $test_cnf
  105. printf '%s\n' "# OCSP extensions." >> $test_cnf
  106. printf '%s\n' "[ v3_ocsp ]" >> $test_cnf
  107. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  108. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  109. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  110. printf '%s\n' "extendedKeyUsage = OCSPSigning" >> $test_cnf
  111. mv $test_cnf $CERT_DIR/$test_cnf
  112. cd $CERT_DIR
  113. CURR_LOC=`pwd`
  114. printf '%s\n' "echo now in $CURR_LOC"
  115. ./renewcerts-for-test.sh $test_cnf
  116. cd $WORKSPACE
  117. }
  118. remove_ready_file() {
  119. if test -e $ready_file; then
  120. printf '%s\n' "removing ready file"
  121. rm $ready_file
  122. fi
  123. if test -e $ready_file2; then
  124. printf '%s\n' "removing ready file: $ready_file2"
  125. rm $ready_file2
  126. fi
  127. }
  128. cleanup()
  129. {
  130. for i in $(jobs -pr)
  131. do
  132. kill -s HUP "$i"
  133. done
  134. remove_ready_file
  135. rm $CERT_DIR/$test_cnf
  136. restore_originals
  137. }
  138. trap cleanup EXIT INT TERM HUP
  139. server=login.live.com
  140. ca=certs/external/baltimore-cybertrust-root.pem
  141. [ ! -x ./examples/client/client ] && echo -e "\n\nClient doesn't exist" && exit 1
  142. ./examples/client/client -? 2>&1 | grep -- 'Client not compiled in!'
  143. if [ $? -eq 0 ]; then
  144. exit 0
  145. fi
  146. # check if supported key size is large enough to handle 4096 bit RSA
  147. size=`./examples/client/client -? | grep "Max RSA key"`
  148. size=`echo ${size//[^0-9]/}`
  149. if [ ! -z "$size" ]; then
  150. printf 'check on max key size of %d ...' $size
  151. if [ $size -lt 4096 ]; then
  152. printf '%s\n' "4096 bit RSA keys not supported"
  153. exit 0
  154. fi
  155. printf 'OK\n'
  156. fi
  157. # test interop fail case
  158. ready_file=`pwd`/wolf_ocsp_readyF$$
  159. printf '%s\n' "ready file: $ready_file"
  160. ./examples/server/server -o -R $ready_file &
  161. wolf_pid=$!
  162. wait_for_readyFile $ready_file
  163. if [ ! -f $ready_file ]; then
  164. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  165. exit 1
  166. else
  167. # should fail if ocspstapling is also enabled
  168. RPORTSELECTED=`cat $ready_file`
  169. echo "hi" | openssl s_client -status -connect 127.0.0.1:${RPORTSELECTED} -cert ./certs/client-cert.pem -key ./certs/client-key.pem -CAfile ./certs/ocsp/root-ca-cert.pem
  170. if [ $? -eq 0 ]; then
  171. printf '%s\n' "Succeeded when should have failed"
  172. remove_single_rF $ready_file
  173. exit 1
  174. fi
  175. remove_single_rF $ready_file
  176. wait $wolf_pid
  177. if [ $? -ne 1 ]; then
  178. printf '%s\n' "wolfSSL server unexpected fail value"
  179. exit 1
  180. fi
  181. fi
  182. # create a port 0 port to use with openssl ocsp responder
  183. ./examples/server/server -R $ready_file -p $resume_port &
  184. wait_for_readyFile $ready_file
  185. if [ ! -f $ready_file ]; then
  186. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  187. exit 1
  188. else
  189. RPORTSELECTED=`cat $ready_file`
  190. printf '%s\n' "Random port selected: $RPORTSELECTED"
  191. # Use client connection to shutdown the server cleanly
  192. ./examples/client/client -p $RPORTSELECTED
  193. create_new_cnf $RPORTSELECTED
  194. fi
  195. sleep 1
  196. # is our desired server there? - login.live.com doesn't answers PING
  197. #./scripts/ping.test $server 2
  198. # client test against the server
  199. ./examples/client/client -C -h $server -p 443 -A $ca -g -W 1
  200. RESULT=$?
  201. [ $RESULT -ne 0 ] && echo -e "\n\nClient connection failed" && exit 1
  202. # Test with example server
  203. ./examples/server/server -? 2>&1 | grep -- 'Server not compiled in!'
  204. if [ $? -eq 0 ]; then
  205. exit 0
  206. fi
  207. # setup ocsp responder
  208. # OLD: ./certs/ocsp/ocspd-intermediate1-ca-issued-certs.sh &
  209. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  210. # purposes!
  211. openssl ocsp -port $RPORTSELECTED -nmin 1 \
  212. -index certs/ocsp/index-intermediate1-ca-issued-certs.txt \
  213. -rsigner certs/ocsp/ocsp-responder-cert.pem \
  214. -rkey certs/ocsp/ocsp-responder-key.pem \
  215. -CA certs/ocsp/intermediate1-ca-cert.pem \
  216. "$@" &
  217. sleep 1
  218. # "jobs" is not portable for posix. Must use bash interpreter!
  219. [ $(jobs -r | wc -l) -ne 1 ] && \
  220. printf '\n\n%s\n' "Setup ocsp responder failed, skipping" && exit 0
  221. printf '%s\n\n' "------------- TEST CASE 1 SHOULD PASS ------------------------"
  222. # client test against our own server - GOOD CERT
  223. ./examples/server/server -c certs/ocsp/server1-cert.pem -R $ready_file2 \
  224. -k certs/ocsp/server1-key.pem -p $resume_port &
  225. wait_for_readyFile $ready_file2
  226. CLI_PORT=`cat $ready_file2`
  227. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -p $CLI_PORT
  228. RESULT=$?
  229. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 2 failed" && exit 1
  230. printf '%s\n\n' "Test PASSED!"
  231. printf '%s\n\n' "------------- TEST CASE 2 SHOULD REVOKE ----------------------"
  232. # client test against our own server - REVOKED CERT
  233. remove_single_rF $ready_file2
  234. ./examples/server/server -c certs/ocsp/server2-cert.pem -R $ready_file2 \
  235. -k certs/ocsp/server2-key.pem -p $resume_port &
  236. wait_for_readyFile $ready_file2
  237. sleep 1
  238. CLI_PORT=`cat $ready_file2`
  239. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -p $CLI_PORT
  240. RESULT=$?
  241. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" \
  242. && exit 1
  243. printf '%s\n\n' "Test successfully REVOKED!"
  244. ./examples/client/client -v 4 2>&1 | grep -- 'Bad SSL version'
  245. if [ $? -ne 0 ]; then
  246. printf '%s\n\n' "------------- TEST CASE 3 SHOULD PASS --------------------"
  247. # client test against our own server - GOOD CERT
  248. remove_single_rF $ready_file2
  249. ./examples/server/server -c certs/ocsp/server1-cert.pem -R $ready_file2 \
  250. -k certs/ocsp/server1-key.pem -v 4 \
  251. -p $resume_port &
  252. wait_for_readyFile $ready_file2
  253. CLI_PORT=`cat $ready_file2`
  254. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -v 4 -F 1 \
  255. -p $CLI_PORT
  256. RESULT=$?
  257. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 3 failed" && exit 1
  258. printf '%s\n\n' "Test PASSED!"
  259. printf '%s\n\n' "------------- TEST CASE 4 SHOULD REVOKE ------------------"
  260. # client test against our own server - REVOKED CERT
  261. remove_single_rF $ready_file2
  262. ./examples/server/server -c certs/ocsp/server2-cert.pem -R $ready_file2 \
  263. -k certs/ocsp/server2-key.pem -v 4 \
  264. -p $resume_port &
  265. wait_for_readyFile $ready_file2
  266. CLI_PORT=`cat $ready_file2`
  267. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -v 4 -F 1 \
  268. -p $CLI_PORT
  269. RESULT=$?
  270. [ $RESULT -ne 1 ] && \
  271. printf '\n\n%s\n' "Client connection succeeded $RESULT" \
  272. && exit 1
  273. printf '%s\n\n' "Test successfully REVOKED!"
  274. fi
  275. exit 0