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