ocsp-stapling.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. if openssl s_server -help 2>&1 | fgrep -q -i ipv6 && nc -h 2>&1 | fgrep -q -i ipv6; then
  15. IPV6_SUPPORTED=yes
  16. else
  17. IPV6_SUPPORTED=no
  18. fi
  19. if ./examples/client/client '-#' | fgrep -q -e ' -DTEST_IPV6 '; then
  20. if [[ "$IPV6_SUPPORTED" == "no" ]]; then
  21. echo 'Skipping IPV6 test in environment lacking IPV6 support.'
  22. exit 0
  23. fi
  24. LOCALHOST='[::1]'
  25. LOCALHOST_FOR_NC='::1'
  26. V4V6=6
  27. V4V6_FLAG=-6
  28. else
  29. LOCALHOST='127.0.0.1'
  30. LOCALHOST_FOR_NC='127.0.0.1'
  31. if [[ "$IPV6_SUPPORTED" == "yes" ]]; then
  32. V4V6_FLAG=-4
  33. else
  34. V4V6_FLAG=
  35. fi
  36. V4V6=4
  37. fi
  38. PARENTDIR="$PWD"
  39. # create a unique workspace directory ending in PID for the script instance ($$)
  40. # to make this instance orthogonal to any others running, even on same repo.
  41. # TCP ports are also carefully formed below from the PID, to minimize conflicts.
  42. WORKSPACE="${PARENTDIR}/workspace.pid$$"
  43. mkdir "${WORKSPACE}" || exit $?
  44. cp -pR certs "${WORKSPACE}"/ || exit $?
  45. cd "$WORKSPACE" || exit $?
  46. ln -s ../examples
  47. CERT_DIR="./certs/ocsp"
  48. ready_file="$WORKSPACE"/wolf_ocsp_s1_readyF$$
  49. ready_file2="$WORKSPACE"/wolf_ocsp_s1_readyF2$$
  50. printf '%s\n' "ready file: $ready_file"
  51. test_cnf="ocsp_s1.cnf"
  52. wait_for_readyFile(){
  53. counter=0
  54. while [ ! -s $1 -a "$counter" -lt 20 ]; do
  55. if [[ -n "${2-}" ]]; then
  56. if ! kill -0 $2 2>&-; then
  57. echo "pid $2 for port ${3-} exited before creating ready file. bailing..."
  58. exit 1
  59. fi
  60. fi
  61. echo -e "waiting for ready file..."
  62. sleep 0.1
  63. counter=$((counter+ 1))
  64. done
  65. if test -e $1; then
  66. echo -e "found ready file, starting client..."
  67. else
  68. echo -e "NO ready file at $1 -- ending test..."
  69. exit 1
  70. fi
  71. }
  72. remove_single_rF(){
  73. if test -e $1; then
  74. printf '%s\n' "removing ready file: $1"
  75. rm $1
  76. fi
  77. }
  78. #create a configure file for cert generation with the port 0 solution
  79. create_new_cnf() {
  80. printf '%s\n' "Random Port Selected: $1"
  81. printf '%s\n' "#" > $test_cnf
  82. printf '%s\n' "# openssl configuration file for OCSP certificates" >> $test_cnf
  83. printf '%s\n' "#" >> $test_cnf
  84. printf '%s\n' "" >> $test_cnf
  85. printf '%s\n' "# Extensions to add to a certificate request (intermediate1-ca)" >> $test_cnf
  86. printf '%s\n' "[ v3_req1 ]" >> $test_cnf
  87. printf '%s\n' "basicConstraints = CA:false" >> $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 = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  91. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:$1" >> $test_cnf
  92. printf '%s\n' "" >> $test_cnf
  93. printf '%s\n' "# Extensions to add to a certificate request (intermediate2-ca)" >> $test_cnf
  94. printf '%s\n' "[ v3_req2 ]" >> $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' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  99. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22222" >> $test_cnf
  100. printf '%s\n' "" >> $test_cnf
  101. printf '%s\n' "# Extensions to add to a certificate request (intermediate3-ca)" >> $test_cnf
  102. printf '%s\n' "[ v3_req3 ]" >> $test_cnf
  103. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  104. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  105. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  106. printf '%s\n' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  107. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22223" >> $test_cnf
  108. printf '%s\n' "" >> $test_cnf
  109. printf '%s\n' "# Extensions for a typical CA" >> $test_cnf
  110. printf '%s\n' "[ v3_ca ]" >> $test_cnf
  111. printf '%s\n' "basicConstraints = CA:true" >> $test_cnf
  112. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  113. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  114. printf '%s\n' "keyUsage = keyCertSign, cRLSign" >> $test_cnf
  115. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22220" >> $test_cnf
  116. printf '%s\n' "" >> $test_cnf
  117. printf '%s\n' "# OCSP extensions." >> $test_cnf
  118. printf '%s\n' "[ v3_ocsp ]" >> $test_cnf
  119. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  120. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  121. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  122. printf '%s\n' "extendedKeyUsage = OCSPSigning" >> $test_cnf
  123. mv $test_cnf $CERT_DIR/$test_cnf
  124. cd $CERT_DIR
  125. CURR_LOC="$PWD"
  126. printf '%s\n' "echo now in $CURR_LOC"
  127. ./renewcerts-for-test.sh $test_cnf
  128. cd $WORKSPACE
  129. }
  130. remove_ready_file() {
  131. if test -e $ready_file; then
  132. printf '%s\n' "removing ready file"
  133. rm $ready_file
  134. fi
  135. if test -e $ready_file2; then
  136. printf '%s\n' "removing ready file: $ready_file2"
  137. rm $ready_file2
  138. fi
  139. }
  140. cleanup()
  141. {
  142. exit_status=$?
  143. for i in $(jobs -pr)
  144. do
  145. kill -s HUP "$i"
  146. done
  147. remove_ready_file
  148. rm $CERT_DIR/$test_cnf
  149. cd "$PARENTDIR" || return 1
  150. rm -r "$WORKSPACE" || return 1
  151. if [[ ("$exit_status" == 1) && ($RETRIES_REMAINING -gt 0) ]]; then
  152. echo "retrying..."
  153. RETRIES_REMAINING=$((RETRIES_REMAINING - 1))
  154. exec $0 "$@"
  155. fi
  156. }
  157. trap cleanup EXIT INT TERM HUP
  158. [ ! -x ./examples/client/client ] && echo -e "\n\nClient doesn't exist" && exit 1
  159. ./examples/client/client '-?' 2>&1 | grep -- 'Client not compiled in!'
  160. if [ $? -eq 0 ]; then
  161. exit 0
  162. fi
  163. # check if supported key size is large enough to handle 4096 bit RSA
  164. size="$(./examples/client/client '-?' | grep "Max RSA key")"
  165. size="${size//[^0-9]/}"
  166. if [ ! -z "$size" ]; then
  167. printf 'check on max key size of %d ...' $size
  168. if [ $size -lt 4096 ]; then
  169. printf '%s\n' "4096 bit RSA keys not supported"
  170. exit 0
  171. fi
  172. printf 'OK\n'
  173. fi
  174. # choose consecutive ports based on the PID, skipping any that are
  175. # already bound, to avoid the birthday problem in case other
  176. # instances are sharing this host.
  177. get_first_free_port() {
  178. local ret="$1"
  179. while :; do
  180. if [[ "$ret" -ge 65536 ]]; then
  181. ret=1024
  182. fi
  183. if ! nc -z $V4V6_FLAG $LOCALHOST_FOR_NC "$ret"; then
  184. break
  185. fi
  186. ret=$((ret+1))
  187. done
  188. echo "$ret"
  189. return 0
  190. }
  191. base_port=$((((($$ + $RETRIES_REMAINING) * 5) % (65536 - 2048)) + 1024))
  192. port1=$(get_first_free_port $base_port)
  193. port2=$(get_first_free_port $((port1 + 1)))
  194. port3=$(get_first_free_port $((port2 + 1)))
  195. # test interop fail case
  196. ready_file=$PWD/wolf_ocsp_readyF$$
  197. printf '%s\n' "ready file: $ready_file"
  198. timeout 60 ./examples/server/server -b -p $port1 -o -R $ready_file &
  199. wolf_pid=$!
  200. wait_for_readyFile $ready_file $wolf_pid $port1
  201. if [ ! -f $ready_file ]; then
  202. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  203. exit 1
  204. else
  205. # should fail if ocspstapling is also enabled
  206. echo "hi" | openssl s_client -status $V4V6_FLAG -connect ${LOCALHOST}:$port1 -cert ./certs/client-cert.pem -key ./certs/client-key.pem -CAfile ./certs/ocsp/root-ca-cert.pem 2>&1 | tee /dev/stderr | fgrep -q 'self signed certificate in certificate chain'
  207. if [ $? -neq 0 ]; then
  208. printf '%s\n' "Expected verification error from s_client is missing."
  209. remove_single_rF $ready_file
  210. exit 1
  211. fi
  212. remove_single_rF $ready_file
  213. wait $wolf_pid
  214. if [ $? -ne 1 ]; then
  215. printf '%s\n' "wolfSSL server unexpected fail value"
  216. exit 1
  217. fi
  218. fi
  219. # create a port to use with openssl ocsp responder
  220. ./examples/server/server -b -p $port2 -R $ready_file &
  221. wolf_pid2=$!
  222. wait_for_readyFile $ready_file $wolf_pid2 $port2
  223. if [ ! -f $ready_file ]; then
  224. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  225. exit 1
  226. else
  227. printf '%s\n' "Random port selected: $port2"
  228. # Use client connection to shutdown the server cleanly
  229. ./examples/client/client -p $port2
  230. create_new_cnf $port2
  231. fi
  232. sleep 0.1
  233. # is our desired server there? - login.live.com doesn't answers PING
  234. #./scripts/ping.test $server 2
  235. # client test against the server
  236. server=login.live.com
  237. #ca=certs/external/baltimore-cybertrust-root.pem
  238. ca=certs/external/ca_collection.pem
  239. if [[ "$V4V6" == "4" ]]; then
  240. ./examples/client/client -C -h $server -p 443 -A $ca -g -W 1
  241. RESULT=$?
  242. [ $RESULT -ne 0 ] && echo -e "\n\nClient connection failed" && exit 1
  243. else
  244. echo "Skipping OCSP test on $server (IPv6 test client)"
  245. fi
  246. # Test with example server
  247. ./examples/server/server '-?' 2>&1 | grep -- 'Server not compiled in!'
  248. if [ $? -eq 0 ]; then
  249. exit 0
  250. fi
  251. # setup ocsp responder
  252. # OLD: ./certs/ocsp/ocspd-intermediate1-ca-issued-certs.sh &
  253. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  254. # purposes!
  255. openssl ocsp -port $port2 -nmin 1 \
  256. -index certs/ocsp/index-intermediate1-ca-issued-certs.txt \
  257. -rsigner certs/ocsp/ocsp-responder-cert.pem \
  258. -rkey certs/ocsp/ocsp-responder-key.pem \
  259. -CA certs/ocsp/intermediate1-ca-cert.pem \
  260. "$@" &
  261. sleep 0.1
  262. # "jobs" is not portable for posix. Must use bash interpreter!
  263. [ $(jobs -r | wc -l) -ne 1 ] && \
  264. printf '\n\n%s\n' "Setup ocsp responder failed, skipping" && exit 0
  265. printf '%s\n\n' "------------- TEST CASE 1 SHOULD PASS ------------------------"
  266. # client test against our own server - GOOD CERT
  267. ./examples/server/server -c certs/ocsp/server1-cert.pem -R $ready_file2 \
  268. -k certs/ocsp/server1-key.pem -p $port3 &
  269. wolf_pid3=$!
  270. wait_for_readyFile $ready_file2 $wolf_pid3 $port3
  271. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -p $port3
  272. RESULT=$?
  273. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 2 failed" && exit 1
  274. printf '%s\n\n' "Test PASSED!"
  275. printf '%s\n\n' "------------- TEST CASE 2 SHOULD REVOKE ----------------------"
  276. # client test against our own server - REVOKED CERT
  277. remove_single_rF $ready_file2
  278. ./examples/server/server -c certs/ocsp/server2-cert.pem -R $ready_file2 \
  279. -k certs/ocsp/server2-key.pem -p $port3 &
  280. wolf_pid3=$!
  281. wait_for_readyFile $ready_file2 $wolf_pid3 $port3
  282. sleep 0.1
  283. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -p $port3
  284. RESULT=$?
  285. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" \
  286. && exit 1
  287. printf '%s\n\n' "Test successfully REVOKED!"
  288. ./examples/client/client -v 4 2>&1 | grep -- 'Bad SSL version'
  289. if [ $? -ne 0 ]; then
  290. printf '%s\n\n' "------------- TEST CASE 3 SHOULD PASS --------------------"
  291. # client test against our own server - GOOD CERT
  292. remove_single_rF $ready_file2
  293. ./examples/server/server -c certs/ocsp/server1-cert.pem -R $ready_file2 \
  294. -k certs/ocsp/server1-key.pem -v 4 \
  295. -p $port3 &
  296. wolf_pid3=$!
  297. wait_for_readyFile $ready_file2 $wolf_pid3 $port3
  298. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -v 4 -F 1 \
  299. -p $port3
  300. RESULT=$?
  301. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 3 failed" && exit 1
  302. printf '%s\n\n' "Test PASSED!"
  303. printf '%s\n\n' "------------- TEST CASE 4 SHOULD REVOKE ------------------"
  304. # client test against our own server - REVOKED CERT
  305. remove_single_rF $ready_file2
  306. ./examples/server/server -c certs/ocsp/server2-cert.pem -R $ready_file2 \
  307. -k certs/ocsp/server2-key.pem -v 4 \
  308. -p $port3 &
  309. wolf_pid3=$!
  310. wait_for_readyFile $ready_file2 $wolf_pid3 $port3
  311. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 -v 4 -F 1 \
  312. -p $port3
  313. RESULT=$?
  314. [ $RESULT -ne 1 ] && \
  315. printf '\n\n%s\n' "Client connection succeeded $RESULT" \
  316. && exit 1
  317. printf '%s\n\n' "Test successfully REVOKED!"
  318. fi
  319. # need a unique port since may run the same time as testsuite
  320. generate_port() {
  321. port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
  322. }
  323. # Start OpenSSL server that has no OCSP responses to return
  324. generate_port
  325. openssl s_server $V4V6_FLAG -cert ./certs/server-cert.pem -key certs/server-key.pem -www -port $port &
  326. openssl_pid=$!
  327. sleep 0.1
  328. printf '%s\n\n' "------------- TEST CASE 5 SHOULD PASS ----------------------"
  329. # client asks for OCSP staple but doesn't fail when none returned
  330. ./examples/client/client -p $port -g -v 3 -W 1
  331. RESULT=$?
  332. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 5 failed" && exit 1
  333. printf '%s\n\n' "Test PASSED!"
  334. printf '%s\n\n' "------------- TEST CASE 6 SHOULD UNKNOWN -------------------"
  335. # client asks for OCSP staple but doesn't fail when none returned
  336. ./examples/client/client -p $port -g -v 3 -W 1m
  337. RESULT=$?
  338. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection 6 succeeded $RESULT" \
  339. && exit 1
  340. printf '%s\n\n' "Test PASSED!"
  341. openssl ciphers -tls1_3
  342. openssl_tls13=$?
  343. ./examples/client/client -v 4 2>&1 | grep -- 'Bad SSL version'
  344. wolfssl_not_tls13=$?
  345. if [ "$openssl_tls13" = "0" -a "wolfssl_not_tls13" != "0" ]; then
  346. printf '%s\n\n' "------------- TEST CASE 7 SHOULD PASS --------------------"
  347. # client asks for OCSP staple but doesn't fail when none returned
  348. ./examples/client/client -p $port -g -v 4 -W 1
  349. RESULT=$?
  350. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 7 failed" && exit 1
  351. printf '%s\n\n' "Test PASSED!"
  352. printf '%s\n\n' "------------- TEST CASE 8 SHOULD UNKNOWN -----------------"
  353. # client asks for OCSP staple but doesn't fail when none returned
  354. ./examples/client/client -p $port -g -v 4 -W 1m
  355. RESULT=$?
  356. [ $RESULT -ne 1 ] \
  357. && printf '\n\n%s\n' "Client connection 8 succeeded $RESULT" \
  358. && exit 1
  359. printf '%s\n\n' "Test PASSED!"
  360. fi
  361. printf '%s\n\n' "------------------- TESTS COMPLETE ---------------------------"
  362. exit 0