ocsp-stapling2.test 16 KB

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