bad_dtls_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Unit test for Cisco DTLS1_BAD_VER session resume, as used by
  11. * AnyConnect VPN protocol.
  12. *
  13. * This is designed to exercise the code paths in
  14. * http://git.infradead.org/users/dwmw2/openconnect.git/blob/HEAD:/dtls.c
  15. * which have frequently been affected by regressions in DTLS1_BAD_VER
  16. * support.
  17. *
  18. * Note that unlike other SSL tests, we don't test against our own SSL
  19. * server method. Firstly because we don't have one; we *only* support
  20. * DTLS1_BAD_VER as a client. And secondly because even if that were
  21. * fixed up it's the wrong thing to test against - because if changes
  22. * are made in generic DTLS code which don't take DTLS1_BAD_VER into
  23. * account, there's plenty of scope for making those changes such that
  24. * they break *both* the client and the server in the same way.
  25. *
  26. * So we handle the server side manually. In a session resume there isn't
  27. * much to be done anyway.
  28. */
  29. #include <string.h>
  30. #include <openssl/core_names.h>
  31. #include <openssl/params.h>
  32. #include <openssl/opensslconf.h>
  33. #include <openssl/bio.h>
  34. #include <openssl/crypto.h>
  35. #include <openssl/evp.h>
  36. #include <openssl/ssl.h>
  37. #include <openssl/err.h>
  38. #include <openssl/rand.h>
  39. #include <openssl/kdf.h>
  40. #include "internal/packet.h"
  41. #include "internal/nelem.h"
  42. #include "testutil.h"
  43. /* For DTLS1_BAD_VER packets the MAC doesn't include the handshake header */
  44. #define MAC_OFFSET (DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH)
  45. static unsigned char client_random[SSL3_RANDOM_SIZE];
  46. static unsigned char server_random[SSL3_RANDOM_SIZE];
  47. /* These are all generated locally, sized purely according to our own whim */
  48. static unsigned char session_id[32];
  49. static unsigned char master_secret[48];
  50. static unsigned char cookie[20];
  51. /* We've hard-coded the cipher suite; we know it's 104 bytes */
  52. static unsigned char key_block[104];
  53. #define mac_key (key_block + 20)
  54. #define dec_key (key_block + 40)
  55. #define enc_key (key_block + 56)
  56. static EVP_MD_CTX *handshake_md;
  57. static int do_PRF(const void *seed1, int seed1_len,
  58. const void *seed2, int seed2_len,
  59. const void *seed3, int seed3_len,
  60. unsigned char *out, int olen)
  61. {
  62. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
  63. size_t outlen = olen;
  64. /* No error handling. If it all screws up, the test will fail anyway */
  65. EVP_PKEY_derive_init(pctx);
  66. EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1());
  67. EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, master_secret, sizeof(master_secret));
  68. EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, seed1_len);
  69. EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, seed2_len);
  70. EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, seed3_len);
  71. EVP_PKEY_derive(pctx, out, &outlen);
  72. EVP_PKEY_CTX_free(pctx);
  73. return 1;
  74. }
  75. static SSL_SESSION *client_session(void)
  76. {
  77. static unsigned char session_asn1[] = {
  78. 0x30, 0x5F, /* SEQUENCE, length 0x5F */
  79. 0x02, 0x01, 0x01, /* INTEGER, SSL_SESSION_ASN1_VERSION */
  80. 0x02, 0x02, 0x01, 0x00, /* INTEGER, DTLS1_BAD_VER */
  81. 0x04, 0x02, 0x00, 0x2F, /* OCTET_STRING, AES128-SHA */
  82. 0x04, 0x20, /* OCTET_STRING, session id */
  83. #define SS_SESSID_OFS 15 /* Session ID goes here */
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x04, 0x30, /* OCTET_STRING, master secret */
  89. #define SS_SECRET_OFS 49 /* Master secret goes here */
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. };
  97. const unsigned char *p = session_asn1;
  98. /* Copy the randomly-generated fields into the above ASN1 */
  99. memcpy(session_asn1 + SS_SESSID_OFS, session_id, sizeof(session_id));
  100. memcpy(session_asn1 + SS_SECRET_OFS, master_secret, sizeof(master_secret));
  101. return d2i_SSL_SESSION(NULL, &p, sizeof(session_asn1));
  102. }
  103. /* Returns 1 for initial ClientHello, 2 for ClientHello with cookie */
  104. static int validate_client_hello(BIO *wbio)
  105. {
  106. PACKET pkt, pkt2;
  107. long len;
  108. unsigned char *data;
  109. int cookie_found = 0;
  110. unsigned int u = 0;
  111. len = BIO_get_mem_data(wbio, (char **)&data);
  112. if (!PACKET_buf_init(&pkt, data, len))
  113. return 0;
  114. /* Check record header type */
  115. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE)
  116. return 0;
  117. /* Version */
  118. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  119. return 0;
  120. /* Skip the rest of the record header */
  121. if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3))
  122. return 0;
  123. /* Check it's a ClientHello */
  124. if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CLIENT_HELLO)
  125. return 0;
  126. /* Skip the rest of the handshake message header */
  127. if (!PACKET_forward(&pkt, DTLS1_HM_HEADER_LENGTH - 1))
  128. return 0;
  129. /* Check client version */
  130. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  131. return 0;
  132. /* Store random */
  133. if (!PACKET_copy_bytes(&pkt, client_random, SSL3_RANDOM_SIZE))
  134. return 0;
  135. /* Check session id length and content */
  136. if (!PACKET_get_length_prefixed_1(&pkt, &pkt2) ||
  137. !PACKET_equal(&pkt2, session_id, sizeof(session_id)))
  138. return 0;
  139. /* Check cookie */
  140. if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
  141. return 0;
  142. if (PACKET_remaining(&pkt2)) {
  143. if (!PACKET_equal(&pkt2, cookie, sizeof(cookie)))
  144. return 0;
  145. cookie_found = 1;
  146. }
  147. /* Skip ciphers */
  148. if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u))
  149. return 0;
  150. /* Skip compression */
  151. if (!PACKET_get_1(&pkt, &u) || !PACKET_forward(&pkt, u))
  152. return 0;
  153. /* Skip extensions */
  154. if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u))
  155. return 0;
  156. /* Now we are at the end */
  157. if (PACKET_remaining(&pkt))
  158. return 0;
  159. /* Update handshake MAC for second ClientHello (with cookie) */
  160. if (cookie_found && !EVP_DigestUpdate(handshake_md, data + MAC_OFFSET,
  161. len - MAC_OFFSET))
  162. return 0;
  163. (void)BIO_reset(wbio);
  164. return 1 + cookie_found;
  165. }
  166. static int send_hello_verify(BIO *rbio)
  167. {
  168. static unsigned char hello_verify[] = {
  169. 0x16, /* Handshake */
  170. 0x01, 0x00, /* DTLS1_BAD_VER */
  171. 0x00, 0x00, /* Epoch 0 */
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Seq# 0 */
  173. 0x00, 0x23, /* Length */
  174. 0x03, /* Hello Verify */
  175. 0x00, 0x00, 0x17, /* Length */
  176. 0x00, 0x00, /* Seq# 0 */
  177. 0x00, 0x00, 0x00, /* Fragment offset */
  178. 0x00, 0x00, 0x17, /* Fragment length */
  179. 0x01, 0x00, /* DTLS1_BAD_VER */
  180. 0x14, /* Cookie length */
  181. #define HV_COOKIE_OFS 28 /* Cookie goes here */
  182. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0x00,
  185. };
  186. memcpy(hello_verify + HV_COOKIE_OFS, cookie, sizeof(cookie));
  187. BIO_write(rbio, hello_verify, sizeof(hello_verify));
  188. return 1;
  189. }
  190. static int send_server_hello(BIO *rbio)
  191. {
  192. static unsigned char server_hello[] = {
  193. 0x16, /* Handshake */
  194. 0x01, 0x00, /* DTLS1_BAD_VER */
  195. 0x00, 0x00, /* Epoch 0 */
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* Seq# 1 */
  197. 0x00, 0x52, /* Length */
  198. 0x02, /* Server Hello */
  199. 0x00, 0x00, 0x46, /* Length */
  200. 0x00, 0x01, /* Seq# */
  201. 0x00, 0x00, 0x00, /* Fragment offset */
  202. 0x00, 0x00, 0x46, /* Fragment length */
  203. 0x01, 0x00, /* DTLS1_BAD_VER */
  204. #define SH_RANDOM_OFS 27 /* Server random goes here */
  205. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x20, /* Session ID length */
  210. #define SH_SESSID_OFS 60 /* Session ID goes here */
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x2f, /* Cipher suite AES128-SHA */
  216. 0x00, /* Compression null */
  217. };
  218. static unsigned char change_cipher_spec[] = {
  219. 0x14, /* Change Cipher Spec */
  220. 0x01, 0x00, /* DTLS1_BAD_VER */
  221. 0x00, 0x00, /* Epoch 0 */
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, /* Seq# 2 */
  223. 0x00, 0x03, /* Length */
  224. 0x01, 0x00, 0x02, /* Message */
  225. };
  226. memcpy(server_hello + SH_RANDOM_OFS, server_random, sizeof(server_random));
  227. memcpy(server_hello + SH_SESSID_OFS, session_id, sizeof(session_id));
  228. if (!EVP_DigestUpdate(handshake_md, server_hello + MAC_OFFSET,
  229. sizeof(server_hello) - MAC_OFFSET))
  230. return 0;
  231. BIO_write(rbio, server_hello, sizeof(server_hello));
  232. BIO_write(rbio, change_cipher_spec, sizeof(change_cipher_spec));
  233. return 1;
  234. }
  235. /* Create header, HMAC, pad, encrypt and send a record */
  236. static int send_record(BIO *rbio, unsigned char type, uint64_t seqnr,
  237. const void *msg, size_t len)
  238. {
  239. /* Note that the order of the record header fields on the wire,
  240. * and in the HMAC, is different. So we just keep them in separate
  241. * variables and handle them individually. */
  242. static unsigned char epoch[2] = { 0x00, 0x01 };
  243. static unsigned char seq[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  244. static unsigned char ver[2] = { 0x01, 0x00 }; /* DTLS1_BAD_VER */
  245. unsigned char lenbytes[2];
  246. EVP_MAC *hmac;
  247. EVP_MAC_CTX *ctx;
  248. EVP_CIPHER_CTX *enc_ctx;
  249. unsigned char iv[16];
  250. unsigned char pad;
  251. unsigned char *enc;
  252. OSSL_PARAM params[3];
  253. seq[0] = (seqnr >> 40) & 0xff;
  254. seq[1] = (seqnr >> 32) & 0xff;
  255. seq[2] = (seqnr >> 24) & 0xff;
  256. seq[3] = (seqnr >> 16) & 0xff;
  257. seq[4] = (seqnr >> 8) & 0xff;
  258. seq[5] = seqnr & 0xff;
  259. pad = 15 - ((len + SHA_DIGEST_LENGTH) % 16);
  260. enc = OPENSSL_malloc(len + SHA_DIGEST_LENGTH + 1 + pad);
  261. if (enc == NULL)
  262. return 0;
  263. /* Copy record to encryption buffer */
  264. memcpy(enc, msg, len);
  265. /* Append HMAC to data */
  266. hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
  267. ctx = EVP_MAC_CTX_new(hmac);
  268. EVP_MAC_free(hmac);
  269. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  270. "SHA1", 0);
  271. params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  272. mac_key, 20);
  273. params[2] = OSSL_PARAM_construct_end();
  274. EVP_MAC_CTX_set_params(ctx, params);
  275. EVP_MAC_init(ctx);
  276. EVP_MAC_update(ctx, epoch, 2);
  277. EVP_MAC_update(ctx, seq, 6);
  278. EVP_MAC_update(ctx, &type, 1);
  279. EVP_MAC_update(ctx, ver, 2); /* Version */
  280. lenbytes[0] = (unsigned char)(len >> 8);
  281. lenbytes[1] = (unsigned char)(len);
  282. EVP_MAC_update(ctx, lenbytes, 2); /* Length */
  283. EVP_MAC_update(ctx, enc, len); /* Finally the data itself */
  284. EVP_MAC_final(ctx, enc + len, NULL, SHA_DIGEST_LENGTH);
  285. EVP_MAC_CTX_free(ctx);
  286. /* Append padding bytes */
  287. len += SHA_DIGEST_LENGTH;
  288. do {
  289. enc[len++] = pad;
  290. } while (len % 16);
  291. /* Generate IV, and encrypt */
  292. RAND_bytes(iv, sizeof(iv));
  293. enc_ctx = EVP_CIPHER_CTX_new();
  294. EVP_CipherInit_ex(enc_ctx, EVP_aes_128_cbc(), NULL, enc_key, iv, 1);
  295. EVP_Cipher(enc_ctx, enc, enc, len);
  296. EVP_CIPHER_CTX_free(enc_ctx);
  297. /* Finally write header (from fragmented variables), IV and encrypted record */
  298. BIO_write(rbio, &type, 1);
  299. BIO_write(rbio, ver, 2);
  300. BIO_write(rbio, epoch, 2);
  301. BIO_write(rbio, seq, 6);
  302. lenbytes[0] = (unsigned char)((len + sizeof(iv)) >> 8);
  303. lenbytes[1] = (unsigned char)(len + sizeof(iv));
  304. BIO_write(rbio, lenbytes, 2);
  305. BIO_write(rbio, iv, sizeof(iv));
  306. BIO_write(rbio, enc, len);
  307. OPENSSL_free(enc);
  308. return 1;
  309. }
  310. static int send_finished(SSL *s, BIO *rbio)
  311. {
  312. static unsigned char finished_msg[DTLS1_HM_HEADER_LENGTH +
  313. TLS1_FINISH_MAC_LENGTH] = {
  314. 0x14, /* Finished */
  315. 0x00, 0x00, 0x0c, /* Length */
  316. 0x00, 0x03, /* Seq# 3 */
  317. 0x00, 0x00, 0x00, /* Fragment offset */
  318. 0x00, 0x00, 0x0c, /* Fragment length */
  319. /* Finished MAC (12 bytes) */
  320. };
  321. unsigned char handshake_hash[EVP_MAX_MD_SIZE];
  322. /* Derive key material */
  323. do_PRF(TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  324. server_random, SSL3_RANDOM_SIZE,
  325. client_random, SSL3_RANDOM_SIZE,
  326. key_block, sizeof(key_block));
  327. /* Generate Finished MAC */
  328. if (!EVP_DigestFinal_ex(handshake_md, handshake_hash, NULL))
  329. return 0;
  330. do_PRF(TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  331. handshake_hash, EVP_MD_CTX_size(handshake_md),
  332. NULL, 0,
  333. finished_msg + DTLS1_HM_HEADER_LENGTH, TLS1_FINISH_MAC_LENGTH);
  334. return send_record(rbio, SSL3_RT_HANDSHAKE, 0,
  335. finished_msg, sizeof(finished_msg));
  336. }
  337. static int validate_ccs(BIO *wbio)
  338. {
  339. PACKET pkt;
  340. long len;
  341. unsigned char *data;
  342. unsigned int u;
  343. len = BIO_get_mem_data(wbio, (char **)&data);
  344. if (!PACKET_buf_init(&pkt, data, len))
  345. return 0;
  346. /* Check record header type */
  347. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_CHANGE_CIPHER_SPEC)
  348. return 0;
  349. /* Version */
  350. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  351. return 0;
  352. /* Skip the rest of the record header */
  353. if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3))
  354. return 0;
  355. /* Check ChangeCipherSpec message */
  356. if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CCS)
  357. return 0;
  358. /* A DTLS1_BAD_VER ChangeCipherSpec also contains the
  359. * handshake sequence number (which is 2 here) */
  360. if (!PACKET_get_net_2(&pkt, &u) || u != 0x0002)
  361. return 0;
  362. /* Now check the Finished packet */
  363. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE)
  364. return 0;
  365. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  366. return 0;
  367. /* Check epoch is now 1 */
  368. if (!PACKET_get_net_2(&pkt, &u) || u != 0x0001)
  369. return 0;
  370. /* That'll do for now. If OpenSSL accepted *our* Finished packet
  371. * then it's evidently remembered that DTLS1_BAD_VER doesn't
  372. * include the handshake header in the MAC. There's not a lot of
  373. * point in implementing decryption here, just to check that it
  374. * continues to get it right for one more packet. */
  375. return 1;
  376. }
  377. #define NODROP(x) { x##UL, 0 }
  378. #define DROP(x) { x##UL, 1 }
  379. static struct {
  380. uint64_t seq;
  381. int drop;
  382. } tests[] = {
  383. NODROP(1), NODROP(3), NODROP(2),
  384. NODROP(0x1234), NODROP(0x1230), NODROP(0x1235),
  385. NODROP(0xffff), NODROP(0x10001), NODROP(0xfffe), NODROP(0x10000),
  386. DROP(0x10001), DROP(0xff), NODROP(0x100000), NODROP(0x800000), NODROP(0x7fffe1),
  387. NODROP(0xffffff), NODROP(0x1000000), NODROP(0xfffffe), DROP(0xffffff), NODROP(0x1000010),
  388. NODROP(0xfffffd), NODROP(0x1000011), DROP(0x12), NODROP(0x1000012),
  389. NODROP(0x1ffffff), NODROP(0x2000000), DROP(0x1ff00fe), NODROP(0x2000001),
  390. NODROP(0x20fffff), NODROP(0x2105500), DROP(0x20ffffe), NODROP(0x21054ff),
  391. NODROP(0x211ffff), DROP(0x2110000), NODROP(0x2120000)
  392. /* The last test should be NODROP, because a DROP wouldn't get tested. */
  393. };
  394. static int test_bad_dtls(void)
  395. {
  396. SSL_SESSION *sess = NULL;
  397. SSL_CTX *ctx = NULL;
  398. SSL *con = NULL;
  399. BIO *rbio = NULL;
  400. BIO *wbio = NULL;
  401. time_t now = 0;
  402. int testresult = 0;
  403. int ret;
  404. int i;
  405. RAND_bytes(session_id, sizeof(session_id));
  406. RAND_bytes(master_secret, sizeof(master_secret));
  407. RAND_bytes(cookie, sizeof(cookie));
  408. RAND_bytes(server_random + 4, sizeof(server_random) - 4);
  409. now = time(NULL);
  410. memcpy(server_random, &now, sizeof(now));
  411. sess = client_session();
  412. if (!TEST_ptr(sess))
  413. goto end;
  414. handshake_md = EVP_MD_CTX_new();
  415. if (!TEST_ptr(handshake_md)
  416. || !TEST_true(EVP_DigestInit_ex(handshake_md, EVP_md5_sha1(),
  417. NULL)))
  418. goto end;
  419. ctx = SSL_CTX_new(DTLS_client_method());
  420. if (!TEST_ptr(ctx)
  421. || !TEST_true(SSL_CTX_set_min_proto_version(ctx, DTLS1_BAD_VER))
  422. || !TEST_true(SSL_CTX_set_max_proto_version(ctx, DTLS1_BAD_VER))
  423. || !TEST_true(SSL_CTX_set_cipher_list(ctx, "AES128-SHA")))
  424. goto end;
  425. con = SSL_new(ctx);
  426. if (!TEST_ptr(con)
  427. || !TEST_true(SSL_set_session(con, sess)))
  428. goto end;
  429. SSL_SESSION_free(sess);
  430. rbio = BIO_new(BIO_s_mem());
  431. wbio = BIO_new(BIO_s_mem());
  432. if (!TEST_ptr(rbio)
  433. || !TEST_ptr(wbio))
  434. goto end;
  435. SSL_set_bio(con, rbio, wbio);
  436. if (!TEST_true(BIO_up_ref(rbio))) {
  437. /*
  438. * We can't up-ref but we assigned ownership to con, so we shouldn't
  439. * free in the "end" block
  440. */
  441. rbio = wbio = NULL;
  442. goto end;
  443. }
  444. if (!TEST_true(BIO_up_ref(wbio))) {
  445. wbio = NULL;
  446. goto end;
  447. }
  448. SSL_set_connect_state(con);
  449. /* Send initial ClientHello */
  450. ret = SSL_do_handshake(con);
  451. if (!TEST_int_le(ret, 0)
  452. || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ)
  453. || !TEST_int_eq(validate_client_hello(wbio), 1)
  454. || !TEST_true(send_hello_verify(rbio)))
  455. goto end;
  456. ret = SSL_do_handshake(con);
  457. if (!TEST_int_le(ret, 0)
  458. || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ)
  459. || !TEST_int_eq(validate_client_hello(wbio), 2)
  460. || !TEST_true(send_server_hello(rbio)))
  461. goto end;
  462. ret = SSL_do_handshake(con);
  463. if (!TEST_int_le(ret, 0)
  464. || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ)
  465. || !TEST_true(send_finished(con, rbio)))
  466. goto end;
  467. ret = SSL_do_handshake(con);
  468. if (!TEST_int_gt(ret, 0)
  469. || !TEST_true(validate_ccs(wbio)))
  470. goto end;
  471. /* While we're here and crafting packets by hand, we might as well do a
  472. bit of a stress test on the DTLS record replay handling. Not Cisco-DTLS
  473. specific but useful anyway for the general case. It's been broken
  474. before, and in fact was broken even for a basic 0, 2, 1 test case
  475. when this test was first added.... */
  476. for (i = 0; i < (int)OSSL_NELEM(tests); i++) {
  477. uint64_t recv_buf[2];
  478. if (!TEST_true(send_record(rbio, SSL3_RT_APPLICATION_DATA, tests[i].seq,
  479. &tests[i].seq, sizeof(uint64_t)))) {
  480. TEST_error("Failed to send data seq #0x%x%08x (%d)\n",
  481. (unsigned int)(tests[i].seq >> 32), (unsigned int)tests[i].seq, i);
  482. goto end;
  483. }
  484. if (tests[i].drop)
  485. continue;
  486. ret = SSL_read(con, recv_buf, 2 * sizeof(uint64_t));
  487. if (!TEST_int_eq(ret, (int)sizeof(uint64_t))) {
  488. TEST_error("SSL_read failed or wrong size on seq#0x%x%08x (%d)\n",
  489. (unsigned int)(tests[i].seq >> 32), (unsigned int)tests[i].seq, i);
  490. goto end;
  491. }
  492. if (!TEST_true(recv_buf[0] == tests[i].seq))
  493. goto end;
  494. }
  495. /* The last test cannot be DROP() */
  496. if (!TEST_false(tests[i-1].drop))
  497. goto end;
  498. testresult = 1;
  499. end:
  500. BIO_free(rbio);
  501. BIO_free(wbio);
  502. SSL_free(con);
  503. SSL_CTX_free(ctx);
  504. EVP_MD_CTX_free(handshake_md);
  505. return testresult;
  506. }
  507. int setup_tests(void)
  508. {
  509. ADD_TEST(test_bad_dtls);
  510. return 1;
  511. }