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