bad_dtls_test.c 20 KB

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