ssl_test_common_source.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Common source code for SSL test programs. This file is included by
  3. * both ssl_client2.c and ssl_server2.c and is intended for source
  4. * code that is textually identical in both programs, but that cannot be
  5. * compiled separately because it refers to types or macros that are
  6. * different in the two programs, or because it would have an incomplete
  7. * type.
  8. *
  9. * This file is meant to be #include'd and cannot be compiled separately.
  10. *
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. void eap_tls_key_derivation(void *p_expkey,
  27. mbedtls_ssl_key_export_type secret_type,
  28. const unsigned char *secret,
  29. size_t secret_len,
  30. const unsigned char client_random[32],
  31. const unsigned char server_random[32],
  32. mbedtls_tls_prf_types tls_prf_type)
  33. {
  34. eap_tls_keys *keys = (eap_tls_keys *) p_expkey;
  35. /* We're only interested in the TLS 1.2 master secret */
  36. if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
  37. return;
  38. }
  39. if (secret_len != sizeof(keys->master_secret)) {
  40. return;
  41. }
  42. memcpy(keys->master_secret, secret, sizeof(keys->master_secret));
  43. memcpy(keys->randbytes, client_random, 32);
  44. memcpy(keys->randbytes + 32, server_random, 32);
  45. keys->tls_prf_type = tls_prf_type;
  46. }
  47. void nss_keylog_export(void *p_expkey,
  48. mbedtls_ssl_key_export_type secret_type,
  49. const unsigned char *secret,
  50. size_t secret_len,
  51. const unsigned char client_random[32],
  52. const unsigned char server_random[32],
  53. mbedtls_tls_prf_types tls_prf_type)
  54. {
  55. char nss_keylog_line[200];
  56. size_t const client_random_len = 32;
  57. size_t len = 0;
  58. size_t j;
  59. /* We're only interested in the TLS 1.2 master secret */
  60. if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
  61. return;
  62. }
  63. ((void) p_expkey);
  64. ((void) server_random);
  65. ((void) tls_prf_type);
  66. len += sprintf(nss_keylog_line + len,
  67. "%s", "CLIENT_RANDOM ");
  68. for (j = 0; j < client_random_len; j++) {
  69. len += sprintf(nss_keylog_line + len,
  70. "%02x", client_random[j]);
  71. }
  72. len += sprintf(nss_keylog_line + len, " ");
  73. for (j = 0; j < secret_len; j++) {
  74. len += sprintf(nss_keylog_line + len,
  75. "%02x", secret[j]);
  76. }
  77. len += sprintf(nss_keylog_line + len, "\n");
  78. nss_keylog_line[len] = '\0';
  79. mbedtls_printf("\n");
  80. mbedtls_printf("---------------- NSS KEYLOG -----------------\n");
  81. mbedtls_printf("%s", nss_keylog_line);
  82. mbedtls_printf("---------------------------------------------\n");
  83. if (opt.nss_keylog_file != NULL) {
  84. FILE *f;
  85. if ((f = fopen(opt.nss_keylog_file, "a")) == NULL) {
  86. goto exit;
  87. }
  88. /* Ensure no stdio buffering of secrets, as such buffers cannot be
  89. * wiped. */
  90. mbedtls_setbuf(f, NULL);
  91. if (fwrite(nss_keylog_line, 1, len, f) != len) {
  92. fclose(f);
  93. goto exit;
  94. }
  95. fclose(f);
  96. }
  97. exit:
  98. mbedtls_platform_zeroize(nss_keylog_line,
  99. sizeof(nss_keylog_line));
  100. }
  101. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  102. void dtls_srtp_key_derivation(void *p_expkey,
  103. mbedtls_ssl_key_export_type secret_type,
  104. const unsigned char *secret,
  105. size_t secret_len,
  106. const unsigned char client_random[32],
  107. const unsigned char server_random[32],
  108. mbedtls_tls_prf_types tls_prf_type)
  109. {
  110. dtls_srtp_keys *keys = (dtls_srtp_keys *) p_expkey;
  111. /* We're only interested in the TLS 1.2 master secret */
  112. if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
  113. return;
  114. }
  115. if (secret_len != sizeof(keys->master_secret)) {
  116. return;
  117. }
  118. memcpy(keys->master_secret, secret, sizeof(keys->master_secret));
  119. memcpy(keys->randbytes, client_random, 32);
  120. memcpy(keys->randbytes + 32, server_random, 32);
  121. keys->tls_prf_type = tls_prf_type;
  122. }
  123. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  124. int ssl_check_record(mbedtls_ssl_context const *ssl,
  125. unsigned char const *buf, size_t len)
  126. {
  127. int my_ret = 0, ret_cr1, ret_cr2;
  128. unsigned char *tmp_buf;
  129. /* Record checking may modify the input buffer,
  130. * so make a copy. */
  131. tmp_buf = mbedtls_calloc(1, len);
  132. if (tmp_buf == NULL) {
  133. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  134. }
  135. memcpy(tmp_buf, buf, len);
  136. ret_cr1 = mbedtls_ssl_check_record(ssl, tmp_buf, len);
  137. if (ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
  138. /* Test-only: Make sure that mbedtls_ssl_check_record()
  139. * doesn't alter state. */
  140. memcpy(tmp_buf, buf, len); /* Restore buffer */
  141. ret_cr2 = mbedtls_ssl_check_record(ssl, tmp_buf, len);
  142. if (ret_cr2 != ret_cr1) {
  143. mbedtls_printf("mbedtls_ssl_check_record() returned inconsistent results.\n");
  144. my_ret = -1;
  145. goto cleanup;
  146. }
  147. switch (ret_cr1) {
  148. case 0:
  149. break;
  150. case MBEDTLS_ERR_SSL_INVALID_RECORD:
  151. if (opt.debug_level > 1) {
  152. mbedtls_printf("mbedtls_ssl_check_record() detected invalid record.\n");
  153. }
  154. break;
  155. case MBEDTLS_ERR_SSL_INVALID_MAC:
  156. if (opt.debug_level > 1) {
  157. mbedtls_printf("mbedtls_ssl_check_record() detected unauthentic record.\n");
  158. }
  159. break;
  160. case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
  161. if (opt.debug_level > 1) {
  162. mbedtls_printf("mbedtls_ssl_check_record() detected unexpected record.\n");
  163. }
  164. break;
  165. default:
  166. mbedtls_printf("mbedtls_ssl_check_record() failed fatally with -%#04x.\n",
  167. (unsigned int) -ret_cr1);
  168. my_ret = -1;
  169. goto cleanup;
  170. }
  171. /* Regardless of the outcome, forward the record to the stack. */
  172. }
  173. cleanup:
  174. mbedtls_free(tmp_buf);
  175. return my_ret;
  176. }
  177. int recv_cb(void *ctx, unsigned char *buf, size_t len)
  178. {
  179. io_ctx_t *io_ctx = (io_ctx_t *) ctx;
  180. size_t recv_len;
  181. int ret;
  182. if (opt.nbio == 2) {
  183. ret = delayed_recv(io_ctx->net, buf, len);
  184. } else {
  185. ret = mbedtls_net_recv(io_ctx->net, buf, len);
  186. }
  187. if (ret < 0) {
  188. return ret;
  189. }
  190. recv_len = (size_t) ret;
  191. if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  192. /* Here's the place to do any datagram/record checking
  193. * in between receiving the packet from the underlying
  194. * transport and passing it on to the TLS stack. */
  195. if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
  196. return -1;
  197. }
  198. }
  199. return (int) recv_len;
  200. }
  201. int recv_timeout_cb(void *ctx, unsigned char *buf, size_t len,
  202. uint32_t timeout)
  203. {
  204. io_ctx_t *io_ctx = (io_ctx_t *) ctx;
  205. int ret;
  206. size_t recv_len;
  207. ret = mbedtls_net_recv_timeout(io_ctx->net, buf, len, timeout);
  208. if (ret < 0) {
  209. return ret;
  210. }
  211. recv_len = (size_t) ret;
  212. if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  213. /* Here's the place to do any datagram/record checking
  214. * in between receiving the packet from the underlying
  215. * transport and passing it on to the TLS stack. */
  216. if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
  217. return -1;
  218. }
  219. }
  220. return (int) recv_len;
  221. }
  222. int send_cb(void *ctx, unsigned char const *buf, size_t len)
  223. {
  224. io_ctx_t *io_ctx = (io_ctx_t *) ctx;
  225. if (opt.nbio == 2) {
  226. return delayed_send(io_ctx->net, buf, len);
  227. }
  228. return mbedtls_net_send(io_ctx->net, buf, len);
  229. }
  230. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  231. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && defined(MBEDTLS_RSA_C)
  232. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  233. /*
  234. * When GnuTLS/Openssl server is configured in TLS 1.2 mode with a certificate
  235. * declaring an RSA public key and Mbed TLS is configured in hybrid mode, if
  236. * `rsa_pss_rsae_*` algorithms are before `rsa_pkcs1_*` ones in this list then
  237. * the GnuTLS/Openssl server chooses an `rsa_pss_rsae_*` signature algorithm
  238. * for its signature in the key exchange message. As Mbed TLS 1.2 does not
  239. * support them, the handshake fails.
  240. */
  241. #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
  242. ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
  243. (0x800 | hash),
  244. #else
  245. #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
  246. ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
  247. #endif
  248. #elif defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  249. #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA),
  250. #elif defined(MBEDTLS_RSA_C)
  251. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  252. /* See above */
  253. #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
  254. (0x800 | hash),
  255. #else
  256. #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
  257. #endif
  258. #else
  259. #define MBEDTLS_SSL_SIG_ALG(hash)
  260. #endif
  261. uint16_t ssl_sig_algs_for_test[] = {
  262. #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  263. MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA512)
  264. #endif
  265. #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  266. MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA384)
  267. #endif
  268. #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  269. MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA256)
  270. #endif
  271. #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  272. MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA224)
  273. #endif
  274. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  275. MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
  276. #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
  277. #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
  278. /* Allow SHA-1 as we use it extensively in tests. */
  279. MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA1)
  280. #endif
  281. MBEDTLS_TLS1_3_SIG_NONE
  282. };
  283. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  284. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  285. /** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
  286. * for more info.
  287. */
  288. int x509_crt_verify_info(char *buf, size_t size, const char *prefix,
  289. uint32_t flags)
  290. {
  291. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  292. return mbedtls_x509_crt_verify_info(buf, size, prefix, flags);
  293. #else /* !MBEDTLS_X509_REMOVE_INFO */
  294. int ret;
  295. char *p = buf;
  296. size_t n = size;
  297. #define X509_CRT_ERROR_INFO(err, err_str, info) \
  298. if ((flags & err) != 0) \
  299. { \
  300. ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, info); \
  301. MBEDTLS_X509_SAFE_SNPRINTF; \
  302. flags ^= err; \
  303. }
  304. MBEDTLS_X509_CRT_ERROR_INFO_LIST
  305. #undef X509_CRT_ERROR_INFO
  306. if (flags != 0) {
  307. ret = mbedtls_snprintf(p, n, "%sUnknown reason "
  308. "(this should not happen)\n", prefix);
  309. MBEDTLS_X509_SAFE_SNPRINTF;
  310. }
  311. return (int) (size - n);
  312. #endif /* MBEDTLS_X509_REMOVE_INFO */
  313. }
  314. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  315. void mbedtls_print_supported_sig_algs(void)
  316. {
  317. mbedtls_printf("supported signature algorithms:\n");
  318. mbedtls_printf("\trsa_pkcs1_sha256 ");
  319. mbedtls_printf("rsa_pkcs1_sha384 ");
  320. mbedtls_printf("rsa_pkcs1_sha512\n");
  321. mbedtls_printf("\tecdsa_secp256r1_sha256 ");
  322. mbedtls_printf("ecdsa_secp384r1_sha384 ");
  323. mbedtls_printf("ecdsa_secp521r1_sha512\n");
  324. mbedtls_printf("\trsa_pss_rsae_sha256 ");
  325. mbedtls_printf("rsa_pss_rsae_sha384 ");
  326. mbedtls_printf("rsa_pss_rsae_sha512\n");
  327. mbedtls_printf("\trsa_pss_pss_sha256 ");
  328. mbedtls_printf("rsa_pss_pss_sha384 ");
  329. mbedtls_printf("rsa_pss_pss_sha512\n");
  330. mbedtls_printf("\ted25519 ");
  331. mbedtls_printf("ed448 ");
  332. mbedtls_printf("rsa_pkcs1_sha1 ");
  333. mbedtls_printf("ecdsa_sha1\n");
  334. mbedtls_printf("\n");
  335. }