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