tls13encryptiontest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #include <openssl/ssl.h>
  10. #include <openssl/evp.h>
  11. #include "../ssl/ssl_local.h"
  12. #include "../ssl/record/record_local.h"
  13. #include "internal/nelem.h"
  14. #include "testutil.h"
  15. /*
  16. * Based on the test vectors provided in:
  17. * https://tools.ietf.org/html/draft-ietf-tls-tls13-vectors-06
  18. */
  19. typedef struct {
  20. /*
  21. * We split these into 3 chunks in order to work around the 509 character
  22. * limit that the standard specifies for string literals
  23. */
  24. const char *plaintext[3];
  25. const char *ciphertext[3];
  26. const char *key;
  27. const char *iv;
  28. const char *seq;
  29. } RECORD_DATA;
  30. /*
  31. * Note 1: The plaintext values given here have an additional "16" or "17" byte
  32. * added to the end when compared to the official vectors. The official vectors
  33. * do not include the inner content type, but we require it.
  34. *
  35. * Note 2: These are the vectors for the "Simple 1-RTT Handshake"
  36. */
  37. static RECORD_DATA refdata[] = {
  38. {
  39. /*
  40. * Server: EncryptedExtensions, Certificate, CertificateVerify and
  41. * Finished
  42. */
  43. {
  44. "080000240022000a00140012001d00170018001901000101010201030104001c"
  45. "00024001000000000b0001b9000001b50001b0308201ac30820115a003020102"
  46. "020102300d06092a864886f70d01010b0500300e310c300a0603550403130372"
  47. "7361301e170d3136303733303031323335395a170d3236303733303031323335"
  48. "395a300e310c300a0603550403130372736130819f300d06092a864886f70d01"
  49. "0101050003818d0030818902818100b4bb498f8279303d980836399b36c6988c"
  50. "0c68de55e1bdb826d3901a2461eafd2de49a91d015abbc9a95137ace6c1af19e",
  51. "aa6af98c7ced43120998e187a80ee0ccb0524b1b018c3e0b63264d449a6d38e2"
  52. "2a5fda430846748030530ef0461c8ca9d9efbfae8ea6d1d03e2bd193eff0ab9a"
  53. "8002c47428a6d35a8d88d79f7f1e3f0203010001a31a301830090603551d1304"
  54. "023000300b0603551d0f0404030205a0300d06092a864886f70d01010b050003"
  55. "81810085aad2a0e5b9276b908c65f73a7267170618a54c5f8a7b337d2df7a594"
  56. "365417f2eae8f8a58c8f8172f9319cf36b7fd6c55b80f21a03015156726096fd"
  57. "335e5e67f2dbf102702e608ccae6bec1fc63a42a99be5c3eb7107c3c54e9b9eb",
  58. "2bd5203b1c3b84e0a8b2f759409ba3eac9d91d402dcc0cc8f8961229ac9187b4"
  59. "2b4de100000f00008408040080754040d0ddab8cf0e2da2bc4995b868ad745c8"
  60. "e1564e33cde17880a42392cc624aeef6b67bb3f0ae71d9d54a2309731d87dc59"
  61. "f642d733be2eb27484ad8a8c8eb3516a7ac57f2625e2b5c0888a8541f4e734f7"
  62. "3d054761df1dd02f0e3e9a33cfa10b6e3eb4ebf7ac053b01fdabbddfc54133bc"
  63. "d24c8bbdceb223b2aa03452a2914000020ac86acbc9cd25a45b57ad5b64db15d"
  64. "4405cf8c80e314583ebf3283ef9a99310c16"
  65. },
  66. {
  67. "f10b26d8fcaf67b5b828f712122216a1cd14187465b77637cbcd78539128bb93"
  68. "246dcca1af56f1eaa271666077455bc54965d85f05f9bd36d6996171eb536aff"
  69. "613eeddc42bad5a2d2227c4606f1215f980e7afaf56bd3b85a51be130003101a"
  70. "758d077b1c891d8e7a22947e5a229851fd42a9dd422608f868272abf92b3d43f"
  71. "b46ac420259346067f66322fd708885680f4b4433c29116f2dfa529e09bba53c"
  72. "7cd920121724809eaddcc84307ef46fc51a0b33d99d39db337fcd761ce0f2b02"
  73. "dc73dedb6fddb77c4f8099bde93d5bee08bcf2131f29a2a37ff07949e8f8bcdd",
  74. "3e8310b8bf8b3444c85aaf0d2aeb2d4f36fd14d5cb51fcebff418b3827136ab9"
  75. "529e9a3d3f35e4c0ae749ea2dbc94982a1281d3e6daab719aa4460889321a008"
  76. "bf10fa06ac0c61cc122cc90d5e22c0030c986ae84a33a0c47df174bcfbd50bf7"
  77. "8ffdf24051ab423db63d5815db2f830040f30521131c98c66f16c362addce2fb"
  78. "a0602cf0a7dddf22e8def7516cdfee95b4056cc9ad38c95352335421b5b1ffba"
  79. "df75e5212fdad7a75f52a2801486a1eec3539580bee0e4b337cda6085ac9eccd"
  80. "1a0f1a46cebfbb5cdfa3251ac28c3bc826148c6d8c1eb6a06f77f6ff632c6a83",
  81. "e283e8f9df7c6dbabf1c6ea40629a85b43ab0c73d34f9d5072832a104eda3f75"
  82. "f5d83da6e14822a18e14099d749eafd823ca2ac7542086501eca206ce7887920"
  83. "008573757ce2f230a890782b99cc682377beee812756d04f9025135fb599d746"
  84. "fefe7316c922ac265ca0d29021375adb63c1509c3e242dfb92b8dee891f7368c"
  85. "4058399b8db9075f2dcc8216194e503b6652d87d2cb41f99adfdcc5be5ec7e1e"
  86. "6326ac22d70bd3ba652827532d669aff005173597f8039c3ea4922d3ec757670"
  87. "222f6ac29b93e90d7ad3f6dd96328e429cfcfd5cca22707fe2d86ad1dcb0be75"
  88. "6e8e"
  89. },
  90. "c66cb1aec519df44c91e10995511ac8b",
  91. "f7f6884c4981716c2d0d29a4",
  92. "0000000000000000"
  93. },
  94. {
  95. /* Client: Finished */
  96. {
  97. "14000020b9027a0204b972b52cdefa58950fa1580d68c9cb124dbe691a7178f2"
  98. "5c554b2316", "", ""
  99. },
  100. {
  101. "9539b4ae2f87fd8e616b295628ea953d9e3858db274970d19813ec136cae7d96"
  102. "e0417775fcabd3d8858fdc60240912d218f5afb21c", "", ""
  103. },
  104. "2679a43e1d76784034ea1797d5ad2649",
  105. "5482405290dd0d2f81c0d942",
  106. "0000000000000000"
  107. },
  108. {
  109. /* Server: NewSessionTicket */
  110. {
  111. "040000c90000001e2fd3992f02000000b2ff099f9676cdff8b0bf8825d000000"
  112. "007905a9d28efeef4a47c6f9b06a0cecdb0070d920b898997c75b79636943ed4"
  113. "2046a96142bd084a04acfa0c490f452d756dea02c0f927259f1f3231ac0d541a"
  114. "769129b740ce38090842b828c27fd729f59737ba98aa7b42e043c5da28f8dca8"
  115. "590b2df410d5134fd6c4cacad8b30370602afa35d265bf4d127976bb36dbda6a"
  116. "626f0270e20eebc73d6fcae2b1a0da122ee9042f76be56ebf41aa469c3d2c9da"
  117. "9197d80008002a00040000040016", "", ""
  118. },
  119. {
  120. "3680c2b2109d25caa26c3b06eea9fdc5cb31613ba702176596da2e886bf6af93"
  121. "507bd68161ad9cb4780653842e1041ecbf0088a65ac4ef438419dd1d95ddd9bd"
  122. "2ad4484e7e167d0e6c008448ae58a0418713b6fc6c51e4bb23a537fb75a74f73"
  123. "de31fe6aa0bc522515f8b25f8955428b5de5ac06762cec22b0aa78c94385ef8e"
  124. "70fa24945b7c1f268510871689bbbbfaf2e7f4a19277024f95f1143ab12a31ec"
  125. "63adb128cb390711fd6d06a498df3e98615d8eb102e23353b480efcca5e8e026"
  126. "7a6d0fe2441f14c8c9664aefb2cfff6ae9e0442728b6a0940c1e824fda06",
  127. "", ""
  128. },
  129. "a688ebb5ac826d6f42d45c0cc44b9b7d",
  130. "c1cad4425a438b5de714830a",
  131. "0000000000000000"
  132. },
  133. {
  134. /* Client: Application Data */
  135. {
  136. "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  137. "202122232425262728292a2b2c2d2e2f303117", "", ""
  138. },
  139. {
  140. "8c3497da00ae023e53c01b4324b665404c1b49e78fe2bf4d17f6348ae8340551"
  141. "e363a0cd05f2179c4fef5ad689b5cae0bae94adc63632e571fb79aa91544c639"
  142. "4d28a1", "", ""
  143. },
  144. "88b96ad686c84be55ace18a59cce5c87",
  145. "b99dc58cd5ff5ab082fdad19",
  146. "0000000000000000"
  147. },
  148. {
  149. /* Server: Application Data */
  150. {
  151. "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  152. "202122232425262728292a2b2c2d2e2f303117", "", ""
  153. },
  154. {
  155. "f65f49fd2df6cd2347c3d30166e3cfddb6308a5906c076112c6a37ff1dbd406b"
  156. "5813c0abd734883017a6b2833186b13c14da5d75f33d8760789994e27d82043a"
  157. "b88d65", "", ""
  158. },
  159. "a688ebb5ac826d6f42d45c0cc44b9b7d",
  160. "c1cad4425a438b5de714830a",
  161. "0000000000000001"
  162. },
  163. {
  164. /* Client: CloseNotify */
  165. {
  166. "010015", "", ""
  167. },
  168. {
  169. "2c2148163d7938a35f6acf2a6606f8cbd1d9f2", "", ""
  170. },
  171. "88b96ad686c84be55ace18a59cce5c87",
  172. "b99dc58cd5ff5ab082fdad19",
  173. "0000000000000001"
  174. },
  175. {
  176. /* Server: CloseNotify */
  177. {
  178. "010015", "", ""
  179. },
  180. {
  181. "f8141ebdb5eda511e0bce639a56ff9ea825a21", "", ""
  182. },
  183. "a688ebb5ac826d6f42d45c0cc44b9b7d",
  184. "c1cad4425a438b5de714830a",
  185. "0000000000000002"
  186. }
  187. };
  188. /*
  189. * Same thing as OPENSSL_hexstr2buf() but enables us to pass the string in
  190. * 3 chunks
  191. */
  192. static unsigned char *multihexstr2buf(const char *str[3], size_t *len)
  193. {
  194. size_t outer, inner, curr = 0;
  195. unsigned char *outbuf;
  196. size_t totlen = 0;
  197. /* Check lengths of all input strings are even */
  198. for (outer = 0; outer < 3; outer++) {
  199. totlen += strlen(str[outer]);
  200. if ((totlen & 1) != 0)
  201. return NULL;
  202. }
  203. totlen /= 2;
  204. outbuf = OPENSSL_malloc(totlen);
  205. if (outbuf == NULL)
  206. return NULL;
  207. for (outer = 0; outer < 3; outer++) {
  208. for (inner = 0; str[outer][inner] != 0; inner += 2) {
  209. int hi, lo;
  210. hi = OPENSSL_hexchar2int(str[outer][inner]);
  211. lo = OPENSSL_hexchar2int(str[outer][inner + 1]);
  212. if (hi < 0 || lo < 0) {
  213. OPENSSL_free(outbuf);
  214. return NULL;
  215. }
  216. outbuf[curr++] = (hi << 4) | lo;
  217. }
  218. }
  219. *len = totlen;
  220. return outbuf;
  221. }
  222. static int load_record(SSL3_RECORD *rec, RECORD_DATA *recd, unsigned char **key,
  223. unsigned char *iv, size_t ivlen, unsigned char *seq)
  224. {
  225. unsigned char *pt = NULL, *sq = NULL, *ivtmp = NULL;
  226. size_t ptlen;
  227. *key = OPENSSL_hexstr2buf(recd->key, NULL);
  228. ivtmp = OPENSSL_hexstr2buf(recd->iv, NULL);
  229. sq = OPENSSL_hexstr2buf(recd->seq, NULL);
  230. pt = multihexstr2buf(recd->plaintext, &ptlen);
  231. if (*key == NULL || ivtmp == NULL || sq == NULL || pt == NULL)
  232. goto err;
  233. rec->data = rec->input = OPENSSL_malloc(ptlen + EVP_GCM_TLS_TAG_LEN);
  234. if (rec->data == NULL)
  235. goto err;
  236. rec->length = ptlen;
  237. memcpy(rec->data, pt, ptlen);
  238. OPENSSL_free(pt);
  239. memcpy(seq, sq, SEQ_NUM_SIZE);
  240. OPENSSL_free(sq);
  241. memcpy(iv, ivtmp, ivlen);
  242. OPENSSL_free(ivtmp);
  243. return 1;
  244. err:
  245. OPENSSL_free(*key);
  246. *key = NULL;
  247. OPENSSL_free(ivtmp);
  248. OPENSSL_free(sq);
  249. OPENSSL_free(pt);
  250. return 0;
  251. }
  252. static int test_record(SSL3_RECORD *rec, RECORD_DATA *recd, int enc)
  253. {
  254. int ret = 0;
  255. unsigned char *refd;
  256. size_t refdatalen = 0;
  257. if (enc)
  258. refd = multihexstr2buf(recd->ciphertext, &refdatalen);
  259. else
  260. refd = multihexstr2buf(recd->plaintext, &refdatalen);
  261. if (!TEST_ptr(refd)) {
  262. TEST_info("Failed to get reference data");
  263. goto err;
  264. }
  265. if (!TEST_mem_eq(rec->data, rec->length, refd, refdatalen))
  266. goto err;
  267. ret = 1;
  268. err:
  269. OPENSSL_free(refd);
  270. return ret;
  271. }
  272. #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
  273. static int test_tls13_encryption(void)
  274. {
  275. SSL_CTX *ctx = NULL;
  276. SSL *s = NULL;
  277. SSL3_RECORD rec;
  278. unsigned char *key = NULL, *iv = NULL, *seq = NULL;
  279. const EVP_CIPHER *ciph = EVP_aes_128_gcm();
  280. int ret = 0;
  281. size_t ivlen, ctr;
  282. /*
  283. * Encrypted TLSv1.3 records always have an outer content type of
  284. * application data, and a record version of TLSv1.2.
  285. */
  286. rec.data = NULL;
  287. rec.type = SSL3_RT_APPLICATION_DATA;
  288. rec.rec_version = TLS1_2_VERSION;
  289. ctx = SSL_CTX_new(TLS_method());
  290. if (!TEST_ptr(ctx)) {
  291. TEST_info("Failed creating SSL_CTX");
  292. goto err;
  293. }
  294. s = SSL_new(ctx);
  295. if (!TEST_ptr(s)) {
  296. TEST_info("Failed creating SSL");
  297. goto err;
  298. }
  299. s->enc_read_ctx = EVP_CIPHER_CTX_new();
  300. if (!TEST_ptr(s->enc_read_ctx))
  301. goto err;
  302. s->enc_write_ctx = EVP_CIPHER_CTX_new();
  303. if (!TEST_ptr(s->enc_write_ctx))
  304. goto err;
  305. s->s3.tmp.new_cipher = SSL_CIPHER_find(s, TLS13_AES_128_GCM_SHA256_BYTES);
  306. if (!TEST_ptr(s->s3.tmp.new_cipher)) {
  307. TEST_info("Failed to find cipher");
  308. goto err;
  309. }
  310. for (ctr = 0; ctr < OSSL_NELEM(refdata); ctr++) {
  311. /* Load the record */
  312. ivlen = EVP_CIPHER_iv_length(ciph);
  313. if (!load_record(&rec, &refdata[ctr], &key, s->read_iv, ivlen,
  314. RECORD_LAYER_get_read_sequence(&s->rlayer))) {
  315. TEST_error("Failed loading key into EVP_CIPHER_CTX");
  316. goto err;
  317. }
  318. /* Set up the read/write sequences */
  319. memcpy(RECORD_LAYER_get_write_sequence(&s->rlayer),
  320. RECORD_LAYER_get_read_sequence(&s->rlayer), SEQ_NUM_SIZE);
  321. memcpy(s->write_iv, s->read_iv, ivlen);
  322. /* Load the key into the EVP_CIPHER_CTXs */
  323. if (EVP_CipherInit_ex(s->enc_write_ctx, ciph, NULL, key, NULL, 1) <= 0
  324. || EVP_CipherInit_ex(s->enc_read_ctx, ciph, NULL, key, NULL, 0)
  325. <= 0) {
  326. TEST_error("Failed loading key into EVP_CIPHER_CTX\n");
  327. goto err;
  328. }
  329. /* Encrypt it */
  330. if (!TEST_size_t_eq(tls13_enc(s, &rec, 1, 1, NULL, 0), 1)) {
  331. TEST_info("Failed to encrypt record %zu", ctr);
  332. goto err;
  333. }
  334. if (!TEST_true(test_record(&rec, &refdata[ctr], 1))) {
  335. TEST_info("Record %zu encryption test failed", ctr);
  336. goto err;
  337. }
  338. /* Decrypt it */
  339. if (!TEST_int_eq(tls13_enc(s, &rec, 1, 0, NULL, 0), 1)) {
  340. TEST_info("Failed to decrypt record %zu", ctr);
  341. goto err;
  342. }
  343. if (!TEST_true(test_record(&rec, &refdata[ctr], 0))) {
  344. TEST_info("Record %zu decryption test failed", ctr);
  345. goto err;
  346. }
  347. OPENSSL_free(rec.data);
  348. OPENSSL_free(key);
  349. OPENSSL_free(iv);
  350. OPENSSL_free(seq);
  351. rec.data = NULL;
  352. key = NULL;
  353. iv = NULL;
  354. seq = NULL;
  355. }
  356. TEST_note("PASS: %zu records tested", ctr);
  357. ret = 1;
  358. err:
  359. OPENSSL_free(rec.data);
  360. OPENSSL_free(key);
  361. OPENSSL_free(iv);
  362. OPENSSL_free(seq);
  363. SSL_free(s);
  364. SSL_CTX_free(ctx);
  365. return ret;
  366. }
  367. int setup_tests(void)
  368. {
  369. ADD_TEST(test_tls13_encryption);
  370. return 1;
  371. }