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