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