tls13encryptiontest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2016-2018 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. #include <openssl/ssl.h>
  10. #include <openssl/evp.h>
  11. #ifdef __VMS
  12. # pragma names save
  13. # pragma names as_is,shortened
  14. #endif
  15. #include "../ssl/ssl_locl.h"
  16. #include "../ssl/record/record_locl.h"
  17. #ifdef __VMS
  18. # pragma names restore
  19. #endif
  20. #include "internal/nelem.h"
  21. #include "testutil.h"
  22. /*
  23. * Based on the test vectors provided in:
  24. * https://www.ietf.org/id/draft-thomson-tls-tls13-vectors-01.txt
  25. */
  26. typedef struct {
  27. /*
  28. * We split these into 3 chunks in order to work around the 509 character
  29. * limit that the standard specifies for string literals
  30. */
  31. const char *plaintext[3];
  32. const char *ciphertext[3];
  33. const char *key;
  34. const char *iv;
  35. const char *seq;
  36. } RECORD_DATA;
  37. static RECORD_DATA refdata[] = {
  38. {
  39. /*
  40. * Server: EncryptedExtensions, Certificate, CertificateVerify and
  41. * Finished
  42. */
  43. {
  44. "0800001e001c000a00140012001d001700180019010001010102010301040000"
  45. "00000b0001b9000001b50001b0308201ac30820115a003020102020102300d06"
  46. "092a864886f70d01010b0500300e310c300a06035504031303727361301e170d"
  47. "3136303733303031323335395a170d3236303733303031323335395a300e310c"
  48. "300a0603550403130372736130819f300d06092a864886f70d01010105000381"
  49. "8d0030818902818100b4bb498f8279303d980836399b36c6988c0c68de55e1bd"
  50. "b826d3901a2461eafd2de49a91d015abbc9a95137ace6c1af19eaa6af98c7ced",
  51. "43120998e187a80ee0ccb0524b1b018c3e0b63264d449a6d38e22a5fda430846"
  52. "748030530ef0461c8ca9d9efbfae8ea6d1d03e2bd193eff0ab9a8002c47428a6"
  53. "d35a8d88d79f7f1e3f0203010001a31a301830090603551d1304023000300b06"
  54. "03551d0f0404030205a0300d06092a864886f70d01010b05000381810085aad2"
  55. "a0e5b9276b908c65f73a7267170618a54c5f8a7b337d2df7a594365417f2eae8"
  56. "f8a58c8f8172f9319cf36b7fd6c55b80f21a03015156726096fd335e5e67f2db"
  57. "f102702e608ccae6bec1fc63a42a99be5c3eb7107c3c54e9b9eb2bd5203b1c3b",
  58. "84e0a8b2f759409ba3eac9d91d402dcc0cc8f8961229ac9187b42b4de100000f"
  59. "00008408040080134e22eac57321ab47db6b38b2992cec2dd79bd065a034a9af"
  60. "6b9e3d03475e4309e6523ccdf055453fb480804a3a7e996229eb28e734f6702b"
  61. "ea2b32149899ac043a4b44468197868da77147ce9f73c0543c4e3fc33e306cac"
  62. "8506faa80a959c5f1edccbee76eda1ad7a4fa440de35dcb87e82ec94e8725355"
  63. "ce7507713a609e140000207304bb73321f01b71dd94622fae98daf634490d220"
  64. "e4c8f3ffa2559911a56e5116"
  65. },
  66. {
  67. "40ae92071a3a548b26af31e116dfc0ba4549210b17e70da16cfbda9ccdad844d"
  68. "94264a9ae65b786b3eaf0de20aa89c6babb448b6f32d07f233584296eefe1931"
  69. "6bd979659472ee8567cb01d70b0366cddb3c60eb9e1d789a3691dc254c14de73"
  70. "f4f20100504544ce184d44547e124b1f18303b4859f8f2e2b04423d23a866b43"
  71. "866374d54af41649d25f4a3ec2cecd5d4e6de1b24953440b46fbb74c1dbec6fb"
  72. "b1f16bc21d4aa0e1e936a49c07127e19719bc652a2f0b7f8df4a150b2b3c9e9e"
  73. "353d6ed101970ddc611abad0632c6793f9379c9d06846c311fcbd6f85edd569b",
  74. "8782c4c5f62294c4611ae60f83230a53aa95e3bcbed204f19a7a1db83c0fbfec"
  75. "1edd2c17498fa7b5aa2321248a92592d891e4947df6bcef52f4481797d032ad3"
  76. "32046a384abece6454b3e356d7249bfa5696793c7f7d3048dc87fa7409a46918"
  77. "87caaf0982c402b902d699f62dc4d5e153f13e8589e4a6206c7f74eb26ddefbb"
  78. "92309fb753decfea972dec7de02eda9c6d26acd7be53a8aa20f1a93f082ae6eb"
  79. "927a6a1b7bd9153551aedfaf94f61dd4cb9355ad7ab09f615d9f92c21712c732"
  80. "c0e7e117797f38cbdc184e3a65e15a89f46cb3624f5fdb8dbbd275f2c8492f8d",
  81. "95bdbd8d1dc1b9f21107bd433acbbac247239c073a2f24a4a9f8074f325f277d"
  82. "579b6bff0269ff19aed3809a9ddd21dd29c1363c9dc44812dd41d2111f9c2e83"
  83. "42046c14133b853262676f15e94de18660e04ae5c0c661ea43559af5842e161c"
  84. "83dd29f64508b2ec3e635a2134fc0e1a39d3ecb51dcddfcf8382c88ffe2a7378"
  85. "42ad1de7fe505b6c4d1673870f6fc2a0f2f7972acaee368a1599d64ba18798f1"
  86. "0333f9779bd5b05f9b084d03dab2f3d80c2eb74ec70c9866ea31c18b491cd597"
  87. "aae3e941205fcc38a3a10ce8f2e230d97e3406b77ee53d84d89ca548"
  88. },
  89. "d2dd45f87ad87801a85ac38187f9023b",
  90. "f0a14f808692cef87a3daf70",
  91. "0000000000000000"
  92. },
  93. {
  94. /* Client: Finished */
  95. {
  96. "1400002078367856d3c8cc4e0a95eb98906ca7a48bd3cc7029f48bd4ae0dc91a"
  97. "b903ca8916","",""
  98. },
  99. {
  100. "fa15e92daa21cd05d8f9c3152a61748d9aaf049da559718e583f95aacecad657"
  101. "b52a6562da66864fd14969acc30dc04a78c38283c5","",""
  102. },
  103. "40e1201d75d419627f04c88530a15c9d",
  104. "a0f073f3b35e18f96969696b",
  105. "0000000000000000"
  106. },
  107. {
  108. /* Server: NewSessionTicket */
  109. {
  110. "040000a60002a3004abe594b00924e535321cadc96238da09caf9b02fecafdd6"
  111. "5e3e418f03e43772cf512ed8066100503b1c08abbbf298a9d138ce821dd12fe1"
  112. "710e2137cd12e6a85cd3fd7f73706e7f5dddefb87c1ef83824638464099c9d13"
  113. "63e3c64ed2075c16b8ccd8e524a6bbd7a6a6e34ea1579782b15bbe7dfed5c0c0"
  114. "d980fb330f9d8ab252ffe7be1277d418b6828ead4dae3b30d448442417ef76af"
  115. "0008002e00040002000016","",""
  116. },
  117. {
  118. "45a6626fa13b66ce2c5b3ef807e299a118296f26a2dd9ec7487a0673e2460d4c"
  119. "79f40087dcd014c59c51379c90d26b4e4f9bb2b78f5b6761594f013ff3e4c78d"
  120. "836905229eac811c4ef8b2faa89867e9ffc586f7f03c216591aa5e620eac3c62"
  121. "dfe60f846036bd7ecc4464b584af184e9644e94ee1d7834dba408a51cbe42480"
  122. "04796ed9c558e0f5f96115a6f6ba487e17d16a2e20a3d3a650a9a070fb53d9da"
  123. "82864b5621d77650bd0c7972f592aa8546de09b8e46921fab4d876","",""
  124. },
  125. "3381f6b3f94500f16226de440193e858",
  126. "4f1d73cc1d465eb30021c41f",
  127. "0000000000000000"
  128. },
  129. {
  130. /* Client: Application Data */
  131. {
  132. "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  133. "202122232425262728292a2b2c2d2e2f303117","",""
  134. },
  135. {
  136. "e306178ad97f74bb64f35eaf3c39846b83aef8472cbc9046749b81a949dfb12c"
  137. "fbc65cbabd20ade92c1f944605892ceeb12fde5781d40e2ca080fc921b750b8c"
  138. "21bd8d","",""
  139. },
  140. "eb23a804904b80ba4fe8399e09b1ce42",
  141. "efa8c50c06b9c9b8c483e174",
  142. "0000000000000000"
  143. },
  144. {
  145. /* Server: Application Data */
  146. {
  147. "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  148. "202122232425262728292a2b2c2d2e2f303117","",""
  149. },
  150. {
  151. "467d99a807dbf778e6ffd8be52456c70665f890811ef2f3c495d5bbe983feeda"
  152. "b0c251dde596bc7e2b135909ec9f9166fb01526c70c7e42b6df52d63b0000222"
  153. "cb2047","",""
  154. },
  155. "3381f6b3f94500f16226de440193e858",
  156. "4f1d73cc1d465eb30021c41f",
  157. "0000000000000001"
  158. },
  159. {
  160. /* Client: CloseNotify */
  161. {
  162. "010015","",""
  163. },
  164. {
  165. "6bdf609107610cff95d70387a67b89e2494f0d","",""
  166. },
  167. "eb23a804904b80ba4fe8399e09b1ce42",
  168. "efa8c50c06b9c9b8c483e174",
  169. "0000000000000001"
  170. },
  171. {
  172. /* Server: CloseNotify */
  173. {
  174. "010015","",""
  175. },
  176. {
  177. "621b7c60d32528b149b36a78c8891a8d2f65ad","",""
  178. },
  179. "3381f6b3f94500f16226de440193e858",
  180. "4f1d73cc1d465eb30021c41f",
  181. "0000000000000002"
  182. }
  183. };
  184. /*
  185. * Same thing as OPENSSL_hexstr2buf() but enables us to pass the string in
  186. * 3 chunks
  187. */
  188. static unsigned char *multihexstr2buf(const char *str[3], size_t *len)
  189. {
  190. size_t outer, inner, curr = 0;
  191. unsigned char *outbuf;
  192. size_t totlen = 0;
  193. /* Check lengths of all input strings are even */
  194. for (outer = 0; outer < 3; outer++) {
  195. totlen += strlen(str[outer]);
  196. if ((totlen & 1) != 0)
  197. return NULL;
  198. }
  199. totlen /= 2;
  200. outbuf = OPENSSL_malloc(totlen);
  201. if (outbuf == NULL)
  202. return NULL;
  203. for (outer = 0; outer < 3; outer++) {
  204. for (inner = 0; str[outer][inner] != 0; inner += 2) {
  205. int hi, lo;
  206. hi = OPENSSL_hexchar2int(str[outer][inner]);
  207. lo = OPENSSL_hexchar2int(str[outer][inner + 1]);
  208. if (hi < 0 || lo < 0) {
  209. OPENSSL_free(outbuf);
  210. return NULL;
  211. }
  212. outbuf[curr++] = (hi << 4) | lo;
  213. }
  214. }
  215. *len = totlen;
  216. return outbuf;
  217. }
  218. static int load_record(SSL3_RECORD *rec, RECORD_DATA *recd, unsigned char **key,
  219. unsigned char *iv, size_t ivlen, unsigned char *seq)
  220. {
  221. unsigned char *pt = NULL, *sq = NULL, *ivtmp = NULL;
  222. size_t ptlen;
  223. *key = OPENSSL_hexstr2buf(recd->key, NULL);
  224. ivtmp = OPENSSL_hexstr2buf(recd->iv, NULL);
  225. sq = OPENSSL_hexstr2buf(recd->seq, NULL);
  226. pt = multihexstr2buf(recd->plaintext, &ptlen);
  227. if (*key == NULL || ivtmp == NULL || sq == NULL || pt == NULL)
  228. goto err;
  229. rec->data = rec->input = OPENSSL_malloc(ptlen + EVP_GCM_TLS_TAG_LEN);
  230. if (rec->data == NULL)
  231. goto err;
  232. rec->length = ptlen;
  233. memcpy(rec->data, pt, ptlen);
  234. OPENSSL_free(pt);
  235. memcpy(seq, sq, SEQ_NUM_SIZE);
  236. OPENSSL_free(sq);
  237. memcpy(iv, ivtmp, ivlen);
  238. OPENSSL_free(ivtmp);
  239. return 1;
  240. err:
  241. OPENSSL_free(*key);
  242. *key = NULL;
  243. OPENSSL_free(ivtmp);
  244. OPENSSL_free(sq);
  245. OPENSSL_free(pt);
  246. return 0;
  247. }
  248. static int test_record(SSL3_RECORD *rec, RECORD_DATA *recd, int enc)
  249. {
  250. int ret = 0;
  251. unsigned char *refd;
  252. size_t refdatalen;
  253. if (enc)
  254. refd = multihexstr2buf(recd->ciphertext, &refdatalen);
  255. else
  256. refd = multihexstr2buf(recd->plaintext, &refdatalen);
  257. if (!TEST_ptr(refd)) {
  258. TEST_info("Failed to get reference data");
  259. goto err;
  260. }
  261. if (!TEST_mem_eq(rec->data, rec->length, refd, refdatalen))
  262. goto err;
  263. ret = 1;
  264. err:
  265. OPENSSL_free(refd);
  266. return ret;
  267. }
  268. #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
  269. static int test_tls13_encryption(void)
  270. {
  271. SSL_CTX *ctx = NULL;
  272. SSL *s = NULL;
  273. SSL3_RECORD rec;
  274. unsigned char *key = NULL, *iv = NULL, *seq = NULL;
  275. const EVP_CIPHER *ciph = EVP_aes_128_gcm();
  276. int ret = 0;
  277. size_t ivlen, ctr;
  278. /*
  279. * Encrypted TLSv1.3 records always have an outer content type of
  280. * application data, and a record version of TLSv1.2.
  281. */
  282. rec.data = NULL;
  283. rec.type = SSL3_RT_APPLICATION_DATA;
  284. rec.rec_version = TLS1_2_VERSION;
  285. ctx = SSL_CTX_new(TLS_method());
  286. if (!TEST_ptr(ctx)) {
  287. TEST_info("Failed creating SSL_CTX");
  288. goto err;
  289. }
  290. s = SSL_new(ctx);
  291. if (!TEST_ptr(s)) {
  292. TEST_info("Failed creating SSL");
  293. goto err;
  294. }
  295. s->enc_read_ctx = EVP_CIPHER_CTX_new();
  296. if (!TEST_ptr(s->enc_read_ctx))
  297. goto err;
  298. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  299. if (!TEST_ptr(s->enc_write_ctx))
  300. goto err;
  301. s->s3->tmp.new_cipher = SSL_CIPHER_find(s, TLS13_AES_128_GCM_SHA256_BYTES);
  302. if (!TEST_ptr(s->s3->tmp.new_cipher)) {
  303. TEST_info("Failed to find cipher");
  304. goto err;
  305. }
  306. for (ctr = 0; ctr < OSSL_NELEM(refdata); ctr++) {
  307. /* Load the record */
  308. ivlen = EVP_CIPHER_iv_length(ciph);
  309. if (!load_record(&rec, &refdata[ctr], &key, s->read_iv, ivlen,
  310. RECORD_LAYER_get_read_sequence(&s->rlayer))) {
  311. TEST_error("Failed loading key into EVP_CIPHER_CTX");
  312. goto err;
  313. }
  314. /* Set up the read/write sequences */
  315. memcpy(RECORD_LAYER_get_write_sequence(&s->rlayer),
  316. RECORD_LAYER_get_read_sequence(&s->rlayer), SEQ_NUM_SIZE);
  317. memcpy(s->write_iv, s->read_iv, ivlen);
  318. /* Load the key into the EVP_CIPHER_CTXs */
  319. if (EVP_CipherInit_ex(s->enc_write_ctx, ciph, NULL, key, NULL, 1) <= 0
  320. || EVP_CipherInit_ex(s->enc_read_ctx, ciph, NULL, key, NULL, 0)
  321. <= 0) {
  322. TEST_error("Failed loading key into EVP_CIPHER_CTX\n");
  323. goto err;
  324. }
  325. /* Encrypt it */
  326. if (!TEST_size_t_eq(tls13_enc(s, &rec, 1, 1), 1)) {
  327. TEST_info("Failed to encrypt record %zu", ctr);
  328. goto err;
  329. }
  330. if (!TEST_true(test_record(&rec, &refdata[ctr], 1))) {
  331. TEST_info("Record %zu encryption test failed", ctr);
  332. goto err;
  333. }
  334. /* Decrypt it */
  335. if (!TEST_int_eq(tls13_enc(s, &rec, 1, 0), 1)) {
  336. TEST_info("Failed to decrypt record %zu", ctr);
  337. goto err;
  338. }
  339. if (!TEST_true(test_record(&rec, &refdata[ctr], 0))) {
  340. TEST_info("Record %zu decryption test failed", ctr);
  341. goto err;
  342. }
  343. OPENSSL_free(rec.data);
  344. OPENSSL_free(key);
  345. OPENSSL_free(iv);
  346. OPENSSL_free(seq);
  347. rec.data = NULL;
  348. key = NULL;
  349. iv = NULL;
  350. seq = NULL;
  351. }
  352. TEST_note("PASS: %zu records tested", ctr);
  353. ret = 1;
  354. err:
  355. OPENSSL_free(rec.data);
  356. OPENSSL_free(key);
  357. OPENSSL_free(iv);
  358. OPENSSL_free(seq);
  359. SSL_free(s);
  360. SSL_CTX_free(ctx);
  361. return ret;
  362. }
  363. int setup_tests(void)
  364. {
  365. ADD_TEST(test_tls13_encryption);
  366. return 1;
  367. }