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