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