ocsp-stapling2.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #!/bin/bash
  2. # ocsp-stapling2.test
  3. # Test requires HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST_V2
  4. SCRIPT_DIR="$(dirname "$0")"
  5. # if we can, isolate the network namespace to eliminate port collisions.
  6. if [ "${AM_BWRAPPED-}" != "yes" ]; then
  7. bwrap_path="$(command -v bwrap)"
  8. if [ -n "$bwrap_path" ]; then
  9. export AM_BWRAPPED=yes
  10. exec "$bwrap_path" --unshare-net --dev-bind / / "$0" "$@"
  11. fi
  12. unset AM_BWRAPPED
  13. fi
  14. if [[ -z "${RETRIES_REMAINING-}" ]]; then
  15. export RETRIES_REMAINING=2
  16. fi
  17. ./examples/client/client -v 3 2>&1 | grep -- 'Bad SSL version'
  18. if [ $? -eq 0 ]; then
  19. echo "TLS 1.2 or lower required"
  20. echo "Skipped"
  21. exit 0
  22. fi
  23. if openssl s_server -help 2>&1 | fgrep -q -i ipv6 && nc -h 2>&1 | fgrep -q -i ipv6; then
  24. IPV6_SUPPORTED=yes
  25. else
  26. IPV6_SUPPORTED=no
  27. fi
  28. if ./examples/client/client '-#' | fgrep -q -e ' -DTEST_IPV6 '; then
  29. if [[ "$IPV6_SUPPORTED" == "no" ]]; then
  30. echo 'Skipping IPV6 test in environment lacking IPV6 support.'
  31. exit 0
  32. fi
  33. LOCALHOST='[::1]'
  34. LOCALHOST_FOR_NC='-6 ::1'
  35. else
  36. LOCALHOST='127.0.0.1'
  37. LOCALHOST_FOR_NC='127.0.0.1'
  38. fi
  39. PARENTDIR="$PWD"
  40. # create a unique workspace directory ending in PID for the script instance ($$)
  41. # to make this instance orthogonal to any others running, even on same repo.
  42. # TCP ports are also carefully formed below from the PID, to minimize conflicts.
  43. WORKSPACE="${PARENTDIR}/workspace.pid$$"
  44. mkdir "${WORKSPACE}" || exit $?
  45. cp -pR ${SCRIPT_DIR}/../certs "${WORKSPACE}"/ || exit $?
  46. cd "$WORKSPACE" || exit $?
  47. ln -s ../examples
  48. CERT_DIR="certs/ocsp"
  49. ready_file1="$WORKSPACE"/wolf_ocsp_s2_readyF1$$
  50. ready_file2="$WORKSPACE"/wolf_ocsp_s2_readyF2$$
  51. ready_file3="$WORKSPACE"/wolf_ocsp_s2_readyF3$$
  52. ready_file4="$WORKSPACE"/wolf_ocsp_s2_readyF4$$
  53. ready_file5="$WORKSPACE"/wolf_ocsp_s2_readyF5$$
  54. printf '%s\n' "ready file 1: $ready_file1"
  55. printf '%s\n' "ready file 2: $ready_file2"
  56. printf '%s\n' "ready file 3: $ready_file3"
  57. printf '%s\n' "ready file 4: $ready_file4"
  58. printf '%s\n' "ready file 5: $ready_file5"
  59. test_cnf="ocsp_s2.cnf"
  60. wait_for_readyFile(){
  61. counter=0
  62. while [ ! -s $1 -a "$counter" -lt 20 ]; do
  63. if [[ -n "${2-}" ]]; then
  64. if ! kill -0 $2 2>&-; then
  65. echo "pid $2 for port ${3-} exited before creating ready file. bailing..."
  66. exit 1
  67. fi
  68. fi
  69. echo -e "waiting for ready file..."
  70. sleep 0.1
  71. counter=$((counter+ 1))
  72. done
  73. if test -e $1; then
  74. echo -e "found ready file, starting client..."
  75. else
  76. echo -e "NO ready file at $1 -- ending test..."
  77. exit 1
  78. fi
  79. }
  80. remove_single_rF(){
  81. if test -e $1; then
  82. printf '%s\n' "removing ready file: $1"
  83. rm $1
  84. fi
  85. }
  86. #create a configure file for cert generation with the port 0 solution
  87. create_new_cnf() {
  88. printf '%s\n' "Random Ports Selected: $1 $2 $3 $4"
  89. printf '%s\n' "#" > $test_cnf
  90. printf '%s\n' "# openssl configuration file for OCSP certificates" >> $test_cnf
  91. printf '%s\n' "#" >> $test_cnf
  92. printf '%s\n' "" >> $test_cnf
  93. printf '%s\n' "# Extensions to add to a certificate request (intermediate1-ca)" >> $test_cnf
  94. printf '%s\n' "[ v3_req1 ]" >> $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:$1" >> $test_cnf
  100. printf '%s\n' "" >> $test_cnf
  101. printf '%s\n' "# Extensions to add to a certificate request (intermediate2-ca)" >> $test_cnf
  102. printf '%s\n' "[ v3_req2 ]" >> $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:$2" >> $test_cnf
  108. printf '%s\n' "" >> $test_cnf
  109. printf '%s\n' "# Extensions to add to a certificate request (intermediate3-ca)" >> $test_cnf
  110. printf '%s\n' "[ v3_req3 ]" >> $test_cnf
  111. printf '%s\n' "basicConstraints = CA:false" >> $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 = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  115. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:$3" >> $test_cnf
  116. printf '%s\n' "" >> $test_cnf
  117. printf '%s\n' "# Extensions for a typical CA" >> $test_cnf
  118. printf '%s\n' "[ v3_ca ]" >> $test_cnf
  119. printf '%s\n' "basicConstraints = CA:true" >> $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' "keyUsage = keyCertSign, cRLSign" >> $test_cnf
  123. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:$4" >> $test_cnf
  124. printf '%s\n' "" >> $test_cnf
  125. printf '%s\n' "# OCSP extensions." >> $test_cnf
  126. printf '%s\n' "[ v3_ocsp ]" >> $test_cnf
  127. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  128. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  129. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  130. printf '%s\n' "extendedKeyUsage = OCSPSigning" >> $test_cnf
  131. mv $test_cnf $CERT_DIR/$test_cnf
  132. cd $CERT_DIR
  133. CURR_LOC="$PWD"
  134. printf '%s\n' "echo now in $CURR_LOC"
  135. ./renewcerts-for-test.sh $test_cnf
  136. cd $WORKSPACE
  137. }
  138. remove_ready_file(){
  139. if test -e $ready_file1; then
  140. printf '%s\n' "removing ready file: $ready_file1"
  141. rm $ready_file1
  142. fi
  143. if test -e $ready_file2; then
  144. printf '%s\n' "removing ready file: $ready_file2"
  145. rm $ready_file2
  146. fi
  147. if test -e $ready_file3; then
  148. printf '%s\n' "removing ready file: $ready_file3"
  149. rm $ready_file3
  150. fi
  151. if test -e $ready_file4; then
  152. printf '%s\n' "removing ready file: $ready_file4"
  153. rm $ready_file4
  154. fi
  155. if test -e $ready_file5; then
  156. printf '%s\n' "removing ready file: $ready_file5"
  157. rm $ready_file5
  158. fi
  159. }
  160. cleanup()
  161. {
  162. exit_status=$?
  163. for i in $(jobs -pr)
  164. do
  165. kill -s HUP "$i"
  166. done
  167. remove_ready_file
  168. rm $CERT_DIR/$test_cnf
  169. cd "$PARENTDIR" || return 1
  170. rm -r "$WORKSPACE" || return 1
  171. if [[ ("$exit_status" == 1) && ($RETRIES_REMAINING -gt 0) ]]; then
  172. echo "retrying..."
  173. RETRIES_REMAINING=$((RETRIES_REMAINING - 1))
  174. exec $0 "$@"
  175. fi
  176. }
  177. trap cleanup EXIT INT TERM HUP
  178. [ ! -x ./examples/client/client ] && echo -e "\n\nClient doesn't exist" && exit 1
  179. # check if supported key size is large enough to handle 4096 bit RSA
  180. size="$(./examples/client/client '-?' | grep "Max RSA key")"
  181. size="${size//[^0-9]/}"
  182. if [ ! -z "$size" ]; then
  183. printf 'check on max key size of %d ...' $size
  184. if [ $size -lt 4096 ]; then
  185. printf '%s\n' "4096 bit RSA keys not supported"
  186. exit 0
  187. fi
  188. printf 'OK\n'
  189. fi
  190. #get four unique ports
  191. # choose consecutive ports based on the PID, skipping any that are
  192. # already bound, to avoid the birthday problem in case other
  193. # instances are sharing this host.
  194. get_first_free_port() {
  195. local ret="$1"
  196. while :; do
  197. if [[ "$ret" -ge 65536 ]]; then
  198. ret=1024
  199. fi
  200. if ! nc -z ${LOCALHOST_FOR_NC} "$ret"; then
  201. break
  202. fi
  203. ret=$((ret+1))
  204. done
  205. echo "$ret"
  206. return 0
  207. }
  208. base_port=$((((($$ + $RETRIES_REMAINING) * 5) % (65536 - 2048)) + 1024))
  209. port1=$(get_first_free_port $base_port)
  210. port2=$(get_first_free_port $((port1 + 1)))
  211. port3=$(get_first_free_port $((port2 + 1)))
  212. port4=$(get_first_free_port $((port3 + 1)))
  213. port5=$(get_first_free_port $((port4 + 1)))
  214. # 1:
  215. ./examples/server/server -R $ready_file1 -p $port1 &
  216. server_pid1=$!
  217. wait_for_readyFile $ready_file1 $server_pid1 $port1
  218. if [ ! -f $ready_file1 ]; then
  219. printf '%s\n' "Failed to create ready file1: \"$ready_file1\""
  220. exit 1
  221. fi
  222. # 2:
  223. ./examples/server/server -R $ready_file2 -p $port2 &
  224. server_pid2=$!
  225. wait_for_readyFile $ready_file2 $server_pid2 $port2
  226. if [ ! -f $ready_file2 ]; then
  227. printf '%s\n' "Failed to create ready file2: \"$ready_file2\""
  228. exit 1
  229. fi
  230. # 3:
  231. ./examples/server/server -R $ready_file3 -p $port3 &
  232. server_pid3=$!
  233. wait_for_readyFile $ready_file3 $server_pid3 $port3
  234. if [ ! -f $ready_file3 ]; then
  235. printf '%s\n' "Failed to create ready file3: \"$ready_file3\""
  236. exit 1
  237. fi
  238. # 4:
  239. ./examples/server/server -R $ready_file4 -p $port4 &
  240. server_pid4=$!
  241. wait_for_readyFile $ready_file4 $server_pid4 $port4
  242. if [ ! -f $ready_file4 ]; then
  243. printf '%s\n' "Failed to create ready file4: \"$ready_file4\""
  244. exit 1
  245. fi
  246. printf '%s\n' "------------- PORTS ---------------"
  247. printf '%s' "Random ports selected: $port1 $port2"
  248. printf '%s\n' " $port3 $port4"
  249. printf '%s\n' "-----------------------------------"
  250. # Use client connections to cleanly shutdown the servers
  251. ./examples/client/client -p $port1
  252. ./examples/client/client -p $port2
  253. ./examples/client/client -p $port3
  254. ./examples/client/client -p $port4
  255. create_new_cnf $port1 $port2 $port3 \
  256. $port4
  257. sleep 0.1
  258. # setup ocsp responders
  259. # OLD: ./certs/ocsp/ocspd-root-ca-and-intermediate-cas.sh &
  260. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  261. # purposes!
  262. openssl ocsp -port $port1 -nmin 1 \
  263. -index certs/ocsp/index-ca-and-intermediate-cas.txt \
  264. -rsigner certs/ocsp/ocsp-responder-cert.pem \
  265. -rkey certs/ocsp/ocsp-responder-key.pem \
  266. -CA certs/ocsp/root-ca-cert.pem \
  267. $@ \
  268. &
  269. # OLD: ./certs/ocsp/ocspd-intermediate2-ca-issued-certs.sh &
  270. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  271. # purposes!
  272. openssl ocsp -port $port2 -nmin 1 \
  273. -index certs/ocsp/index-intermediate2-ca-issued-certs.txt \
  274. -rsigner certs/ocsp/ocsp-responder-cert.pem \
  275. -rkey certs/ocsp/ocsp-responder-key.pem \
  276. -CA certs/ocsp/intermediate2-ca-cert.pem \
  277. $@ \
  278. &
  279. # OLD: ./certs/ocsp/ocspd-intermediate3-ca-issued-certs.sh &
  280. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  281. # purposes!
  282. openssl ocsp -port $port3 -nmin 1 \
  283. -index certs/ocsp/index-intermediate3-ca-issued-certs.txt \
  284. -rsigner certs/ocsp/ocsp-responder-cert.pem \
  285. -rkey certs/ocsp/ocsp-responder-key.pem \
  286. -CA certs/ocsp/intermediate3-ca-cert.pem \
  287. $@ \
  288. &
  289. sleep 0.1
  290. # "jobs" is not portable for posix. Must use bash interpreter!
  291. [ $(jobs -r | wc -l) -ne 3 ] && printf '\n\n%s\n' "Setup ocsp responder failed, skipping" && exit 0
  292. printf '\n\n%s\n\n' "All OCSP responders started successfully!"
  293. printf '%s\n\n' "------------- TEST CASE 1 SHOULD PASS ------------------------"
  294. # client test against our own server - GOOD CERTS
  295. ./examples/server/server -c certs/ocsp/server3-cert.pem \
  296. -k certs/ocsp/server3-key.pem -R $ready_file5 \
  297. -p $port5 &
  298. server_pid5=$!
  299. wait_for_readyFile $ready_file5 $server_pid5 $port5
  300. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 2 -v 3 \
  301. -p $port5
  302. RESULT=$?
  303. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 1 failed" && exit 1
  304. printf '%s\n\n' "Test PASSED!"
  305. printf '%s\n\n' "TEST CASE 2 DISABLED PENDING REVIEW"
  306. #printf '%s\n\n' "------------- TEST CASE 2 SHOULD PASS ------------------------"
  307. #remove_single_rF $ready_file5
  308. #./examples/server/server -c certs/ocsp/server3-cert.pem \
  309. # -k certs/ocsp/server3-key.pem -R $ready_file5 \
  310. # -p $port5 &
  311. #wait_for_readyFile $ready_file5 $server_pid5 $port5
  312. #./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 3 -v 3 \
  313. # -p $port5
  314. #RESULT=$?
  315. #[ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 2 failed" && exit 1
  316. #printf '%s\n\n' "Test PASSED!"
  317. printf '%s\n\n' "------------- TEST CASE 3 SHOULD REVOKE ----------------------"
  318. # client test against our own server - REVOKED SERVER CERT
  319. remove_single_rF $ready_file5
  320. ./examples/server/server -c certs/ocsp/server4-cert.pem \
  321. -k certs/ocsp/server4-key.pem -R $ready_file5 \
  322. -p $port5 &
  323. server_pid5=$!
  324. wait_for_readyFile $ready_file5 $server_pid5 $port5
  325. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 2 -v 3 \
  326. -p $port5
  327. RESULT=$?
  328. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" && exit 1
  329. printf '%s\n\n' "Test successfully REVOKED!"
  330. printf '%s\n\n' "------------- TEST CASE 4 SHOULD REVOKE ----------------------"
  331. remove_single_rF $ready_file5
  332. ./examples/server/server -c certs/ocsp/server4-cert.pem \
  333. -k certs/ocsp/server4-key.pem -R $ready_file5 \
  334. -p $port5 &
  335. sleep 0.1
  336. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 3 -v 3 \
  337. -p $port5
  338. RESULT=$?
  339. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" && exit 1
  340. printf '%s\n\n' "Test successfully REVOKED!"
  341. printf '%s\n\n' "------------- TEST CASE 5 SHOULD PASS ------------------------"
  342. # client test against our own server - REVOKED INTERMEDIATE CERT
  343. remove_single_rF $ready_file5
  344. ./examples/server/server -c certs/ocsp/server5-cert.pem \
  345. -k certs/ocsp/server5-key.pem -R $ready_file5 \
  346. -p $port5 &
  347. server_pid5=$!
  348. wait_for_readyFile $ready_file5 $server_pid5 $port5
  349. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 2 -v 3 \
  350. -p $port5
  351. RESULT=$?
  352. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 3 failed $RESULT" && exit 1
  353. printf '%s\n\n' "Test PASSED!"
  354. printf '%s\n\n' "------------- TEST CASE 6 SHOULD REVOKE ----------------------"
  355. remove_single_rF $ready_file5
  356. ./examples/server/server -c certs/ocsp/server5-cert.pem \
  357. -k certs/ocsp/server5-key.pem -R $ready_file5 \
  358. -p $port5 &
  359. server_pid5=$!
  360. wait_for_readyFile $ready_file5 $server_pid5 $port5
  361. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 3 -v 3 \
  362. -p $port5
  363. RESULT=$?
  364. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" && exit 1
  365. printf '%s\n\n' "Test successfully REVOKED!"
  366. printf '%s\n\n' "------------- TEST CASE 7 LOAD CERT IN SSL -------------------"
  367. remove_single_rF $ready_file5
  368. ./examples/server/server -c certs/ocsp/server1-cert.pem \
  369. -k certs/ocsp/server1-key.pem -R $ready_file5 \
  370. -p $port5 -H loadSSL &
  371. server_pid5=$!
  372. wait_for_readyFile $ready_file5 $server_pid5 $port5
  373. echo "test connection" | openssl s_client -status -connect ${LOCALHOST}:$port5 -cert ./certs/client-cert.pem -key ./certs/client-key.pem -CAfile ./certs/ocsp/root-ca-cert.pem
  374. RESULT=$?
  375. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection failed $RESULT" && exit 1
  376. wait $server_pid5
  377. if [ $? -ne 0 ]; then
  378. printf '%s\n' "Unexpected server result"
  379. exit 1
  380. fi
  381. printf '%s\n\n' "Test successful"
  382. printf '%s\n\n' "------------- TEST CASE 8 SHOULD REVOKE ----------------------"
  383. remove_single_rF $ready_file5
  384. ./examples/server/server -c certs/ocsp/server4-cert.pem \
  385. -k certs/ocsp/server4-key.pem -R $ready_file5 \
  386. -p $port5 -H loadSSL &
  387. server_pid5=$!
  388. sleep 0.1
  389. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 3 -v 3 \
  390. -p $port5
  391. RESULT=$?
  392. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" && exit 1
  393. wait $server_pid5
  394. if [ $? -ne 1 ]; then
  395. printf '%s\n' "Unexpected server result"
  396. exit 1
  397. fi
  398. printf '%s\n\n' "Test successfully REVOKED!"
  399. # need a unique port since may run the same time as testsuite
  400. generate_port() {
  401. port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
  402. }
  403. # Start OpenSSL server that has no OCSP responses to return
  404. generate_port
  405. openssl s_server -cert ./certs/server-cert.pem -key certs/server-key.pem -www -port $port &
  406. openssl_pid=$!
  407. sleep 0.1
  408. printf '%s\n\n' "------------- TEST CASE 9 SHOULD PASS ----------------------"
  409. # client asks for OCSP staple but doesn't fail when none returned
  410. ./examples/client/client -p $port -g -v 3 -W 2
  411. RESULT=$?
  412. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection 9 failed" && exit 1
  413. printf '%s\n\n' "Test PASSED!"
  414. printf '%s\n\n' "------------- TEST CASE 10 SHOULD UNKNOWN -------------------"
  415. # client asks for OCSP staple but doesn't fail when none returned
  416. ./examples/client/client -p $port -g -v 3 -W 2m
  417. RESULT=$?
  418. [ $RESULT -ne 1 ] \
  419. && printf '\n\n%s\n' "Client connection 10 succeeded $RESULT" \
  420. && exit 1
  421. printf '%s\n\n' "Test PASSED!"
  422. printf '%s\n\n' "------------------- TESTS COMPLETE ---------------------------"
  423. exit 0