]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
f1fd4544 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
f1fd4544 | 8 | */ |
58964a49 | 9 | |
a76ce286 P |
10 | /* We need access to the deprecated low level HMAC APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
58964a49 | 13 | #include <stdio.h> |
aa474d1f | 14 | #include <stdlib.h> |
ec577822 | 15 | #include <openssl/objects.h> |
6434abbf DSH |
16 | #include <openssl/evp.h> |
17 | #include <openssl/hmac.h> | |
a76ce286 | 18 | #include <openssl/core_names.h> |
67c8e7f4 | 19 | #include <openssl/ocsp.h> |
5951e840 MC |
20 | #include <openssl/conf.h> |
21 | #include <openssl/x509v3.h> | |
3c27208f RS |
22 | #include <openssl/dh.h> |
23 | #include <openssl/bn.h> | |
677963e5 | 24 | #include "internal/nelem.h" |
c2041da8 | 25 | #include "internal/evp.h" |
706457b7 | 26 | #include "ssl_local.h" |
3c27208f | 27 | #include <openssl/ct.h> |
58964a49 | 28 | |
852c2ed2 RS |
29 | DEFINE_STACK_OF_CONST(SSL_CIPHER) |
30 | DEFINE_STACK_OF(X509) | |
31 | DEFINE_STACK_OF(X509_NAME) | |
32 | ||
5235ef44 | 33 | static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey); |
b0031e5d | 34 | static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu); |
5235ef44 | 35 | |
0f113f3e MC |
36 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
37 | tls1_enc, | |
38 | tls1_mac, | |
39 | tls1_setup_key_block, | |
40 | tls1_generate_master_secret, | |
41 | tls1_change_cipher_state, | |
42 | tls1_final_finish_mac, | |
0f113f3e MC |
43 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
44 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
45 | tls1_alert_code, | |
46 | tls1_export_keying_material, | |
47 | 0, | |
a29fa98c | 48 | ssl3_set_handshake_header, |
2c7b4dbc | 49 | tls_close_construct_packet, |
0f113f3e MC |
50 | ssl3_handshake_write |
51 | }; | |
52 | ||
53 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | |
54 | tls1_enc, | |
55 | tls1_mac, | |
56 | tls1_setup_key_block, | |
57 | tls1_generate_master_secret, | |
58 | tls1_change_cipher_state, | |
59 | tls1_final_finish_mac, | |
0f113f3e MC |
60 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
61 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
62 | tls1_alert_code, | |
63 | tls1_export_keying_material, | |
64 | SSL_ENC_FLAG_EXPLICIT_IV, | |
a29fa98c | 65 | ssl3_set_handshake_header, |
2c7b4dbc | 66 | tls_close_construct_packet, |
0f113f3e MC |
67 | ssl3_handshake_write |
68 | }; | |
69 | ||
70 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | |
71 | tls1_enc, | |
72 | tls1_mac, | |
73 | tls1_setup_key_block, | |
74 | tls1_generate_master_secret, | |
75 | tls1_change_cipher_state, | |
76 | tls1_final_finish_mac, | |
0f113f3e MC |
77 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
78 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
79 | tls1_alert_code, | |
80 | tls1_export_keying_material, | |
81 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
82 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
a29fa98c | 83 | ssl3_set_handshake_header, |
2c7b4dbc | 84 | tls_close_construct_packet, |
0f113f3e MC |
85 | ssl3_handshake_write |
86 | }; | |
58964a49 | 87 | |
582a17d6 | 88 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
bebc0c7d | 89 | tls13_enc, |
582a17d6 | 90 | tls1_mac, |
92760c21 MC |
91 | tls13_setup_key_block, |
92 | tls13_generate_master_secret, | |
93 | tls13_change_cipher_state, | |
94 | tls13_final_finish_mac, | |
582a17d6 MC |
95 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
96 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
04904312 | 97 | tls13_alert_code, |
0ca8d1ec | 98 | tls13_export_keying_material, |
bebc0c7d | 99 | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
582a17d6 MC |
100 | ssl3_set_handshake_header, |
101 | tls_close_construct_packet, | |
102 | ssl3_handshake_write | |
103 | }; | |
104 | ||
f3b656b2 | 105 | long tls1_default_timeout(void) |
0f113f3e MC |
106 | { |
107 | /* | |
108 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for | |
109 | * http, the cache would over fill | |
110 | */ | |
111 | return (60 * 60 * 2); | |
112 | } | |
58964a49 | 113 | |
6b691a5c | 114 | int tls1_new(SSL *s) |
0f113f3e MC |
115 | { |
116 | if (!ssl3_new(s)) | |
b77f3ed1 MC |
117 | return 0; |
118 | if (!s->method->ssl_clear(s)) | |
119 | return 0; | |
120 | ||
121 | return 1; | |
0f113f3e | 122 | } |
58964a49 | 123 | |
6b691a5c | 124 | void tls1_free(SSL *s) |
0f113f3e | 125 | { |
aff8c126 | 126 | OPENSSL_free(s->ext.session_ticket); |
0f113f3e MC |
127 | ssl3_free(s); |
128 | } | |
58964a49 | 129 | |
b77f3ed1 | 130 | int tls1_clear(SSL *s) |
0f113f3e | 131 | { |
b77f3ed1 MC |
132 | if (!ssl3_clear(s)) |
133 | return 0; | |
134 | ||
4fa52141 | 135 | if (s->method->version == TLS_ANY_VERSION) |
5c587fb6 | 136 | s->version = TLS_MAX_VERSION_INTERNAL; |
4fa52141 VD |
137 | else |
138 | s->version = s->method->version; | |
b77f3ed1 MC |
139 | |
140 | return 1; | |
0f113f3e | 141 | } |
58964a49 | 142 | |
2dc1aeed | 143 | /* |
dbc6268f | 144 | * Table of group information. |
2dc1aeed | 145 | */ |
65dc5c3c | 146 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) |
0e464d9d | 147 | static const TLS_GROUP_INFO nid_list[] = { |
65dc5c3c | 148 | # ifndef OPENSSL_NO_EC |
c0bfc473 MC |
149 | {NID_sect163k1, "EC", 80, TLS_GROUP_CURVE_CHAR2, 0x0001}, /* sect163k1 (1) */ |
150 | {NID_sect163r1, "EC", 80, TLS_GROUP_CURVE_CHAR2, 0x0002}, /* sect163r1 (2) */ | |
151 | {NID_sect163r2, "EC", 80, TLS_GROUP_CURVE_CHAR2, 0x0003}, /* sect163r2 (3) */ | |
152 | {NID_sect193r1, "EC", 80, TLS_GROUP_CURVE_CHAR2, 0x0004}, /* sect193r1 (4) */ | |
153 | {NID_sect193r2, "EC", 80, TLS_GROUP_CURVE_CHAR2, 0x0005}, /* sect193r2 (5) */ | |
154 | {NID_sect233k1, "EC", 112, TLS_GROUP_CURVE_CHAR2, 0x0006}, /* sect233k1 (6) */ | |
155 | {NID_sect233r1, "EC", 112, TLS_GROUP_CURVE_CHAR2, 0x0007}, /* sect233r1 (7) */ | |
156 | {NID_sect239k1, "EC", 112, TLS_GROUP_CURVE_CHAR2, 0x0008}, /* sect239k1 (8) */ | |
157 | {NID_sect283k1, "EC", 128, TLS_GROUP_CURVE_CHAR2, 0x0009}, /* sect283k1 (9) */ | |
158 | {NID_sect283r1, "EC", 128, TLS_GROUP_CURVE_CHAR2, 0x000A}, /* sect283r1 (10) */ | |
159 | {NID_sect409k1, "EC", 192, TLS_GROUP_CURVE_CHAR2, 0x000B}, /* sect409k1 (11) */ | |
160 | {NID_sect409r1, "EC", 192, TLS_GROUP_CURVE_CHAR2, 0x000C}, /* sect409r1 (12) */ | |
161 | {NID_sect571k1, "EC", 256, TLS_GROUP_CURVE_CHAR2, 0x000D}, /* sect571k1 (13) */ | |
162 | {NID_sect571r1, "EC", 256, TLS_GROUP_CURVE_CHAR2, 0x000E}, /* sect571r1 (14) */ | |
163 | {NID_secp160k1, "EC", 80, TLS_GROUP_CURVE_PRIME, 0x000F}, /* secp160k1 (15) */ | |
164 | {NID_secp160r1, "EC", 80, TLS_GROUP_CURVE_PRIME, 0x0010}, /* secp160r1 (16) */ | |
165 | {NID_secp160r2, "EC", 80, TLS_GROUP_CURVE_PRIME, 0x0011}, /* secp160r2 (17) */ | |
166 | {NID_secp192k1, "EC", 80, TLS_GROUP_CURVE_PRIME, 0x0012}, /* secp192k1 (18) */ | |
167 | {NID_X9_62_prime192v1, "EC", 80, TLS_GROUP_CURVE_PRIME, 0x0013}, /* secp192r1 (19) */ | |
168 | {NID_secp224k1, "EC", 112, TLS_GROUP_CURVE_PRIME, 0x0014}, /* secp224k1 (20) */ | |
169 | {NID_secp224r1, "EC", 112, TLS_GROUP_CURVE_PRIME, 0x0015}, /* secp224r1 (21) */ | |
170 | {NID_secp256k1, "EC", 128, TLS_GROUP_CURVE_PRIME, 0x0016}, /* secp256k1 (22) */ | |
171 | {NID_X9_62_prime256v1, "EC", 128, TLS_GROUP_CURVE_PRIME, 0x0017}, /* secp256r1 (23) */ | |
172 | {NID_secp384r1, "EC", 192, TLS_GROUP_CURVE_PRIME, 0x0018}, /* secp384r1 (24) */ | |
173 | {NID_secp521r1, "EC", 256, TLS_GROUP_CURVE_PRIME, 0x0019}, /* secp521r1 (25) */ | |
174 | {NID_brainpoolP256r1, "EC", 128, TLS_GROUP_CURVE_PRIME, 0x001A}, /* brainpoolP256r1 (26) */ | |
175 | {NID_brainpoolP384r1, "EC", 192, TLS_GROUP_CURVE_PRIME, 0x001B}, /* brainpoolP384r1 (27) */ | |
176 | {NID_brainpoolP512r1, "EC", 256, TLS_GROUP_CURVE_PRIME, 0x001C}, /* brainpool512r1 (28) */ | |
177 | {EVP_PKEY_X25519, "X25519", 128, TLS_GROUP_CURVE_CUSTOM, 0x001D}, /* X25519 (29) */ | |
178 | {EVP_PKEY_X448, "X448", 224, TLS_GROUP_CURVE_CUSTOM, 0x001E}, /* X448 (30) */ | |
65dc5c3c | 179 | # endif /* OPENSSL_NO_EC */ |
fa555aa8 | 180 | # ifndef OPENSSL_NO_GOST |
c9c78d27 NM |
181 | {NID_id_tc26_gost_3410_2012_256_paramSetA, "GOST_2012_256", 128, TLS_GROUP_CURVE_PRIME, 0x0022}, /* GC256A (34) */ |
182 | {NID_id_tc26_gost_3410_2012_256_paramSetB, "GOST_2012_256", 128, TLS_GROUP_CURVE_PRIME, 0x0023}, /* GC256B (35) */ | |
183 | {NID_id_tc26_gost_3410_2012_256_paramSetC, "GOST_2012_256", 128, TLS_GROUP_CURVE_PRIME, 0x0024}, /* GC256C (36) */ | |
184 | {NID_id_tc26_gost_3410_2012_256_paramSetD, "GOST_2012_256", 128, TLS_GROUP_CURVE_PRIME, 0x0025}, /* GC256D (37) */ | |
185 | {NID_id_tc26_gost_3410_2012_512_paramSetA, "GOST_2012_512", 256, TLS_GROUP_CURVE_PRIME, 0x0026}, /* GC512A (38) */ | |
186 | {NID_id_tc26_gost_3410_2012_512_paramSetB, "GOST_2012_512", 256, TLS_GROUP_CURVE_PRIME, 0x0027}, /* GC512B (39) */ | |
187 | {NID_id_tc26_gost_3410_2012_512_paramSetC, "GOST_2012_512", 256, TLS_GROUP_CURVE_PRIME, 0x0028}, /* GC512C (40) */ | |
fa555aa8 | 188 | # endif /* OPENSSL_NO_GOST */ |
65dc5c3c | 189 | # ifndef OPENSSL_NO_DH |
9aaecbfc | 190 | /* Security bit values for FFDHE groups are updated as per RFC 7919 */ |
c0bfc473 MC |
191 | {NID_ffdhe2048, "DH", 103, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0100}, /* ffdhe2048 (0x0100) */ |
192 | {NID_ffdhe3072, "DH", 125, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0101}, /* ffdhe3072 (0x0101) */ | |
193 | {NID_ffdhe4096, "DH", 150, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0102}, /* ffdhe4096 (0x0102) */ | |
194 | {NID_ffdhe6144, "DH", 175, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0103}, /* ffdhe6144 (0x0103) */ | |
195 | {NID_ffdhe8192, "DH", 192, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0104}, /* ffdhe8192 (0x0104) */ | |
65dc5c3c | 196 | # endif /* OPENSSL_NO_DH */ |
0f113f3e | 197 | }; |
65dc5c3c | 198 | #endif |
0f113f3e | 199 | |
dbc6268f | 200 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
201 | static const unsigned char ecformats_default[] = { |
202 | TLSEXT_ECPOINTFORMAT_uncompressed, | |
203 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | |
204 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | |
205 | }; | |
65dc5c3c | 206 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ |
0f113f3e | 207 | |
fe6ef247 | 208 | /* The default curves */ |
65dc5c3c | 209 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) |
9aaecbfc | 210 | static const uint16_t supported_groups_default[] = { |
65dc5c3c | 211 | # ifndef OPENSSL_NO_EC |
9e84a42d DSH |
212 | 29, /* X25519 (29) */ |
213 | 23, /* secp256r1 (23) */ | |
0e1d6ecf | 214 | 30, /* X448 (30) */ |
9e84a42d DSH |
215 | 25, /* secp521r1 (25) */ |
216 | 24, /* secp384r1 (24) */ | |
65dc5c3c | 217 | # endif |
fa555aa8 NM |
218 | # ifndef OPENSSL_NO_GOST |
219 | 34, /* GC256A (34) */ | |
220 | 35, /* GC256B (35) */ | |
221 | 36, /* GC256C (36) */ | |
222 | 37, /* GC256D (37) */ | |
223 | 38, /* GC512A (38) */ | |
224 | 39, /* GC512B (39) */ | |
225 | 40, /* GC512C (40) */ | |
226 | # endif | |
65dc5c3c | 227 | # ifndef OPENSSL_NO_DH |
9aaecbfc | 228 | 0x100, /* ffdhe2048 (0x100) */ |
229 | 0x101, /* ffdhe3072 (0x101) */ | |
230 | 0x102, /* ffdhe4096 (0x102) */ | |
231 | 0x103, /* ffdhe6144 (0x103) */ | |
232 | 0x104, /* ffdhe8192 (0x104) */ | |
65dc5c3c | 233 | # endif |
de57d237 | 234 | }; |
65dc5c3c | 235 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ |
de57d237 | 236 | |
dbc6268f | 237 | #ifndef OPENSSL_NO_EC |
9e84a42d DSH |
238 | static const uint16_t suiteb_curves[] = { |
239 | TLSEXT_curve_P_256, | |
240 | TLSEXT_curve_P_384 | |
0f113f3e | 241 | }; |
dbc6268f | 242 | #endif |
2ea80354 | 243 | |
f48d826e | 244 | const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id) |
0f113f3e | 245 | { |
65dc5c3c | 246 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) |
9aaecbfc | 247 | size_t i; |
248 | ||
249 | /* ECC curves from RFC 4492 and RFC 7027 FFDHE group from RFC 8446 */ | |
250 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { | |
251 | if (nid_list[i].group_id == group_id) | |
252 | return &nid_list[i]; | |
253 | } | |
65dc5c3c | 254 | #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */ |
9aaecbfc | 255 | return NULL; |
0f113f3e | 256 | } |
525de5d3 | 257 | |
65dc5c3c | 258 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) |
84d4b9e3 | 259 | int tls1_group_id2nid(uint16_t group_id) |
260 | { | |
261 | const TLS_GROUP_INFO *ginf = tls1_group_id_lookup(group_id); | |
262 | ||
263 | return ginf == NULL ? NID_undef : ginf->nid; | |
264 | } | |
265 | ||
4a1b4280 | 266 | static uint16_t tls1_nid2group_id(int nid) |
0f113f3e | 267 | { |
2fa2d15a | 268 | size_t i; |
9aaecbfc | 269 | |
2fa2d15a DSH |
270 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { |
271 | if (nid_list[i].nid == nid) | |
9aaecbfc | 272 | return nid_list[i].group_id; |
0f113f3e | 273 | } |
2fa2d15a | 274 | return 0; |
0f113f3e | 275 | } |
65dc5c3c | 276 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ |
0f113f3e | 277 | |
740580c2 | 278 | /* |
ff6d20a6 DSH |
279 | * Set *pgroups to the supported groups list and *pgroupslen to |
280 | * the number of groups supported. | |
fd2b65ce | 281 | */ |
ff6d20a6 DSH |
282 | void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups, |
283 | size_t *pgroupslen) | |
0f113f3e | 284 | { |
65dc5c3c | 285 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
34e5292c DSH |
286 | /* For Suite B mode only include P-256, P-384 */ |
287 | switch (tls1_suiteb(s)) { | |
65dc5c3c | 288 | # ifndef OPENSSL_NO_EC |
34e5292c | 289 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
ff6d20a6 DSH |
290 | *pgroups = suiteb_curves; |
291 | *pgroupslen = OSSL_NELEM(suiteb_curves); | |
34e5292c DSH |
292 | break; |
293 | ||
294 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
ff6d20a6 DSH |
295 | *pgroups = suiteb_curves; |
296 | *pgroupslen = 1; | |
34e5292c DSH |
297 | break; |
298 | ||
299 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
ff6d20a6 DSH |
300 | *pgroups = suiteb_curves + 1; |
301 | *pgroupslen = 1; | |
34e5292c | 302 | break; |
65dc5c3c | 303 | # endif |
34e5292c DSH |
304 | |
305 | default: | |
306 | if (s->ext.supportedgroups == NULL) { | |
9aaecbfc | 307 | *pgroups = supported_groups_default; |
308 | *pgroupslen = OSSL_NELEM(supported_groups_default); | |
34e5292c | 309 | } else { |
ff6d20a6 DSH |
310 | *pgroups = s->ext.supportedgroups; |
311 | *pgroupslen = s->ext.supportedgroups_len; | |
0f113f3e | 312 | } |
34e5292c | 313 | break; |
0f113f3e | 314 | } |
65dc5c3c MC |
315 | #else |
316 | *pgroups = NULL; | |
317 | *pgroupslen = 0; | |
318 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ | |
0f113f3e | 319 | } |
b362ccab | 320 | |
9aaecbfc | 321 | int tls_valid_group(SSL *s, uint16_t group_id, int version) |
322 | { | |
323 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group_id); | |
324 | ||
325 | if (version < TLS1_3_VERSION) { | |
326 | if ((ginfo->flags & TLS_GROUP_ONLY_FOR_TLS1_3) != 0) | |
327 | return 0; | |
328 | } | |
329 | return 1; | |
330 | } | |
331 | ||
dbc6268f MC |
332 | /* See if group is allowed by security callback */ |
333 | int tls_group_allowed(SSL *s, uint16_t group, int op) | |
0f113f3e | 334 | { |
dbc6268f MC |
335 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group); |
336 | unsigned char gtmp[2]; | |
5ce5f787 | 337 | |
dbc6268f | 338 | if (ginfo == NULL) |
0f113f3e | 339 | return 0; |
dbc6268f MC |
340 | #ifdef OPENSSL_NO_EC2M |
341 | if (ginfo->flags & TLS_GROUP_CURVE_CHAR2) | |
9aaecbfc | 342 | return 0; |
dbc6268f MC |
343 | #endif |
344 | #ifdef OPENSSL_NO_DH | |
345 | if (ginfo->flags & TLS_GROUP_FFDHE) | |
0f113f3e | 346 | return 0; |
dbc6268f MC |
347 | #endif |
348 | gtmp[0] = group >> 8; | |
349 | gtmp[1] = group & 0xff; | |
350 | return ssl_security(s, op, ginfo->secbits, ginfo->nid, (void *)gtmp); | |
0f113f3e | 351 | } |
b362ccab | 352 | |
b50951d3 DSH |
353 | /* Return 1 if "id" is in "list" */ |
354 | static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) | |
355 | { | |
356 | size_t i; | |
357 | for (i = 0; i < listlen; i++) | |
358 | if (list[i] == id) | |
359 | return 1; | |
360 | return 0; | |
361 | } | |
362 | ||
1d97c843 | 363 | /*- |
8841154a | 364 | * For nmatch >= 0, return the id of the |nmatch|th shared group or 0 |
6977e8ee KR |
365 | * if there is no match. |
366 | * For nmatch == -1, return number of matches | |
8841154a | 367 | * For nmatch == -2, return the id of the group to use for |
b50951d3 | 368 | * a tmp key, or 0 if there is no match. |
d0595f17 | 369 | */ |
8841154a | 370 | uint16_t tls1_shared_group(SSL *s, int nmatch) |
0f113f3e | 371 | { |
9e84a42d | 372 | const uint16_t *pref, *supp; |
b50951d3 | 373 | size_t num_pref, num_supp, i; |
0f113f3e | 374 | int k; |
3e373518 | 375 | |
0f113f3e MC |
376 | /* Can't do anything on client side */ |
377 | if (s->server == 0) | |
8841154a | 378 | return 0; |
0f113f3e MC |
379 | if (nmatch == -2) { |
380 | if (tls1_suiteb(s)) { | |
381 | /* | |
382 | * For Suite B ciphersuite determines curve: we already know | |
383 | * these are acceptable due to previous checks. | |
384 | */ | |
555cbb32 | 385 | unsigned long cid = s->s3.tmp.new_cipher->id; |
3e373518 | 386 | |
0f113f3e | 387 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
8841154a | 388 | return TLSEXT_curve_P_256; |
0f113f3e | 389 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
8841154a | 390 | return TLSEXT_curve_P_384; |
0f113f3e | 391 | /* Should never happen */ |
8841154a | 392 | return 0; |
0f113f3e MC |
393 | } |
394 | /* If not Suite B just return first preference shared curve */ | |
395 | nmatch = 0; | |
396 | } | |
397 | /* | |
ff6d20a6 DSH |
398 | * If server preference set, our groups are the preference order |
399 | * otherwise peer decides. | |
0f113f3e | 400 | */ |
ff6d20a6 DSH |
401 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
402 | tls1_get_supported_groups(s, &pref, &num_pref); | |
403 | tls1_get_peer_groups(s, &supp, &num_supp); | |
404 | } else { | |
405 | tls1_get_peer_groups(s, &pref, &num_pref); | |
406 | tls1_get_supported_groups(s, &supp, &num_supp); | |
407 | } | |
3c06513f | 408 | |
9e84a42d DSH |
409 | for (k = 0, i = 0; i < num_pref; i++) { |
410 | uint16_t id = pref[i]; | |
3e373518 | 411 | |
b50951d3 | 412 | if (!tls1_in_list(id, supp, num_supp) |
dbc6268f | 413 | || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED)) |
0f113f3e | 414 | continue; |
b50951d3 DSH |
415 | if (nmatch == k) |
416 | return id; | |
417 | k++; | |
0f113f3e MC |
418 | } |
419 | if (nmatch == -1) | |
420 | return k; | |
421 | /* Out of range (nmatch > k). */ | |
8841154a | 422 | return 0; |
0f113f3e | 423 | } |
d0595f17 | 424 | |
9e84a42d | 425 | int tls1_set_groups(uint16_t **pext, size_t *pextlen, |
de4d764e | 426 | int *groups, size_t ngroups) |
0f113f3e | 427 | { |
65dc5c3c | 428 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
9e84a42d | 429 | uint16_t *glist; |
0f113f3e MC |
430 | size_t i; |
431 | /* | |
9aaecbfc | 432 | * Bitmap of groups included to detect duplicates: two variables are added |
433 | * to detect duplicates as some values are more than 32. | |
0f113f3e | 434 | */ |
9aaecbfc | 435 | unsigned long *dup_list = NULL; |
436 | unsigned long dup_list_egrp = 0; | |
437 | unsigned long dup_list_dhgrp = 0; | |
cdb10bae | 438 | |
680bd131 MC |
439 | if (ngroups == 0) { |
440 | SSLerr(SSL_F_TLS1_SET_GROUPS, SSL_R_BAD_LENGTH); | |
441 | return 0; | |
442 | } | |
cdb10bae RS |
443 | if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) { |
444 | SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 445 | return 0; |
cdb10bae | 446 | } |
9e84a42d | 447 | for (i = 0; i < ngroups; i++) { |
0f113f3e | 448 | unsigned long idmask; |
9e84a42d | 449 | uint16_t id; |
4a1b4280 | 450 | id = tls1_nid2group_id(groups[i]); |
9aaecbfc | 451 | if ((id & 0x00FF) >= (sizeof(unsigned long) * 8)) |
452 | goto err; | |
453 | idmask = 1L << (id & 0x00FF); | |
454 | dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp; | |
455 | if (!id || ((*dup_list) & idmask)) | |
456 | goto err; | |
457 | *dup_list |= idmask; | |
9e84a42d | 458 | glist[i] = id; |
0f113f3e | 459 | } |
b548a1f1 | 460 | OPENSSL_free(*pext); |
de4d764e | 461 | *pext = glist; |
9e84a42d | 462 | *pextlen = ngroups; |
0f113f3e | 463 | return 1; |
9aaecbfc | 464 | err: |
465 | OPENSSL_free(glist); | |
466 | return 0; | |
65dc5c3c MC |
467 | #else |
468 | return 0; | |
469 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ | |
0f113f3e MC |
470 | } |
471 | ||
65dc5c3c MC |
472 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
473 | # define MAX_GROUPLIST OSSL_NELEM(nid_list) | |
0f113f3e MC |
474 | |
475 | typedef struct { | |
476 | size_t nidcnt; | |
dbc6268f | 477 | int nid_arr[MAX_GROUPLIST]; |
0f113f3e | 478 | } nid_cb_st; |
d0595f17 DSH |
479 | |
480 | static int nid_cb(const char *elem, int len, void *arg) | |
0f113f3e MC |
481 | { |
482 | nid_cb_st *narg = arg; | |
483 | size_t i; | |
dbc6268f | 484 | int nid = NID_undef; |
0f113f3e | 485 | char etmp[20]; |
2747d73c KR |
486 | if (elem == NULL) |
487 | return 0; | |
dbc6268f | 488 | if (narg->nidcnt == MAX_GROUPLIST) |
0f113f3e MC |
489 | return 0; |
490 | if (len > (int)(sizeof(etmp) - 1)) | |
491 | return 0; | |
492 | memcpy(etmp, elem, len); | |
493 | etmp[len] = 0; | |
65dc5c3c | 494 | # ifndef OPENSSL_NO_EC |
0f113f3e | 495 | nid = EC_curve_nist2nid(etmp); |
65dc5c3c | 496 | # endif |
0f113f3e MC |
497 | if (nid == NID_undef) |
498 | nid = OBJ_sn2nid(etmp); | |
499 | if (nid == NID_undef) | |
500 | nid = OBJ_ln2nid(etmp); | |
501 | if (nid == NID_undef) | |
502 | return 0; | |
503 | for (i = 0; i < narg->nidcnt; i++) | |
504 | if (narg->nid_arr[i] == nid) | |
505 | return 0; | |
506 | narg->nid_arr[narg->nidcnt++] = nid; | |
507 | return 1; | |
508 | } | |
65dc5c3c | 509 | #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */ |
0f113f3e | 510 | |
de4d764e | 511 | /* Set groups based on a colon separate list */ |
9e84a42d | 512 | int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str) |
0f113f3e | 513 | { |
65dc5c3c | 514 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
0f113f3e MC |
515 | nid_cb_st ncb; |
516 | ncb.nidcnt = 0; | |
517 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) | |
518 | return 0; | |
519 | if (pext == NULL) | |
520 | return 1; | |
de4d764e | 521 | return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); |
65dc5c3c MC |
522 | #else |
523 | return 0; | |
524 | #endif | |
0f113f3e | 525 | } |
b50951d3 | 526 | |
4a1b4280 | 527 | /* Check a group id matches preferences */ |
dcf8b01f | 528 | int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups) |
4a1b4280 DSH |
529 | { |
530 | const uint16_t *groups; | |
b50951d3 | 531 | size_t groups_len; |
4a1b4280 DSH |
532 | |
533 | if (group_id == 0) | |
534 | return 0; | |
535 | ||
6447e818 | 536 | /* Check for Suite B compliance */ |
555cbb32 TS |
537 | if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { |
538 | unsigned long cid = s->s3.tmp.new_cipher->id; | |
6447e818 DSH |
539 | |
540 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | |
541 | if (group_id != TLSEXT_curve_P_256) | |
542 | return 0; | |
543 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | |
544 | if (group_id != TLSEXT_curve_P_384) | |
545 | return 0; | |
546 | } else { | |
547 | /* Should never happen */ | |
548 | return 0; | |
549 | } | |
550 | } | |
b50951d3 | 551 | |
dcf8b01f MC |
552 | if (check_own_groups) { |
553 | /* Check group is one of our preferences */ | |
554 | tls1_get_supported_groups(s, &groups, &groups_len); | |
555 | if (!tls1_in_list(group_id, groups, groups_len)) | |
556 | return 0; | |
557 | } | |
4a1b4280 | 558 | |
dbc6268f | 559 | if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) |
6447e818 DSH |
560 | return 0; |
561 | ||
4a1b4280 DSH |
562 | /* For clients, nothing more to check */ |
563 | if (!s->server) | |
564 | return 1; | |
565 | ||
566 | /* Check group is one of peers preferences */ | |
ff6d20a6 | 567 | tls1_get_peer_groups(s, &groups, &groups_len); |
4a1b4280 DSH |
568 | |
569 | /* | |
570 | * RFC 4492 does not require the supported elliptic curves extension | |
571 | * so if it is not sent we can just choose any curve. | |
572 | * It is invalid to send an empty list in the supported groups | |
573 | * extension, so groups_len == 0 always means no extension. | |
574 | */ | |
575 | if (groups_len == 0) | |
576 | return 1; | |
b50951d3 | 577 | return tls1_in_list(group_id, groups, groups_len); |
0f113f3e | 578 | } |
d61ff83b | 579 | |
dbc6268f | 580 | #ifndef OPENSSL_NO_EC |
7da160b0 MC |
581 | void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
582 | size_t *num_formats) | |
0f113f3e MC |
583 | { |
584 | /* | |
585 | * If we have a custom point format list use it otherwise use default | |
586 | */ | |
aff8c126 RS |
587 | if (s->ext.ecpointformats) { |
588 | *pformats = s->ext.ecpointformats; | |
589 | *num_formats = s->ext.ecpointformats_len; | |
0f113f3e MC |
590 | } else { |
591 | *pformats = ecformats_default; | |
592 | /* For Suite B we don't support char2 fields */ | |
593 | if (tls1_suiteb(s)) | |
594 | *num_formats = sizeof(ecformats_default) - 1; | |
595 | else | |
596 | *num_formats = sizeof(ecformats_default); | |
597 | } | |
598 | } | |
599 | ||
dbc6268f MC |
600 | /* Check a key is compatible with compression extension */ |
601 | static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey) | |
602 | { | |
603 | const EC_KEY *ec; | |
604 | const EC_GROUP *grp; | |
605 | unsigned char comp_id; | |
606 | size_t i; | |
607 | ||
608 | /* If not an EC key nothing to check */ | |
c2041da8 | 609 | if (!EVP_PKEY_is_a(pkey, "EC")) |
dbc6268f MC |
610 | return 1; |
611 | ec = EVP_PKEY_get0_EC_KEY(pkey); | |
612 | grp = EC_KEY_get0_group(ec); | |
613 | ||
614 | /* Get required compression id */ | |
615 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { | |
616 | comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | |
617 | } else if (SSL_IS_TLS13(s)) { | |
618 | /* | |
619 | * ec_point_formats extension is not used in TLSv1.3 so we ignore | |
620 | * this check. | |
621 | */ | |
622 | return 1; | |
623 | } else { | |
23ccae80 | 624 | int field_type = EC_GROUP_get_field_type(grp); |
dbc6268f MC |
625 | |
626 | if (field_type == NID_X9_62_prime_field) | |
627 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; | |
628 | else if (field_type == NID_X9_62_characteristic_two_field) | |
629 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | |
630 | else | |
631 | return 0; | |
632 | } | |
633 | /* | |
634 | * If point formats extension present check it, otherwise everything is | |
635 | * supported (see RFC4492). | |
636 | */ | |
cd0fb43c | 637 | if (s->ext.peer_ecpointformats == NULL) |
dbc6268f MC |
638 | return 1; |
639 | ||
cd0fb43c MC |
640 | for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
641 | if (s->ext.peer_ecpointformats[i] == comp_id) | |
dbc6268f MC |
642 | return 1; |
643 | } | |
644 | return 0; | |
645 | } | |
646 | ||
647 | /* Return group id of a key */ | |
648 | static uint16_t tls1_get_group_id(EVP_PKEY *pkey) | |
649 | { | |
c2041da8 | 650 | int curve_nid = evp_pkey_get_EC_KEY_curve_nid(pkey); |
dbc6268f | 651 | |
c2041da8 | 652 | if (curve_nid == NID_undef) |
dbc6268f | 653 | return 0; |
c2041da8 | 654 | return tls1_nid2group_id(curve_nid); |
dbc6268f MC |
655 | } |
656 | ||
0f113f3e MC |
657 | /* |
658 | * Check cert parameters compatible with extensions: currently just checks EC | |
659 | * certificates have compatible curves and compression. | |
d61ff83b | 660 | */ |
9195ddcd | 661 | static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md) |
0f113f3e | 662 | { |
4a1b4280 | 663 | uint16_t group_id; |
0f113f3e | 664 | EVP_PKEY *pkey; |
8382fd3a | 665 | pkey = X509_get0_pubkey(x); |
4a1b4280 | 666 | if (pkey == NULL) |
0f113f3e MC |
667 | return 0; |
668 | /* If not EC nothing to do */ | |
c2041da8 | 669 | if (!EVP_PKEY_is_a(pkey, "EC")) |
0f113f3e | 670 | return 1; |
4a1b4280 DSH |
671 | /* Check compression */ |
672 | if (!tls1_check_pkey_comp(s, pkey)) | |
0f113f3e | 673 | return 0; |
4a1b4280 | 674 | group_id = tls1_get_group_id(pkey); |
dcf8b01f MC |
675 | /* |
676 | * For a server we allow the certificate to not be in our list of supported | |
677 | * groups. | |
678 | */ | |
679 | if (!tls1_check_group_id(s, group_id, !s->server)) | |
0f113f3e MC |
680 | return 0; |
681 | /* | |
682 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or | |
9195ddcd | 683 | * SHA384+P-384. |
0f113f3e | 684 | */ |
9195ddcd | 685 | if (check_ee_md && tls1_suiteb(s)) { |
0f113f3e MC |
686 | int check_md; |
687 | size_t i; | |
9e84a42d | 688 | |
0f113f3e | 689 | /* Check to see we have necessary signing algorithm */ |
4a1b4280 | 690 | if (group_id == TLSEXT_curve_P_256) |
0f113f3e | 691 | check_md = NID_ecdsa_with_SHA256; |
4a1b4280 | 692 | else if (group_id == TLSEXT_curve_P_384) |
0f113f3e MC |
693 | check_md = NID_ecdsa_with_SHA384; |
694 | else | |
695 | return 0; /* Should never happen */ | |
29948ac8 BK |
696 | for (i = 0; i < s->shared_sigalgslen; i++) { |
697 | if (check_md == s->shared_sigalgs[i]->sigandhash) | |
4a1b4280 DSH |
698 | return 1;; |
699 | } | |
700 | return 0; | |
0f113f3e | 701 | } |
4a1b4280 | 702 | return 1; |
0f113f3e MC |
703 | } |
704 | ||
6977e8ee | 705 | /* |
8483a003 | 706 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
6977e8ee KR |
707 | * @s: SSL connection |
708 | * @cid: Cipher ID we're considering using | |
709 | * | |
710 | * Checks that the kECDHE cipher suite we're considering using | |
711 | * is compatible with the client extensions. | |
712 | * | |
713 | * Returns 0 when the cipher can't be used or 1 when it can. | |
714 | */ | |
2ea80354 | 715 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
0f113f3e | 716 | { |
4a1b4280 DSH |
717 | /* If not Suite B just need a shared group */ |
718 | if (!tls1_suiteb(s)) | |
719 | return tls1_shared_group(s, 0) != 0; | |
0f113f3e MC |
720 | /* |
721 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
722 | * curves permitted. | |
723 | */ | |
4a1b4280 | 724 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
dcf8b01f | 725 | return tls1_check_group_id(s, TLSEXT_curve_P_256, 1); |
4a1b4280 | 726 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
dcf8b01f | 727 | return tls1_check_group_id(s, TLSEXT_curve_P_384, 1); |
4a1b4280 DSH |
728 | |
729 | return 0; | |
0f113f3e | 730 | } |
d0595f17 | 731 | |
14536c8c DSH |
732 | #else |
733 | ||
734 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) | |
0f113f3e MC |
735 | { |
736 | return 1; | |
737 | } | |
14536c8c | 738 | |
0f113f3e | 739 | #endif /* OPENSSL_NO_EC */ |
f1fd4544 | 740 | |
703bcee0 | 741 | /* Default sigalg schemes */ |
98c792d1 | 742 | static const uint16_t tls12_sigalgs[] = { |
703bcee0 MC |
743 | #ifndef OPENSSL_NO_EC |
744 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, | |
745 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, | |
746 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, | |
3d234c9e | 747 | TLSEXT_SIGALG_ed25519, |
0e1d6ecf | 748 | TLSEXT_SIGALG_ed448, |
e481f9b9 | 749 | #endif |
0f113f3e | 750 | |
f55e99f7 BK |
751 | TLSEXT_SIGALG_rsa_pss_pss_sha256, |
752 | TLSEXT_SIGALG_rsa_pss_pss_sha384, | |
753 | TLSEXT_SIGALG_rsa_pss_pss_sha512, | |
754 | TLSEXT_SIGALG_rsa_pss_rsae_sha256, | |
755 | TLSEXT_SIGALG_rsa_pss_rsae_sha384, | |
756 | TLSEXT_SIGALG_rsa_pss_rsae_sha512, | |
536199ec | 757 | |
703bcee0 MC |
758 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
759 | TLSEXT_SIGALG_rsa_pkcs1_sha384, | |
760 | TLSEXT_SIGALG_rsa_pkcs1_sha512, | |
0f113f3e | 761 | |
5eeb6c6e | 762 | #ifndef OPENSSL_NO_EC |
d8311fc9 | 763 | TLSEXT_SIGALG_ecdsa_sha224, |
42ab2230 | 764 | TLSEXT_SIGALG_ecdsa_sha1, |
5eeb6c6e | 765 | #endif |
d8311fc9 | 766 | TLSEXT_SIGALG_rsa_pkcs1_sha224, |
42ab2230 | 767 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
5eeb6c6e | 768 | #ifndef OPENSSL_NO_DSA |
d8311fc9 | 769 | TLSEXT_SIGALG_dsa_sha224, |
42ab2230 MC |
770 | TLSEXT_SIGALG_dsa_sha1, |
771 | ||
703bcee0 MC |
772 | TLSEXT_SIGALG_dsa_sha256, |
773 | TLSEXT_SIGALG_dsa_sha384, | |
41f10305 DB |
774 | TLSEXT_SIGALG_dsa_sha512, |
775 | #endif | |
776 | #ifndef OPENSSL_NO_GOST | |
6f892296 NM |
777 | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
778 | TLSEXT_SIGALG_gostr34102012_512_intrinsic, | |
41f10305 DB |
779 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
780 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, | |
781 | TLSEXT_SIGALG_gostr34102001_gostr3411, | |
5eeb6c6e | 782 | #endif |
fc101f88 | 783 | }; |
0f113f3e | 784 | |
e481f9b9 | 785 | #ifndef OPENSSL_NO_EC |
98c792d1 | 786 | static const uint16_t suiteb_sigalgs[] = { |
703bcee0 MC |
787 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
788 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 | |
2ea80354 | 789 | }; |
e481f9b9 | 790 | #endif |
aff8c126 | 791 | |
7a531ee4 | 792 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
5eeb6c6e | 793 | #ifndef OPENSSL_NO_EC |
edbfba1a | 794 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
17ae384e DSH |
795 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
796 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, | |
edbfba1a | 797 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
17ae384e DSH |
798 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
799 | NID_ecdsa_with_SHA384, NID_secp384r1}, | |
edbfba1a | 800 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
17ae384e DSH |
801 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
802 | NID_ecdsa_with_SHA512, NID_secp521r1}, | |
b04d4e38 | 803 | {"ed25519", TLSEXT_SIGALG_ed25519, |
d2916a5b | 804 | NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, |
b04d4e38 | 805 | NID_undef, NID_undef}, |
0e1d6ecf MC |
806 | {"ed448", TLSEXT_SIGALG_ed448, |
807 | NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, | |
808 | NID_undef, NID_undef}, | |
d8311fc9 MC |
809 | {NULL, TLSEXT_SIGALG_ecdsa_sha224, |
810 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
811 | NID_ecdsa_with_SHA224, NID_undef}, | |
edbfba1a | 812 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
17ae384e DSH |
813 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
814 | NID_ecdsa_with_SHA1, NID_undef}, | |
5eeb6c6e | 815 | #endif |
f55e99f7 BK |
816 | {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
817 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
818 | NID_undef, NID_undef}, | |
819 | {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, | |
820 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
821 | NID_undef, NID_undef}, | |
822 | {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, | |
823 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
824 | NID_undef, NID_undef}, | |
825 | {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256, | |
17ae384e DSH |
826 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
827 | NID_undef, NID_undef}, | |
f55e99f7 | 828 | {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384, |
17ae384e DSH |
829 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
830 | NID_undef, NID_undef}, | |
f55e99f7 | 831 | {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512, |
17ae384e DSH |
832 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
833 | NID_undef, NID_undef}, | |
edbfba1a | 834 | {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
d0ff28f8 | 835 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 836 | NID_sha256WithRSAEncryption, NID_undef}, |
edbfba1a | 837 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
d0ff28f8 | 838 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 839 | NID_sha384WithRSAEncryption, NID_undef}, |
edbfba1a | 840 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
d0ff28f8 | 841 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 842 | NID_sha512WithRSAEncryption, NID_undef}, |
d8311fc9 MC |
843 | {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
844 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, | |
845 | NID_sha224WithRSAEncryption, NID_undef}, | |
edbfba1a | 846 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
d0ff28f8 | 847 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 848 | NID_sha1WithRSAEncryption, NID_undef}, |
5eeb6c6e | 849 | #ifndef OPENSSL_NO_DSA |
edbfba1a | 850 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
17ae384e DSH |
851 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
852 | NID_dsa_with_SHA256, NID_undef}, | |
edbfba1a | 853 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
17ae384e DSH |
854 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
855 | NID_undef, NID_undef}, | |
edbfba1a | 856 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
17ae384e DSH |
857 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
858 | NID_undef, NID_undef}, | |
d8311fc9 MC |
859 | {NULL, TLSEXT_SIGALG_dsa_sha224, |
860 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, | |
861 | NID_undef, NID_undef}, | |
edbfba1a | 862 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
17ae384e DSH |
863 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
864 | NID_dsaWithSHA1, NID_undef}, | |
5eeb6c6e MC |
865 | #endif |
866 | #ifndef OPENSSL_NO_GOST | |
6f892296 NM |
867 | {NULL, TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
868 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, | |
869 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
870 | NID_undef, NID_undef}, | |
871 | {NULL, TLSEXT_SIGALG_gostr34102012_512_intrinsic, | |
872 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, | |
873 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
874 | NID_undef, NID_undef}, | |
edbfba1a | 875 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
17ae384e DSH |
876 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
877 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
878 | NID_undef, NID_undef}, | |
edbfba1a | 879 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
17ae384e DSH |
880 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
881 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
882 | NID_undef, NID_undef}, | |
edbfba1a | 883 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
17ae384e DSH |
884 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
885 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, | |
886 | NID_undef, NID_undef} | |
5eeb6c6e | 887 | #endif |
703bcee0 | 888 | }; |
0972bc5c DSH |
889 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
890 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { | |
891 | "rsa_pkcs1_md5_sha1", 0, | |
892 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, | |
893 | EVP_PKEY_RSA, SSL_PKEY_RSA, | |
894 | NID_undef, NID_undef | |
895 | }; | |
896 | ||
897 | /* | |
898 | * Default signature algorithm values used if signature algorithms not present. | |
899 | * From RFC5246. Note: order must match certificate index order. | |
900 | */ | |
901 | static const uint16_t tls_default_sigalg[] = { | |
902 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ | |
045d078a | 903 | 0, /* SSL_PKEY_RSA_PSS_SIGN */ |
0972bc5c DSH |
904 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
905 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ | |
906 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ | |
6f892296 NM |
907 | TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */ |
908 | TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */ | |
0e1d6ecf MC |
909 | 0, /* SSL_PKEY_ED25519 */ |
910 | 0, /* SSL_PKEY_ED448 */ | |
0972bc5c | 911 | }; |
703bcee0 | 912 | |
4d43ee28 DSH |
913 | /* Lookup TLS signature algorithm */ |
914 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) | |
703bcee0 MC |
915 | { |
916 | size_t i; | |
4d43ee28 | 917 | const SIGALG_LOOKUP *s; |
703bcee0 | 918 | |
4d43ee28 DSH |
919 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
920 | i++, s++) { | |
921 | if (s->sigalg == sigalg) | |
922 | return s; | |
703bcee0 | 923 | } |
4d43ee28 DSH |
924 | return NULL; |
925 | } | |
168067b6 | 926 | /* Lookup hash: return 0 if invalid or not enabled */ |
c8f6c28a | 927 | int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd) |
168067b6 DSH |
928 | { |
929 | const EVP_MD *md; | |
930 | if (lu == NULL) | |
931 | return 0; | |
932 | /* lu->hash == NID_undef means no associated digest */ | |
933 | if (lu->hash == NID_undef) { | |
934 | md = NULL; | |
935 | } else { | |
c8f6c28a | 936 | md = ssl_md(ctx, lu->hash_idx); |
168067b6 DSH |
937 | if (md == NULL) |
938 | return 0; | |
939 | } | |
940 | if (pmd) | |
941 | *pmd = md; | |
942 | return 1; | |
943 | } | |
944 | ||
0fe3db25 NR |
945 | /* |
946 | * Check if key is large enough to generate RSA-PSS signature. | |
947 | * | |
948 | * The key must greater than or equal to 2 * hash length + 2. | |
949 | * SHA512 has a hash length of 64 bytes, which is incompatible | |
950 | * with a 128 byte (1024 bit) key. | |
951 | */ | |
952 | #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2) | |
c5f87134 | 953 | static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey, |
c8f6c28a | 954 | const SIGALG_LOOKUP *lu) |
0fe3db25 NR |
955 | { |
956 | const EVP_MD *md; | |
957 | ||
c5f87134 | 958 | if (pkey == NULL) |
0fe3db25 | 959 | return 0; |
c8f6c28a | 960 | if (!tls1_lookup_md(ctx, lu, &md) || md == NULL) |
0fe3db25 | 961 | return 0; |
c5f87134 | 962 | if (EVP_PKEY_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md)) |
0fe3db25 NR |
963 | return 0; |
964 | return 1; | |
965 | } | |
966 | ||
0972bc5c | 967 | /* |
b0031e5d KR |
968 | * Returns a signature algorithm when the peer did not send a list of supported |
969 | * signature algorithms. The signature algorithm is fixed for the certificate | |
970 | * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the | |
971 | * certificate type from |s| will be used. | |
972 | * Returns the signature algorithm to use, or NULL on error. | |
0972bc5c DSH |
973 | */ |
974 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) | |
975 | { | |
7f6b466b DSH |
976 | if (idx == -1) { |
977 | if (s->server) { | |
978 | size_t i; | |
979 | ||
980 | /* Work out index corresponding to ciphersuite */ | |
981 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
982 | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i); | |
983 | ||
555cbb32 | 984 | if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { |
7f6b466b DSH |
985 | idx = i; |
986 | break; | |
987 | } | |
988 | } | |
1f65c045 DB |
989 | |
990 | /* | |
991 | * Some GOST ciphersuites allow more than one signature algorithms | |
992 | * */ | |
555cbb32 | 993 | if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { |
1f65c045 DB |
994 | int real_idx; |
995 | ||
996 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; | |
997 | real_idx--) { | |
998 | if (s->cert->pkeys[real_idx].privatekey != NULL) { | |
999 | idx = real_idx; | |
1000 | break; | |
1001 | } | |
1002 | } | |
1003 | } | |
5a5530a2 DB |
1004 | /* |
1005 | * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used | |
1006 | * with new (aGOST12-only) ciphersuites, we should find out which one is available really. | |
1007 | */ | |
1008 | else if (idx == SSL_PKEY_GOST12_256) { | |
1009 | int real_idx; | |
1010 | ||
1011 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256; | |
1012 | real_idx--) { | |
1013 | if (s->cert->pkeys[real_idx].privatekey != NULL) { | |
1014 | idx = real_idx; | |
1015 | break; | |
1016 | } | |
1017 | } | |
1018 | } | |
7f6b466b DSH |
1019 | } else { |
1020 | idx = s->cert->key - s->cert->pkeys; | |
1021 | } | |
1022 | } | |
0972bc5c DSH |
1023 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
1024 | return NULL; | |
1025 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { | |
1026 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); | |
1027 | ||
c8f6c28a | 1028 | if (!tls1_lookup_md(s->ctx, lu, NULL)) |
0972bc5c | 1029 | return NULL; |
b0031e5d KR |
1030 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
1031 | return NULL; | |
0972bc5c DSH |
1032 | return lu; |
1033 | } | |
b0031e5d KR |
1034 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg)) |
1035 | return NULL; | |
0972bc5c DSH |
1036 | return &legacy_rsa_sigalg; |
1037 | } | |
1038 | /* Set peer sigalg based key type */ | |
1039 | int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) | |
1040 | { | |
52fd27f9 DSH |
1041 | size_t idx; |
1042 | const SIGALG_LOOKUP *lu; | |
0972bc5c | 1043 | |
52fd27f9 DSH |
1044 | if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) |
1045 | return 0; | |
1046 | lu = tls1_get_legacy_sigalg(s, idx); | |
0972bc5c DSH |
1047 | if (lu == NULL) |
1048 | return 0; | |
555cbb32 | 1049 | s->s3.tmp.peer_sigalg = lu; |
0972bc5c DSH |
1050 | return 1; |
1051 | } | |
703bcee0 | 1052 | |
98c792d1 | 1053 | size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs) |
0f113f3e MC |
1054 | { |
1055 | /* | |
1056 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
1057 | * preferences. | |
1058 | */ | |
e481f9b9 | 1059 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1060 | switch (tls1_suiteb(s)) { |
1061 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
1062 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 1063 | return OSSL_NELEM(suiteb_sigalgs); |
0f113f3e MC |
1064 | |
1065 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
1066 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 1067 | return 1; |
0f113f3e MC |
1068 | |
1069 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
7a531ee4 MC |
1070 | *psigs = suiteb_sigalgs + 1; |
1071 | return 1; | |
0f113f3e | 1072 | } |
e481f9b9 | 1073 | #endif |
a9669ddc DSH |
1074 | /* |
1075 | * We use client_sigalgs (if not NULL) if we're a server | |
1076 | * and sending a certificate request or if we're a client and | |
1077 | * determining which shared algorithm to use. | |
1078 | */ | |
1079 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { | |
0f113f3e MC |
1080 | *psigs = s->cert->client_sigalgs; |
1081 | return s->cert->client_sigalgslen; | |
1082 | } else if (s->cert->conf_sigalgs) { | |
1083 | *psigs = s->cert->conf_sigalgs; | |
1084 | return s->cert->conf_sigalgslen; | |
1085 | } else { | |
1086 | *psigs = tls12_sigalgs; | |
703bcee0 | 1087 | return OSSL_NELEM(tls12_sigalgs); |
0f113f3e MC |
1088 | } |
1089 | } | |
1090 | ||
65d2c16c | 1091 | #ifndef OPENSSL_NO_EC |
de4dc598 MC |
1092 | /* |
1093 | * Called by servers only. Checks that we have a sig alg that supports the | |
1094 | * specified EC curve. | |
1095 | */ | |
1096 | int tls_check_sigalg_curve(const SSL *s, int curve) | |
1097 | { | |
1098 | const uint16_t *sigs; | |
1099 | size_t siglen, i; | |
1100 | ||
1101 | if (s->cert->conf_sigalgs) { | |
1102 | sigs = s->cert->conf_sigalgs; | |
1103 | siglen = s->cert->conf_sigalgslen; | |
1104 | } else { | |
1105 | sigs = tls12_sigalgs; | |
1106 | siglen = OSSL_NELEM(tls12_sigalgs); | |
1107 | } | |
1108 | ||
1109 | for (i = 0; i < siglen; i++) { | |
1110 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(sigs[i]); | |
1111 | ||
1112 | if (lu == NULL) | |
1113 | continue; | |
1114 | if (lu->sig == EVP_PKEY_EC | |
1115 | && lu->curve != NID_undef | |
1116 | && curve == lu->curve) | |
1117 | return 1; | |
1118 | } | |
1119 | ||
1120 | return 0; | |
1121 | } | |
65d2c16c | 1122 | #endif |
de4dc598 | 1123 | |
620c97b6 KR |
1124 | /* |
1125 | * Return the number of security bits for the signature algorithm, or 0 on | |
1126 | * error. | |
1127 | */ | |
1128 | static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) | |
1129 | { | |
1130 | const EVP_MD *md = NULL; | |
1131 | int secbits = 0; | |
1132 | ||
1133 | if (!tls1_lookup_md(ctx, lu, &md)) | |
1134 | return 0; | |
1135 | if (md != NULL) | |
1136 | { | |
1137 | /* Security bits: half digest bits */ | |
1138 | secbits = EVP_MD_size(md) * 4; | |
1139 | } else { | |
1140 | /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */ | |
1141 | if (lu->sigalg == TLSEXT_SIGALG_ed25519) | |
1142 | secbits = 128; | |
1143 | else if (lu->sigalg == TLSEXT_SIGALG_ed448) | |
1144 | secbits = 224; | |
1145 | } | |
1146 | return secbits; | |
1147 | } | |
1148 | ||
0f113f3e MC |
1149 | /* |
1150 | * Check signature algorithm is consistent with sent supported signature | |
b2eb6998 DSH |
1151 | * algorithms and if so set relevant digest and signature scheme in |
1152 | * s. | |
ec4a50b3 | 1153 | */ |
f742cda8 | 1154 | int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey) |
0f113f3e | 1155 | { |
98c792d1 | 1156 | const uint16_t *sent_sigs; |
5554facb | 1157 | const EVP_MD *md = NULL; |
703bcee0 | 1158 | char sigalgstr[2]; |
11d2641f | 1159 | size_t sent_sigslen, i, cidx; |
c2041da8 | 1160 | int pkeyid = -1; |
f742cda8 | 1161 | const SIGALG_LOOKUP *lu; |
620c97b6 | 1162 | int secbits = 0; |
4d43ee28 | 1163 | |
c2041da8 RL |
1164 | /* |
1165 | * TODO(3.0) Remove this when we adapted this function for provider | |
1166 | * side keys. We know that EVP_PKEY_get0() downgrades an EVP_PKEY | |
1167 | * to contain a legacy key. | |
1168 | * | |
1169 | * THIS IS TEMPORARY | |
1170 | */ | |
1171 | EVP_PKEY_get0(pkey); | |
1172 | if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) | |
1173 | return 0; | |
1174 | ||
1175 | pkeyid = EVP_PKEY_id(pkey); | |
0f113f3e | 1176 | /* Should never happen */ |
536199ec | 1177 | if (pkeyid == -1) |
0f113f3e | 1178 | return -1; |
5a8916d9 DSH |
1179 | if (SSL_IS_TLS13(s)) { |
1180 | /* Disallow DSA for TLS 1.3 */ | |
1181 | if (pkeyid == EVP_PKEY_DSA) { | |
f63a17d6 MC |
1182 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, |
1183 | SSL_R_WRONG_SIGNATURE_TYPE); | |
5a8916d9 DSH |
1184 | return 0; |
1185 | } | |
1186 | /* Only allow PSS for TLS 1.3 */ | |
1187 | if (pkeyid == EVP_PKEY_RSA) | |
1188 | pkeyid = EVP_PKEY_RSA_PSS; | |
1189 | } | |
f742cda8 DSH |
1190 | lu = tls1_lookup_sigalg(sig); |
1191 | /* | |
d8311fc9 MC |
1192 | * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
1193 | * is consistent with signature: RSA keys can be used for RSA-PSS | |
f742cda8 | 1194 | */ |
d8311fc9 MC |
1195 | if (lu == NULL |
1196 | || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) | |
095a982b | 1197 | || (pkeyid != lu->sig |
f742cda8 | 1198 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
f63a17d6 MC |
1199 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, |
1200 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e MC |
1201 | return 0; |
1202 | } | |
11d2641f MC |
1203 | /* Check the sigalg is consistent with the key OID */ |
1204 | if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx) | |
1205 | || lu->sig_idx != (int)cidx) { | |
1206 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, | |
1207 | SSL_R_WRONG_SIGNATURE_TYPE); | |
1208 | return 0; | |
1209 | } | |
1210 | ||
e481f9b9 | 1211 | #ifndef OPENSSL_NO_EC |
fe3066ee | 1212 | if (pkeyid == EVP_PKEY_EC) { |
44b6318f | 1213 | |
4a1b4280 DSH |
1214 | /* Check point compression is permitted */ |
1215 | if (!tls1_check_pkey_comp(s, pkey)) { | |
f63a17d6 MC |
1216 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1217 | SSL_F_TLS12_CHECK_PEER_SIGALG, | |
1218 | SSL_R_ILLEGAL_POINT_COMPRESSION); | |
4a1b4280 DSH |
1219 | return 0; |
1220 | } | |
1221 | ||
1222 | /* For TLS 1.3 or Suite B check curve matches signature algorithm */ | |
1223 | if (SSL_IS_TLS13(s) || tls1_suiteb(s)) { | |
c2041da8 | 1224 | int curve = evp_pkey_get_EC_KEY_curve_nid(pkey); |
4a1b4280 | 1225 | |
a34a9df0 | 1226 | if (lu->curve != NID_undef && curve != lu->curve) { |
f63a17d6 MC |
1227 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1228 | SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
8f88cb53 DSH |
1229 | return 0; |
1230 | } | |
4a1b4280 DSH |
1231 | } |
1232 | if (!SSL_IS_TLS13(s)) { | |
1233 | /* Check curve matches extensions */ | |
dcf8b01f | 1234 | if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { |
f63a17d6 MC |
1235 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1236 | SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
8f88cb53 DSH |
1237 | return 0; |
1238 | } | |
8f88cb53 | 1239 | if (tls1_suiteb(s)) { |
f1adb006 DSH |
1240 | /* Check sigalg matches a permissible Suite B value */ |
1241 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 | |
1242 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { | |
f63a17d6 MC |
1243 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
1244 | SSL_F_TLS12_CHECK_PEER_SIGALG, | |
1245 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e | 1246 | return 0; |
f1adb006 | 1247 | } |
8f88cb53 | 1248 | } |
0f113f3e | 1249 | } |
8f88cb53 | 1250 | } else if (tls1_suiteb(s)) { |
f63a17d6 MC |
1251 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, |
1252 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e | 1253 | return 0; |
8f88cb53 | 1254 | } |
e481f9b9 | 1255 | #endif |
0f113f3e MC |
1256 | |
1257 | /* Check signature matches a type we sent */ | |
a9669ddc | 1258 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
536199ec | 1259 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
703bcee0 | 1260 | if (sig == *sent_sigs) |
0f113f3e MC |
1261 | break; |
1262 | } | |
1263 | /* Allow fallback to SHA1 if not strict mode */ | |
f742cda8 DSH |
1264 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
1265 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
f63a17d6 MC |
1266 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, |
1267 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e MC |
1268 | return 0; |
1269 | } | |
c8f6c28a | 1270 | if (!tls1_lookup_md(s->ctx, lu, &md)) { |
f63a17d6 MC |
1271 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, |
1272 | SSL_R_UNKNOWN_DIGEST); | |
1273 | return 0; | |
0f113f3e | 1274 | } |
620c97b6 KR |
1275 | /* |
1276 | * Make sure security callback allows algorithm. For historical | |
1277 | * reasons we have to pass the sigalg as a two byte char array. | |
1278 | */ | |
1279 | sigalgstr[0] = (sig >> 8) & 0xff; | |
1280 | sigalgstr[1] = sig & 0xff; | |
1281 | secbits = sigalg_security_bits(s->ctx, lu); | |
1282 | if (secbits == 0 || | |
1283 | !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits, | |
1284 | md != NULL ? EVP_MD_type(md) : NID_undef, | |
1285 | (void *)sigalgstr)) { | |
1286 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, | |
1287 | SSL_R_WRONG_SIGNATURE_TYPE); | |
1288 | return 0; | |
0f113f3e | 1289 | } |
6cbebb55 | 1290 | /* Store the sigalg the peer uses */ |
555cbb32 | 1291 | s->s3.tmp.peer_sigalg = lu; |
0f113f3e MC |
1292 | return 1; |
1293 | } | |
2ea80354 | 1294 | |
42ef7aea DSH |
1295 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
1296 | { | |
555cbb32 | 1297 | if (s->s3.tmp.peer_sigalg == NULL) |
42ef7aea | 1298 | return 0; |
555cbb32 | 1299 | *pnid = s->s3.tmp.peer_sigalg->sig; |
42ef7aea DSH |
1300 | return 1; |
1301 | } | |
1302 | ||
a51c9f63 VD |
1303 | int SSL_get_signature_type_nid(const SSL *s, int *pnid) |
1304 | { | |
555cbb32 | 1305 | if (s->s3.tmp.sigalg == NULL) |
a51c9f63 | 1306 | return 0; |
555cbb32 | 1307 | *pnid = s->s3.tmp.sigalg->sig; |
a51c9f63 VD |
1308 | return 1; |
1309 | } | |
1310 | ||
0f113f3e | 1311 | /* |
3eb2aff4 KR |
1312 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
1313 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
1314 | * by the enabled protocol versions or by the security level. | |
1315 | * | |
1316 | * This function should only be used for checking which ciphers are supported | |
1317 | * by the client. | |
1318 | * | |
1319 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b | 1320 | */ |
1d0c08b4 | 1321 | int ssl_set_client_disabled(SSL *s) |
0f113f3e | 1322 | { |
555cbb32 TS |
1323 | s->s3.tmp.mask_a = 0; |
1324 | s->s3.tmp.mask_k = 0; | |
1325 | ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); | |
1326 | if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver, | |
1327 | &s->s3.tmp.max_ver, NULL) != 0) | |
1d0c08b4 | 1328 | return 0; |
a230b26e | 1329 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1330 | /* with PSK there must be client callback set */ |
1331 | if (!s->psk_client_callback) { | |
555cbb32 TS |
1332 | s->s3.tmp.mask_a |= SSL_aPSK; |
1333 | s->s3.tmp.mask_k |= SSL_PSK; | |
0f113f3e | 1334 | } |
a230b26e | 1335 | #endif /* OPENSSL_NO_PSK */ |
e481f9b9 | 1336 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1337 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
555cbb32 TS |
1338 | s->s3.tmp.mask_a |= SSL_aSRP; |
1339 | s->s3.tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 1340 | } |
e481f9b9 | 1341 | #endif |
1d0c08b4 | 1342 | return 1; |
0f113f3e | 1343 | } |
fc101f88 | 1344 | |
3eb2aff4 KR |
1345 | /* |
1346 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
1347 | * @s: SSL connection that you want to use the cipher on | |
1348 | * @c: cipher to check | |
1349 | * @op: Security check that you want to do | |
8af91fd9 | 1350 | * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
3eb2aff4 KR |
1351 | * |
1352 | * Returns 1 when it's disabled, 0 when enabled. | |
1353 | */ | |
b0031e5d | 1354 | int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int ecdhe) |
0f113f3e | 1355 | { |
555cbb32 TS |
1356 | if (c->algorithm_mkey & s->s3.tmp.mask_k |
1357 | || c->algorithm_auth & s->s3.tmp.mask_a) | |
0f113f3e | 1358 | return 1; |
555cbb32 | 1359 | if (s->s3.tmp.max_ver == 0) |
3eb2aff4 | 1360 | return 1; |
8af91fd9 MC |
1361 | if (!SSL_IS_DTLS(s)) { |
1362 | int min_tls = c->min_tls; | |
1363 | ||
1364 | /* | |
1365 | * For historical reasons we will allow ECHDE to be selected by a server | |
1366 | * in SSLv3 if we are a client | |
1367 | */ | |
1368 | if (min_tls == TLS1_VERSION && ecdhe | |
1369 | && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) | |
1370 | min_tls = SSL3_VERSION; | |
1371 | ||
555cbb32 | 1372 | if ((min_tls > s->s3.tmp.max_ver) || (c->max_tls < s->s3.tmp.min_ver)) |
8af91fd9 MC |
1373 | return 1; |
1374 | } | |
555cbb32 TS |
1375 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3.tmp.max_ver) |
1376 | || DTLS_VERSION_LT(c->max_dtls, s->s3.tmp.min_ver))) | |
3eb2aff4 KR |
1377 | return 1; |
1378 | ||
0f113f3e MC |
1379 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
1380 | } | |
b362ccab | 1381 | |
7da160b0 | 1382 | int tls_use_ticket(SSL *s) |
0f113f3e | 1383 | { |
08191294 | 1384 | if ((s->options & SSL_OP_NO_TICKET)) |
0f113f3e MC |
1385 | return 0; |
1386 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
1387 | } | |
ed3883d2 | 1388 | |
e469af8d | 1389 | int tls1_set_server_sigalgs(SSL *s) |
0f113f3e | 1390 | { |
0f113f3e | 1391 | size_t i; |
8483a003 F |
1392 | |
1393 | /* Clear any shared signature algorithms */ | |
29948ac8 BK |
1394 | OPENSSL_free(s->shared_sigalgs); |
1395 | s->shared_sigalgs = NULL; | |
1396 | s->shared_sigalgslen = 0; | |
9195ddcd DSH |
1397 | /* Clear certificate validity flags */ |
1398 | for (i = 0; i < SSL_PKEY_NUM; i++) | |
555cbb32 | 1399 | s->s3.tmp.valid_flags[i] = 0; |
a8bb912d DSH |
1400 | /* |
1401 | * If peer sent no signature algorithms check to see if we support | |
1402 | * the default algorithm for each certificate type | |
1403 | */ | |
555cbb32 TS |
1404 | if (s->s3.tmp.peer_cert_sigalgs == NULL |
1405 | && s->s3.tmp.peer_sigalgs == NULL) { | |
a8bb912d DSH |
1406 | const uint16_t *sent_sigs; |
1407 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
0f113f3e | 1408 | |
a8bb912d DSH |
1409 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
1410 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); | |
1411 | size_t j; | |
1412 | ||
1413 | if (lu == NULL) | |
1414 | continue; | |
1415 | /* Check default matches a type we sent */ | |
1416 | for (j = 0; j < sent_sigslen; j++) { | |
1417 | if (lu->sigalg == sent_sigs[j]) { | |
555cbb32 | 1418 | s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN; |
a8bb912d DSH |
1419 | break; |
1420 | } | |
1421 | } | |
1422 | } | |
9195ddcd | 1423 | return 1; |
a8bb912d | 1424 | } |
9195ddcd DSH |
1425 | |
1426 | if (!tls1_process_sigalgs(s)) { | |
f63a17d6 MC |
1427 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
1428 | SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR); | |
1429 | return 0; | |
d376e57d | 1430 | } |
29948ac8 | 1431 | if (s->shared_sigalgs != NULL) |
9195ddcd | 1432 | return 1; |
f63a17d6 | 1433 | |
fb34a0f4 | 1434 | /* Fatal error if no shared signature algorithms */ |
f63a17d6 MC |
1435 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS, |
1436 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); | |
0f113f3e MC |
1437 | return 0; |
1438 | } | |
e469af8d | 1439 | |
1d97c843 | 1440 | /*- |
1ab3836b | 1441 | * Gets the ticket information supplied by the client if any. |
e7f0d921 | 1442 | * |
1ab3836b | 1443 | * hello: The parsed ClientHello data |
c519e89f BM |
1444 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
1445 | * point to the resulting session. | |
6434abbf | 1446 | */ |
61fb5923 | 1447 | SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
df0fed9a | 1448 | SSL_SESSION **ret) |
0f113f3e | 1449 | { |
1ab3836b MC |
1450 | size_t size; |
1451 | RAW_EXTENSION *ticketext; | |
e7f0d921 | 1452 | |
0f113f3e | 1453 | *ret = NULL; |
aff8c126 | 1454 | s->ext.ticket_expected = 0; |
0f113f3e MC |
1455 | |
1456 | /* | |
9362c93e MC |
1457 | * If tickets disabled or not supported by the protocol version |
1458 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful | |
0f113f3e MC |
1459 | * resumption. |
1460 | */ | |
1ab3836b | 1461 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
df0fed9a | 1462 | return SSL_TICKET_NONE; |
9ceb2426 | 1463 | |
70af3d8e MC |
1464 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
1465 | if (!ticketext->present) | |
df0fed9a | 1466 | return SSL_TICKET_NONE; |
1ab3836b MC |
1467 | |
1468 | size = PACKET_remaining(&ticketext->data); | |
70af3d8e | 1469 | |
c0638ade | 1470 | return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
70af3d8e | 1471 | hello->session_id, hello->session_id_len, ret); |
1ab3836b MC |
1472 | } |
1473 | ||
1d97c843 TH |
1474 | /*- |
1475 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f | 1476 | * |
61fb5923 MC |
1477 | * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are |
1478 | * expecting a pre-shared key ciphersuite, in which case we have no use for | |
1479 | * session tickets and one will never be decrypted, nor will | |
1480 | * s->ext.ticket_expected be set to 1. | |
1481 | * | |
1482 | * Side effects: | |
1483 | * Sets s->ext.ticket_expected to 1 if the server will have to issue | |
1484 | * a new session ticket to the client because the client indicated support | |
1485 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
1486 | * a session ticket or we couldn't use the one it gave us, or if | |
1487 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. | |
1488 | * Otherwise, s->ext.ticket_expected is set to 0. | |
1489 | * | |
c519e89f | 1490 | * etick: points to the body of the session ticket extension. |
8483a003 | 1491 | * eticklen: the length of the session tickets extension. |
c519e89f BM |
1492 | * sess_id: points at the session ID. |
1493 | * sesslen: the length of the session ID. | |
1494 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
1495 | * point to the resulting session. | |
c519e89f | 1496 | */ |
61fb5923 | 1497 | SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
df0fed9a TS |
1498 | size_t eticklen, const unsigned char *sess_id, |
1499 | size_t sesslen, SSL_SESSION **psess) | |
0f113f3e | 1500 | { |
61fb5923 | 1501 | SSL_SESSION *sess = NULL; |
0f113f3e MC |
1502 | unsigned char *sdec; |
1503 | const unsigned char *p; | |
ddf6ec00 | 1504 | int slen, renew_ticket = 0, declen; |
61fb5923 | 1505 | SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; |
348240c6 | 1506 | size_t mlen; |
0f113f3e | 1507 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
a76ce286 | 1508 | SSL_HMAC *hctx = NULL; |
ee763495 | 1509 | EVP_CIPHER_CTX *ctx = NULL; |
222da979 | 1510 | SSL_CTX *tctx = s->session_ctx; |
e97763c9 | 1511 | |
61fb5923 MC |
1512 | if (eticklen == 0) { |
1513 | /* | |
1514 | * The client will accept a ticket but doesn't currently have | |
1515 | * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 | |
1516 | */ | |
1517 | ret = SSL_TICKET_EMPTY; | |
1518 | goto end; | |
1519 | } | |
1520 | if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) { | |
1521 | /* | |
1522 | * Indicate that the ticket couldn't be decrypted rather than | |
1523 | * generating the session from ticket now, trigger | |
1524 | * abbreviated handshake based on external mechanism to | |
1525 | * calculate the master secret later. | |
1526 | */ | |
1527 | ret = SSL_TICKET_NO_DECRYPT; | |
1528 | goto end; | |
1529 | } | |
1530 | ||
ee763495 MC |
1531 | /* Need at least keyname + iv */ |
1532 | if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { | |
df0fed9a | 1533 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1534 | goto end; |
ee763495 MC |
1535 | } |
1536 | ||
0f113f3e | 1537 | /* Initialize session ticket encryption and HMAC contexts */ |
a76ce286 | 1538 | hctx = ssl_hmac_new(tctx); |
c0638ade MC |
1539 | if (hctx == NULL) { |
1540 | ret = SSL_TICKET_FATAL_ERR_MALLOC; | |
1541 | goto end; | |
1542 | } | |
846ec07d | 1543 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 | 1544 | if (ctx == NULL) { |
df0fed9a | 1545 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
c0638ade | 1546 | goto end; |
35b1a433 | 1547 | } |
a76ce286 P |
1548 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
1549 | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) | |
1550 | #else | |
1551 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
1552 | #endif | |
1553 | { | |
0f113f3e | 1554 | unsigned char *nctick = (unsigned char *)etick; |
a76ce286 P |
1555 | int rv = 0; |
1556 | ||
1557 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
1558 | rv = tctx->ext.ticket_key_evp_cb(s, nctick, | |
1559 | nctick + TLSEXT_KEYNAME_LENGTH, | |
1560 | ctx, | |
1561 | ssl_hmac_get0_EVP_MAC_CTX(hctx), | |
1562 | 0); | |
1563 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
1564 | else if (tctx->ext.ticket_key_cb != NULL) | |
1565 | /* if 0 is returned, write an empty ticket */ | |
1566 | rv = tctx->ext.ticket_key_cb(s, nctick, | |
ee763495 | 1567 | nctick + TLSEXT_KEYNAME_LENGTH, |
a76ce286 P |
1568 | ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0); |
1569 | #endif | |
c0638ade MC |
1570 | if (rv < 0) { |
1571 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1572 | goto end; | |
1573 | } | |
35b1a433 | 1574 | if (rv == 0) { |
df0fed9a | 1575 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1576 | goto end; |
35b1a433 | 1577 | } |
0f113f3e MC |
1578 | if (rv == 2) |
1579 | renew_ticket = 1; | |
1580 | } else { | |
148bfd26 MC |
1581 | EVP_CIPHER *aes256cbc = NULL; |
1582 | ||
0f113f3e | 1583 | /* Check key name matches */ |
aff8c126 | 1584 | if (memcmp(etick, tctx->ext.tick_key_name, |
ee763495 | 1585 | TLSEXT_KEYNAME_LENGTH) != 0) { |
df0fed9a | 1586 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1587 | goto end; |
35b1a433 | 1588 | } |
148bfd26 MC |
1589 | |
1590 | aes256cbc = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC", | |
1591 | s->ctx->propq); | |
1592 | if (aes256cbc == NULL | |
1593 | || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, | |
1594 | sizeof(tctx->ext.secure->tick_hmac_key), | |
1595 | "SHA256") <= 0 | |
1596 | || EVP_DecryptInit_ex(ctx, aes256cbc, NULL, | |
4bfb96f2 | 1597 | tctx->ext.secure->tick_aes_key, |
ee763495 | 1598 | etick + TLSEXT_KEYNAME_LENGTH) <= 0) { |
148bfd26 | 1599 | EVP_CIPHER_free(aes256cbc); |
c0638ade MC |
1600 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1601 | goto end; | |
a230b26e | 1602 | } |
148bfd26 | 1603 | EVP_CIPHER_free(aes256cbc); |
c0638ade MC |
1604 | if (SSL_IS_TLS13(s)) |
1605 | renew_ticket = 1; | |
0f113f3e MC |
1606 | } |
1607 | /* | |
1608 | * Attempt to process session ticket, first conduct sanity and integrity | |
1609 | * checks on ticket. | |
1610 | */ | |
a76ce286 | 1611 | mlen = ssl_hmac_size(hctx); |
348240c6 | 1612 | if (mlen == 0) { |
c0638ade MC |
1613 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1614 | goto end; | |
0f113f3e | 1615 | } |
c0638ade | 1616 | |
e97763c9 DSH |
1617 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
1618 | if (eticklen <= | |
348240c6 | 1619 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
df0fed9a | 1620 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1621 | goto end; |
e97763c9 | 1622 | } |
0f113f3e MC |
1623 | eticklen -= mlen; |
1624 | /* Check HMAC of encrypted ticket */ | |
a76ce286 P |
1625 | if (ssl_hmac_update(hctx, etick, eticklen) <= 0 |
1626 | || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { | |
c0638ade MC |
1627 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1628 | goto end; | |
5f3d93e4 | 1629 | } |
c0638ade | 1630 | |
0f113f3e | 1631 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
c0638ade MC |
1632 | ret = SSL_TICKET_NO_DECRYPT; |
1633 | goto end; | |
0f113f3e MC |
1634 | } |
1635 | /* Attempt to decrypt session data */ | |
1636 | /* Move p after IV to start of encrypted ticket, update length */ | |
57b0d651 BE |
1637 | p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
1638 | eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); | |
0f113f3e | 1639 | sdec = OPENSSL_malloc(eticklen); |
348240c6 MC |
1640 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
1641 | (int)eticklen) <= 0) { | |
d1247df2 | 1642 | OPENSSL_free(sdec); |
c0638ade MC |
1643 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1644 | goto end; | |
0f113f3e | 1645 | } |
348240c6 | 1646 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
0f113f3e | 1647 | OPENSSL_free(sdec); |
c0638ade MC |
1648 | ret = SSL_TICKET_NO_DECRYPT; |
1649 | goto end; | |
0f113f3e | 1650 | } |
348240c6 | 1651 | slen += declen; |
0f113f3e MC |
1652 | p = sdec; |
1653 | ||
1654 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
d3bc9805 | 1655 | slen -= p - sdec; |
0f113f3e MC |
1656 | OPENSSL_free(sdec); |
1657 | if (sess) { | |
79020b27 | 1658 | /* Some additional consistency checks */ |
32305f88 | 1659 | if (slen != 0) { |
79020b27 | 1660 | SSL_SESSION_free(sess); |
5f96a95e | 1661 | sess = NULL; |
c0638ade MC |
1662 | ret = SSL_TICKET_NO_DECRYPT; |
1663 | goto end; | |
79020b27 | 1664 | } |
0f113f3e MC |
1665 | /* |
1666 | * The session ID, if non-empty, is used by some clients to detect | |
1667 | * that the ticket has been accepted. So we copy it to the session | |
1668 | * structure. If it is empty set length to zero as required by | |
1669 | * standard. | |
1670 | */ | |
32305f88 | 1671 | if (sesslen) { |
0f113f3e | 1672 | memcpy(sess->session_id, sess_id, sesslen); |
32305f88 MC |
1673 | sess->session_id_length = sesslen; |
1674 | } | |
0f113f3e | 1675 | if (renew_ticket) |
c0638ade | 1676 | ret = SSL_TICKET_SUCCESS_RENEW; |
0f113f3e | 1677 | else |
c0638ade MC |
1678 | ret = SSL_TICKET_SUCCESS; |
1679 | goto end; | |
0f113f3e MC |
1680 | } |
1681 | ERR_clear_error(); | |
1682 | /* | |
1683 | * For session parse failure, indicate that we need to send a new ticket. | |
1684 | */ | |
c0638ade MC |
1685 | ret = SSL_TICKET_NO_DECRYPT; |
1686 | ||
1687 | end: | |
846ec07d | 1688 | EVP_CIPHER_CTX_free(ctx); |
a76ce286 | 1689 | ssl_hmac_free(hctx); |
c0638ade MC |
1690 | |
1691 | /* | |
61fb5923 MC |
1692 | * If set, the decrypt_ticket_cb() is called unless a fatal error was |
1693 | * detected above. The callback is responsible for checking |ret| before it | |
1694 | * performs any action | |
c0638ade | 1695 | */ |
61fb5923 MC |
1696 | if (s->session_ctx->decrypt_ticket_cb != NULL |
1697 | && (ret == SSL_TICKET_EMPTY | |
1698 | || ret == SSL_TICKET_NO_DECRYPT | |
1699 | || ret == SSL_TICKET_SUCCESS | |
1700 | || ret == SSL_TICKET_SUCCESS_RENEW)) { | |
c0638ade | 1701 | size_t keyname_len = eticklen; |
61fb5923 | 1702 | int retcb; |
c0638ade MC |
1703 | |
1704 | if (keyname_len > TLSEXT_KEYNAME_LENGTH) | |
1705 | keyname_len = TLSEXT_KEYNAME_LENGTH; | |
61fb5923 MC |
1706 | retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len, |
1707 | ret, | |
1708 | s->session_ctx->ticket_cb_data); | |
1709 | switch (retcb) { | |
1710 | case SSL_TICKET_RETURN_ABORT: | |
1711 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1712 | break; | |
1713 | ||
1714 | case SSL_TICKET_RETURN_IGNORE: | |
1715 | ret = SSL_TICKET_NONE; | |
1716 | SSL_SESSION_free(sess); | |
1717 | sess = NULL; | |
1718 | break; | |
1719 | ||
1720 | case SSL_TICKET_RETURN_IGNORE_RENEW: | |
1721 | if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) | |
1722 | ret = SSL_TICKET_NO_DECRYPT; | |
1723 | /* else the value of |ret| will already do the right thing */ | |
1724 | SSL_SESSION_free(sess); | |
1725 | sess = NULL; | |
1726 | break; | |
1727 | ||
1728 | case SSL_TICKET_RETURN_USE: | |
1729 | case SSL_TICKET_RETURN_USE_RENEW: | |
1730 | if (ret != SSL_TICKET_SUCCESS | |
1731 | && ret != SSL_TICKET_SUCCESS_RENEW) | |
1732 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1733 | else if (retcb == SSL_TICKET_RETURN_USE) | |
1734 | ret = SSL_TICKET_SUCCESS; | |
1735 | else | |
1736 | ret = SSL_TICKET_SUCCESS_RENEW; | |
1737 | break; | |
1738 | ||
1739 | default: | |
1740 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1741 | } | |
c0638ade MC |
1742 | } |
1743 | ||
309371d6 MC |
1744 | if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) { |
1745 | switch (ret) { | |
1746 | case SSL_TICKET_NO_DECRYPT: | |
1747 | case SSL_TICKET_SUCCESS_RENEW: | |
1748 | case SSL_TICKET_EMPTY: | |
1749 | s->ext.ticket_expected = 1; | |
1750 | } | |
c0638ade MC |
1751 | } |
1752 | ||
61fb5923 MC |
1753 | *psess = sess; |
1754 | ||
1755 | return ret; | |
0f113f3e | 1756 | } |
6434abbf | 1757 | |
b362ccab | 1758 | /* Check to see if a signature algorithm is allowed */ |
b0031e5d | 1759 | static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu) |
0f113f3e | 1760 | { |
703bcee0 | 1761 | unsigned char sigalgstr[2]; |
44b6318f | 1762 | int secbits; |
703bcee0 | 1763 | |
44b6318f | 1764 | /* See if sigalgs is recognised and if hash is enabled */ |
c8f6c28a | 1765 | if (!tls1_lookup_md(s->ctx, lu, NULL)) |
0f113f3e | 1766 | return 0; |
224b4e37 DSH |
1767 | /* DSA is not allowed in TLS 1.3 */ |
1768 | if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) | |
1769 | return 0; | |
6ffeb269 | 1770 | /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */ |
555cbb32 | 1771 | if (!s->server && !SSL_IS_DTLS(s) && s->s3.tmp.min_ver >= TLS1_3_VERSION |
6ffeb269 BK |
1772 | && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX |
1773 | || lu->hash_idx == SSL_MD_MD5_IDX | |
1774 | || lu->hash_idx == SSL_MD_SHA224_IDX)) | |
1775 | return 0; | |
871980a9 | 1776 | |
0f113f3e | 1777 | /* See if public key algorithm allowed */ |
b8858aec | 1778 | if (ssl_cert_is_disabled(lu->sig_idx)) |
0f113f3e | 1779 | return 0; |
871980a9 MC |
1780 | |
1781 | if (lu->sig == NID_id_GostR3410_2012_256 | |
1782 | || lu->sig == NID_id_GostR3410_2012_512 | |
1783 | || lu->sig == NID_id_GostR3410_2001) { | |
1784 | /* We never allow GOST sig algs on the server with TLSv1.3 */ | |
1785 | if (s->server && SSL_IS_TLS13(s)) | |
1786 | return 0; | |
1787 | if (!s->server | |
1788 | && s->method->version == TLS_ANY_VERSION | |
555cbb32 | 1789 | && s->s3.tmp.max_ver >= TLS1_3_VERSION) { |
871980a9 MC |
1790 | int i, num; |
1791 | STACK_OF(SSL_CIPHER) *sk; | |
1792 | ||
1793 | /* | |
1794 | * We're a client that could negotiate TLSv1.3. We only allow GOST | |
1795 | * sig algs if we could negotiate TLSv1.2 or below and we have GOST | |
1796 | * ciphersuites enabled. | |
1797 | */ | |
1798 | ||
555cbb32 | 1799 | if (s->s3.tmp.min_ver >= TLS1_3_VERSION) |
871980a9 MC |
1800 | return 0; |
1801 | ||
1802 | sk = SSL_get_ciphers(s); | |
1803 | num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; | |
1804 | for (i = 0; i < num; i++) { | |
1805 | const SSL_CIPHER *c; | |
1806 | ||
1807 | c = sk_SSL_CIPHER_value(sk, i); | |
1808 | /* Skip disabled ciphers */ | |
1809 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) | |
1810 | continue; | |
1811 | ||
5a5530a2 | 1812 | if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0) |
871980a9 MC |
1813 | break; |
1814 | } | |
1815 | if (i == num) | |
1816 | return 0; | |
1817 | } | |
1818 | } | |
1819 | ||
0f113f3e | 1820 | /* Finally see if security callback allows it */ |
620c97b6 | 1821 | secbits = sigalg_security_bits(s->ctx, lu); |
b0e9ab95 DSH |
1822 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
1823 | sigalgstr[1] = lu->sigalg & 0xff; | |
44b6318f | 1824 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
0f113f3e MC |
1825 | } |
1826 | ||
1827 | /* | |
1828 | * Get a mask of disabled public key algorithms based on supported signature | |
1829 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
1830 | * disabled. | |
b362ccab DSH |
1831 | */ |
1832 | ||
90d9e49a | 1833 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
0f113f3e | 1834 | { |
98c792d1 | 1835 | const uint16_t *sigalgs; |
0f113f3e | 1836 | size_t i, sigalgslen; |
13cc2574 | 1837 | uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; |
0f113f3e | 1838 | /* |
13cc2574 DSH |
1839 | * Go through all signature algorithms seeing if we support any |
1840 | * in disabled_mask. | |
0f113f3e | 1841 | */ |
a9669ddc | 1842 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
b2555168 | 1843 | for (i = 0; i < sigalgslen; i++, sigalgs++) { |
b0e9ab95 | 1844 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs); |
13cc2574 | 1845 | const SSL_CERT_LOOKUP *clu; |
b0e9ab95 DSH |
1846 | |
1847 | if (lu == NULL) | |
1848 | continue; | |
13cc2574 DSH |
1849 | |
1850 | clu = ssl_cert_lookup_by_idx(lu->sig_idx); | |
dd6b2706 P |
1851 | if (clu == NULL) |
1852 | continue; | |
13cc2574 DSH |
1853 | |
1854 | /* If algorithm is disabled see if we can enable it */ | |
1855 | if ((clu->amask & disabled_mask) != 0 | |
1856 | && tls12_sigalg_allowed(s, op, lu)) | |
1857 | disabled_mask &= ~clu->amask; | |
0f113f3e | 1858 | } |
13cc2574 | 1859 | *pmask_a |= disabled_mask; |
0f113f3e | 1860 | } |
b362ccab | 1861 | |
ae2f7b37 | 1862 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
98c792d1 | 1863 | const uint16_t *psig, size_t psiglen) |
2c7b4dbc MC |
1864 | { |
1865 | size_t i; | |
b0e9ab95 | 1866 | int rv = 0; |
c0f9e23c | 1867 | |
703bcee0 | 1868 | for (i = 0; i < psiglen; i++, psig++) { |
b0e9ab95 DSH |
1869 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig); |
1870 | ||
1871 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) | |
1872 | continue; | |
1873 | if (!WPACKET_put_bytes_u16(pkt, *psig)) | |
1874 | return 0; | |
1875 | /* | |
1876 | * If TLS 1.3 must have at least one valid TLS 1.3 message | |
d8311fc9 | 1877 | * signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
b0e9ab95 DSH |
1878 | */ |
1879 | if (rv == 0 && (!SSL_IS_TLS13(s) | |
d8311fc9 MC |
1880 | || (lu->sig != EVP_PKEY_RSA |
1881 | && lu->hash != NID_sha1 | |
1882 | && lu->hash != NID_sha224))) | |
b0e9ab95 | 1883 | rv = 1; |
2c7b4dbc | 1884 | } |
5528d68f DSH |
1885 | if (rv == 0) |
1886 | SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
b0e9ab95 | 1887 | return rv; |
2c7b4dbc MC |
1888 | } |
1889 | ||
4453cd8c | 1890 | /* Given preference and allowed sigalgs set shared sigalgs */ |
4d43ee28 | 1891 | static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig, |
98c792d1 DSH |
1892 | const uint16_t *pref, size_t preflen, |
1893 | const uint16_t *allow, size_t allowlen) | |
0f113f3e | 1894 | { |
98c792d1 | 1895 | const uint16_t *ptmp, *atmp; |
0f113f3e | 1896 | size_t i, j, nmatch = 0; |
703bcee0 | 1897 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
b0e9ab95 DSH |
1898 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp); |
1899 | ||
0f113f3e | 1900 | /* Skip disabled hashes or signature algorithms */ |
b0e9ab95 | 1901 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
0f113f3e | 1902 | continue; |
703bcee0 MC |
1903 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
1904 | if (*ptmp == *atmp) { | |
0f113f3e | 1905 | nmatch++; |
b0e9ab95 DSH |
1906 | if (shsig) |
1907 | *shsig++ = lu; | |
0f113f3e MC |
1908 | break; |
1909 | } | |
1910 | } | |
1911 | } | |
1912 | return nmatch; | |
1913 | } | |
4453cd8c DSH |
1914 | |
1915 | /* Set shared signature algorithms for SSL structures */ | |
1916 | static int tls1_set_shared_sigalgs(SSL *s) | |
0f113f3e | 1917 | { |
98c792d1 | 1918 | const uint16_t *pref, *allow, *conf; |
0f113f3e MC |
1919 | size_t preflen, allowlen, conflen; |
1920 | size_t nmatch; | |
4d43ee28 | 1921 | const SIGALG_LOOKUP **salgs = NULL; |
0f113f3e MC |
1922 | CERT *c = s->cert; |
1923 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 | 1924 | |
29948ac8 BK |
1925 | OPENSSL_free(s->shared_sigalgs); |
1926 | s->shared_sigalgs = NULL; | |
1927 | s->shared_sigalgslen = 0; | |
0f113f3e MC |
1928 | /* If client use client signature algorithms if not NULL */ |
1929 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
1930 | conf = c->client_sigalgs; | |
1931 | conflen = c->client_sigalgslen; | |
1932 | } else if (c->conf_sigalgs && !is_suiteb) { | |
1933 | conf = c->conf_sigalgs; | |
1934 | conflen = c->conf_sigalgslen; | |
1935 | } else | |
a9669ddc | 1936 | conflen = tls12_get_psigalgs(s, 0, &conf); |
0f113f3e MC |
1937 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
1938 | pref = conf; | |
1939 | preflen = conflen; | |
555cbb32 TS |
1940 | allow = s->s3.tmp.peer_sigalgs; |
1941 | allowlen = s->s3.tmp.peer_sigalgslen; | |
0f113f3e MC |
1942 | } else { |
1943 | allow = conf; | |
1944 | allowlen = conflen; | |
555cbb32 TS |
1945 | pref = s->s3.tmp.peer_sigalgs; |
1946 | preflen = s->s3.tmp.peer_sigalgslen; | |
0f113f3e MC |
1947 | } |
1948 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf | 1949 | if (nmatch) { |
cdb10bae RS |
1950 | if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) { |
1951 | SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE); | |
34e3edbf | 1952 | return 0; |
cdb10bae | 1953 | } |
34e3edbf DSH |
1954 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
1955 | } else { | |
1956 | salgs = NULL; | |
1957 | } | |
29948ac8 BK |
1958 | s->shared_sigalgs = salgs; |
1959 | s->shared_sigalgslen = nmatch; | |
0f113f3e MC |
1960 | return 1; |
1961 | } | |
4453cd8c | 1962 | |
9e84a42d | 1963 | int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) |
0f113f3e | 1964 | { |
98c792d1 | 1965 | unsigned int stmp; |
703bcee0 | 1966 | size_t size, i; |
9e84a42d | 1967 | uint16_t *buf; |
0f113f3e | 1968 | |
703bcee0 MC |
1969 | size = PACKET_remaining(pkt); |
1970 | ||
1971 | /* Invalid data length */ | |
8f12296e | 1972 | if (size == 0 || (size & 1) != 0) |
703bcee0 MC |
1973 | return 0; |
1974 | ||
1975 | size >>= 1; | |
1976 | ||
cdb10bae RS |
1977 | if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) { |
1978 | SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 1979 | return 0; |
cdb10bae | 1980 | } |
98c792d1 | 1981 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
9e84a42d | 1982 | buf[i] = stmp; |
703bcee0 | 1983 | |
9e84a42d DSH |
1984 | if (i != size) { |
1985 | OPENSSL_free(buf); | |
703bcee0 | 1986 | return 0; |
9e84a42d DSH |
1987 | } |
1988 | ||
1989 | OPENSSL_free(*pdest); | |
1990 | *pdest = buf; | |
1991 | *pdestlen = size; | |
703bcee0 | 1992 | |
0f113f3e MC |
1993 | return 1; |
1994 | } | |
6b7be581 | 1995 | |
c589c34e | 1996 | int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert) |
9e84a42d DSH |
1997 | { |
1998 | /* Extension ignored for inappropriate versions */ | |
1999 | if (!SSL_USE_SIGALGS(s)) | |
2000 | return 1; | |
2001 | /* Should never happen */ | |
2002 | if (s->cert == NULL) | |
2003 | return 0; | |
2004 | ||
c589c34e | 2005 | if (cert) |
555cbb32 TS |
2006 | return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs, |
2007 | &s->s3.tmp.peer_cert_sigalgslen); | |
c589c34e | 2008 | else |
555cbb32 TS |
2009 | return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs, |
2010 | &s->s3.tmp.peer_sigalgslen); | |
9e84a42d | 2011 | |
9e84a42d DSH |
2012 | } |
2013 | ||
2014 | /* Set preferred digest for each key type */ | |
2015 | ||
c800c27a | 2016 | int tls1_process_sigalgs(SSL *s) |
0f113f3e | 2017 | { |
0f113f3e | 2018 | size_t i; |
555cbb32 | 2019 | uint32_t *pvalid = s->s3.tmp.valid_flags; |
4d43ee28 | 2020 | |
0f113f3e MC |
2021 | if (!tls1_set_shared_sigalgs(s)) |
2022 | return 0; | |
2023 | ||
9195ddcd DSH |
2024 | for (i = 0; i < SSL_PKEY_NUM; i++) |
2025 | pvalid[i] = 0; | |
2026 | ||
29948ac8 BK |
2027 | for (i = 0; i < s->shared_sigalgslen; i++) { |
2028 | const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i]; | |
9195ddcd | 2029 | int idx = sigptr->sig_idx; |
4d43ee28 | 2030 | |
523fb323 | 2031 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
4d43ee28 | 2032 | if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
523fb323 | 2033 | continue; |
9195ddcd | 2034 | /* If not disabled indicate we can explicitly sign */ |
b8858aec DSH |
2035 | if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx)) |
2036 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; | |
0f113f3e MC |
2037 | } |
2038 | return 1; | |
2039 | } | |
4817504d | 2040 | |
e7f8ff43 | 2041 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
2042 | int *psign, int *phash, int *psignhash, |
2043 | unsigned char *rsig, unsigned char *rhash) | |
2044 | { | |
555cbb32 TS |
2045 | uint16_t *psig = s->s3.tmp.peer_sigalgs; |
2046 | size_t numsigalgs = s->s3.tmp.peer_sigalgslen; | |
348240c6 | 2047 | if (psig == NULL || numsigalgs > INT_MAX) |
0f113f3e MC |
2048 | return 0; |
2049 | if (idx >= 0) { | |
4d43ee28 DSH |
2050 | const SIGALG_LOOKUP *lu; |
2051 | ||
703bcee0 | 2052 | if (idx >= (int)numsigalgs) |
0f113f3e MC |
2053 | return 0; |
2054 | psig += idx; | |
4d43ee28 | 2055 | if (rhash != NULL) |
536199ec | 2056 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
4d43ee28 | 2057 | if (rsig != NULL) |
536199ec | 2058 | *rsig = (unsigned char)(*psig & 0xff); |
4d43ee28 DSH |
2059 | lu = tls1_lookup_sigalg(*psig); |
2060 | if (psign != NULL) | |
2061 | *psign = lu != NULL ? lu->sig : NID_undef; | |
2062 | if (phash != NULL) | |
2063 | *phash = lu != NULL ? lu->hash : NID_undef; | |
2064 | if (psignhash != NULL) | |
2065 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; | |
0f113f3e | 2066 | } |
348240c6 | 2067 | return (int)numsigalgs; |
0f113f3e | 2068 | } |
4453cd8c DSH |
2069 | |
2070 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
2071 | int *psign, int *phash, int *psignhash, |
2072 | unsigned char *rsig, unsigned char *rhash) | |
2073 | { | |
4d43ee28 | 2074 | const SIGALG_LOOKUP *shsigalgs; |
29948ac8 | 2075 | if (s->shared_sigalgs == NULL |
6d047e06 | 2076 | || idx < 0 |
29948ac8 BK |
2077 | || idx >= (int)s->shared_sigalgslen |
2078 | || s->shared_sigalgslen > INT_MAX) | |
0f113f3e | 2079 | return 0; |
29948ac8 | 2080 | shsigalgs = s->shared_sigalgs[idx]; |
4d43ee28 DSH |
2081 | if (phash != NULL) |
2082 | *phash = shsigalgs->hash; | |
2083 | if (psign != NULL) | |
2084 | *psign = shsigalgs->sig; | |
2085 | if (psignhash != NULL) | |
2086 | *psignhash = shsigalgs->sigandhash; | |
2087 | if (rsig != NULL) | |
2088 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); | |
2089 | if (rhash != NULL) | |
2090 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); | |
29948ac8 | 2091 | return (int)s->shared_sigalgslen; |
0f113f3e MC |
2092 | } |
2093 | ||
787ebcaf DSH |
2094 | /* Maximum possible number of unique entries in sigalgs array */ |
2095 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) | |
0f229cce | 2096 | |
0f113f3e MC |
2097 | typedef struct { |
2098 | size_t sigalgcnt; | |
fd5e1a8c BK |
2099 | /* TLSEXT_SIGALG_XXX values */ |
2100 | uint16_t sigalgs[TLS_MAX_SIGALGCNT]; | |
0f113f3e | 2101 | } sig_cb_st; |
0f229cce | 2102 | |
431f458d DSH |
2103 | static void get_sigorhash(int *psig, int *phash, const char *str) |
2104 | { | |
2105 | if (strcmp(str, "RSA") == 0) { | |
2106 | *psig = EVP_PKEY_RSA; | |
b2eb6998 DSH |
2107 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
2108 | *psig = EVP_PKEY_RSA_PSS; | |
431f458d DSH |
2109 | } else if (strcmp(str, "DSA") == 0) { |
2110 | *psig = EVP_PKEY_DSA; | |
2111 | } else if (strcmp(str, "ECDSA") == 0) { | |
2112 | *psig = EVP_PKEY_EC; | |
2113 | } else { | |
2114 | *phash = OBJ_sn2nid(str); | |
2115 | if (*phash == NID_undef) | |
2116 | *phash = OBJ_ln2nid(str); | |
2117 | } | |
2118 | } | |
787ebcaf DSH |
2119 | /* Maximum length of a signature algorithm string component */ |
2120 | #define TLS_MAX_SIGSTRING_LEN 40 | |
431f458d | 2121 | |
0f229cce | 2122 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
2123 | { |
2124 | sig_cb_st *sarg = arg; | |
2125 | size_t i; | |
fd5e1a8c | 2126 | const SIGALG_LOOKUP *s; |
787ebcaf | 2127 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
431f458d | 2128 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
2129 | if (elem == NULL) |
2130 | return 0; | |
787ebcaf | 2131 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
0f113f3e MC |
2132 | return 0; |
2133 | if (len > (int)(sizeof(etmp) - 1)) | |
2134 | return 0; | |
2135 | memcpy(etmp, elem, len); | |
2136 | etmp[len] = 0; | |
2137 | p = strchr(etmp, '+'); | |
fd5e1a8c BK |
2138 | /* |
2139 | * We only allow SignatureSchemes listed in the sigalg_lookup_tbl; | |
2140 | * if there's no '+' in the provided name, look for the new-style combined | |
2141 | * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. | |
2142 | * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and | |
2143 | * rsa_pss_rsae_* that differ only by public key OID; in such cases | |
2144 | * we will pick the _rsae_ variant, by virtue of them appearing earlier | |
2145 | * in the table. | |
2146 | */ | |
8a43a42a | 2147 | if (p == NULL) { |
8a43a42a DSH |
2148 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
2149 | i++, s++) { | |
2150 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { | |
fd5e1a8c | 2151 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
8a43a42a DSH |
2152 | break; |
2153 | } | |
2154 | } | |
fd5e1a8c BK |
2155 | if (i == OSSL_NELEM(sigalg_lookup_tbl)) |
2156 | return 0; | |
8a43a42a DSH |
2157 | } else { |
2158 | *p = 0; | |
2159 | p++; | |
2160 | if (*p == 0) | |
2161 | return 0; | |
2162 | get_sigorhash(&sig_alg, &hash_alg, etmp); | |
2163 | get_sigorhash(&sig_alg, &hash_alg, p); | |
fd5e1a8c BK |
2164 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
2165 | return 0; | |
2166 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); | |
2167 | i++, s++) { | |
2168 | if (s->hash == hash_alg && s->sig == sig_alg) { | |
2169 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; | |
2170 | break; | |
2171 | } | |
2172 | } | |
2173 | if (i == OSSL_NELEM(sigalg_lookup_tbl)) | |
2174 | return 0; | |
8a43a42a | 2175 | } |
0f113f3e | 2176 | |
fd5e1a8c BK |
2177 | /* Reject duplicates */ |
2178 | for (i = 0; i < sarg->sigalgcnt - 1; i++) { | |
c1acef92 | 2179 | if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { |
fd5e1a8c | 2180 | sarg->sigalgcnt--; |
0f113f3e | 2181 | return 0; |
fd5e1a8c | 2182 | } |
0f113f3e | 2183 | } |
0f113f3e MC |
2184 | return 1; |
2185 | } | |
2186 | ||
2187 | /* | |
9d22666e | 2188 | * Set supported signature algorithms based on a colon separated list of the |
0f113f3e MC |
2189 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
2190 | */ | |
3dbc46df | 2191 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
2192 | { |
2193 | sig_cb_st sig; | |
2194 | sig.sigalgcnt = 0; | |
2195 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
2196 | return 0; | |
2197 | if (c == NULL) | |
2198 | return 1; | |
fd5e1a8c BK |
2199 | return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
2200 | } | |
2201 | ||
2202 | int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, | |
2203 | int client) | |
2204 | { | |
2205 | uint16_t *sigalgs; | |
2206 | ||
cdb10bae RS |
2207 | if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) { |
2208 | SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE); | |
fd5e1a8c | 2209 | return 0; |
cdb10bae | 2210 | } |
fd5e1a8c BK |
2211 | memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); |
2212 | ||
2213 | if (client) { | |
2214 | OPENSSL_free(c->client_sigalgs); | |
2215 | c->client_sigalgs = sigalgs; | |
2216 | c->client_sigalgslen = salglen; | |
2217 | } else { | |
2218 | OPENSSL_free(c->conf_sigalgs); | |
2219 | c->conf_sigalgs = sigalgs; | |
2220 | c->conf_sigalgslen = salglen; | |
2221 | } | |
2222 | ||
2223 | return 1; | |
0f113f3e MC |
2224 | } |
2225 | ||
a230b26e | 2226 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
0f113f3e | 2227 | { |
98c792d1 | 2228 | uint16_t *sigalgs, *sptr; |
0f113f3e | 2229 | size_t i; |
63c1df09 | 2230 | |
0f113f3e MC |
2231 | if (salglen & 1) |
2232 | return 0; | |
cdb10bae RS |
2233 | if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) { |
2234 | SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 2235 | return 0; |
cdb10bae | 2236 | } |
0f113f3e | 2237 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
63c1df09 | 2238 | size_t j; |
7a531ee4 | 2239 | const SIGALG_LOOKUP *curr; |
63c1df09 MC |
2240 | int md_id = *psig_nids++; |
2241 | int sig_id = *psig_nids++; | |
2242 | ||
2243 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); | |
2244 | j++, curr++) { | |
fe3066ee | 2245 | if (curr->hash == md_id && curr->sig == sig_id) { |
63c1df09 MC |
2246 | *sptr++ = curr->sigalg; |
2247 | break; | |
2248 | } | |
2249 | } | |
0f113f3e | 2250 | |
63c1df09 | 2251 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
0f113f3e | 2252 | goto err; |
0f113f3e MC |
2253 | } |
2254 | ||
2255 | if (client) { | |
b548a1f1 | 2256 | OPENSSL_free(c->client_sigalgs); |
0f113f3e | 2257 | c->client_sigalgs = sigalgs; |
7a531ee4 | 2258 | c->client_sigalgslen = salglen / 2; |
0f113f3e | 2259 | } else { |
b548a1f1 | 2260 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e | 2261 | c->conf_sigalgs = sigalgs; |
7a531ee4 | 2262 | c->conf_sigalgslen = salglen / 2; |
0f113f3e MC |
2263 | } |
2264 | ||
2265 | return 1; | |
2266 | ||
2267 | err: | |
2268 | OPENSSL_free(sigalgs); | |
2269 | return 0; | |
2270 | } | |
4453cd8c | 2271 | |
29948ac8 | 2272 | static int tls1_check_sig_alg(SSL *s, X509 *x, int default_nid) |
0f113f3e | 2273 | { |
5235ef44 | 2274 | int sig_nid, use_pc_sigalgs = 0; |
0f113f3e | 2275 | size_t i; |
5235ef44 MC |
2276 | const SIGALG_LOOKUP *sigalg; |
2277 | size_t sigalgslen; | |
0f113f3e MC |
2278 | if (default_nid == -1) |
2279 | return 1; | |
2280 | sig_nid = X509_get_signature_nid(x); | |
2281 | if (default_nid) | |
2282 | return sig_nid == default_nid ? 1 : 0; | |
5235ef44 MC |
2283 | |
2284 | if (SSL_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { | |
2285 | /* | |
2286 | * If we're in TLSv1.3 then we only get here if we're checking the | |
2287 | * chain. If the peer has specified peer_cert_sigalgs then we use them | |
2288 | * otherwise we default to normal sigalgs. | |
2289 | */ | |
2290 | sigalgslen = s->s3.tmp.peer_cert_sigalgslen; | |
2291 | use_pc_sigalgs = 1; | |
2292 | } else { | |
2293 | sigalgslen = s->shared_sigalgslen; | |
2294 | } | |
2295 | for (i = 0; i < sigalgslen; i++) { | |
2296 | sigalg = use_pc_sigalgs | |
2297 | ? tls1_lookup_sigalg(s->s3.tmp.peer_cert_sigalgs[i]) | |
2298 | : s->shared_sigalgs[i]; | |
a87f3fe0 | 2299 | if (sigalg != NULL && sig_nid == sigalg->sigandhash) |
0f113f3e | 2300 | return 1; |
5235ef44 | 2301 | } |
0f113f3e MC |
2302 | return 0; |
2303 | } | |
2304 | ||
6dbb6219 DSH |
2305 | /* Check to see if a certificate issuer name matches list of CA names */ |
2306 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e | 2307 | { |
8cc86b81 | 2308 | const X509_NAME *nm; |
0f113f3e MC |
2309 | int i; |
2310 | nm = X509_get_issuer_name(x); | |
2311 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
2312 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
2313 | return 1; | |
2314 | } | |
2315 | return 0; | |
2316 | } | |
2317 | ||
2318 | /* | |
2319 | * Check certificate chain is consistent with TLS extensions and is usable by | |
2320 | * server. This servers two purposes: it allows users to check chains before | |
2321 | * passing them to the server and it allows the server to check chains before | |
2322 | * attempting to use them. | |
d61ff83b | 2323 | */ |
6dbb6219 | 2324 | |
69687aa8 | 2325 | /* Flags which need to be set for a certificate when strict mode not set */ |
6dbb6219 | 2326 | |
e481f9b9 | 2327 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 2328 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 2329 | /* Strict mode flags */ |
e481f9b9 | 2330 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
2331 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
2332 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 2333 | |
d61ff83b | 2334 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
0f113f3e MC |
2335 | int idx) |
2336 | { | |
2337 | int i; | |
2338 | int rv = 0; | |
2339 | int check_flags = 0, strict_mode; | |
2340 | CERT_PKEY *cpk = NULL; | |
2341 | CERT *c = s->cert; | |
f7d53487 | 2342 | uint32_t *pvalid; |
0f113f3e MC |
2343 | unsigned int suiteb_flags = tls1_suiteb(s); |
2344 | /* idx == -1 means checking server chains */ | |
2345 | if (idx != -1) { | |
2346 | /* idx == -2 means checking client certificate chains */ | |
2347 | if (idx == -2) { | |
2348 | cpk = c->key; | |
348240c6 | 2349 | idx = (int)(cpk - c->pkeys); |
0f113f3e MC |
2350 | } else |
2351 | cpk = c->pkeys + idx; | |
555cbb32 | 2352 | pvalid = s->s3.tmp.valid_flags + idx; |
0f113f3e MC |
2353 | x = cpk->x509; |
2354 | pk = cpk->privatekey; | |
2355 | chain = cpk->chain; | |
2356 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
2357 | /* If no cert or key, forget it */ | |
2358 | if (!x || !pk) | |
2359 | goto end; | |
0f113f3e | 2360 | } else { |
52fd27f9 DSH |
2361 | size_t certidx; |
2362 | ||
0f113f3e | 2363 | if (!x || !pk) |
d813f9eb | 2364 | return 0; |
52fd27f9 DSH |
2365 | |
2366 | if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL) | |
d813f9eb | 2367 | return 0; |
52fd27f9 | 2368 | idx = certidx; |
555cbb32 | 2369 | pvalid = s->s3.tmp.valid_flags + idx; |
6383d316 | 2370 | |
0f113f3e MC |
2371 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
2372 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
2373 | else | |
2374 | check_flags = CERT_PKEY_VALID_FLAGS; | |
2375 | strict_mode = 1; | |
2376 | } | |
2377 | ||
2378 | if (suiteb_flags) { | |
2379 | int ok; | |
2380 | if (check_flags) | |
2381 | check_flags |= CERT_PKEY_SUITEB; | |
2382 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
2383 | if (ok == X509_V_OK) | |
2384 | rv |= CERT_PKEY_SUITEB; | |
2385 | else if (!check_flags) | |
2386 | goto end; | |
2387 | } | |
2388 | ||
2389 | /* | |
2390 | * Check all signature algorithms are consistent with signature | |
2391 | * algorithms extension if TLS 1.2 or later and strict mode. | |
2392 | */ | |
2393 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { | |
2394 | int default_nid; | |
536199ec | 2395 | int rsign = 0; |
555cbb32 TS |
2396 | if (s->s3.tmp.peer_cert_sigalgs != NULL |
2397 | || s->s3.tmp.peer_sigalgs != NULL) { | |
0f113f3e MC |
2398 | default_nid = 0; |
2399 | /* If no sigalgs extension use defaults from RFC5246 */ | |
c589c34e | 2400 | } else { |
0f113f3e | 2401 | switch (idx) { |
d0ff28f8 | 2402 | case SSL_PKEY_RSA: |
536199ec | 2403 | rsign = EVP_PKEY_RSA; |
0f113f3e MC |
2404 | default_nid = NID_sha1WithRSAEncryption; |
2405 | break; | |
2406 | ||
2407 | case SSL_PKEY_DSA_SIGN: | |
536199ec | 2408 | rsign = EVP_PKEY_DSA; |
0f113f3e MC |
2409 | default_nid = NID_dsaWithSHA1; |
2410 | break; | |
2411 | ||
2412 | case SSL_PKEY_ECC: | |
536199ec | 2413 | rsign = EVP_PKEY_EC; |
0f113f3e MC |
2414 | default_nid = NID_ecdsa_with_SHA1; |
2415 | break; | |
2416 | ||
e44380a9 | 2417 | case SSL_PKEY_GOST01: |
536199ec | 2418 | rsign = NID_id_GostR3410_2001; |
e44380a9 DB |
2419 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
2420 | break; | |
2421 | ||
2422 | case SSL_PKEY_GOST12_256: | |
536199ec | 2423 | rsign = NID_id_GostR3410_2012_256; |
e44380a9 DB |
2424 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
2425 | break; | |
2426 | ||
2427 | case SSL_PKEY_GOST12_512: | |
536199ec | 2428 | rsign = NID_id_GostR3410_2012_512; |
e44380a9 DB |
2429 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
2430 | break; | |
2431 | ||
0f113f3e MC |
2432 | default: |
2433 | default_nid = -1; | |
2434 | break; | |
2435 | } | |
2436 | } | |
2437 | /* | |
2438 | * If peer sent no signature algorithms extension and we have set | |
2439 | * preferred signature algorithms check we support sha1. | |
2440 | */ | |
2441 | if (default_nid > 0 && c->conf_sigalgs) { | |
2442 | size_t j; | |
98c792d1 | 2443 | const uint16_t *p = c->conf_sigalgs; |
703bcee0 | 2444 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
44b6318f DSH |
2445 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); |
2446 | ||
2447 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) | |
0f113f3e MC |
2448 | break; |
2449 | } | |
2450 | if (j == c->conf_sigalgslen) { | |
2451 | if (check_flags) | |
2452 | goto skip_sigs; | |
2453 | else | |
2454 | goto end; | |
2455 | } | |
2456 | } | |
2457 | /* Check signature algorithm of each cert in chain */ | |
5235ef44 MC |
2458 | if (SSL_IS_TLS13(s)) { |
2459 | /* | |
2460 | * We only get here if the application has called SSL_check_chain(), | |
2461 | * so check_flags is always set. | |
2462 | */ | |
2463 | if (find_sig_alg(s, x, pk) != NULL) | |
2464 | rv |= CERT_PKEY_EE_SIGNATURE; | |
2465 | } else if (!tls1_check_sig_alg(s, x, default_nid)) { | |
0f113f3e MC |
2466 | if (!check_flags) |
2467 | goto end; | |
2468 | } else | |
2469 | rv |= CERT_PKEY_EE_SIGNATURE; | |
2470 | rv |= CERT_PKEY_CA_SIGNATURE; | |
2471 | for (i = 0; i < sk_X509_num(chain); i++) { | |
29948ac8 | 2472 | if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { |
0f113f3e MC |
2473 | if (check_flags) { |
2474 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
2475 | break; | |
2476 | } else | |
2477 | goto end; | |
2478 | } | |
2479 | } | |
2480 | } | |
2481 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
2482 | else if (check_flags) | |
2483 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
2484 | skip_sigs: | |
2485 | /* Check cert parameters are consistent */ | |
9195ddcd | 2486 | if (tls1_check_cert_param(s, x, 1)) |
0f113f3e MC |
2487 | rv |= CERT_PKEY_EE_PARAM; |
2488 | else if (!check_flags) | |
2489 | goto end; | |
2490 | if (!s->server) | |
2491 | rv |= CERT_PKEY_CA_PARAM; | |
2492 | /* In strict mode check rest of chain too */ | |
2493 | else if (strict_mode) { | |
2494 | rv |= CERT_PKEY_CA_PARAM; | |
2495 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2496 | X509 *ca = sk_X509_value(chain, i); | |
2497 | if (!tls1_check_cert_param(s, ca, 0)) { | |
2498 | if (check_flags) { | |
2499 | rv &= ~CERT_PKEY_CA_PARAM; | |
2500 | break; | |
2501 | } else | |
2502 | goto end; | |
2503 | } | |
2504 | } | |
2505 | } | |
2506 | if (!s->server && strict_mode) { | |
2507 | STACK_OF(X509_NAME) *ca_dn; | |
2508 | int check_type = 0; | |
c2041da8 RL |
2509 | |
2510 | if (EVP_PKEY_is_a(pk, "RSA")) | |
0f113f3e | 2511 | check_type = TLS_CT_RSA_SIGN; |
c2041da8 | 2512 | else if (EVP_PKEY_is_a(pk, "DSA")) |
0f113f3e | 2513 | check_type = TLS_CT_DSS_SIGN; |
c2041da8 | 2514 | else if (EVP_PKEY_is_a(pk, "EC")) |
0f113f3e | 2515 | check_type = TLS_CT_ECDSA_SIGN; |
c2041da8 | 2516 | |
0f113f3e | 2517 | if (check_type) { |
555cbb32 | 2518 | const uint8_t *ctypes = s->s3.tmp.ctype; |
75c13e78 DSH |
2519 | size_t j; |
2520 | ||
555cbb32 | 2521 | for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { |
75c13e78 | 2522 | if (*ctypes == check_type) { |
0f113f3e MC |
2523 | rv |= CERT_PKEY_CERT_TYPE; |
2524 | break; | |
2525 | } | |
2526 | } | |
2527 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
2528 | goto end; | |
75c13e78 | 2529 | } else { |
0f113f3e | 2530 | rv |= CERT_PKEY_CERT_TYPE; |
75c13e78 | 2531 | } |
0f113f3e | 2532 | |
555cbb32 | 2533 | ca_dn = s->s3.tmp.peer_ca_names; |
0f113f3e MC |
2534 | |
2535 | if (!sk_X509_NAME_num(ca_dn)) | |
2536 | rv |= CERT_PKEY_ISSUER_NAME; | |
2537 | ||
2538 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2539 | if (ssl_check_ca_name(ca_dn, x)) | |
2540 | rv |= CERT_PKEY_ISSUER_NAME; | |
2541 | } | |
2542 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2543 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2544 | X509 *xtmp = sk_X509_value(chain, i); | |
2545 | if (ssl_check_ca_name(ca_dn, xtmp)) { | |
2546 | rv |= CERT_PKEY_ISSUER_NAME; | |
2547 | break; | |
2548 | } | |
2549 | } | |
2550 | } | |
2551 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) | |
2552 | goto end; | |
2553 | } else | |
2554 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
2555 | ||
2556 | if (!check_flags || (rv & check_flags) == check_flags) | |
2557 | rv |= CERT_PKEY_VALID; | |
2558 | ||
2559 | end: | |
2560 | ||
a8bb912d DSH |
2561 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
2562 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); | |
2563 | else | |
0f113f3e MC |
2564 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
2565 | ||
2566 | /* | |
2567 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
2568 | * chain is invalid. | |
2569 | */ | |
2570 | if (!check_flags) { | |
a8bb912d | 2571 | if (rv & CERT_PKEY_VALID) { |
6383d316 | 2572 | *pvalid = rv; |
a8bb912d DSH |
2573 | } else { |
2574 | /* Preserve sign and explicit sign flag, clear rest */ | |
2575 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; | |
0f113f3e MC |
2576 | return 0; |
2577 | } | |
2578 | } | |
2579 | return rv; | |
2580 | } | |
d61ff83b DSH |
2581 | |
2582 | /* Set validity of certificates in an SSL structure */ | |
2583 | void tls1_set_cert_validity(SSL *s) | |
0f113f3e | 2584 | { |
d0ff28f8 | 2585 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
045d078a | 2586 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); |
17dd65e6 | 2587 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
17dd65e6 | 2588 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
2589 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
2590 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
2591 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
3d234c9e | 2592 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
0e1d6ecf | 2593 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); |
0f113f3e MC |
2594 | } |
2595 | ||
69687aa8 | 2596 | /* User level utility function to check a chain is suitable */ |
18d71588 | 2597 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
0f113f3e MC |
2598 | { |
2599 | return tls1_check_chain(s, x, pk, chain, -1); | |
2600 | } | |
d61ff83b | 2601 | |
09599b52 DSH |
2602 | #ifndef OPENSSL_NO_DH |
2603 | DH *ssl_get_auto_dh(SSL *s) | |
0f113f3e MC |
2604 | { |
2605 | int dh_secbits = 80; | |
2606 | if (s->cert->dh_tmp_auto == 2) | |
2607 | return DH_get_1024_160(); | |
555cbb32 TS |
2608 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
2609 | if (s->s3.tmp.new_cipher->strength_bits == 256) | |
0f113f3e MC |
2610 | dh_secbits = 128; |
2611 | else | |
2612 | dh_secbits = 80; | |
2613 | } else { | |
555cbb32 | 2614 | if (s->s3.tmp.cert == NULL) |
f365a3e2 | 2615 | return NULL; |
555cbb32 | 2616 | dh_secbits = EVP_PKEY_security_bits(s->s3.tmp.cert->privatekey); |
0f113f3e MC |
2617 | } |
2618 | ||
2619 | if (dh_secbits >= 128) { | |
2620 | DH *dhp = DH_new(); | |
0aeddcfa | 2621 | BIGNUM *p, *g; |
a71edf3b | 2622 | if (dhp == NULL) |
0f113f3e | 2623 | return NULL; |
0aeddcfa | 2624 | g = BN_new(); |
8eab767a P |
2625 | if (g == NULL || !BN_set_word(g, 2)) { |
2626 | DH_free(dhp); | |
2627 | BN_free(g); | |
2628 | return NULL; | |
2629 | } | |
0f113f3e | 2630 | if (dh_secbits >= 192) |
9021a5df | 2631 | p = BN_get_rfc3526_prime_8192(NULL); |
0f113f3e | 2632 | else |
9021a5df | 2633 | p = BN_get_rfc3526_prime_3072(NULL); |
8eab767a | 2634 | if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
0f113f3e | 2635 | DH_free(dhp); |
0aeddcfa MC |
2636 | BN_free(p); |
2637 | BN_free(g); | |
0f113f3e MC |
2638 | return NULL; |
2639 | } | |
2640 | return dhp; | |
2641 | } | |
2642 | if (dh_secbits >= 112) | |
2643 | return DH_get_2048_224(); | |
2644 | return DH_get_1024_160(); | |
2645 | } | |
09599b52 | 2646 | #endif |
b362ccab DSH |
2647 | |
2648 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e | 2649 | { |
72245f34 | 2650 | int secbits = -1; |
8382fd3a | 2651 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
0f113f3e | 2652 | if (pkey) { |
72245f34 DSH |
2653 | /* |
2654 | * If no parameters this will return -1 and fail using the default | |
2655 | * security callback for any non-zero security level. This will | |
2656 | * reject keys which omit parameters but this only affects DSA and | |
2657 | * omission of parameters is never (?) done in practice. | |
2658 | */ | |
0f113f3e | 2659 | secbits = EVP_PKEY_security_bits(pkey); |
72245f34 | 2660 | } |
0f113f3e MC |
2661 | if (s) |
2662 | return ssl_security(s, op, secbits, 0, x); | |
2663 | else | |
2664 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
2665 | } | |
b362ccab DSH |
2666 | |
2667 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e MC |
2668 | { |
2669 | /* Lookup signature algorithm digest */ | |
65e89736 | 2670 | int secbits, nid, pknid; |
221c7b55 DSH |
2671 | /* Don't check signature if self signed */ |
2672 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
2673 | return 1; | |
65e89736 DSH |
2674 | if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
2675 | secbits = -1; | |
2676 | /* If digest NID not defined use signature NID */ | |
2677 | if (nid == NID_undef) | |
2678 | nid = pknid; | |
0f113f3e | 2679 | if (s) |
65e89736 | 2680 | return ssl_security(s, op, secbits, nid, x); |
0f113f3e | 2681 | else |
65e89736 | 2682 | return ssl_ctx_security(ctx, op, secbits, nid, x); |
0f113f3e | 2683 | } |
b362ccab DSH |
2684 | |
2685 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) | |
0f113f3e MC |
2686 | { |
2687 | if (vfy) | |
2688 | vfy = SSL_SECOP_PEER; | |
2689 | if (is_ee) { | |
2690 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
2691 | return SSL_R_EE_KEY_TOO_SMALL; | |
2692 | } else { | |
2693 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
2694 | return SSL_R_CA_KEY_TOO_SMALL; | |
2695 | } | |
2696 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
2697 | return SSL_R_CA_MD_TOO_WEAK; | |
2698 | return 1; | |
2699 | } | |
2700 | ||
2701 | /* | |
69687aa8 F |
2702 | * Check security of a chain, if |sk| includes the end entity certificate then |
2703 | * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending | |
0f113f3e | 2704 | * one to the peer. Return values: 1 if ok otherwise error code to use |
b362ccab DSH |
2705 | */ |
2706 | ||
2707 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) | |
0f113f3e MC |
2708 | { |
2709 | int rv, start_idx, i; | |
2710 | if (x == NULL) { | |
2711 | x = sk_X509_value(sk, 0); | |
2712 | start_idx = 1; | |
2713 | } else | |
2714 | start_idx = 0; | |
2715 | ||
2716 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
2717 | if (rv != 1) | |
2718 | return rv; | |
2719 | ||
2720 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
2721 | x = sk_X509_value(sk, i); | |
2722 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
2723 | if (rv != 1) | |
2724 | return rv; | |
2725 | } | |
2726 | return 1; | |
2727 | } | |
93a77f9e | 2728 | |
7f6b466b DSH |
2729 | /* |
2730 | * For TLS 1.2 servers check if we have a certificate which can be used | |
b46867d7 | 2731 | * with the signature algorithm "lu" and return index of certificate. |
7f6b466b DSH |
2732 | */ |
2733 | ||
b46867d7 | 2734 | static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu) |
7f6b466b | 2735 | { |
b46867d7 DSH |
2736 | int sig_idx = lu->sig_idx; |
2737 | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx); | |
7f6b466b DSH |
2738 | |
2739 | /* If not recognised or not supported by cipher mask it is not suitable */ | |
b8fef8ee | 2740 | if (clu == NULL |
555cbb32 | 2741 | || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0 |
b8fef8ee | 2742 | || (clu->nid == EVP_PKEY_RSA_PSS |
555cbb32 | 2743 | && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) |
b46867d7 DSH |
2744 | return -1; |
2745 | ||
555cbb32 | 2746 | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; |
7f6b466b DSH |
2747 | } |
2748 | ||
c589c34e | 2749 | /* |
5235ef44 MC |
2750 | * Checks the given cert against signature_algorithm_cert restrictions sent by |
2751 | * the peer (if any) as well as whether the hash from the sigalg is usable with | |
2752 | * the key. | |
2753 | * Returns true if the cert is usable and false otherwise. | |
c589c34e | 2754 | */ |
5235ef44 MC |
2755 | static int check_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x, |
2756 | EVP_PKEY *pkey) | |
c589c34e BK |
2757 | { |
2758 | const SIGALG_LOOKUP *lu; | |
ecbb2fca | 2759 | int mdnid, pknid, supported; |
c589c34e BK |
2760 | size_t i; |
2761 | ||
b5a27688 DW |
2762 | /* |
2763 | * If the given EVP_PKEY cannot supporting signing with this sigalg, | |
2764 | * the answer is simply 'no'. | |
2765 | */ | |
2766 | ERR_set_mark(); | |
2767 | supported = EVP_PKEY_supports_digest_nid(pkey, sig->hash); | |
2768 | ERR_pop_to_mark(); | |
2769 | if (supported == 0) | |
2770 | return 0; | |
2771 | ||
2772 | /* | |
2773 | * The TLS 1.3 signature_algorithms_cert extension places restrictions | |
2774 | * on the sigalg with which the certificate was signed (by its issuer). | |
2775 | */ | |
555cbb32 | 2776 | if (s->s3.tmp.peer_cert_sigalgs != NULL) { |
b5a27688 DW |
2777 | if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL)) |
2778 | return 0; | |
555cbb32 TS |
2779 | for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { |
2780 | lu = tls1_lookup_sigalg(s->s3.tmp.peer_cert_sigalgs[i]); | |
b5a27688 | 2781 | if (lu == NULL) |
c589c34e | 2782 | continue; |
ecbb2fca | 2783 | |
5235ef44 | 2784 | /* |
b5a27688 DW |
2785 | * TODO this does not differentiate between the |
2786 | * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not | |
2787 | * have a chain here that lets us look at the key OID in the | |
2788 | * signing certificate. | |
5235ef44 | 2789 | */ |
b5a27688 DW |
2790 | if (mdnid == lu->hash && pknid == lu->sig) |
2791 | return 1; | |
c589c34e BK |
2792 | } |
2793 | return 0; | |
2794 | } | |
b5a27688 | 2795 | |
5235ef44 | 2796 | /* |
b5a27688 DW |
2797 | * Without signat_algorithms_cert, any certificate for which we have |
2798 | * a viable public key is permitted. | |
5235ef44 | 2799 | */ |
ecbb2fca | 2800 | return 1; |
c589c34e BK |
2801 | } |
2802 | ||
5235ef44 MC |
2803 | /* |
2804 | * Returns true if |s| has a usable certificate configured for use | |
2805 | * with signature scheme |sig|. | |
2806 | * "Usable" includes a check for presence as well as applying | |
2807 | * the signature_algorithm_cert restrictions sent by the peer (if any). | |
2808 | * Returns false if no usable certificate is found. | |
2809 | */ | |
2810 | static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx) | |
2811 | { | |
2812 | /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */ | |
2813 | if (idx == -1) | |
2814 | idx = sig->sig_idx; | |
2815 | if (!ssl_has_cert(s, idx)) | |
2816 | return 0; | |
2817 | ||
2818 | return check_cert_usable(s, sig, s->cert->pkeys[idx].x509, | |
2819 | s->cert->pkeys[idx].privatekey); | |
2820 | } | |
2821 | ||
2822 | /* | |
2823 | * Returns true if the supplied cert |x| and key |pkey| is usable with the | |
2824 | * specified signature scheme |sig|, or false otherwise. | |
2825 | */ | |
2826 | static int is_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x, | |
2827 | EVP_PKEY *pkey) | |
2828 | { | |
2829 | size_t idx; | |
2830 | ||
2831 | if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) | |
2832 | return 0; | |
2833 | ||
2834 | /* Check the key is consistent with the sig alg */ | |
2835 | if ((int)idx != sig->sig_idx) | |
2836 | return 0; | |
2837 | ||
2838 | return check_cert_usable(s, sig, x, pkey); | |
2839 | } | |
2840 | ||
2841 | /* | |
2842 | * Find a signature scheme that works with the supplied certificate |x| and key | |
2843 | * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our | |
2844 | * available certs/keys to find one that works. | |
2845 | */ | |
2846 | static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey) | |
2847 | { | |
2848 | const SIGALG_LOOKUP *lu = NULL; | |
2849 | size_t i; | |
2850 | #ifndef OPENSSL_NO_EC | |
2851 | int curve = -1; | |
2852 | #endif | |
2853 | EVP_PKEY *tmppkey; | |
2854 | ||
2855 | /* Look for a shared sigalgs matching possible certificates */ | |
2856 | for (i = 0; i < s->shared_sigalgslen; i++) { | |
2857 | lu = s->shared_sigalgs[i]; | |
2858 | ||
2859 | /* Skip SHA1, SHA224, DSA and RSA if not PSS */ | |
2860 | if (lu->hash == NID_sha1 | |
2861 | || lu->hash == NID_sha224 | |
2862 | || lu->sig == EVP_PKEY_DSA | |
2863 | || lu->sig == EVP_PKEY_RSA) | |
2864 | continue; | |
2865 | /* Check that we have a cert, and signature_algorithms_cert */ | |
c8f6c28a | 2866 | if (!tls1_lookup_md(s->ctx, lu, NULL)) |
5235ef44 MC |
2867 | continue; |
2868 | if ((pkey == NULL && !has_usable_cert(s, lu, -1)) | |
2869 | || (pkey != NULL && !is_cert_usable(s, lu, x, pkey))) | |
2870 | continue; | |
2871 | ||
2872 | tmppkey = (pkey != NULL) ? pkey | |
2873 | : s->cert->pkeys[lu->sig_idx].privatekey; | |
2874 | ||
2875 | if (lu->sig == EVP_PKEY_EC) { | |
2876 | #ifndef OPENSSL_NO_EC | |
c2041da8 RL |
2877 | if (curve == -1) |
2878 | curve = evp_pkey_get_EC_KEY_curve_nid(tmppkey); | |
5235ef44 MC |
2879 | if (lu->curve != NID_undef && curve != lu->curve) |
2880 | continue; | |
2881 | #else | |
2882 | continue; | |
2883 | #endif | |
2884 | } else if (lu->sig == EVP_PKEY_RSA_PSS) { | |
2885 | /* validate that key is large enough for the signature algorithm */ | |
c5f87134 | 2886 | if (!rsa_pss_check_min_key_size(s->ctx, tmppkey, lu)) |
5235ef44 MC |
2887 | continue; |
2888 | } | |
2889 | break; | |
2890 | } | |
2891 | ||
2892 | if (i == s->shared_sigalgslen) | |
2893 | return NULL; | |
2894 | ||
2895 | return lu; | |
2896 | } | |
2897 | ||
93a77f9e DSH |
2898 | /* |
2899 | * Choose an appropriate signature algorithm based on available certificates | |
717a265a DSH |
2900 | * Sets chosen certificate and signature algorithm. |
2901 | * | |
f63a17d6 MC |
2902 | * For servers if we fail to find a required certificate it is a fatal error, |
2903 | * an appropriate error code is set and a TLS alert is sent. | |
717a265a | 2904 | * |
f63a17d6 | 2905 | * For clients fatalerrs is set to 0. If a certificate is not suitable it is not |
717a265a DSH |
2906 | * a fatal error: we will either try another certificate or not present one |
2907 | * to the server. In this case no error is set. | |
93a77f9e | 2908 | */ |
f63a17d6 | 2909 | int tls_choose_sigalg(SSL *s, int fatalerrs) |
93a77f9e | 2910 | { |
0972bc5c | 2911 | const SIGALG_LOOKUP *lu = NULL; |
b46867d7 | 2912 | int sig_idx = -1; |
0972bc5c | 2913 | |
555cbb32 TS |
2914 | s->s3.tmp.cert = NULL; |
2915 | s->s3.tmp.sigalg = NULL; | |
717a265a | 2916 | |
93a77f9e | 2917 | if (SSL_IS_TLS13(s)) { |
5235ef44 MC |
2918 | lu = find_sig_alg(s, NULL, NULL); |
2919 | if (lu == NULL) { | |
f63a17d6 | 2920 | if (!fatalerrs) |
717a265a | 2921 | return 1; |
f63a17d6 MC |
2922 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG, |
2923 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
0972bc5c DSH |
2924 | return 0; |
2925 | } | |
2926 | } else { | |
7f6b466b | 2927 | /* If ciphersuite doesn't require a cert nothing to do */ |
555cbb32 | 2928 | if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT)) |
7f6b466b DSH |
2929 | return 1; |
2930 | if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys)) | |
717a265a | 2931 | return 1; |
0972bc5c DSH |
2932 | |
2933 | if (SSL_USE_SIGALGS(s)) { | |
c589c34e | 2934 | size_t i; |
555cbb32 | 2935 | if (s->s3.tmp.peer_sigalgs != NULL) { |
599b586d | 2936 | #ifndef OPENSSL_NO_EC |
c2041da8 | 2937 | int curve = -1; |
599b586d DSH |
2938 | |
2939 | /* For Suite B need to match signature algorithm to curve */ | |
c2041da8 RL |
2940 | if (tls1_suiteb(s)) |
2941 | curve = | |
2942 | evp_pkey_get_EC_KEY_curve_nid(s->cert->pkeys[SSL_PKEY_ECC] | |
2943 | .privatekey); | |
599b586d | 2944 | #endif |
0972bc5c DSH |
2945 | |
2946 | /* | |
2947 | * Find highest preference signature algorithm matching | |
2948 | * cert type | |
2949 | */ | |
29948ac8 BK |
2950 | for (i = 0; i < s->shared_sigalgslen; i++) { |
2951 | lu = s->shared_sigalgs[i]; | |
7f6b466b DSH |
2952 | |
2953 | if (s->server) { | |
b46867d7 | 2954 | if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) |
7f6b466b | 2955 | continue; |
b46867d7 DSH |
2956 | } else { |
2957 | int cc_idx = s->cert->key - s->cert->pkeys; | |
2958 | ||
2959 | sig_idx = lu->sig_idx; | |
c589c34e BK |
2960 | if (cc_idx != sig_idx) |
2961 | continue; | |
b2021556 | 2962 | } |
c589c34e BK |
2963 | /* Check that we have a cert, and sig_algs_cert */ |
2964 | if (!has_usable_cert(s, lu, sig_idx)) | |
2965 | continue; | |
0fe3db25 NR |
2966 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
2967 | /* validate that key is large enough for the signature algorithm */ | |
bcec0b94 | 2968 | EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; |
0fe3db25 | 2969 | |
c5f87134 | 2970 | if (!rsa_pss_check_min_key_size(s->ctx, pkey, lu)) |
0fe3db25 NR |
2971 | continue; |
2972 | } | |
7f6b466b DSH |
2973 | #ifndef OPENSSL_NO_EC |
2974 | if (curve == -1 || lu->curve == curve) | |
599b586d | 2975 | #endif |
0972bc5c DSH |
2976 | break; |
2977 | } | |
db30f432 DB |
2978 | #ifndef OPENSSL_NO_GOST |
2979 | /* | |
2980 | * Some Windows-based implementations do not send GOST algorithms indication | |
2981 | * in supported_algorithms extension, so when we have GOST-based ciphersuite, | |
2982 | * we have to assume GOST support. | |
2983 | */ | |
2984 | if (i == s->shared_sigalgslen && s->s3.tmp.new_cipher->algorithm_auth & (SSL_aGOST01 | SSL_aGOST12)) { | |
2985 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { | |
2986 | if (!fatalerrs) | |
2987 | return 1; | |
2988 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, | |
2989 | SSL_F_TLS_CHOOSE_SIGALG, | |
2990 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
2991 | return 0; | |
2992 | } else { | |
2993 | i = 0; | |
2994 | sig_idx = lu->sig_idx; | |
2995 | } | |
2996 | } | |
2997 | #endif | |
29948ac8 | 2998 | if (i == s->shared_sigalgslen) { |
f63a17d6 | 2999 | if (!fatalerrs) |
717a265a | 3000 | return 1; |
b8fef8ee MC |
3001 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3002 | SSL_F_TLS_CHOOSE_SIGALG, | |
3003 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
0972bc5c DSH |
3004 | return 0; |
3005 | } | |
3006 | } else { | |
3007 | /* | |
3008 | * If we have no sigalg use defaults | |
3009 | */ | |
3010 | const uint16_t *sent_sigs; | |
c589c34e | 3011 | size_t sent_sigslen; |
0972bc5c | 3012 | |
7f6b466b | 3013 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
f63a17d6 | 3014 | if (!fatalerrs) |
717a265a | 3015 | return 1; |
f63a17d6 | 3016 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG, |
a70535f8 | 3017 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3018 | return 0; |
3019 | } | |
3020 | ||
3021 | /* Check signature matches a type we sent */ | |
3022 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
3023 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | |
c589c34e BK |
3024 | if (lu->sigalg == *sent_sigs |
3025 | && has_usable_cert(s, lu, lu->sig_idx)) | |
0972bc5c DSH |
3026 | break; |
3027 | } | |
3028 | if (i == sent_sigslen) { | |
f63a17d6 | 3029 | if (!fatalerrs) |
717a265a | 3030 | return 1; |
f63a17d6 MC |
3031 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
3032 | SSL_F_TLS_CHOOSE_SIGALG, | |
3033 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0972bc5c DSH |
3034 | return 0; |
3035 | } | |
3036 | } | |
3037 | } else { | |
7f6b466b | 3038 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
f63a17d6 | 3039 | if (!fatalerrs) |
717a265a | 3040 | return 1; |
f63a17d6 | 3041 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG, |
a70535f8 | 3042 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3043 | return 0; |
3044 | } | |
3045 | } | |
93a77f9e | 3046 | } |
b46867d7 DSH |
3047 | if (sig_idx == -1) |
3048 | sig_idx = lu->sig_idx; | |
555cbb32 TS |
3049 | s->s3.tmp.cert = &s->cert->pkeys[sig_idx]; |
3050 | s->cert->key = s->s3.tmp.cert; | |
3051 | s->s3.tmp.sigalg = lu; | |
93a77f9e DSH |
3052 | return 1; |
3053 | } | |
cf72c757 F |
3054 | |
3055 | int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) | |
3056 | { | |
3057 | if (mode != TLSEXT_max_fragment_length_DISABLED | |
3058 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | |
3059 | SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH, | |
3060 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); | |
3061 | return 0; | |
3062 | } | |
3063 | ||
3064 | ctx->ext.max_fragment_len_mode = mode; | |
3065 | return 1; | |
3066 | } | |
3067 | ||
3068 | int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) | |
3069 | { | |
3070 | if (mode != TLSEXT_max_fragment_length_DISABLED | |
3071 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | |
3072 | SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH, | |
3073 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); | |
3074 | return 0; | |
3075 | } | |
3076 | ||
3077 | ssl->ext.max_fragment_len_mode = mode; | |
3078 | return 1; | |
3079 | } | |
3080 | ||
3081 | uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) | |
3082 | { | |
3083 | return session->ext.max_fragment_len_mode; | |
3084 | } | |
a76ce286 P |
3085 | |
3086 | /* | |
3087 | * Helper functions for HMAC access with legacy support included. | |
3088 | */ | |
3089 | SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx) | |
3090 | { | |
3091 | SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret)); | |
3092 | EVP_MAC *mac = NULL; | |
3093 | ||
3094 | if (ret == NULL) | |
3095 | return NULL; | |
3096 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3097 | if (ctx->ext.ticket_key_evp_cb == NULL | |
3098 | && ctx->ext.ticket_key_cb != NULL) { | |
3099 | ret->old_ctx = HMAC_CTX_new(); | |
3100 | if (ret->old_ctx == NULL) | |
3101 | goto err; | |
3102 | return ret; | |
3103 | } | |
3104 | #endif | |
3105 | mac = EVP_MAC_fetch(ctx->libctx, "HMAC", NULL); | |
3106 | if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL) | |
3107 | goto err; | |
3108 | EVP_MAC_free(mac); | |
3109 | return ret; | |
3110 | err: | |
3111 | EVP_MAC_CTX_free(ret->ctx); | |
3112 | EVP_MAC_free(mac); | |
3113 | OPENSSL_free(ret); | |
3114 | return NULL; | |
3115 | } | |
3116 | ||
3117 | void ssl_hmac_free(SSL_HMAC *ctx) | |
3118 | { | |
3119 | if (ctx != NULL) { | |
3120 | EVP_MAC_CTX_free(ctx->ctx); | |
3121 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3122 | HMAC_CTX_free(ctx->old_ctx); | |
3123 | #endif | |
3124 | OPENSSL_free(ctx); | |
3125 | } | |
3126 | } | |
3127 | ||
3128 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3129 | HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx) | |
3130 | { | |
3131 | return ctx->old_ctx; | |
3132 | } | |
3133 | #endif | |
3134 | ||
3135 | EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx) | |
3136 | { | |
3137 | return ctx->ctx; | |
3138 | } | |
3139 | ||
3140 | int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md) | |
3141 | { | |
3142 | OSSL_PARAM params[3], *p = params; | |
3143 | ||
3144 | if (ctx->ctx != NULL) { | |
3145 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0); | |
3146 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, len); | |
3147 | *p = OSSL_PARAM_construct_end(); | |
3148 | if (EVP_MAC_CTX_set_params(ctx->ctx, params) && EVP_MAC_init(ctx->ctx)) | |
3149 | return 1; | |
3150 | } | |
3151 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3152 | if (ctx->old_ctx != NULL) | |
3153 | return HMAC_Init_ex(ctx->old_ctx, key, len, | |
3154 | EVP_get_digestbyname(md), NULL); | |
3155 | #endif | |
3156 | return 0; | |
3157 | } | |
3158 | ||
3159 | int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len) | |
3160 | { | |
3161 | if (ctx->ctx != NULL) | |
3162 | return EVP_MAC_update(ctx->ctx, data, len); | |
3163 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3164 | if (ctx->old_ctx != NULL) | |
3165 | return HMAC_Update(ctx->old_ctx, data, len); | |
3166 | #endif | |
3167 | return 0; | |
3168 | } | |
3169 | ||
3170 | int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len, | |
3171 | size_t max_size) | |
3172 | { | |
3173 | if (ctx->ctx != NULL) | |
3174 | return EVP_MAC_final(ctx->ctx, md, len, max_size); | |
3175 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3176 | if (ctx->old_ctx != NULL) { | |
3177 | unsigned int l; | |
3178 | ||
3179 | if (HMAC_Final(ctx->old_ctx, md, &l) > 0) { | |
3180 | if (len != NULL) | |
3181 | *len = l; | |
3182 | return 1; | |
3183 | } | |
3184 | } | |
3185 | #endif | |
3186 | return 0; | |
3187 | } | |
3188 | ||
3189 | size_t ssl_hmac_size(const SSL_HMAC *ctx) | |
3190 | { | |
3191 | if (ctx->ctx != NULL) | |
3192 | return EVP_MAC_size(ctx->ctx); | |
3193 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3194 | if (ctx->old_ctx != NULL) | |
3195 | return HMAC_size(ctx->old_ctx); | |
3196 | #endif | |
3197 | return 0; | |
3198 | } | |
3199 |