Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/ssl_ciphers_test.cc

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4
5 // ssl_test_ticket_aead_failure_mode enumerates the possible ways in which the
6 // test |SSL_TICKET_AEAD_METHOD| can fail.
7
8 #include <gtest/gtest.h>
9
10 #include "../crypto/test/file_util.h"
11 #include "../crypto/test/test_util.h"
12 #include "internal.h"
13 #include "ssl_common_test.h"
14
15 BSSL_NAMESPACE_BEGIN
16
17
18 struct ExpectedCipher {
19 unsigned long id;
20 int in_group_flag;
21 };
22
23 struct CipherTest {
24 // The rule string to apply.
25 const char *rule;
26 // The list of expected ciphers, in order.
27 std::vector<ExpectedCipher> expected;
28 // True if this cipher list should fail in strict mode.
29 bool strict_fail;
30 };
31
32 struct CurveTest {
33 // The rule string to apply.
34 const char *rule;
35 // The list of expected curves, in order.
36 std::vector<uint16_t> expected;
37 };
38
39
40 static const CipherTest kCipherTests[] = {
41 // Selecting individual ciphers should work.
42 {
43 "ECDHE-ECDSA-CHACHA20-POLY1305:"
44 "ECDHE-RSA-CHACHA20-POLY1305:"
45 "ECDHE-ECDSA-AES128-GCM-SHA256:"
46 "ECDHE-RSA-AES128-SHA256:"
47 "ECDHE-RSA-AES128-GCM-SHA256",
48 {
49 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
50 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
51 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
52 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, 0},
53 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
54 },
55 false,
56 },
57 // + reorders selected ciphers to the end, keeping their relative order.
58 {
59 "ECDHE-ECDSA-CHACHA20-POLY1305:"
60 "ECDHE-RSA-CHACHA20-POLY1305:"
61 "ECDHE-ECDSA-AES128-GCM-SHA256:"
62 "ECDHE-RSA-AES128-GCM-SHA256:"
63 "+aRSA",
64 {
65 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
66 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
67 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
68 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
69 },
70 false,
71 },
72 // ! banishes ciphers from future selections.
73 {
74 "!aRSA:"
75 "ECDHE-ECDSA-CHACHA20-POLY1305:"
76 "ECDHE-RSA-CHACHA20-POLY1305:"
77 "ECDHE-ECDSA-AES128-GCM-SHA256:"
78 "ECDHE-RSA-AES128-GCM-SHA256",
79 {
80 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
81 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
82 },
83 false,
84 },
85 // Multiple masks can be ANDed in a single rule.
86 {
87 "kRSA+AESGCM+AES128",
88 {
89 {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0},
90 },
91 false,
92 },
93 // - removes selected ciphers, but preserves their order for future
94 // selections. Select AES_128_GCM, but order the key exchanges RSA,
95 // ECDHE_RSA.
96 {
97 "ALL:-kECDHE:"
98 "-kRSA:-ALL:"
99 "AESGCM+AES128+aRSA",
100 {
101 {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0},
102 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
103 },
104 false,
105 },
106 // Unknown selectors are no-ops, except in strict mode.
107 {
108 "ECDHE-ECDSA-CHACHA20-POLY1305:"
109 "ECDHE-RSA-CHACHA20-POLY1305:"
110 "ECDHE-ECDSA-AES128-GCM-SHA256:"
111 "ECDHE-RSA-AES128-GCM-SHA256:"
112 "BOGUS1",
113 {
114 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
115 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
116 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
117 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
118 },
119 true,
120 },
121 // Unknown selectors are no-ops, except in strict mode.
122 {
123 "ECDHE-ECDSA-CHACHA20-POLY1305:"
124 "ECDHE-RSA-CHACHA20-POLY1305:"
125 "ECDHE-ECDSA-AES128-GCM-SHA256:"
126 "ECDHE-RSA-AES128-GCM-SHA256:"
127 "-BOGUS2:+BOGUS3:!BOGUS4",
128 {
129 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
130 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
131 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
132 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
133 },
134 true,
135 },
136 // Square brackets specify equi-preference groups.
137 {
138 "[ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-ECDSA-AES128-GCM-SHA256]:"
139 "[ECDHE-RSA-CHACHA20-POLY1305]:"
140 "ECDHE-RSA-AES128-GCM-SHA256",
141 {
142 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1},
143 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
144 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
145 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
146 },
147 false,
148 },
149 // Standard names may be used instead of OpenSSL names.
150 {
151 "[TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256|"
152 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256]:"
153 "[TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256]:"
154 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
155 {
156 {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1},
157 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
158 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
159 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
160 },
161 false,
162 },
163 // @STRENGTH performs a stable strength-sort of the selected ciphers and
164 // only the selected ciphers.
165 {
166 // To simplify things, banish all but {ECDHE_RSA,RSA} x
167 // {CHACHA20,AES_256_CBC,AES_128_CBC} x SHA1.
168 "!AESGCM:!3DES:"
169 // Order some ciphers backwards by strength.
170 "ALL:-CHACHA20:-AES256:-AES128:-ALL:"
171 // Select ECDHE ones and sort them by strength. Ties should resolve
172 // based on the order above.
173 "kECDHE:@STRENGTH:-ALL:"
174 // Now bring back everything uses RSA. ECDHE_RSA should be first, sorted
175 // by strength. Then RSA, backwards by strength.
176 "aRSA",
177 {
178 {TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, 0},
179 {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, 0},
180 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
181 {TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, 0},
182 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, 0},
183 {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
184 {TLS1_CK_RSA_WITH_AES_128_SHA256, 0},
185 {TLS1_CK_RSA_WITH_AES_256_SHA, 0},
186 },
187 false,
188 },
189 // Additional masks after @STRENGTH get silently discarded.
190 //
191 // TODO(davidben): Make this an error. If not silently discarded, they get
192 // interpreted as + opcodes which are very different.
193 {
194 "ECDHE-RSA-AES128-GCM-SHA256:"
195 "ECDHE-RSA-AES256-GCM-SHA384:"
196 "@STRENGTH+AES256",
197 {
198 {TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 0},
199 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
200 },
201 false,
202 },
203 {
204 "ECDHE-RSA-AES128-GCM-SHA256:"
205 "ECDHE-RSA-AES256-GCM-SHA384:"
206 "@STRENGTH+AES256:"
207 "ECDHE-RSA-CHACHA20-POLY1305",
208 {
209 {TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 0},
210 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
211 {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
212 },
213 false,
214 },
215 // Exact ciphers may not be used in multi-part rules; they are treated
216 // as unknown aliases.
217 {
218 "ECDHE-ECDSA-AES128-GCM-SHA256:"
219 "ECDHE-RSA-AES128-GCM-SHA256:"
220 "!ECDHE-RSA-AES128-GCM-SHA256+RSA:"
221 "!ECDSA+ECDHE-ECDSA-AES128-GCM-SHA256",
222 {
223 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
224 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
225 },
226 true,
227 },
228 // SSLv3 matches everything that existed before TLS 1.2.
229 {
230 "AES128-SHA:AES128-SHA256:ECDHE-RSA-AES128-GCM-SHA256:!SSLv3",
231 {
232 {TLS1_CK_RSA_WITH_AES_128_SHA256, 0},
233 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
234 },
235 false,
236 },
237 // TLSv1.2 matches everything added in TLS 1.2.
238 {
239 "AES128-SHA:AES128-SHA256:ECDHE-RSA-AES128-GCM-SHA256:!TLSv1.2",
240 {
241 {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
242 },
243 false,
244 },
245 // The two directives have no intersection. But each component is valid, so
246 // even in strict mode it is accepted.
247 {
248 "AES128-SHA:ECDHE-RSA-AES128-GCM-SHA256:!TLSv1.2+SSLv3",
249 {
250 {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
251 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
252 },
253 false,
254 },
255 // Spaces, semi-colons and commas are separators.
256 {
257 "AES128-SHA: ECDHE-RSA-AES128-GCM-SHA256 AES256-SHA "
258 ",ECDHE-ECDSA-AES128-GCM-SHA256 ; AES128-GCM-SHA256",
259 {
260 {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
261 {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
262 {TLS1_CK_RSA_WITH_AES_256_SHA, 0},
263 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
264 {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0},
265 },
266 // …but not in strict mode.
267 true,
268 },
269 };
270
271 // In kTLSv13RuleOnly, |rule| of each CipherTest can only match TLSv1.3 ciphers.
272 // e.g. kTLSv13RuleOnly does not have rule "AESGCM+AES128", which can match
273 // some TLSv1.2 ciphers.
274 static const CipherTest kTLSv13RuleOnly[] = {
275 // Selecting individual ciphers should work.
276 {
277 "TLS_AES_256_GCM_SHA384:"
278 "TLS_CHACHA20_POLY1305_SHA256:"
279 "TLS_AES_128_GCM_SHA256",
280 {
281 {TLS1_CK_AES_256_GCM_SHA384, 0},
282 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
283 {TLS1_CK_AES_128_GCM_SHA256, 0},
284 },
285 false,
286 },
287 // + reorders selected ciphers to the end, keeping their relative order.
288 {
289 "TLS_AES_256_GCM_SHA384:"
290 "TLS_CHACHA20_POLY1305_SHA256:"
291 "TLS_AES_128_GCM_SHA256:"
292 "+AES",
293 {
294 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
295 {TLS1_CK_AES_256_GCM_SHA384, 0},
296 {TLS1_CK_AES_128_GCM_SHA256, 0},
297 },
298 false,
299 },
300 // ! banishes ciphers from future selections.
301 {
302 "!CHACHA20:"
303 "TLS_AES_256_GCM_SHA384:"
304 "TLS_CHACHA20_POLY1305_SHA256:"
305 "TLS_AES_128_GCM_SHA256:"
306 "TLS_CHACHA20_POLY1305_SHA256",
307 {
308 {TLS1_CK_AES_256_GCM_SHA384, 0},
309 {TLS1_CK_AES_128_GCM_SHA256, 0},
310 },
311 false,
312 },
313 // Square brackets specify equi-preference groups.
314 {
315 "[TLS_AES_128_GCM_SHA256|TLS_AES_256_GCM_SHA384]:"
316 "TLS_CHACHA20_POLY1305_SHA256",
317 {
318 {TLS1_CK_AES_128_GCM_SHA256, 1},
319 {TLS1_CK_AES_256_GCM_SHA384, 0},
320 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
321 },
322 false,
323 },
324 // Spaces, semi-colons and commas are separators.
325 {
326 "TLS_AES_256_GCM_SHA384 ; "
327 "TLS_CHACHA20_POLY1305_SHA256 , "
328 "TLS_AES_128_GCM_SHA256",
329 {
330 {TLS1_CK_AES_256_GCM_SHA384, 0},
331 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
332 {TLS1_CK_AES_128_GCM_SHA256, 0},
333 },
334 false,
335 },
336 };
337
338 // Besides kTLSv13RuleOnly, additional rules can match TLSv1.3 ciphers.
339 static const CipherTest kTLSv13CipherTests[] = {
340 // Multiple masks can be ANDed in a single rule.
341 {
342 "AESGCM+AES128",
343 {
344 {TLS1_CK_AES_128_GCM_SHA256, 0},
345 },
346 false,
347 },
348 // - removes selected ciphers, but preserves their order for future
349 // selections. Select AES_128_GCM, but order the key exchanges RSA,
350 // ECDHE_RSA.
351 {
352 "ALL:-AES",
353 {
354 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
355 },
356 false,
357 },
358 // Unknown selectors or TLSv1.2 ciphers are no-ops.
359 {
360 "TLS_CHACHA20_POLY1305_SHA256:"
361 "ECDHE-RSA-CHACHA20-POLY1305:"
362 "BOGUS1",
363 {
364 {TLS1_CK_CHACHA20_POLY1305_SHA256, 0},
365 },
366 true,
367 },
368 };
369
370 static const char *kBadRules[] = {
371 // Invalid brackets.
372 "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256",
373 "RSA]",
374 "[[RSA]]",
375 // Operators inside brackets.
376 "[+RSA]",
377 // Unknown directive.
378 "@BOGUS",
379 "BOGUS",
380 // COMPLEMENTOFDEFAULT is empty.
381 "COMPLEMENTOFDEFAULT",
382 // Invalid command.
383 "?BAR",
384 // Special operators are not allowed if equi-preference groups are used.
385 "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:+FOO",
386 "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:!FOO",
387 "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:-FOO",
388 "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:@STRENGTH",
389 // Opcode supplied, but missing selector.
390 "+",
391 // Spaces are forbidden in equal-preference groups.
392 "[AES128-SHA | AES128-SHA256]",
393 };
394
395 static const char *kMustNotIncludeNull[] = {
396 "ALL", "DEFAULT", "HIGH", "FIPS", "SHA",
397 "SHA1", "RSA", "SSLv3", "TLSv1", "TLSv1.2",
398 };
399
400 static const char *kTLSv13MustNotIncludeNull[] = {
401 "ALL",
402 "DEFAULT",
403 "HIGH",
404 "FIPS",
405 };
406
407 static const char *kMustNotInclude3DES[] = {
408 "ALL", "DEFAULT", "HIGH", "FIPS", "SSLv3", "TLSv1", "TLSv1.2",
409 };
410
411 static const CurveTest kCurveTests[] = {
412 {
413 "P-256",
414 {SSL_GROUP_SECP256R1},
415 },
416 {
417 "P-256:P-384:P-521:X25519",
418 {
419 SSL_GROUP_SECP256R1,
420 SSL_GROUP_SECP384R1,
421 SSL_GROUP_SECP521R1,
422 SSL_GROUP_X25519,
423 },
424 },
425 {
426 "prime256v1:secp384r1:secp521r1:x25519",
427 {
428 SSL_GROUP_SECP256R1,
429 SSL_GROUP_SECP384R1,
430 SSL_GROUP_SECP521R1,
431 SSL_GROUP_X25519,
432 },
433 },
434 {
435 "SecP256r1MLKEM768:prime256v1:secp384r1:secp521r1:x25519",
436 {
437 SSL_GROUP_SECP256R1_MLKEM768,
438 SSL_GROUP_SECP256R1,
439 SSL_GROUP_SECP384R1,
440 SSL_GROUP_SECP521R1,
441 SSL_GROUP_X25519,
442 },
443 },
444 {
445 "X25519MLKEM768:prime256v1:secp384r1",
446 {
447 SSL_GROUP_X25519_MLKEM768,
448 SSL_GROUP_SECP256R1,
449 SSL_GROUP_SECP384R1,
450 },
451 },
452 {
453 "X25519:X25519MLKEM768",
454 {
455 SSL_GROUP_X25519,
456 SSL_GROUP_X25519_MLKEM768,
457 },
458 },
459 {
460 "X25519:SecP256r1MLKEM768:prime256v1",
461 {
462 SSL_GROUP_X25519,
463 SSL_GROUP_SECP256R1_MLKEM768,
464 SSL_GROUP_SECP256R1,
465 },
466 },
467 {
468 "SecP384r1MLKEM1024:X25519MLKEM768:SecP256r1MLKEM768",
469 {
470 SSL_GROUP_SECP384R1_MLKEM1024,
471 SSL_GROUP_X25519_MLKEM768,
472 SSL_GROUP_SECP256R1_MLKEM768,
473 },
474 },
475 {
476 "MLKEM512:MLKEM768:MLKEM1024",
477 {
478 SSL_GROUP_MLKEM512,
479 SSL_GROUP_MLKEM768,
480 SSL_GROUP_MLKEM1024,
481 },
482 },
483 {
484 "ML-KEM-512:ML-KEM-768:ML-KEM-1024",
485 {
486 SSL_GROUP_MLKEM512,
487 SSL_GROUP_MLKEM768,
488 SSL_GROUP_MLKEM1024,
489 },
490 },
491 };
492
493
494
495 static const char *kBadCurvesLists[] = {
496 "",
497 ":",
498 "::",
499 "P-256::X25519",
500 "RSA:P-256",
501 "P-256:RSA",
502 "X25519:P-256:",
503 ":X25519:P-256",
504 "mlkem768",
505 "x25519_mlkem768:prime256v1",
506 };
507
508 static STACK_OF(SSL_CIPHER) *tls13_ciphers(const SSL_CTX *ctx) {
509 return ctx->tls13_cipher_list->ciphers.get();
510 }
511
512 static std::string CipherListToString(SSL_CTX *ctx, bool tlsv13_ciphers) {
513 bool in_group = false;
514 std::string ret;
515 const STACK_OF(SSL_CIPHER) *ciphers =
516 tlsv13_ciphers ? tls13_ciphers(ctx) : SSL_CTX_get_ciphers(ctx);
517 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
518 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
519 if (!in_group && SSL_CTX_cipher_in_group(ctx, i)) {
520 ret += "\t[\n";
521 in_group = true;
522 }
523 ret += "\t";
524 if (in_group) {
525 ret += " ";
526 }
527 ret += SSL_CIPHER_get_name(cipher);
528 ret += "\n";
529 if (in_group && !SSL_CTX_cipher_in_group(ctx, i)) {
530 ret += "\t]\n";
531 in_group = false;
532 }
533 }
534 return ret;
535 }
536
537 static bool CipherListsEqual(SSL_CTX *ctx,
538 const std::vector<ExpectedCipher> &expected,
539 bool tlsv13_ciphers) {
540 STACK_OF(SSL_CIPHER) *ciphers =
541 tlsv13_ciphers ? tls13_ciphers(ctx) : SSL_CTX_get_ciphers(ctx);
542 if (sk_SSL_CIPHER_num(ciphers) != expected.size()) {
543 return false;
544 }
545
546 for (size_t i = 0; i < expected.size(); i++) {
547 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
548 if (expected[i].id != SSL_CIPHER_get_id(cipher) ||
549 expected[i].in_group_flag != !!SSL_CTX_cipher_in_group(ctx, i)) {
550 return false;
551 }
552 }
553
554 return true;
555 }
556
557 TEST(SSLTest, CipherRules) {
558 for (const CipherTest &t : kCipherTests) {
559 SCOPED_TRACE(t.rule);
560 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
561 ASSERT_TRUE(ctx);
562
563 // We configure default TLS 1.3 ciphersuites in |SSL_CTX| which pollute
564 // |ctx->cipher_list|. Set it to an empty list so we can test TLS 1.2
565 // configurations.
566 ASSERT_TRUE(SSL_CTX_set_ciphersuites(ctx.get(), ""));
567
568 // Test lax mode.
569 ASSERT_TRUE(SSL_CTX_set_cipher_list(ctx.get(), t.rule));
570 EXPECT_TRUE(
571 CipherListsEqual(ctx.get(), t.expected, false /* not TLSv1.3 only */))
572 << "Cipher rule evaluated to:\n"
573 << CipherListToString(ctx.get(), false /* not TLSv1.3 only */);
574
575 // Test strict mode.
576 if (t.strict_fail) {
577 EXPECT_FALSE(SSL_CTX_set_strict_cipher_list(ctx.get(), t.rule));
578 } else {
579 ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), t.rule));
580 EXPECT_TRUE(
581 CipherListsEqual(ctx.get(), t.expected, false /* not TLSv1.3 only */))
582 << "Cipher rule evaluated to:\n"
583 << CipherListToString(ctx.get(), false /* not TLSv1.3 only */);
584 }
585 }
586
587 for (const char *rule : kBadRules) {
588 SCOPED_TRACE(rule);
589 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
590 ASSERT_TRUE(ctx);
591
592 EXPECT_FALSE(SSL_CTX_set_cipher_list(ctx.get(), rule));
593 ERR_clear_error();
594 }
595
596 // kTLSv13RuleOnly are valid test cases for |SSL_CTX_set_ciphersuites|,
597 // which configures only TLSv1.3 ciphers.
598 // |SSL_CTX_set_cipher_list| only supports TLSv1.2 and below ciphers
599 // configuration. Accordingly, kTLSv13RuleOnly result in
600 // |SSL_R_NO_CIPHER_MATCH|.
601 for (const CipherTest &t : kTLSv13RuleOnly) {
602 SCOPED_TRACE(t.rule);
603 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
604 ASSERT_TRUE(ctx);
605
606 // We configure default TLS 1.3 ciphersuites in |SSL_CTX| which pollute
607 // |ctx->cipher_list|. Set it to an empty list so we can test TLS 1.2
608 // configurations.
609 ASSERT_TRUE(SSL_CTX_set_ciphersuites(ctx.get(), ""));
610
611 EXPECT_FALSE(SSL_CTX_set_cipher_list(ctx.get(), t.rule));
612 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH);
613 ERR_clear_error();
614 }
615
616 for (const char *rule : kMustNotIncludeNull) {
617 SCOPED_TRACE(rule);
618 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
619 ASSERT_TRUE(ctx);
620
621 ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), rule));
622 for (const SSL_CIPHER *cipher : SSL_CTX_get_ciphers(ctx.get())) {
623 EXPECT_NE(NID_undef, SSL_CIPHER_get_cipher_nid(cipher));
624 }
625 }
626
627 for (const char *rule : kMustNotInclude3DES) {
628 SCOPED_TRACE(rule);
629 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
630 ASSERT_TRUE(ctx);
631
632 ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), rule));
633 for (const SSL_CIPHER *cipher : SSL_CTX_get_ciphers(ctx.get())) {
634 EXPECT_NE(NID_des_ede3_cbc, SSL_CIPHER_get_cipher_nid(cipher));
635 }
636 }
637 }
638
639 static std::vector<CipherTest> combine_tests(const CipherTest *c1,
640 size_t size_1,
641 const CipherTest *c2,
642 size_t size_2) {
643 std::vector<CipherTest> ret(size_1 + size_2);
644 size_t j = 0;
645 for (size_t i = 0; i < size_1; i++) {
646 ret[j++] = c1[i];
647 }
648 for (size_t i = 0; i < size_2; i++) {
649 ret[j++] = c2[i];
650 }
651 return ret;
652 }
653
654 TEST(SSLTest, TLSv13CipherRules) {
655 std::vector<CipherTest> cipherRules =
656 combine_tests(kTLSv13RuleOnly, OPENSSL_ARRAY_SIZE(kTLSv13RuleOnly),
657 kTLSv13CipherTests, OPENSSL_ARRAY_SIZE(kTLSv13CipherTests));
658 for (const CipherTest &t : cipherRules) {
659 SCOPED_TRACE(t.rule);
660 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
661 ASSERT_TRUE(ctx);
662 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
663 ASSERT_TRUE(ssl);
664
665 // Test lax mode.
666 ASSERT_TRUE(SSL_CTX_set_ciphersuites(ctx.get(), t.rule));
667 ASSERT_TRUE(SSL_set_ciphersuites(ssl.get(), t.rule));
668 EXPECT_TRUE(
669 CipherListsEqual(ctx.get(), t.expected, true /* TLSv1.3 only */))
670 << "Cipher rule evaluated to:\n"
671 << CipherListToString(ctx.get(), true /* TLSv1.3 only */);
672
673 // TODO: add |SSL_CTX_set_strict_ciphersuites| and test strict mode.
674 }
675
676 for (const char *rule : kBadRules) {
677 SCOPED_TRACE(rule);
678 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
679 ASSERT_TRUE(ctx);
680 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
681 ASSERT_TRUE(ssl);
682
683 EXPECT_FALSE(SSL_CTX_set_ciphersuites(ctx.get(), rule));
684 EXPECT_FALSE(SSL_set_ciphersuites(ssl.get(), rule));
685 ERR_clear_error();
686 }
687
688 for (const char *rule : kTLSv13MustNotIncludeNull) {
689 SCOPED_TRACE(rule);
690 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
691 ASSERT_TRUE(ctx);
692 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
693 ASSERT_TRUE(ssl);
694
695 ASSERT_TRUE(SSL_CTX_set_ciphersuites(ctx.get(), rule));
696 ASSERT_TRUE(SSL_set_ciphersuites(ssl.get(), rule));
697 // Currenly, only three TLSv1.3 ciphers are supported.
698 EXPECT_EQ(3u, sk_SSL_CIPHER_num(tls13_ciphers(ctx.get())));
699 for (const SSL_CIPHER *cipher : tls13_ciphers(ctx.get())) {
700 EXPECT_NE(NID_undef, SSL_CIPHER_get_cipher_nid(cipher));
701 }
702 }
703
704 // kCipherTests are valid test cases for |SSL_CTX_set_cipher_list|,
705 // which configures only TLSv1.2 and below ciphers.
706 // |SSL_CTX_set_ciphersuites| only supports TLSv1.3 ciphers configuration.
707 // Accordingly, kCipherTests result in |SSL_R_NO_CIPHER_MATCH|.
708 // If kCipherTests starts to include rules that can match TLSv1.3 ciphers,
709 // kCipherTests should get splitted.
710 for (const CipherTest &t : kCipherTests) {
711 SCOPED_TRACE(t.rule);
712 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
713 ASSERT_TRUE(ctx);
714 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
715 ASSERT_TRUE(ssl);
716
717 EXPECT_FALSE(SSL_CTX_set_ciphersuites(ctx.get(), t.rule));
718 EXPECT_FALSE(SSL_set_ciphersuites(ssl.get(), t.rule));
719 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH);
720 ERR_clear_error();
721 }
722 }
723
724 TEST(SSLTest, CurveRules) {
725 for (const CurveTest &t : kCurveTests) {
726 SCOPED_TRACE(t.rule);
727 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
728 ASSERT_TRUE(ctx);
729
730 ASSERT_TRUE(SSL_CTX_set1_groups_list(ctx.get(), t.rule));
731 ASSERT_EQ(t.expected.size(), ctx->supported_group_list.size());
732 for (size_t i = 0; i < t.expected.size(); i++) {
733 EXPECT_EQ(t.expected[i], ctx->supported_group_list[i]);
734 }
735 }
736
737 for (const char *rule : kBadCurvesLists) {
738 SCOPED_TRACE(rule);
739 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
740 ASSERT_TRUE(ctx);
741
742 EXPECT_FALSE(SSL_CTX_set1_groups_list(ctx.get(), rule));
743 ERR_clear_error();
744 }
745 }
746
747
748
749 static void ExpectDefaultVersion(uint16_t min_version, uint16_t max_version,
750 const SSL_METHOD *(*method)(void)) {
751 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method()));
752 ASSERT_TRUE(ctx);
753 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
754 ASSERT_TRUE(ssl);
755 EXPECT_EQ(0, SSL_CTX_get_min_proto_version(ctx.get()));
756 EXPECT_EQ(0, SSL_CTX_get_max_proto_version(ctx.get()));
757 EXPECT_EQ(0, SSL_get_min_proto_version(ssl.get()));
758 EXPECT_EQ(0, SSL_get_max_proto_version(ssl.get()));
759
760 EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), min_version));
761 EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), max_version));
762 EXPECT_EQ(min_version, SSL_CTX_get_min_proto_version(ctx.get()));
763 EXPECT_EQ(max_version, SSL_CTX_get_max_proto_version(ctx.get()));
764 EXPECT_TRUE(SSL_set_min_proto_version(ssl.get(), min_version));
765 EXPECT_TRUE(SSL_set_max_proto_version(ssl.get(), max_version));
766 EXPECT_EQ(min_version, SSL_get_min_proto_version(ssl.get()));
767 EXPECT_EQ(max_version, SSL_get_max_proto_version(ssl.get()));
768 }
769
770 TEST(SSLTest, DefaultVersion) {
771 ExpectDefaultVersion(TLS1_VERSION, TLS1_3_VERSION, &TLS_method);
772 ExpectDefaultVersion(TLS1_VERSION, TLS1_VERSION, &TLSv1_method);
773 ExpectDefaultVersion(TLS1_1_VERSION, TLS1_1_VERSION, &TLSv1_1_method);
774 ExpectDefaultVersion(TLS1_2_VERSION, TLS1_2_VERSION, &TLSv1_2_method);
775 ExpectDefaultVersion(DTLS1_VERSION, DTLS1_2_VERSION, &DTLS_method);
776 ExpectDefaultVersion(DTLS1_VERSION, DTLS1_VERSION, &DTLSv1_method);
777 ExpectDefaultVersion(DTLS1_2_VERSION, DTLS1_2_VERSION, &DTLSv1_2_method);
778 }
779
780 TEST(SSLTest, CipherProperties) {
781 static const struct {
782 int id;
783 const char *standard_name;
784 int cipher_nid;
785 int digest_nid;
786 int kx_nid;
787 int auth_nid;
788 int prf_nid;
789 } kTests[] = {
790 {
791 SSL3_CK_RSA_DES_192_CBC3_SHA,
792 "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
793 NID_des_ede3_cbc,
794 NID_sha1,
795 NID_kx_rsa,
796 NID_auth_rsa,
797 NID_md5_sha1,
798 },
799 {
800 TLS1_CK_RSA_WITH_AES_128_SHA,
801 "TLS_RSA_WITH_AES_128_CBC_SHA",
802 NID_aes_128_cbc,
803 NID_sha1,
804 NID_kx_rsa,
805 NID_auth_rsa,
806 NID_md5_sha1,
807 },
808 {
809 TLS1_CK_RSA_WITH_AES_128_SHA256,
810 "TLS_RSA_WITH_AES_128_CBC_SHA256",
811 NID_aes_128_cbc,
812 NID_sha256,
813 NID_kx_rsa,
814 NID_auth_rsa,
815 NID_sha256,
816 },
817 {
818 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
819 "TLS_PSK_WITH_AES_256_CBC_SHA",
820 NID_aes_256_cbc,
821 NID_sha1,
822 NID_kx_psk,
823 NID_auth_psk,
824 NID_md5_sha1,
825 },
826 {
827 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
828 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
829 NID_aes_128_cbc,
830 NID_sha1,
831 NID_kx_ecdhe,
832 NID_auth_rsa,
833 NID_md5_sha1,
834 },
835 {
836 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
837 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
838 NID_aes_128_cbc,
839 NID_sha256,
840 NID_kx_ecdhe,
841 NID_auth_rsa,
842 NID_sha256,
843 },
844 {
845 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
846 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
847 NID_aes_256_cbc,
848 NID_sha1,
849 NID_kx_ecdhe,
850 NID_auth_rsa,
851 NID_md5_sha1,
852 },
853 {
854 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
855 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
856 NID_aes_128_gcm,
857 NID_undef,
858 NID_kx_ecdhe,
859 NID_auth_rsa,
860 NID_sha256,
861 },
862 {
863 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
864 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
865 NID_aes_128_gcm,
866 NID_undef,
867 NID_kx_ecdhe,
868 NID_auth_ecdsa,
869 NID_sha256,
870 },
871 {
872 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
873 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
874 NID_aes_256_gcm,
875 NID_undef,
876 NID_kx_ecdhe,
877 NID_auth_ecdsa,
878 NID_sha384,
879 },
880 {
881 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
882 "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
883 NID_aes_128_cbc,
884 NID_sha1,
885 NID_kx_ecdhe,
886 NID_auth_psk,
887 NID_md5_sha1,
888 },
889 {
890 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
891 "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
892 NID_chacha20_poly1305,
893 NID_undef,
894 NID_kx_ecdhe,
895 NID_auth_rsa,
896 NID_sha256,
897 },
898 {
899 TLS1_3_CK_AES_256_GCM_SHA384,
900 "TLS_AES_256_GCM_SHA384",
901 NID_aes_256_gcm,
902 NID_undef,
903 NID_kx_any,
904 NID_auth_any,
905 NID_sha384,
906 },
907 {
908 TLS1_3_CK_AES_128_GCM_SHA256,
909 "TLS_AES_128_GCM_SHA256",
910 NID_aes_128_gcm,
911 NID_undef,
912 NID_kx_any,
913 NID_auth_any,
914 NID_sha256,
915 },
916 {
917 TLS1_3_CK_CHACHA20_POLY1305_SHA256,
918 "TLS_CHACHA20_POLY1305_SHA256",
919 NID_chacha20_poly1305,
920 NID_undef,
921 NID_kx_any,
922 NID_auth_any,
923 NID_sha256,
924 },
925 };
926
927 for (const auto &t : kTests) {
928 SCOPED_TRACE(t.standard_name);
929
930 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(t.id & 0xffff);
931 ASSERT_TRUE(cipher);
932 EXPECT_STREQ(t.standard_name, SSL_CIPHER_standard_name(cipher));
933
934 EXPECT_EQ(t.cipher_nid, SSL_CIPHER_get_cipher_nid(cipher));
935 EXPECT_EQ(t.digest_nid, SSL_CIPHER_get_digest_nid(cipher));
936 EXPECT_EQ(t.kx_nid, SSL_CIPHER_get_kx_nid(cipher));
937 EXPECT_EQ(t.auth_nid, SSL_CIPHER_get_auth_nid(cipher));
938 const EVP_MD *md = SSL_CIPHER_get_handshake_digest(cipher);
939 ASSERT_TRUE(md);
940 EXPECT_EQ(t.prf_nid, EVP_MD_nid(md));
941 EXPECT_EQ(t.prf_nid, SSL_CIPHER_get_prf_nid(cipher));
942 }
943 }
944
945 // Correct ID and name
946 #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
947 #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
948 // Invalid ID
949 #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x13")
950 TEST(SSLTest, FindingCipher) {
951 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
952 bssl::UniquePtr<SSL_CTX> server_ctx =
953 CreateContextWithTestCertificate(TLS_method());
954 ASSERT_TRUE(client_ctx);
955 ASSERT_TRUE(server_ctx);
956 // Configure only TLS 1.3.
957 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION));
958 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
959
960 bssl::UniquePtr<SSL> client, server;
961 ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(),
962 server_ctx.get()));
963
964 SCOPED_TRACE("TLS_AES_128_GCM_SHA256");
965 const SSL_CIPHER *cipher1 =
966 SSL_CIPHER_find(server.get(), TLS13_AES_128_GCM_SHA256_BYTES);
967 ASSERT_TRUE(cipher1);
968 EXPECT_STREQ("TLS_AES_128_GCM_SHA256", SSL_CIPHER_standard_name(cipher1));
969
970 SCOPED_TRACE("TLS_CHACHA20_POLY1305_SHA256");
971 const SSL_CIPHER *cipher2 =
972 SSL_CIPHER_find(server.get(), TLS13_CHACHA20_POLY1305_SHA256_BYTES);
973 ASSERT_TRUE(cipher2);
974 EXPECT_STREQ("TLS_CHACHA20_POLY1305_SHA256",
975 SSL_CIPHER_standard_name(cipher2));
976
977 SCOPED_TRACE("TLS_AES_256_GCM_SHA384");
978 const SSL_CIPHER *cipher3 =
979 SSL_CIPHER_find(client.get(), TLS13_AES_256_GCM_SHA384_BYTES);
980 ASSERT_FALSE(cipher3);
981 }
982
983 TEST(SSLTest, CheckSSLCipherInheritance) {
984 // This configures SSL_CTX objects with default TLS 1.2 and 1.3 ciphersuites
985 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
986 bssl::UniquePtr<SSL_CTX> server_ctx =
987 CreateContextWithTestCertificate(TLS_method());
988 ASSERT_TRUE(client_ctx);
989 ASSERT_TRUE(server_ctx);
990
991 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_2_VERSION));
992 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
993 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_2_VERSION));
994 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION));
995
996 ASSERT_TRUE(
997 SSL_CTX_set_ciphersuites(client_ctx.get(), "TLS_AES_128_GCM_SHA256"));
998 ASSERT_TRUE(
999 SSL_CTX_set_ciphersuites(server_ctx.get(), "TLS_AES_128_GCM_SHA256"));
1000 ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(),
1001 "TLS_RSA_WITH_AES_128_CBC_SHA"));
1002 ASSERT_TRUE(SSL_CTX_set_cipher_list(server_ctx.get(),
1003 "TLS_RSA_WITH_AES_128_CBC_SHA"));
1004
1005 bssl::UniquePtr<SSL> client, server;
1006 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1007 server_ctx.get()));
1008
1009 // Modify CTX ciphersuites
1010 ASSERT_TRUE(
1011 SSL_CTX_set_ciphersuites(client_ctx.get(), "TLS_AES_256_GCM_SHA384"));
1012 ASSERT_TRUE(
1013 SSL_CTX_set_ciphersuites(server_ctx.get(), "TLS_AES_256_GCM_SHA384"));
1014 ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(),
1015 "TLS_RSA_WITH_AES_256_CBC_SHA"));
1016 ASSERT_TRUE(SSL_CTX_set_cipher_list(server_ctx.get(),
1017 "TLS_RSA_WITH_AES_256_CBC_SHA"));
1018
1019 // Ensure SSL object inherits initial CTX cipher suites
1020 STACK_OF(SSL_CIPHER) *client_ciphers = SSL_get_ciphers(client.get());
1021 STACK_OF(SSL_CIPHER) *server_ciphers = SSL_get_ciphers(server.get());
1022 ASSERT_TRUE(client_ciphers);
1023 ASSERT_TRUE(server_ciphers);
1024 ASSERT_EQ(sk_SSL_CIPHER_num(client_ciphers), 2u);
1025 ASSERT_EQ(sk_SSL_CIPHER_num(server_ciphers), 2u);
1026 const SSL_CIPHER *tls13_cipher =
1027 SSL_get_cipher_by_value(TLS1_3_CK_AES_128_GCM_SHA256 & 0xFFFF);
1028 const SSL_CIPHER *tls12_cipher =
1029 SSL_get_cipher_by_value(TLS1_CK_RSA_WITH_AES_128_SHA & 0xFFFF);
1030 ASSERT_TRUE(tls13_cipher);
1031 ASSERT_TRUE(tls12_cipher);
1032
1033 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, tls12_cipher));
1034 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, tls13_cipher));
1035 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, tls12_cipher));
1036 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, tls13_cipher));
1037
1038 SSL_set_shed_handshake_config(client.get(), 1);
1039 SSL_set_shed_handshake_config(server.get(), 1);
1040
1041 ASSERT_TRUE(CompleteHandshakes(client.get(), server.get()));
1042
1043 // Ensure we fall back to ctx once config is shed
1044 client_ciphers = SSL_get_ciphers(client.get());
1045 server_ciphers = SSL_get_ciphers(server.get());
1046
1047 tls13_cipher = SSL_get_cipher_by_value(TLS1_3_CK_AES_256_GCM_SHA384 & 0xFFFF);
1048 tls12_cipher = SSL_get_cipher_by_value(TLS1_CK_RSA_WITH_AES_256_SHA & 0xFFFF);
1049
1050 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, tls13_cipher));
1051 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, tls12_cipher));
1052 }
1053
1054 TEST(SSLTest, SSLGetCiphersReturnsTLS13Default) {
1055 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1056 bssl::UniquePtr<SSL_CTX> server_ctx =
1057 CreateContextWithTestCertificate(TLS_method());
1058 ASSERT_TRUE(client_ctx);
1059 ASSERT_TRUE(server_ctx);
1060 // Configure only TLS 1.3.
1061 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION));
1062 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1063 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION));
1064 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION));
1065
1066 bssl::UniquePtr<SSL> client, server;
1067 // Have to ensure config is not shed per current implementation of
1068 // SSL_get_ciphers.
1069 ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(),
1070 server_ctx.get(), ClientConfig(), false));
1071
1072 // Ensure default TLS 1.3 Ciphersuites are present
1073 const SSL_CIPHER *cipher1 =
1074 SSL_get_cipher_by_value(TLS1_3_CK_AES_128_GCM_SHA256 & 0xFFFF);
1075 ASSERT_TRUE(cipher1);
1076 const SSL_CIPHER *cipher2 =
1077 SSL_get_cipher_by_value(TLS1_3_CK_AES_256_GCM_SHA384 & 0xFFFF);
1078 ASSERT_TRUE(cipher2);
1079 const SSL_CIPHER *cipher3 =
1080 SSL_get_cipher_by_value(TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xFFFF);
1081 ASSERT_TRUE(cipher3);
1082
1083 STACK_OF(SSL_CIPHER) *client_ciphers = SSL_get_ciphers(client.get());
1084 STACK_OF(SSL_CIPHER) *server_ciphers = SSL_get_ciphers(server.get());
1085
1086 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher1));
1087 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher2));
1088 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher3));
1089
1090 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher1));
1091 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher2));
1092 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher3));
1093 }
1094
1095 TEST(SSLTest, TLS13ConfigCiphers) {
1096 // This configures SSL_CTX objects with default TLS 1.2 and 1.3 ciphersuites
1097 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1098 bssl::UniquePtr<SSL_CTX> server_ctx =
1099 CreateContextWithTestCertificate(TLS_method());
1100 ASSERT_TRUE(client_ctx);
1101 ASSERT_TRUE(server_ctx);
1102
1103 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_2_VERSION));
1104 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1105 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_2_VERSION));
1106 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION));
1107
1108 // Restrict TLS 1.3 ciphersuite
1109 ASSERT_TRUE(SSL_CTX_set_ciphersuites(
1110 client_ctx.get(), "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"));
1111 ASSERT_TRUE(SSL_CTX_set_ciphersuites(
1112 server_ctx.get(), "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"));
1113
1114 bssl::UniquePtr<SSL> client, server;
1115 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1116 server_ctx.get()));
1117
1118 // Modify ciphersuites on the SSL object, this modifies ssl->config
1119 ASSERT_TRUE(SSL_set_ciphersuites(client.get(), "TLS_AES_256_GCM_SHA384"));
1120 ASSERT_TRUE(SSL_set_ciphersuites(server.get(), "TLS_AES_128_GCM_SHA256"));
1121
1122 // Handshake should fail as config objects have no shared cipher.
1123 ASSERT_FALSE(CompleteHandshakes(client.get(), server.get()));
1124 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1125
1126 bssl::UniquePtr<SSL> client2, server2;
1127 ASSERT_TRUE(CreateClientAndServer(&client2, &server2, client_ctx.get(),
1128 server_ctx.get()));
1129
1130 // Modify ciphersuites on the SSL object, this modifies ssl->config
1131 ASSERT_TRUE(
1132 SSL_set_ciphersuites(client2.get(), "TLS_CHACHA20_POLY1305_SHA256"));
1133 ASSERT_TRUE(
1134 SSL_set_ciphersuites(server2.get(), "TLS_CHACHA20_POLY1305_SHA256"));
1135
1136 ASSERT_TRUE(CompleteHandshakes(client2.get(), server2.get()));
1137 ASSERT_EQ(SSL_CIPHER_get_id(SSL_get_current_cipher(client2.get())),
1138 (uint32_t)TLS1_3_CK_CHACHA20_POLY1305_SHA256);
1139 ASSERT_EQ(SSL_CIPHER_get_id(SSL_get_current_cipher(server2.get())),
1140 (uint32_t)TLS1_3_CK_CHACHA20_POLY1305_SHA256);
1141 }
1142
1143 TEST(SSLTest, TLS13ConfigCtxInteraction) {
1144 // This configures SSL_CTX objects with default TLS 1.2 and 1.3 ciphersuites
1145 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1146 bssl::UniquePtr<SSL_CTX> server_ctx =
1147 CreateContextWithTestCertificate(TLS_method());
1148 ASSERT_TRUE(client_ctx);
1149 ASSERT_TRUE(server_ctx);
1150
1151 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_2_VERSION));
1152 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1153 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_2_VERSION));
1154 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION));
1155
1156 // Restrict TLS 1.3 ciphersuite on the SSL_CTX objects
1157 ASSERT_TRUE(
1158 SSL_CTX_set_ciphersuites(client_ctx.get(), "TLS_AES_128_GCM_SHA256"));
1159 ASSERT_TRUE(
1160 SSL_CTX_set_ciphersuites(server_ctx.get(), "TLS_AES_128_GCM_SHA256"));
1161
1162 bssl::UniquePtr<SSL> client, server;
1163 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1164 server_ctx.get()));
1165
1166 // Modify TLS 1.3 ciphersuites for client's SSL object, but not server
1167 ASSERT_TRUE(SSL_set_ciphersuites(client.get(), "TLS_AES_256_GCM_SHA384"));
1168
1169 // Handshake should fail as client SSL config and server CTX objects have no
1170 // shared TLS 1.3 cipher.
1171 ASSERT_FALSE(CompleteHandshakes(client.get(), server.get()));
1172 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1173
1174 ERR_clear_error();
1175
1176 bssl::UniquePtr<SSL> client2, server2;
1177 ASSERT_TRUE(CreateClientAndServer(&client2, &server2, client_ctx.get(),
1178 server_ctx.get()));
1179
1180 // Modify TLS 1.3 ciphersuites for server2 SSL object, but not client
1181 ASSERT_TRUE(SSL_set_ciphersuites(server2.get(), "TLS_AES_256_GCM_SHA384"));
1182
1183 // Handshake should fail as server SSL config and client CTX objects have no
1184 // shared TLS 1.3 cipher.
1185 ASSERT_FALSE(CompleteHandshakes(client2.get(), server2.get()));
1186 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1187 }
1188
1189 TEST(SSLTest, TLS12ConfigCiphers) {
1190 // This configures SSL_CTX objects with default TLS 1.2 and 1.3 ciphersuites
1191 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1192 bssl::UniquePtr<SSL_CTX> server_ctx =
1193 CreateContextWithTestCertificate(TLS_method());
1194 ASSERT_TRUE(client_ctx);
1195 ASSERT_TRUE(server_ctx);
1196
1197 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_VERSION));
1198 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_2_VERSION));
1199 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_VERSION));
1200 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION));
1201
1202 // Restrict TLS 1.2 ciphersuite
1203 ASSERT_TRUE(SSL_CTX_set_cipher_list(
1204 client_ctx.get(),
1205 "TLS_RSA_WITH_AES_256_CBC_SHA:TLS_RSA_WITH_AES_256_GCM_SHA384"));
1206 ASSERT_TRUE(SSL_CTX_set_cipher_list(
1207 server_ctx.get(),
1208 "TLS_RSA_WITH_AES_256_CBC_SHA:TLS_RSA_WITH_AES_256_GCM_SHA384"));
1209
1210 bssl::UniquePtr<SSL> client, server;
1211 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1212 server_ctx.get()));
1213
1214 // Modify ciphersuites on the SSL object and introduce mismatch, this modifies
1215 // ssl->config
1216 ASSERT_TRUE(
1217 SSL_set_cipher_list(client.get(), "TLS_RSA_WITH_AES_256_CBC_SHA"));
1218 ASSERT_TRUE(
1219 SSL_set_cipher_list(server.get(), "TLS_RSA_WITH_AES_256_GCM_SHA384"));
1220
1221 // Handshake should fail as config objects have no shared cipher.
1222 ASSERT_FALSE(CompleteHandshakes(client.get(), server.get()));
1223 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1224
1225 ERR_clear_error();
1226
1227 bssl::UniquePtr<SSL> client2, server2;
1228 ASSERT_TRUE(CreateClientAndServer(&client2, &server2, client_ctx.get(),
1229 server_ctx.get()));
1230
1231 // Modify ciphersuites on the SSL object with a new third cipher, this
1232 // modifies ssl->config
1233 ASSERT_TRUE(
1234 SSL_set_cipher_list(client2.get(), "TLS_RSA_WITH_AES_128_CBC_SHA"));
1235 ASSERT_TRUE(
1236 SSL_set_cipher_list(server2.get(), "TLS_RSA_WITH_AES_128_CBC_SHA"));
1237
1238 ASSERT_TRUE(CompleteHandshakes(client2.get(), server2.get()));
1239 ASSERT_EQ(SSL_CIPHER_get_id(SSL_get_current_cipher(client2.get())),
1240 (uint32_t)TLS1_CK_RSA_WITH_AES_128_SHA);
1241 ASSERT_EQ(SSL_CIPHER_get_id(SSL_get_current_cipher(server2.get())),
1242 (uint32_t)TLS1_CK_RSA_WITH_AES_128_SHA);
1243 }
1244
1245 TEST(SSLTest, TLS12ConfigCtxInteraction) {
1246 // This configures SSL_CTX objects with default TLS 1.2 and 1.3 ciphersuites
1247 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1248 bssl::UniquePtr<SSL_CTX> server_ctx =
1249 CreateContextWithTestCertificate(TLS_method());
1250 ASSERT_TRUE(client_ctx);
1251 ASSERT_TRUE(server_ctx);
1252
1253 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_VERSION));
1254 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_2_VERSION));
1255 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_VERSION));
1256 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION));
1257
1258 // Restrict TLS 1.2 ciphersuites on the SSL_CTX objects
1259 ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(),
1260 "TLS_RSA_WITH_AES_256_CBC_SHA"));
1261 ASSERT_TRUE(SSL_CTX_set_cipher_list(server_ctx.get(),
1262 "TLS_RSA_WITH_AES_256_CBC_SHA"));
1263
1264 bssl::UniquePtr<SSL> client, server;
1265 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1266 server_ctx.get()));
1267
1268 // Modify TLS 1.2 ciphersuite for client's SSL object, but not server
1269 ASSERT_TRUE(
1270 SSL_set_cipher_list(client.get(), "TLS_RSA_WITH_AES_256_GCM_SHA384"));
1271
1272 // Handshake should fail as client SSL config and server CTX objects have no
1273 // shared TLS 1.2 cipher.
1274 ASSERT_FALSE(CompleteHandshakes(client.get(), server.get()));
1275 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1276
1277 ERR_clear_error();
1278
1279 bssl::UniquePtr<SSL> client2, server2;
1280 ASSERT_TRUE(CreateClientAndServer(&client2, &server2, client_ctx.get(),
1281 server_ctx.get()));
1282
1283 // Modify TLS 1.2 ciphersuites for server2 SSL object, but not client
1284 ASSERT_TRUE(
1285 SSL_set_cipher_list(server2.get(), "TLS_RSA_WITH_AES_256_GCM_SHA384"));
1286
1287 // Handshake should fail as server SSL config and client CTX objects have no
1288 // shared TLS 1.2 cipher.
1289 ASSERT_FALSE(CompleteHandshakes(client2.get(), server2.get()));
1290 ASSERT_EQ(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_SHARED_CIPHER);
1291 }
1292
1293 TEST(SSLTest, SSLGetCiphersReturnsTLS13Custom) {
1294 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1295 bssl::UniquePtr<SSL_CTX> server_ctx =
1296 CreateContextWithTestCertificate(TLS_method());
1297 ASSERT_TRUE(client_ctx);
1298 ASSERT_TRUE(server_ctx);
1299
1300 // Configure custom TLS 1.3 Ciphersuites
1301 SSL_CTX_set_ciphersuites(server_ctx.get(), "TLS_AES_128_GCM_SHA256");
1302 SSL_CTX_set_ciphersuites(client_ctx.get(),
1303 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
1304
1305 // Configure only TLS 1.3.
1306 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION));
1307 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1308 ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION));
1309 ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION));
1310
1311 bssl::UniquePtr<SSL> client, server;
1312 // Have to ensure config is not shed per current implementation of
1313 // SSL_get_ciphers.
1314 ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(),
1315 server_ctx.get(), ClientConfig(), false));
1316
1317 ASSERT_TRUE(CompleteHandshakes(client.get(), server.get()));
1318 // Ensure default TLS 1.3 Ciphersuites are present
1319 const SSL_CIPHER *cipher1 =
1320 SSL_get_cipher_by_value(TLS1_3_CK_AES_128_GCM_SHA256 & 0xFFFF);
1321 ASSERT_TRUE(cipher1);
1322 const SSL_CIPHER *cipher2 =
1323 SSL_get_cipher_by_value(TLS1_3_CK_AES_256_GCM_SHA384 & 0xFFFF);
1324 ASSERT_TRUE(cipher2);
1325 const SSL_CIPHER *cipher3 =
1326 SSL_get_cipher_by_value(TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xFFFF);
1327 ASSERT_TRUE(cipher3);
1328
1329 STACK_OF(SSL_CIPHER) *client_ciphers = SSL_get_ciphers(client.get());
1330 STACK_OF(SSL_CIPHER) *server_ciphers = SSL_get_ciphers(server.get());
1331
1332 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher1));
1333 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher2));
1334 ASSERT_FALSE(sk_SSL_CIPHER_find_awslc(client_ciphers, NULL, cipher3));
1335
1336 ASSERT_TRUE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher1));
1337 ASSERT_FALSE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher2));
1338 ASSERT_FALSE(sk_SSL_CIPHER_find_awslc(server_ciphers, NULL, cipher3));
1339 }
1340
1341 TEST(SSLTest, GetClientCiphersAfterHandshakeFailure1_3) {
1342 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1343 bssl::UniquePtr<SSL_CTX> server_ctx =
1344 CreateContextWithTestCertificate(TLS_method());
1345
1346 // configure client to add fake ciphersuite
1347 SSL_CTX_set_grease_enabled(client_ctx.get(), 1);
1348
1349 // There will be no cipher match, handshake will not succeed.
1350 ASSERT_TRUE(SSL_CTX_set_ciphersuites(
1351 client_ctx.get(), "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256"));
1352 ASSERT_TRUE(
1353 SSL_CTX_set_ciphersuites(server_ctx.get(), "TLS_AES_128_GCM_SHA256"));
1354
1355 ASSERT_TRUE(client_ctx);
1356 ASSERT_TRUE(server_ctx);
1357
1358 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION));
1359 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1360
1361 bssl::UniquePtr<SSL> client, server;
1362 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1363 server_ctx.get()));
1364
1365 const unsigned char *tmp = nullptr;
1366 // Handshake not completed, getting ciphers should fail
1367 ASSERT_FALSE(SSL_client_hello_get0_ciphers(client.get(), &tmp));
1368 ASSERT_FALSE(SSL_client_hello_get0_ciphers(server.get(), &tmp));
1369 ASSERT_FALSE(tmp);
1370
1371 // This should fail, but should be able to inspect client ciphers still
1372 ASSERT_FALSE(CompleteHandshakes(client.get(), server.get()));
1373
1374 ASSERT_EQ(SSL_client_hello_get0_ciphers(client.get(), nullptr), (size_t)0);
1375
1376 const unsigned char expected_cipher_bytes[] = {0x13, 0x02, 0x13, 0x03};
1377 const unsigned char *p = nullptr;
1378
1379 // Expected size is 2 bytes more than |expected_cipher_bytes| to account for
1380 // grease value
1381 ASSERT_EQ(SSL_client_hello_get0_ciphers(server.get(), &p),
1382 sizeof(expected_cipher_bytes) + 2);
1383
1384 // Grab the first 2 bytes and check grease value
1385 uint16_t grease_val = CRYPTO_load_u16_be(p);
1386 ASSERT_FALSE(SSL_get_cipher_by_value(grease_val));
1387
1388 // Sanity check for first cipher ID after grease value
1389 uint16_t cipher_val = CRYPTO_load_u16_be(p + 2);
1390 ASSERT_TRUE(SSL_get_cipher_by_value((cipher_val)));
1391
1392 // Check order and validity of the rest of the client cipher suites,
1393 // excluding the grease value (2nd byte onwards)
1394 ASSERT_EQ(Bytes(expected_cipher_bytes, sizeof(expected_cipher_bytes)),
1395 Bytes(p + 2, sizeof(expected_cipher_bytes)));
1396
1397 // Parsed ciphersuite list should only have 2 valid ciphersuites as configured
1398 // (grease value should not be included). Even though the handshake fails,
1399 // client cipher data should be available through the server SSL object.
1400 ASSERT_TRUE(sk_SSL_CIPHER_num(server.get()->client_cipher_suites.get()) == 2);
1401 }
1402
1403 TEST(SSLTest, GetClientCiphers1_3) {
1404 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1405 bssl::UniquePtr<SSL_CTX> server_ctx =
1406 CreateContextWithTestCertificate(TLS_method());
1407
1408 // configure client to add fake ciphersuite
1409 SSL_CTX_set_grease_enabled(client_ctx.get(), 1);
1410
1411 ASSERT_TRUE(SSL_CTX_set_ciphersuites(client_ctx.get(),
1412 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_"
1413 "SHA384:TLS_CHACHA20_POLY1305_SHA256"));
1414
1415 ASSERT_TRUE(client_ctx);
1416 ASSERT_TRUE(server_ctx);
1417 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION));
1418 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION));
1419
1420 bssl::UniquePtr<SSL> client, server;
1421 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1422 server_ctx.get()));
1423
1424 const unsigned char *tmp = nullptr;
1425 // Handshake not completed, getting ciphers should fail
1426 ASSERT_FALSE(SSL_client_hello_get0_ciphers(client.get(), &tmp));
1427 ASSERT_FALSE(SSL_client_hello_get0_ciphers(server.get(), &tmp));
1428 ASSERT_FALSE(tmp);
1429
1430 ASSERT_TRUE(CompleteHandshakes(client.get(), server.get()));
1431
1432 ASSERT_EQ(SSL_client_hello_get0_ciphers(client.get(), nullptr), (size_t)0);
1433
1434 const unsigned char expected_cipher_bytes[] = {0x13, 0x01, 0x13,
1435 0x02, 0x13, 0x03};
1436 const unsigned char *p = nullptr;
1437
1438 // Expected size is 2 bytes more than |expected_cipher_bytes| to account for
1439 // grease value
1440 ASSERT_EQ(SSL_client_hello_get0_ciphers(server.get(), &p),
1441 sizeof(expected_cipher_bytes) + 2);
1442
1443 // Grab the first 2 bytes and check grease value
1444 uint16_t grease_val = CRYPTO_load_u16_be(p);
1445 ASSERT_FALSE(SSL_get_cipher_by_value(grease_val));
1446
1447 // Sanity check for first cipher ID after grease value
1448 uint16_t cipher_val = CRYPTO_load_u16_be(p + 2);
1449 ASSERT_TRUE(SSL_get_cipher_by_value((cipher_val)));
1450
1451 // Check order and validity of the rest of the client cipher suites,
1452 // excluding the grease value (2nd byte onwards)
1453 ASSERT_EQ(Bytes(expected_cipher_bytes, sizeof(expected_cipher_bytes)),
1454 Bytes(p + 2, sizeof(expected_cipher_bytes)));
1455
1456 // Parsed ciphersuite list should only have 3 valid ciphersuites as configured
1457 // (grease value should not be included)
1458 ASSERT_TRUE(sk_SSL_CIPHER_num(server.get()->client_cipher_suites.get()) == 3);
1459 }
1460
1461 TEST(SSLTest, GetClientCiphers1_2) {
1462 bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
1463 bssl::UniquePtr<SSL_CTX> server_ctx =
1464 CreateContextWithTestCertificate(TLS_method());
1465
1466 // configure client to add fake ciphersuite
1467 SSL_CTX_set_grease_enabled(client_ctx.get(), 1);
1468
1469 ASSERT_TRUE(SSL_CTX_set_cipher_list(client_ctx.get(),
1470 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:"
1471 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"));
1472 ASSERT_TRUE(client_ctx);
1473 ASSERT_TRUE(server_ctx);
1474 ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_2_VERSION));
1475 ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_2_VERSION));
1476
1477 bssl::UniquePtr<SSL> client, server;
1478 ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(),
1479 server_ctx.get()));
1480
1481 const unsigned char *tmp = nullptr;
1482 // Handshake not completed, getting ciphers should fail
1483 ASSERT_FALSE(SSL_client_hello_get0_ciphers(client.get(), &tmp));
1484 ASSERT_FALSE(SSL_client_hello_get0_ciphers(server.get(), &tmp));
1485 ASSERT_FALSE(tmp);
1486
1487 ASSERT_TRUE(CompleteHandshakes(client.get(), server.get()));
1488
1489 ASSERT_EQ(SSL_client_hello_get0_ciphers(client.get(), nullptr), (size_t)0);
1490
1491 const unsigned char expected_cipher_bytes[] = {0xC0, 0x2C, 0xC0, 0x13};
1492 const unsigned char *p = nullptr;
1493
1494 // Expected size is 2 bytes more than |expected_cipher_bytes| to account for
1495 // grease value
1496 ASSERT_EQ(SSL_client_hello_get0_ciphers(server.get(), &p),
1497 sizeof(expected_cipher_bytes) + 2);
1498
1499 // Grab the first 2 bytes and check grease value
1500 uint16_t grease_val = CRYPTO_load_u16_be(p);
1501 ASSERT_FALSE(SSL_get_cipher_by_value(grease_val));
1502
1503 // Sanity check for first cipher ID after grease value
1504 uint16_t cipher_val = CRYPTO_load_u16_be(p + 2);
1505 ASSERT_TRUE(SSL_get_cipher_by_value((cipher_val)));
1506
1507 // Check order and validity of the rest of the client cipher suites,
1508 // excluding the grease value (2nd byte onwards)
1509 ASSERT_EQ(Bytes(expected_cipher_bytes, sizeof(expected_cipher_bytes)),
1510 Bytes(p + 2, sizeof(expected_cipher_bytes)));
1511
1512 // Parsed ciphersuite list should only have 2 valid ciphersuites as configured
1513 // (grease value should not be included)
1514 ASSERT_TRUE(sk_SSL_CIPHER_num(server.get()->client_cipher_suites.get()) == 2);
1515 }
1516
1517
1518 TEST(SSLTest, DISABLED_ApplyHandoffRemovesUnsupportedCiphers) {
1519 bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method()));
1520 ASSERT_TRUE(server_ctx);
1521 bssl::UniquePtr<SSL> server(SSL_new(server_ctx.get()));
1522 ASSERT_TRUE(server);
1523
1524 // handoff is a handoff message that has been artificially modified to pretend
1525 // that only cipher 0x0A is supported. When it is applied to |server|, all
1526 // ciphers but that one should be removed.
1527 //
1528 // To make a new one of these, try sticking this in the |Handoff| test above:
1529 //
1530 // hexdump(stderr, "", handoff.data(), handoff.size());
1531 // sed -e 's/\(..\)/0x\1, /g'
1532 //
1533 // and modify serialize_features() to emit only cipher 0x0A.
1534
1535 uint8_t handoff[] = {
1536 0x30, 0x81, 0x9a, 0x02, 0x01, 0x00, 0x04, 0x00, 0x04, 0x81, 0x82, 0x01,
1537 0x00, 0x00, 0x7e, 0x03, 0x03, 0x30, 0x8e, 0x8f, 0x79, 0xd2, 0x87, 0x39,
1538 0xc2, 0x23, 0x23, 0x13, 0xca, 0x3c, 0x80, 0x44, 0xfd, 0x80, 0x83, 0x62,
1539 0x3c, 0xcc, 0xf8, 0x76, 0xd3, 0x62, 0xbb, 0x54, 0xe3, 0xc4, 0x39, 0x24,
1540 0xa5, 0x00, 0x00, 0x1e, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
1541 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14,
1542 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
1543 0x00, 0x37, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00,
1544 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00,
1545 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00,
1546 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08,
1547 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x04, 0x02, 0x00,
1548 0x0a, 0x04, 0x0a, 0x00, 0x15, 0x00, 0x17, 0x00, 0x18, 0x00, 0x19, 0x00,
1549 0x1d,
1550 };
1551
1552 EXPECT_EQ(22u, sk_SSL_CIPHER_num(SSL_get_ciphers(server.get())));
1553 ASSERT_TRUE(
1554 SSL_apply_handoff(server.get(), {handoff, OPENSSL_ARRAY_SIZE(handoff)}));
1555 EXPECT_EQ(1u, sk_SSL_CIPHER_num(SSL_get_ciphers(server.get())));
1556 }
1557
1558
1559 TEST(SSLTest, ApplyHandoffRemovesUnsupportedCurves) {
1560 bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method()));
1561 ASSERT_TRUE(server_ctx);
1562 bssl::UniquePtr<SSL> server(SSL_new(server_ctx.get()));
1563 ASSERT_TRUE(server);
1564
1565 // handoff is a handoff message that has been artificially modified to pretend
1566 // that only one ECDH group is supported. When it is applied to |server|, all
1567 // groups but that one should be removed.
1568 //
1569 // See |ApplyHandoffRemovesUnsupportedCiphers| for how to make a new one of
1570 // these.
1571 uint8_t handoff[] = {
1572 0x30, 0x81, 0xc0, 0x02, 0x01, 0x00, 0x04, 0x00, 0x04, 0x81, 0x82, 0x01,
1573 0x00, 0x00, 0x7e, 0x03, 0x03, 0x98, 0x30, 0xce, 0xd9, 0xb0, 0xdf, 0x5f,
1574 0x82, 0x05, 0x4a, 0x43, 0x67, 0x7e, 0xdb, 0x6a, 0x4f, 0x21, 0x18, 0x4e,
1575 0x0d, 0x94, 0x63, 0x18, 0x8b, 0x54, 0x89, 0xdb, 0x8b, 0x1d, 0x84, 0xbc,
1576 0x09, 0x00, 0x00, 0x1e, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
1577 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14,
1578 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00,
1579 0x00, 0x37, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00,
1580 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00,
1581 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00,
1582 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08,
1583 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x04, 0x30, 0x00,
1584 0x02, 0x00, 0x0a, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x8c, 0x00, 0x8d, 0x00,
1585 0x9c, 0x00, 0x9d, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x09, 0xc0,
1586 0x0a, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x2b, 0xc0, 0x2c, 0xc0, 0x2f, 0xc0,
1587 0x30, 0xc0, 0x35, 0xc0, 0x36, 0xcc, 0xa8, 0xcc, 0xa9, 0xcc, 0xac, 0x04,
1588 0x02, 0x00, 0x17,
1589 };
1590
1591 // The zero length means that the default list of groups is used.
1592 EXPECT_EQ(0u, server->config->supported_group_list.size());
1593 ASSERT_TRUE(
1594 SSL_apply_handoff(server.get(), {handoff, OPENSSL_ARRAY_SIZE(handoff)}));
1595 EXPECT_EQ(1u, server->config->supported_group_list.size());
1596 }
1597
1598 BSSL_NAMESPACE_END