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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/bin/bash
  2. # ocsp-stapling-with-ca-as-responder.test
  3. # if we can, isolate the network namespace to eliminate port collisions.
  4. if [ "${AM_BWRAPPED-}" != "yes" ]; then
  5. bwrap_path="$(command -v bwrap)"
  6. if [ -n "$bwrap_path" ]; then
  7. export AM_BWRAPPED=yes
  8. exec "$bwrap_path" --unshare-net --dev-bind / / "$0" "$@"
  9. fi
  10. unset AM_BWRAPPED
  11. fi
  12. if [[ -z "${RETRIES_REMAINING-}" ]]; then
  13. export RETRIES_REMAINING=2
  14. fi
  15. ./examples/client/client -v 3 2>&1 | grep -- 'Bad SSL version'
  16. if [ $? -eq 0 ]; then
  17. echo "TLS 1.2 or lower required"
  18. echo "Skipped"
  19. exit 0
  20. fi
  21. PARENTDIR=`pwd`
  22. # create a unique workspace directory ending in PID for the script instance ($$)
  23. # to make this instance orthogonal to any others running, even on same repo.
  24. # TCP ports are also carefully formed below from the PID, to minimize conflicts.
  25. WORKSPACE="${PARENTDIR}/workspace.pid$$"
  26. mkdir "${WORKSPACE}" || exit $?
  27. cp -pR certs "${WORKSPACE}"/ || exit $?
  28. cd "$WORKSPACE" || exit $?
  29. ln -s ../examples
  30. CERT_DIR="certs/ocsp"
  31. ready_file="${WORKSPACE}"/wolf_ocsp_s1_readyF$$
  32. ready_file2="${WORKSPACE}"/wolf_ocsp_s1_readyF2$$
  33. printf '%s\n' "ready files: $ready_file $ready_file2"
  34. test_cnf="ocsp_s_w_ca_a_r.cnf"
  35. wait_for_readyFile(){
  36. counter=0
  37. while [ ! -s $1 -a "$counter" -lt 20 ]; do
  38. if [[ -n "${2-}" ]]; then
  39. if ! kill -0 $2 2>&-; then
  40. echo "pid $2 for port ${3-} exited before creating ready file. bailing..."
  41. exit 1
  42. fi
  43. fi
  44. echo -e "waiting for ready file..."
  45. sleep 0.1
  46. counter=$((counter+ 1))
  47. done
  48. if test -e $1; then
  49. echo -e "found ready file, starting client..."
  50. else
  51. echo -e "NO ready file at $1 -- ending test..."
  52. exit 1
  53. fi
  54. }
  55. remove_single_rF(){
  56. if test -e $1; then
  57. printf '%s\n' "removing ready file: $1"
  58. rm $1
  59. fi
  60. }
  61. #create a configure file for cert generation with the port 0 solution
  62. create_new_cnf() {
  63. printf '%s\n' "Random Port Selected: $RPORTSELECTED"
  64. printf '%s\n' "#" > $test_cnf
  65. printf '%s\n' "# openssl configuration file for OCSP certificates" >> $test_cnf
  66. printf '%s\n' "#" >> $test_cnf
  67. printf '%s\n' "" >> $test_cnf
  68. printf '%s\n' "# Extensions to add to a certificate request (intermediate1-ca)" >> $test_cnf
  69. printf '%s\n' "[ v3_req1 ]" >> $test_cnf
  70. printf '%s\n' "basicConstraints = CA:false" >> $test_cnf
  71. printf '%s\n' "subjectKeyIdentifier = hash" >> $test_cnf
  72. printf '%s\n' "authorityKeyIdentifier = keyid:always,issuer:always" >> $test_cnf
  73. printf '%s\n' "keyUsage = nonRepudiation, digitalSignature, keyEncipherment" >> $test_cnf
  74. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:$1" >> $test_cnf
  75. printf '%s\n' "" >> $test_cnf
  76. printf '%s\n' "# Extensions to add to a certificate request (intermediate2-ca)" >> $test_cnf
  77. printf '%s\n' "[ v3_req2 ]" >> $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:22222" >> $test_cnf
  83. printf '%s\n' "" >> $test_cnf
  84. printf '%s\n' "# Extensions to add to a certificate request (intermediate3-ca)" >> $test_cnf
  85. printf '%s\n' "[ v3_req3 ]" >> $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:22223" >> $test_cnf
  91. printf '%s\n' "" >> $test_cnf
  92. printf '%s\n' "# Extensions for a typical CA" >> $test_cnf
  93. printf '%s\n' "[ v3_ca ]" >> $test_cnf
  94. printf '%s\n' "basicConstraints = CA:true" >> $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 = keyCertSign, cRLSign" >> $test_cnf
  98. printf '%s\n' "authorityInfoAccess = OCSP;URI:http://127.0.0.1:22220" >> $test_cnf
  99. printf '%s\n' "" >> $test_cnf
  100. printf '%s\n' "# OCSP extensions." >> $test_cnf
  101. printf '%s\n' "[ v3_ocsp ]" >> $test_cnf
  102. printf '%s\n' "basicConstraints = CA:false" >> $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' "extendedKeyUsage = OCSPSigning" >> $test_cnf
  106. mv $test_cnf $CERT_DIR/$test_cnf
  107. cd $CERT_DIR
  108. CURR_LOC=`pwd`
  109. printf '%s\n' "echo now in $CURR_LOC"
  110. ./renewcerts-for-test.sh $test_cnf
  111. cd $WORKSPACE
  112. }
  113. remove_ready_file() {
  114. if test -e $ready_file; then
  115. printf '%s\n' "removing ready file"
  116. rm $ready_file
  117. fi
  118. if test -e $ready_file2; then
  119. printf '%s\n' "removing ready file: $ready_file2"
  120. rm $ready_file2
  121. fi
  122. }
  123. cleanup()
  124. {
  125. exit_status=$?
  126. for i in $(jobs -pr)
  127. do
  128. kill -s HUP "$i"
  129. done
  130. remove_ready_file
  131. rm $CERT_DIR/$test_cnf
  132. cd "$PARENTDIR" || return 1
  133. rm -r "$WORKSPACE" || return 1
  134. if [[ ("$exit_status" == 1) && ($RETRIES_REMAINING -gt 0) ]]; then
  135. echo "retrying..."
  136. RETRIES_REMAINING=$((RETRIES_REMAINING - 1))
  137. exec $0 "$@"
  138. fi
  139. }
  140. trap cleanup EXIT INT TERM HUP
  141. server=login.live.com
  142. ca=certs/external/baltimore-cybertrust-root.pem
  143. [ ! -x ./examples/client/client ] && printf '\n\n%s\n' "Client doesn't exist" && exit 1
  144. # choose consecutive ports based on the PID, skipping any that are
  145. # already bound, to avoid the birthday problem in case other
  146. # instances are sharing this host.
  147. get_first_free_port() {
  148. local ret="$1"
  149. while :; do
  150. if [[ "$ret" -ge 65536 ]]; then
  151. ret=1024
  152. fi
  153. if ! nc -z 127.0.0.1 "$ret"; then
  154. break
  155. fi
  156. ret=$((ret+1))
  157. done
  158. echo "$ret"
  159. return 0
  160. }
  161. base_port=$((((($$ + $RETRIES_REMAINING) * 5) % (65536 - 2048)) + 1024))
  162. port1=$(get_first_free_port $base_port)
  163. port2=$(get_first_free_port $((port1 + 1)))
  164. # create a port to use with openssl ocsp responder
  165. ./examples/server/server -R $ready_file -p $port1 &
  166. wolf_pid=$!
  167. wait_for_readyFile $ready_file $wolf_pid $port1
  168. if [ ! -f $ready_file ]; then
  169. printf '%s\n' "Failed to create ready file: \"$ready_file\""
  170. exit 1
  171. else
  172. RPORTSELECTED=`cat $ready_file`
  173. printf '%s\n' "Random port selected: $RPORTSELECTED"
  174. # Use client connection to shutdown the server cleanly
  175. ./examples/client/client -p $RPORTSELECTED
  176. create_new_cnf $RPORTSELECTED
  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 $RPORTSELECTED -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. wait_for_readyFile $ready_file2
  207. CLI_PORT=`cat $ready_file2`
  208. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 \
  209. -p $CLI_PORT
  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. wait_for_readyFile $ready_file2
  220. CLI_PORT=`cat $ready_file2`
  221. ./examples/client/client -C -A certs/ocsp/root-ca-cert.pem -W 1 \
  222. -p $CLI_PORT
  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