2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 const protocol_version protocol_versions
[] = {
30 { "tls10", BR_TLS10
, "TLS 1.0" },
31 { "tls11", BR_TLS11
, "TLS 1.1" },
32 { "tls12", BR_TLS12
, "TLS 1.2" },
37 const hash_function hash_functions
[] = {
38 { "md5", &br_md5_vtable
, "MD5" },
39 { "sha1", &br_sha1_vtable
, "SHA-1" },
40 { "sha224", &br_sha224_vtable
, "SHA-224" },
41 { "sha256", &br_sha256_vtable
, "SHA-256" },
42 { "sha384", &br_sha384_vtable
, "SHA-384" },
43 { "sha512", &br_sha512_vtable
, "SHA-512" },
48 const cipher_suite cipher_suites
[] = {
50 "ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
51 BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
,
52 REQ_ECDHE_ECDSA
| REQ_CHAPOL
| REQ_SHA256
| REQ_TLS12
,
53 "ECDHE with ECDSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
56 "ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
57 BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
,
58 REQ_ECDHE_RSA
| REQ_CHAPOL
| REQ_SHA256
| REQ_TLS12
,
59 "ECDHE with ECDSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
62 "ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
63 BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
,
64 REQ_ECDHE_ECDSA
| REQ_AESGCM
| REQ_SHA256
| REQ_TLS12
,
65 "ECDHE with ECDSA, AES-128/GCM encryption (TLS 1.2+)"
68 "ECDHE_RSA_WITH_AES_128_GCM_SHA256",
69 BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,
70 REQ_ECDHE_RSA
| REQ_AESGCM
| REQ_SHA256
| REQ_TLS12
,
71 "ECDHE with RSA, AES-128/GCM encryption (TLS 1.2+)"
74 "ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
75 BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
,
76 REQ_ECDHE_ECDSA
| REQ_AESGCM
| REQ_SHA384
| REQ_TLS12
,
77 "ECDHE with ECDSA, AES-256/GCM encryption (TLS 1.2+)"
80 "ECDHE_RSA_WITH_AES_256_GCM_SHA384",
81 BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
,
82 REQ_ECDHE_RSA
| REQ_AESGCM
| REQ_SHA384
| REQ_TLS12
,
83 "ECDHE with RSA, AES-256/GCM encryption (TLS 1.2+)"
86 "ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
87 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
,
88 REQ_ECDHE_ECDSA
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
89 "ECDHE with ECDSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
92 "ECDHE_RSA_WITH_AES_128_CBC_SHA256",
93 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
,
94 REQ_ECDHE_RSA
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
95 "ECDHE with RSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
98 "ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
99 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
,
100 REQ_ECDHE_ECDSA
| REQ_AESCBC
| REQ_SHA384
| REQ_TLS12
,
101 "ECDHE with ECDSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
104 "ECDHE_RSA_WITH_AES_256_CBC_SHA384",
105 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
,
106 REQ_ECDHE_RSA
| REQ_AESCBC
| REQ_SHA384
| REQ_TLS12
,
107 "ECDHE with RSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
110 "ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
111 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
,
112 REQ_ECDHE_ECDSA
| REQ_AESCBC
| REQ_SHA1
,
113 "ECDHE with ECDSA, AES-128/CBC + SHA-1"
116 "ECDHE_RSA_WITH_AES_128_CBC_SHA",
117 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
,
118 REQ_ECDHE_RSA
| REQ_AESCBC
| REQ_SHA1
,
119 "ECDHE with RSA, AES-128/CBC + SHA-1"
122 "ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
123 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
,
124 REQ_ECDHE_ECDSA
| REQ_AESCBC
| REQ_SHA1
,
125 "ECDHE with ECDSA, AES-256/CBC + SHA-1"
128 "ECDHE_RSA_WITH_AES_256_CBC_SHA",
129 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
,
130 REQ_ECDHE_RSA
| REQ_AESCBC
| REQ_SHA1
,
131 "ECDHE with RSA, AES-256/CBC + SHA-1"
134 "ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
135 BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
,
136 REQ_ECDH
| REQ_AESGCM
| REQ_SHA256
| REQ_TLS12
,
137 "ECDH key exchange (EC cert), AES-128/GCM (TLS 1.2+)"
140 "ECDH_RSA_WITH_AES_128_GCM_SHA256",
141 BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
,
142 REQ_ECDH
| REQ_AESGCM
| REQ_SHA256
| REQ_TLS12
,
143 "ECDH key exchange (RSA cert), AES-128/GCM (TLS 1.2+)"
146 "ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
147 BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
,
148 REQ_ECDH
| REQ_AESGCM
| REQ_SHA384
| REQ_TLS12
,
149 "ECDH key exchange (EC cert), AES-256/GCM (TLS 1.2+)"
152 "ECDH_RSA_WITH_AES_256_GCM_SHA384",
153 BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
,
154 REQ_ECDH
| REQ_AESGCM
| REQ_SHA384
| REQ_TLS12
,
155 "ECDH key exchange (RSA cert), AES-256/GCM (TLS 1.2+)"
158 "ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
159 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
,
160 REQ_ECDH
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
161 "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
164 "ECDH_RSA_WITH_AES_128_CBC_SHA256",
165 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
,
166 REQ_ECDH
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
167 "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
170 "ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
171 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
,
172 REQ_ECDH
| REQ_AESCBC
| REQ_SHA384
| REQ_TLS12
,
173 "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
176 "ECDH_RSA_WITH_AES_256_CBC_SHA384",
177 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
,
178 REQ_ECDH
| REQ_AESCBC
| REQ_SHA384
| REQ_TLS12
,
179 "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
182 "ECDH_ECDSA_WITH_AES_128_CBC_SHA",
183 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
,
184 REQ_ECDH
| REQ_AESCBC
| REQ_SHA1
,
185 "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-1"
188 "ECDH_RSA_WITH_AES_128_CBC_SHA",
189 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
,
190 REQ_ECDH
| REQ_AESCBC
| REQ_SHA1
,
191 "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-1"
194 "ECDH_ECDSA_WITH_AES_256_CBC_SHA",
195 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
,
196 REQ_ECDH
| REQ_AESCBC
| REQ_SHA1
,
197 "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-1"
200 "ECDH_RSA_WITH_AES_256_CBC_SHA",
201 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
,
202 REQ_ECDH
| REQ_AESCBC
| REQ_SHA1
,
203 "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-1"
206 "RSA_WITH_AES_128_GCM_SHA256",
207 BR_TLS_RSA_WITH_AES_128_GCM_SHA256
,
208 REQ_RSAKEYX
| REQ_AESGCM
| REQ_SHA256
| REQ_TLS12
,
209 "RSA key exchange, AES-128/GCM encryption (TLS 1.2+)"
212 "RSA_WITH_AES_256_GCM_SHA384",
213 BR_TLS_RSA_WITH_AES_256_GCM_SHA384
,
214 REQ_RSAKEYX
| REQ_AESGCM
| REQ_SHA384
| REQ_TLS12
,
215 "RSA key exchange, AES-256/GCM encryption (TLS 1.2+)"
218 "RSA_WITH_AES_128_CBC_SHA256",
219 BR_TLS_RSA_WITH_AES_128_CBC_SHA256
,
220 REQ_RSAKEYX
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
221 "RSA key exchange, AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
224 "RSA_WITH_AES_256_CBC_SHA256",
225 BR_TLS_RSA_WITH_AES_256_CBC_SHA256
,
226 REQ_RSAKEYX
| REQ_AESCBC
| REQ_SHA256
| REQ_TLS12
,
227 "RSA key exchange, AES-256/CBC + HMAC/SHA-256 (TLS 1.2+)"
230 "RSA_WITH_AES_128_CBC_SHA",
231 BR_TLS_RSA_WITH_AES_128_CBC_SHA
,
232 REQ_RSAKEYX
| REQ_AESCBC
| REQ_SHA1
,
233 "RSA key exchange, AES-128/CBC + HMAC/SHA-1"
236 "RSA_WITH_AES_256_CBC_SHA",
237 BR_TLS_RSA_WITH_AES_256_CBC_SHA
,
238 REQ_RSAKEYX
| REQ_AESCBC
| REQ_SHA1
,
239 "RSA key exchange, AES-256/CBC + HMAC/SHA-1"
242 "ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
243 BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
,
244 REQ_ECDHE_ECDSA
| REQ_3DESCBC
| REQ_SHA1
,
245 "ECDHE with ECDSA, 3DES/CBC + SHA-1"
248 "ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
249 BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
,
250 REQ_ECDHE_RSA
| REQ_3DESCBC
| REQ_SHA1
,
251 "ECDHE with RSA, 3DES/CBC + SHA-1"
254 "ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",
255 BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
,
256 REQ_ECDH
| REQ_3DESCBC
| REQ_SHA1
,
257 "ECDH key exchange (EC cert), 3DES/CBC + HMAC/SHA-1"
260 "ECDH_RSA_WITH_3DES_EDE_CBC_SHA",
261 BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
,
262 REQ_ECDH
| REQ_3DESCBC
| REQ_SHA1
,
263 "ECDH key exchange (RSA cert), 3DES/CBC + HMAC/SHA-1"
266 "RSA_WITH_3DES_EDE_CBC_SHA",
267 BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA
,
268 REQ_RSAKEYX
| REQ_3DESCBC
| REQ_SHA1
,
269 "RSA key exchange, 3DES/CBC + HMAC/SHA-1"
276 get_suite_name(unsigned suite
)
280 for (u
= 0; cipher_suites
[u
].name
; u
++) {
281 if (cipher_suites
[u
].suite
== suite
) {
282 return cipher_suites
[u
].name
;
290 get_suite_name_ext(unsigned suite
, char *dst
, size_t len
)
296 name
= get_suite_name(suite
);
298 sprintf(tmp
, "unknown (0x%04X)", suite
);
301 n
= 1 + strlen(name
);
308 memcpy(dst
, name
, n
);
318 printf("Protocol versions:\n");
319 for (u
= 0; protocol_versions
[u
].name
; u
++) {
321 protocol_versions
[u
].name
,
322 protocol_versions
[u
].comment
);
324 printf("Hash functions:\n");
325 for (u
= 0; hash_functions
[u
].name
; u
++) {
327 hash_functions
[u
].name
,
328 hash_functions
[u
].comment
);
330 printf("Cipher suites:\n");
331 for (u
= 0; cipher_suites
[u
].name
; u
++) {
333 cipher_suites
[u
].name
,
334 cipher_suites
[u
].comment
);
344 if (c
<= 32 || c
== '-' || c
== '_' || c
== '.'
345 || c
== '/' || c
== '+' || c
== ':')
353 * Get next non-ignored character, normalised:
354 * ASCII letters are converted to lowercase
355 * control characters, space, '-', '_', '.', '/', '+' and ':' are ignored
356 * A terminating zero is returned as 0.
359 next_char(const char **ps
, const char *limit
)
371 if (c
>= 'A' && c
<= 'Z') {
381 * Partial string equality comparison, with normalisation.
384 eqstr_chunk(const char *s1
, size_t s1_len
, const char *s2
, size_t s2_len
)
386 const char *lim1
, *lim2
;
393 c1
= next_char(&s1
, lim1
);
394 c2
= next_char(&s2
, lim2
);
406 eqstr(const char *s1
, const char *s2
)
408 return eqstr_chunk(s1
, strlen(s1
), s2
, strlen(s2
));
414 if (c
>= '0' && c
<= '9') {
416 } else if (c
>= 'A' && c
<= 'F') {
418 } else if (c
>= 'a' && c
<= 'f') {
427 parse_size(const char *s
)
434 if (t
[0] == '0' && (t
[1] == 'x' || t
[1] == 'X')) {
450 if (d
< 0 || d
>= radix
) {
451 fprintf(stderr
, "ERROR: not a valid digit: '%c'\n", c
);
454 z
= acc
* (size_t)radix
+ (size_t)d
;
455 if (z
< (size_t)d
|| (z
/ (size_t)radix
) != acc
458 fprintf(stderr
, "ERROR: value too large: %s\n", s
);
466 * Comma-separated list enumeration. This returns a pointer to the first
467 * word in the string, skipping leading ignored characters. '*len' is
468 * set to the word length (not counting trailing ignored characters).
469 * '*str' is updated to point to immediately after the next comma, or to
470 * the terminating zero, whichever comes first.
472 * Empty words are skipped. If there is no next non-empty word, then this
473 * function returns NULL and sets *len to 0.
476 next_word(const char **str
, size_t *len
)
483 * Find next non-ignored character which is not a comma.
491 if (!is_ign(c
) && c
!= ',') {
498 * Find next comma or terminator.
503 if (c
== 0 || c
== ',') {
510 * Remove trailing ignored characters.
512 u
= (size_t)(*str
- begin
);
513 while (u
> 0 && is_ign(begin
[u
- 1])) {
525 parse_version(const char *name
, size_t len
)
532 ref
= protocol_versions
[u
].name
;
534 fprintf(stderr
, "ERROR: unrecognised protocol"
535 " version name: '%s'\n", name
);
538 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
539 return protocol_versions
[u
].version
;
546 parse_hash_functions(const char *arg
)
556 name
= next_word(&arg
, &len
);
563 ref
= hash_functions
[u
].name
;
565 fprintf(stderr
, "ERROR: unrecognised"
566 " hash function name: '");
567 fwrite(name
, 1, len
, stderr
);
568 fprintf(stderr
, "'\n");
571 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
574 id
= (hash_functions
[u
].hclass
->desc
575 >> BR_HASHDESC_ID_OFF
)
576 & BR_HASHDESC_ID_MASK
;
577 r
|= (unsigned)1 << id
;
583 fprintf(stderr
, "ERROR: no hash function name provided\n");
590 parse_suites(const char *arg
, size_t *num
)
592 VECTOR(cipher_suite
) suites
= VEC_INIT
;
599 name
= next_word(&arg
, &len
);
606 ref
= cipher_suites
[u
].name
;
608 fprintf(stderr
, "ERROR: unrecognised"
610 fwrite(name
, 1, len
, stderr
);
611 fprintf(stderr
, "'\n");
614 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
615 VEC_ADD(suites
, cipher_suites
[u
]);
620 if (VEC_LEN(suites
) == 0) {
621 fprintf(stderr
, "ERROR: no cipher suite provided\n");
623 r
= VEC_TOARRAY(suites
);
624 *num
= VEC_LEN(suites
);
631 ec_curve_name(int curve
)
634 case BR_EC_sect163k1
: return "sect163k1";
635 case BR_EC_sect163r1
: return "sect163r1";
636 case BR_EC_sect163r2
: return "sect163r2";
637 case BR_EC_sect193r1
: return "sect193r1";
638 case BR_EC_sect193r2
: return "sect193r2";
639 case BR_EC_sect233k1
: return "sect233k1";
640 case BR_EC_sect233r1
: return "sect233r1";
641 case BR_EC_sect239k1
: return "sect239k1";
642 case BR_EC_sect283k1
: return "sect283k1";
643 case BR_EC_sect283r1
: return "sect283r1";
644 case BR_EC_sect409k1
: return "sect409k1";
645 case BR_EC_sect409r1
: return "sect409r1";
646 case BR_EC_sect571k1
: return "sect571k1";
647 case BR_EC_sect571r1
: return "sect571r1";
648 case BR_EC_secp160k1
: return "secp160k1";
649 case BR_EC_secp160r1
: return "secp160r1";
650 case BR_EC_secp160r2
: return "secp160r2";
651 case BR_EC_secp192k1
: return "secp192k1";
652 case BR_EC_secp192r1
: return "secp192r1";
653 case BR_EC_secp224k1
: return "secp224k1";
654 case BR_EC_secp224r1
: return "secp224r1";
655 case BR_EC_secp256k1
: return "secp256k1";
656 case BR_EC_secp256r1
: return "secp256r1";
657 case BR_EC_secp384r1
: return "secp384r1";
658 case BR_EC_secp521r1
: return "secp521r1";
659 case BR_EC_brainpoolP256r1
: return "brainpoolP256r1";
660 case BR_EC_brainpoolP384r1
: return "brainpoolP384r1";
661 case BR_EC_brainpoolP512r1
: return "brainpoolP512r1";
669 hash_function_name(int id
)
672 case br_md5sha1_ID
: return "MD5+SHA-1";
673 case br_md5_ID
: return "MD5";
674 case br_sha1_ID
: return "SHA-1";
675 case br_sha224_ID
: return "SHA-224";
676 case br_sha256_ID
: return "SHA-256";
677 case br_sha384_ID
: return "SHA-384";
678 case br_sha512_ID
: return "SHA-512";