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