openssl.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #!/bin/sh
  2. #openssl.test
  3. # need a unique port since may run the same time as testsuite
  4. generate_port() {
  5. port=`LC_CTYPE=C tr -cd 0-9 </dev/urandom | head -c 7`
  6. port=$((`LC_CTYPE=C tr -cd 1-9 </dev/urandom | head -c 1`$port))
  7. port=$(($port % (65535-49512)))
  8. port=$(($port + 49512))
  9. }
  10. generate_port
  11. openssl_port=$port
  12. no_pid=-1
  13. server_pid=$no_pid
  14. ecdh_server_pid=$no_pid
  15. wolf_suites_tested=0
  16. wolf_suites_total=0
  17. counter=0
  18. testing_summary="OpenSSL Interop Testing Summary:\nVersion\tTested\t#Found\t#Tested\n"
  19. versionName="Invalid"
  20. if [ "$OPENSSL" = "" ]; then
  21. OPENSSL=openssl
  22. fi
  23. version_name() {
  24. case $version in "0")
  25. versionName="SSLv3"
  26. ;;
  27. "1")
  28. versionName="TLSv1"
  29. ;;
  30. "2")
  31. versionName="TLSv1.1"
  32. ;;
  33. "3")
  34. versionName="TLSv1.2"
  35. ;;
  36. "4")
  37. versionName="ALL"
  38. ;;
  39. esac
  40. }
  41. do_cleanup() {
  42. echo "in cleanup"
  43. if [ $server_pid != $no_pid ]
  44. then
  45. echo "killing server"
  46. kill -9 $server_pid
  47. fi
  48. if [ $ecdh_server_pid != $no_pid ]
  49. then
  50. echo "killing ECDH-RSA server"
  51. kill -9 $ecdh_server_pid
  52. fi
  53. }
  54. do_trap() {
  55. echo "got trap"
  56. do_cleanup
  57. exit 1
  58. }
  59. trap do_trap INT TERM
  60. if test -n "$WOLFSSL_OPENSSL_TEST"; then
  61. echo "WOLFSSL_OPENSSL_TEST set, running test..."
  62. else
  63. echo "WOLFSSL_OPENSSL_TEST NOT set, won't run"
  64. exit 0
  65. fi
  66. echo -e "\nTesting existence of openssl command...\n"
  67. command -v $OPENSSL >/dev/null 2>&1 || { echo >&2 "Requires openssl command, but it's not installed. Ending."; exit 0; }
  68. echo -e "\nTesting for _build directory as part of distcheck, different paths"
  69. currentDir=`pwd`
  70. if [ $currentDir = *"_build" ]
  71. then
  72. echo -e "_build directory detected, moving a directory back"
  73. cd ..
  74. fi
  75. # get wolfssl ciphers
  76. wolf_ciphers=`./examples/client/client -e`
  77. found_free_port=0
  78. while [ "$counter" -lt 20 ]; do
  79. echo -e "\nTrying to start openssl server on port $openssl_port...\n"
  80. $OPENSSL s_server -accept $openssl_port -cert ./certs/server-cert.pem -key ./certs/server-key.pem -quiet -CAfile ./certs/client-ca.pem -www -dhparam ./certs/dh2048.pem -dcert ./certs/server-ecc.pem -dkey ./certs/ecc-key.pem -verify 10 -verify_return_error -psk 1a2b3c4d -cipher "ALL:eNULL" &
  81. server_pid=$!
  82. # wait to see if s_server successfully starts before continuing
  83. sleep 0.1
  84. if ps -p $server_pid > /dev/null
  85. then
  86. echo "s_server started successfully on port $openssl_port"
  87. found_free_port=1
  88. break
  89. else
  90. #port already started, try a different port
  91. counter=$((counter+ 1))
  92. generate_port
  93. openssl_port=$port
  94. fi
  95. done
  96. if [ $found_free_port = 0 ]
  97. then
  98. echo -e "Couldn't find free port for server"
  99. do_cleanup
  100. exit 1
  101. fi
  102. # if ECDH-RSA is enabled then start up server for ECDH-RSA suites
  103. case $wolf_ciphers in
  104. *ECDH-RSA*)
  105. generate_port
  106. ecdh_port=$port
  107. found_free_port=0
  108. counter=0
  109. while [ "$counter" -lt 20 ]; do
  110. echo -e "\nTrying to start ECDH-RSA openssl server on port $ecdh_port...\n"
  111. $OPENSSL s_server -accept $ecdh_port -cert ./certs/server-ecc-rsa.pem -key ./certs/ecc-key.pem -quiet -CAfile ./certs/client-ca.pem -www -dhparam ./certs/dh2048.pem -verify 10 -verify_return_error -cipher "ALL:eNULL" &
  112. ecdh_server_pid=$!
  113. # wait to see if s_server successfully starts before continuing
  114. sleep 0.1
  115. if ps -p $ecdh_server_pid > /dev/null
  116. then
  117. echo "s_server started successfully on port $ecdh_port"
  118. found_free_port=1
  119. break
  120. else
  121. #port already started, try a different port
  122. counter=$((counter+ 1))
  123. generate_port
  124. ecdh_port=$port
  125. fi
  126. done
  127. if [ $found_free_port = 0 ]
  128. then
  129. echo -e "Couldn't find free port for server"
  130. do_cleanup
  131. exit 1
  132. fi
  133. ;;
  134. esac
  135. # server should be ready, let's make sure
  136. server_ready=0
  137. while [ "$counter" -lt 20 ]; do
  138. echo -e "waiting for openssl s_server ready..."
  139. nc -z localhost $openssl_port
  140. nc_result=$?
  141. if [ $nc_result = 0 ]
  142. then
  143. echo -e "openssl s_server ready!"
  144. server_ready=1
  145. break
  146. fi
  147. sleep 0.1
  148. counter=$((counter+ 1))
  149. done
  150. if [ $server_ready = 0 ]
  151. then
  152. echo -e "Couldn't verify openssl server is running, timeout error"
  153. do_cleanup
  154. exit 1
  155. fi
  156. OIFS=$IFS # store old seperator to reset
  157. IFS=$'\:' # set delimiter
  158. set -f # no globbing
  159. wolf_versions=`./examples/client/client -V`
  160. wolf_versions="$wolf_versions:4" #:4 will test without -v flag
  161. wolf_temp_suites_total=0
  162. wolf_temp_suites_tested=0
  163. for version in $wolf_versions;
  164. do
  165. echo -e "version = $version"
  166. # get openssl ciphers depending on version
  167. case $version in "0")
  168. openssl_ciphers=`$OPENSSL ciphers "SSLv3"`
  169. # double check that can actually do a sslv3 connection using
  170. # client-cert.pem to send but any file with EOF works
  171. $OPENSSL s_client -ssl3 -no_ign_eof -host localhost -port $openssl_port < ./certs/client-cert.pem
  172. sslv3_sup=$?
  173. if [ $sslv3_sup != 0 ]
  174. then
  175. echo -e "Not testing SSLv3. No OpenSSL support for 'SSLv3' modifier"
  176. testing_summary="$testing_summary SSLv3\tNo\tN/A\tN/A\t (No OpenSSL Support for cipherstring)\n"
  177. continue
  178. fi
  179. ;;
  180. "1")
  181. openssl_ciphers=`$OPENSSL ciphers "TLSv1"`
  182. tlsv1_sup=$?
  183. if [ $tlsv1_sup != 0 ]
  184. then
  185. echo -e "Not testing TLSv1. No OpenSSL support for 'TLSv1' modifier"
  186. testing_summary="$testing_summary TLSv1\tNo\tN/A\tN/A\t (No OpenSSL Support for cipherstring)\n"
  187. continue
  188. fi
  189. ;;
  190. "2")
  191. openssl_ciphers=`$OPENSSL ciphers "TLSv1.1"`
  192. tlsv1_1_sup=$?
  193. if [ $tlsv1_1_sup != 0 ]
  194. then
  195. echo -e "Not testing TLSv1.1. No OpenSSL support for 'TLSv1.1' modifier"
  196. testing_summary="${testing_summary}TLSv1.1\tNo\tN/A\tN/A\t (No OpenSSL Support for cipherstring)\n"
  197. continue
  198. fi
  199. ;;
  200. "3")
  201. openssl_ciphers=`$OPENSSL ciphers "TLSv1.2"`
  202. tlsv1_2_sup=$?
  203. if [ $tlsv1_2_sup != 0 ]
  204. then
  205. echo -e "Not testing TLSv1.2. No OpenSSL support for 'TLSv1.2' modifier"
  206. testing_summary="$testing_summary TLSv1.2\tNo\tN/A\tN/A\t (No OpenSSL Support for cipherstring)\n"
  207. continue
  208. fi
  209. ;;
  210. "4") #test all suites
  211. openssl_ciphers=`$OPENSSL ciphers "ALL"`
  212. all_sup=$?
  213. if [ $all_sup != 0 ]
  214. then
  215. echo -e "Not testing ALL. No OpenSSL support for ALL modifier"
  216. testing_summary="$testing_summary ALL\tNo\tN/A\tN/A\t (No OpenSSL Support for cipherstring)\n"
  217. continue
  218. fi
  219. ;;
  220. esac
  221. for wolfSuite in $wolf_ciphers; do
  222. echo -e "trying wolfSSL cipher suite $wolfSuite"
  223. wolf_temp_suites_total=$((wolf_temp_suites_total + 1))
  224. matchSuite=0;
  225. case ":$openssl_ciphers:" in *":$wolfSuite:"*) # add extra : for edge cases
  226. echo -e "Matched to OpenSSL suite support"
  227. matchSuite=1;;
  228. esac
  229. if [ $matchSuite = 0 ]
  230. then
  231. echo -e "Couldn't match suite, continuing..."
  232. continue
  233. fi
  234. # check for psk suite and turn on client psk if so
  235. psk=""
  236. adh=""
  237. port=$openssl_port
  238. caCert=""
  239. case $wolfSuite in
  240. *ECDH-RSA*)
  241. port=$ecdh_port ;;
  242. *ECDHE-ECDSA*|*ECDH-ECDSA*)
  243. caCert="-A./certs/ca-ecc-cert.pem" ;;
  244. *PSK*)
  245. psk="-s " ;;
  246. *ADH*)
  247. adh="-a " ;;
  248. esac
  249. if [ $version -lt 4 ]
  250. then
  251. ./examples/client/client -p $port -g -r -l $wolfSuite -v $version $psk $adh $caCert
  252. else
  253. # do all versions
  254. ./examples/client/client -p $port -g -r -l $wolfSuite $psk $adh $caCert
  255. fi
  256. client_result=$?
  257. if [ $client_result != 0 ]
  258. then
  259. echo -e "client failed! Suite = $wolfSuite version = $version"
  260. do_cleanup
  261. exit 1
  262. fi
  263. wolf_temp_suites_tested=$((wolf_temp_suites_tested+1))
  264. done
  265. wolf_suites_tested=$((wolf_temp_suites_tested+wolf_suites_tested))
  266. wolf_suites_total=$((wolf_temp_suites_total+wolf_suites_total))
  267. echo -e "wolfSSL suites tested with version:$version $wolf_temp_suites_tested"
  268. version_name
  269. testing_summary="$testing_summary$versionName\tYes\t$wolf_temp_suites_total\t$wolf_temp_suites_tested\n"
  270. wolf_temp_suites_total=0
  271. wolf_temp_suites_tested=0
  272. done
  273. IFS=$OIFS #restore separator
  274. kill -9 $server_pid
  275. if [ $ecdh_server_pid != $no_pid ]
  276. then
  277. kill -9 $ecdh_server_pid
  278. fi
  279. echo -e "wolfSSL total suites $wolf_suites_total"
  280. echo -e "wolfSSL suites tested $wolf_suites_tested"
  281. echo -e "\nSuccess!\n\n\n\n"
  282. echo -e "$testing_summary"
  283. exit 0