context-info.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #!/bin/sh
  2. # context-info.sh
  3. #
  4. # Copyright The Mbed TLS Contributors
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # This program is intended for testing the ssl_context_info program
  20. #
  21. set -eu
  22. if ! cd "$(dirname "$0")"; then
  23. exit 125
  24. fi
  25. # Variables
  26. THIS_SCRIPT_NAME=$(basename "$0")
  27. PROG_PATH="../programs/ssl/ssl_context_info"
  28. OUT_FILE="ssl_context_info.log"
  29. IN_DIR="data_files/base64"
  30. USE_VALGRIND=0
  31. T_COUNT=0
  32. T_PASSED=0
  33. T_FAILED=0
  34. # Functions
  35. print_usage() {
  36. echo "Usage: $0 [options]"
  37. printf " -h|--help\tPrint this help.\n"
  38. printf " -m|--memcheck\tUse valgrind to check the memory.\n"
  39. }
  40. # Print test name <name>
  41. print_name() {
  42. printf "%s %.*s " "$1" $(( 71 - ${#1} )) \
  43. "........................................................................"
  44. }
  45. # Print header to the test output file <test name> <file path> <test command>
  46. print_header()
  47. {
  48. date="$(date)"
  49. echo "******************************************************************" > $2
  50. echo "* File created by: $THIS_SCRIPT_NAME" >> $2
  51. echo "* Test name: $1" >> $2
  52. echo "* Date: $date" >> $2
  53. echo "* Command: $3" >> $2
  54. echo "******************************************************************" >> $2
  55. echo "" >> $2
  56. }
  57. # Print footer at the end of file <file path>
  58. print_footer()
  59. {
  60. echo "" >> $1
  61. echo "******************************************************************" >> $1
  62. echo "* End command" >> $1
  63. echo "******************************************************************" >> $1
  64. echo "" >> $1
  65. }
  66. # Use the arguments of this script
  67. get_options() {
  68. while [ $# -gt 0 ]; do
  69. case "$1" in
  70. -h|--help)
  71. print_usage
  72. exit 0
  73. ;;
  74. -m|--memcheck)
  75. USE_VALGRIND=1
  76. ;;
  77. *)
  78. echo "Unknown argument: '$1'"
  79. print_usage
  80. exit 1
  81. ;;
  82. esac
  83. shift
  84. done
  85. }
  86. # Current test failed
  87. fail()
  88. {
  89. T_FAILED=$(( $T_FAILED + 1))
  90. FAIL_OUT="Fail.$T_FAILED""_$OUT_FILE"
  91. echo "FAIL"
  92. echo " Error: $1"
  93. cp -f "$OUT_FILE" "$FAIL_OUT"
  94. echo "Error: $1" >> "$FAIL_OUT"
  95. }
  96. # Current test passed
  97. pass()
  98. {
  99. T_PASSED=$(( $T_PASSED + 1))
  100. echo "PASS"
  101. }
  102. # Usage: run_test <name> <input file with b64 code> [ -arg <extra arguments for tested program> ] [option [...]]
  103. # Options: -m <pattern that MUST be present in the output of tested program>
  104. # -n <pattern that must NOT be present in the output of tested program>
  105. # -u <pattern that must be UNIQUE in the output of tested program>
  106. run_test()
  107. {
  108. TEST_NAME="$1"
  109. RUN_CMD="$PROG_PATH -f $IN_DIR/$2"
  110. if [ "-arg" = "$3" ]; then
  111. RUN_CMD="$RUN_CMD $4"
  112. shift 4
  113. else
  114. shift 2
  115. fi
  116. # prepend valgrind to our commands if active
  117. if [ "$USE_VALGRIND" -gt 0 ]; then
  118. RUN_CMD="valgrind --leak-check=full $RUN_CMD"
  119. fi
  120. T_COUNT=$(( $T_COUNT + 1))
  121. print_name "$TEST_NAME"
  122. # run tested program
  123. print_header "$TEST_NAME" "$OUT_FILE" "$RUN_CMD"
  124. eval "$RUN_CMD" >> "$OUT_FILE" 2>&1
  125. print_footer "$OUT_FILE"
  126. # check valgrind's results
  127. if [ "$USE_VALGRIND" -gt 0 ]; then
  128. if ! ( grep -F 'All heap blocks were freed -- no leaks are possible' "$OUT_FILE" &&
  129. grep -F 'ERROR SUMMARY: 0 errors from 0 contexts' "$OUT_FILE" ) > /dev/null
  130. then
  131. fail "Memory error detected"
  132. return
  133. fi
  134. fi
  135. # check other assertions
  136. # lines beginning with == are added by valgrind, ignore them, because we already checked them before
  137. # lines with 'Serious error when reading debug info', are valgrind issues as well
  138. # lines beginning with * are added by this script, ignore too
  139. while [ $# -gt 0 ]
  140. do
  141. case $1 in
  142. "-m")
  143. if grep -v '^==' "$OUT_FILE" | grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" >/dev/null; then :; else
  144. fail "pattern '$2' MUST be present in the output"
  145. return
  146. fi
  147. ;;
  148. "-n")
  149. if grep -v '^==' "$OUT_FILE" | grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" >/dev/null; then
  150. fail "pattern '$2' MUST NOT be present in the output"
  151. return
  152. fi
  153. ;;
  154. "-u")
  155. if [ $(grep -v '^==' "$OUT_FILE"| grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" | wc -l) -ne 1 ]; then
  156. fail "lines following pattern '$2' must be once in the output"
  157. return
  158. fi
  159. ;;
  160. *)
  161. echo "Unknown test: $1" >&2
  162. exit 1
  163. esac
  164. shift 2
  165. done
  166. rm -f "$OUT_FILE"
  167. pass
  168. }
  169. get_options "$@"
  170. # Tests
  171. run_test "Default configuration, server" \
  172. "srv_def.txt" \
  173. -n "ERROR" \
  174. -u "major.* 2$" \
  175. -u "minor.* 21$" \
  176. -u "path.* 0$" \
  177. -u "MBEDTLS_HAVE_TIME$" \
  178. -u "MBEDTLS_X509_CRT_PARSE_C$" \
  179. -u "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
  180. -u "MBEDTLS_SSL_TRUNCATED_HMAC$" \
  181. -u "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
  182. -u "MBEDTLS_SSL_SESSION_TICKETS$" \
  183. -u "MBEDTLS_SSL_SESSION_TICKETS and client$" \
  184. -u "MBEDTLS_SSL_DTLS_BADMAC_LIMIT$" \
  185. -u "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
  186. -u "MBEDTLS_SSL_ALPN$" \
  187. -u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
  188. -u "cipher flags.* 0x00$" \
  189. -u "Message-Digest.* SHA256$" \
  190. -u "compression.* disabled$" \
  191. -u "DTLS datagram packing.* enabled$" \
  192. -n "Certificate" \
  193. -n "bytes left to analyze from context"
  194. run_test "Default configuration, client" \
  195. "cli_def.txt" \
  196. -n "ERROR" \
  197. -u "major.* 2$" \
  198. -u "minor.* 21$" \
  199. -u "path.* 0$" \
  200. -u "MBEDTLS_HAVE_TIME$" \
  201. -u "MBEDTLS_X509_CRT_PARSE_C$" \
  202. -u "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
  203. -u "MBEDTLS_SSL_TRUNCATED_HMAC$" \
  204. -u "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
  205. -u "MBEDTLS_SSL_SESSION_TICKETS$" \
  206. -u "MBEDTLS_SSL_SESSION_TICKETS and client$" \
  207. -u "MBEDTLS_SSL_DTLS_BADMAC_LIMIT$" \
  208. -u "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
  209. -u "MBEDTLS_SSL_ALPN$" \
  210. -u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
  211. -u "cipher flags.* 0x00$" \
  212. -u "Message-Digest.* SHA256$" \
  213. -u "compression.* disabled$" \
  214. -u "DTLS datagram packing.* enabled$" \
  215. -u "cert. version .* 3$" \
  216. -u "serial number.* 02$" \
  217. -u "issuer name.* C=NL, O=PolarSSL, CN=PolarSSL Test CA$" \
  218. -u "subject name.* C=NL, O=PolarSSL, CN=localhost$" \
  219. -u "issued on.* 2019-02-10 14:44:06$" \
  220. -u "expires on.* 2029-02-10 14:44:06$" \
  221. -u "signed using.* RSA with SHA-256$" \
  222. -u "RSA key size.* 2048 bits$" \
  223. -u "basic constraints.* CA=false$" \
  224. -n "bytes left to analyze from context"
  225. run_test "Ciphersuite TLS-RSA-WITH-AES-256-CCM-8, server" \
  226. "srv_ciphersuite.txt" \
  227. -n "ERROR" \
  228. -u "ciphersuite.* TLS-RSA-WITH-AES-256-CCM-8$" \
  229. run_test "Ciphersuite TLS-RSA-WITH-AES-256-CCM-8, client" \
  230. "cli_ciphersuite.txt" \
  231. -n "ERROR" \
  232. -u "ciphersuite.* TLS-RSA-WITH-AES-256-CCM-8$" \
  233. run_test "No packing, server" \
  234. "srv_no_packing.txt" \
  235. -n "ERROR" \
  236. -u "DTLS datagram packing.* disabled"
  237. run_test "No packing, client" \
  238. "cli_no_packing.txt" \
  239. -n "ERROR" \
  240. -u "DTLS datagram packing.* disabled"
  241. run_test "DTLS CID, server" \
  242. "srv_cid.txt" \
  243. -n "ERROR" \
  244. -u "in CID.* DE AD" \
  245. -u "out CID.* BE EF"
  246. run_test "DTLS CID, client" \
  247. "cli_cid.txt" \
  248. -n "ERROR" \
  249. -u "in CID.* BE EF" \
  250. -u "out CID.* DE AD"
  251. run_test "No MBEDTLS_SSL_MAX_FRAGMENT_LENGTH, server" \
  252. "srv_no_mfl.txt" \
  253. -n "ERROR" \
  254. -n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
  255. run_test "No MBEDTLS_SSL_MAX_FRAGMENT_LENGTH, client" \
  256. "cli_no_mfl.txt" \
  257. -n "ERROR" \
  258. -n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
  259. run_test "No MBEDTLS_SSL_ALPN, server" \
  260. "srv_no_alpn.txt" \
  261. -n "ERROR" \
  262. -n "MBEDTLS_SSL_ALPN"
  263. run_test "No MBEDTLS_SSL_ALPN, client" \
  264. "cli_no_alpn.txt" \
  265. -n "ERROR" \
  266. -n "MBEDTLS_SSL_ALPN"
  267. run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, server" \
  268. "srv_no_keep_cert.txt" \
  269. -arg "--keep-peer-cert=0" \
  270. -u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
  271. -u "cipher flags.* 0x00" \
  272. -u "compression.* disabled" \
  273. -u "DTLS datagram packing.* enabled" \
  274. -n "ERROR"
  275. run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, client" \
  276. "cli_no_keep_cert.txt" \
  277. -arg "--keep-peer-cert=0" \
  278. -u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
  279. -u "cipher flags.* 0x00" \
  280. -u "compression.* disabled" \
  281. -u "DTLS datagram packing.* enabled" \
  282. -n "ERROR"
  283. run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, negative, server" \
  284. "srv_no_keep_cert.txt" \
  285. -m "Deserializing" \
  286. -m "ERROR"
  287. run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, negative, client" \
  288. "cli_no_keep_cert.txt" \
  289. -m "Deserializing" \
  290. -m "ERROR"
  291. run_test "Minimal configuration, server" \
  292. "srv_min_cfg.txt" \
  293. -n "ERROR" \
  294. -n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
  295. -n "MBEDTLS_SSL_TRUNCATED_HMAC$" \
  296. -n "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
  297. -n "MBEDTLS_SSL_SESSION_TICKETS$" \
  298. -n "MBEDTLS_SSL_SESSION_TICKETS and client$" \
  299. -n "MBEDTLS_SSL_DTLS_BADMAC_LIMIT$" \
  300. -n "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
  301. -n "MBEDTLS_SSL_ALPN$" \
  302. run_test "Minimal configuration, client" \
  303. "cli_min_cfg.txt" \
  304. -n "ERROR" \
  305. -n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
  306. -n "MBEDTLS_SSL_TRUNCATED_HMAC$" \
  307. -n "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
  308. -n "MBEDTLS_SSL_SESSION_TICKETS$" \
  309. -n "MBEDTLS_SSL_SESSION_TICKETS and client$" \
  310. -n "MBEDTLS_SSL_DTLS_BADMAC_LIMIT$" \
  311. -n "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
  312. -n "MBEDTLS_SSL_ALPN$" \
  313. run_test "MTU=10000" \
  314. "mtu_10000.txt" \
  315. -n "ERROR" \
  316. -u "MTU.* 10000$"
  317. run_test "MFL=1024" \
  318. "mfl_1024.txt" \
  319. -n "ERROR" \
  320. -u "MFL.* 1024$"
  321. run_test "Older version (v2.19.1)" \
  322. "v2.19.1.txt" \
  323. -n "ERROR" \
  324. -u "major.* 2$" \
  325. -u "minor.* 19$" \
  326. -u "path.* 1$" \
  327. -u "ciphersuite.* TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8$" \
  328. -u "Message-Digest.* SHA256$" \
  329. -u "compression.* disabled$" \
  330. -u "serial number.* 01:70:AF:40:B4:E6$" \
  331. -u "issuer name.* CN=ca$" \
  332. -u "subject name.* L=160001, OU=acc1, CN=device01$" \
  333. -u "issued on.* 2020-03-06 09:50:18$" \
  334. -u "expires on.* 2056-02-26 09:50:18$" \
  335. -u "signed using.* ECDSA with SHA256$" \
  336. -u "lifetime.* 0 sec.$" \
  337. -u "MFL.* none$" \
  338. -u "negotiate truncated HMAC.* disabled$" \
  339. -u "Encrypt-then-MAC.* enabled$" \
  340. -u "DTLS datagram packing.* enabled$" \
  341. -u "verify result.* 0x00000000$" \
  342. -n "bytes left to analyze from context"
  343. run_test "Wrong base64 format" \
  344. "def_bad_b64.txt" \
  345. -m "ERROR" \
  346. -u "The length of the base64 code found should be a multiple of 4" \
  347. -n "bytes left to analyze from context"
  348. run_test "Too much data at the beginning of base64 code" \
  349. "def_b64_too_big_1.txt" \
  350. -m "ERROR" \
  351. -n "The length of the base64 code found should be a multiple of 4" \
  352. run_test "Too much data in the middle of base64 code" \
  353. "def_b64_too_big_2.txt" \
  354. -m "ERROR" \
  355. -n "The length of the base64 code found should be a multiple of 4" \
  356. run_test "Too much data at the end of base64 code" \
  357. "def_b64_too_big_3.txt" \
  358. -m "ERROR" \
  359. -n "The length of the base64 code found should be a multiple of 4" \
  360. -u "bytes left to analyze from context"
  361. run_test "Empty file as input" \
  362. "empty.txt" \
  363. -u "Finished. No valid base64 code found"
  364. run_test "Not empty file without base64 code" \
  365. "../../context-info.sh" \
  366. -n "Deserializing"
  367. run_test "Binary file instead of text file" \
  368. "../../../programs/ssl/ssl_context_info" \
  369. -m "ERROR" \
  370. -u "Too many bad symbols detected. File check aborted" \
  371. -n "Deserializing"
  372. run_test "Decoder continues past 0xff character" \
  373. "def_b64_ff.bin" \
  374. -n "No valid base64" \
  375. -u "ciphersuite.* TLS-"
  376. # End of tests
  377. echo
  378. if [ $T_FAILED -eq 0 ]; then
  379. echo "PASSED ( $T_COUNT tests )"
  380. else
  381. echo "FAILED ( $T_FAILED / $T_COUNT tests )"
  382. fi
  383. exit $T_FAILED