]>
Commit | Line | Data |
---|---|---|
58964a49 RE |
1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | * All rights reserved. | |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
58964a49 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
58964a49 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
58964a49 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
58964a49 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
58964a49 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
58964a49 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
f1fd4544 | 57 | /* ==================================================================== |
52b8dad8 | 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
f1fd4544 BM |
59 | * |
60 | * Redistribution and use in source and binary forms, with or without | |
61 | * modification, are permitted provided that the following conditions | |
62 | * are met: | |
63 | * | |
64 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 65 | * notice, this list of conditions and the following disclaimer. |
f1fd4544 BM |
66 | * |
67 | * 2. Redistributions in binary form must reproduce the above copyright | |
68 | * notice, this list of conditions and the following disclaimer in | |
69 | * the documentation and/or other materials provided with the | |
70 | * distribution. | |
71 | * | |
72 | * 3. All advertising materials mentioning features or use of this | |
73 | * software must display the following acknowledgment: | |
74 | * "This product includes software developed by the OpenSSL Project | |
75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
76 | * | |
77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
78 | * endorse or promote products derived from this software without | |
79 | * prior written permission. For written permission, please contact | |
80 | * openssl-core@openssl.org. | |
81 | * | |
82 | * 5. Products derived from this software may not be called "OpenSSL" | |
83 | * nor may "OpenSSL" appear in their names without prior written | |
84 | * permission of the OpenSSL Project. | |
85 | * | |
86 | * 6. Redistributions of any form whatsoever must retain the following | |
87 | * acknowledgment: | |
88 | * "This product includes software developed by the OpenSSL Project | |
89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
90 | * | |
91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
102 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
103 | * ==================================================================== | |
104 | * | |
105 | * This product includes cryptographic software written by Eric Young | |
106 | * (eay@cryptsoft.com). This product includes software written by Tim | |
107 | * Hudson (tjh@cryptsoft.com). | |
108 | * | |
109 | */ | |
58964a49 RE |
110 | |
111 | #include <stdio.h> | |
aa474d1f | 112 | #include <stdlib.h> |
ec577822 | 113 | #include <openssl/objects.h> |
6434abbf DSH |
114 | #include <openssl/evp.h> |
115 | #include <openssl/hmac.h> | |
67c8e7f4 | 116 | #include <openssl/ocsp.h> |
4817504d | 117 | #include <openssl/rand.h> |
3c27208f RS |
118 | #include <openssl/dh.h> |
119 | #include <openssl/bn.h> | |
58964a49 | 120 | #include "ssl_locl.h" |
3c27208f | 121 | #include <openssl/ct.h> |
58964a49 | 122 | |
6434abbf | 123 | static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, |
0f113f3e MC |
124 | const unsigned char *sess_id, int sesslen, |
125 | SSL_SESSION **psess); | |
2daceb03 | 126 | static int ssl_check_clienthello_tlsext_early(SSL *s); |
aa474d1f | 127 | static int ssl_check_serverhello_tlsext(SSL *s); |
6434abbf | 128 | |
0f113f3e MC |
129 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
130 | tls1_enc, | |
131 | tls1_mac, | |
132 | tls1_setup_key_block, | |
133 | tls1_generate_master_secret, | |
134 | tls1_change_cipher_state, | |
135 | tls1_final_finish_mac, | |
136 | TLS1_FINISH_MAC_LENGTH, | |
0f113f3e MC |
137 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
138 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
139 | tls1_alert_code, | |
140 | tls1_export_keying_material, | |
141 | 0, | |
142 | SSL3_HM_HEADER_LENGTH, | |
143 | ssl3_set_handshake_header, | |
144 | ssl3_handshake_write | |
145 | }; | |
146 | ||
147 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | |
148 | tls1_enc, | |
149 | tls1_mac, | |
150 | tls1_setup_key_block, | |
151 | tls1_generate_master_secret, | |
152 | tls1_change_cipher_state, | |
153 | tls1_final_finish_mac, | |
154 | TLS1_FINISH_MAC_LENGTH, | |
0f113f3e MC |
155 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
156 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
157 | tls1_alert_code, | |
158 | tls1_export_keying_material, | |
159 | SSL_ENC_FLAG_EXPLICIT_IV, | |
160 | SSL3_HM_HEADER_LENGTH, | |
161 | ssl3_set_handshake_header, | |
162 | ssl3_handshake_write | |
163 | }; | |
164 | ||
165 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | |
166 | tls1_enc, | |
167 | tls1_mac, | |
168 | tls1_setup_key_block, | |
169 | tls1_generate_master_secret, | |
170 | tls1_change_cipher_state, | |
171 | tls1_final_finish_mac, | |
172 | TLS1_FINISH_MAC_LENGTH, | |
0f113f3e MC |
173 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
174 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
175 | tls1_alert_code, | |
176 | tls1_export_keying_material, | |
177 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
178 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
179 | SSL3_HM_HEADER_LENGTH, | |
180 | ssl3_set_handshake_header, | |
181 | ssl3_handshake_write | |
182 | }; | |
58964a49 | 183 | |
f3b656b2 | 184 | long tls1_default_timeout(void) |
0f113f3e MC |
185 | { |
186 | /* | |
187 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for | |
188 | * http, the cache would over fill | |
189 | */ | |
190 | return (60 * 60 * 2); | |
191 | } | |
58964a49 | 192 | |
6b691a5c | 193 | int tls1_new(SSL *s) |
0f113f3e MC |
194 | { |
195 | if (!ssl3_new(s)) | |
196 | return (0); | |
197 | s->method->ssl_clear(s); | |
198 | return (1); | |
199 | } | |
58964a49 | 200 | |
6b691a5c | 201 | void tls1_free(SSL *s) |
0f113f3e | 202 | { |
b548a1f1 | 203 | OPENSSL_free(s->tlsext_session_ticket); |
0f113f3e MC |
204 | ssl3_free(s); |
205 | } | |
58964a49 | 206 | |
6b691a5c | 207 | void tls1_clear(SSL *s) |
0f113f3e MC |
208 | { |
209 | ssl3_clear(s); | |
4fa52141 VD |
210 | if (s->method->version == TLS_ANY_VERSION) |
211 | s->version = TLS_MAX_VERSION; | |
212 | else | |
213 | s->version = s->method->version; | |
0f113f3e | 214 | } |
58964a49 | 215 | |
525de5d3 | 216 | #ifndef OPENSSL_NO_EC |
eda3766b | 217 | |
0f113f3e MC |
218 | typedef struct { |
219 | int nid; /* Curve NID */ | |
220 | int secbits; /* Bits of security (from SP800-57) */ | |
221 | unsigned int flags; /* Flags: currently just field type */ | |
222 | } tls_curve_info; | |
223 | ||
1db3107a DSH |
224 | /* Mask for curve type */ |
225 | # define TLS_CURVE_TYPE 0x3 | |
0f113f3e | 226 | # define TLS_CURVE_PRIME 0x0 |
1db3107a DSH |
227 | # define TLS_CURVE_CHAR2 0x1 |
228 | # define TLS_CURVE_CUSTOM 0x2 | |
0f113f3e | 229 | |
2dc1aeed DSH |
230 | /* |
231 | * Table of curve information. | |
ddb4c047 | 232 | * Do not delete entries or reorder this array! It is used as a lookup |
2dc1aeed DSH |
233 | * table: the index of each entry is one less than the TLS curve id. |
234 | */ | |
0f113f3e MC |
235 | static const tls_curve_info nid_list[] = { |
236 | {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ | |
237 | {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ | |
238 | {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ | |
239 | {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ | |
240 | {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ | |
241 | {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ | |
242 | {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ | |
243 | {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ | |
244 | {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ | |
245 | {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ | |
246 | {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ | |
247 | {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ | |
248 | {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ | |
249 | {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ | |
250 | {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ | |
251 | {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ | |
252 | {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ | |
253 | {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ | |
254 | {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ | |
255 | {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ | |
256 | {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ | |
257 | {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ | |
258 | {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ | |
259 | {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ | |
260 | {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ | |
261 | {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ | |
262 | {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ | |
263 | {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ | |
1db3107a DSH |
264 | /* X25519 (29) */ |
265 | {NID_X25519, 128, TLS_CURVE_CUSTOM}, | |
0f113f3e MC |
266 | }; |
267 | ||
268 | static const unsigned char ecformats_default[] = { | |
269 | TLSEXT_ECPOINTFORMAT_uncompressed, | |
270 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | |
271 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | |
272 | }; | |
273 | ||
fe6ef247 KR |
274 | /* The default curves */ |
275 | static const unsigned char eccurves_default[] = { | |
1db3107a | 276 | 0, 29, /* X25519 (29) */ |
de57d237 | 277 | 0, 23, /* secp256r1 (23) */ |
0f113f3e | 278 | 0, 25, /* secp521r1 (25) */ |
0f113f3e | 279 | 0, 24, /* secp384r1 (24) */ |
de57d237 EK |
280 | }; |
281 | ||
282 | static const unsigned char eccurves_all[] = { | |
1db3107a | 283 | 0, 29, /* X25519 (29) */ |
de57d237 | 284 | 0, 23, /* secp256r1 (23) */ |
de57d237 | 285 | 0, 25, /* secp521r1 (25) */ |
de57d237 | 286 | 0, 24, /* secp384r1 (24) */ |
0f113f3e | 287 | 0, 26, /* brainpoolP256r1 (26) */ |
ddb4c047 RS |
288 | 0, 27, /* brainpoolP384r1 (27) */ |
289 | 0, 28, /* brainpool512r1 (28) */ | |
290 | ||
291 | /* | |
292 | * Remaining curves disabled by default but still permitted if set | |
293 | * via an explicit callback or parameters. | |
294 | */ | |
0f113f3e | 295 | 0, 22, /* secp256k1 (22) */ |
de57d237 EK |
296 | 0, 14, /* sect571r1 (14) */ |
297 | 0, 13, /* sect571k1 (13) */ | |
298 | 0, 11, /* sect409k1 (11) */ | |
299 | 0, 12, /* sect409r1 (12) */ | |
300 | 0, 9, /* sect283k1 (9) */ | |
301 | 0, 10, /* sect283r1 (10) */ | |
de57d237 EK |
302 | 0, 20, /* secp224k1 (20) */ |
303 | 0, 21, /* secp224r1 (21) */ | |
304 | 0, 18, /* secp192k1 (18) */ | |
305 | 0, 19, /* secp192r1 (19) */ | |
306 | 0, 15, /* secp160k1 (15) */ | |
307 | 0, 16, /* secp160r1 (16) */ | |
308 | 0, 17, /* secp160r2 (17) */ | |
0f113f3e MC |
309 | 0, 8, /* sect239k1 (8) */ |
310 | 0, 6, /* sect233k1 (6) */ | |
311 | 0, 7, /* sect233r1 (7) */ | |
0f113f3e MC |
312 | 0, 4, /* sect193r1 (4) */ |
313 | 0, 5, /* sect193r2 (5) */ | |
0f113f3e MC |
314 | 0, 1, /* sect163k1 (1) */ |
315 | 0, 2, /* sect163r1 (2) */ | |
316 | 0, 3, /* sect163r2 (3) */ | |
0f113f3e MC |
317 | }; |
318 | ||
de57d237 | 319 | |
0f113f3e MC |
320 | static const unsigned char suiteb_curves[] = { |
321 | 0, TLSEXT_curve_P_256, | |
322 | 0, TLSEXT_curve_P_384 | |
323 | }; | |
2ea80354 | 324 | |
525de5d3 | 325 | int tls1_ec_curve_id2nid(int curve_id) |
0f113f3e MC |
326 | { |
327 | /* ECC curves from RFC 4492 and RFC 7027 */ | |
b6eb9827 | 328 | if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) |
0f113f3e MC |
329 | return 0; |
330 | return nid_list[curve_id - 1].nid; | |
331 | } | |
525de5d3 DSH |
332 | |
333 | int tls1_ec_nid2curve_id(int nid) | |
0f113f3e | 334 | { |
2fa2d15a DSH |
335 | size_t i; |
336 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { | |
337 | if (nid_list[i].nid == nid) | |
338 | return i + 1; | |
0f113f3e | 339 | } |
2fa2d15a | 340 | return 0; |
0f113f3e MC |
341 | } |
342 | ||
740580c2 EK |
343 | /* |
344 | * Get curves list, if "sess" is set return client curves otherwise | |
345 | * preferred list. | |
346 | * Sets |num_curves| to the number of curves in the list, i.e., | |
347 | * the length of |pcurves| is 2 * num_curves. | |
348 | * Returns 1 on success and 0 if the client curves list has invalid format. | |
349 | * The latter indicates an internal error: we should not be accepting such | |
350 | * lists in the first place. | |
351 | * TODO(emilia): we should really be storing the curves list in explicitly | |
352 | * parsed form instead. (However, this would affect binary compatibility | |
353 | * so cannot happen in the 1.0.x series.) | |
fd2b65ce | 354 | */ |
740580c2 | 355 | static int tls1_get_curvelist(SSL *s, int sess, |
0f113f3e MC |
356 | const unsigned char **pcurves, |
357 | size_t *num_curves) | |
358 | { | |
359 | size_t pcurveslen = 0; | |
360 | if (sess) { | |
361 | *pcurves = s->session->tlsext_ellipticcurvelist; | |
362 | pcurveslen = s->session->tlsext_ellipticcurvelist_length; | |
363 | } else { | |
364 | /* For Suite B mode only include P-256, P-384 */ | |
365 | switch (tls1_suiteb(s)) { | |
366 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
367 | *pcurves = suiteb_curves; | |
368 | pcurveslen = sizeof(suiteb_curves); | |
369 | break; | |
370 | ||
371 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
372 | *pcurves = suiteb_curves; | |
373 | pcurveslen = 2; | |
374 | break; | |
375 | ||
376 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
377 | *pcurves = suiteb_curves + 2; | |
378 | pcurveslen = 2; | |
379 | break; | |
380 | default: | |
381 | *pcurves = s->tlsext_ellipticcurvelist; | |
382 | pcurveslen = s->tlsext_ellipticcurvelist_length; | |
383 | } | |
384 | if (!*pcurves) { | |
fe6ef247 KR |
385 | *pcurves = eccurves_default; |
386 | pcurveslen = sizeof(eccurves_default); | |
0f113f3e MC |
387 | } |
388 | } | |
389 | ||
390 | /* We do not allow odd length arrays to enter the system. */ | |
391 | if (pcurveslen & 1) { | |
392 | SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR); | |
393 | *num_curves = 0; | |
394 | return 0; | |
395 | } else { | |
396 | *num_curves = pcurveslen / 2; | |
397 | return 1; | |
398 | } | |
399 | } | |
b362ccab DSH |
400 | |
401 | /* See if curve is allowed by security callback */ | |
402 | static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) | |
0f113f3e MC |
403 | { |
404 | const tls_curve_info *cinfo; | |
405 | if (curve[0]) | |
406 | return 1; | |
b6eb9827 | 407 | if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) |
0f113f3e MC |
408 | return 0; |
409 | cinfo = &nid_list[curve[1] - 1]; | |
410 | # ifdef OPENSSL_NO_EC2M | |
411 | if (cinfo->flags & TLS_CURVE_CHAR2) | |
412 | return 0; | |
413 | # endif | |
414 | return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); | |
415 | } | |
b362ccab | 416 | |
d18b716d DSH |
417 | /* Check a curve is one of our preferences */ |
418 | int tls1_check_curve(SSL *s, const unsigned char *p, size_t len) | |
0f113f3e MC |
419 | { |
420 | const unsigned char *curves; | |
421 | size_t num_curves, i; | |
422 | unsigned int suiteb_flags = tls1_suiteb(s); | |
423 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) | |
424 | return 0; | |
425 | /* Check curve matches Suite B preferences */ | |
426 | if (suiteb_flags) { | |
427 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
428 | if (p[1]) | |
429 | return 0; | |
430 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | |
431 | if (p[2] != TLSEXT_curve_P_256) | |
432 | return 0; | |
433 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | |
434 | if (p[2] != TLSEXT_curve_P_384) | |
435 | return 0; | |
436 | } else /* Should never happen */ | |
437 | return 0; | |
438 | } | |
439 | if (!tls1_get_curvelist(s, 0, &curves, &num_curves)) | |
440 | return 0; | |
441 | for (i = 0; i < num_curves; i++, curves += 2) { | |
442 | if (p[1] == curves[0] && p[2] == curves[1]) | |
443 | return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK); | |
444 | } | |
445 | return 0; | |
446 | } | |
d0595f17 | 447 | |
1d97c843 | 448 | /*- |
6977e8ee KR |
449 | * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef |
450 | * if there is no match. | |
451 | * For nmatch == -1, return number of matches | |
376e2ca3 EK |
452 | * For nmatch == -2, return the NID of the curve to use for |
453 | * an EC tmp key, or NID_undef if there is no match. | |
d0595f17 | 454 | */ |
a4352630 | 455 | int tls1_shared_curve(SSL *s, int nmatch) |
0f113f3e MC |
456 | { |
457 | const unsigned char *pref, *supp; | |
458 | size_t num_pref, num_supp, i, j; | |
459 | int k; | |
460 | /* Can't do anything on client side */ | |
461 | if (s->server == 0) | |
462 | return -1; | |
463 | if (nmatch == -2) { | |
464 | if (tls1_suiteb(s)) { | |
465 | /* | |
466 | * For Suite B ciphersuite determines curve: we already know | |
467 | * these are acceptable due to previous checks. | |
468 | */ | |
469 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
470 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
471 | return NID_X9_62_prime256v1; /* P-256 */ | |
472 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
473 | return NID_secp384r1; /* P-384 */ | |
474 | /* Should never happen */ | |
475 | return NID_undef; | |
476 | } | |
477 | /* If not Suite B just return first preference shared curve */ | |
478 | nmatch = 0; | |
479 | } | |
480 | /* | |
481 | * Avoid truncation. tls1_get_curvelist takes an int | |
482 | * but s->options is a long... | |
483 | */ | |
484 | if (!tls1_get_curvelist | |
485 | (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp, | |
486 | &num_supp)) | |
487 | /* In practice, NID_undef == 0 but let's be precise. */ | |
488 | return nmatch == -1 ? 0 : NID_undef; | |
489 | if (!tls1_get_curvelist | |
490 | (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, | |
491 | &num_pref)) | |
492 | return nmatch == -1 ? 0 : NID_undef; | |
3c06513f KR |
493 | |
494 | /* | |
495 | * If the client didn't send the elliptic_curves extension all of them | |
496 | * are allowed. | |
497 | */ | |
498 | if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) { | |
499 | supp = eccurves_all; | |
500 | num_supp = sizeof(eccurves_all) / 2; | |
501 | } else if (num_pref == 0 && | |
502 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) { | |
503 | pref = eccurves_all; | |
504 | num_pref = sizeof(eccurves_all) / 2; | |
505 | } | |
506 | ||
0f113f3e MC |
507 | k = 0; |
508 | for (i = 0; i < num_pref; i++, pref += 2) { | |
509 | const unsigned char *tsupp = supp; | |
510 | for (j = 0; j < num_supp; j++, tsupp += 2) { | |
511 | if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) { | |
512 | if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED)) | |
513 | continue; | |
514 | if (nmatch == k) { | |
515 | int id = (pref[0] << 8) | pref[1]; | |
516 | return tls1_ec_curve_id2nid(id); | |
517 | } | |
518 | k++; | |
519 | } | |
520 | } | |
521 | } | |
522 | if (nmatch == -1) | |
523 | return k; | |
524 | /* Out of range (nmatch > k). */ | |
525 | return NID_undef; | |
526 | } | |
d0595f17 DSH |
527 | |
528 | int tls1_set_curves(unsigned char **pext, size_t *pextlen, | |
0f113f3e MC |
529 | int *curves, size_t ncurves) |
530 | { | |
531 | unsigned char *clist, *p; | |
532 | size_t i; | |
533 | /* | |
534 | * Bitmap of curves included to detect duplicates: only works while curve | |
535 | * ids < 32 | |
536 | */ | |
537 | unsigned long dup_list = 0; | |
538 | clist = OPENSSL_malloc(ncurves * 2); | |
a71edf3b | 539 | if (clist == NULL) |
0f113f3e MC |
540 | return 0; |
541 | for (i = 0, p = clist; i < ncurves; i++) { | |
542 | unsigned long idmask; | |
543 | int id; | |
544 | id = tls1_ec_nid2curve_id(curves[i]); | |
545 | idmask = 1L << id; | |
546 | if (!id || (dup_list & idmask)) { | |
547 | OPENSSL_free(clist); | |
548 | return 0; | |
549 | } | |
550 | dup_list |= idmask; | |
551 | s2n(id, p); | |
552 | } | |
b548a1f1 | 553 | OPENSSL_free(*pext); |
0f113f3e MC |
554 | *pext = clist; |
555 | *pextlen = ncurves * 2; | |
556 | return 1; | |
557 | } | |
558 | ||
559 | # define MAX_CURVELIST 28 | |
560 | ||
561 | typedef struct { | |
562 | size_t nidcnt; | |
563 | int nid_arr[MAX_CURVELIST]; | |
564 | } nid_cb_st; | |
d0595f17 DSH |
565 | |
566 | static int nid_cb(const char *elem, int len, void *arg) | |
0f113f3e MC |
567 | { |
568 | nid_cb_st *narg = arg; | |
569 | size_t i; | |
570 | int nid; | |
571 | char etmp[20]; | |
2747d73c KR |
572 | if (elem == NULL) |
573 | return 0; | |
0f113f3e MC |
574 | if (narg->nidcnt == MAX_CURVELIST) |
575 | return 0; | |
576 | if (len > (int)(sizeof(etmp) - 1)) | |
577 | return 0; | |
578 | memcpy(etmp, elem, len); | |
579 | etmp[len] = 0; | |
580 | nid = EC_curve_nist2nid(etmp); | |
581 | if (nid == NID_undef) | |
582 | nid = OBJ_sn2nid(etmp); | |
583 | if (nid == NID_undef) | |
584 | nid = OBJ_ln2nid(etmp); | |
585 | if (nid == NID_undef) | |
586 | return 0; | |
587 | for (i = 0; i < narg->nidcnt; i++) | |
588 | if (narg->nid_arr[i] == nid) | |
589 | return 0; | |
590 | narg->nid_arr[narg->nidcnt++] = nid; | |
591 | return 1; | |
592 | } | |
593 | ||
d0595f17 | 594 | /* Set curves based on a colon separate list */ |
0f113f3e MC |
595 | int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, |
596 | const char *str) | |
597 | { | |
598 | nid_cb_st ncb; | |
599 | ncb.nidcnt = 0; | |
600 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) | |
601 | return 0; | |
602 | if (pext == NULL) | |
603 | return 1; | |
604 | return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt); | |
605 | } | |
606 | ||
fd2b65ce DSH |
607 | /* For an EC key set TLS id and required compression based on parameters */ |
608 | static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id, | |
0f113f3e MC |
609 | EC_KEY *ec) |
610 | { | |
2235b7f2 | 611 | int id; |
0f113f3e | 612 | const EC_GROUP *grp; |
0f113f3e MC |
613 | if (!ec) |
614 | return 0; | |
615 | /* Determine if it is a prime field */ | |
616 | grp = EC_KEY_get0_group(ec); | |
617 | if (!grp) | |
618 | return 0; | |
0f113f3e MC |
619 | /* Determine curve ID */ |
620 | id = EC_GROUP_get_curve_name(grp); | |
621 | id = tls1_ec_nid2curve_id(id); | |
2235b7f2 DSH |
622 | /* If no id return error: we don't support arbitrary explicit curves */ |
623 | if (id == 0) | |
624 | return 0; | |
625 | curve_id[0] = 0; | |
626 | curve_id[1] = (unsigned char)id; | |
0f113f3e MC |
627 | if (comp_id) { |
628 | if (EC_KEY_get0_public_key(ec) == NULL) | |
629 | return 0; | |
2235b7f2 DSH |
630 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { |
631 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | |
632 | } else { | |
633 | if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME) | |
0f113f3e MC |
634 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
635 | else | |
636 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | |
2235b7f2 | 637 | } |
0f113f3e MC |
638 | } |
639 | return 1; | |
640 | } | |
641 | ||
fd2b65ce DSH |
642 | /* Check an EC key is compatible with extensions */ |
643 | static int tls1_check_ec_key(SSL *s, | |
0f113f3e MC |
644 | unsigned char *curve_id, unsigned char *comp_id) |
645 | { | |
646 | const unsigned char *pformats, *pcurves; | |
647 | size_t num_formats, num_curves, i; | |
648 | int j; | |
649 | /* | |
650 | * If point formats extension present check it, otherwise everything is | |
651 | * supported (see RFC4492). | |
652 | */ | |
653 | if (comp_id && s->session->tlsext_ecpointformatlist) { | |
654 | pformats = s->session->tlsext_ecpointformatlist; | |
655 | num_formats = s->session->tlsext_ecpointformatlist_length; | |
656 | for (i = 0; i < num_formats; i++, pformats++) { | |
657 | if (*comp_id == *pformats) | |
658 | break; | |
659 | } | |
660 | if (i == num_formats) | |
661 | return 0; | |
662 | } | |
663 | if (!curve_id) | |
664 | return 1; | |
665 | /* Check curve is consistent with client and server preferences */ | |
666 | for (j = 0; j <= 1; j++) { | |
667 | if (!tls1_get_curvelist(s, j, &pcurves, &num_curves)) | |
668 | return 0; | |
b79d2410 MC |
669 | if (j == 1 && num_curves == 0) { |
670 | /* | |
671 | * If we've not received any curves then skip this check. | |
672 | * RFC 4492 does not require the supported elliptic curves extension | |
673 | * so if it is not sent we can just choose any curve. | |
674 | * It is invalid to send an empty list in the elliptic curves | |
675 | * extension, so num_curves == 0 always means no extension. | |
676 | */ | |
677 | break; | |
678 | } | |
0f113f3e MC |
679 | for (i = 0; i < num_curves; i++, pcurves += 2) { |
680 | if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1]) | |
681 | break; | |
682 | } | |
683 | if (i == num_curves) | |
684 | return 0; | |
685 | /* For clients can only check sent curve list */ | |
686 | if (!s->server) | |
687 | break; | |
688 | } | |
689 | return 1; | |
690 | } | |
d61ff83b | 691 | |
5087afa1 | 692 | static void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
0f113f3e MC |
693 | size_t *num_formats) |
694 | { | |
695 | /* | |
696 | * If we have a custom point format list use it otherwise use default | |
697 | */ | |
698 | if (s->tlsext_ecpointformatlist) { | |
699 | *pformats = s->tlsext_ecpointformatlist; | |
700 | *num_formats = s->tlsext_ecpointformatlist_length; | |
701 | } else { | |
702 | *pformats = ecformats_default; | |
703 | /* For Suite B we don't support char2 fields */ | |
704 | if (tls1_suiteb(s)) | |
705 | *num_formats = sizeof(ecformats_default) - 1; | |
706 | else | |
707 | *num_formats = sizeof(ecformats_default); | |
708 | } | |
709 | } | |
710 | ||
711 | /* | |
712 | * Check cert parameters compatible with extensions: currently just checks EC | |
713 | * certificates have compatible curves and compression. | |
d61ff83b | 714 | */ |
2ea80354 | 715 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
0f113f3e MC |
716 | { |
717 | unsigned char comp_id, curve_id[2]; | |
718 | EVP_PKEY *pkey; | |
719 | int rv; | |
8382fd3a | 720 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
721 | if (!pkey) |
722 | return 0; | |
723 | /* If not EC nothing to do */ | |
3aeb9348 | 724 | if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) |
0f113f3e | 725 | return 1; |
3aeb9348 | 726 | rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)); |
0f113f3e MC |
727 | if (!rv) |
728 | return 0; | |
729 | /* | |
730 | * Can't check curve_id for client certs as we don't have a supported | |
731 | * curves extension. | |
732 | */ | |
733 | rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id); | |
734 | if (!rv) | |
735 | return 0; | |
736 | /* | |
737 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or | |
738 | * SHA384+P-384, adjust digest if necessary. | |
739 | */ | |
740 | if (set_ee_md && tls1_suiteb(s)) { | |
741 | int check_md; | |
742 | size_t i; | |
743 | CERT *c = s->cert; | |
744 | if (curve_id[0]) | |
745 | return 0; | |
746 | /* Check to see we have necessary signing algorithm */ | |
747 | if (curve_id[1] == TLSEXT_curve_P_256) | |
748 | check_md = NID_ecdsa_with_SHA256; | |
749 | else if (curve_id[1] == TLSEXT_curve_P_384) | |
750 | check_md = NID_ecdsa_with_SHA384; | |
751 | else | |
752 | return 0; /* Should never happen */ | |
753 | for (i = 0; i < c->shared_sigalgslen; i++) | |
754 | if (check_md == c->shared_sigalgs[i].signandhash_nid) | |
755 | break; | |
756 | if (i == c->shared_sigalgslen) | |
757 | return 0; | |
758 | if (set_ee_md == 2) { | |
759 | if (check_md == NID_ecdsa_with_SHA256) | |
d376e57d | 760 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256(); |
0f113f3e | 761 | else |
d376e57d | 762 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384(); |
0f113f3e MC |
763 | } |
764 | } | |
765 | return rv; | |
766 | } | |
767 | ||
10bf4fc2 | 768 | # ifndef OPENSSL_NO_EC |
6977e8ee | 769 | /* |
e771eea6 | 770 | * tls1_check_ec_tmp_key - Check EC temporary key compatiblity |
6977e8ee KR |
771 | * @s: SSL connection |
772 | * @cid: Cipher ID we're considering using | |
773 | * | |
774 | * Checks that the kECDHE cipher suite we're considering using | |
775 | * is compatible with the client extensions. | |
776 | * | |
777 | * Returns 0 when the cipher can't be used or 1 when it can. | |
778 | */ | |
2ea80354 | 779 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
0f113f3e | 780 | { |
0f113f3e MC |
781 | /* |
782 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
783 | * curves permitted. | |
784 | */ | |
785 | if (tls1_suiteb(s)) { | |
6977e8ee | 786 | unsigned char curve_id[2]; |
0f113f3e MC |
787 | /* Curve to check determined by ciphersuite */ |
788 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
789 | curve_id[1] = TLSEXT_curve_P_256; | |
790 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
791 | curve_id[1] = TLSEXT_curve_P_384; | |
792 | else | |
793 | return 0; | |
794 | curve_id[0] = 0; | |
795 | /* Check this curve is acceptable */ | |
796 | if (!tls1_check_ec_key(s, curve_id, NULL)) | |
797 | return 0; | |
fe6ef247 | 798 | return 1; |
0f113f3e | 799 | } |
fe6ef247 KR |
800 | /* Need a shared curve */ |
801 | if (tls1_shared_curve(s, 0)) | |
802 | return 1; | |
6977e8ee | 803 | return 0; |
0f113f3e | 804 | } |
10bf4fc2 | 805 | # endif /* OPENSSL_NO_EC */ |
d0595f17 | 806 | |
14536c8c DSH |
807 | #else |
808 | ||
809 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) | |
0f113f3e MC |
810 | { |
811 | return 1; | |
812 | } | |
14536c8c | 813 | |
0f113f3e | 814 | #endif /* OPENSSL_NO_EC */ |
f1fd4544 | 815 | |
0f113f3e MC |
816 | /* |
817 | * List of supported signature algorithms and hashes. Should make this | |
fc101f88 DSH |
818 | * customisable at some point, for now include everything we support. |
819 | */ | |
820 | ||
e481f9b9 MC |
821 | #ifdef OPENSSL_NO_RSA |
822 | # define tlsext_sigalg_rsa(md) /* */ | |
823 | #else | |
824 | # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, | |
825 | #endif | |
0f113f3e | 826 | |
e481f9b9 MC |
827 | #ifdef OPENSSL_NO_DSA |
828 | # define tlsext_sigalg_dsa(md) /* */ | |
829 | #else | |
830 | # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa, | |
831 | #endif | |
0f113f3e | 832 | |
e481f9b9 MC |
833 | #ifdef OPENSSL_NO_EC |
834 | # define tlsext_sigalg_ecdsa(md) /* */ | |
835 | #else | |
836 | # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, | |
837 | #endif | |
0f113f3e | 838 | |
e481f9b9 | 839 | #define tlsext_sigalg(md) \ |
0f113f3e MC |
840 | tlsext_sigalg_rsa(md) \ |
841 | tlsext_sigalg_dsa(md) \ | |
842 | tlsext_sigalg_ecdsa(md) | |
fc101f88 | 843 | |
d97ed219 | 844 | static const unsigned char tls12_sigalgs[] = { |
0f113f3e MC |
845 | tlsext_sigalg(TLSEXT_hash_sha512) |
846 | tlsext_sigalg(TLSEXT_hash_sha384) | |
0f113f3e MC |
847 | tlsext_sigalg(TLSEXT_hash_sha256) |
848 | tlsext_sigalg(TLSEXT_hash_sha224) | |
0f113f3e | 849 | tlsext_sigalg(TLSEXT_hash_sha1) |
e44380a9 DB |
850 | #ifndef OPENSSL_NO_GOST |
851 | TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001, | |
852 | TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256, | |
853 | TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512 | |
854 | #endif | |
fc101f88 | 855 | }; |
0f113f3e | 856 | |
e481f9b9 | 857 | #ifndef OPENSSL_NO_EC |
d97ed219 | 858 | static const unsigned char suiteb_sigalgs[] = { |
0f113f3e MC |
859 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha256) |
860 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha384) | |
2ea80354 | 861 | }; |
e481f9b9 | 862 | #endif |
b7bfe69b | 863 | size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs) |
0f113f3e MC |
864 | { |
865 | /* | |
866 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
867 | * preferences. | |
868 | */ | |
e481f9b9 | 869 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
870 | switch (tls1_suiteb(s)) { |
871 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
872 | *psigs = suiteb_sigalgs; | |
873 | return sizeof(suiteb_sigalgs); | |
874 | ||
875 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
876 | *psigs = suiteb_sigalgs; | |
877 | return 2; | |
878 | ||
879 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
880 | *psigs = suiteb_sigalgs + 2; | |
881 | return 2; | |
882 | } | |
e481f9b9 | 883 | #endif |
0f113f3e MC |
884 | /* If server use client authentication sigalgs if not NULL */ |
885 | if (s->server && s->cert->client_sigalgs) { | |
886 | *psigs = s->cert->client_sigalgs; | |
887 | return s->cert->client_sigalgslen; | |
888 | } else if (s->cert->conf_sigalgs) { | |
889 | *psigs = s->cert->conf_sigalgs; | |
890 | return s->cert->conf_sigalgslen; | |
891 | } else { | |
892 | *psigs = tls12_sigalgs; | |
893 | return sizeof(tls12_sigalgs); | |
894 | } | |
895 | } | |
896 | ||
897 | /* | |
898 | * Check signature algorithm is consistent with sent supported signature | |
ec4a50b3 DSH |
899 | * algorithms and if so return relevant digest. |
900 | */ | |
901 | int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s, | |
0f113f3e MC |
902 | const unsigned char *sig, EVP_PKEY *pkey) |
903 | { | |
904 | const unsigned char *sent_sigs; | |
905 | size_t sent_sigslen, i; | |
906 | int sigalg = tls12_get_sigid(pkey); | |
907 | /* Should never happen */ | |
908 | if (sigalg == -1) | |
909 | return -1; | |
910 | /* Check key type is consistent with signature */ | |
911 | if (sigalg != (int)sig[1]) { | |
912 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
913 | return 0; | |
914 | } | |
e481f9b9 | 915 | #ifndef OPENSSL_NO_EC |
3aeb9348 | 916 | if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { |
0f113f3e MC |
917 | unsigned char curve_id[2], comp_id; |
918 | /* Check compression and curve matches extensions */ | |
3aeb9348 | 919 | if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey))) |
0f113f3e MC |
920 | return 0; |
921 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { | |
922 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
923 | return 0; | |
924 | } | |
925 | /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */ | |
926 | if (tls1_suiteb(s)) { | |
927 | if (curve_id[0]) | |
928 | return 0; | |
929 | if (curve_id[1] == TLSEXT_curve_P_256) { | |
930 | if (sig[0] != TLSEXT_hash_sha256) { | |
931 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
932 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
933 | return 0; | |
934 | } | |
935 | } else if (curve_id[1] == TLSEXT_curve_P_384) { | |
936 | if (sig[0] != TLSEXT_hash_sha384) { | |
937 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
938 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
939 | return 0; | |
940 | } | |
941 | } else | |
942 | return 0; | |
943 | } | |
944 | } else if (tls1_suiteb(s)) | |
945 | return 0; | |
e481f9b9 | 946 | #endif |
0f113f3e MC |
947 | |
948 | /* Check signature matches a type we sent */ | |
949 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); | |
950 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { | |
951 | if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1]) | |
952 | break; | |
953 | } | |
954 | /* Allow fallback to SHA1 if not strict mode */ | |
955 | if (i == sent_sigslen | |
956 | && (sig[0] != TLSEXT_hash_sha1 | |
957 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
958 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
959 | return 0; | |
960 | } | |
961 | *pmd = tls12_get_hash(sig[0]); | |
962 | if (*pmd == NULL) { | |
963 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); | |
964 | return 0; | |
965 | } | |
966 | /* Make sure security callback allows algorithm */ | |
967 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, | |
968 | EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), | |
969 | (void *)sig)) { | |
970 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
971 | return 0; | |
972 | } | |
973 | /* | |
974 | * Store the digest used so applications can retrieve it if they wish. | |
975 | */ | |
d376e57d | 976 | s->s3->tmp.peer_md = *pmd; |
0f113f3e MC |
977 | return 1; |
978 | } | |
2ea80354 | 979 | |
0f113f3e | 980 | /* |
3eb2aff4 KR |
981 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
982 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
983 | * by the enabled protocol versions or by the security level. | |
984 | * | |
985 | * This function should only be used for checking which ciphers are supported | |
986 | * by the client. | |
987 | * | |
988 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b DSH |
989 | */ |
990 | void ssl_set_client_disabled(SSL *s) | |
0f113f3e | 991 | { |
4d69f9e6 DSH |
992 | s->s3->tmp.mask_a = 0; |
993 | s->s3->tmp.mask_k = 0; | |
4d69f9e6 | 994 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
3eb2aff4 | 995 | ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
0f113f3e MC |
996 | # ifndef OPENSSL_NO_PSK |
997 | /* with PSK there must be client callback set */ | |
998 | if (!s->psk_client_callback) { | |
4d69f9e6 | 999 | s->s3->tmp.mask_a |= SSL_aPSK; |
fe5eef3a | 1000 | s->s3->tmp.mask_k |= SSL_PSK; |
0f113f3e | 1001 | } |
e481f9b9 MC |
1002 | #endif /* OPENSSL_NO_PSK */ |
1003 | #ifndef OPENSSL_NO_SRP | |
0f113f3e | 1004 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
4d69f9e6 DSH |
1005 | s->s3->tmp.mask_a |= SSL_aSRP; |
1006 | s->s3->tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 1007 | } |
e481f9b9 | 1008 | #endif |
0f113f3e | 1009 | } |
fc101f88 | 1010 | |
3eb2aff4 KR |
1011 | /* |
1012 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
1013 | * @s: SSL connection that you want to use the cipher on | |
1014 | * @c: cipher to check | |
1015 | * @op: Security check that you want to do | |
1016 | * | |
1017 | * Returns 1 when it's disabled, 0 when enabled. | |
1018 | */ | |
b362ccab | 1019 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
0f113f3e | 1020 | { |
3eb2aff4 | 1021 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
4d69f9e6 | 1022 | || c->algorithm_auth & s->s3->tmp.mask_a) |
0f113f3e | 1023 | return 1; |
3eb2aff4 KR |
1024 | if (s->s3->tmp.max_ver == 0) |
1025 | return 1; | |
1026 | if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver) | |
1027 | || (c->max_tls < s->s3->tmp.min_ver))) | |
1028 | return 1; | |
1029 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) | |
1030 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) | |
1031 | return 1; | |
1032 | ||
0f113f3e MC |
1033 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
1034 | } | |
b362ccab DSH |
1035 | |
1036 | static int tls_use_ticket(SSL *s) | |
0f113f3e MC |
1037 | { |
1038 | if (s->options & SSL_OP_NO_TICKET) | |
1039 | return 0; | |
1040 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
1041 | } | |
ed3883d2 | 1042 | |
aa474d1f EK |
1043 | static int compare_uint(const void *p1, const void *p2) { |
1044 | unsigned int u1 = *((const unsigned int *)p1); | |
1045 | unsigned int u2 = *((const unsigned int *)p2); | |
1046 | if (u1 < u2) | |
1047 | return -1; | |
1048 | else if (u1 > u2) | |
1049 | return 1; | |
1050 | else | |
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | /* | |
1055 | * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be | |
1056 | * more than one extension of the same type in a ClientHello or ServerHello. | |
1057 | * This function does an initial scan over the extensions block to filter those | |
1058 | * out. It returns 1 if all extensions are unique, and 0 if the extensions | |
1059 | * contain duplicates, could not be successfully parsed, or an internal error | |
1060 | * occurred. | |
1061 | */ | |
1062 | static int tls1_check_duplicate_extensions(const PACKET *packet) { | |
1063 | PACKET extensions = *packet; | |
1064 | size_t num_extensions = 0, i = 0; | |
1065 | unsigned int *extension_types = NULL; | |
1066 | int ret = 0; | |
1067 | ||
1068 | /* First pass: count the extensions. */ | |
1069 | while (PACKET_remaining(&extensions) > 0) { | |
1070 | unsigned int type; | |
1071 | PACKET extension; | |
1072 | if (!PACKET_get_net_2(&extensions, &type) || | |
1073 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { | |
1074 | goto done; | |
1075 | } | |
1076 | num_extensions++; | |
1077 | } | |
1078 | ||
1079 | if (num_extensions <= 1) | |
1080 | return 1; | |
1081 | ||
1082 | extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions); | |
1083 | if (extension_types == NULL) { | |
1084 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE); | |
1085 | goto done; | |
1086 | } | |
1087 | ||
1088 | /* Second pass: gather the extension types. */ | |
1089 | extensions = *packet; | |
1090 | for (i = 0; i < num_extensions; i++) { | |
1091 | PACKET extension; | |
1092 | if (!PACKET_get_net_2(&extensions, &extension_types[i]) || | |
1093 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { | |
1094 | /* This should not happen. */ | |
1095 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); | |
1096 | goto done; | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | if (PACKET_remaining(&extensions) != 0) { | |
1101 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); | |
1102 | goto done; | |
1103 | } | |
1104 | /* Sort the extensions and make sure there are no duplicates. */ | |
1105 | qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint); | |
1106 | for (i = 1; i < num_extensions; i++) { | |
1107 | if (extension_types[i - 1] == extension_types[i]) | |
1108 | goto done; | |
1109 | } | |
1110 | ret = 1; | |
1111 | done: | |
1112 | OPENSSL_free(extension_types); | |
1113 | return ret; | |
1114 | } | |
1115 | ||
0f113f3e MC |
1116 | unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, |
1117 | unsigned char *limit, int *al) | |
1118 | { | |
1119 | int extdatalen = 0; | |
1120 | unsigned char *orig = buf; | |
1121 | unsigned char *ret = buf; | |
e481f9b9 | 1122 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1123 | /* See if we support any ECC ciphersuites */ |
1124 | int using_ecc = 0; | |
1125 | if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) { | |
1126 | int i; | |
1127 | unsigned long alg_k, alg_a; | |
1128 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); | |
1129 | ||
1130 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { | |
4a640fb6 | 1131 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
0f113f3e MC |
1132 | |
1133 | alg_k = c->algorithm_mkey; | |
1134 | alg_a = c->algorithm_auth; | |
ce0c1f2b DSH |
1135 | if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) |
1136 | || (alg_a & SSL_aECDSA)) { | |
0f113f3e MC |
1137 | using_ecc = 1; |
1138 | break; | |
1139 | } | |
1140 | } | |
1141 | } | |
e481f9b9 | 1142 | #endif |
ed3883d2 | 1143 | |
0f113f3e | 1144 | ret += 2; |
6434abbf | 1145 | |
0f113f3e MC |
1146 | if (ret >= limit) |
1147 | return NULL; /* this really never occurs, but ... */ | |
5a3d8eeb | 1148 | |
0f113f3e MC |
1149 | /* Add RI if renegotiating */ |
1150 | if (s->renegotiate) { | |
1151 | int el; | |
5a3d8eeb | 1152 | |
0f113f3e MC |
1153 | if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) { |
1154 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1155 | return NULL; | |
1156 | } | |
5a3d8eeb | 1157 | |
0f113f3e MC |
1158 | if ((limit - ret - 4 - el) < 0) |
1159 | return NULL; | |
5a3d8eeb | 1160 | |
0f113f3e MC |
1161 | s2n(TLSEXT_TYPE_renegotiate, ret); |
1162 | s2n(el, ret); | |
5a3d8eeb | 1163 | |
0f113f3e MC |
1164 | if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) { |
1165 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1166 | return NULL; | |
5a3d8eeb | 1167 | } |
edc032b5 | 1168 | |
0f113f3e MC |
1169 | ret += el; |
1170 | } | |
1171 | /* Only add RI for SSLv3 */ | |
1172 | if (s->client_version == SSL3_VERSION) | |
1173 | goto done; | |
1174 | ||
1175 | if (s->tlsext_hostname != NULL) { | |
1176 | /* Add TLS extension servername to the Client Hello message */ | |
1177 | unsigned long size_str; | |
1178 | long lenmax; | |
1179 | ||
50e735f9 MC |
1180 | /*- |
1181 | * check for enough space. | |
e771eea6 | 1182 | * 4 for the servername type and entension length |
50e735f9 MC |
1183 | * 2 for servernamelist length |
1184 | * 1 for the hostname type | |
1185 | * 2 for hostname length | |
1186 | * + hostname length | |
1187 | */ | |
0f113f3e MC |
1188 | |
1189 | if ((lenmax = limit - ret - 9) < 0 | |
1190 | || (size_str = | |
1191 | strlen(s->tlsext_hostname)) > (unsigned long)lenmax) | |
1192 | return NULL; | |
1193 | ||
1194 | /* extension type and length */ | |
1195 | s2n(TLSEXT_TYPE_server_name, ret); | |
1196 | s2n(size_str + 5, ret); | |
1197 | ||
1198 | /* length of servername list */ | |
1199 | s2n(size_str + 3, ret); | |
1200 | ||
1201 | /* hostname type, length and hostname */ | |
1202 | *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name; | |
1203 | s2n(size_str, ret); | |
1204 | memcpy(ret, s->tlsext_hostname, size_str); | |
1205 | ret += size_str; | |
1206 | } | |
e481f9b9 | 1207 | #ifndef OPENSSL_NO_SRP |
0f113f3e MC |
1208 | /* Add SRP username if there is one */ |
1209 | if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the | |
1210 | * Client Hello message */ | |
1211 | ||
1212 | int login_len = strlen(s->srp_ctx.login); | |
1213 | if (login_len > 255 || login_len == 0) { | |
1214 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1215 | return NULL; | |
1216 | } | |
761772d7 | 1217 | |
50e735f9 MC |
1218 | /*- |
1219 | * check for enough space. | |
e771eea6 | 1220 | * 4 for the srp type type and entension length |
50e735f9 MC |
1221 | * 1 for the srp user identity |
1222 | * + srp user identity length | |
1223 | */ | |
0f113f3e MC |
1224 | if ((limit - ret - 5 - login_len) < 0) |
1225 | return NULL; | |
1226 | ||
1227 | /* fill in the extension */ | |
1228 | s2n(TLSEXT_TYPE_srp, ret); | |
1229 | s2n(login_len + 1, ret); | |
1230 | (*ret++) = (unsigned char)login_len; | |
1231 | memcpy(ret, s->srp_ctx.login, login_len); | |
1232 | ret += login_len; | |
1233 | } | |
e481f9b9 | 1234 | #endif |
0f113f3e | 1235 | |
e481f9b9 | 1236 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1237 | if (using_ecc) { |
1238 | /* | |
1239 | * Add TLS extension ECPointFormats to the ClientHello message | |
1240 | */ | |
1241 | long lenmax; | |
1242 | const unsigned char *pcurves, *pformats; | |
1243 | size_t num_curves, num_formats, curves_list_len; | |
1244 | size_t i; | |
1245 | unsigned char *etmp; | |
1246 | ||
1247 | tls1_get_formatlist(s, &pformats, &num_formats); | |
1248 | ||
1249 | if ((lenmax = limit - ret - 5) < 0) | |
1250 | return NULL; | |
1251 | if (num_formats > (size_t)lenmax) | |
1252 | return NULL; | |
1253 | if (num_formats > 255) { | |
1254 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1255 | return NULL; | |
1256 | } | |
4817504d | 1257 | |
0f113f3e MC |
1258 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
1259 | /* The point format list has 1-byte length. */ | |
1260 | s2n(num_formats + 1, ret); | |
1261 | *(ret++) = (unsigned char)num_formats; | |
1262 | memcpy(ret, pformats, num_formats); | |
1263 | ret += num_formats; | |
1264 | ||
1265 | /* | |
1266 | * Add TLS extension EllipticCurves to the ClientHello message | |
1267 | */ | |
1268 | pcurves = s->tlsext_ellipticcurvelist; | |
1269 | if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) | |
1270 | return NULL; | |
1271 | ||
1272 | if ((lenmax = limit - ret - 6) < 0) | |
1273 | return NULL; | |
1274 | if (num_curves > (size_t)lenmax / 2) | |
1275 | return NULL; | |
1276 | if (num_curves > 65532 / 2) { | |
1277 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1278 | return NULL; | |
1279 | } | |
ee2ffc27 | 1280 | |
0f113f3e MC |
1281 | s2n(TLSEXT_TYPE_elliptic_curves, ret); |
1282 | etmp = ret + 4; | |
1283 | /* Copy curve ID if supported */ | |
1284 | for (i = 0; i < num_curves; i++, pcurves += 2) { | |
1285 | if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) { | |
1286 | *etmp++ = pcurves[0]; | |
1287 | *etmp++ = pcurves[1]; | |
1288 | } | |
1289 | } | |
01f2f18f | 1290 | |
0f113f3e MC |
1291 | curves_list_len = etmp - ret - 4; |
1292 | ||
1293 | s2n(curves_list_len + 2, ret); | |
1294 | s2n(curves_list_len, ret); | |
1295 | ret += curves_list_len; | |
1296 | } | |
e481f9b9 | 1297 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
1298 | |
1299 | if (tls_use_ticket(s)) { | |
1300 | int ticklen; | |
1301 | if (!s->new_session && s->session && s->session->tlsext_tick) | |
1302 | ticklen = s->session->tlsext_ticklen; | |
1303 | else if (s->session && s->tlsext_session_ticket && | |
1304 | s->tlsext_session_ticket->data) { | |
1305 | ticklen = s->tlsext_session_ticket->length; | |
1306 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); | |
a71edf3b | 1307 | if (s->session->tlsext_tick == NULL) |
0f113f3e MC |
1308 | return NULL; |
1309 | memcpy(s->session->tlsext_tick, | |
1310 | s->tlsext_session_ticket->data, ticklen); | |
1311 | s->session->tlsext_ticklen = ticklen; | |
1312 | } else | |
1313 | ticklen = 0; | |
1314 | if (ticklen == 0 && s->tlsext_session_ticket && | |
1315 | s->tlsext_session_ticket->data == NULL) | |
1316 | goto skip_ext; | |
1317 | /* | |
1318 | * Check for enough room 2 for extension type, 2 for len rest for | |
1319 | * ticket | |
1320 | */ | |
1321 | if ((long)(limit - ret - 4 - ticklen) < 0) | |
1322 | return NULL; | |
1323 | s2n(TLSEXT_TYPE_session_ticket, ret); | |
1324 | s2n(ticklen, ret); | |
1325 | if (ticklen) { | |
1326 | memcpy(ret, s->session->tlsext_tick, ticklen); | |
1327 | ret += ticklen; | |
1328 | } | |
1329 | } | |
1330 | skip_ext: | |
1331 | ||
1332 | if (SSL_USE_SIGALGS(s)) { | |
1333 | size_t salglen; | |
1334 | const unsigned char *salg; | |
1335 | unsigned char *etmp; | |
1336 | salglen = tls12_get_psigalgs(s, &salg); | |
1337 | if ((size_t)(limit - ret) < salglen + 6) | |
1338 | return NULL; | |
1339 | s2n(TLSEXT_TYPE_signature_algorithms, ret); | |
1340 | etmp = ret; | |
1341 | /* Skip over lengths for now */ | |
1342 | ret += 4; | |
1343 | salglen = tls12_copy_sigalgs(s, ret, salg, salglen); | |
1344 | /* Fill in lengths */ | |
1345 | s2n(salglen + 2, etmp); | |
1346 | s2n(salglen, etmp); | |
1347 | ret += salglen; | |
1348 | } | |
0f113f3e | 1349 | |
3e41ac35 | 1350 | #ifndef OPENSSL_NO_OCSP |
0f113f3e MC |
1351 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { |
1352 | int i; | |
1353 | long extlen, idlen, itmp; | |
1354 | OCSP_RESPID *id; | |
1355 | ||
1356 | idlen = 0; | |
1357 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | |
1358 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); | |
1359 | itmp = i2d_OCSP_RESPID(id, NULL); | |
1360 | if (itmp <= 0) | |
1361 | return NULL; | |
1362 | idlen += itmp + 2; | |
860c3dd1 DSH |
1363 | } |
1364 | ||
0f113f3e MC |
1365 | if (s->tlsext_ocsp_exts) { |
1366 | extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); | |
1367 | if (extlen < 0) | |
1368 | return NULL; | |
1369 | } else | |
1370 | extlen = 0; | |
1371 | ||
1372 | if ((long)(limit - ret - 7 - extlen - idlen) < 0) | |
1373 | return NULL; | |
1374 | s2n(TLSEXT_TYPE_status_request, ret); | |
1375 | if (extlen + idlen > 0xFFF0) | |
1376 | return NULL; | |
1377 | s2n(extlen + idlen + 5, ret); | |
1378 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; | |
1379 | s2n(idlen, ret); | |
1380 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | |
1381 | /* save position of id len */ | |
1382 | unsigned char *q = ret; | |
1383 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); | |
1384 | /* skip over id len */ | |
1385 | ret += 2; | |
1386 | itmp = i2d_OCSP_RESPID(id, &ret); | |
1387 | /* write id len */ | |
1388 | s2n(itmp, q); | |
1389 | } | |
1390 | s2n(extlen, ret); | |
1391 | if (extlen > 0) | |
1392 | i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret); | |
1393 | } | |
3e41ac35 | 1394 | #endif |
e481f9b9 | 1395 | #ifndef OPENSSL_NO_HEARTBEATS |
22e3dcb7 RS |
1396 | if (SSL_IS_DTLS(s)) { |
1397 | /* Add Heartbeat extension */ | |
1398 | if ((limit - ret - 4 - 1) < 0) | |
1399 | return NULL; | |
1400 | s2n(TLSEXT_TYPE_heartbeat, ret); | |
1401 | s2n(1, ret); | |
1402 | /*- | |
1403 | * Set mode: | |
1404 | * 1: peer may send requests | |
1405 | * 2: peer not allowed to send requests | |
1406 | */ | |
1407 | if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS) | |
1408 | *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS; | |
1409 | else | |
1410 | *(ret++) = SSL_DTLSEXT_HB_ENABLED; | |
1411 | } | |
e481f9b9 | 1412 | #endif |
0f113f3e | 1413 | |
e481f9b9 | 1414 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1415 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { |
1416 | /* | |
e771eea6 | 1417 | * The client advertises an emtpy extension to indicate its support |
0f113f3e MC |
1418 | * for Next Protocol Negotiation |
1419 | */ | |
1420 | if (limit - ret - 4 < 0) | |
1421 | return NULL; | |
1422 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | |
1423 | s2n(0, ret); | |
1424 | } | |
e481f9b9 | 1425 | #endif |
0f113f3e | 1426 | |
817cd0d5 TS |
1427 | /* |
1428 | * finish_md_len is non-zero during a renegotiation, so | |
1429 | * this avoids sending ALPN during the renegotiation | |
1430 | * (see longer comment below) | |
1431 | */ | |
0f113f3e MC |
1432 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) { |
1433 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) | |
1434 | return NULL; | |
1435 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); | |
1436 | s2n(2 + s->alpn_client_proto_list_len, ret); | |
1437 | s2n(s->alpn_client_proto_list_len, ret); | |
1438 | memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len); | |
1439 | ret += s->alpn_client_proto_list_len; | |
817cd0d5 | 1440 | s->s3->alpn_sent = 1; |
0f113f3e | 1441 | } |
e481f9b9 | 1442 | #ifndef OPENSSL_NO_SRTP |
0f113f3e MC |
1443 | if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) { |
1444 | int el; | |
1445 | ||
69f68237 MC |
1446 | /* Returns 0 on success!! */ |
1447 | if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) { | |
1448 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1449 | return NULL; | |
1450 | } | |
0f113f3e MC |
1451 | |
1452 | if ((limit - ret - 4 - el) < 0) | |
1453 | return NULL; | |
1454 | ||
1455 | s2n(TLSEXT_TYPE_use_srtp, ret); | |
1456 | s2n(el, ret); | |
1457 | ||
1458 | if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { | |
1459 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1460 | return NULL; | |
1461 | } | |
1462 | ret += el; | |
1463 | } | |
e481f9b9 | 1464 | #endif |
0f113f3e MC |
1465 | custom_ext_init(&s->cert->cli_ext); |
1466 | /* Add custom TLS Extensions to ClientHello */ | |
1467 | if (!custom_ext_add(s, 0, &ret, limit, al)) | |
1468 | return NULL; | |
e481f9b9 | 1469 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
0f113f3e MC |
1470 | s2n(TLSEXT_TYPE_encrypt_then_mac, ret); |
1471 | s2n(0, ret); | |
ed29e82a RP |
1472 | #endif |
1473 | #ifndef OPENSSL_NO_CT | |
1474 | if (s->ct_validation_callback != NULL) { | |
1475 | s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret); | |
1476 | s2n(0, ret); | |
1477 | } | |
e481f9b9 | 1478 | #endif |
ddc06b35 DSH |
1479 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
1480 | s2n(0, ret); | |
0f113f3e MC |
1481 | |
1482 | /* | |
1483 | * Add padding to workaround bugs in F5 terminators. See | |
1484 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this | |
1485 | * code works out the length of all existing extensions it MUST always | |
1486 | * appear last. | |
1487 | */ | |
1488 | if (s->options & SSL_OP_TLSEXT_PADDING) { | |
1489 | int hlen = ret - (unsigned char *)s->init_buf->data; | |
a3680c8f | 1490 | |
0f113f3e MC |
1491 | if (hlen > 0xff && hlen < 0x200) { |
1492 | hlen = 0x200 - hlen; | |
1493 | if (hlen >= 4) | |
1494 | hlen -= 4; | |
1495 | else | |
1496 | hlen = 0; | |
1497 | ||
1498 | s2n(TLSEXT_TYPE_padding, ret); | |
1499 | s2n(hlen, ret); | |
1500 | memset(ret, 0, hlen); | |
1501 | ret += hlen; | |
1502 | } | |
1503 | } | |
5a3d8eeb | 1504 | |
0f113f3e | 1505 | done: |
5a3d8eeb | 1506 | |
0f113f3e MC |
1507 | if ((extdatalen = ret - orig - 2) == 0) |
1508 | return orig; | |
5a3d8eeb | 1509 | |
0f113f3e MC |
1510 | s2n(extdatalen, orig); |
1511 | return ret; | |
1512 | } | |
333f926d | 1513 | |
0f113f3e MC |
1514 | unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, |
1515 | unsigned char *limit, int *al) | |
1516 | { | |
1517 | int extdatalen = 0; | |
1518 | unsigned char *orig = buf; | |
1519 | unsigned char *ret = buf; | |
e481f9b9 | 1520 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 1521 | int next_proto_neg_seen; |
e481f9b9 MC |
1522 | #endif |
1523 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
1524 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
1525 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
ce0c1f2b | 1526 | int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
0f113f3e | 1527 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
e481f9b9 | 1528 | #endif |
0f113f3e MC |
1529 | |
1530 | ret += 2; | |
1531 | if (ret >= limit) | |
1532 | return NULL; /* this really never occurs, but ... */ | |
1533 | ||
1534 | if (s->s3->send_connection_binding) { | |
1535 | int el; | |
1536 | ||
1537 | if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) { | |
1538 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1539 | return NULL; | |
1540 | } | |
333f926d | 1541 | |
0f113f3e MC |
1542 | if ((limit - ret - 4 - el) < 0) |
1543 | return NULL; | |
333f926d | 1544 | |
0f113f3e MC |
1545 | s2n(TLSEXT_TYPE_renegotiate, ret); |
1546 | s2n(el, ret); | |
333f926d | 1547 | |
0f113f3e MC |
1548 | if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) { |
1549 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1550 | return NULL; | |
1551 | } | |
333f926d | 1552 | |
0f113f3e MC |
1553 | ret += el; |
1554 | } | |
1555 | ||
1556 | /* Only add RI for SSLv3 */ | |
1557 | if (s->version == SSL3_VERSION) | |
1558 | goto done; | |
1559 | ||
1560 | if (!s->hit && s->servername_done == 1 | |
1561 | && s->session->tlsext_hostname != NULL) { | |
1562 | if ((long)(limit - ret - 4) < 0) | |
1563 | return NULL; | |
1564 | ||
1565 | s2n(TLSEXT_TYPE_server_name, ret); | |
1566 | s2n(0, ret); | |
1567 | } | |
e481f9b9 | 1568 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1569 | if (using_ecc) { |
1570 | const unsigned char *plist; | |
1571 | size_t plistlen; | |
1572 | /* | |
1573 | * Add TLS extension ECPointFormats to the ServerHello message | |
1574 | */ | |
1575 | long lenmax; | |
1576 | ||
1577 | tls1_get_formatlist(s, &plist, &plistlen); | |
1578 | ||
1579 | if ((lenmax = limit - ret - 5) < 0) | |
1580 | return NULL; | |
1581 | if (plistlen > (size_t)lenmax) | |
1582 | return NULL; | |
1583 | if (plistlen > 255) { | |
1584 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1585 | return NULL; | |
1586 | } | |
4817504d | 1587 | |
0f113f3e MC |
1588 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
1589 | s2n(plistlen + 1, ret); | |
1590 | *(ret++) = (unsigned char)plistlen; | |
1591 | memcpy(ret, plist, plistlen); | |
1592 | ret += plistlen; | |
1593 | ||
1594 | } | |
1595 | /* | |
1596 | * Currently the server should not respond with a SupportedCurves | |
1597 | * extension | |
1598 | */ | |
e481f9b9 | 1599 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
1600 | |
1601 | if (s->tlsext_ticket_expected && tls_use_ticket(s)) { | |
1602 | if ((long)(limit - ret - 4) < 0) | |
1603 | return NULL; | |
1604 | s2n(TLSEXT_TYPE_session_ticket, ret); | |
1605 | s2n(0, ret); | |
1606 | } | |
1607 | ||
1608 | if (s->tlsext_status_expected) { | |
1609 | if ((long)(limit - ret - 4) < 0) | |
1610 | return NULL; | |
1611 | s2n(TLSEXT_TYPE_status_request, ret); | |
1612 | s2n(0, ret); | |
1613 | } | |
0f113f3e | 1614 | |
e481f9b9 | 1615 | #ifndef OPENSSL_NO_SRTP |
0f113f3e MC |
1616 | if (SSL_IS_DTLS(s) && s->srtp_profile) { |
1617 | int el; | |
1618 | ||
69f68237 | 1619 | /* Returns 0 on success!! */ |
61986d32 | 1620 | if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) { |
69f68237 MC |
1621 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1622 | return NULL; | |
1623 | } | |
0f113f3e MC |
1624 | if ((limit - ret - 4 - el) < 0) |
1625 | return NULL; | |
1626 | ||
1627 | s2n(TLSEXT_TYPE_use_srtp, ret); | |
1628 | s2n(el, ret); | |
1629 | ||
1630 | if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { | |
1631 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1632 | return NULL; | |
1633 | } | |
1634 | ret += el; | |
1635 | } | |
e481f9b9 | 1636 | #endif |
0f113f3e MC |
1637 | |
1638 | if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80 | |
1639 | || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81) | |
1640 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { | |
1641 | const unsigned char cryptopro_ext[36] = { | |
1642 | 0xfd, 0xe8, /* 65000 */ | |
1643 | 0x00, 0x20, /* 32 bytes length */ | |
1644 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, | |
1645 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, | |
1646 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, | |
1647 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 | |
1648 | }; | |
1649 | if (limit - ret < 36) | |
1650 | return NULL; | |
1651 | memcpy(ret, cryptopro_ext, 36); | |
1652 | ret += 36; | |
1653 | ||
1654 | } | |
e481f9b9 | 1655 | #ifndef OPENSSL_NO_HEARTBEATS |
0f113f3e | 1656 | /* Add Heartbeat extension if we've received one */ |
22e3dcb7 | 1657 | if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) { |
0f113f3e MC |
1658 | if ((limit - ret - 4 - 1) < 0) |
1659 | return NULL; | |
1660 | s2n(TLSEXT_TYPE_heartbeat, ret); | |
1661 | s2n(1, ret); | |
50e735f9 MC |
1662 | /*- |
1663 | * Set mode: | |
1664 | * 1: peer may send requests | |
1665 | * 2: peer not allowed to send requests | |
1666 | */ | |
22e3dcb7 RS |
1667 | if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS) |
1668 | *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS; | |
0f113f3e | 1669 | else |
22e3dcb7 | 1670 | *(ret++) = SSL_DTLSEXT_HB_ENABLED; |
0f113f3e MC |
1671 | |
1672 | } | |
e481f9b9 | 1673 | #endif |
0f113f3e | 1674 | |
e481f9b9 | 1675 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1676 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
1677 | s->s3->next_proto_neg_seen = 0; | |
1678 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { | |
1679 | const unsigned char *npa; | |
1680 | unsigned int npalen; | |
1681 | int r; | |
1682 | ||
1683 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, | |
1684 | s-> | |
1685 | ctx->next_protos_advertised_cb_arg); | |
1686 | if (r == SSL_TLSEXT_ERR_OK) { | |
1687 | if ((long)(limit - ret - 4 - npalen) < 0) | |
1688 | return NULL; | |
1689 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | |
1690 | s2n(npalen, ret); | |
1691 | memcpy(ret, npa, npalen); | |
1692 | ret += npalen; | |
1693 | s->s3->next_proto_neg_seen = 1; | |
1694 | } | |
1695 | } | |
e481f9b9 | 1696 | #endif |
0f113f3e MC |
1697 | if (!custom_ext_add(s, 1, &ret, limit, al)) |
1698 | return NULL; | |
e481f9b9 | 1699 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
0f113f3e MC |
1700 | if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) { |
1701 | /* | |
1702 | * Don't use encrypt_then_mac if AEAD or RC4 might want to disable | |
1703 | * for other cases too. | |
1704 | */ | |
1705 | if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD | |
e44380a9 DB |
1706 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4 |
1707 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT | |
1708 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) | |
0f113f3e MC |
1709 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
1710 | else { | |
1711 | s2n(TLSEXT_TYPE_encrypt_then_mac, ret); | |
1712 | s2n(0, ret); | |
1713 | } | |
1714 | } | |
e481f9b9 | 1715 | #endif |
e7f0d921 | 1716 | if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) { |
ddc06b35 DSH |
1717 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
1718 | s2n(0, ret); | |
1719 | } | |
0f113f3e | 1720 | |
817cd0d5 | 1721 | if (s->s3->alpn_selected != NULL) { |
0f113f3e | 1722 | const unsigned char *selected = s->s3->alpn_selected; |
817cd0d5 | 1723 | unsigned int len = s->s3->alpn_selected_len; |
0f113f3e MC |
1724 | |
1725 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) | |
1726 | return NULL; | |
1727 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); | |
1728 | s2n(3 + len, ret); | |
1729 | s2n(1 + len, ret); | |
1730 | *ret++ = len; | |
1731 | memcpy(ret, selected, len); | |
1732 | ret += len; | |
1733 | } | |
1734 | ||
1735 | done: | |
1736 | ||
1737 | if ((extdatalen = ret - orig - 2) == 0) | |
1738 | return orig; | |
1739 | ||
1740 | s2n(extdatalen, orig); | |
1741 | return ret; | |
1742 | } | |
a398f821 | 1743 | |
0f113f3e | 1744 | /* |
817cd0d5 | 1745 | * Save the ALPN extension in a ClientHello. |
06217867 EK |
1746 | * pkt: the contents of the ALPN extension, not including type and length. |
1747 | * al: a pointer to the alert value to send in the event of a failure. | |
1748 | * returns: 1 on success, 0 on error. | |
0f113f3e | 1749 | */ |
9ceb2426 | 1750 | static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al) |
0f113f3e | 1751 | { |
06217867 | 1752 | PACKET protocol_list, save_protocol_list, protocol; |
0f113f3e | 1753 | |
06217867 | 1754 | *al = SSL_AD_DECODE_ERROR; |
0f113f3e | 1755 | |
06217867 EK |
1756 | if (!PACKET_as_length_prefixed_2(pkt, &protocol_list) |
1757 | || PACKET_remaining(&protocol_list) < 2) { | |
1758 | return 0; | |
1759 | } | |
0f113f3e | 1760 | |
06217867 | 1761 | save_protocol_list = protocol_list; |
9ceb2426 | 1762 | do { |
06217867 EK |
1763 | /* Protocol names can't be empty. */ |
1764 | if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol) | |
1765 | || PACKET_remaining(&protocol) == 0) { | |
1766 | return 0; | |
1767 | } | |
1768 | } while (PACKET_remaining(&protocol_list) != 0); | |
1769 | ||
817cd0d5 TS |
1770 | if (!PACKET_memdup(&save_protocol_list, |
1771 | &s->s3->alpn_proposed, | |
1772 | &s->s3->alpn_proposed_len)) { | |
1773 | *al = TLS1_AD_INTERNAL_ERROR; | |
1774 | return 0; | |
1775 | } | |
1776 | ||
1777 | return 1; | |
1778 | } | |
1779 | ||
1780 | /* | |
1781 | * Process the ALPN extension in a ClientHello. | |
1782 | * ret: a pointer to the TLSEXT return value: SSL_TLSEXT_ERR_* | |
1783 | * al: a pointer to the alert value to send in the event of a failure. | |
1784 | * returns 1 on success, 0 | |
1785 | */ | |
1786 | static int tls1_alpn_handle_client_hello_late(SSL *s, int *ret, int *al) | |
1787 | { | |
1788 | const unsigned char *selected = NULL; | |
1789 | unsigned char selected_len = 0; | |
1790 | ||
1791 | if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) { | |
1792 | int r = s->ctx->alpn_select_cb(s, &selected, &selected_len, | |
1793 | s->s3->alpn_proposed, | |
1794 | s->s3->alpn_proposed_len, | |
1795 | s->ctx->alpn_select_cb_arg); | |
0f113f3e | 1796 | |
817cd0d5 TS |
1797 | if (r == SSL_TLSEXT_ERR_OK) { |
1798 | OPENSSL_free(s->s3->alpn_selected); | |
1799 | s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len); | |
1800 | if (s->s3->alpn_selected == NULL) { | |
1801 | *al = SSL_AD_INTERNAL_ERROR; | |
1802 | *ret = SSL_TLSEXT_ERR_ALERT_FATAL; | |
1803 | return 0; | |
1804 | } | |
1805 | s->s3->alpn_selected_len = selected_len; | |
0351baae TS |
1806 | #ifndef OPENSSL_NO_NEXTPROTONEG |
1807 | /* ALPN takes precedence over NPN. */ | |
1808 | s->s3->next_proto_neg_seen = 0; | |
1809 | #endif | |
817cd0d5 TS |
1810 | } else { |
1811 | *al = SSL_AD_NO_APPLICATION_PROTOCOL; | |
1812 | *ret = SSL_TLSEXT_ERR_ALERT_FATAL; | |
06217867 | 1813 | return 0; |
0f113f3e | 1814 | } |
0f113f3e | 1815 | } |
0f113f3e | 1816 | |
06217867 | 1817 | return 1; |
0f113f3e | 1818 | } |
6f017a8f | 1819 | |
e481f9b9 | 1820 | #ifndef OPENSSL_NO_EC |
1d97c843 TH |
1821 | /*- |
1822 | * ssl_check_for_safari attempts to fingerprint Safari using OS X | |
06217867 | 1823 | * SecureTransport using the TLS extension block in |pkt|. |
dece3209 RS |
1824 | * Safari, since 10.6, sends exactly these extensions, in this order: |
1825 | * SNI, | |
1826 | * elliptic_curves | |
1827 | * ec_point_formats | |
1828 | * | |
1829 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, | |
1830 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. | |
1831 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from | |
1832 | * 10.8..10.8.3 (which don't work). | |
1833 | */ | |
68a16628 | 1834 | static void ssl_check_for_safari(SSL *s, const PACKET *pkt) |
0f113f3e | 1835 | { |
06217867 EK |
1836 | unsigned int type; |
1837 | PACKET sni, tmppkt; | |
1838 | size_t ext_len; | |
9ceb2426 | 1839 | |
0f113f3e MC |
1840 | static const unsigned char kSafariExtensionsBlock[] = { |
1841 | 0x00, 0x0a, /* elliptic_curves extension */ | |
1842 | 0x00, 0x08, /* 8 bytes */ | |
1843 | 0x00, 0x06, /* 6 bytes of curve ids */ | |
1844 | 0x00, 0x17, /* P-256 */ | |
1845 | 0x00, 0x18, /* P-384 */ | |
1846 | 0x00, 0x19, /* P-521 */ | |
1847 | ||
1848 | 0x00, 0x0b, /* ec_point_formats */ | |
1849 | 0x00, 0x02, /* 2 bytes */ | |
1850 | 0x01, /* 1 point format */ | |
1851 | 0x00, /* uncompressed */ | |
06217867 | 1852 | /* The following is only present in TLS 1.2 */ |
0f113f3e MC |
1853 | 0x00, 0x0d, /* signature_algorithms */ |
1854 | 0x00, 0x0c, /* 12 bytes */ | |
1855 | 0x00, 0x0a, /* 10 bytes */ | |
1856 | 0x05, 0x01, /* SHA-384/RSA */ | |
1857 | 0x04, 0x01, /* SHA-256/RSA */ | |
1858 | 0x02, 0x01, /* SHA-1/RSA */ | |
1859 | 0x04, 0x03, /* SHA-256/ECDSA */ | |
1860 | 0x02, 0x03, /* SHA-1/ECDSA */ | |
1861 | }; | |
1862 | ||
06217867 EK |
1863 | /* Length of the common prefix (first two extensions). */ |
1864 | static const size_t kSafariCommonExtensionsLength = 18; | |
1865 | ||
68a16628 MC |
1866 | tmppkt = *pkt; |
1867 | ||
1868 | if (!PACKET_forward(&tmppkt, 2) | |
06217867 EK |
1869 | || !PACKET_get_net_2(&tmppkt, &type) |
1870 | || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { | |
0f113f3e | 1871 | return; |
06217867 | 1872 | } |
0f113f3e MC |
1873 | |
1874 | if (type != TLSEXT_TYPE_server_name) | |
1875 | return; | |
1876 | ||
06217867 EK |
1877 | ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ? |
1878 | sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength; | |
0f113f3e | 1879 | |
06217867 EK |
1880 | s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock, |
1881 | ext_len); | |
dece3209 | 1882 | } |
e481f9b9 | 1883 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e | 1884 | |
06217867 EK |
1885 | /* |
1886 | * Parse ClientHello extensions and stash extension info in various parts of | |
1887 | * the SSL object. Verify that there are no duplicate extensions. | |
1888 | * | |
1889 | * Behaviour upon resumption is extension-specific. If the extension has no | |
1890 | * effect during resumption, it is parsed (to verify its format) but otherwise | |
1891 | * ignored. | |
1892 | * | |
1893 | * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure. | |
1894 | * Upon failure, sets |al| to the appropriate alert. | |
1895 | */ | |
9ceb2426 | 1896 | static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al) |
0f113f3e | 1897 | { |
9ceb2426 | 1898 | unsigned int type; |
0f113f3e | 1899 | int renegotiate_seen = 0; |
06217867 | 1900 | PACKET extensions; |
0f113f3e | 1901 | |
06217867 | 1902 | *al = SSL_AD_DECODE_ERROR; |
0f113f3e MC |
1903 | s->servername_done = 0; |
1904 | s->tlsext_status_type = -1; | |
e481f9b9 | 1905 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 1906 | s->s3->next_proto_neg_seen = 0; |
e481f9b9 | 1907 | #endif |
0f113f3e | 1908 | |
b548a1f1 RS |
1909 | OPENSSL_free(s->s3->alpn_selected); |
1910 | s->s3->alpn_selected = NULL; | |
0351baae TS |
1911 | s->s3->alpn_selected_len = 0; |
1912 | OPENSSL_free(s->s3->alpn_proposed); | |
1913 | s->s3->alpn_proposed = NULL; | |
1914 | s->s3->alpn_proposed_len = 0; | |
e481f9b9 | 1915 | #ifndef OPENSSL_NO_HEARTBEATS |
22e3dcb7 RS |
1916 | s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED | |
1917 | SSL_DTLSEXT_HB_DONT_SEND_REQUESTS); | |
e481f9b9 | 1918 | #endif |
0f113f3e | 1919 | |
e481f9b9 | 1920 | #ifndef OPENSSL_NO_EC |
0f113f3e | 1921 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
9ceb2426 MC |
1922 | ssl_check_for_safari(s, pkt); |
1923 | # endif /* !OPENSSL_NO_EC */ | |
0f113f3e MC |
1924 | |
1925 | /* Clear any signature algorithms extension received */ | |
76106e60 DSH |
1926 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
1927 | s->s3->tmp.peer_sigalgs = NULL; | |
e481f9b9 | 1928 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
0f113f3e | 1929 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
e481f9b9 | 1930 | #endif |
0f113f3e | 1931 | |
e481f9b9 | 1932 | #ifndef OPENSSL_NO_SRP |
b548a1f1 RS |
1933 | OPENSSL_free(s->srp_ctx.login); |
1934 | s->srp_ctx.login = NULL; | |
e481f9b9 | 1935 | #endif |
0f113f3e MC |
1936 | |
1937 | s->srtp_profile = NULL; | |
1938 | ||
9ceb2426 | 1939 | if (PACKET_remaining(pkt) == 0) |
1ae3fdbe AL |
1940 | goto ri_check; |
1941 | ||
06217867 EK |
1942 | if (!PACKET_as_length_prefixed_2(pkt, &extensions)) |
1943 | return 0; | |
aa474d1f | 1944 | |
06217867 EK |
1945 | if (!tls1_check_duplicate_extensions(&extensions)) |
1946 | return 0; | |
0f113f3e | 1947 | |
06217867 EK |
1948 | /* |
1949 | * We parse all extensions to ensure the ClientHello is well-formed but, | |
1950 | * unless an extension specifies otherwise, we ignore extensions upon | |
1951 | * resumption. | |
1952 | */ | |
1953 | while (PACKET_get_net_2(&extensions, &type)) { | |
1954 | PACKET extension; | |
1955 | if (!PACKET_get_length_prefixed_2(&extensions, &extension)) | |
1956 | return 0; | |
9ceb2426 | 1957 | |
0f113f3e | 1958 | if (s->tlsext_debug_cb) |
06217867 EK |
1959 | s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension), |
1960 | PACKET_remaining(&extension), | |
1961 | s->tlsext_debug_arg); | |
9ceb2426 | 1962 | |
0f113f3e | 1963 | if (type == TLSEXT_TYPE_renegotiate) { |
06217867 | 1964 | if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al)) |
0f113f3e MC |
1965 | return 0; |
1966 | renegotiate_seen = 1; | |
1967 | } else if (s->version == SSL3_VERSION) { | |
1968 | } | |
1d97c843 TH |
1969 | /*- |
1970 | * The servername extension is treated as follows: | |
1971 | * | |
1972 | * - Only the hostname type is supported with a maximum length of 255. | |
1973 | * - The servername is rejected if too long or if it contains zeros, | |
1974 | * in which case an fatal alert is generated. | |
1975 | * - The servername field is maintained together with the session cache. | |
1976 | * - When a session is resumed, the servername call back invoked in order | |
0f113f3e MC |
1977 | * to allow the application to position itself to the right context. |
1978 | * - The servername is acknowledged if it is new for a session or when | |
1979 | * it is identical to a previously used for the same session. | |
1d97c843 TH |
1980 | * Applications can control the behaviour. They can at any time |
1981 | * set a 'desirable' servername for a new SSL object. This can be the | |
1982 | * case for example with HTTPS when a Host: header field is received and | |
1983 | * a renegotiation is requested. In this case, a possible servername | |
1984 | * presented in the new client hello is only acknowledged if it matches | |
0f113f3e | 1985 | * the value of the Host: field. |
1d97c843 | 1986 | * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
0f113f3e MC |
1987 | * if they provide for changing an explicit servername context for the |
1988 | * session, i.e. when the session has been established with a servername | |
1989 | * extension. | |
1990 | * - On session reconnect, the servername extension may be absent. | |
1d97c843 | 1991 | * |
0f113f3e | 1992 | */ |
ed3883d2 | 1993 | |
0f113f3e | 1994 | else if (type == TLSEXT_TYPE_server_name) { |
9ceb2426 | 1995 | unsigned int servname_type; |
06217867 EK |
1996 | PACKET sni, hostname; |
1997 | ||
1998 | if (!PACKET_as_length_prefixed_2(&extension, &sni) | |
1999 | /* ServerNameList must be at least 1 byte long. */ | |
2000 | || PACKET_remaining(&sni) == 0) { | |
2001 | return 0; | |
0f113f3e | 2002 | } |
6f017a8f | 2003 | |
06217867 EK |
2004 | /* |
2005 | * Although the server_name extension was intended to be | |
2006 | * extensible to new name types, RFC 4366 defined the | |
e771eea6 | 2007 | * syntax inextensibly and OpenSSL 1.0.x parses it as |
06217867 EK |
2008 | * such. |
2009 | * RFC 6066 corrected the mistake but adding new name types | |
2010 | * is nevertheless no longer feasible, so act as if no other | |
2011 | * SNI types can exist, to simplify parsing. | |
2012 | * | |
2013 | * Also note that the RFC permits only one SNI value per type, | |
2014 | * i.e., we can only have a single hostname. | |
2015 | */ | |
2016 | if (!PACKET_get_1(&sni, &servname_type) | |
2017 | || servname_type != TLSEXT_NAMETYPE_host_name | |
2018 | || !PACKET_as_length_prefixed_2(&sni, &hostname)) { | |
2019 | return 0; | |
2020 | } | |
2021 | ||
2022 | if (!s->hit) { | |
2023 | if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) { | |
2024 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2025 | return 0; | |
2026 | } | |
2027 | ||
2028 | if (PACKET_contains_zero_byte(&hostname)) { | |
2029 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2030 | return 0; | |
2031 | } | |
2032 | ||
2033 | if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) { | |
2034 | *al = TLS1_AD_INTERNAL_ERROR; | |
2035 | return 0; | |
2036 | } | |
2037 | ||
2038 | s->servername_done = 1; | |
2039 | } else { | |
2040 | /* | |
2041 | * TODO(openssl-team): if the SNI doesn't match, we MUST | |
2042 | * fall back to a full handshake. | |
2043 | */ | |
2044 | s->servername_done = s->session->tlsext_hostname | |
2045 | && PACKET_equal(&hostname, s->session->tlsext_hostname, | |
2046 | strlen(s->session->tlsext_hostname)); | |
2047 | } | |
0f113f3e | 2048 | } |
e481f9b9 | 2049 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 2050 | else if (type == TLSEXT_TYPE_srp) { |
06217867 EK |
2051 | PACKET srp_I; |
2052 | ||
2053 | if (!PACKET_as_length_prefixed_1(&extension, &srp_I)) | |
2054 | return 0; | |
2055 | ||
2056 | if (PACKET_contains_zero_byte(&srp_I)) | |
2057 | return 0; | |
2058 | ||
2059 | /* | |
2060 | * TODO(openssl-team): currently, we re-authenticate the user | |
2061 | * upon resumption. Instead, we MUST ignore the login. | |
2062 | */ | |
2063 | if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) { | |
2064 | *al = TLS1_AD_INTERNAL_ERROR; | |
2065 | return 0; | |
2066 | } | |
0f113f3e | 2067 | } |
e481f9b9 | 2068 | #endif |
0f113f3e | 2069 | |
e481f9b9 | 2070 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2071 | else if (type == TLSEXT_TYPE_ec_point_formats) { |
06217867 | 2072 | PACKET ec_point_format_list; |
0f113f3e | 2073 | |
06217867 EK |
2074 | if (!PACKET_as_length_prefixed_1(&extension, |
2075 | &ec_point_format_list) | |
2076 | || PACKET_remaining(&ec_point_format_list) == 0) { | |
2077 | return 0; | |
2078 | } | |
9ceb2426 | 2079 | |
0f113f3e | 2080 | if (!s->hit) { |
06217867 EK |
2081 | if (!PACKET_memdup(&ec_point_format_list, |
2082 | &s->session->tlsext_ecpointformatlist, | |
2083 | &s->session->tlsext_ecpointformatlist_length)) { | |
0f113f3e MC |
2084 | *al = TLS1_AD_INTERNAL_ERROR; |
2085 | return 0; | |
2086 | } | |
0f113f3e | 2087 | } |
0f113f3e | 2088 | } else if (type == TLSEXT_TYPE_elliptic_curves) { |
06217867 | 2089 | PACKET elliptic_curve_list; |
0f113f3e | 2090 | |
06217867 EK |
2091 | /* Each NamedCurve is 2 bytes and we must have at least 1. */ |
2092 | if (!PACKET_as_length_prefixed_2(&extension, | |
2093 | &elliptic_curve_list) | |
2094 | || PACKET_remaining(&elliptic_curve_list) == 0 | |
2095 | || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) { | |
2096 | return 0; | |
2097 | } | |
54e3ad00 | 2098 | |
0f113f3e | 2099 | if (!s->hit) { |
06217867 EK |
2100 | if (!PACKET_memdup(&elliptic_curve_list, |
2101 | &s->session->tlsext_ellipticcurvelist, | |
2102 | &s->session->tlsext_ellipticcurvelist_length)) { | |
0f113f3e MC |
2103 | *al = TLS1_AD_INTERNAL_ERROR; |
2104 | return 0; | |
2105 | } | |
0f113f3e | 2106 | } |
0f113f3e | 2107 | } |
e481f9b9 | 2108 | #endif /* OPENSSL_NO_EC */ |
0f113f3e | 2109 | else if (type == TLSEXT_TYPE_session_ticket) { |
06217867 EK |
2110 | if (s->tls_session_ticket_ext_cb && |
2111 | !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension), | |
2112 | PACKET_remaining(&extension), | |
2113 | s->tls_session_ticket_ext_cb_arg)) { | |
0f113f3e MC |
2114 | *al = TLS1_AD_INTERNAL_ERROR; |
2115 | return 0; | |
2116 | } | |
2117 | } else if (type == TLSEXT_TYPE_signature_algorithms) { | |
06217867 EK |
2118 | PACKET supported_sig_algs; |
2119 | ||
2120 | if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs) | |
2121 | || (PACKET_remaining(&supported_sig_algs) % 2) != 0 | |
2122 | || PACKET_remaining(&supported_sig_algs) == 0) { | |
2123 | return 0; | |
2124 | } | |
2125 | ||
2126 | if (!s->hit) { | |
2127 | if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs), | |
2128 | PACKET_remaining(&supported_sig_algs))) { | |
2129 | return 0; | |
2130 | } | |
9ceb2426 | 2131 | } |
0f113f3e | 2132 | } else if (type == TLSEXT_TYPE_status_request) { |
06217867 EK |
2133 | if (!PACKET_get_1(&extension, |
2134 | (unsigned int *)&s->tlsext_status_type)) { | |
2135 | return 0; | |
2136 | } | |
0f113f3e | 2137 | |
3e41ac35 | 2138 | #ifndef OPENSSL_NO_OCSP |
0f113f3e | 2139 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { |
3e41ac35 | 2140 | const unsigned char *ext_data; |
06217867 EK |
2141 | PACKET responder_id_list, exts; |
2142 | if (!PACKET_get_length_prefixed_2(&extension, &responder_id_list)) | |
2143 | return 0; | |
2144 | ||
2145 | while (PACKET_remaining(&responder_id_list) > 0) { | |
0f113f3e | 2146 | OCSP_RESPID *id; |
06217867 EK |
2147 | PACKET responder_id; |
2148 | const unsigned char *id_data; | |
9ceb2426 | 2149 | |
06217867 EK |
2150 | if (!PACKET_get_length_prefixed_2(&responder_id_list, |
2151 | &responder_id) | |
2152 | || PACKET_remaining(&responder_id) == 0) { | |
2153 | return 0; | |
9ceb2426 | 2154 | } |
06217867 EK |
2155 | |
2156 | if (s->tlsext_ocsp_ids == NULL | |
2157 | && (s->tlsext_ocsp_ids = | |
2158 | sk_OCSP_RESPID_new_null()) == NULL) { | |
2159 | *al = SSL_AD_INTERNAL_ERROR; | |
2160 | return 0; | |
0f113f3e | 2161 | } |
06217867 EK |
2162 | |
2163 | id_data = PACKET_data(&responder_id); | |
2164 | id = d2i_OCSP_RESPID(NULL, &id_data, | |
2165 | PACKET_remaining(&responder_id)); | |
2166 | if (id == NULL) | |
2167 | return 0; | |
2168 | ||
2169 | if (id_data != PACKET_end(&responder_id)) { | |
0f113f3e | 2170 | OCSP_RESPID_free(id); |
0f113f3e MC |
2171 | return 0; |
2172 | } | |
06217867 | 2173 | |
0f113f3e MC |
2174 | if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) { |
2175 | OCSP_RESPID_free(id); | |
2176 | *al = SSL_AD_INTERNAL_ERROR; | |
2177 | return 0; | |
2178 | } | |
2179 | } | |
4817504d | 2180 | |
0f113f3e | 2181 | /* Read in request_extensions */ |
06217867 EK |
2182 | if (!PACKET_as_length_prefixed_2(&extension, &exts)) |
2183 | return 0; | |
2184 | ||
2185 | if (PACKET_remaining(&exts) > 0) { | |
2186 | ext_data = PACKET_data(&exts); | |
222561fe RS |
2187 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, |
2188 | X509_EXTENSION_free); | |
0f113f3e | 2189 | s->tlsext_ocsp_exts = |
06217867 EK |
2190 | d2i_X509_EXTENSIONS(NULL, &ext_data, |
2191 | PACKET_remaining(&exts)); | |
2192 | if (s->tlsext_ocsp_exts == NULL | |
2193 | || ext_data != PACKET_end(&exts)) { | |
2194 | return 0; | |
2195 | } | |
0f113f3e | 2196 | } |
3e41ac35 MC |
2197 | } else |
2198 | #endif | |
2199 | { | |
2200 | /* | |
2201 | * We don't know what to do with any other type so ignore it. | |
2202 | */ | |
0f113f3e | 2203 | s->tlsext_status_type = -1; |
06217867 | 2204 | } |
0f113f3e | 2205 | } |
e481f9b9 | 2206 | #ifndef OPENSSL_NO_HEARTBEATS |
22e3dcb7 | 2207 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) { |
9ceb2426 MC |
2208 | unsigned int hbtype; |
2209 | ||
06217867 EK |
2210 | if (!PACKET_get_1(&extension, &hbtype) |
2211 | || PACKET_remaining(&extension)) { | |
9ceb2426 MC |
2212 | *al = SSL_AD_DECODE_ERROR; |
2213 | return 0; | |
2214 | } | |
2215 | switch (hbtype) { | |
0f113f3e | 2216 | case 0x01: /* Client allows us to send HB requests */ |
22e3dcb7 | 2217 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED; |
0f113f3e MC |
2218 | break; |
2219 | case 0x02: /* Client doesn't accept HB requests */ | |
22e3dcb7 RS |
2220 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED; |
2221 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS; | |
0f113f3e MC |
2222 | break; |
2223 | default: | |
2224 | *al = SSL_AD_ILLEGAL_PARAMETER; | |
2225 | return 0; | |
2226 | } | |
2227 | } | |
e481f9b9 MC |
2228 | #endif |
2229 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
0f113f3e | 2230 | else if (type == TLSEXT_TYPE_next_proto_neg && |
0351baae | 2231 | s->s3->tmp.finish_md_len == 0) { |
50e735f9 MC |
2232 | /*- |
2233 | * We shouldn't accept this extension on a | |
2234 | * renegotiation. | |
2235 | * | |
2236 | * s->new_session will be set on renegotiation, but we | |
2237 | * probably shouldn't rely that it couldn't be set on | |
e771eea6 | 2238 | * the initial renegotation too in certain cases (when |
50e735f9 MC |
2239 | * there's some other reason to disallow resuming an |
2240 | * earlier session -- the current code won't be doing | |
2241 | * anything like that, but this might change). | |
2242 | * | |
2243 | * A valid sign that there's been a previous handshake | |
2244 | * in this connection is if s->s3->tmp.finish_md_len > | |
2245 | * 0. (We are talking about a check that will happen | |
2246 | * in the Hello protocol round, well before a new | |
2247 | * Finished message could have been computed.) | |
2248 | */ | |
0f113f3e MC |
2249 | s->s3->next_proto_neg_seen = 1; |
2250 | } | |
e481f9b9 | 2251 | #endif |
0f113f3e MC |
2252 | |
2253 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && | |
06217867 EK |
2254 | s->s3->tmp.finish_md_len == 0) { |
2255 | if (!tls1_alpn_handle_client_hello(s, &extension, al)) | |
0f113f3e | 2256 | return 0; |
0f113f3e | 2257 | } |
5e3ff62c | 2258 | |
0f113f3e | 2259 | /* session ticket processed earlier */ |
e481f9b9 | 2260 | #ifndef OPENSSL_NO_SRTP |
0f113f3e MC |
2261 | else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s) |
2262 | && type == TLSEXT_TYPE_use_srtp) { | |
06217867 | 2263 | if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al)) |
0f113f3e MC |
2264 | return 0; |
2265 | } | |
e481f9b9 MC |
2266 | #endif |
2267 | #ifdef TLSEXT_TYPE_encrypt_then_mac | |
0f113f3e MC |
2268 | else if (type == TLSEXT_TYPE_encrypt_then_mac) |
2269 | s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; | |
e481f9b9 | 2270 | #endif |
e7f0d921 DSH |
2271 | /* |
2272 | * Note: extended master secret extension handled in | |
2273 | * tls_check_serverhello_tlsext_early() | |
2274 | */ | |
2275 | ||
0f113f3e MC |
2276 | /* |
2277 | * If this ClientHello extension was unhandled and this is a | |
2278 | * nonresumed connection, check whether the extension is a custom | |
2279 | * TLS Extension (has a custom_srv_ext_record), and if so call the | |
2280 | * callback and record the extension number so that an appropriate | |
2281 | * ServerHello may be later returned. | |
2282 | */ | |
2283 | else if (!s->hit) { | |
06217867 EK |
2284 | if (custom_ext_parse(s, 1, type, PACKET_data(&extension), |
2285 | PACKET_remaining(&extension), al) <= 0) | |
0f113f3e MC |
2286 | return 0; |
2287 | } | |
0f113f3e | 2288 | } |
6f017a8f | 2289 | |
06217867 EK |
2290 | if (PACKET_remaining(pkt) != 0) { |
2291 | /* tls1_check_duplicate_extensions should ensure this never happens. */ | |
2292 | *al = SSL_AD_INTERNAL_ERROR; | |
2293 | return 0; | |
2294 | } | |
54e3ad00 | 2295 | |
0f113f3e | 2296 | ri_check: |
ed3883d2 | 2297 | |
0f113f3e MC |
2298 | /* Need RI if renegotiating */ |
2299 | ||
2300 | if (!renegotiate_seen && s->renegotiate && | |
2301 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
2302 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2303 | SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT, | |
2304 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
2305 | return 0; | |
2306 | } | |
2307 | ||
06217867 EK |
2308 | /* |
2309 | * This function currently has no state to clean up, so it returns directly. | |
2310 | * If parsing fails at any point, the function returns early. | |
2311 | * The SSL object may be left with partial data from extensions, but it must | |
2312 | * then no longer be used, and clearing it up will free the leftovers. | |
2313 | */ | |
0f113f3e MC |
2314 | return 1; |
2315 | } | |
2316 | ||
9ceb2426 | 2317 | int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt) |
0f113f3e MC |
2318 | { |
2319 | int al = -1; | |
2320 | custom_ext_init(&s->cert->srv_ext); | |
9ceb2426 | 2321 | if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) { |
0f113f3e MC |
2322 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
2323 | return 0; | |
2324 | } | |
0f113f3e MC |
2325 | if (ssl_check_clienthello_tlsext_early(s) <= 0) { |
2326 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT); | |
2327 | return 0; | |
2328 | } | |
2329 | return 1; | |
2330 | } | |
2331 | ||
e481f9b9 | 2332 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2333 | /* |
2334 | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No | |
2335 | * elements of zero length are allowed and the set of elements must exactly | |
2336 | * fill the length of the block. | |
2337 | */ | |
50932c4a | 2338 | static char ssl_next_proto_validate(PACKET *pkt) |
0f113f3e | 2339 | { |
50932c4a | 2340 | unsigned int len; |
0f113f3e | 2341 | |
50932c4a MC |
2342 | while (PACKET_remaining(pkt)) { |
2343 | if (!PACKET_get_1(pkt, &len) | |
2344 | || !PACKET_forward(pkt, len)) | |
0f113f3e | 2345 | return 0; |
0f113f3e MC |
2346 | } |
2347 | ||
50932c4a | 2348 | return 1; |
0f113f3e | 2349 | } |
e481f9b9 | 2350 | #endif |
0f113f3e | 2351 | |
50932c4a | 2352 | static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al) |
0f113f3e | 2353 | { |
50932c4a | 2354 | unsigned int length, type, size; |
0f113f3e MC |
2355 | int tlsext_servername = 0; |
2356 | int renegotiate_seen = 0; | |
2357 | ||
e481f9b9 | 2358 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 2359 | s->s3->next_proto_neg_seen = 0; |
e481f9b9 | 2360 | #endif |
0f113f3e MC |
2361 | s->tlsext_ticket_expected = 0; |
2362 | ||
b548a1f1 RS |
2363 | OPENSSL_free(s->s3->alpn_selected); |
2364 | s->s3->alpn_selected = NULL; | |
e481f9b9 | 2365 | #ifndef OPENSSL_NO_HEARTBEATS |
22e3dcb7 RS |
2366 | s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED | |
2367 | SSL_DTLSEXT_HB_DONT_SEND_REQUESTS); | |
e481f9b9 | 2368 | #endif |
0f113f3e | 2369 | |
e481f9b9 | 2370 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
0f113f3e | 2371 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
e481f9b9 | 2372 | #endif |
0f113f3e | 2373 | |
e7f0d921 DSH |
2374 | s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; |
2375 | ||
50932c4a | 2376 | if (!PACKET_get_net_2(pkt, &length)) |
0f113f3e MC |
2377 | goto ri_check; |
2378 | ||
50932c4a | 2379 | if (PACKET_remaining(pkt) != length) { |
0f113f3e MC |
2380 | *al = SSL_AD_DECODE_ERROR; |
2381 | return 0; | |
2382 | } | |
2383 | ||
aa474d1f EK |
2384 | if (!tls1_check_duplicate_extensions(pkt)) { |
2385 | *al = SSL_AD_DECODE_ERROR; | |
2386 | return 0; | |
2387 | } | |
2388 | ||
50932c4a | 2389 | while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) { |
b6981744 | 2390 | const unsigned char *data; |
50932c4a | 2391 | PACKET spkt; |
0f113f3e | 2392 | |
50932c4a MC |
2393 | if (!PACKET_get_sub_packet(pkt, &spkt, size) |
2394 | || !PACKET_peek_bytes(&spkt, &data, size)) | |
0f113f3e MC |
2395 | goto ri_check; |
2396 | ||
2397 | if (s->tlsext_debug_cb) | |
2398 | s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg); | |
2399 | ||
2400 | if (type == TLSEXT_TYPE_renegotiate) { | |
50932c4a | 2401 | if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al)) |
0f113f3e MC |
2402 | return 0; |
2403 | renegotiate_seen = 1; | |
2404 | } else if (s->version == SSL3_VERSION) { | |
2405 | } else if (type == TLSEXT_TYPE_server_name) { | |
2406 | if (s->tlsext_hostname == NULL || size > 0) { | |
2407 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2408 | return 0; | |
2409 | } | |
2410 | tlsext_servername = 1; | |
2411 | } | |
e481f9b9 | 2412 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2413 | else if (type == TLSEXT_TYPE_ec_point_formats) { |
50932c4a MC |
2414 | unsigned int ecpointformatlist_length; |
2415 | if (!PACKET_get_1(&spkt, &ecpointformatlist_length) | |
2416 | || ecpointformatlist_length != size - 1) { | |
0f113f3e MC |
2417 | *al = TLS1_AD_DECODE_ERROR; |
2418 | return 0; | |
2419 | } | |
2420 | if (!s->hit) { | |
2421 | s->session->tlsext_ecpointformatlist_length = 0; | |
b548a1f1 | 2422 | OPENSSL_free(s->session->tlsext_ecpointformatlist); |
0f113f3e MC |
2423 | if ((s->session->tlsext_ecpointformatlist = |
2424 | OPENSSL_malloc(ecpointformatlist_length)) == NULL) { | |
2425 | *al = TLS1_AD_INTERNAL_ERROR; | |
2426 | return 0; | |
2427 | } | |
2428 | s->session->tlsext_ecpointformatlist_length = | |
2429 | ecpointformatlist_length; | |
50932c4a MC |
2430 | if (!PACKET_copy_bytes(&spkt, |
2431 | s->session->tlsext_ecpointformatlist, | |
2432 | ecpointformatlist_length)) { | |
2433 | *al = TLS1_AD_DECODE_ERROR; | |
2434 | return 0; | |
2435 | } | |
2436 | ||
0f113f3e | 2437 | } |
0f113f3e | 2438 | } |
e481f9b9 | 2439 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
2440 | |
2441 | else if (type == TLSEXT_TYPE_session_ticket) { | |
2442 | if (s->tls_session_ticket_ext_cb && | |
2443 | !s->tls_session_ticket_ext_cb(s, data, size, | |
2444 | s->tls_session_ticket_ext_cb_arg)) | |
2445 | { | |
2446 | *al = TLS1_AD_INTERNAL_ERROR; | |
2447 | return 0; | |
2448 | } | |
2449 | if (!tls_use_ticket(s) || (size > 0)) { | |
2450 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2451 | return 0; | |
2452 | } | |
2453 | s->tlsext_ticket_expected = 1; | |
2454 | } | |
0f113f3e MC |
2455 | else if (type == TLSEXT_TYPE_status_request) { |
2456 | /* | |
2457 | * MUST be empty and only sent if we've requested a status | |
2458 | * request message. | |
2459 | */ | |
2460 | if ((s->tlsext_status_type == -1) || (size > 0)) { | |
2461 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2462 | return 0; | |
2463 | } | |
2464 | /* Set flag to expect CertificateStatus message */ | |
2465 | s->tlsext_status_expected = 1; | |
2466 | } | |
ed29e82a RP |
2467 | #ifndef OPENSSL_NO_CT |
2468 | /* | |
2469 | * Only take it if we asked for it - i.e if there is no CT validation | |
2470 | * callback set, then a custom extension MAY be processing it, so we | |
2471 | * need to let control continue to flow to that. | |
2472 | */ | |
2473 | else if (type == TLSEXT_TYPE_signed_certificate_timestamp && | |
2474 | s->ct_validation_callback != NULL) { | |
2475 | /* Simply copy it off for later processing */ | |
2476 | if (s->tlsext_scts != NULL) { | |
2477 | OPENSSL_free(s->tlsext_scts); | |
2478 | s->tlsext_scts = NULL; | |
2479 | } | |
2480 | s->tlsext_scts_len = size; | |
2481 | if (size > 0) { | |
2482 | s->tlsext_scts = OPENSSL_malloc(size); | |
2483 | if (s->tlsext_scts == NULL) { | |
2484 | *al = TLS1_AD_INTERNAL_ERROR; | |
2485 | return 0; | |
2486 | } | |
2487 | memcpy(s->tlsext_scts, data, size); | |
2488 | } | |
2489 | } | |
2490 | #endif | |
e481f9b9 | 2491 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2492 | else if (type == TLSEXT_TYPE_next_proto_neg && |
2493 | s->s3->tmp.finish_md_len == 0) { | |
2494 | unsigned char *selected; | |
2495 | unsigned char selected_len; | |
0f113f3e MC |
2496 | /* We must have requested it. */ |
2497 | if (s->ctx->next_proto_select_cb == NULL) { | |
2498 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2499 | return 0; | |
2500 | } | |
2501 | /* The data must be valid */ | |
50932c4a | 2502 | if (!ssl_next_proto_validate(&spkt)) { |
0f113f3e MC |
2503 | *al = TLS1_AD_DECODE_ERROR; |
2504 | return 0; | |
2505 | } | |
2506 | if (s-> | |
2507 | ctx->next_proto_select_cb(s, &selected, &selected_len, data, | |
2508 | size, | |
2509 | s->ctx->next_proto_select_cb_arg) != | |
2510 | SSL_TLSEXT_ERR_OK) { | |
2511 | *al = TLS1_AD_INTERNAL_ERROR; | |
2512 | return 0; | |
2513 | } | |
2514 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); | |
a71edf3b | 2515 | if (s->next_proto_negotiated == NULL) { |
0f113f3e MC |
2516 | *al = TLS1_AD_INTERNAL_ERROR; |
2517 | return 0; | |
2518 | } | |
2519 | memcpy(s->next_proto_negotiated, selected, selected_len); | |
2520 | s->next_proto_negotiated_len = selected_len; | |
2521 | s->s3->next_proto_neg_seen = 1; | |
2522 | } | |
e481f9b9 | 2523 | #endif |
0f113f3e MC |
2524 | |
2525 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) { | |
2526 | unsigned len; | |
0f113f3e | 2527 | /* We must have requested it. */ |
817cd0d5 | 2528 | if (!s->s3->alpn_sent) { |
0f113f3e MC |
2529 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
2530 | return 0; | |
2531 | } | |
50e735f9 MC |
2532 | /*- |
2533 | * The extension data consists of: | |
2534 | * uint16 list_length | |
2535 | * uint8 proto_length; | |
2536 | * uint8 proto[proto_length]; | |
2537 | */ | |
50932c4a MC |
2538 | if (!PACKET_get_net_2(&spkt, &len) |
2539 | || PACKET_remaining(&spkt) != len | |
2540 | || !PACKET_get_1(&spkt, &len) | |
2541 | || PACKET_remaining(&spkt) != len) { | |
0f113f3e MC |
2542 | *al = TLS1_AD_DECODE_ERROR; |
2543 | return 0; | |
2544 | } | |
b548a1f1 | 2545 | OPENSSL_free(s->s3->alpn_selected); |
0f113f3e | 2546 | s->s3->alpn_selected = OPENSSL_malloc(len); |
a71edf3b | 2547 | if (s->s3->alpn_selected == NULL) { |
0f113f3e MC |
2548 | *al = TLS1_AD_INTERNAL_ERROR; |
2549 | return 0; | |
2550 | } | |
50932c4a MC |
2551 | if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) { |
2552 | *al = TLS1_AD_DECODE_ERROR; | |
2553 | return 0; | |
2554 | } | |
0f113f3e MC |
2555 | s->s3->alpn_selected_len = len; |
2556 | } | |
e481f9b9 | 2557 | #ifndef OPENSSL_NO_HEARTBEATS |
22e3dcb7 | 2558 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) { |
50932c4a MC |
2559 | unsigned int hbtype; |
2560 | if (!PACKET_get_1(&spkt, &hbtype)) { | |
2561 | *al = SSL_AD_DECODE_ERROR; | |
2562 | return 0; | |
2563 | } | |
2564 | switch (hbtype) { | |
0f113f3e | 2565 | case 0x01: /* Server allows us to send HB requests */ |
22e3dcb7 | 2566 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED; |
0f113f3e MC |
2567 | break; |
2568 | case 0x02: /* Server doesn't accept HB requests */ | |
22e3dcb7 RS |
2569 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED; |
2570 | s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS; | |
0f113f3e MC |
2571 | break; |
2572 | default: | |
2573 | *al = SSL_AD_ILLEGAL_PARAMETER; | |
2574 | return 0; | |
2575 | } | |
2576 | } | |
e481f9b9 MC |
2577 | #endif |
2578 | #ifndef OPENSSL_NO_SRTP | |
0f113f3e | 2579 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { |
50932c4a | 2580 | if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al)) |
0f113f3e MC |
2581 | return 0; |
2582 | } | |
e481f9b9 MC |
2583 | #endif |
2584 | #ifdef TLSEXT_TYPE_encrypt_then_mac | |
0f113f3e MC |
2585 | else if (type == TLSEXT_TYPE_encrypt_then_mac) { |
2586 | /* Ignore if inappropriate ciphersuite */ | |
2587 | if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD | |
2588 | && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4) | |
2589 | s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; | |
2590 | } | |
e481f9b9 | 2591 | #endif |
ddc06b35 | 2592 | else if (type == TLSEXT_TYPE_extended_master_secret) { |
e7f0d921 | 2593 | s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
ddc06b35 DSH |
2594 | if (!s->hit) |
2595 | s->session->flags |= SSL_SESS_FLAG_EXTMS; | |
2596 | } | |
0f113f3e MC |
2597 | /* |
2598 | * If this extension type was not otherwise handled, but matches a | |
2599 | * custom_cli_ext_record, then send it to the c callback | |
2600 | */ | |
2601 | else if (custom_ext_parse(s, 0, type, data, size, al) <= 0) | |
2602 | return 0; | |
0f113f3e MC |
2603 | } |
2604 | ||
50932c4a | 2605 | if (PACKET_remaining(pkt) != 0) { |
0f113f3e MC |
2606 | *al = SSL_AD_DECODE_ERROR; |
2607 | return 0; | |
2608 | } | |
2609 | ||
2610 | if (!s->hit && tlsext_servername == 1) { | |
2611 | if (s->tlsext_hostname) { | |
2612 | if (s->session->tlsext_hostname == NULL) { | |
7644a9ae | 2613 | s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname); |
0f113f3e MC |
2614 | if (!s->session->tlsext_hostname) { |
2615 | *al = SSL_AD_UNRECOGNIZED_NAME; | |
2616 | return 0; | |
2617 | } | |
2618 | } else { | |
2619 | *al = SSL_AD_DECODE_ERROR; | |
2620 | return 0; | |
2621 | } | |
2622 | } | |
2623 | } | |
2624 | ||
0f113f3e MC |
2625 | ri_check: |
2626 | ||
2627 | /* | |
2628 | * Determine if we need to see RI. Strictly speaking if we want to avoid | |
2629 | * an attack we should *always* see RI even on initial server hello | |
2630 | * because the client doesn't see any renegotiation during an attack. | |
2631 | * However this would mean we could not connect to any server which | |
2632 | * doesn't support RI so for the immediate future tolerate RI absence on | |
2633 | * initial connect only. | |
2634 | */ | |
2635 | if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) | |
2636 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
2637 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2638 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, | |
2639 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
2640 | return 0; | |
2641 | } | |
2642 | ||
e7f0d921 DSH |
2643 | if (s->hit) { |
2644 | /* | |
2645 | * Check extended master secret extension is consistent with | |
2646 | * original session. | |
2647 | */ | |
2648 | if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) != | |
2649 | !(s->session->flags & SSL_SESS_FLAG_EXTMS)) { | |
2650 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2651 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS); | |
2652 | return 0; | |
2653 | } | |
2654 | } | |
2655 | ||
0f113f3e MC |
2656 | return 1; |
2657 | } | |
b2172f4f | 2658 | |
36ca4ba6 | 2659 | int ssl_prepare_clienthello_tlsext(SSL *s) |
0f113f3e | 2660 | { |
817cd0d5 | 2661 | s->s3->alpn_sent = 0; |
0f113f3e MC |
2662 | return 1; |
2663 | } | |
36ca4ba6 BM |
2664 | |
2665 | int ssl_prepare_serverhello_tlsext(SSL *s) | |
0f113f3e MC |
2666 | { |
2667 | return 1; | |
2668 | } | |
36ca4ba6 | 2669 | |
2daceb03 | 2670 | static int ssl_check_clienthello_tlsext_early(SSL *s) |
0f113f3e MC |
2671 | { |
2672 | int ret = SSL_TLSEXT_ERR_NOACK; | |
2673 | int al = SSL_AD_UNRECOGNIZED_NAME; | |
2674 | ||
e481f9b9 | 2675 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2676 | /* |
2677 | * The handling of the ECPointFormats extension is done elsewhere, namely | |
2678 | * in ssl3_choose_cipher in s3_lib.c. | |
2679 | */ | |
2680 | /* | |
2681 | * The handling of the EllipticCurves extension is done elsewhere, namely | |
2682 | * in ssl3_choose_cipher in s3_lib.c. | |
2683 | */ | |
e481f9b9 | 2684 | #endif |
0f113f3e MC |
2685 | |
2686 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | |
2687 | ret = | |
2688 | s->ctx->tlsext_servername_callback(s, &al, | |
2689 | s->ctx->tlsext_servername_arg); | |
2690 | else if (s->initial_ctx != NULL | |
2691 | && s->initial_ctx->tlsext_servername_callback != 0) | |
2692 | ret = | |
2693 | s->initial_ctx->tlsext_servername_callback(s, &al, | |
2694 | s-> | |
2695 | initial_ctx->tlsext_servername_arg); | |
2696 | ||
0f113f3e MC |
2697 | switch (ret) { |
2698 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2699 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2700 | return -1; | |
2701 | ||
2702 | case SSL_TLSEXT_ERR_ALERT_WARNING: | |
2703 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | |
2704 | return 1; | |
2705 | ||
2706 | case SSL_TLSEXT_ERR_NOACK: | |
2707 | s->servername_done = 0; | |
2708 | default: | |
2709 | return 1; | |
2710 | } | |
2711 | } | |
d376e57d | 2712 | /* Initialise digests to default values */ |
a0f63828 | 2713 | void ssl_set_default_md(SSL *s) |
d376e57d DSH |
2714 | { |
2715 | const EVP_MD **pmd = s->s3->tmp.md; | |
2716 | #ifndef OPENSSL_NO_DSA | |
152fbc28 | 2717 | pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
d376e57d DSH |
2718 | #endif |
2719 | #ifndef OPENSSL_NO_RSA | |
d18d31a1 | 2720 | if (SSL_USE_SIGALGS(s)) |
152fbc28 | 2721 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
d18d31a1 | 2722 | else |
152fbc28 | 2723 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX); |
d18d31a1 | 2724 | pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN]; |
d376e57d DSH |
2725 | #endif |
2726 | #ifndef OPENSSL_NO_EC | |
152fbc28 | 2727 | pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX); |
d376e57d | 2728 | #endif |
e44380a9 | 2729 | #ifndef OPENSSL_NO_GOST |
152fbc28 DSH |
2730 | pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX); |
2731 | pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX); | |
2732 | pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX); | |
e44380a9 | 2733 | #endif |
d376e57d | 2734 | } |
f1fd4544 | 2735 | |
e469af8d | 2736 | int tls1_set_server_sigalgs(SSL *s) |
0f113f3e MC |
2737 | { |
2738 | int al; | |
2739 | size_t i; | |
e771eea6 | 2740 | /* Clear any shared sigtnature algorithms */ |
b548a1f1 RS |
2741 | OPENSSL_free(s->cert->shared_sigalgs); |
2742 | s->cert->shared_sigalgs = NULL; | |
2743 | s->cert->shared_sigalgslen = 0; | |
0f113f3e MC |
2744 | /* Clear certificate digests and validity flags */ |
2745 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
d376e57d | 2746 | s->s3->tmp.md[i] = NULL; |
6383d316 | 2747 | s->s3->tmp.valid_flags[i] = 0; |
0f113f3e MC |
2748 | } |
2749 | ||
2750 | /* If sigalgs received process it. */ | |
76106e60 | 2751 | if (s->s3->tmp.peer_sigalgs) { |
0f113f3e MC |
2752 | if (!tls1_process_sigalgs(s)) { |
2753 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); | |
2754 | al = SSL_AD_INTERNAL_ERROR; | |
2755 | goto err; | |
2756 | } | |
2757 | /* Fatal error is no shared signature algorithms */ | |
2758 | if (!s->cert->shared_sigalgs) { | |
2759 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, | |
2760 | SSL_R_NO_SHARED_SIGATURE_ALGORITHMS); | |
2761 | al = SSL_AD_ILLEGAL_PARAMETER; | |
2762 | goto err; | |
2763 | } | |
d376e57d DSH |
2764 | } else { |
2765 | ssl_set_default_md(s); | |
2766 | } | |
0f113f3e MC |
2767 | return 1; |
2768 | err: | |
2769 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2770 | return 0; | |
2771 | } | |
e469af8d | 2772 | |
2daceb03 | 2773 | int ssl_check_clienthello_tlsext_late(SSL *s) |
0f113f3e MC |
2774 | { |
2775 | int ret = SSL_TLSEXT_ERR_OK; | |
4c9b0a03 | 2776 | int al = SSL_AD_INTERNAL_ERROR; |
0f113f3e MC |
2777 | |
2778 | /* | |
2779 | * If status request then ask callback what to do. Note: this must be | |
2780 | * called after servername callbacks in case the certificate has changed, | |
2781 | * and must be called after the cipher has been chosen because this may | |
2782 | * influence which certificate is sent | |
2783 | */ | |
2784 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { | |
2785 | int r; | |
2786 | CERT_PKEY *certpkey; | |
2787 | certpkey = ssl_get_server_send_pkey(s); | |
2788 | /* If no certificate can't return certificate status */ | |
2789 | if (certpkey == NULL) { | |
2790 | s->tlsext_status_expected = 0; | |
2791 | return 1; | |
2792 | } | |
2793 | /* | |
2794 | * Set current certificate to one we will use so SSL_get_certificate | |
2795 | * et al can pick it up. | |
2796 | */ | |
2797 | s->cert->key = certpkey; | |
2798 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | |
2799 | switch (r) { | |
2800 | /* We don't want to send a status request response */ | |
2801 | case SSL_TLSEXT_ERR_NOACK: | |
2802 | s->tlsext_status_expected = 0; | |
2803 | break; | |
2804 | /* status request response should be sent */ | |
2805 | case SSL_TLSEXT_ERR_OK: | |
2806 | if (s->tlsext_ocsp_resp) | |
2807 | s->tlsext_status_expected = 1; | |
2808 | else | |
2809 | s->tlsext_status_expected = 0; | |
2810 | break; | |
2811 | /* something bad happened */ | |
2812 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2813 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | |
2814 | al = SSL_AD_INTERNAL_ERROR; | |
2815 | goto err; | |
2816 | } | |
2817 | } else | |
2818 | s->tlsext_status_expected = 0; | |
2daceb03 | 2819 | |
817cd0d5 TS |
2820 | if (!tls1_alpn_handle_client_hello_late(s, &ret, &al)) { |
2821 | goto err; | |
2822 | } | |
2823 | ||
2daceb03 | 2824 | err: |
0f113f3e MC |
2825 | switch (ret) { |
2826 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2827 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2828 | return -1; | |
2829 | ||
2830 | case SSL_TLSEXT_ERR_ALERT_WARNING: | |
2831 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | |
2832 | return 1; | |
2833 | ||
2834 | default: | |
2835 | return 1; | |
2836 | } | |
2837 | } | |
2daceb03 | 2838 | |
36ca4ba6 | 2839 | int ssl_check_serverhello_tlsext(SSL *s) |
0f113f3e MC |
2840 | { |
2841 | int ret = SSL_TLSEXT_ERR_NOACK; | |
2842 | int al = SSL_AD_UNRECOGNIZED_NAME; | |
2843 | ||
e481f9b9 | 2844 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2845 | /* |
2846 | * If we are client and using an elliptic curve cryptography cipher | |
2847 | * suite, then if server returns an EC point formats lists extension it | |
2848 | * must contain uncompressed. | |
2849 | */ | |
2850 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
2851 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
2852 | if ((s->tlsext_ecpointformatlist != NULL) | |
2853 | && (s->tlsext_ecpointformatlist_length > 0) | |
2854 | && (s->session->tlsext_ecpointformatlist != NULL) | |
2855 | && (s->session->tlsext_ecpointformatlist_length > 0) | |
ce0c1f2b | 2856 | && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { |
0f113f3e MC |
2857 | /* we are using an ECC cipher */ |
2858 | size_t i; | |
2859 | unsigned char *list; | |
2860 | int found_uncompressed = 0; | |
2861 | list = s->session->tlsext_ecpointformatlist; | |
2862 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { | |
2863 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { | |
2864 | found_uncompressed = 1; | |
2865 | break; | |
2866 | } | |
2867 | } | |
2868 | if (!found_uncompressed) { | |
2869 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT, | |
2870 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); | |
2871 | return -1; | |
2872 | } | |
2873 | } | |
2874 | ret = SSL_TLSEXT_ERR_OK; | |
e481f9b9 | 2875 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
2876 | |
2877 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | |
2878 | ret = | |
2879 | s->ctx->tlsext_servername_callback(s, &al, | |
2880 | s->ctx->tlsext_servername_arg); | |
2881 | else if (s->initial_ctx != NULL | |
2882 | && s->initial_ctx->tlsext_servername_callback != 0) | |
2883 | ret = | |
2884 | s->initial_ctx->tlsext_servername_callback(s, &al, | |
2885 | s-> | |
2886 | initial_ctx->tlsext_servername_arg); | |
2887 | ||
b1931d43 MC |
2888 | /* |
2889 | * Ensure we get sensible values passed to tlsext_status_cb in the event | |
2890 | * that we don't receive a status message | |
2891 | */ | |
bb1aaab4 MC |
2892 | OPENSSL_free(s->tlsext_ocsp_resp); |
2893 | s->tlsext_ocsp_resp = NULL; | |
2894 | s->tlsext_ocsp_resplen = -1; | |
0f113f3e MC |
2895 | |
2896 | switch (ret) { | |
2897 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2898 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2899 | return -1; | |
2900 | ||
2901 | case SSL_TLSEXT_ERR_ALERT_WARNING: | |
2902 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | |
2903 | return 1; | |
2904 | ||
2905 | case SSL_TLSEXT_ERR_NOACK: | |
2906 | s->servername_done = 0; | |
2907 | default: | |
2908 | return 1; | |
2909 | } | |
2910 | } | |
761772d7 | 2911 | |
50932c4a | 2912 | int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt) |
0f113f3e MC |
2913 | { |
2914 | int al = -1; | |
2915 | if (s->version < SSL3_VERSION) | |
2916 | return 1; | |
50932c4a | 2917 | if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) { |
0f113f3e MC |
2918 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
2919 | return 0; | |
2920 | } | |
2921 | ||
2922 | if (ssl_check_serverhello_tlsext(s) <= 0) { | |
2923 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT); | |
2924 | return 0; | |
2925 | } | |
2926 | return 1; | |
09e4e4b9 DSH |
2927 | } |
2928 | ||
1d97c843 TH |
2929 | /*- |
2930 | * Since the server cache lookup is done early on in the processing of the | |
e7f0d921 DSH |
2931 | * ClientHello and other operations depend on the result some extensions |
2932 | * need to be handled at the same time. | |
2933 | * | |
2934 | * Two extensions are currently handled, session ticket and extended master | |
2935 | * secret. | |
c519e89f | 2936 | * |
b3e2272c EK |
2937 | * session_id: ClientHello session ID. |
2938 | * ext: ClientHello extensions (including length prefix) | |
c519e89f BM |
2939 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
2940 | * point to the resulting session. | |
2941 | * | |
2942 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | |
2943 | * ciphersuite, in which case we have no use for session tickets and one will | |
2944 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. | |
2945 | * | |
2946 | * Returns: | |
2947 | * -1: fatal error, either from parsing or decrypting the ticket. | |
2948 | * 0: no ticket was found (or was ignored, based on settings). | |
2949 | * 1: a zero length extension was found, indicating that the client supports | |
2950 | * session tickets but doesn't currently have one to offer. | |
2951 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | |
2952 | * couldn't be decrypted because of a non-fatal error. | |
2953 | * 3: a ticket was successfully decrypted and *ret was set. | |
2954 | * | |
2955 | * Side effects: | |
2956 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue | |
2957 | * a new session ticket to the client because the client indicated support | |
2958 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
2959 | * a session ticket or we couldn't use the one it gave us, or if | |
2960 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. | |
2961 | * Otherwise, s->tlsext_ticket_expected is set to 0. | |
e7f0d921 DSH |
2962 | * |
2963 | * For extended master secret flag is set if the extension is present. | |
2964 | * | |
6434abbf | 2965 | */ |
e7f0d921 DSH |
2966 | int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext, |
2967 | const PACKET *session_id, | |
2968 | SSL_SESSION **ret) | |
0f113f3e | 2969 | { |
9ceb2426 | 2970 | unsigned int i; |
b3e2272c | 2971 | PACKET local_ext = *ext; |
9ceb2426 | 2972 | int retv = -1; |
0f113f3e | 2973 | |
e7f0d921 DSH |
2974 | int have_ticket = 0; |
2975 | int use_ticket = tls_use_ticket(s); | |
2976 | ||
0f113f3e MC |
2977 | *ret = NULL; |
2978 | s->tlsext_ticket_expected = 0; | |
e7f0d921 | 2979 | s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; |
0f113f3e MC |
2980 | |
2981 | /* | |
2982 | * If tickets disabled behave as if no ticket present to permit stateful | |
2983 | * resumption. | |
2984 | */ | |
9ceb2426 | 2985 | if ((s->version <= SSL3_VERSION)) |
0f113f3e | 2986 | return 0; |
9ceb2426 | 2987 | |
b3e2272c | 2988 | if (!PACKET_get_net_2(&local_ext, &i)) { |
9ceb2426 MC |
2989 | retv = 0; |
2990 | goto end; | |
2991 | } | |
b3e2272c | 2992 | while (PACKET_remaining(&local_ext) >= 4) { |
9ceb2426 MC |
2993 | unsigned int type, size; |
2994 | ||
b3e2272c EK |
2995 | if (!PACKET_get_net_2(&local_ext, &type) |
2996 | || !PACKET_get_net_2(&local_ext, &size)) { | |
9ceb2426 MC |
2997 | /* Shouldn't ever happen */ |
2998 | retv = -1; | |
2999 | goto end; | |
3000 | } | |
b3e2272c | 3001 | if (PACKET_remaining(&local_ext) < size) { |
9ceb2426 MC |
3002 | retv = 0; |
3003 | goto end; | |
3004 | } | |
e7f0d921 | 3005 | if (type == TLSEXT_TYPE_session_ticket && use_ticket) { |
0f113f3e | 3006 | int r; |
b6981744 | 3007 | const unsigned char *etick; |
9ceb2426 | 3008 | |
e7f0d921 DSH |
3009 | /* Duplicate extension */ |
3010 | if (have_ticket != 0) { | |
3011 | retv = -1; | |
3012 | goto end; | |
3013 | } | |
3014 | have_ticket = 1; | |
3015 | ||
0f113f3e MC |
3016 | if (size == 0) { |
3017 | /* | |
3018 | * The client will accept a ticket but doesn't currently have | |
3019 | * one. | |
3020 | */ | |
3021 | s->tlsext_ticket_expected = 1; | |
9ceb2426 | 3022 | retv = 1; |
e7f0d921 | 3023 | continue; |
0f113f3e MC |
3024 | } |
3025 | if (s->tls_session_secret_cb) { | |
3026 | /* | |
3027 | * Indicate that the ticket couldn't be decrypted rather than | |
3028 | * generating the session from ticket now, trigger | |
3029 | * abbreviated handshake based on external mechanism to | |
3030 | * calculate the master secret later. | |
3031 | */ | |
9ceb2426 | 3032 | retv = 2; |
e7f0d921 | 3033 | continue; |
9ceb2426 | 3034 | } |
b3e2272c | 3035 | if (!PACKET_get_bytes(&local_ext, &etick, size)) { |
9ceb2426 MC |
3036 | /* Shouldn't ever happen */ |
3037 | retv = -1; | |
3038 | goto end; | |
0f113f3e | 3039 | } |
b3e2272c EK |
3040 | r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id), |
3041 | PACKET_remaining(session_id), ret); | |
0f113f3e MC |
3042 | switch (r) { |
3043 | case 2: /* ticket couldn't be decrypted */ | |
3044 | s->tlsext_ticket_expected = 1; | |
9ceb2426 MC |
3045 | retv = 2; |
3046 | break; | |
0f113f3e | 3047 | case 3: /* ticket was decrypted */ |
9ceb2426 MC |
3048 | retv = r; |
3049 | break; | |
0f113f3e MC |
3050 | case 4: /* ticket decrypted but need to renew */ |
3051 | s->tlsext_ticket_expected = 1; | |
9ceb2426 MC |
3052 | retv = 3; |
3053 | break; | |
0f113f3e | 3054 | default: /* fatal error */ |
9ceb2426 MC |
3055 | retv = -1; |
3056 | break; | |
0f113f3e | 3057 | } |
e7f0d921 | 3058 | continue; |
c83eda8c | 3059 | } else { |
e7f0d921 DSH |
3060 | if (type == TLSEXT_TYPE_extended_master_secret) |
3061 | s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; | |
b3e2272c | 3062 | if (!PACKET_forward(&local_ext, size)) { |
c83eda8c MC |
3063 | retv = -1; |
3064 | goto end; | |
3065 | } | |
0f113f3e | 3066 | } |
0f113f3e | 3067 | } |
e7f0d921 DSH |
3068 | if (have_ticket == 0) |
3069 | retv = 0; | |
9ceb2426 | 3070 | end: |
9ceb2426 | 3071 | return retv; |
0f113f3e | 3072 | } |
6434abbf | 3073 | |
1d97c843 TH |
3074 | /*- |
3075 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f BM |
3076 | * |
3077 | * etick: points to the body of the session ticket extension. | |
e771eea6 | 3078 | * eticklen: the length of the session tickets extenion. |
c519e89f BM |
3079 | * sess_id: points at the session ID. |
3080 | * sesslen: the length of the session ID. | |
3081 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
3082 | * point to the resulting session. | |
3083 | * | |
3084 | * Returns: | |
bf7c6817 | 3085 | * -2: fatal error, malloc failure. |
c519e89f BM |
3086 | * -1: fatal error, either from parsing or decrypting the ticket. |
3087 | * 2: the ticket couldn't be decrypted. | |
3088 | * 3: a ticket was successfully decrypted and *psess was set. | |
3089 | * 4: same as 3, but the ticket needs to be renewed. | |
3090 | */ | |
0f113f3e MC |
3091 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
3092 | int eticklen, const unsigned char *sess_id, | |
3093 | int sesslen, SSL_SESSION **psess) | |
3094 | { | |
3095 | SSL_SESSION *sess; | |
3096 | unsigned char *sdec; | |
3097 | const unsigned char *p; | |
35b1a433 | 3098 | int slen, mlen, renew_ticket = 0, ret = -1; |
0f113f3e | 3099 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
bf7c6817 | 3100 | HMAC_CTX *hctx = NULL; |
846ec07d | 3101 | EVP_CIPHER_CTX *ctx; |
0f113f3e MC |
3102 | SSL_CTX *tctx = s->initial_ctx; |
3103 | /* Need at least keyname + iv + some encrypted data */ | |
3104 | if (eticklen < 48) | |
3105 | return 2; | |
3106 | /* Initialize session ticket encryption and HMAC contexts */ | |
bf7c6817 RL |
3107 | hctx = HMAC_CTX_new(); |
3108 | if (hctx == NULL) | |
3109 | return -2; | |
846ec07d | 3110 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 MC |
3111 | if (ctx == NULL) { |
3112 | ret = -2; | |
3113 | goto err; | |
3114 | } | |
0f113f3e MC |
3115 | if (tctx->tlsext_ticket_key_cb) { |
3116 | unsigned char *nctick = (unsigned char *)etick; | |
3117 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, | |
846ec07d | 3118 | ctx, hctx, 0); |
0f113f3e | 3119 | if (rv < 0) |
35b1a433 MC |
3120 | goto err; |
3121 | if (rv == 0) { | |
3122 | ret = 2; | |
3123 | goto err; | |
3124 | } | |
0f113f3e MC |
3125 | if (rv == 2) |
3126 | renew_ticket = 1; | |
3127 | } else { | |
3128 | /* Check key name matches */ | |
35b1a433 MC |
3129 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) { |
3130 | ret = 2; | |
3131 | goto err; | |
3132 | } | |
bf7c6817 | 3133 | if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16, |
5f3d93e4 | 3134 | EVP_sha256(), NULL) <= 0 |
846ec07d | 3135 | || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, |
5f3d93e4 MC |
3136 | tctx->tlsext_tick_aes_key, |
3137 | etick + 16) <= 0) { | |
3138 | goto err; | |
3139 | } | |
0f113f3e MC |
3140 | } |
3141 | /* | |
3142 | * Attempt to process session ticket, first conduct sanity and integrity | |
3143 | * checks on ticket. | |
3144 | */ | |
bf7c6817 | 3145 | mlen = HMAC_size(hctx); |
0f113f3e | 3146 | if (mlen < 0) { |
5f3d93e4 | 3147 | goto err; |
0f113f3e MC |
3148 | } |
3149 | eticklen -= mlen; | |
3150 | /* Check HMAC of encrypted ticket */ | |
bf7c6817 RL |
3151 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
3152 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { | |
5f3d93e4 MC |
3153 | goto err; |
3154 | } | |
bf7c6817 | 3155 | HMAC_CTX_free(hctx); |
0f113f3e | 3156 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
846ec07d | 3157 | EVP_CIPHER_CTX_free(ctx); |
0f113f3e MC |
3158 | return 2; |
3159 | } | |
3160 | /* Attempt to decrypt session data */ | |
3161 | /* Move p after IV to start of encrypted ticket, update length */ | |
846ec07d RL |
3162 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx); |
3163 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx); | |
0f113f3e | 3164 | sdec = OPENSSL_malloc(eticklen); |
5f3d93e4 | 3165 | if (sdec == NULL |
846ec07d RL |
3166 | || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) { |
3167 | EVP_CIPHER_CTX_free(ctx); | |
d1247df2 | 3168 | OPENSSL_free(sdec); |
0f113f3e MC |
3169 | return -1; |
3170 | } | |
846ec07d RL |
3171 | if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) { |
3172 | EVP_CIPHER_CTX_free(ctx); | |
0f113f3e MC |
3173 | OPENSSL_free(sdec); |
3174 | return 2; | |
3175 | } | |
3176 | slen += mlen; | |
846ec07d RL |
3177 | EVP_CIPHER_CTX_free(ctx); |
3178 | ctx = NULL; | |
0f113f3e MC |
3179 | p = sdec; |
3180 | ||
3181 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
3182 | OPENSSL_free(sdec); | |
3183 | if (sess) { | |
3184 | /* | |
3185 | * The session ID, if non-empty, is used by some clients to detect | |
3186 | * that the ticket has been accepted. So we copy it to the session | |
3187 | * structure. If it is empty set length to zero as required by | |
3188 | * standard. | |
3189 | */ | |
3190 | if (sesslen) | |
3191 | memcpy(sess->session_id, sess_id, sesslen); | |
3192 | sess->session_id_length = sesslen; | |
3193 | *psess = sess; | |
3194 | if (renew_ticket) | |
3195 | return 4; | |
3196 | else | |
3197 | return 3; | |
3198 | } | |
3199 | ERR_clear_error(); | |
3200 | /* | |
3201 | * For session parse failure, indicate that we need to send a new ticket. | |
3202 | */ | |
3203 | return 2; | |
5f3d93e4 | 3204 | err: |
846ec07d | 3205 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 3206 | HMAC_CTX_free(hctx); |
35b1a433 | 3207 | return ret; |
0f113f3e | 3208 | } |
6434abbf | 3209 | |
6b7be581 DSH |
3210 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
3211 | ||
0f113f3e MC |
3212 | typedef struct { |
3213 | int nid; | |
3214 | int id; | |
3215 | } tls12_lookup; | |
6b7be581 | 3216 | |
d97ed219 | 3217 | static const tls12_lookup tls12_md[] = { |
0f113f3e MC |
3218 | {NID_md5, TLSEXT_hash_md5}, |
3219 | {NID_sha1, TLSEXT_hash_sha1}, | |
3220 | {NID_sha224, TLSEXT_hash_sha224}, | |
3221 | {NID_sha256, TLSEXT_hash_sha256}, | |
3222 | {NID_sha384, TLSEXT_hash_sha384}, | |
e44380a9 DB |
3223 | {NID_sha512, TLSEXT_hash_sha512}, |
3224 | {NID_id_GostR3411_94, TLSEXT_hash_gostr3411}, | |
3225 | {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256}, | |
3226 | {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512}, | |
6b7be581 DSH |
3227 | }; |
3228 | ||
d97ed219 | 3229 | static const tls12_lookup tls12_sig[] = { |
0f113f3e MC |
3230 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
3231 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, | |
e44380a9 DB |
3232 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}, |
3233 | {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001}, | |
3234 | {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256}, | |
3235 | {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512} | |
6b7be581 DSH |
3236 | }; |
3237 | ||
d97ed219 | 3238 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) |
0f113f3e MC |
3239 | { |
3240 | size_t i; | |
3241 | for (i = 0; i < tlen; i++) { | |
3242 | if (table[i].nid == nid) | |
3243 | return table[i].id; | |
3244 | } | |
3245 | return -1; | |
3246 | } | |
e7f8ff43 | 3247 | |
d97ed219 | 3248 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) |
0f113f3e MC |
3249 | { |
3250 | size_t i; | |
3251 | for (i = 0; i < tlen; i++) { | |
3252 | if ((table[i].id) == id) | |
3253 | return table[i].nid; | |
3254 | } | |
3255 | return NID_undef; | |
3256 | } | |
3257 | ||
3258 | int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, | |
3259 | const EVP_MD *md) | |
3260 | { | |
3261 | int sig_id, md_id; | |
3262 | if (!md) | |
3263 | return 0; | |
b6eb9827 | 3264 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md)); |
0f113f3e MC |
3265 | if (md_id == -1) |
3266 | return 0; | |
3267 | sig_id = tls12_get_sigid(pk); | |
3268 | if (sig_id == -1) | |
3269 | return 0; | |
3270 | p[0] = (unsigned char)md_id; | |
3271 | p[1] = (unsigned char)sig_id; | |
3272 | return 1; | |
3273 | } | |
6b7be581 | 3274 | |
a2f9200f | 3275 | int tls12_get_sigid(const EVP_PKEY *pk) |
0f113f3e | 3276 | { |
3aeb9348 | 3277 | return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig)); |
0f113f3e MC |
3278 | } |
3279 | ||
3280 | typedef struct { | |
3281 | int nid; | |
3282 | int secbits; | |
7afd2312 | 3283 | int md_idx; |
e44380a9 | 3284 | unsigned char tlsext_hash; |
0f113f3e | 3285 | } tls12_hash_info; |
b362ccab DSH |
3286 | |
3287 | static const tls12_hash_info tls12_md_info[] = { | |
7afd2312 DSH |
3288 | {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5}, |
3289 | {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1}, | |
3290 | {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224}, | |
3291 | {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256}, | |
3292 | {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384}, | |
3293 | {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512}, | |
3294 | {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411}, | |
3295 | {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256}, | |
3296 | {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512}, | |
b362ccab | 3297 | }; |
a2f9200f | 3298 | |
b362ccab | 3299 | static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg) |
0f113f3e | 3300 | { |
e44380a9 | 3301 | unsigned int i; |
0f113f3e MC |
3302 | if (hash_alg == 0) |
3303 | return NULL; | |
e44380a9 DB |
3304 | |
3305 | for (i=0; i < OSSL_NELEM(tls12_md_info); i++) | |
3306 | { | |
3307 | if (tls12_md_info[i].tlsext_hash == hash_alg) | |
3308 | return tls12_md_info + i; | |
3309 | } | |
3310 | ||
3311 | return NULL; | |
0f113f3e | 3312 | } |
a2f9200f | 3313 | |
b362ccab | 3314 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
0f113f3e MC |
3315 | { |
3316 | const tls12_hash_info *inf; | |
3317 | if (hash_alg == TLSEXT_hash_md5 && FIPS_mode()) | |
3318 | return NULL; | |
3319 | inf = tls12_get_hash_info(hash_alg); | |
7afd2312 | 3320 | if (!inf) |
0f113f3e | 3321 | return NULL; |
7afd2312 | 3322 | return ssl_md(inf->md_idx); |
0f113f3e | 3323 | } |
a2f9200f | 3324 | |
4453cd8c | 3325 | static int tls12_get_pkey_idx(unsigned char sig_alg) |
0f113f3e MC |
3326 | { |
3327 | switch (sig_alg) { | |
e481f9b9 | 3328 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3329 | case TLSEXT_signature_rsa: |
3330 | return SSL_PKEY_RSA_SIGN; | |
e481f9b9 MC |
3331 | #endif |
3332 | #ifndef OPENSSL_NO_DSA | |
0f113f3e MC |
3333 | case TLSEXT_signature_dsa: |
3334 | return SSL_PKEY_DSA_SIGN; | |
e481f9b9 MC |
3335 | #endif |
3336 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
3337 | case TLSEXT_signature_ecdsa: |
3338 | return SSL_PKEY_ECC; | |
e481f9b9 | 3339 | #endif |
e44380a9 DB |
3340 | # ifndef OPENSSL_NO_GOST |
3341 | case TLSEXT_signature_gostr34102001: | |
3342 | return SSL_PKEY_GOST01; | |
3343 | ||
3344 | case TLSEXT_signature_gostr34102012_256: | |
3345 | return SSL_PKEY_GOST12_256; | |
3346 | ||
3347 | case TLSEXT_signature_gostr34102012_512: | |
3348 | return SSL_PKEY_GOST12_512; | |
3349 | # endif | |
0f113f3e MC |
3350 | } |
3351 | return -1; | |
3352 | } | |
4453cd8c DSH |
3353 | |
3354 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ | |
3355 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, | |
0f113f3e MC |
3356 | int *psignhash_nid, const unsigned char *data) |
3357 | { | |
330dcb09 | 3358 | int sign_nid = NID_undef, hash_nid = NID_undef; |
0f113f3e MC |
3359 | if (!phash_nid && !psign_nid && !psignhash_nid) |
3360 | return; | |
3361 | if (phash_nid || psignhash_nid) { | |
b6eb9827 | 3362 | hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md)); |
0f113f3e MC |
3363 | if (phash_nid) |
3364 | *phash_nid = hash_nid; | |
3365 | } | |
3366 | if (psign_nid || psignhash_nid) { | |
b6eb9827 | 3367 | sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig)); |
0f113f3e MC |
3368 | if (psign_nid) |
3369 | *psign_nid = sign_nid; | |
3370 | } | |
3371 | if (psignhash_nid) { | |
330dcb09 MC |
3372 | if (sign_nid == NID_undef || hash_nid == NID_undef |
3373 | || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, | |
3374 | sign_nid) <= 0) | |
0f113f3e MC |
3375 | *psignhash_nid = NID_undef; |
3376 | } | |
3377 | } | |
3378 | ||
b362ccab DSH |
3379 | /* Check to see if a signature algorithm is allowed */ |
3380 | static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp) | |
0f113f3e MC |
3381 | { |
3382 | /* See if we have an entry in the hash table and it is enabled */ | |
3383 | const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]); | |
7afd2312 | 3384 | if (hinf == NULL || ssl_md(hinf->md_idx) == NULL) |
0f113f3e MC |
3385 | return 0; |
3386 | /* See if public key algorithm allowed */ | |
3387 | if (tls12_get_pkey_idx(ptmp[1]) == -1) | |
3388 | return 0; | |
3389 | /* Finally see if security callback allows it */ | |
3390 | return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp); | |
3391 | } | |
3392 | ||
3393 | /* | |
3394 | * Get a mask of disabled public key algorithms based on supported signature | |
3395 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
3396 | * disabled. | |
b362ccab DSH |
3397 | */ |
3398 | ||
90d9e49a | 3399 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
0f113f3e MC |
3400 | { |
3401 | const unsigned char *sigalgs; | |
3402 | size_t i, sigalgslen; | |
3403 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; | |
3404 | /* | |
3405 | * Now go through all signature algorithms seeing if we support any for | |
3406 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep | |
3407 | * down calls to security callback only check if we have to. | |
3408 | */ | |
3409 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); | |
3410 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { | |
3411 | switch (sigalgs[1]) { | |
e481f9b9 | 3412 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3413 | case TLSEXT_signature_rsa: |
3414 | if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3415 | have_rsa = 1; | |
3416 | break; | |
e481f9b9 MC |
3417 | #endif |
3418 | #ifndef OPENSSL_NO_DSA | |
0f113f3e MC |
3419 | case TLSEXT_signature_dsa: |
3420 | if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3421 | have_dsa = 1; | |
3422 | break; | |
e481f9b9 MC |
3423 | #endif |
3424 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
3425 | case TLSEXT_signature_ecdsa: |
3426 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3427 | have_ecdsa = 1; | |
3428 | break; | |
e481f9b9 | 3429 | #endif |
0f113f3e MC |
3430 | } |
3431 | } | |
3432 | if (!have_rsa) | |
3433 | *pmask_a |= SSL_aRSA; | |
3434 | if (!have_dsa) | |
3435 | *pmask_a |= SSL_aDSS; | |
3436 | if (!have_ecdsa) | |
3437 | *pmask_a |= SSL_aECDSA; | |
3438 | } | |
b362ccab DSH |
3439 | |
3440 | size_t tls12_copy_sigalgs(SSL *s, unsigned char *out, | |
0f113f3e MC |
3441 | const unsigned char *psig, size_t psiglen) |
3442 | { | |
3443 | unsigned char *tmpout = out; | |
3444 | size_t i; | |
3445 | for (i = 0; i < psiglen; i += 2, psig += 2) { | |
3446 | if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) { | |
3447 | *tmpout++ = psig[0]; | |
3448 | *tmpout++ = psig[1]; | |
3449 | } | |
3450 | } | |
3451 | return tmpout - out; | |
3452 | } | |
b362ccab | 3453 | |
4453cd8c | 3454 | /* Given preference and allowed sigalgs set shared sigalgs */ |
b362ccab | 3455 | static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig, |
0f113f3e MC |
3456 | const unsigned char *pref, size_t preflen, |
3457 | const unsigned char *allow, size_t allowlen) | |
3458 | { | |
3459 | const unsigned char *ptmp, *atmp; | |
3460 | size_t i, j, nmatch = 0; | |
3461 | for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) { | |
3462 | /* Skip disabled hashes or signature algorithms */ | |
3463 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp)) | |
3464 | continue; | |
3465 | for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) { | |
3466 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) { | |
3467 | nmatch++; | |
3468 | if (shsig) { | |
3469 | shsig->rhash = ptmp[0]; | |
3470 | shsig->rsign = ptmp[1]; | |
3471 | tls1_lookup_sigalg(&shsig->hash_nid, | |
3472 | &shsig->sign_nid, | |
3473 | &shsig->signandhash_nid, ptmp); | |
3474 | shsig++; | |
3475 | } | |
3476 | break; | |
3477 | } | |
3478 | } | |
3479 | } | |
3480 | return nmatch; | |
3481 | } | |
4453cd8c DSH |
3482 | |
3483 | /* Set shared signature algorithms for SSL structures */ | |
3484 | static int tls1_set_shared_sigalgs(SSL *s) | |
0f113f3e MC |
3485 | { |
3486 | const unsigned char *pref, *allow, *conf; | |
3487 | size_t preflen, allowlen, conflen; | |
3488 | size_t nmatch; | |
3489 | TLS_SIGALGS *salgs = NULL; | |
3490 | CERT *c = s->cert; | |
3491 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 RS |
3492 | |
3493 | OPENSSL_free(c->shared_sigalgs); | |
3494 | c->shared_sigalgs = NULL; | |
3495 | c->shared_sigalgslen = 0; | |
0f113f3e MC |
3496 | /* If client use client signature algorithms if not NULL */ |
3497 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
3498 | conf = c->client_sigalgs; | |
3499 | conflen = c->client_sigalgslen; | |
3500 | } else if (c->conf_sigalgs && !is_suiteb) { | |
3501 | conf = c->conf_sigalgs; | |
3502 | conflen = c->conf_sigalgslen; | |
3503 | } else | |
3504 | conflen = tls12_get_psigalgs(s, &conf); | |
3505 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { | |
3506 | pref = conf; | |
3507 | preflen = conflen; | |
76106e60 DSH |
3508 | allow = s->s3->tmp.peer_sigalgs; |
3509 | allowlen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
3510 | } else { |
3511 | allow = conf; | |
3512 | allowlen = conflen; | |
76106e60 DSH |
3513 | pref = s->s3->tmp.peer_sigalgs; |
3514 | preflen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
3515 | } |
3516 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf DSH |
3517 | if (nmatch) { |
3518 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); | |
a71edf3b | 3519 | if (salgs == NULL) |
34e3edbf DSH |
3520 | return 0; |
3521 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); | |
3522 | } else { | |
3523 | salgs = NULL; | |
3524 | } | |
0f113f3e MC |
3525 | c->shared_sigalgs = salgs; |
3526 | c->shared_sigalgslen = nmatch; | |
3527 | return 1; | |
3528 | } | |
4453cd8c | 3529 | |
6b7be581 DSH |
3530 | /* Set preferred digest for each key type */ |
3531 | ||
c800c27a | 3532 | int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize) |
0f113f3e MC |
3533 | { |
3534 | CERT *c = s->cert; | |
3535 | /* Extension ignored for inappropriate versions */ | |
3536 | if (!SSL_USE_SIGALGS(s)) | |
3537 | return 1; | |
3538 | /* Should never happen */ | |
3539 | if (!c) | |
3540 | return 0; | |
3541 | ||
76106e60 DSH |
3542 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
3543 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize); | |
3544 | if (s->s3->tmp.peer_sigalgs == NULL) | |
0f113f3e | 3545 | return 0; |
76106e60 DSH |
3546 | s->s3->tmp.peer_sigalgslen = dsize; |
3547 | memcpy(s->s3->tmp.peer_sigalgs, data, dsize); | |
0f113f3e MC |
3548 | return 1; |
3549 | } | |
6b7be581 | 3550 | |
c800c27a | 3551 | int tls1_process_sigalgs(SSL *s) |
0f113f3e MC |
3552 | { |
3553 | int idx; | |
3554 | size_t i; | |
3555 | const EVP_MD *md; | |
d376e57d | 3556 | const EVP_MD **pmd = s->s3->tmp.md; |
f7d53487 | 3557 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
0f113f3e MC |
3558 | CERT *c = s->cert; |
3559 | TLS_SIGALGS *sigptr; | |
3560 | if (!tls1_set_shared_sigalgs(s)) | |
3561 | return 0; | |
3562 | ||
0f113f3e MC |
3563 | for (i = 0, sigptr = c->shared_sigalgs; |
3564 | i < c->shared_sigalgslen; i++, sigptr++) { | |
3565 | idx = tls12_get_pkey_idx(sigptr->rsign); | |
d376e57d | 3566 | if (idx > 0 && pmd[idx] == NULL) { |
0f113f3e | 3567 | md = tls12_get_hash(sigptr->rhash); |
d376e57d | 3568 | pmd[idx] = md; |
6383d316 | 3569 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN; |
0f113f3e | 3570 | if (idx == SSL_PKEY_RSA_SIGN) { |
6383d316 | 3571 | pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN; |
d376e57d | 3572 | pmd[SSL_PKEY_RSA_ENC] = md; |
0f113f3e MC |
3573 | } |
3574 | } | |
6b7be581 | 3575 | |
0f113f3e MC |
3576 | } |
3577 | /* | |
3578 | * In strict mode leave unset digests as NULL to indicate we can't use | |
3579 | * the certificate for signing. | |
3580 | */ | |
3581 | if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
3582 | /* | |
3583 | * Set any remaining keys to default values. NOTE: if alg is not | |
3584 | * supported it stays as NULL. | |
3585 | */ | |
e481f9b9 | 3586 | #ifndef OPENSSL_NO_DSA |
d376e57d DSH |
3587 | if (pmd[SSL_PKEY_DSA_SIGN] == NULL) |
3588 | pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1(); | |
e481f9b9 MC |
3589 | #endif |
3590 | #ifndef OPENSSL_NO_RSA | |
d376e57d DSH |
3591 | if (pmd[SSL_PKEY_RSA_SIGN] == NULL) { |
3592 | pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1(); | |
3593 | pmd[SSL_PKEY_RSA_ENC] = EVP_sha1(); | |
0f113f3e | 3594 | } |
e481f9b9 MC |
3595 | #endif |
3596 | #ifndef OPENSSL_NO_EC | |
d376e57d DSH |
3597 | if (pmd[SSL_PKEY_ECC] == NULL) |
3598 | pmd[SSL_PKEY_ECC] = EVP_sha1(); | |
e481f9b9 | 3599 | #endif |
e44380a9 DB |
3600 | # ifndef OPENSSL_NO_GOST |
3601 | if (pmd[SSL_PKEY_GOST01] == NULL) | |
3602 | pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94); | |
3603 | if (pmd[SSL_PKEY_GOST12_256] == NULL) | |
3604 | pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256); | |
3605 | if (pmd[SSL_PKEY_GOST12_512] == NULL) | |
3606 | pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512); | |
3607 | # endif | |
0f113f3e MC |
3608 | } |
3609 | return 1; | |
3610 | } | |
4817504d | 3611 | |
e7f8ff43 | 3612 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
3613 | int *psign, int *phash, int *psignhash, |
3614 | unsigned char *rsig, unsigned char *rhash) | |
3615 | { | |
76106e60 | 3616 | const unsigned char *psig = s->s3->tmp.peer_sigalgs; |
0f113f3e MC |
3617 | if (psig == NULL) |
3618 | return 0; | |
3619 | if (idx >= 0) { | |
3620 | idx <<= 1; | |
76106e60 | 3621 | if (idx >= (int)s->s3->tmp.peer_sigalgslen) |
0f113f3e MC |
3622 | return 0; |
3623 | psig += idx; | |
3624 | if (rhash) | |
3625 | *rhash = psig[0]; | |
3626 | if (rsig) | |
3627 | *rsig = psig[1]; | |
3628 | tls1_lookup_sigalg(phash, psign, psignhash, psig); | |
3629 | } | |
76106e60 | 3630 | return s->s3->tmp.peer_sigalgslen / 2; |
0f113f3e | 3631 | } |
4453cd8c DSH |
3632 | |
3633 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
3634 | int *psign, int *phash, int *psignhash, |
3635 | unsigned char *rsig, unsigned char *rhash) | |
3636 | { | |
3637 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; | |
3638 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) | |
3639 | return 0; | |
3640 | shsigalgs += idx; | |
3641 | if (phash) | |
3642 | *phash = shsigalgs->hash_nid; | |
3643 | if (psign) | |
3644 | *psign = shsigalgs->sign_nid; | |
3645 | if (psignhash) | |
3646 | *psignhash = shsigalgs->signandhash_nid; | |
3647 | if (rsig) | |
3648 | *rsig = shsigalgs->rsign; | |
3649 | if (rhash) | |
3650 | *rhash = shsigalgs->rhash; | |
3651 | return s->cert->shared_sigalgslen; | |
3652 | } | |
3653 | ||
e481f9b9 | 3654 | #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2) |
0f229cce | 3655 | |
0f113f3e MC |
3656 | typedef struct { |
3657 | size_t sigalgcnt; | |
3658 | int sigalgs[MAX_SIGALGLEN]; | |
3659 | } sig_cb_st; | |
0f229cce | 3660 | |
431f458d DSH |
3661 | static void get_sigorhash(int *psig, int *phash, const char *str) |
3662 | { | |
3663 | if (strcmp(str, "RSA") == 0) { | |
3664 | *psig = EVP_PKEY_RSA; | |
3665 | } else if (strcmp(str, "DSA") == 0) { | |
3666 | *psig = EVP_PKEY_DSA; | |
3667 | } else if (strcmp(str, "ECDSA") == 0) { | |
3668 | *psig = EVP_PKEY_EC; | |
3669 | } else { | |
3670 | *phash = OBJ_sn2nid(str); | |
3671 | if (*phash == NID_undef) | |
3672 | *phash = OBJ_ln2nid(str); | |
3673 | } | |
3674 | } | |
3675 | ||
0f229cce | 3676 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
3677 | { |
3678 | sig_cb_st *sarg = arg; | |
3679 | size_t i; | |
3680 | char etmp[20], *p; | |
431f458d | 3681 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
3682 | if (elem == NULL) |
3683 | return 0; | |
0f113f3e MC |
3684 | if (sarg->sigalgcnt == MAX_SIGALGLEN) |
3685 | return 0; | |
3686 | if (len > (int)(sizeof(etmp) - 1)) | |
3687 | return 0; | |
3688 | memcpy(etmp, elem, len); | |
3689 | etmp[len] = 0; | |
3690 | p = strchr(etmp, '+'); | |
3691 | if (!p) | |
3692 | return 0; | |
3693 | *p = 0; | |
3694 | p++; | |
3695 | if (!*p) | |
3696 | return 0; | |
3697 | ||
431f458d DSH |
3698 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
3699 | get_sigorhash(&sig_alg, &hash_alg, p); | |
0f113f3e | 3700 | |
431f458d | 3701 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
0f113f3e MC |
3702 | return 0; |
3703 | ||
3704 | for (i = 0; i < sarg->sigalgcnt; i += 2) { | |
3705 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) | |
3706 | return 0; | |
3707 | } | |
3708 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; | |
3709 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; | |
3710 | return 1; | |
3711 | } | |
3712 | ||
3713 | /* | |
3714 | * Set suppored signature algorithms based on a colon separated list of the | |
3715 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 | |
3716 | */ | |
3dbc46df | 3717 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
3718 | { |
3719 | sig_cb_st sig; | |
3720 | sig.sigalgcnt = 0; | |
3721 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
3722 | return 0; | |
3723 | if (c == NULL) | |
3724 | return 1; | |
3725 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); | |
3726 | } | |
3727 | ||
3728 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, | |
3729 | int client) | |
3730 | { | |
3731 | unsigned char *sigalgs, *sptr; | |
3732 | int rhash, rsign; | |
3733 | size_t i; | |
3734 | if (salglen & 1) | |
3735 | return 0; | |
3736 | sigalgs = OPENSSL_malloc(salglen); | |
3737 | if (sigalgs == NULL) | |
3738 | return 0; | |
3739 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | |
b6eb9827 DSH |
3740 | rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md)); |
3741 | rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig)); | |
0f113f3e MC |
3742 | |
3743 | if (rhash == -1 || rsign == -1) | |
3744 | goto err; | |
3745 | *sptr++ = rhash; | |
3746 | *sptr++ = rsign; | |
3747 | } | |
3748 | ||
3749 | if (client) { | |
b548a1f1 | 3750 | OPENSSL_free(c->client_sigalgs); |
0f113f3e MC |
3751 | c->client_sigalgs = sigalgs; |
3752 | c->client_sigalgslen = salglen; | |
3753 | } else { | |
b548a1f1 | 3754 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e MC |
3755 | c->conf_sigalgs = sigalgs; |
3756 | c->conf_sigalgslen = salglen; | |
3757 | } | |
3758 | ||
3759 | return 1; | |
3760 | ||
3761 | err: | |
3762 | OPENSSL_free(sigalgs); | |
3763 | return 0; | |
3764 | } | |
4453cd8c | 3765 | |
d61ff83b | 3766 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
0f113f3e MC |
3767 | { |
3768 | int sig_nid; | |
3769 | size_t i; | |
3770 | if (default_nid == -1) | |
3771 | return 1; | |
3772 | sig_nid = X509_get_signature_nid(x); | |
3773 | if (default_nid) | |
3774 | return sig_nid == default_nid ? 1 : 0; | |
3775 | for (i = 0; i < c->shared_sigalgslen; i++) | |
3776 | if (sig_nid == c->shared_sigalgs[i].signandhash_nid) | |
3777 | return 1; | |
3778 | return 0; | |
3779 | } | |
3780 | ||
6dbb6219 DSH |
3781 | /* Check to see if a certificate issuer name matches list of CA names */ |
3782 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e MC |
3783 | { |
3784 | X509_NAME *nm; | |
3785 | int i; | |
3786 | nm = X509_get_issuer_name(x); | |
3787 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
3788 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
3789 | return 1; | |
3790 | } | |
3791 | return 0; | |
3792 | } | |
3793 | ||
3794 | /* | |
3795 | * Check certificate chain is consistent with TLS extensions and is usable by | |
3796 | * server. This servers two purposes: it allows users to check chains before | |
3797 | * passing them to the server and it allows the server to check chains before | |
3798 | * attempting to use them. | |
d61ff83b | 3799 | */ |
6dbb6219 DSH |
3800 | |
3801 | /* Flags which need to be set for a certificate when stict mode not set */ | |
3802 | ||
e481f9b9 | 3803 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 3804 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 3805 | /* Strict mode flags */ |
e481f9b9 | 3806 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
3807 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
3808 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 3809 | |
d61ff83b | 3810 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
0f113f3e MC |
3811 | int idx) |
3812 | { | |
3813 | int i; | |
3814 | int rv = 0; | |
3815 | int check_flags = 0, strict_mode; | |
3816 | CERT_PKEY *cpk = NULL; | |
3817 | CERT *c = s->cert; | |
f7d53487 | 3818 | uint32_t *pvalid; |
0f113f3e MC |
3819 | unsigned int suiteb_flags = tls1_suiteb(s); |
3820 | /* idx == -1 means checking server chains */ | |
3821 | if (idx != -1) { | |
3822 | /* idx == -2 means checking client certificate chains */ | |
3823 | if (idx == -2) { | |
3824 | cpk = c->key; | |
3825 | idx = cpk - c->pkeys; | |
3826 | } else | |
3827 | cpk = c->pkeys + idx; | |
6383d316 | 3828 | pvalid = s->s3->tmp.valid_flags + idx; |
0f113f3e MC |
3829 | x = cpk->x509; |
3830 | pk = cpk->privatekey; | |
3831 | chain = cpk->chain; | |
3832 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
3833 | /* If no cert or key, forget it */ | |
3834 | if (!x || !pk) | |
3835 | goto end; | |
0f113f3e MC |
3836 | } else { |
3837 | if (!x || !pk) | |
d813f9eb | 3838 | return 0; |
0f113f3e MC |
3839 | idx = ssl_cert_type(x, pk); |
3840 | if (idx == -1) | |
d813f9eb | 3841 | return 0; |
6383d316 DSH |
3842 | pvalid = s->s3->tmp.valid_flags + idx; |
3843 | ||
0f113f3e MC |
3844 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
3845 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
3846 | else | |
3847 | check_flags = CERT_PKEY_VALID_FLAGS; | |
3848 | strict_mode = 1; | |
3849 | } | |
3850 | ||
3851 | if (suiteb_flags) { | |
3852 | int ok; | |
3853 | if (check_flags) | |
3854 | check_flags |= CERT_PKEY_SUITEB; | |
3855 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
3856 | if (ok == X509_V_OK) | |
3857 | rv |= CERT_PKEY_SUITEB; | |
3858 | else if (!check_flags) | |
3859 | goto end; | |
3860 | } | |
3861 | ||
3862 | /* | |
3863 | * Check all signature algorithms are consistent with signature | |
3864 | * algorithms extension if TLS 1.2 or later and strict mode. | |
3865 | */ | |
3866 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { | |
3867 | int default_nid; | |
3868 | unsigned char rsign = 0; | |
76106e60 | 3869 | if (s->s3->tmp.peer_sigalgs) |
0f113f3e MC |
3870 | default_nid = 0; |
3871 | /* If no sigalgs extension use defaults from RFC5246 */ | |
3872 | else { | |
3873 | switch (idx) { | |
3874 | case SSL_PKEY_RSA_ENC: | |
3875 | case SSL_PKEY_RSA_SIGN: | |
0f113f3e MC |
3876 | rsign = TLSEXT_signature_rsa; |
3877 | default_nid = NID_sha1WithRSAEncryption; | |
3878 | break; | |
3879 | ||
3880 | case SSL_PKEY_DSA_SIGN: | |
0f113f3e MC |
3881 | rsign = TLSEXT_signature_dsa; |
3882 | default_nid = NID_dsaWithSHA1; | |
3883 | break; | |
3884 | ||
3885 | case SSL_PKEY_ECC: | |
3886 | rsign = TLSEXT_signature_ecdsa; | |
3887 | default_nid = NID_ecdsa_with_SHA1; | |
3888 | break; | |
3889 | ||
e44380a9 DB |
3890 | case SSL_PKEY_GOST01: |
3891 | rsign = TLSEXT_signature_gostr34102001; | |
3892 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; | |
3893 | break; | |
3894 | ||
3895 | case SSL_PKEY_GOST12_256: | |
3896 | rsign = TLSEXT_signature_gostr34102012_256; | |
3897 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; | |
3898 | break; | |
3899 | ||
3900 | case SSL_PKEY_GOST12_512: | |
3901 | rsign = TLSEXT_signature_gostr34102012_512; | |
3902 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; | |
3903 | break; | |
3904 | ||
0f113f3e MC |
3905 | default: |
3906 | default_nid = -1; | |
3907 | break; | |
3908 | } | |
3909 | } | |
3910 | /* | |
3911 | * If peer sent no signature algorithms extension and we have set | |
3912 | * preferred signature algorithms check we support sha1. | |
3913 | */ | |
3914 | if (default_nid > 0 && c->conf_sigalgs) { | |
3915 | size_t j; | |
3916 | const unsigned char *p = c->conf_sigalgs; | |
3917 | for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) { | |
3918 | if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign) | |
3919 | break; | |
3920 | } | |
3921 | if (j == c->conf_sigalgslen) { | |
3922 | if (check_flags) | |
3923 | goto skip_sigs; | |
3924 | else | |
3925 | goto end; | |
3926 | } | |
3927 | } | |
3928 | /* Check signature algorithm of each cert in chain */ | |
3929 | if (!tls1_check_sig_alg(c, x, default_nid)) { | |
3930 | if (!check_flags) | |
3931 | goto end; | |
3932 | } else | |
3933 | rv |= CERT_PKEY_EE_SIGNATURE; | |
3934 | rv |= CERT_PKEY_CA_SIGNATURE; | |
3935 | for (i = 0; i < sk_X509_num(chain); i++) { | |
3936 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { | |
3937 | if (check_flags) { | |
3938 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
3939 | break; | |
3940 | } else | |
3941 | goto end; | |
3942 | } | |
3943 | } | |
3944 | } | |
3945 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
3946 | else if (check_flags) | |
3947 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
3948 | skip_sigs: | |
3949 | /* Check cert parameters are consistent */ | |
3950 | if (tls1_check_cert_param(s, x, check_flags ? 1 : 2)) | |
3951 | rv |= CERT_PKEY_EE_PARAM; | |
3952 | else if (!check_flags) | |
3953 | goto end; | |
3954 | if (!s->server) | |
3955 | rv |= CERT_PKEY_CA_PARAM; | |
3956 | /* In strict mode check rest of chain too */ | |
3957 | else if (strict_mode) { | |
3958 | rv |= CERT_PKEY_CA_PARAM; | |
3959 | for (i = 0; i < sk_X509_num(chain); i++) { | |
3960 | X509 *ca = sk_X509_value(chain, i); | |
3961 | if (!tls1_check_cert_param(s, ca, 0)) { | |
3962 | if (check_flags) { | |
3963 | rv &= ~CERT_PKEY_CA_PARAM; | |
3964 | break; | |
3965 | } else | |
3966 | goto end; | |
3967 | } | |
3968 | } | |
3969 | } | |
3970 | if (!s->server && strict_mode) { | |
3971 | STACK_OF(X509_NAME) *ca_dn; | |
3972 | int check_type = 0; | |
3aeb9348 | 3973 | switch (EVP_PKEY_id(pk)) { |
0f113f3e MC |
3974 | case EVP_PKEY_RSA: |
3975 | check_type = TLS_CT_RSA_SIGN; | |
3976 | break; | |
3977 | case EVP_PKEY_DSA: | |
3978 | check_type = TLS_CT_DSS_SIGN; | |
3979 | break; | |
3980 | case EVP_PKEY_EC: | |
3981 | check_type = TLS_CT_ECDSA_SIGN; | |
3982 | break; | |
0f113f3e MC |
3983 | } |
3984 | if (check_type) { | |
3985 | const unsigned char *ctypes; | |
3986 | int ctypelen; | |
3987 | if (c->ctypes) { | |
3988 | ctypes = c->ctypes; | |
3989 | ctypelen = (int)c->ctype_num; | |
3990 | } else { | |
3991 | ctypes = (unsigned char *)s->s3->tmp.ctype; | |
3992 | ctypelen = s->s3->tmp.ctype_num; | |
3993 | } | |
3994 | for (i = 0; i < ctypelen; i++) { | |
3995 | if (ctypes[i] == check_type) { | |
3996 | rv |= CERT_PKEY_CERT_TYPE; | |
3997 | break; | |
3998 | } | |
3999 | } | |
4000 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
4001 | goto end; | |
4002 | } else | |
4003 | rv |= CERT_PKEY_CERT_TYPE; | |
4004 | ||
4005 | ca_dn = s->s3->tmp.ca_names; | |
4006 | ||
4007 | if (!sk_X509_NAME_num(ca_dn)) | |
4008 | rv |= CERT_PKEY_ISSUER_NAME; | |
4009 | ||
4010 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
4011 | if (ssl_check_ca_name(ca_dn, x)) | |
4012 | rv |= CERT_PKEY_ISSUER_NAME; | |
4013 | } | |
4014 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
4015 | for (i = 0; i < sk_X509_num(chain); i++) { | |
4016 | X509 *xtmp = sk_X509_value(chain, i); | |
4017 | if (ssl_check_ca_name(ca_dn, xtmp)) { | |
4018 | rv |= CERT_PKEY_ISSUER_NAME; | |
4019 | break; | |
4020 | } | |
4021 | } | |
4022 | } | |
4023 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) | |
4024 | goto end; | |
4025 | } else | |
4026 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
4027 | ||
4028 | if (!check_flags || (rv & check_flags) == check_flags) | |
4029 | rv |= CERT_PKEY_VALID; | |
4030 | ||
4031 | end: | |
4032 | ||
4033 | if (TLS1_get_version(s) >= TLS1_2_VERSION) { | |
6383d316 | 4034 | if (*pvalid & CERT_PKEY_EXPLICIT_SIGN) |
0f113f3e | 4035 | rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
d376e57d | 4036 | else if (s->s3->tmp.md[idx] != NULL) |
0f113f3e MC |
4037 | rv |= CERT_PKEY_SIGN; |
4038 | } else | |
4039 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; | |
4040 | ||
4041 | /* | |
4042 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
4043 | * chain is invalid. | |
4044 | */ | |
4045 | if (!check_flags) { | |
4046 | if (rv & CERT_PKEY_VALID) | |
6383d316 | 4047 | *pvalid = rv; |
0f113f3e MC |
4048 | else { |
4049 | /* Preserve explicit sign flag, clear rest */ | |
6383d316 | 4050 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN; |
0f113f3e MC |
4051 | return 0; |
4052 | } | |
4053 | } | |
4054 | return rv; | |
4055 | } | |
d61ff83b DSH |
4056 | |
4057 | /* Set validity of certificates in an SSL structure */ | |
4058 | void tls1_set_cert_validity(SSL *s) | |
0f113f3e | 4059 | { |
17dd65e6 MC |
4060 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC); |
4061 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN); | |
4062 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); | |
17dd65e6 | 4063 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
4064 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
4065 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
4066 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
0f113f3e MC |
4067 | } |
4068 | ||
18d71588 DSH |
4069 | /* User level utiity function to check a chain is suitable */ |
4070 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) | |
0f113f3e MC |
4071 | { |
4072 | return tls1_check_chain(s, x, pk, chain, -1); | |
4073 | } | |
d61ff83b | 4074 | |
09599b52 DSH |
4075 | |
4076 | #ifndef OPENSSL_NO_DH | |
4077 | DH *ssl_get_auto_dh(SSL *s) | |
0f113f3e MC |
4078 | { |
4079 | int dh_secbits = 80; | |
4080 | if (s->cert->dh_tmp_auto == 2) | |
4081 | return DH_get_1024_160(); | |
adc5506a | 4082 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
0f113f3e MC |
4083 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
4084 | dh_secbits = 128; | |
4085 | else | |
4086 | dh_secbits = 80; | |
4087 | } else { | |
4088 | CERT_PKEY *cpk = ssl_get_server_send_pkey(s); | |
4089 | dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); | |
4090 | } | |
4091 | ||
4092 | if (dh_secbits >= 128) { | |
4093 | DH *dhp = DH_new(); | |
0aeddcfa | 4094 | BIGNUM *p, *g; |
a71edf3b | 4095 | if (dhp == NULL) |
0f113f3e | 4096 | return NULL; |
0aeddcfa MC |
4097 | g = BN_new(); |
4098 | if (g != NULL) | |
4099 | BN_set_word(g, 2); | |
0f113f3e | 4100 | if (dh_secbits >= 192) |
0aeddcfa | 4101 | p = get_rfc3526_prime_8192(NULL); |
0f113f3e | 4102 | else |
0aeddcfa MC |
4103 | p = get_rfc3526_prime_3072(NULL); |
4104 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { | |
0f113f3e | 4105 | DH_free(dhp); |
0aeddcfa MC |
4106 | BN_free(p); |
4107 | BN_free(g); | |
0f113f3e MC |
4108 | return NULL; |
4109 | } | |
4110 | return dhp; | |
4111 | } | |
4112 | if (dh_secbits >= 112) | |
4113 | return DH_get_2048_224(); | |
4114 | return DH_get_1024_160(); | |
4115 | } | |
09599b52 | 4116 | #endif |
b362ccab DSH |
4117 | |
4118 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e | 4119 | { |
72245f34 | 4120 | int secbits = -1; |
8382fd3a | 4121 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
0f113f3e | 4122 | if (pkey) { |
72245f34 DSH |
4123 | /* |
4124 | * If no parameters this will return -1 and fail using the default | |
4125 | * security callback for any non-zero security level. This will | |
4126 | * reject keys which omit parameters but this only affects DSA and | |
4127 | * omission of parameters is never (?) done in practice. | |
4128 | */ | |
0f113f3e | 4129 | secbits = EVP_PKEY_security_bits(pkey); |
72245f34 | 4130 | } |
0f113f3e MC |
4131 | if (s) |
4132 | return ssl_security(s, op, secbits, 0, x); | |
4133 | else | |
4134 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
4135 | } | |
b362ccab DSH |
4136 | |
4137 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e MC |
4138 | { |
4139 | /* Lookup signature algorithm digest */ | |
4140 | int secbits = -1, md_nid = NID_undef, sig_nid; | |
221c7b55 DSH |
4141 | /* Don't check signature if self signed */ |
4142 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
4143 | return 1; | |
0f113f3e MC |
4144 | sig_nid = X509_get_signature_nid(x); |
4145 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { | |
4146 | const EVP_MD *md; | |
4147 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) | |
4148 | secbits = EVP_MD_size(md) * 4; | |
4149 | } | |
4150 | if (s) | |
4151 | return ssl_security(s, op, secbits, md_nid, x); | |
4152 | else | |
4153 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); | |
4154 | } | |
b362ccab DSH |
4155 | |
4156 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) | |
0f113f3e MC |
4157 | { |
4158 | if (vfy) | |
4159 | vfy = SSL_SECOP_PEER; | |
4160 | if (is_ee) { | |
4161 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
4162 | return SSL_R_EE_KEY_TOO_SMALL; | |
4163 | } else { | |
4164 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
4165 | return SSL_R_CA_KEY_TOO_SMALL; | |
4166 | } | |
4167 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
4168 | return SSL_R_CA_MD_TOO_WEAK; | |
4169 | return 1; | |
4170 | } | |
4171 | ||
4172 | /* | |
4173 | * Check security of a chain, if sk includes the end entity certificate then | |
4174 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending | |
4175 | * one to the peer. Return values: 1 if ok otherwise error code to use | |
b362ccab DSH |
4176 | */ |
4177 | ||
4178 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) | |
0f113f3e MC |
4179 | { |
4180 | int rv, start_idx, i; | |
4181 | if (x == NULL) { | |
4182 | x = sk_X509_value(sk, 0); | |
4183 | start_idx = 1; | |
4184 | } else | |
4185 | start_idx = 0; | |
4186 | ||
4187 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
4188 | if (rv != 1) | |
4189 | return rv; | |
4190 | ||
4191 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
4192 | x = sk_X509_value(sk, i); | |
4193 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
4194 | if (rv != 1) | |
4195 | return rv; | |
4196 | } | |
4197 | return 1; | |
4198 | } |