ocsp-stapling-with-ca-as-responder.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/bin/bash
  2. # ocsp-stapling-with-ca-as-responder.test
  3. SCRIPT_DIR="$(dirname "$0")"
  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 ${SCRIPT_DIR}/../certs "${WORKSPACE}"/ || exit $?
  29. cd "$WORKSPACE" || exit $?
  30. ln -s ../examples
  31. CERT_DIR="certs/ocsp"
  32. ready_file="${WORKSPACE}"/wolf_ocsp_s1_readyF$$
  33. ready_file2="${WORKSPACE}"/wolf_ocsp_s1_readyF2$$
  34. printf '%s\n' "ready files: $ready_file $ready_file2"
  35. test_cnf="ocsp_s_w_ca_a_r.cnf"
  36. wait_for_readyFile(){
  37. counter=0
  38. while [ ! -s $1 -a "$counter" -lt 20 ]; do
  39. if [[ -n "${2-}" ]]; then
  40. if ! kill -0 $2 2>&-; then
  41. echo "pid $2 for port ${3-} exited before creating ready file. bailing..."
  42. exit 1
  43. fi
  44. fi
  45. echo -e "waiting for ready file..."
  46. sleep 0.1
  47. counter=$((counter+ 1))
  48. done
  49. if test -e $1; then
  50. echo -e "found ready file, starting client..."
  51. else
  52. echo -e "NO ready file at $1 -- ending test..."
  53. exit 1
  54. fi
  55. }
  56. remove_single_rF(){
  57. if test -e $1; then
  58. printf '%s\n' "removing ready file: $1"
  59. rm $1
  60. fi
  61. }
  62. #create a configure file for cert generation with the port 0 solution
  63. create_new_cnf() {
  64. printf '%s\n' "Random Port Selected: $RPORTSELECTED"
  65. printf '%s\n' "#" > $test_cnf
  66. printf '%s\n' "# openssl configuration file for OCSP certificates" >> $test_cnf
  67. printf '%s\n' "#" >> $test_cnf
  68. printf '%s\n' "" >> $test_cnf
  69. printf '%s\n' "# Extensions to add to a certificate request (intermediate1-ca)" >> $test_cnf
  70. printf '%s\n' "[ v3_req1 ]" >> $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:$1" >> $test_cnf
  76. printf '%s\n' "" >> $test_cnf
  77. printf '%s\n' "# Extensions to add to a certificate request (intermediate2-ca)" >> $test_cnf
  78. printf '%s\n' "[ v3_req2 ]" >> $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:22222" >> $test_cnf
  84. printf '%s\n' "" >> $test_cnf
  85. printf '%s\n' "# Extensions to add to a certificate request (intermediate3-ca)" >> $test_cnf
  86. printf '%s\n' "[ v3_req3 ]" >> $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:22223" >> $test_cnf
  92. printf '%s\n' "" >> $test_cnf
  93. printf '%s\n' "# Extensions for a typical CA" >> $test_cnf
  94. printf '%s\n' "[ v3_ca ]" >> $test_cnf
  95. printf '%s\n' "basicConstraints = CA:true" >> $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 = keyCertSign, cRLSign" >> $test_cnf
  99. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22220" >> $test_cnf
  100. printf '%s\n' "" >> $test_cnf
  101. printf '%s\n' "# OCSP extensions." >> $test_cnf
  102. printf '%s\n' "[ v3_ocsp ]" >> $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' "extendedKeyUsage = OCSPSigning" >> $test_cnf
  107. mv $test_cnf $CERT_DIR/$test_cnf
  108. cd $CERT_DIR
  109. CURR_LOC="$PWD"
  110. printf '%s\n' "echo now in $CURR_LOC"
  111. ./renewcerts-for-test.sh $test_cnf
  112. cd $WORKSPACE
  113. }
  114. remove_ready_file() {
  115. if test -e $ready_file; then
  116. printf '%s\n' "removing ready file"
  117. rm $ready_file
  118. fi
  119. if test -e $ready_file2; then
  120. printf '%s\n' "removing ready file: $ready_file2"
  121. rm $ready_file2
  122. fi
  123. }
  124. cleanup()
  125. {
  126. exit_status=$?
  127. for i in $(jobs -pr)
  128. do
  129. kill -s HUP "$i"
  130. done
  131. remove_ready_file
  132. rm $CERT_DIR/$test_cnf
  133. cd "$PARENTDIR" || return 1
  134. rm -r "$WORKSPACE" || return 1
  135. if [[ ("$exit_status" == 1) && ($RETRIES_REMAINING -gt 0) ]]; then
  136. echo "retrying..."
  137. RETRIES_REMAINING=$((RETRIES_REMAINING - 1))
  138. exec $0 "$@"
  139. fi
  140. }
  141. trap cleanup EXIT INT TERM HUP
  142. server=login.live.com
  143. ca=certs/external/baltimore-cybertrust-root.pem
  144. [ ! -x ./examples/client/client ] && printf '\n\n%s\n' "Client doesn't exist" && exit 1
  145. # choose consecutive ports based on the PID, skipping any that are
  146. # already bound, to avoid the birthday problem in case other
  147. # instances are sharing this host.
  148. get_first_free_port() {
  149. local ret="$1"
  150. while :; do
  151. if [[ "$ret" -ge 65536 ]]; then
  152. ret=1024
  153. fi
  154. if ! nc -z 127.0.0.1 "$ret"; then
  155. break
  156. fi
  157. ret=$((ret+1))
  158. done
  159. echo "$ret"
  160. return 0
  161. }
  162. base_port=$((((($$ + $RETRIES_REMAINING) * 5) % (65536 - 2048)) + 1024))
  163. port1=$(get_first_free_port $base_port)
  164. port2=$(get_first_free_port $((port1 + 1)))
  165. # create a port to use with openssl ocsp responder
  166. ./examples/server/server -R $ready_file -p $port1 &
  167. wolf_pid=$!
  168. wait_for_readyFile $ready_file $wolf_pid $port1
  169. if [ ! -f $ready_file ]; then
  170. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  171. exit 1
  172. else
  173. printf '%s\n' "Random port selected: $port1"
  174. # Use client connection to shutdown the server cleanly
  175. ./examples/client/client -p $port1
  176. create_new_cnf $port1
  177. fi
  178. sleep 0.1
  179. # is our desired server there? - login.live.com doesn't answers PING
  180. #./scripts/ping.test $server 2
  181. # client test against the server
  182. # external test case was never running, disable for now but retain case in event
  183. # we wish to re-activate in the future.
  184. #./examples/client/client -X -C -h $server -p 443 -A $ca -g -W 1
  185. #RESULT=$?
  186. #[ $RESULT -ne 0 ] && echo -e "\n\nClient connection failed" && exit 1
  187. # setup ocsp responder
  188. # OLD: ./certs/ocsp/ocspd-intermediate1-ca-issued-certs-with-ca-as-responder.sh &
  189. # NEW: openssl isn't being cleaned up, invoke directly in script for cleanup
  190. # purposes!
  191. openssl ocsp -port $port1 -nmin 1 \
  192. -index certs/ocsp/index-intermediate1-ca-issued-certs.txt \
  193. -rsigner certs/ocsp/intermediate1-ca-cert.pem \
  194. -rkey certs/ocsp/intermediate1-ca-key.pem \
  195. -CA certs/ocsp/intermediate1-ca-cert.pem \
  196. $@ \
  197. &
  198. sleep 0.1
  199. # "jobs" is not portable for posix. Must use bash interpreter!
  200. [ $(jobs -r | wc -l) -ne 1 ] && printf '\n\n%s\n' "Setup ocsp responder failed, skipping" && exit 0
  201. printf '%s\n\n' "------------- TEST CASE 1 SHOULD PASS ------------------------"
  202. # client test against our own server - GOOD CERT
  203. ./examples/server/server -c certs/ocsp/server1-cert.pem \
  204. -k certs/ocsp/server1-key.pem -R $ready_file2 \
  205. -p $port2 &
  206. wolf_pid2=$!
  207. wait_for_readyFile $ready_file2 $wolf_pid2 $port2
  208. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 \
  209. -p $port2
  210. RESULT=$?
  211. [ $RESULT -ne 0 ] && printf '\n\n%s\n' "Client connection failed" && exit 1
  212. printf '%s\n\n' "Test PASSED!"
  213. printf '%s\n\n' "------------- TEST CASE 2 SHOULD REVOKE ----------------------"
  214. # client test against our own server - REVOKED CERT
  215. remove_single_rF $ready_file2
  216. ./examples/server/server -c certs/ocsp/server2-cert.pem \
  217. -k certs/ocsp/server2-key.pem -R $ready_file2 \
  218. -p $port2 &
  219. wolf_pid2=$!
  220. wait_for_readyFile $ready_file2 $wolf_pid2 $port2
  221. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 \
  222. -p $port2
  223. RESULT=$?
  224. [ $RESULT -ne 1 ] && printf '\n\n%s\n' "Client connection succeeded $RESULT" && exit 1
  225. printf '%s\n\n' "Test successfully REVOKED!"
  226. exit 0