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