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 RSA, 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"
323 "secp256r1 (P-256)" },
325 "secp384r1 (P-384)" },
327 "secp521r1 (P-521)" },
328 { BR_EC_brainpoolP256r1
,
330 { BR_EC_brainpoolP384r1
,
332 { BR_EC_brainpoolP512r1
,
343 get_curve_name(int id
)
347 for (u
= 0; curves
[u
].name
; u
++) {
348 if (curves
[u
].id
== id
) {
349 return curves
[u
].name
;
357 get_curve_name_ext(int id
, char *dst
, size_t len
)
363 name
= get_curve_name(id
);
365 sprintf(tmp
, "unknown (%d)", id
);
368 n
= 1 + strlen(name
);
375 memcpy(dst
, name
, n
);
381 get_suite_name(unsigned suite
)
385 for (u
= 0; cipher_suites
[u
].name
; u
++) {
386 if (cipher_suites
[u
].suite
== suite
) {
387 return cipher_suites
[u
].name
;
395 get_suite_name_ext(unsigned suite
, char *dst
, size_t len
)
401 name
= get_suite_name(suite
);
403 sprintf(tmp
, "unknown (0x%04X)", suite
);
406 n
= 1 + strlen(name
);
413 memcpy(dst
, name
, n
);
419 uses_ecdhe(unsigned suite
)
423 for (u
= 0; cipher_suites
[u
].name
; u
++) {
424 if (cipher_suites
[u
].suite
== suite
) {
425 return (cipher_suites
[u
].req
426 & (REQ_ECDHE_RSA
| REQ_ECDHE_ECDSA
)) != 0;
438 printf("Protocol versions:\n");
439 for (u
= 0; protocol_versions
[u
].name
; u
++) {
441 protocol_versions
[u
].name
,
442 protocol_versions
[u
].comment
);
444 printf("Hash functions:\n");
445 for (u
= 0; hash_functions
[u
].name
; u
++) {
447 hash_functions
[u
].name
,
448 hash_functions
[u
].comment
);
450 printf("Cipher suites:\n");
451 for (u
= 0; cipher_suites
[u
].name
; u
++) {
453 cipher_suites
[u
].name
,
454 cipher_suites
[u
].comment
);
464 if (c
<= 32 || c
== '-' || c
== '_' || c
== '.'
465 || c
== '/' || c
== '+' || c
== ':')
473 * Get next non-ignored character, normalised:
474 * ASCII letters are converted to lowercase
475 * control characters, space, '-', '_', '.', '/', '+' and ':' are ignored
476 * A terminating zero is returned as 0.
479 next_char(const char **ps
, const char *limit
)
491 if (c
>= 'A' && c
<= 'Z') {
501 * Partial string equality comparison, with normalisation.
504 eqstr_chunk(const char *s1
, size_t s1_len
, const char *s2
, size_t s2_len
)
506 const char *lim1
, *lim2
;
513 c1
= next_char(&s1
, lim1
);
514 c2
= next_char(&s2
, lim2
);
526 eqstr(const char *s1
, const char *s2
)
528 return eqstr_chunk(s1
, strlen(s1
), s2
, strlen(s2
));
534 if (c
>= '0' && c
<= '9') {
536 } else if (c
>= 'A' && c
<= 'F') {
538 } else if (c
>= 'a' && c
<= 'f') {
547 parse_size(const char *s
)
554 if (t
[0] == '0' && (t
[1] == 'x' || t
[1] == 'X')) {
570 if (d
< 0 || d
>= radix
) {
571 fprintf(stderr
, "ERROR: not a valid digit: '%c'\n", c
);
574 z
= acc
* (size_t)radix
+ (size_t)d
;
575 if (z
< (size_t)d
|| (z
/ (size_t)radix
) != acc
578 fprintf(stderr
, "ERROR: value too large: %s\n", s
);
586 * Comma-separated list enumeration. This returns a pointer to the first
587 * word in the string, skipping leading ignored characters. '*len' is
588 * set to the word length (not counting trailing ignored characters).
589 * '*str' is updated to point to immediately after the next comma, or to
590 * the terminating zero, whichever comes first.
592 * Empty words are skipped. If there is no next non-empty word, then this
593 * function returns NULL and sets *len to 0.
596 next_word(const char **str
, size_t *len
)
603 * Find next non-ignored character which is not a comma.
611 if (!is_ign(c
) && c
!= ',') {
618 * Find next comma or terminator.
623 if (c
== 0 || c
== ',') {
630 * Remove trailing ignored characters.
632 u
= (size_t)(*str
- begin
);
633 while (u
> 0 && is_ign(begin
[u
- 1])) {
645 parse_version(const char *name
, size_t len
)
652 ref
= protocol_versions
[u
].name
;
654 fprintf(stderr
, "ERROR: unrecognised protocol"
655 " version name: '%s'\n", name
);
658 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
659 return protocol_versions
[u
].version
;
666 parse_hash_functions(const char *arg
)
676 name
= next_word(&arg
, &len
);
683 ref
= hash_functions
[u
].name
;
685 fprintf(stderr
, "ERROR: unrecognised"
686 " hash function name: '");
687 fwrite(name
, 1, len
, stderr
);
688 fprintf(stderr
, "'\n");
691 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
694 id
= (hash_functions
[u
].hclass
->desc
695 >> BR_HASHDESC_ID_OFF
)
696 & BR_HASHDESC_ID_MASK
;
697 r
|= (unsigned)1 << id
;
703 fprintf(stderr
, "ERROR: no hash function name provided\n");
710 parse_suites(const char *arg
, size_t *num
)
712 VECTOR(cipher_suite
) suites
= VEC_INIT
;
719 name
= next_word(&arg
, &len
);
726 ref
= cipher_suites
[u
].name
;
728 fprintf(stderr
, "ERROR: unrecognised"
730 fwrite(name
, 1, len
, stderr
);
731 fprintf(stderr
, "'\n");
734 if (eqstr_chunk(ref
, strlen(ref
), name
, len
)) {
735 VEC_ADD(suites
, cipher_suites
[u
]);
740 if (VEC_LEN(suites
) == 0) {
741 fprintf(stderr
, "ERROR: no cipher suite provided\n");
743 r
= VEC_TOARRAY(suites
);
744 *num
= VEC_LEN(suites
);
751 ec_curve_name(int curve
)
754 case BR_EC_sect163k1
: return "sect163k1";
755 case BR_EC_sect163r1
: return "sect163r1";
756 case BR_EC_sect163r2
: return "sect163r2";
757 case BR_EC_sect193r1
: return "sect193r1";
758 case BR_EC_sect193r2
: return "sect193r2";
759 case BR_EC_sect233k1
: return "sect233k1";
760 case BR_EC_sect233r1
: return "sect233r1";
761 case BR_EC_sect239k1
: return "sect239k1";
762 case BR_EC_sect283k1
: return "sect283k1";
763 case BR_EC_sect283r1
: return "sect283r1";
764 case BR_EC_sect409k1
: return "sect409k1";
765 case BR_EC_sect409r1
: return "sect409r1";
766 case BR_EC_sect571k1
: return "sect571k1";
767 case BR_EC_sect571r1
: return "sect571r1";
768 case BR_EC_secp160k1
: return "secp160k1";
769 case BR_EC_secp160r1
: return "secp160r1";
770 case BR_EC_secp160r2
: return "secp160r2";
771 case BR_EC_secp192k1
: return "secp192k1";
772 case BR_EC_secp192r1
: return "secp192r1";
773 case BR_EC_secp224k1
: return "secp224k1";
774 case BR_EC_secp224r1
: return "secp224r1";
775 case BR_EC_secp256k1
: return "secp256k1";
776 case BR_EC_secp256r1
: return "secp256r1";
777 case BR_EC_secp384r1
: return "secp384r1";
778 case BR_EC_secp521r1
: return "secp521r1";
779 case BR_EC_brainpoolP256r1
: return "brainpoolP256r1";
780 case BR_EC_brainpoolP384r1
: return "brainpoolP384r1";
781 case BR_EC_brainpoolP512r1
: return "brainpoolP512r1";
789 hash_function_name(int id
)
792 case br_md5sha1_ID
: return "MD5+SHA-1";
793 case br_md5_ID
: return "MD5";
794 case br_sha1_ID
: return "SHA-1";
795 case br_sha224_ID
: return "SHA-224";
796 case br_sha256_ID
: return "SHA-256";
797 case br_sha384_ID
: return "SHA-384";
798 case br_sha512_ID
: return "SHA-512";