trusted_peer.test 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/bin/sh
  2. # trusted_peer.test
  3. # copyright wolfSSL 2016
  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. # getting unique port is modeled after resume.test script
  14. # need a unique port since may run the same time as testsuite
  15. # use server port zero hack to get one
  16. port=0
  17. no_pid=-1
  18. server_pid=$no_pid
  19. counter=0
  20. # let's use absolute path to a local dir (make distcheck may be in sub dir)
  21. # also let's add some randomness by adding pid in case multiple 'make check's
  22. # per source tree
  23. ready_file=`pwd`/wolfssl_tp_ready$$
  24. # variables for certs so can use RSA or ECC
  25. client_cert=`pwd`/certs/client-cert.pem
  26. client_ca=`pwd`/certs/ca-cert.pem
  27. client_key=`pwd`/certs/client-key.pem
  28. ca_key=`pwd`/certs/ca-key.pem
  29. server_cert=`pwd`/certs/server-cert.pem
  30. server_key=`pwd`/certs/server-key.pem
  31. combined_cert=`pwd`/certs/client_combined.pem
  32. wrong_ca=`pwd`/certs/wolfssl-website-ca.pem
  33. wrong_cert=`pwd`/certs/server-revoked-cert.pem
  34. echo "ready file $ready_file"
  35. create_port() {
  36. while [ ! -s $ready_file -a "$counter" -lt 20 ]; do
  37. echo -e "waiting for ready file..."
  38. sleep 0.1
  39. counter=$((counter+ 1))
  40. done
  41. if test -e $ready_file; then
  42. echo -e "found ready file, starting client..."
  43. # sleep for an additional 0.1 to mitigate race on write/read of $ready_file:
  44. sleep 0.1
  45. # get created port 0 ephemeral port
  46. port=`cat $ready_file`
  47. else
  48. echo -e "NO ready file ending test..."
  49. do_cleanup
  50. fi
  51. }
  52. remove_ready_file() {
  53. if test -e $ready_file; then
  54. echo -e "removing existing ready file"
  55. rm $ready_file
  56. fi
  57. }
  58. do_cleanup() {
  59. echo "in cleanup"
  60. if [ $server_pid != $no_pid ]
  61. then
  62. echo "killing server"
  63. kill -9 $server_pid
  64. fi
  65. remove_ready_file
  66. }
  67. do_trap() {
  68. echo "got trap"
  69. do_cleanup
  70. exit -1
  71. }
  72. trap do_trap INT TERM
  73. [ ! -x ./examples/client/client ] && echo -e "\n\nClient doesn't exist" && exit 1
  74. # Look for if RSA and/or ECC is enabled and adjust certs/keys
  75. ciphers=`./examples/client/client -e`
  76. if [[ $ciphers != *"RSA"* ]]; then
  77. if [[ $ciphers == *"ECDSA"* ]]; then
  78. client_cert=`pwd`/certs/client-ecc-cert.pem
  79. client_ca=`pwd`/certs/server-ecc.pem
  80. client_key=`pwd`/certs/ecc-client-key.pem
  81. ca_key=`pwd`/certs/ecc-key.pem
  82. server_cert=`pwd`/certs/server-ecc.pem
  83. server_key=`pwd`/certs/ecc-key.pem
  84. wrong_ca=`pwd`/certs/server-ecc-comp.pem
  85. wrong_cert=`pwd`/certs/server-ecc-comp.pem
  86. else
  87. echo "configure options not set up for test. No RSA or ECC"
  88. exit 0
  89. fi
  90. fi
  91. # CRL list not set up for tests
  92. crl_test=`./examples/client/client -h`
  93. if [[ $crl_test == *"-C "* ]]; then
  94. echo "test not set up to run with CRL"
  95. exit 0
  96. fi
  97. # Test for trusted peer certs build
  98. echo ""
  99. echo "Checking built with trusted peer certs "
  100. echo "-----------------------------------------------------"
  101. port=0
  102. remove_ready_file
  103. ./examples/server/server -E $client_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  104. server_pid=$!
  105. create_port
  106. ./examples/client/client -A $client_ca -p $port
  107. RESULT=$?
  108. remove_ready_file
  109. # if fail here then is a settings issue so return 0
  110. if [ $RESULT -ne 0 ]; then
  111. echo -e "\n\nTrusted peer certs not enabled \"WOLFSSL_TRUST_PEER_CERT\""
  112. do_cleanup
  113. exit 0
  114. fi
  115. echo ""
  116. # Test that using no CA's and only trusted peer certs works
  117. echo "Server and Client relying on trusted peer cert loaded"
  118. echo "-----------------------------------------------------"
  119. port=0
  120. ./examples/server/server -A $wrong_ca -E $client_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  121. server_pid=$!
  122. create_port
  123. ./examples/client/client -A $wrong_ca -E $server_cert -c $client_cert -p $port
  124. RESULT=$?
  125. remove_ready_file
  126. if [ $RESULT -ne 0 ]; then
  127. echo -e "\nServer and Client trusted peer cert failed!"
  128. do_cleanup
  129. exit 1
  130. fi
  131. echo ""
  132. # Test that using server trusted peer certs works
  133. echo "Server relying on trusted peer cert loaded"
  134. echo "-----------------------------------------------------"
  135. port=0
  136. ./examples/server/server -A $wrong_ca -E $client_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  137. server_pid=$!
  138. create_port
  139. ./examples/client/client -A $client_ca -c $client_cert -p $port
  140. RESULT=$?
  141. remove_ready_file
  142. if [ $RESULT -ne 0 ]; then
  143. echo -e "\nServer trusted peer cert test failed!"
  144. do_cleanup
  145. exit 1
  146. fi
  147. echo ""
  148. # Test that using client trusted peer certs works
  149. echo "Client relying on trusted peer cert loaded"
  150. echo "-----------------------------------------------------"
  151. port=0
  152. ./examples/server/server -c $server_cert -k $server_key -R $ready_file -p $port &
  153. server_pid=$!
  154. create_port
  155. ./examples/client/client -A $wrong_ca -E $server_cert -p $port
  156. RESULT=$?
  157. remove_ready_file
  158. if [ $RESULT -ne 0 ]; then
  159. echo -e "\nClient trusted peer cert test failed!"
  160. do_cleanup
  161. exit 1
  162. fi
  163. echo ""
  164. # Test that client fall through to CA works
  165. echo "Client fall through to loaded CAs"
  166. echo "-----------------------------------------------------"
  167. port=0
  168. ./examples/server/server -c $server_cert -k $server_key -R $ready_file -p $port &
  169. server_pid=$!
  170. create_port
  171. ./examples/client/client -A $client_ca -E $wrong_cert -p $port
  172. RESULT=$?
  173. remove_ready_file
  174. if [ $RESULT -ne 0 ]; then
  175. echo -e "\nClient trusted peer cert fall through to CA test failed!"
  176. do_cleanup
  177. exit 1
  178. fi
  179. echo ""
  180. # Test that client can fail
  181. # check if using ECC client example is hard coded to load correct ECC ca so skip
  182. if [[ $wrong_ca != *"ecc"* ]]; then
  183. echo "Client wrong CA and wrong trusted peer cert loaded"
  184. echo "-----------------------------------------------------"
  185. port=0
  186. ./examples/server/server -c $server_cert -k $server_key -R $ready_file -p $port &
  187. server_pid=$!
  188. create_port
  189. ./examples/client/client -A $wrong_ca -E $wrong_cert -p $port
  190. RESULT=$?
  191. remove_ready_file
  192. if [ $RESULT -eq 0 ]; then
  193. echo -e "\nClient trusted peer cert test failed!"
  194. do_cleanup
  195. exit 1
  196. fi
  197. echo ""
  198. fi
  199. # Test that server can fail
  200. echo "Server wrong CA and wrong trusted peer cert loaded"
  201. echo "-----------------------------------------------------"
  202. port=0
  203. ./examples/server/server -A $wrong_ca -E $wrong_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  204. server_pid=$!
  205. create_port
  206. ./examples/client/client -A $client_ca -p $port
  207. RESULT=$?
  208. remove_ready_file
  209. if [ $RESULT -eq 0 ]; then
  210. echo -e "\nServer trusted peer cert test failed!"
  211. do_cleanup
  212. exit 1
  213. fi
  214. echo ""
  215. # Test that server fall through to CA works
  216. echo "Server fall through to loaded CAs"
  217. echo "-----------------------------------------------------"
  218. port=0
  219. ./examples/server/server -E $wrong_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  220. server_pid=$!
  221. create_port
  222. ./examples/client/client -A $client_ca -p $port
  223. RESULT=$?
  224. remove_ready_file
  225. if [ $RESULT -ne 0 ]; then
  226. echo -e "\nServer trusted peer cert fall through to CA test failed!"
  227. do_cleanup
  228. exit 1
  229. fi
  230. echo ""
  231. # test loading multiple certs
  232. echo "Server loading multiple trusted peer certs"
  233. echo "Test two success cases and one fail case"
  234. echo "-----------------------------------------------------"
  235. port=0
  236. cat $client_cert $client_ca > $combined_cert
  237. ./examples/server/server -i -A $wrong_ca -E $combined_cert -c $server_cert -k $server_key -R $ready_file -p $port &
  238. server_pid=$!
  239. create_port
  240. ./examples/client/client -A $client_ca -c $client_cert -k $client_key -p $port
  241. RESULT=$?
  242. if [ $RESULT -ne 0 ]; then
  243. echo -e "\nServer load multiple trusted peer certs failed!"
  244. do_cleanup
  245. exit 1
  246. fi
  247. ./examples/client/client -A $client_ca -c $client_ca -k $ca_key -p $port
  248. RESULT=$?
  249. if [ $RESULT -ne 0 ]; then
  250. echo -e "\nServer load multiple trusted peer certs failed!"
  251. do_cleanup
  252. exit 1
  253. fi
  254. ./examples/client/client -A $client_ca -c $wrong_cert -k $client_key -p $port
  255. RESULT=$?
  256. if [ $RESULT -eq 0 ]; then
  257. echo -e "\nServer load multiple trusted peer certs failed!"
  258. do_cleanup
  259. exit 1
  260. fi
  261. do_cleanup # kill PID of server running in infinite loop
  262. rm $combined_cert
  263. remove_ready_file
  264. echo ""
  265. echo "-----------------------------------------------------"
  266. echo "ALL TESTS PASSED"
  267. echo "-----------------------------------------------------"
  268. exit 0