tls13encryptiontest.c 14 KB

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