]>
Commit | Line | Data |
---|---|---|
f01675c6 MC |
1 | /* |
2 | * Copyright 2016 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 | ||
10 | #include <openssl/ssl.h> | |
11 | #include <openssl/evp.h> | |
12 | #include "../ssl/ssl_locl.h" | |
13 | #include "../ssl/record/record_locl.h" | |
14 | ||
15 | #include "testutil.h" | |
16 | #include "test_main.h" | |
17 | ||
18 | struct record_data { | |
19 | const char *plaintext; | |
20 | const char *ciphertext; | |
21 | const char *key; | |
22 | const char *iv; | |
23 | const char *seq; | |
24 | } refdata[] = { | |
25 | { | |
26 | "0800001e001c000a00140012001d0017001800190100010101020103010400000000" | |
27 | "0b0001b9000001b50001b0308201ac30820115a003020102020102300d06092a8648" | |
28 | "86f70d01010b0500300e310c300a06035504031303727361301e170d313630373330" | |
29 | "3031323335395a170d3236303733303031323335395a300e310c300a060355040313" | |
30 | "0372736130819f300d06092a864886f70d010101050003818d0030818902818100b4" | |
31 | "bb498f8279303d980836399b36c6988c0c68de55e1bdb826d3901a2461eafd2de49a" | |
32 | "91d015abbc9a95137ace6c1af19eaa6af98c7ced43120998e187a80ee0ccb0524b1b" | |
33 | "018c3e0b63264d449a6d38e22a5fda430846748030530ef0461c8ca9d9efbfae8ea6" | |
34 | "d1d03e2bd193eff0ab9a8002c47428a6d35a8d88d79f7f1e3f0203010001a31a3018" | |
35 | "30090603551d1304023000300b0603551d0f0404030205a0300d06092a864886f70d" | |
36 | "01010b05000381810085aad2a0e5b9276b908c65f73a7267170618a54c5f8a7b337d" | |
37 | "2df7a594365417f2eae8f8a58c8f8172f9319cf36b7fd6c55b80f21a030151567260" | |
38 | "96fd335e5e67f2dbf102702e608ccae6bec1fc63a42a99be5c3eb7107c3c54e9b9eb" | |
39 | "2bd5203b1c3b84e0a8b2f759409ba3eac9d91d402dcc0cc8f8961229ac9187b42b4d" | |
40 | "e100000f00008408040080134e22eac57321ab47db6b38b2992cec2dd79bd065a034" | |
41 | "a9af6b9e3d03475e4309e6523ccdf055453fb480804a3a7e996229eb28e734f6702b" | |
42 | "ea2b32149899ac043a4b44468197868da77147ce9f73c0543c4e3fc33e306cac8506" | |
43 | "faa80a959c5f1edccbee76eda1ad7a4fa440de35dcb87e82ec94e8725355ce750771" | |
44 | "3a609e140000207304bb73321f01b71dd94622fae98daf634490d220e4c8f3ffa255" | |
45 | "9911a56e5116", | |
46 | "40ae92071a3a548b26af31e116dfc0ba4549210b17e70da16cfbda9ccdad844d9426" | |
47 | "4a9ae65b786b3eaf0de20aa89c6babb448b6f32d07f233584296eefe19316bd97965" | |
48 | "9472ee8567cb01d70b0366cddb3c60eb9e1d789a3691dc254c14de73f4f201005045" | |
49 | "44ce184d44547e124b1f18303b4859f8f2e2b04423d23a866b43866374d54af41649" | |
50 | "d25f4a3ec2cecd5d4e6de1b24953440b46fbb74c1dbec6fbb1f16bc21d4aa0e1e936" | |
51 | "a49c07127e19719bc652a2f0b7f8df4a150b2b3c9e9e353d6ed101970ddc611abad0" | |
52 | "632c6793f9379c9d06846c311fcbd6f85edd569b8782c4c5f62294c4611ae60f8323" | |
53 | "0a53aa95e3bcbed204f19a7a1db83c0fbfec1edd2c17498fa7b5aa2321248a92592d" | |
54 | "891e4947df6bcef52f4481797d032ad332046a384abece6454b3e356d7249bfa5696" | |
55 | "793c7f7d3048dc87fa7409a4691887caaf0982c402b902d699f62dc4d5e153f13e85" | |
56 | "89e4a6206c7f74eb26ddefbb92309fb753decfea972dec7de02eda9c6d26acd7be53" | |
57 | "a8aa20f1a93f082ae6eb927a6a1b7bd9153551aedfaf94f61dd4cb9355ad7ab09f61" | |
58 | "5d9f92c21712c732c0e7e117797f38cbdc184e3a65e15a89f46cb3624f5fdb8dbbd2" | |
59 | "75f2c8492f8d95bdbd8d1dc1b9f21107bd433acbbac247239c073a2f24a4a9f8074f" | |
60 | "325f277d579b6bff0269ff19aed3809a9ddd21dd29c1363c9dc44812dd41d2111f9c" | |
61 | "2e8342046c14133b853262676f15e94de18660e04ae5c0c661ea43559af5842e161c" | |
62 | "83dd29f64508b2ec3e635a2134fc0e1a39d3ecb51dcddfcf8382c88ffe2a737842ad" | |
63 | "1de7fe505b6c4d1673870f6fc2a0f2f7972acaee368a1599d64ba18798f10333f977" | |
64 | "9bd5b05f9b084d03dab2f3d80c2eb74ec70c9866ea31c18b491cd597aae3e941205f" | |
65 | "cc38a3a10ce8c0269f02ccc9c51278e25f1a0f0731a9", | |
66 | "d2dd45f87ad87801a85ac38187f9023b", | |
67 | "f0a14f808692cef87a3daf70", | |
68 | "0000000000000000" | |
69 | }, | |
70 | { | |
71 | "1400002078367856d3c8cc4e0a95eb98906ca7a48bd3cc7029f48bd4ae0dc91ab903" | |
72 | "ca8916", | |
73 | "fa15e92daa21cd05d8f9c3152a61748d9aaf049da559718e583f95aacecad657b52a" | |
74 | "6562da09a5819e864d86ac2989360a1eb22795", | |
75 | "40e1201d75d419627f04c88530a15c9d", | |
76 | "a0f073f3b35e18f96969696b", | |
77 | "0000000000000000" | |
78 | }, | |
79 | { | |
80 | "040000a60002a3004abe594b00924e535321cadc96238da09caf9b02fecafdd65e3e" | |
81 | "418f03e43772cf512ed8066100503b1c08abbbf298a9d138ce821dd12fe1710e2137" | |
82 | "cd12e6a85cd3fd7f73706e7f5dddefb87c1ef83824638464099c9d1363e3c64ed207" | |
83 | "5c16b8ccd8e524a6bbd7a6a6e34ea1579782b15bbe7dfed5c0c0d980fb330f9d8ab2" | |
84 | "52ffe7be1277d418b6828ead4dae3b30d448442417ef76af0008002e000400020000" | |
85 | "16", | |
86 | "45a6626fa13b66ce2c5b3ef807e299a118296f26a2dd9ec7487a0673e2460d4c79f4" | |
87 | "0087dcd014c59c51379c90d26b4e4f9bb2b78f5b6761594f013ff3e4c78d83690522" | |
88 | "9eac811c4ef8b2faa89867e9ffc586f7f03c216591aa5e620eac3c62dfe60f846036" | |
89 | "bd7ecc4464b584af184e9644e94ee1d7834dba408a51cbe4248004796ed9c558e0f5" | |
90 | "f96115a6f6ba487e17d16a2e20a3d3a650a9a070fb53d9da82864b5621d77650bd0c" | |
91 | "7947e9889917b53d0515627c72b0ded521", | |
92 | "3381f6b3f94500f16226de440193e858", | |
93 | "4f1d73cc1d465eb30021c41f", | |
94 | "0000000000000000" | |
95 | }, | |
96 | { | |
97 | "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021" | |
98 | "22232425262728292a2b2c2d2e2f303117", | |
99 | "e306178ad97f74bb64f35eaf3c39846b83aef8472cbc9046749b81a949dfb12cfbc6" | |
100 | "5cbabd20ade92c1f944605892ceeb12fdee8a927bce77c83036ac5a794a8f54a69", | |
101 | "eb23a804904b80ba4fe8399e09b1ce42", | |
102 | "efa8c50c06b9c9b8c483e174", | |
103 | "0000000000000000" | |
104 | }, | |
105 | { | |
106 | "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212" | |
107 | "2232425262728292a2b2c2d2e2f303117", | |
108 | "467d99a807dbf778e6ffd8be52456c70665f890811ef2f3c495d5bbe983feedab0c25" | |
109 | "1dde596bc7e2b135909ec9f9166fb0152e8c16a84e4b1039256467f9538be4463", | |
110 | "3381f6b3f94500f16226de440193e858", | |
111 | "4f1d73cc1d465eb30021c41f", | |
112 | "0000000000000001" | |
113 | }, | |
114 | { | |
115 | "010015", | |
116 | "6bdf60847ba6fb650da36e872adc684a4af2e8", | |
117 | "eb23a804904b80ba4fe8399e09b1ce42", | |
118 | "efa8c50c06b9c9b8c483e174", | |
119 | "0000000000000001" | |
120 | }, | |
121 | { | |
122 | "010015", | |
123 | "621b7cc1962cd8a70109fee68a52efedf87d2e", | |
124 | "3381f6b3f94500f16226de440193e858", | |
125 | "4f1d73cc1d465eb30021c41f", | |
126 | "0000000000000002" | |
127 | } | |
128 | }; | |
129 | ||
130 | static int load_record(SSL3_RECORD *rec, size_t recnum, unsigned char **key, | |
131 | unsigned char *iv, size_t ivlen, unsigned char *seq) | |
132 | { | |
133 | unsigned char *pt = NULL, *sq = NULL, *ivtmp = NULL;; | |
134 | long ptlen; | |
135 | ||
136 | *key = OPENSSL_hexstr2buf(refdata[recnum].key, NULL); | |
137 | ivtmp = OPENSSL_hexstr2buf(refdata[recnum].iv, NULL); | |
138 | sq = OPENSSL_hexstr2buf(refdata[recnum].seq, NULL); | |
139 | pt = OPENSSL_hexstr2buf(refdata[recnum].plaintext, &ptlen); | |
140 | ||
141 | if (*key == NULL || ivtmp == NULL || sq == NULL || pt == NULL) | |
142 | goto err; | |
143 | ||
144 | rec->data = rec->input = OPENSSL_malloc(ptlen + EVP_GCM_TLS_TAG_LEN); | |
145 | ||
146 | if (rec->data == NULL) | |
147 | goto err; | |
148 | ||
149 | rec->length = ptlen; | |
150 | memcpy(rec->data, pt, ptlen); | |
151 | OPENSSL_free(pt); | |
152 | memcpy(seq, sq, SEQ_NUM_SIZE); | |
153 | OPENSSL_free(sq); | |
154 | memcpy(iv, ivtmp, ivlen); | |
155 | OPENSSL_free(ivtmp); | |
156 | ||
157 | return 1; | |
158 | err: | |
159 | OPENSSL_free(*key); | |
160 | OPENSSL_free(ivtmp); | |
161 | OPENSSL_free(sq); | |
162 | OPENSSL_free(pt); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | static int test_record(SSL3_RECORD *rec, size_t recnum, int enc) | |
167 | { | |
168 | int ret = 0; | |
169 | unsigned char *refd; | |
170 | long refdatalenl; | |
171 | size_t refdatalen; | |
172 | ||
173 | if (enc) | |
174 | refd = OPENSSL_hexstr2buf(refdata[recnum].ciphertext, &refdatalenl); | |
175 | else | |
176 | refd = OPENSSL_hexstr2buf(refdata[recnum].plaintext, &refdatalenl); | |
177 | ||
178 | if (refd == NULL) { | |
179 | fprintf(stderr, "Failed to get reference data for record %"OSSLzu | |
180 | "\n", recnum); | |
181 | goto err; | |
182 | } | |
183 | refdatalen = (size_t)refdatalenl; | |
184 | ||
185 | if (rec->length != refdatalen) { | |
186 | fprintf(stderr, "Unexpected length for record %"OSSLzu"\n", recnum); | |
187 | goto err; | |
188 | } | |
189 | ||
190 | if (memcmp(rec->data, refd, refdatalen) != 0) { | |
191 | fprintf(stderr, "Data does not match for record %"OSSLzu"\n", recnum); | |
192 | goto err; | |
193 | } | |
194 | ||
195 | ret = 1; | |
196 | ||
197 | err: | |
198 | OPENSSL_free(refd); | |
199 | return ret; | |
200 | } | |
201 | static int test_tls13_encryption(void) | |
202 | { | |
203 | SSL_CTX *ctx = NULL; | |
204 | SSL *s = NULL; | |
205 | SSL3_RECORD rec; | |
206 | unsigned char *key = NULL, *iv = NULL, *seq = NULL; | |
207 | const EVP_CIPHER *ciph = EVP_aes_128_gcm(); | |
208 | int ret = 0; | |
209 | size_t ivlen, ctr; | |
210 | ||
211 | rec.data = NULL; | |
212 | ||
213 | ctx = SSL_CTX_new(TLS_method()); | |
214 | if (ctx == NULL) { | |
215 | fprintf(stderr, "Failed creating SSL_CTX\n"); | |
216 | goto err; | |
217 | } | |
218 | ||
219 | s = SSL_new(ctx); | |
220 | if (s == NULL) { | |
221 | fprintf(stderr, "Failed creating SSL\n"); | |
222 | goto err; | |
223 | } | |
224 | ||
225 | s->enc_read_ctx = EVP_CIPHER_CTX_new(); | |
226 | s->enc_write_ctx = EVP_CIPHER_CTX_new(); | |
227 | if (s->enc_read_ctx == NULL || s->enc_write_ctx == NULL) { | |
228 | fprintf(stderr, "Failed creating EVP_CIPHER_CTX\n"); | |
229 | goto err; | |
230 | } | |
231 | ||
232 | for (ctr = 0; ctr < OSSL_NELEM(refdata); ctr++) { | |
233 | /* | |
234 | * Load the record, set up the read/write sequences and load the key into | |
235 | * the EVP_CIPHER_CTXs | |
236 | */ | |
237 | ivlen = EVP_CIPHER_iv_length(ciph); | |
238 | if (!load_record(&rec, ctr, &key, s->read_iv, ivlen, | |
239 | RECORD_LAYER_get_read_sequence(&s->rlayer))) { | |
240 | fprintf(stderr, "Failed loading key into EVP_CIPHER_CTX\n"); | |
241 | goto err; | |
242 | } | |
243 | ||
244 | memcpy(RECORD_LAYER_get_write_sequence(&s->rlayer), | |
245 | RECORD_LAYER_get_read_sequence(&s->rlayer), SEQ_NUM_SIZE); | |
246 | memcpy(s->write_iv, s->read_iv, ivlen); | |
247 | ||
248 | if (EVP_CipherInit_ex(s->enc_write_ctx, ciph, NULL, key, NULL, 1) <= 0 | |
249 | || EVP_CipherInit_ex(s->enc_read_ctx, ciph, NULL, key, NULL, 0) | |
250 | <= 0) { | |
251 | fprintf(stderr, "Failed loading key into EVP_CIPHER_CTX\n"); | |
252 | goto err; | |
253 | } | |
254 | ||
255 | /* Encrypt it */ | |
256 | if (tls13_enc(s, &rec, 1, 1) != 1) { | |
257 | fprintf(stderr, "Failed to encrypt record\n"); | |
258 | goto err; | |
259 | } | |
260 | ||
261 | if (!test_record(&rec, ctr, 1)) { | |
262 | fprintf(stderr, "Record encryption test failed\n"); | |
263 | goto err; | |
264 | } | |
265 | ||
266 | /* Decrypt it */ | |
267 | if (tls13_enc(s, &rec, 1, 0) != 1) { | |
268 | fprintf(stderr, "Failed to decrypt record\n"); | |
269 | goto err; | |
270 | } | |
271 | ||
272 | if (!test_record(&rec, ctr, 0)) { | |
273 | fprintf(stderr, "Record decryption test failed\n"); | |
274 | goto err; | |
275 | } | |
276 | ||
277 | OPENSSL_free(rec.data); | |
278 | OPENSSL_free(key); | |
279 | OPENSSL_free(iv); | |
280 | OPENSSL_free(seq); | |
281 | rec.data = NULL; | |
282 | key = NULL; | |
283 | iv = NULL; | |
284 | seq = NULL; | |
285 | } | |
286 | ||
287 | fprintf(stderr, "PASS: %"OSSLzu" records tested\n", ctr); | |
288 | ret = 1; | |
289 | err: | |
290 | OPENSSL_free(rec.data); | |
291 | OPENSSL_free(key); | |
292 | OPENSSL_free(iv); | |
293 | OPENSSL_free(seq); | |
294 | SSL_free(s); | |
295 | SSL_CTX_free(ctx); | |
296 | ||
297 | return ret; | |
298 | } | |
299 | ||
300 | void register_tests(void) | |
301 | { | |
302 | ADD_TEST(test_tls13_encryption); | |
303 | } |