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