bad_dtls_test.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2016 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. /*
  18. * Unit test for Cisco DTLS1_BAD_VER session resume, as used by
  19. * AnyConnect VPN protocol.
  20. *
  21. * This is designed to exercise the code paths in
  22. * https://git.infradead.org/users/dwmw2/openconnect.git/blob/HEAD:/dtls.c
  23. * which have frequently been affected by regressions in DTLS1_BAD_VER
  24. * support.
  25. *
  26. * Note that unlike other SSL tests, we don't test against our own SSL
  27. * server method. Firstly because we don't have one; we *only* support
  28. * DTLS1_BAD_VER as a client. And secondly because even if that were
  29. * fixed up it's the wrong thing to test against — because if changes
  30. * are made in generic DTLS code which don't take DTLS1_BAD_VER into
  31. * account, there's plenty of scope for making those changes such that
  32. * they break *both* the client and the server in the same way.
  33. *
  34. * So we handle the server side manually. In a session resume there isn't
  35. * much to be done anyway.
  36. */
  37. #include <string.h>
  38. #define OPENSSL_SUPPRESS_DEPRECATED
  39. #include <openssl/bio.h>
  40. #include <openssl/crypto.h>
  41. #include <openssl/evp.h>
  42. #include <openssl/ssl.h>
  43. #include <openssl/err.h>
  44. #include <openssl/rand.h>
  45. /* LibreSSL lacks this. Let it fail on testing, not building. */
  46. #ifndef DTLS1_BAD_VER
  47. #define DTLS1_BAD_VER 0x100
  48. #endif
  49. /* PACKET functions lifted from OpenSSL 1.1's ssl/packet_locl.h. Permission
  50. * requested in https://github.com/openssl/openssl/pull/1296 for reuse here
  51. * as an OpenConnect test case. */
  52. /*
  53. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  54. *
  55. * Licensed under the OpenSSL license (the "License"). You may not use
  56. * this file except in compliance with the License. You can obtain a copy
  57. * in the file LICENSE in the source distribution or at
  58. * https://www.openssl.org/source/license.html
  59. */
  60. typedef struct {
  61. /* Pointer to where we are currently reading from */
  62. const unsigned char *curr;
  63. /* Number of bytes remaining */
  64. size_t remaining;
  65. } PACKET;
  66. /* Internal unchecked shorthand; don't use outside this file. */
  67. static inline void packet_forward(PACKET *pkt, size_t len)
  68. {
  69. pkt->curr += len;
  70. pkt->remaining -= len;
  71. }
  72. /*
  73. * Returns the number of bytes remaining to be read in the PACKET
  74. */
  75. static inline size_t PACKET_remaining(const PACKET *pkt)
  76. {
  77. return pkt->remaining;
  78. }
  79. /*
  80. * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
  81. * copy of the data so |buf| must be present for the whole time that the PACKET
  82. * is being used.
  83. */
  84. static inline int PACKET_buf_init(PACKET *pkt,
  85. const unsigned char *buf,
  86. size_t len)
  87. {
  88. /* Sanity check for negative values. */
  89. if (len > (size_t)65536)
  90. return 0;
  91. pkt->curr = buf;
  92. pkt->remaining = len;
  93. return 1;
  94. }
  95. /*
  96. * Returns 1 if the packet has length |num| and its contents equal the |num|
  97. * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
  98. * If lengths are equal, performs the comparison in constant time.
  99. */
  100. static inline int PACKET_equal(const PACKET *pkt, const void *ptr,
  101. size_t num)
  102. {
  103. if (PACKET_remaining(pkt) != num)
  104. return 0;
  105. return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
  106. }
  107. /*
  108. * Peek ahead at 2 bytes in network order from |pkt| and store the value in
  109. * |*data|
  110. */
  111. static inline int PACKET_peek_net_2(const PACKET *pkt,
  112. unsigned int *data)
  113. {
  114. if (PACKET_remaining(pkt) < 2)
  115. return 0;
  116. *data = ((unsigned int)(*pkt->curr)) << 8;
  117. *data |= *(pkt->curr + 1);
  118. return 1;
  119. }
  120. /* Equivalent of n2s */
  121. /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
  122. static inline int PACKET_get_net_2(PACKET *pkt,
  123. unsigned int *data)
  124. {
  125. if (!PACKET_peek_net_2(pkt, data))
  126. return 0;
  127. packet_forward(pkt, 2);
  128. return 1;
  129. }
  130. /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
  131. static inline int PACKET_peek_1(const PACKET *pkt,
  132. unsigned int *data)
  133. {
  134. if (!PACKET_remaining(pkt))
  135. return 0;
  136. *data = *pkt->curr;
  137. return 1;
  138. }
  139. /* Get 1 byte from |pkt| and store the value in |*data| */
  140. static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
  141. {
  142. if (!PACKET_peek_1(pkt, data))
  143. return 0;
  144. packet_forward(pkt, 1);
  145. return 1;
  146. }
  147. /*
  148. * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
  149. * |*data|. This just points at the underlying buffer that |pkt| is using. The
  150. * caller should not free this data directly (it will be freed when the
  151. * underlying buffer gets freed
  152. */
  153. static inline int PACKET_peek_bytes(const PACKET *pkt,
  154. const unsigned char **data,
  155. size_t len)
  156. {
  157. if (PACKET_remaining(pkt) < len)
  158. return 0;
  159. *data = pkt->curr;
  160. return 1;
  161. }
  162. /*
  163. * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
  164. * just points at the underlying buffer that |pkt| is using. The caller should
  165. * not free this data directly (it will be freed when the underlying buffer gets
  166. * freed
  167. */
  168. static inline int PACKET_get_bytes(PACKET *pkt,
  169. const unsigned char **data,
  170. size_t len)
  171. {
  172. if (!PACKET_peek_bytes(pkt, data, len))
  173. return 0;
  174. packet_forward(pkt, len);
  175. return 1;
  176. }
  177. /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
  178. static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
  179. unsigned char *data,
  180. size_t len)
  181. {
  182. if (PACKET_remaining(pkt) < len)
  183. return 0;
  184. memcpy(data, pkt->curr, len);
  185. return 1;
  186. }
  187. /*
  188. * Read |len| bytes from |pkt| and copy them to |data|.
  189. * The caller is responsible for ensuring that |data| can hold |len| bytes.
  190. */
  191. static inline int PACKET_copy_bytes(PACKET *pkt,
  192. unsigned char *data,
  193. size_t len)
  194. {
  195. if (!PACKET_peek_copy_bytes(pkt, data, len))
  196. return 0;
  197. packet_forward(pkt, len);
  198. return 1;
  199. }
  200. /* Move the current reading position forward |len| bytes */
  201. static inline int PACKET_forward(PACKET *pkt, size_t len)
  202. {
  203. if (PACKET_remaining(pkt) < len)
  204. return 0;
  205. packet_forward(pkt, len);
  206. return 1;
  207. }
  208. /*
  209. * Reads a variable-length vector prefixed with a one-byte length, and stores
  210. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  211. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  212. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  213. * Upon failure, the original |pkt| and |subpkt| are not modified.
  214. */
  215. static inline int PACKET_get_length_prefixed_1(PACKET *pkt,
  216. PACKET *subpkt)
  217. {
  218. unsigned int length;
  219. const unsigned char *data;
  220. PACKET tmp = *pkt;
  221. if (!PACKET_get_1(&tmp, &length) ||
  222. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  223. return 0;
  224. }
  225. *pkt = tmp;
  226. subpkt->curr = data;
  227. subpkt->remaining = length;
  228. return 1;
  229. }
  230. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  231. /* For DTLS1_BAD_VER packets the MAC doesn't include the handshake header */
  232. #define MAC_OFFSET (DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH)
  233. static unsigned char client_random[SSL3_RANDOM_SIZE];
  234. static unsigned char server_random[SSL3_RANDOM_SIZE];
  235. /* These are all generated locally, sized purely according to our own whim */
  236. static unsigned char session_id[32];
  237. static unsigned char master_secret[48];
  238. static unsigned char cookie[20];
  239. /* We've hard-coded the cipher suite; we know it's 104 bytes */
  240. static unsigned char key_block[104];
  241. #define mac_key (key_block + 20)
  242. #define dec_key (key_block + 40)
  243. #define enc_key (key_block + 56)
  244. static EVP_MD_CTX *handshake_md5;
  245. static EVP_MD_CTX *handshake_sha1;
  246. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  247. static inline HMAC_CTX *HMAC_CTX_new(void)
  248. {
  249. HMAC_CTX *ret = malloc(sizeof(*ret));
  250. HMAC_CTX_init(ret);
  251. return ret;
  252. }
  253. static inline void HMAC_CTX_free(HMAC_CTX *ctx)
  254. {
  255. HMAC_CTX_cleanup(ctx);
  256. free(ctx);
  257. }
  258. #define EVP_MD_CTX_new EVP_MD_CTX_create
  259. #define EVP_MD_CTX_free EVP_MD_CTX_destroy
  260. #endif
  261. static int tls1_P_hash(const EVP_MD *md,
  262. const unsigned char *sec, int sec_len,
  263. const void *seed1, int seed1_len,
  264. const void *seed2, int seed2_len,
  265. const void *seed3, int seed3_len,
  266. unsigned char *out, int olen)
  267. {
  268. unsigned char A1[EVP_MAX_MD_SIZE];
  269. HMAC_CTX *ctx = HMAC_CTX_new();
  270. unsigned int chunk;
  271. int i = 0;
  272. HMAC_Init_ex(ctx, sec, sec_len, md, NULL);
  273. do {
  274. if (i)
  275. HMAC_Update(ctx, A1, chunk);
  276. if (seed1) HMAC_Update(ctx, seed1, seed1_len);
  277. if (seed2) HMAC_Update(ctx, seed2, seed2_len);
  278. if (seed3) HMAC_Update(ctx, seed3, seed3_len);
  279. /* First generate A1 from the seed */
  280. if (!i)
  281. HMAC_Final(ctx, A1, &chunk);
  282. else if (i * chunk <= olen) {
  283. HMAC_Final(ctx, out + ((i-1) * chunk), NULL);
  284. /* calculate A(n+1) value */
  285. HMAC(md, sec, sec_len, A1, chunk, A1, NULL);
  286. } else {
  287. HMAC_Final(ctx, A1, NULL);
  288. memcpy(out + ((i-1) * chunk), A1, olen % chunk);
  289. break;
  290. }
  291. HMAC_Init_ex(ctx, NULL, 0, NULL, NULL);
  292. i++;
  293. } while ((i-1) * chunk <= olen);
  294. HMAC_CTX_free(ctx);
  295. return 1;
  296. }
  297. /* seed1 through seed5 are virtually concatenated */
  298. static int do_PRF(const void *seed1, int seed1_len,
  299. const void *seed2, int seed2_len,
  300. const void *seed3, int seed3_len,
  301. unsigned char *out, int olen)
  302. {
  303. unsigned char out2[104];
  304. int i, len;
  305. if (olen > (int)sizeof(out2))
  306. return 0;
  307. len = sizeof(master_secret) / 2;
  308. if (!tls1_P_hash(EVP_md5(), master_secret, len,
  309. seed1, seed1_len, seed2, seed2_len, seed3,
  310. seed3_len, out, olen) ||
  311. !tls1_P_hash(EVP_sha1(), master_secret + len, len,
  312. seed1, seed1_len, seed2, seed2_len, seed3,
  313. seed3_len, out2, olen))
  314. return 0;
  315. for (i = 0; i < olen; i++)
  316. out[i] ^= out2[i];
  317. return 1;
  318. }
  319. static SSL_SESSION *client_session(void)
  320. {
  321. static unsigned char session_asn1[] = {
  322. 0x30, 0x5F, /* SEQUENCE, length 0x5F */
  323. 0x02, 0x01, 0x01, /* INTEGER, SSL_SESSION_ASN1_VERSION */
  324. 0x02, 0x02, 0x01, 0x00, /* INTEGER, DTLS1_BAD_VER */
  325. 0x04, 0x02, 0x00, 0x2F, /* OCTET_STRING, AES128-SHA */
  326. 0x04, 0x20, /* OCTET_STRING, session id */
  327. #define SS_SESSID_OFS 15 /* Session ID goes here */
  328. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  329. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  330. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  331. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  332. 0x04, 0x30, /* OCTET_STRING, master secret */
  333. #define SS_SECRET_OFS 49 /* Master secret goes here */
  334. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  336. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  339. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  340. };
  341. const unsigned char *p = session_asn1;
  342. /* Copy the randomly-generated fields into the above ASN1 */
  343. memcpy(session_asn1 + SS_SESSID_OFS, session_id, sizeof(session_id));
  344. memcpy(session_asn1 + SS_SECRET_OFS, master_secret, sizeof(master_secret));
  345. return d2i_SSL_SESSION(NULL, &p, sizeof(session_asn1));
  346. }
  347. /* Returns 1 for initial ClientHello, 2 for ClientHello with cookie */
  348. static int validate_client_hello(BIO *wbio)
  349. {
  350. PACKET pkt, pkt2;
  351. long len;
  352. unsigned char *data;
  353. int cookie_found = 0;
  354. unsigned int u;
  355. len = BIO_get_mem_data(wbio, (char **)&data);
  356. if (!PACKET_buf_init(&pkt, data, len))
  357. return 0;
  358. /* Check record header type */
  359. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE)
  360. return 0;
  361. /* Version */
  362. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  363. return 0;
  364. /* Skip the rest of the record header */
  365. if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3))
  366. return 0;
  367. /* Check it's a ClientHello */
  368. if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CLIENT_HELLO)
  369. return 0;
  370. /* Skip the rest of the handshake message header */
  371. if (!PACKET_forward(&pkt, DTLS1_HM_HEADER_LENGTH - 1))
  372. return 0;
  373. /* Check client version */
  374. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  375. return 0;
  376. /* Store random */
  377. if (!PACKET_copy_bytes(&pkt, client_random, SSL3_RANDOM_SIZE))
  378. return 0;
  379. /* Check session id length and content */
  380. if (!PACKET_get_length_prefixed_1(&pkt, &pkt2) ||
  381. !PACKET_equal(&pkt2, session_id, sizeof(session_id)))
  382. return 0;
  383. /* Check cookie */
  384. if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
  385. return 0;
  386. if (PACKET_remaining(&pkt2)) {
  387. if (!PACKET_equal(&pkt2, cookie, sizeof(cookie)))
  388. return 0;
  389. cookie_found = 1;
  390. }
  391. /* Skip ciphers */
  392. if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u))
  393. return 0;
  394. /* Skip compression */
  395. if (!PACKET_get_1(&pkt, &u) || !PACKET_forward(&pkt, u))
  396. return 0;
  397. /* Skip extensions */
  398. if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u))
  399. return 0;
  400. /* Now we are at the end */
  401. if (PACKET_remaining(&pkt))
  402. return 0;
  403. /* Update handshake MAC for second ClientHello (with cookie) */
  404. if (cookie_found && (!EVP_DigestUpdate(handshake_md5, data + MAC_OFFSET,
  405. len - MAC_OFFSET) ||
  406. !EVP_DigestUpdate(handshake_sha1, data + MAC_OFFSET,
  407. len - MAC_OFFSET)))
  408. printf("EVP_DigestUpdate() failed\n");
  409. (void)BIO_reset(wbio);
  410. return 1 + cookie_found;
  411. }
  412. static int send_hello_verify(BIO *rbio)
  413. {
  414. static unsigned char hello_verify[] = {
  415. 0x16, /* Handshake */
  416. 0x01, 0x00, /* DTLS1_BAD_VER */
  417. 0x00, 0x00, /* Epoch 0 */
  418. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Seq# 0 */
  419. 0x00, 0x23, /* Length */
  420. 0x03, /* Hello Verify */
  421. 0x00, 0x00, 0x17, /* Length */
  422. 0x00, 0x00, /* Seq# 0 */
  423. 0x00, 0x00, 0x00, /* Fragment offset */
  424. 0x00, 0x00, 0x17, /* Fragment length */
  425. 0x01, 0x00, /* DTLS1_BAD_VER */
  426. 0x14, /* Cookie length */
  427. #define HV_COOKIE_OFS 28 /* Cookie goes here */
  428. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  429. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  430. 0x00, 0x00, 0x00, 0x00,
  431. };
  432. memcpy(hello_verify + HV_COOKIE_OFS, cookie, sizeof(cookie));
  433. BIO_write(rbio, hello_verify, sizeof(hello_verify));
  434. return 1;
  435. }
  436. static int send_server_hello(BIO *rbio)
  437. {
  438. static unsigned char server_hello[] = {
  439. 0x16, /* Handshake */
  440. 0x01, 0x00, /* DTLS1_BAD_VER */
  441. 0x00, 0x00, /* Epoch 0 */
  442. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* Seq# 1 */
  443. 0x00, 0x52, /* Length */
  444. 0x02, /* Server Hello */
  445. 0x00, 0x00, 0x46, /* Length */
  446. 0x00, 0x01, /* Seq# */
  447. 0x00, 0x00, 0x00, /* Fragment offset */
  448. 0x00, 0x00, 0x46, /* Fragment length */
  449. 0x01, 0x00, /* DTLS1_BAD_VER */
  450. #define SH_RANDOM_OFS 27 /* Server random goes here */
  451. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  452. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  453. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  454. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  455. 0x20, /* Session ID length */
  456. #define SH_SESSID_OFS 60 /* Session ID goes here */
  457. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  458. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  459. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  461. 0x00, 0x2f, /* Cipher suite AES128-SHA */
  462. 0x00, /* Compression null */
  463. };
  464. static unsigned char change_cipher_spec[] = {
  465. 0x14, /* Change Cipher Spec */
  466. 0x01, 0x00, /* DTLS1_BAD_VER */
  467. 0x00, 0x00, /* Epoch 0 */
  468. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, /* Seq# 2 */
  469. 0x00, 0x03, /* Length */
  470. 0x01, 0x00, 0x02, /* Message */
  471. };
  472. memcpy(server_hello + SH_RANDOM_OFS, server_random, sizeof(server_random));
  473. memcpy(server_hello + SH_SESSID_OFS, session_id, sizeof(session_id));
  474. if (!EVP_DigestUpdate(handshake_md5, server_hello + MAC_OFFSET,
  475. sizeof(server_hello) - MAC_OFFSET) ||
  476. !EVP_DigestUpdate(handshake_sha1, server_hello + MAC_OFFSET,
  477. sizeof(server_hello) - MAC_OFFSET))
  478. printf("EVP_DigestUpdate() failed\n");
  479. BIO_write(rbio, server_hello, sizeof(server_hello));
  480. BIO_write(rbio, change_cipher_spec, sizeof(change_cipher_spec));
  481. return 1;
  482. }
  483. /* Create header, HMAC, pad, encrypt and send a record */
  484. static int send_record(BIO *rbio, unsigned char type, unsigned long seqnr,
  485. const void *msg, size_t len)
  486. {
  487. /* Note that the order of the record header fields on the wire,
  488. * and in the HMAC, is different. So we just keep them in separate
  489. * variables and handle them individually. */
  490. static unsigned char epoch[2] = { 0x00, 0x01 };
  491. static unsigned char seq[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  492. static unsigned char ver[2] = { 0x01, 0x00 }; /* DTLS1_BAD_VER */
  493. unsigned char lenbytes[2];
  494. HMAC_CTX *ctx = HMAC_CTX_new();
  495. EVP_CIPHER_CTX *enc_ctx = EVP_CIPHER_CTX_new();
  496. unsigned char iv[16];
  497. unsigned char pad;
  498. unsigned char *enc;
  499. #ifdef SIXTY_FOUR_BIT_LONG
  500. seq[0] = (seqnr >> 40) & 0xff;
  501. seq[1] = (seqnr >> 32) & 0xff;
  502. #endif
  503. seq[2] = (seqnr >> 24) & 0xff;
  504. seq[3] = (seqnr >> 16) & 0xff;
  505. seq[4] = (seqnr >> 8) & 0xff;
  506. seq[5] = seqnr & 0xff;
  507. pad = 15 - ((len + SHA_DIGEST_LENGTH) % 16);
  508. enc = OPENSSL_malloc(len + SHA_DIGEST_LENGTH + 1 + pad);
  509. if (enc == NULL)
  510. return 0;
  511. /* Copy record to encryption buffer */
  512. memcpy(enc, msg, len);
  513. /* Append HMAC to data */
  514. HMAC_Init_ex(ctx, mac_key, 20, EVP_sha1(), NULL);
  515. HMAC_Update(ctx, epoch, 2);
  516. HMAC_Update(ctx, seq, 6);
  517. HMAC_Update(ctx, &type, 1);
  518. HMAC_Update(ctx, ver, 2); /* Version */
  519. lenbytes[0] = len >> 8;
  520. lenbytes[1] = len & 0xff;
  521. HMAC_Update(ctx, lenbytes, 2); /* Length */
  522. HMAC_Update(ctx, enc, len); /* Finally the data itself */
  523. HMAC_Final(ctx, enc + len, NULL);
  524. HMAC_CTX_free(ctx);
  525. /* Append padding bytes */
  526. len += SHA_DIGEST_LENGTH;
  527. do {
  528. enc[len++] = pad;
  529. } while (len % 16);
  530. /* Generate IV, and encrypt */
  531. RAND_bytes(iv, sizeof(iv));
  532. // EVP_CIPHER_CTX_init(enc_ctx);
  533. EVP_CipherInit_ex(enc_ctx, EVP_aes_128_cbc(), NULL, enc_key, iv, 1);
  534. EVP_Cipher(enc_ctx, enc, enc, len);
  535. EVP_CIPHER_CTX_free(enc_ctx);
  536. /* Finally write header (from fragmented variables), IV and encrypted record */
  537. BIO_write(rbio, &type, 1);
  538. BIO_write(rbio, ver, 2);
  539. BIO_write(rbio, epoch, 2);
  540. BIO_write(rbio, seq, 6);
  541. lenbytes[0] = (len + sizeof(iv)) >> 8;
  542. lenbytes[1] = (len + sizeof(iv)) & 0xff;
  543. BIO_write(rbio, lenbytes, 2);
  544. BIO_write(rbio, iv, sizeof(iv));
  545. BIO_write(rbio, enc, len);
  546. OPENSSL_free(enc);
  547. return 1;
  548. }
  549. static int send_finished(SSL *s, BIO *rbio)
  550. {
  551. static unsigned char finished_msg[DTLS1_HM_HEADER_LENGTH +
  552. TLS1_FINISH_MAC_LENGTH] = {
  553. 0x14, /* Finished */
  554. 0x00, 0x00, 0x0c, /* Length */
  555. 0x00, 0x03, /* Seq# 3 */
  556. 0x00, 0x00, 0x00, /* Fragment offset */
  557. 0x00, 0x00, 0x0c, /* Fragment length */
  558. /* Finished MAC (12 bytes) */
  559. };
  560. unsigned char handshake_hash[EVP_MAX_MD_SIZE * 2];
  561. /* Derive key material */
  562. do_PRF(TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  563. server_random, SSL3_RANDOM_SIZE,
  564. client_random, SSL3_RANDOM_SIZE,
  565. key_block, sizeof(key_block));
  566. /* Generate Finished MAC */
  567. if (!EVP_DigestFinal_ex(handshake_md5, handshake_hash, NULL) ||
  568. !EVP_DigestFinal_ex(handshake_sha1, handshake_hash + EVP_MD_CTX_size(handshake_md5), NULL))
  569. printf("EVP_DigestFinal_ex() failed\n");
  570. do_PRF(TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  571. handshake_hash, EVP_MD_CTX_size(handshake_md5) + EVP_MD_CTX_size(handshake_sha1),
  572. NULL, 0,
  573. finished_msg + DTLS1_HM_HEADER_LENGTH, TLS1_FINISH_MAC_LENGTH);
  574. return send_record(rbio, SSL3_RT_HANDSHAKE, 0,
  575. finished_msg, sizeof(finished_msg));
  576. }
  577. static int validate_ccs(BIO *wbio)
  578. {
  579. PACKET pkt;
  580. long len;
  581. unsigned char *data;
  582. unsigned int u;
  583. len = BIO_get_mem_data(wbio, (char **)&data);
  584. if (!PACKET_buf_init(&pkt, data, len))
  585. return 0;
  586. /* Check record header type */
  587. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_CHANGE_CIPHER_SPEC)
  588. return 0;
  589. /* Version */
  590. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  591. return 0;
  592. /* Skip the rest of the record header */
  593. if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3))
  594. return 0;
  595. /* Check ChangeCipherSpec message */
  596. if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CCS)
  597. return 0;
  598. /* A DTLS1_BAD_VER ChangeCipherSpec also contains the
  599. * handshake sequence number (which is 2 here) */
  600. if (!PACKET_get_net_2(&pkt, &u) || u != 0x0002)
  601. return 0;
  602. /* Now check the Finished packet */
  603. if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE)
  604. return 0;
  605. if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER)
  606. return 0;
  607. /* Check epoch is now 1 */
  608. if (!PACKET_get_net_2(&pkt, &u) || u != 0x0001)
  609. return 0;
  610. /* That'll do for now. If OpenSSL accepted *our* Finished packet
  611. * then it's evidently remembered that DTLS1_BAD_VER doesn't
  612. * include the handshake header in the MAC. There's not a lot of
  613. * point in implementing decryption here, just to check that it
  614. * continues to get it right for one more packet. */
  615. return 1;
  616. }
  617. #define NODROP(x) { x##UL, 0 }
  618. #define DROP(x) { x##UL, 1 }
  619. static struct {
  620. unsigned long seq;
  621. int drop;
  622. } tests[] = {
  623. NODROP(1), NODROP(3), NODROP(2),
  624. NODROP(0x1234), NODROP(0x1230), NODROP(0x1235),
  625. NODROP(0xffff), NODROP(0x10001), NODROP(0xfffe), NODROP(0x10000),
  626. DROP(0x10001), DROP(0xff), NODROP(0x100000), NODROP(0x800000), NODROP(0x7fffe1),
  627. NODROP(0xffffff), NODROP(0x1000000), NODROP(0xfffffe), DROP(0xffffff), NODROP(0x1000010),
  628. NODROP(0xfffffd), NODROP(0x1000011), DROP(0x12), NODROP(0x1000012),
  629. NODROP(0x1ffffff), NODROP(0x2000000), DROP(0x1ff00fe), NODROP(0x2000001),
  630. NODROP(0x20fffff), NODROP(0x2105500), DROP(0x20ffffe), NODROP(0x21054ff),
  631. NODROP(0x211ffff), DROP(0x2110000), NODROP(0x2120000)
  632. /* The last test should be NODROP, because a DROP wouldn't get tested. */
  633. };
  634. int main(int argc, char *argv[])
  635. {
  636. SSL_SESSION *sess;
  637. SSL_CTX *ctx;
  638. SSL *con;
  639. BIO *rbio;
  640. BIO *wbio;
  641. int testresult = 0;
  642. int ret;
  643. int i;
  644. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  645. SSL_library_init();
  646. SSL_load_error_strings();
  647. #endif
  648. RAND_bytes(session_id, sizeof(session_id));
  649. RAND_bytes(master_secret, sizeof(master_secret));
  650. RAND_bytes(cookie, sizeof(cookie));
  651. RAND_bytes(server_random + 4, sizeof(server_random) - 4);
  652. time((void *)server_random);
  653. sess = client_session();
  654. if (sess == NULL) {
  655. printf("Failed to generate SSL_SESSION\n");
  656. goto end;
  657. }
  658. handshake_md5 = EVP_MD_CTX_new();
  659. handshake_sha1 = EVP_MD_CTX_new();
  660. if (!EVP_DigestInit_ex(handshake_md5, EVP_md5(), NULL) ||
  661. !EVP_DigestInit_ex(handshake_sha1, EVP_sha1(), NULL)) {
  662. printf("Failed to initialise handshake_md\n");
  663. goto end;
  664. }
  665. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  666. ctx = SSL_CTX_new(DTLSv1_client_method());
  667. if (ctx == NULL) {
  668. printf("Failed to allocate SSL_CTX\n");
  669. goto end_md;
  670. }
  671. SSL_CTX_set_options(ctx, SSL_OP_CISCO_ANYCONNECT);
  672. #else
  673. ctx = SSL_CTX_new(DTLS_client_method());
  674. if (ctx == NULL ||
  675. !SSL_CTX_set_min_proto_version(ctx, DTLS1_BAD_VER) ||
  676. !SSL_CTX_set_max_proto_version(ctx, DTLS1_BAD_VER) ||
  677. !SSL_CTX_set_options(ctx, SSL_OP_LEGACY_SERVER_CONNECT)) {
  678. printf("Failed to allocate SSL_CTX\n");
  679. goto end_md;
  680. }
  681. #endif
  682. if (!SSL_CTX_set_cipher_list(ctx, "AES128-SHA")) {
  683. printf("SSL_CTX_set_cipher_list() failed\n");
  684. goto end_ctx;
  685. }
  686. con = SSL_new(ctx);
  687. if (!SSL_set_session(con, sess)) {
  688. printf("SSL_set_session() failed\n");
  689. goto end_con;
  690. }
  691. SSL_SESSION_free(sess);
  692. rbio = BIO_new(BIO_s_mem());
  693. wbio = BIO_new(BIO_s_mem());
  694. BIO_set_nbio(rbio, 1);
  695. BIO_set_nbio(wbio, 1);
  696. SSL_set_bio(con, rbio, wbio);
  697. SSL_set_connect_state(con);
  698. /* Send initial ClientHello */
  699. ret = SSL_do_handshake(con);
  700. if (ret > 0 || SSL_get_error(con, ret) != SSL_ERROR_WANT_READ) {
  701. printf("Unexpected handshake result at initial call!\n");
  702. goto end_con;
  703. }
  704. if (validate_client_hello(wbio) != 1) {
  705. printf("Initial ClientHello failed validation\n");
  706. goto end_con;
  707. }
  708. if (send_hello_verify(rbio) != 1) {
  709. printf("Failed to send HelloVerify\n");
  710. goto end_con;
  711. }
  712. ret = SSL_do_handshake(con);
  713. if (ret > 0 || SSL_get_error(con, ret) != SSL_ERROR_WANT_READ) {
  714. printf("Unexpected handshake result after HelloVerify!\n");
  715. goto end_con;
  716. }
  717. if (validate_client_hello(wbio) != 2) {
  718. printf("Second ClientHello failed validation\n");
  719. goto end_con;
  720. }
  721. if (send_server_hello(rbio) != 1) {
  722. printf("Failed to send ServerHello\n");
  723. goto end_con;
  724. }
  725. ret = SSL_do_handshake(con);
  726. if (ret > 0 || SSL_get_error(con, ret) != SSL_ERROR_WANT_READ) {
  727. printf("Unexpected handshake result after ServerHello!\n");
  728. goto end_con;
  729. }
  730. if (send_finished(con, rbio) != 1) {
  731. printf("Failed to send Finished\n");
  732. goto end_con;
  733. }
  734. ret = SSL_do_handshake(con);
  735. if (ret < 1) {
  736. printf("Handshake not successful after Finished!\n");
  737. goto end_con;
  738. }
  739. if (validate_ccs(wbio) != 1) {
  740. printf("Failed to validate client CCS/Finished\n");
  741. goto end_con;
  742. }
  743. /* While we're here and crafting packets by hand, we might as well do a
  744. bit of a stress test on the DTLS record replay handling. Not Cisco-DTLS
  745. specific but useful anyway for the general case. It's been broken
  746. before, and in fact was broken even for a basic 0, 2, 1 test case
  747. when this test was first added.... */
  748. for (i = 0; i < (int)ARRAY_SIZE(tests); i++) {
  749. unsigned long recv_buf[2];
  750. if (send_record(rbio, SSL3_RT_APPLICATION_DATA, tests[i].seq,
  751. &tests[i].seq, sizeof(unsigned long)) != 1) {
  752. printf("Failed to send data seq #0x%lx (%d)\n",
  753. tests[i].seq, i);
  754. goto end_con;
  755. }
  756. if (tests[i].drop)
  757. continue;
  758. ret = SSL_read(con, recv_buf, 2 * sizeof(unsigned long));
  759. if (ret != sizeof(unsigned long)) {
  760. printf("SSL_read failed or wrong size on seq#0x%lx (%d)\n",
  761. tests[i].seq, i);
  762. goto end_con;
  763. }
  764. if (recv_buf[0] != tests[i].seq) {
  765. printf("Wrong data packet received (0x%lx not 0x%lx) at packet %d\n",
  766. recv_buf[0], tests[i].seq, i);
  767. goto end_con;
  768. }
  769. }
  770. if (tests[i-1].drop) {
  771. printf("Error: last test cannot be DROP()\n");
  772. goto end_con;
  773. }
  774. testresult=1;
  775. end_con:
  776. SSL_free(con);
  777. end_ctx:
  778. SSL_CTX_free(ctx);
  779. end_md:
  780. EVP_MD_CTX_free(handshake_md5);
  781. EVP_MD_CTX_free(handshake_sha1);
  782. end:
  783. ERR_print_errors_fp(stderr);
  784. if (!testresult) {
  785. printf("Cisco BadDTLS test: FAILED\n");
  786. }
  787. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  788. ERR_free_strings();
  789. EVP_cleanup();
  790. #endif
  791. return testresult?0:1;
  792. }