ocsp-stapling.test 12 KB

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