diff options
Diffstat (limited to 'src/lib/libssl/s3_clnt.c')
-rw-r--r-- | src/lib/libssl/s3_clnt.c | 2726 |
1 files changed, 0 insertions, 2726 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c deleted file mode 100644 index d68aecf541..0000000000 --- a/src/lib/libssl/s3_clnt.c +++ /dev/null | |||
@@ -1,2726 +0,0 @@ | |||
1 | /* $OpenBSD: s3_clnt.c,v 1.108 2015/03/08 16:48:47 miod Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
111 | /* ==================================================================== | ||
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
113 | * | ||
114 | * Portions of the attached software ("Contribution") are developed by | ||
115 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | ||
116 | * | ||
117 | * The Contribution is licensed pursuant to the OpenSSL open source | ||
118 | * license provided above. | ||
119 | * | ||
120 | * ECC cipher suite support in OpenSSL originally written by | ||
121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | ||
122 | * | ||
123 | */ | ||
124 | /* ==================================================================== | ||
125 | * Copyright 2005 Nokia. All rights reserved. | ||
126 | * | ||
127 | * The portions of the attached software ("Contribution") is developed by | ||
128 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | ||
129 | * license. | ||
130 | * | ||
131 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | ||
132 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | ||
133 | * support (see RFC 4279) to OpenSSL. | ||
134 | * | ||
135 | * No patent licenses or other rights except those expressly stated in | ||
136 | * the OpenSSL open source license shall be deemed granted or received | ||
137 | * expressly, by implication, estoppel, or otherwise. | ||
138 | * | ||
139 | * No assurances are provided by Nokia that the Contribution does not | ||
140 | * infringe the patent or other intellectual property rights of any third | ||
141 | * party or that the license provides you with all the necessary rights | ||
142 | * to make use of the Contribution. | ||
143 | * | ||
144 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | ||
145 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | ||
146 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | ||
147 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | ||
148 | * OTHERWISE. | ||
149 | */ | ||
150 | |||
151 | #include <stdio.h> | ||
152 | |||
153 | #include "ssl_locl.h" | ||
154 | |||
155 | #include <openssl/bn.h> | ||
156 | #include <openssl/buffer.h> | ||
157 | #include <openssl/dh.h> | ||
158 | #include <openssl/evp.h> | ||
159 | #include <openssl/md5.h> | ||
160 | #include <openssl/objects.h> | ||
161 | |||
162 | #ifndef OPENSSL_NO_ENGINE | ||
163 | #include <openssl/engine.h> | ||
164 | #endif | ||
165 | #ifndef OPENSSL_NO_GOST | ||
166 | #include <openssl/gost.h> | ||
167 | #endif | ||
168 | |||
169 | static const SSL_METHOD *ssl3_get_client_method(int ver); | ||
170 | static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b); | ||
171 | |||
172 | const SSL_METHOD SSLv3_client_method_data = { | ||
173 | .version = SSL3_VERSION, | ||
174 | .ssl_new = ssl3_new, | ||
175 | .ssl_clear = ssl3_clear, | ||
176 | .ssl_free = ssl3_free, | ||
177 | .ssl_accept = ssl_undefined_function, | ||
178 | .ssl_connect = ssl3_connect, | ||
179 | .ssl_read = ssl3_read, | ||
180 | .ssl_peek = ssl3_peek, | ||
181 | .ssl_write = ssl3_write, | ||
182 | .ssl_shutdown = ssl3_shutdown, | ||
183 | .ssl_renegotiate = ssl3_renegotiate, | ||
184 | .ssl_renegotiate_check = ssl3_renegotiate_check, | ||
185 | .ssl_get_message = ssl3_get_message, | ||
186 | .ssl_read_bytes = ssl3_read_bytes, | ||
187 | .ssl_write_bytes = ssl3_write_bytes, | ||
188 | .ssl_dispatch_alert = ssl3_dispatch_alert, | ||
189 | .ssl_ctrl = ssl3_ctrl, | ||
190 | .ssl_ctx_ctrl = ssl3_ctx_ctrl, | ||
191 | .get_cipher_by_char = ssl3_get_cipher_by_char, | ||
192 | .put_cipher_by_char = ssl3_put_cipher_by_char, | ||
193 | .ssl_pending = ssl3_pending, | ||
194 | .num_ciphers = ssl3_num_ciphers, | ||
195 | .get_cipher = ssl3_get_cipher, | ||
196 | .get_ssl_method = ssl3_get_client_method, | ||
197 | .get_timeout = ssl3_default_timeout, | ||
198 | .ssl3_enc = &SSLv3_enc_data, | ||
199 | .ssl_version = ssl_undefined_void_function, | ||
200 | .ssl_callback_ctrl = ssl3_callback_ctrl, | ||
201 | .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl, | ||
202 | }; | ||
203 | |||
204 | const SSL_METHOD * | ||
205 | SSLv3_client_method(void) | ||
206 | { | ||
207 | return &SSLv3_client_method_data; | ||
208 | } | ||
209 | |||
210 | static const SSL_METHOD * | ||
211 | ssl3_get_client_method(int ver) | ||
212 | { | ||
213 | if (ver == SSL3_VERSION) | ||
214 | return (SSLv3_client_method()); | ||
215 | return (NULL); | ||
216 | } | ||
217 | |||
218 | int | ||
219 | ssl3_connect(SSL *s) | ||
220 | { | ||
221 | BUF_MEM *buf = NULL; | ||
222 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
223 | int ret = -1; | ||
224 | int new_state, state, skip = 0; | ||
225 | |||
226 | ERR_clear_error(); | ||
227 | errno = 0; | ||
228 | |||
229 | if (s->info_callback != NULL) | ||
230 | cb = s->info_callback; | ||
231 | else if (s->ctx->info_callback != NULL) | ||
232 | cb = s->ctx->info_callback; | ||
233 | |||
234 | s->in_handshake++; | ||
235 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
236 | SSL_clear(s); | ||
237 | |||
238 | for (;;) { | ||
239 | state = s->state; | ||
240 | |||
241 | switch (s->state) { | ||
242 | case SSL_ST_RENEGOTIATE: | ||
243 | s->renegotiate = 1; | ||
244 | s->state = SSL_ST_CONNECT; | ||
245 | s->ctx->stats.sess_connect_renegotiate++; | ||
246 | /* break */ | ||
247 | case SSL_ST_BEFORE: | ||
248 | case SSL_ST_CONNECT: | ||
249 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | ||
250 | case SSL_ST_OK|SSL_ST_CONNECT: | ||
251 | |||
252 | s->server = 0; | ||
253 | if (cb != NULL) | ||
254 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
255 | |||
256 | if ((s->version & 0xff00 ) != 0x0300) { | ||
257 | SSLerr(SSL_F_SSL3_CONNECT, | ||
258 | ERR_R_INTERNAL_ERROR); | ||
259 | ret = -1; | ||
260 | goto end; | ||
261 | } | ||
262 | |||
263 | /* s->version=SSL3_VERSION; */ | ||
264 | s->type = SSL_ST_CONNECT; | ||
265 | |||
266 | if (s->init_buf == NULL) { | ||
267 | if ((buf = BUF_MEM_new()) == NULL) { | ||
268 | ret = -1; | ||
269 | goto end; | ||
270 | } | ||
271 | if (!BUF_MEM_grow(buf, | ||
272 | SSL3_RT_MAX_PLAIN_LENGTH)) { | ||
273 | ret = -1; | ||
274 | goto end; | ||
275 | } | ||
276 | s->init_buf = buf; | ||
277 | buf = NULL; | ||
278 | } | ||
279 | |||
280 | if (!ssl3_setup_buffers(s)) { | ||
281 | ret = -1; | ||
282 | goto end; | ||
283 | } | ||
284 | |||
285 | /* setup buffing BIO */ | ||
286 | if (!ssl_init_wbio_buffer(s, 0)) { | ||
287 | ret = -1; | ||
288 | goto end; | ||
289 | } | ||
290 | |||
291 | /* don't push the buffering BIO quite yet */ | ||
292 | |||
293 | if (!ssl3_init_finished_mac(s)) { | ||
294 | ret = -1; | ||
295 | goto end; | ||
296 | } | ||
297 | |||
298 | s->state = SSL3_ST_CW_CLNT_HELLO_A; | ||
299 | s->ctx->stats.sess_connect++; | ||
300 | s->init_num = 0; | ||
301 | break; | ||
302 | |||
303 | case SSL3_ST_CW_CLNT_HELLO_A: | ||
304 | case SSL3_ST_CW_CLNT_HELLO_B: | ||
305 | |||
306 | s->shutdown = 0; | ||
307 | ret = ssl3_client_hello(s); | ||
308 | if (ret <= 0) | ||
309 | goto end; | ||
310 | s->state = SSL3_ST_CR_SRVR_HELLO_A; | ||
311 | s->init_num = 0; | ||
312 | |||
313 | /* turn on buffering for the next lot of output */ | ||
314 | if (s->bbio != s->wbio) | ||
315 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
316 | |||
317 | break; | ||
318 | |||
319 | case SSL3_ST_CR_SRVR_HELLO_A: | ||
320 | case SSL3_ST_CR_SRVR_HELLO_B: | ||
321 | ret = ssl3_get_server_hello(s); | ||
322 | if (ret <= 0) | ||
323 | goto end; | ||
324 | |||
325 | if (s->hit) { | ||
326 | s->state = SSL3_ST_CR_FINISHED_A; | ||
327 | if (s->tlsext_ticket_expected) { | ||
328 | /* receive renewed session ticket */ | ||
329 | s->state = SSL3_ST_CR_SESSION_TICKET_A; | ||
330 | } | ||
331 | } else | ||
332 | s->state = SSL3_ST_CR_CERT_A; | ||
333 | s->init_num = 0; | ||
334 | break; | ||
335 | |||
336 | case SSL3_ST_CR_CERT_A: | ||
337 | case SSL3_ST_CR_CERT_B: | ||
338 | ret = ssl3_check_finished(s); | ||
339 | if (ret <= 0) | ||
340 | goto end; | ||
341 | if (ret == 2) { | ||
342 | s->hit = 1; | ||
343 | if (s->tlsext_ticket_expected) | ||
344 | s->state = SSL3_ST_CR_SESSION_TICKET_A; | ||
345 | else | ||
346 | s->state = SSL3_ST_CR_FINISHED_A; | ||
347 | s->init_num = 0; | ||
348 | break; | ||
349 | } | ||
350 | /* Check if it is anon DH/ECDH. */ | ||
351 | if (!(s->s3->tmp.new_cipher->algorithm_auth & | ||
352 | SSL_aNULL)) { | ||
353 | ret = ssl3_get_server_certificate(s); | ||
354 | if (ret <= 0) | ||
355 | goto end; | ||
356 | if (s->tlsext_status_expected) | ||
357 | s->state = SSL3_ST_CR_CERT_STATUS_A; | ||
358 | else | ||
359 | s->state = SSL3_ST_CR_KEY_EXCH_A; | ||
360 | } else { | ||
361 | skip = 1; | ||
362 | s->state = SSL3_ST_CR_KEY_EXCH_A; | ||
363 | } | ||
364 | s->init_num = 0; | ||
365 | break; | ||
366 | |||
367 | case SSL3_ST_CR_KEY_EXCH_A: | ||
368 | case SSL3_ST_CR_KEY_EXCH_B: | ||
369 | ret = ssl3_get_key_exchange(s); | ||
370 | if (ret <= 0) | ||
371 | goto end; | ||
372 | s->state = SSL3_ST_CR_CERT_REQ_A; | ||
373 | s->init_num = 0; | ||
374 | |||
375 | /* | ||
376 | * At this point we check that we have the | ||
377 | * required stuff from the server. | ||
378 | */ | ||
379 | if (!ssl3_check_cert_and_algorithm(s)) { | ||
380 | ret = -1; | ||
381 | goto end; | ||
382 | } | ||
383 | break; | ||
384 | |||
385 | case SSL3_ST_CR_CERT_REQ_A: | ||
386 | case SSL3_ST_CR_CERT_REQ_B: | ||
387 | ret = ssl3_get_certificate_request(s); | ||
388 | if (ret <= 0) | ||
389 | goto end; | ||
390 | s->state = SSL3_ST_CR_SRVR_DONE_A; | ||
391 | s->init_num = 0; | ||
392 | break; | ||
393 | |||
394 | case SSL3_ST_CR_SRVR_DONE_A: | ||
395 | case SSL3_ST_CR_SRVR_DONE_B: | ||
396 | ret = ssl3_get_server_done(s); | ||
397 | if (ret <= 0) | ||
398 | goto end; | ||
399 | if (s->s3->tmp.cert_req) | ||
400 | s->state = SSL3_ST_CW_CERT_A; | ||
401 | else | ||
402 | s->state = SSL3_ST_CW_KEY_EXCH_A; | ||
403 | s->init_num = 0; | ||
404 | |||
405 | break; | ||
406 | |||
407 | case SSL3_ST_CW_CERT_A: | ||
408 | case SSL3_ST_CW_CERT_B: | ||
409 | case SSL3_ST_CW_CERT_C: | ||
410 | case SSL3_ST_CW_CERT_D: | ||
411 | ret = ssl3_send_client_certificate(s); | ||
412 | if (ret <= 0) | ||
413 | goto end; | ||
414 | s->state = SSL3_ST_CW_KEY_EXCH_A; | ||
415 | s->init_num = 0; | ||
416 | break; | ||
417 | |||
418 | case SSL3_ST_CW_KEY_EXCH_A: | ||
419 | case SSL3_ST_CW_KEY_EXCH_B: | ||
420 | ret = ssl3_send_client_key_exchange(s); | ||
421 | if (ret <= 0) | ||
422 | goto end; | ||
423 | /* | ||
424 | * EAY EAY EAY need to check for DH fix cert | ||
425 | * sent back | ||
426 | */ | ||
427 | /* | ||
428 | * For TLS, cert_req is set to 2, so a cert chain | ||
429 | * of nothing is sent, but no verify packet is sent | ||
430 | */ | ||
431 | /* | ||
432 | * XXX: For now, we do not support client | ||
433 | * authentication in ECDH cipher suites with | ||
434 | * ECDH (rather than ECDSA) certificates. | ||
435 | * We need to skip the certificate verify | ||
436 | * message when client's ECDH public key is sent | ||
437 | * inside the client certificate. | ||
438 | */ | ||
439 | if (s->s3->tmp.cert_req == 1) { | ||
440 | s->state = SSL3_ST_CW_CERT_VRFY_A; | ||
441 | } else { | ||
442 | s->state = SSL3_ST_CW_CHANGE_A; | ||
443 | s->s3->change_cipher_spec = 0; | ||
444 | } | ||
445 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | ||
446 | s->state = SSL3_ST_CW_CHANGE_A; | ||
447 | s->s3->change_cipher_spec = 0; | ||
448 | } | ||
449 | |||
450 | s->init_num = 0; | ||
451 | break; | ||
452 | |||
453 | case SSL3_ST_CW_CERT_VRFY_A: | ||
454 | case SSL3_ST_CW_CERT_VRFY_B: | ||
455 | ret = ssl3_send_client_verify(s); | ||
456 | if (ret <= 0) | ||
457 | goto end; | ||
458 | s->state = SSL3_ST_CW_CHANGE_A; | ||
459 | s->init_num = 0; | ||
460 | s->s3->change_cipher_spec = 0; | ||
461 | break; | ||
462 | |||
463 | case SSL3_ST_CW_CHANGE_A: | ||
464 | case SSL3_ST_CW_CHANGE_B: | ||
465 | ret = ssl3_send_change_cipher_spec(s, | ||
466 | SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B); | ||
467 | if (ret <= 0) | ||
468 | goto end; | ||
469 | |||
470 | if (s->s3->next_proto_neg_seen) | ||
471 | s->state = SSL3_ST_CW_NEXT_PROTO_A; | ||
472 | else | ||
473 | s->state = SSL3_ST_CW_FINISHED_A; | ||
474 | s->init_num = 0; | ||
475 | |||
476 | s->session->cipher = s->s3->tmp.new_cipher; | ||
477 | if (!s->method->ssl3_enc->setup_key_block(s)) { | ||
478 | ret = -1; | ||
479 | goto end; | ||
480 | } | ||
481 | |||
482 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
483 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { | ||
484 | ret = -1; | ||
485 | goto end; | ||
486 | } | ||
487 | |||
488 | break; | ||
489 | |||
490 | case SSL3_ST_CW_NEXT_PROTO_A: | ||
491 | case SSL3_ST_CW_NEXT_PROTO_B: | ||
492 | ret = ssl3_send_next_proto(s); | ||
493 | if (ret <= 0) | ||
494 | goto end; | ||
495 | s->state = SSL3_ST_CW_FINISHED_A; | ||
496 | break; | ||
497 | |||
498 | case SSL3_ST_CW_FINISHED_A: | ||
499 | case SSL3_ST_CW_FINISHED_B: | ||
500 | ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, | ||
501 | SSL3_ST_CW_FINISHED_B, | ||
502 | s->method->ssl3_enc->client_finished_label, | ||
503 | s->method->ssl3_enc->client_finished_label_len); | ||
504 | if (ret <= 0) | ||
505 | goto end; | ||
506 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
507 | s->state = SSL3_ST_CW_FLUSH; | ||
508 | |||
509 | /* clear flags */ | ||
510 | s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; | ||
511 | if (s->hit) { | ||
512 | s->s3->tmp.next_state = SSL_ST_OK; | ||
513 | if (s->s3->flags & | ||
514 | SSL3_FLAGS_DELAY_CLIENT_FINISHED) { | ||
515 | s->state = SSL_ST_OK; | ||
516 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; | ||
517 | s->s3->delay_buf_pop_ret = 0; | ||
518 | } | ||
519 | } else { | ||
520 | /* Allow NewSessionTicket if ticket expected */ | ||
521 | if (s->tlsext_ticket_expected) | ||
522 | s->s3->tmp.next_state = | ||
523 | SSL3_ST_CR_SESSION_TICKET_A; | ||
524 | else | ||
525 | |||
526 | s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; | ||
527 | } | ||
528 | s->init_num = 0; | ||
529 | break; | ||
530 | |||
531 | case SSL3_ST_CR_SESSION_TICKET_A: | ||
532 | case SSL3_ST_CR_SESSION_TICKET_B: | ||
533 | ret = ssl3_get_new_session_ticket(s); | ||
534 | if (ret <= 0) | ||
535 | goto end; | ||
536 | s->state = SSL3_ST_CR_FINISHED_A; | ||
537 | s->init_num = 0; | ||
538 | break; | ||
539 | |||
540 | case SSL3_ST_CR_CERT_STATUS_A: | ||
541 | case SSL3_ST_CR_CERT_STATUS_B: | ||
542 | ret = ssl3_get_cert_status(s); | ||
543 | if (ret <= 0) | ||
544 | goto end; | ||
545 | s->state = SSL3_ST_CR_KEY_EXCH_A; | ||
546 | s->init_num = 0; | ||
547 | break; | ||
548 | |||
549 | case SSL3_ST_CR_FINISHED_A: | ||
550 | case SSL3_ST_CR_FINISHED_B: | ||
551 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
552 | ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, | ||
553 | SSL3_ST_CR_FINISHED_B); | ||
554 | if (ret <= 0) | ||
555 | goto end; | ||
556 | |||
557 | if (s->hit) | ||
558 | s->state = SSL3_ST_CW_CHANGE_A; | ||
559 | else | ||
560 | s->state = SSL_ST_OK; | ||
561 | s->init_num = 0; | ||
562 | break; | ||
563 | |||
564 | case SSL3_ST_CW_FLUSH: | ||
565 | s->rwstate = SSL_WRITING; | ||
566 | if (BIO_flush(s->wbio) <= 0) { | ||
567 | ret = -1; | ||
568 | goto end; | ||
569 | } | ||
570 | s->rwstate = SSL_NOTHING; | ||
571 | s->state = s->s3->tmp.next_state; | ||
572 | break; | ||
573 | |||
574 | case SSL_ST_OK: | ||
575 | /* clean a few things up */ | ||
576 | ssl3_cleanup_key_block(s); | ||
577 | |||
578 | if (s->init_buf != NULL) { | ||
579 | BUF_MEM_free(s->init_buf); | ||
580 | s->init_buf = NULL; | ||
581 | } | ||
582 | |||
583 | /* | ||
584 | * If we are not 'joining' the last two packets, | ||
585 | * remove the buffering now | ||
586 | */ | ||
587 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | ||
588 | ssl_free_wbio_buffer(s); | ||
589 | /* else do it later in ssl3_write */ | ||
590 | |||
591 | s->init_num = 0; | ||
592 | s->renegotiate = 0; | ||
593 | s->new_session = 0; | ||
594 | |||
595 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); | ||
596 | if (s->hit) | ||
597 | s->ctx->stats.sess_hit++; | ||
598 | |||
599 | ret = 1; | ||
600 | /* s->server=0; */ | ||
601 | s->handshake_func = ssl3_connect; | ||
602 | s->ctx->stats.sess_connect_good++; | ||
603 | |||
604 | if (cb != NULL) | ||
605 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
606 | |||
607 | goto end; | ||
608 | /* break; */ | ||
609 | |||
610 | default: | ||
611 | SSLerr(SSL_F_SSL3_CONNECT, | ||
612 | SSL_R_UNKNOWN_STATE); | ||
613 | ret = -1; | ||
614 | goto end; | ||
615 | /* break; */ | ||
616 | } | ||
617 | |||
618 | /* did we do anything */ | ||
619 | if (!s->s3->tmp.reuse_message && !skip) { | ||
620 | if (s->debug) { | ||
621 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
622 | goto end; | ||
623 | } | ||
624 | |||
625 | if ((cb != NULL) && (s->state != state)) { | ||
626 | new_state = s->state; | ||
627 | s->state = state; | ||
628 | cb(s, SSL_CB_CONNECT_LOOP, 1); | ||
629 | s->state = new_state; | ||
630 | } | ||
631 | } | ||
632 | skip = 0; | ||
633 | } | ||
634 | end: | ||
635 | s->in_handshake--; | ||
636 | if (buf != NULL) | ||
637 | BUF_MEM_free(buf); | ||
638 | if (cb != NULL) | ||
639 | cb(s, SSL_CB_CONNECT_EXIT, ret); | ||
640 | return (ret); | ||
641 | } | ||
642 | |||
643 | |||
644 | int | ||
645 | ssl3_client_hello(SSL *s) | ||
646 | { | ||
647 | unsigned char *bufend, *p, *d; | ||
648 | int i; | ||
649 | |||
650 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { | ||
651 | SSL_SESSION *sess = s->session; | ||
652 | |||
653 | if ((sess == NULL) || | ||
654 | (sess->ssl_version != s->version) || | ||
655 | (!sess->session_id_length && !sess->tlsext_tick) || | ||
656 | (sess->not_resumable)) { | ||
657 | if (!ssl_get_new_session(s, 0)) | ||
658 | goto err; | ||
659 | } | ||
660 | /* else use the pre-loaded session */ | ||
661 | |||
662 | arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); | ||
663 | |||
664 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO); | ||
665 | |||
666 | /* | ||
667 | * Version indicates the negotiated version: for example from | ||
668 | * an SSLv2/v3 compatible client hello). The client_version | ||
669 | * field is the maximum version we permit and it is also | ||
670 | * used in RSA encrypted premaster secrets. Some servers can | ||
671 | * choke if we initially report a higher version then | ||
672 | * renegotiate to a lower one in the premaster secret. This | ||
673 | * didn't happen with TLS 1.0 as most servers supported it | ||
674 | * but it can with TLS 1.1 or later if the server only supports | ||
675 | * 1.0. | ||
676 | * | ||
677 | * Possible scenario with previous logic: | ||
678 | * 1. Client hello indicates TLS 1.2 | ||
679 | * 2. Server hello says TLS 1.0 | ||
680 | * 3. RSA encrypted premaster secret uses 1.2. | ||
681 | * 4. Handhaked proceeds using TLS 1.0. | ||
682 | * 5. Server sends hello request to renegotiate. | ||
683 | * 6. Client hello indicates TLS v1.0 as we now | ||
684 | * know that is maximum server supports. | ||
685 | * 7. Server chokes on RSA encrypted premaster secret | ||
686 | * containing version 1.0. | ||
687 | * | ||
688 | * For interoperability it should be OK to always use the | ||
689 | * maximum version we support in client hello and then rely | ||
690 | * on the checking of version to ensure the servers isn't | ||
691 | * being inconsistent: for example initially negotiating with | ||
692 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | ||
693 | * client_version in client hello and not resetting it to | ||
694 | * the negotiated version. | ||
695 | */ | ||
696 | *(p++) = s->client_version >> 8; | ||
697 | *(p++) = s->client_version & 0xff; | ||
698 | |||
699 | /* Random stuff */ | ||
700 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); | ||
701 | p += SSL3_RANDOM_SIZE; | ||
702 | |||
703 | /* Session ID */ | ||
704 | if (s->new_session) | ||
705 | i = 0; | ||
706 | else | ||
707 | i = s->session->session_id_length; | ||
708 | *(p++) = i; | ||
709 | if (i != 0) { | ||
710 | if (i > (int)sizeof(s->session->session_id)) { | ||
711 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
712 | ERR_R_INTERNAL_ERROR); | ||
713 | goto err; | ||
714 | } | ||
715 | memcpy(p, s->session->session_id, i); | ||
716 | p += i; | ||
717 | } | ||
718 | |||
719 | /* Ciphers supported */ | ||
720 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]); | ||
721 | if (i == 0) { | ||
722 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
723 | SSL_R_NO_CIPHERS_AVAILABLE); | ||
724 | goto err; | ||
725 | } | ||
726 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH | ||
727 | /* | ||
728 | * Some servers hang if client hello > 256 bytes | ||
729 | * as hack workaround chop number of supported ciphers | ||
730 | * to keep it well below this if we use TLS v1.2 | ||
731 | */ | ||
732 | if (TLS1_get_version(s) >= TLS1_2_VERSION && | ||
733 | i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) | ||
734 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | ||
735 | #endif | ||
736 | s2n(i, p); | ||
737 | p += i; | ||
738 | |||
739 | /* add in (no) COMPRESSION */ | ||
740 | *(p++) = 1; | ||
741 | *(p++) = 0; /* Add the NULL method */ | ||
742 | |||
743 | /* TLS extensions*/ | ||
744 | if (ssl_prepare_clienthello_tlsext(s) <= 0) { | ||
745 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
746 | SSL_R_CLIENTHELLO_TLSEXT); | ||
747 | goto err; | ||
748 | } | ||
749 | bufend = (unsigned char *)s->init_buf->data + | ||
750 | SSL3_RT_MAX_PLAIN_LENGTH; | ||
751 | if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) { | ||
752 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
753 | ERR_R_INTERNAL_ERROR); | ||
754 | goto err; | ||
755 | } | ||
756 | |||
757 | s->state = SSL3_ST_CW_CLNT_HELLO_B; | ||
758 | |||
759 | ssl3_handshake_msg_finish(s, p - d); | ||
760 | } | ||
761 | |||
762 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
763 | return (ssl3_handshake_write(s)); | ||
764 | |||
765 | err: | ||
766 | return (-1); | ||
767 | } | ||
768 | |||
769 | int | ||
770 | ssl3_get_server_hello(SSL *s) | ||
771 | { | ||
772 | STACK_OF(SSL_CIPHER) *sk; | ||
773 | const SSL_CIPHER *c; | ||
774 | unsigned char *p, *q, *d; | ||
775 | int i, al, ok; | ||
776 | unsigned int j; | ||
777 | uint16_t cipher_value; | ||
778 | long n; | ||
779 | unsigned long alg_k; | ||
780 | |||
781 | n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, | ||
782 | SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, /* ?? */ &ok); | ||
783 | |||
784 | if (!ok) | ||
785 | return ((int)n); | ||
786 | |||
787 | if (SSL_IS_DTLS(s)) { | ||
788 | if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { | ||
789 | if (s->d1->send_cookie == 0) { | ||
790 | s->s3->tmp.reuse_message = 1; | ||
791 | return (1); | ||
792 | } else { | ||
793 | /* Already sent a cookie. */ | ||
794 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
795 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
796 | SSL_R_BAD_MESSAGE_TYPE); | ||
797 | goto f_err; | ||
798 | } | ||
799 | } | ||
800 | } | ||
801 | |||
802 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { | ||
803 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
804 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
805 | SSL_R_BAD_MESSAGE_TYPE); | ||
806 | goto f_err; | ||
807 | } | ||
808 | |||
809 | d = p = (unsigned char *)s->init_msg; | ||
810 | |||
811 | if (2 > n) | ||
812 | goto truncated; | ||
813 | if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { | ||
814 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); | ||
815 | s->version = (s->version&0xff00) | p[1]; | ||
816 | al = SSL_AD_PROTOCOL_VERSION; | ||
817 | goto f_err; | ||
818 | } | ||
819 | p += 2; | ||
820 | |||
821 | /* load the server hello data */ | ||
822 | |||
823 | if (p + SSL3_RANDOM_SIZE + 1 - d > n) | ||
824 | goto truncated; | ||
825 | |||
826 | /* load the server random */ | ||
827 | memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); | ||
828 | p += SSL3_RANDOM_SIZE; | ||
829 | |||
830 | /* get the session-id */ | ||
831 | j = *(p++); | ||
832 | |||
833 | if ((j > sizeof s->session->session_id) || | ||
834 | (j > SSL3_SESSION_ID_SIZE)) { | ||
835 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
836 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
837 | SSL_R_SSL3_SESSION_ID_TOO_LONG); | ||
838 | goto f_err; | ||
839 | } | ||
840 | |||
841 | if (p + j + 2 - d > n) | ||
842 | goto truncated; | ||
843 | |||
844 | /* Get the cipher value. */ | ||
845 | q = p + j; | ||
846 | n2s(q, cipher_value); | ||
847 | |||
848 | /* | ||
849 | * Check if we want to resume the session based on external | ||
850 | * pre-shared secret | ||
851 | */ | ||
852 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) { | ||
853 | SSL_CIPHER *pref_cipher = NULL; | ||
854 | s->session->master_key_length = sizeof(s->session->master_key); | ||
855 | if (s->tls_session_secret_cb(s, s->session->master_key, | ||
856 | &s->session->master_key_length, NULL, &pref_cipher, | ||
857 | s->tls_session_secret_cb_arg)) { | ||
858 | s->session->cipher = pref_cipher ? pref_cipher : | ||
859 | ssl3_get_cipher_by_value(cipher_value); | ||
860 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
861 | } | ||
862 | } | ||
863 | |||
864 | if (j != 0 && j == s->session->session_id_length && | ||
865 | timingsafe_memcmp(p, s->session->session_id, j) == 0) { | ||
866 | if (s->sid_ctx_length != s->session->sid_ctx_length || | ||
867 | timingsafe_memcmp(s->session->sid_ctx, | ||
868 | s->sid_ctx, s->sid_ctx_length) != 0) { | ||
869 | /* actually a client application bug */ | ||
870 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
871 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
872 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); | ||
873 | goto f_err; | ||
874 | } | ||
875 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
876 | s->hit = 1; | ||
877 | } else { | ||
878 | /* a miss or crap from the other end */ | ||
879 | |||
880 | /* If we were trying for session-id reuse, make a new | ||
881 | * SSL_SESSION so we don't stuff up other people */ | ||
882 | s->hit = 0; | ||
883 | if (s->session->session_id_length > 0) { | ||
884 | if (!ssl_get_new_session(s, 0)) { | ||
885 | al = SSL_AD_INTERNAL_ERROR; | ||
886 | goto f_err; | ||
887 | } | ||
888 | } | ||
889 | s->session->session_id_length = j; | ||
890 | memcpy(s->session->session_id, p, j); /* j could be 0 */ | ||
891 | } | ||
892 | p += j; | ||
893 | |||
894 | if ((c = ssl3_get_cipher_by_value(cipher_value)) == NULL) { | ||
895 | /* unknown cipher */ | ||
896 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
897 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
898 | SSL_R_UNKNOWN_CIPHER_RETURNED); | ||
899 | goto f_err; | ||
900 | } | ||
901 | |||
902 | /* TLS v1.2 only ciphersuites require v1.2 or later */ | ||
903 | if ((c->algorithm_ssl & SSL_TLSV1_2) && | ||
904 | (TLS1_get_version(s) < TLS1_2_VERSION)) { | ||
905 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
906 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
907 | SSL_R_WRONG_CIPHER_RETURNED); | ||
908 | goto f_err; | ||
909 | } | ||
910 | p += SSL3_CIPHER_VALUE_SIZE; | ||
911 | |||
912 | sk = ssl_get_ciphers_by_id(s); | ||
913 | i = sk_SSL_CIPHER_find(sk, c); | ||
914 | if (i < 0) { | ||
915 | /* we did not say we would use this cipher */ | ||
916 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
917 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
918 | SSL_R_WRONG_CIPHER_RETURNED); | ||
919 | goto f_err; | ||
920 | } | ||
921 | |||
922 | /* | ||
923 | * Depending on the session caching (internal/external), the cipher | ||
924 | * and/or cipher_id values may not be set. Make sure that | ||
925 | * cipher_id is set and use it for comparison. | ||
926 | */ | ||
927 | if (s->session->cipher) | ||
928 | s->session->cipher_id = s->session->cipher->id; | ||
929 | if (s->hit && (s->session->cipher_id != c->id)) { | ||
930 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
931 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
932 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); | ||
933 | goto f_err; | ||
934 | } | ||
935 | s->s3->tmp.new_cipher = c; | ||
936 | /* | ||
937 | * Don't digest cached records if no sigalgs: we may need them for | ||
938 | * client authentication. | ||
939 | */ | ||
940 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
941 | if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) && | ||
942 | !ssl3_digest_cached_records(s)) { | ||
943 | al = SSL_AD_INTERNAL_ERROR; | ||
944 | goto f_err; | ||
945 | } | ||
946 | /* lets get the compression algorithm */ | ||
947 | /* COMPRESSION */ | ||
948 | if (p + 1 - d > n) | ||
949 | goto truncated; | ||
950 | if (*(p++) != 0) { | ||
951 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
952 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
953 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); | ||
954 | goto f_err; | ||
955 | } | ||
956 | |||
957 | /* TLS extensions*/ | ||
958 | if (s->version >= SSL3_VERSION) { | ||
959 | if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) { | ||
960 | /* 'al' set by ssl_parse_serverhello_tlsext */ | ||
961 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
962 | SSL_R_PARSE_TLSEXT); | ||
963 | goto f_err; | ||
964 | |||
965 | } | ||
966 | if (ssl_check_serverhello_tlsext(s) <= 0) { | ||
967 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
968 | SSL_R_SERVERHELLO_TLSEXT); | ||
969 | goto err; | ||
970 | } | ||
971 | } | ||
972 | |||
973 | if (p != d + n) | ||
974 | goto truncated; | ||
975 | |||
976 | return (1); | ||
977 | |||
978 | truncated: | ||
979 | /* wrong packet length */ | ||
980 | al = SSL_AD_DECODE_ERROR; | ||
981 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); | ||
982 | f_err: | ||
983 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
984 | err: | ||
985 | return (-1); | ||
986 | } | ||
987 | |||
988 | int | ||
989 | ssl3_get_server_certificate(SSL *s) | ||
990 | { | ||
991 | int al, i, ok, ret = -1; | ||
992 | unsigned long n, nc, llen, l; | ||
993 | X509 *x = NULL; | ||
994 | const unsigned char *q, *p; | ||
995 | unsigned char *d; | ||
996 | STACK_OF(X509) *sk = NULL; | ||
997 | SESS_CERT *sc; | ||
998 | EVP_PKEY *pkey = NULL; | ||
999 | |||
1000 | n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, | ||
1001 | SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok); | ||
1002 | |||
1003 | if (!ok) | ||
1004 | return ((int)n); | ||
1005 | |||
1006 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { | ||
1007 | s->s3->tmp.reuse_message = 1; | ||
1008 | return (1); | ||
1009 | } | ||
1010 | |||
1011 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { | ||
1012 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1013 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1014 | SSL_R_BAD_MESSAGE_TYPE); | ||
1015 | goto f_err; | ||
1016 | } | ||
1017 | p = d = (unsigned char *)s->init_msg; | ||
1018 | |||
1019 | if ((sk = sk_X509_new_null()) == NULL) { | ||
1020 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1021 | ERR_R_MALLOC_FAILURE); | ||
1022 | goto err; | ||
1023 | } | ||
1024 | |||
1025 | if (p + 3 - d > n) | ||
1026 | goto truncated; | ||
1027 | n2l3(p, llen); | ||
1028 | if (llen + 3 != n) { | ||
1029 | al = SSL_AD_DECODE_ERROR; | ||
1030 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1031 | SSL_R_LENGTH_MISMATCH); | ||
1032 | goto f_err; | ||
1033 | } | ||
1034 | for (nc = 0; nc < llen; ) { | ||
1035 | if (p + 3 - d > n) | ||
1036 | goto truncated; | ||
1037 | n2l3(p, l); | ||
1038 | if ((l + nc + 3) > llen) { | ||
1039 | al = SSL_AD_DECODE_ERROR; | ||
1040 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1041 | SSL_R_CERT_LENGTH_MISMATCH); | ||
1042 | goto f_err; | ||
1043 | } | ||
1044 | |||
1045 | q = p; | ||
1046 | x = d2i_X509(NULL, &q, l); | ||
1047 | if (x == NULL) { | ||
1048 | al = SSL_AD_BAD_CERTIFICATE; | ||
1049 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1050 | ERR_R_ASN1_LIB); | ||
1051 | goto f_err; | ||
1052 | } | ||
1053 | if (q != (p + l)) { | ||
1054 | al = SSL_AD_DECODE_ERROR; | ||
1055 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1056 | SSL_R_CERT_LENGTH_MISMATCH); | ||
1057 | goto f_err; | ||
1058 | } | ||
1059 | if (!sk_X509_push(sk, x)) { | ||
1060 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1061 | ERR_R_MALLOC_FAILURE); | ||
1062 | goto err; | ||
1063 | } | ||
1064 | x = NULL; | ||
1065 | nc += l + 3; | ||
1066 | p = q; | ||
1067 | } | ||
1068 | |||
1069 | i = ssl_verify_cert_chain(s, sk); | ||
1070 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) { | ||
1071 | al = ssl_verify_alarm_type(s->verify_result); | ||
1072 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1073 | SSL_R_CERTIFICATE_VERIFY_FAILED); | ||
1074 | goto f_err; | ||
1075 | |||
1076 | } | ||
1077 | ERR_clear_error(); /* but we keep s->verify_result */ | ||
1078 | |||
1079 | sc = ssl_sess_cert_new(); | ||
1080 | if (sc == NULL) | ||
1081 | goto err; | ||
1082 | if (s->session->sess_cert) | ||
1083 | ssl_sess_cert_free(s->session->sess_cert); | ||
1084 | s->session->sess_cert = sc; | ||
1085 | |||
1086 | sc->cert_chain = sk; | ||
1087 | /* | ||
1088 | * Inconsistency alert: cert_chain does include the peer's | ||
1089 | * certificate, which we don't include in s3_srvr.c | ||
1090 | */ | ||
1091 | x = sk_X509_value(sk, 0); | ||
1092 | sk = NULL; | ||
1093 | /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ | ||
1094 | |||
1095 | pkey = X509_get_pubkey(x); | ||
1096 | |||
1097 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { | ||
1098 | x = NULL; | ||
1099 | al = SSL3_AL_FATAL; | ||
1100 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1101 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); | ||
1102 | goto f_err; | ||
1103 | } | ||
1104 | |||
1105 | i = ssl_cert_type(x, pkey); | ||
1106 | if (i < 0) { | ||
1107 | x = NULL; | ||
1108 | al = SSL3_AL_FATAL; | ||
1109 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1110 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); | ||
1111 | goto f_err; | ||
1112 | } | ||
1113 | |||
1114 | sc->peer_cert_type = i; | ||
1115 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
1116 | /* | ||
1117 | * Why would the following ever happen? | ||
1118 | * We just created sc a couple of lines ago. | ||
1119 | */ | ||
1120 | if (sc->peer_pkeys[i].x509 != NULL) | ||
1121 | X509_free(sc->peer_pkeys[i].x509); | ||
1122 | sc->peer_pkeys[i].x509 = x; | ||
1123 | sc->peer_key = &(sc->peer_pkeys[i]); | ||
1124 | |||
1125 | if (s->session->peer != NULL) | ||
1126 | X509_free(s->session->peer); | ||
1127 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
1128 | s->session->peer = x; | ||
1129 | s->session->verify_result = s->verify_result; | ||
1130 | |||
1131 | x = NULL; | ||
1132 | ret = 1; | ||
1133 | |||
1134 | if (0) { | ||
1135 | truncated: | ||
1136 | /* wrong packet length */ | ||
1137 | al = SSL_AD_DECODE_ERROR; | ||
1138 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1139 | SSL_R_BAD_PACKET_LENGTH); | ||
1140 | f_err: | ||
1141 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1142 | } | ||
1143 | err: | ||
1144 | EVP_PKEY_free(pkey); | ||
1145 | X509_free(x); | ||
1146 | sk_X509_pop_free(sk, X509_free); | ||
1147 | return (ret); | ||
1148 | } | ||
1149 | |||
1150 | int | ||
1151 | ssl3_get_key_exchange(SSL *s) | ||
1152 | { | ||
1153 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2]; | ||
1154 | EVP_MD_CTX md_ctx; | ||
1155 | unsigned char *param, *p; | ||
1156 | int al, i, j, param_len, ok; | ||
1157 | long n, alg_k, alg_a; | ||
1158 | EVP_PKEY *pkey = NULL; | ||
1159 | const EVP_MD *md = NULL; | ||
1160 | RSA *rsa = NULL; | ||
1161 | DH *dh = NULL; | ||
1162 | EC_KEY *ecdh = NULL; | ||
1163 | BN_CTX *bn_ctx = NULL; | ||
1164 | EC_POINT *srvr_ecpoint = NULL; | ||
1165 | int curve_nid = 0; | ||
1166 | int encoded_pt_len = 0; | ||
1167 | |||
1168 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
1169 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
1170 | |||
1171 | EVP_MD_CTX_init(&md_ctx); | ||
1172 | |||
1173 | /* | ||
1174 | * Use same message size as in ssl3_get_certificate_request() | ||
1175 | * as ServerKeyExchange message may be skipped. | ||
1176 | */ | ||
1177 | n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A, | ||
1178 | SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list, &ok); | ||
1179 | if (!ok) | ||
1180 | return ((int)n); | ||
1181 | |||
1182 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { | ||
1183 | /* | ||
1184 | * Do not skip server key exchange if this cipher suite uses | ||
1185 | * ephemeral keys. | ||
1186 | */ | ||
1187 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
1188 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1189 | SSL_R_UNEXPECTED_MESSAGE); | ||
1190 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1191 | goto f_err; | ||
1192 | } | ||
1193 | |||
1194 | s->s3->tmp.reuse_message = 1; | ||
1195 | return (1); | ||
1196 | } | ||
1197 | |||
1198 | if (s->session->sess_cert != NULL) { | ||
1199 | DH_free(s->session->sess_cert->peer_dh_tmp); | ||
1200 | s->session->sess_cert->peer_dh_tmp = NULL; | ||
1201 | |||
1202 | EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); | ||
1203 | s->session->sess_cert->peer_ecdh_tmp = NULL; | ||
1204 | } else { | ||
1205 | s->session->sess_cert = ssl_sess_cert_new(); | ||
1206 | if (s->session->sess_cert == NULL) | ||
1207 | goto err; | ||
1208 | } | ||
1209 | |||
1210 | param = p = (unsigned char *)s->init_msg; | ||
1211 | param_len = 0; | ||
1212 | |||
1213 | if (alg_k & SSL_kDHE) { | ||
1214 | if ((dh = DH_new()) == NULL) { | ||
1215 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1216 | ERR_R_DH_LIB); | ||
1217 | goto err; | ||
1218 | } | ||
1219 | if (2 > n) | ||
1220 | goto truncated; | ||
1221 | n2s(p, i); | ||
1222 | param_len = i + 2; | ||
1223 | if (param_len > n) { | ||
1224 | al = SSL_AD_DECODE_ERROR; | ||
1225 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1226 | SSL_R_BAD_DH_P_LENGTH); | ||
1227 | goto f_err; | ||
1228 | } | ||
1229 | if (!(dh->p = BN_bin2bn(p, i, NULL))) { | ||
1230 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1231 | ERR_R_BN_LIB); | ||
1232 | goto err; | ||
1233 | } | ||
1234 | p += i; | ||
1235 | |||
1236 | if (param_len + 2 > n) | ||
1237 | goto truncated; | ||
1238 | n2s(p, i); | ||
1239 | param_len += i + 2; | ||
1240 | if (param_len > n) { | ||
1241 | al = SSL_AD_DECODE_ERROR; | ||
1242 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1243 | SSL_R_BAD_DH_G_LENGTH); | ||
1244 | goto f_err; | ||
1245 | } | ||
1246 | if (!(dh->g = BN_bin2bn(p, i, NULL))) { | ||
1247 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1248 | ERR_R_BN_LIB); | ||
1249 | goto err; | ||
1250 | } | ||
1251 | p += i; | ||
1252 | |||
1253 | if (param_len + 2 > n) | ||
1254 | goto truncated; | ||
1255 | n2s(p, i); | ||
1256 | param_len += i + 2; | ||
1257 | if (param_len > n) { | ||
1258 | al = SSL_AD_DECODE_ERROR; | ||
1259 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1260 | SSL_R_BAD_DH_PUB_KEY_LENGTH); | ||
1261 | goto f_err; | ||
1262 | } | ||
1263 | if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { | ||
1264 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1265 | ERR_R_BN_LIB); | ||
1266 | goto err; | ||
1267 | } | ||
1268 | p += i; | ||
1269 | n -= param_len; | ||
1270 | |||
1271 | /* | ||
1272 | * Check the strength of the DH key just constructed. | ||
1273 | * Discard keys weaker than 1024 bits. | ||
1274 | */ | ||
1275 | |||
1276 | if (DH_size(dh) < 1024 / 8) { | ||
1277 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1278 | SSL_R_BAD_DH_P_LENGTH); | ||
1279 | goto err; | ||
1280 | } | ||
1281 | |||
1282 | if (alg_a & SSL_aRSA) | ||
1283 | pkey = X509_get_pubkey( | ||
1284 | s->session->sess_cert->peer_pkeys[ | ||
1285 | SSL_PKEY_RSA_ENC].x509); | ||
1286 | else if (alg_a & SSL_aDSS) | ||
1287 | pkey = X509_get_pubkey( | ||
1288 | s->session->sess_cert->peer_pkeys[ | ||
1289 | SSL_PKEY_DSA_SIGN].x509); | ||
1290 | /* else anonymous DH, so no certificate or pkey. */ | ||
1291 | |||
1292 | s->session->sess_cert->peer_dh_tmp = dh; | ||
1293 | dh = NULL; | ||
1294 | } else if (alg_k & SSL_kECDHE) { | ||
1295 | const EC_GROUP *group; | ||
1296 | EC_GROUP *ngroup; | ||
1297 | |||
1298 | if ((ecdh = EC_KEY_new()) == NULL) { | ||
1299 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1300 | ERR_R_MALLOC_FAILURE); | ||
1301 | goto err; | ||
1302 | } | ||
1303 | |||
1304 | /* | ||
1305 | * Extract elliptic curve parameters and the | ||
1306 | * server's ephemeral ECDH public key. | ||
1307 | * Keep accumulating lengths of various components in | ||
1308 | * param_len and make sure it never exceeds n. | ||
1309 | */ | ||
1310 | |||
1311 | /* | ||
1312 | * XXX: For now we only support named (not generic) curves | ||
1313 | * and the ECParameters in this case is just three bytes. | ||
1314 | */ | ||
1315 | param_len = 3; | ||
1316 | if (param_len > n) { | ||
1317 | al = SSL_AD_DECODE_ERROR; | ||
1318 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1319 | SSL_R_LENGTH_TOO_SHORT); | ||
1320 | goto f_err; | ||
1321 | } | ||
1322 | |||
1323 | /* | ||
1324 | * Check curve is one of our preferences, if not server has | ||
1325 | * sent an invalid curve. | ||
1326 | */ | ||
1327 | if (tls1_check_curve(s, p, param_len) != 1) { | ||
1328 | al = SSL_AD_DECODE_ERROR; | ||
1329 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); | ||
1330 | goto f_err; | ||
1331 | } | ||
1332 | |||
1333 | if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) { | ||
1334 | al = SSL_AD_INTERNAL_ERROR; | ||
1335 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1336 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); | ||
1337 | goto f_err; | ||
1338 | } | ||
1339 | |||
1340 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); | ||
1341 | if (ngroup == NULL) { | ||
1342 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1343 | ERR_R_EC_LIB); | ||
1344 | goto err; | ||
1345 | } | ||
1346 | if (EC_KEY_set_group(ecdh, ngroup) == 0) { | ||
1347 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1348 | ERR_R_EC_LIB); | ||
1349 | goto err; | ||
1350 | } | ||
1351 | EC_GROUP_free(ngroup); | ||
1352 | |||
1353 | group = EC_KEY_get0_group(ecdh); | ||
1354 | |||
1355 | p += 3; | ||
1356 | |||
1357 | /* Next, get the encoded ECPoint */ | ||
1358 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || | ||
1359 | ((bn_ctx = BN_CTX_new()) == NULL)) { | ||
1360 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1361 | ERR_R_MALLOC_FAILURE); | ||
1362 | goto err; | ||
1363 | } | ||
1364 | |||
1365 | if (param_len + 1 > n) | ||
1366 | goto truncated; | ||
1367 | encoded_pt_len = *p; | ||
1368 | /* length of encoded point */ | ||
1369 | p += 1; | ||
1370 | param_len += (1 + encoded_pt_len); | ||
1371 | if ((param_len > n) || (EC_POINT_oct2point(group, srvr_ecpoint, | ||
1372 | p, encoded_pt_len, bn_ctx) == 0)) { | ||
1373 | al = SSL_AD_DECODE_ERROR; | ||
1374 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1375 | SSL_R_BAD_ECPOINT); | ||
1376 | goto f_err; | ||
1377 | } | ||
1378 | |||
1379 | n -= param_len; | ||
1380 | p += encoded_pt_len; | ||
1381 | |||
1382 | /* | ||
1383 | * The ECC/TLS specification does not mention the use | ||
1384 | * of DSA to sign ECParameters in the server key | ||
1385 | * exchange message. We do support RSA and ECDSA. | ||
1386 | */ | ||
1387 | if (alg_a & SSL_aRSA) | ||
1388 | pkey = X509_get_pubkey( | ||
1389 | s->session->sess_cert->peer_pkeys[ | ||
1390 | SSL_PKEY_RSA_ENC].x509); | ||
1391 | else if (alg_a & SSL_aECDSA) | ||
1392 | pkey = X509_get_pubkey( | ||
1393 | s->session->sess_cert->peer_pkeys[ | ||
1394 | SSL_PKEY_ECC].x509); | ||
1395 | /* Else anonymous ECDH, so no certificate or pkey. */ | ||
1396 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); | ||
1397 | s->session->sess_cert->peer_ecdh_tmp = ecdh; | ||
1398 | ecdh = NULL; | ||
1399 | BN_CTX_free(bn_ctx); | ||
1400 | bn_ctx = NULL; | ||
1401 | EC_POINT_free(srvr_ecpoint); | ||
1402 | srvr_ecpoint = NULL; | ||
1403 | } else if (alg_k) { | ||
1404 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1405 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1406 | SSL_R_UNEXPECTED_MESSAGE); | ||
1407 | goto f_err; | ||
1408 | } | ||
1409 | |||
1410 | /* p points to the next byte, there are 'n' bytes left */ | ||
1411 | |||
1412 | /* if it was signed, check the signature */ | ||
1413 | if (pkey != NULL) { | ||
1414 | if (SSL_USE_SIGALGS(s)) { | ||
1415 | int sigalg = tls12_get_sigid(pkey); | ||
1416 | /* Should never happen */ | ||
1417 | if (sigalg == -1) { | ||
1418 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1419 | ERR_R_INTERNAL_ERROR); | ||
1420 | goto err; | ||
1421 | } | ||
1422 | /* | ||
1423 | * Check key type is consistent | ||
1424 | * with signature | ||
1425 | */ | ||
1426 | if (2 > n) | ||
1427 | goto truncated; | ||
1428 | if (sigalg != (int)p[1]) { | ||
1429 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1430 | SSL_R_WRONG_SIGNATURE_TYPE); | ||
1431 | al = SSL_AD_DECODE_ERROR; | ||
1432 | goto f_err; | ||
1433 | } | ||
1434 | md = tls12_get_hash(p[0]); | ||
1435 | if (md == NULL) { | ||
1436 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1437 | SSL_R_UNKNOWN_DIGEST); | ||
1438 | al = SSL_AD_DECODE_ERROR; | ||
1439 | goto f_err; | ||
1440 | } | ||
1441 | p += 2; | ||
1442 | n -= 2; | ||
1443 | } else | ||
1444 | md = EVP_sha1(); | ||
1445 | |||
1446 | if (2 > n) | ||
1447 | goto truncated; | ||
1448 | n2s(p, i); | ||
1449 | n -= 2; | ||
1450 | j = EVP_PKEY_size(pkey); | ||
1451 | |||
1452 | if (i != n || n > j) { | ||
1453 | /* wrong packet length */ | ||
1454 | al = SSL_AD_DECODE_ERROR; | ||
1455 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1456 | SSL_R_WRONG_SIGNATURE_LENGTH); | ||
1457 | goto f_err; | ||
1458 | } | ||
1459 | |||
1460 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { | ||
1461 | int num; | ||
1462 | |||
1463 | j = 0; | ||
1464 | q = md_buf; | ||
1465 | for (num = 2; num > 0; num--) { | ||
1466 | if (!EVP_DigestInit_ex(&md_ctx, | ||
1467 | (num == 2) ? s->ctx->md5 : s->ctx->sha1, | ||
1468 | NULL)) { | ||
1469 | al = SSL_AD_INTERNAL_ERROR; | ||
1470 | goto f_err; | ||
1471 | } | ||
1472 | EVP_DigestUpdate(&md_ctx, | ||
1473 | s->s3->client_random, | ||
1474 | SSL3_RANDOM_SIZE); | ||
1475 | EVP_DigestUpdate(&md_ctx, | ||
1476 | s->s3->server_random, | ||
1477 | SSL3_RANDOM_SIZE); | ||
1478 | EVP_DigestUpdate(&md_ctx, param, param_len); | ||
1479 | EVP_DigestFinal_ex(&md_ctx, q, | ||
1480 | (unsigned int *)&i); | ||
1481 | q += i; | ||
1482 | j += i; | ||
1483 | } | ||
1484 | i = RSA_verify(NID_md5_sha1, md_buf, j, | ||
1485 | p, n, pkey->pkey.rsa); | ||
1486 | if (i < 0) { | ||
1487 | al = SSL_AD_DECRYPT_ERROR; | ||
1488 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1489 | SSL_R_BAD_RSA_DECRYPT); | ||
1490 | goto f_err; | ||
1491 | } | ||
1492 | if (i == 0) { | ||
1493 | /* bad signature */ | ||
1494 | al = SSL_AD_DECRYPT_ERROR; | ||
1495 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1496 | SSL_R_BAD_SIGNATURE); | ||
1497 | goto f_err; | ||
1498 | } | ||
1499 | } else { | ||
1500 | EVP_VerifyInit_ex(&md_ctx, md, NULL); | ||
1501 | EVP_VerifyUpdate(&md_ctx, s->s3->client_random, | ||
1502 | SSL3_RANDOM_SIZE); | ||
1503 | EVP_VerifyUpdate(&md_ctx, s->s3->server_random, | ||
1504 | SSL3_RANDOM_SIZE); | ||
1505 | EVP_VerifyUpdate(&md_ctx, param, param_len); | ||
1506 | if (EVP_VerifyFinal(&md_ctx, p,(int)n, pkey) <= 0) { | ||
1507 | /* bad signature */ | ||
1508 | al = SSL_AD_DECRYPT_ERROR; | ||
1509 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1510 | SSL_R_BAD_SIGNATURE); | ||
1511 | goto f_err; | ||
1512 | } | ||
1513 | } | ||
1514 | } else { | ||
1515 | /* aNULL does not need public keys. */ | ||
1516 | if (!(alg_a & SSL_aNULL)) { | ||
1517 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1518 | ERR_R_INTERNAL_ERROR); | ||
1519 | goto err; | ||
1520 | } | ||
1521 | /* still data left over */ | ||
1522 | if (n != 0) { | ||
1523 | al = SSL_AD_DECODE_ERROR; | ||
1524 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1525 | SSL_R_EXTRA_DATA_IN_MESSAGE); | ||
1526 | goto f_err; | ||
1527 | } | ||
1528 | } | ||
1529 | EVP_PKEY_free(pkey); | ||
1530 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1531 | return (1); | ||
1532 | truncated: | ||
1533 | /* wrong packet length */ | ||
1534 | al = SSL_AD_DECODE_ERROR; | ||
1535 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1536 | f_err: | ||
1537 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1538 | err: | ||
1539 | EVP_PKEY_free(pkey); | ||
1540 | RSA_free(rsa); | ||
1541 | DH_free(dh); | ||
1542 | BN_CTX_free(bn_ctx); | ||
1543 | EC_POINT_free(srvr_ecpoint); | ||
1544 | EC_KEY_free(ecdh); | ||
1545 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1546 | return (-1); | ||
1547 | } | ||
1548 | |||
1549 | int | ||
1550 | ssl3_get_certificate_request(SSL *s) | ||
1551 | { | ||
1552 | int ok, ret = 0; | ||
1553 | unsigned long n, nc, l; | ||
1554 | unsigned int llen, ctype_num, i; | ||
1555 | X509_NAME *xn = NULL; | ||
1556 | const unsigned char *p, *q; | ||
1557 | unsigned char *d; | ||
1558 | STACK_OF(X509_NAME) *ca_sk = NULL; | ||
1559 | |||
1560 | n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A, | ||
1561 | SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list, &ok); | ||
1562 | |||
1563 | if (!ok) | ||
1564 | return ((int)n); | ||
1565 | |||
1566 | s->s3->tmp.cert_req = 0; | ||
1567 | |||
1568 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { | ||
1569 | s->s3->tmp.reuse_message = 1; | ||
1570 | /* | ||
1571 | * If we get here we don't need any cached handshake records | ||
1572 | * as we wont be doing client auth. | ||
1573 | */ | ||
1574 | if (s->s3->handshake_buffer) { | ||
1575 | if (!ssl3_digest_cached_records(s)) | ||
1576 | goto err; | ||
1577 | } | ||
1578 | return (1); | ||
1579 | } | ||
1580 | |||
1581 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { | ||
1582 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
1583 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1584 | SSL_R_WRONG_MESSAGE_TYPE); | ||
1585 | goto err; | ||
1586 | } | ||
1587 | |||
1588 | /* TLS does not like anon-DH with client cert */ | ||
1589 | if (s->version > SSL3_VERSION) { | ||
1590 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { | ||
1591 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1592 | SSL_AD_UNEXPECTED_MESSAGE); | ||
1593 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1594 | SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); | ||
1595 | goto err; | ||
1596 | } | ||
1597 | } | ||
1598 | |||
1599 | p = d = (unsigned char *)s->init_msg; | ||
1600 | |||
1601 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | ||
1602 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1603 | ERR_R_MALLOC_FAILURE); | ||
1604 | goto err; | ||
1605 | } | ||
1606 | |||
1607 | /* get the certificate types */ | ||
1608 | if (1 > n) | ||
1609 | goto truncated; | ||
1610 | ctype_num= *(p++); | ||
1611 | if (ctype_num > SSL3_CT_NUMBER) | ||
1612 | ctype_num = SSL3_CT_NUMBER; | ||
1613 | if (p + ctype_num - d > n) { | ||
1614 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1615 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1616 | goto err; | ||
1617 | } | ||
1618 | |||
1619 | for (i = 0; i < ctype_num; i++) | ||
1620 | s->s3->tmp.ctype[i] = p[i]; | ||
1621 | p += ctype_num; | ||
1622 | if (SSL_USE_SIGALGS(s)) { | ||
1623 | if (p + 2 - d > n) { | ||
1624 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1625 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1626 | goto err; | ||
1627 | } | ||
1628 | n2s(p, llen); | ||
1629 | /* Check we have enough room for signature algorithms and | ||
1630 | * following length value. | ||
1631 | */ | ||
1632 | if ((unsigned long)(p - d + llen + 2) > n) { | ||
1633 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1634 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1635 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1636 | goto err; | ||
1637 | } | ||
1638 | if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) { | ||
1639 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1640 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1641 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); | ||
1642 | goto err; | ||
1643 | } | ||
1644 | p += llen; | ||
1645 | } | ||
1646 | |||
1647 | /* get the CA RDNs */ | ||
1648 | if (p + 2 - d > n) { | ||
1649 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1650 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1651 | goto err; | ||
1652 | } | ||
1653 | n2s(p, llen); | ||
1654 | |||
1655 | if ((unsigned long)(p - d + llen) != n) { | ||
1656 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1657 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1658 | SSL_R_LENGTH_MISMATCH); | ||
1659 | goto err; | ||
1660 | } | ||
1661 | |||
1662 | for (nc = 0; nc < llen; ) { | ||
1663 | if (p + 2 - d > n) { | ||
1664 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1665 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1666 | goto err; | ||
1667 | } | ||
1668 | n2s(p, l); | ||
1669 | if ((l + nc + 2) > llen) { | ||
1670 | if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) | ||
1671 | goto cont; /* netscape bugs */ | ||
1672 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1673 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1674 | SSL_R_CA_DN_TOO_LONG); | ||
1675 | goto err; | ||
1676 | } | ||
1677 | |||
1678 | q = p; | ||
1679 | |||
1680 | if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { | ||
1681 | /* If netscape tolerance is on, ignore errors */ | ||
1682 | if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) | ||
1683 | goto cont; | ||
1684 | else { | ||
1685 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1686 | SSL_AD_DECODE_ERROR); | ||
1687 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1688 | ERR_R_ASN1_LIB); | ||
1689 | goto err; | ||
1690 | } | ||
1691 | } | ||
1692 | |||
1693 | if (q != (p + l)) { | ||
1694 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1695 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1696 | SSL_R_CA_DN_LENGTH_MISMATCH); | ||
1697 | goto err; | ||
1698 | } | ||
1699 | if (!sk_X509_NAME_push(ca_sk, xn)) { | ||
1700 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1701 | ERR_R_MALLOC_FAILURE); | ||
1702 | goto err; | ||
1703 | } | ||
1704 | |||
1705 | p += l; | ||
1706 | nc += l + 2; | ||
1707 | } | ||
1708 | |||
1709 | if (0) { | ||
1710 | cont: | ||
1711 | ERR_clear_error(); | ||
1712 | } | ||
1713 | |||
1714 | /* we should setup a certificate to return.... */ | ||
1715 | s->s3->tmp.cert_req = 1; | ||
1716 | s->s3->tmp.ctype_num = ctype_num; | ||
1717 | if (s->s3->tmp.ca_names != NULL) | ||
1718 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); | ||
1719 | s->s3->tmp.ca_names = ca_sk; | ||
1720 | ca_sk = NULL; | ||
1721 | |||
1722 | ret = 1; | ||
1723 | if (0) { | ||
1724 | truncated: | ||
1725 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1726 | SSL_R_BAD_PACKET_LENGTH); | ||
1727 | } | ||
1728 | err: | ||
1729 | if (ca_sk != NULL) | ||
1730 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); | ||
1731 | return (ret); | ||
1732 | } | ||
1733 | |||
1734 | static int | ||
1735 | ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) | ||
1736 | { | ||
1737 | return (X509_NAME_cmp(*a, *b)); | ||
1738 | } | ||
1739 | |||
1740 | int | ||
1741 | ssl3_get_new_session_ticket(SSL *s) | ||
1742 | { | ||
1743 | int ok, al, ret = 0, ticklen; | ||
1744 | long n; | ||
1745 | const unsigned char *p; | ||
1746 | unsigned char *d; | ||
1747 | |||
1748 | n = s->method->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A, | ||
1749 | SSL3_ST_CR_SESSION_TICKET_B, -1, 16384, &ok); | ||
1750 | if (!ok) | ||
1751 | return ((int)n); | ||
1752 | |||
1753 | if (s->s3->tmp.message_type == SSL3_MT_FINISHED) { | ||
1754 | s->s3->tmp.reuse_message = 1; | ||
1755 | return (1); | ||
1756 | } | ||
1757 | if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) { | ||
1758 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1759 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1760 | SSL_R_BAD_MESSAGE_TYPE); | ||
1761 | goto f_err; | ||
1762 | } | ||
1763 | if (n < 6) { | ||
1764 | /* need at least ticket_lifetime_hint + ticket length */ | ||
1765 | al = SSL_AD_DECODE_ERROR; | ||
1766 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1767 | SSL_R_LENGTH_MISMATCH); | ||
1768 | goto f_err; | ||
1769 | } | ||
1770 | |||
1771 | p = d = (unsigned char *)s->init_msg; | ||
1772 | n2l(p, s->session->tlsext_tick_lifetime_hint); | ||
1773 | n2s(p, ticklen); | ||
1774 | /* ticket_lifetime_hint + ticket_length + ticket */ | ||
1775 | if (ticklen + 6 != n) { | ||
1776 | al = SSL_AD_DECODE_ERROR; | ||
1777 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1778 | SSL_R_LENGTH_MISMATCH); | ||
1779 | goto f_err; | ||
1780 | } | ||
1781 | free(s->session->tlsext_tick); | ||
1782 | s->session->tlsext_ticklen = 0; | ||
1783 | s->session->tlsext_tick = malloc(ticklen); | ||
1784 | if (!s->session->tlsext_tick) { | ||
1785 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1786 | ERR_R_MALLOC_FAILURE); | ||
1787 | goto err; | ||
1788 | } | ||
1789 | memcpy(s->session->tlsext_tick, p, ticklen); | ||
1790 | s->session->tlsext_ticklen = ticklen; | ||
1791 | /* | ||
1792 | * There are two ways to detect a resumed ticket sesion. | ||
1793 | * One is to set an appropriate session ID and then the server | ||
1794 | * must return a match in ServerHello. This allows the normal | ||
1795 | * client session ID matching to work and we know much | ||
1796 | * earlier that the ticket has been accepted. | ||
1797 | * | ||
1798 | * The other way is to set zero length session ID when the | ||
1799 | * ticket is presented and rely on the handshake to determine | ||
1800 | * session resumption. | ||
1801 | * | ||
1802 | * We choose the former approach because this fits in with | ||
1803 | * assumptions elsewhere in OpenSSL. The session ID is set | ||
1804 | * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the | ||
1805 | * ticket. | ||
1806 | */ | ||
1807 | EVP_Digest(p, ticklen, s->session->session_id, | ||
1808 | &s->session->session_id_length, EVP_sha256(), NULL); | ||
1809 | ret = 1; | ||
1810 | return (ret); | ||
1811 | f_err: | ||
1812 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1813 | err: | ||
1814 | return (-1); | ||
1815 | } | ||
1816 | |||
1817 | int | ||
1818 | ssl3_get_cert_status(SSL *s) | ||
1819 | { | ||
1820 | int ok, al; | ||
1821 | unsigned long resplen, n; | ||
1822 | const unsigned char *p; | ||
1823 | |||
1824 | n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A, | ||
1825 | SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS, | ||
1826 | 16384, &ok); | ||
1827 | |||
1828 | if (!ok) | ||
1829 | return ((int)n); | ||
1830 | if (n < 4) { | ||
1831 | /* need at least status type + length */ | ||
1832 | al = SSL_AD_DECODE_ERROR; | ||
1833 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1834 | SSL_R_LENGTH_MISMATCH); | ||
1835 | goto f_err; | ||
1836 | } | ||
1837 | p = (unsigned char *)s->init_msg; | ||
1838 | if (*p++ != TLSEXT_STATUSTYPE_ocsp) { | ||
1839 | al = SSL_AD_DECODE_ERROR; | ||
1840 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1841 | SSL_R_UNSUPPORTED_STATUS_TYPE); | ||
1842 | goto f_err; | ||
1843 | } | ||
1844 | n2l3(p, resplen); | ||
1845 | if (resplen + 4 != n) { | ||
1846 | al = SSL_AD_DECODE_ERROR; | ||
1847 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1848 | SSL_R_LENGTH_MISMATCH); | ||
1849 | goto f_err; | ||
1850 | } | ||
1851 | free(s->tlsext_ocsp_resp); | ||
1852 | s->tlsext_ocsp_resp = BUF_memdup(p, resplen); | ||
1853 | if (!s->tlsext_ocsp_resp) { | ||
1854 | al = SSL_AD_INTERNAL_ERROR; | ||
1855 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1856 | ERR_R_MALLOC_FAILURE); | ||
1857 | goto f_err; | ||
1858 | } | ||
1859 | s->tlsext_ocsp_resplen = resplen; | ||
1860 | if (s->ctx->tlsext_status_cb) { | ||
1861 | int ret; | ||
1862 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | ||
1863 | if (ret == 0) { | ||
1864 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | ||
1865 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1866 | SSL_R_INVALID_STATUS_RESPONSE); | ||
1867 | goto f_err; | ||
1868 | } | ||
1869 | if (ret < 0) { | ||
1870 | al = SSL_AD_INTERNAL_ERROR; | ||
1871 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1872 | ERR_R_MALLOC_FAILURE); | ||
1873 | goto f_err; | ||
1874 | } | ||
1875 | } | ||
1876 | return (1); | ||
1877 | f_err: | ||
1878 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1879 | return (-1); | ||
1880 | } | ||
1881 | |||
1882 | int | ||
1883 | ssl3_get_server_done(SSL *s) | ||
1884 | { | ||
1885 | int ok, ret = 0; | ||
1886 | long n; | ||
1887 | |||
1888 | n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A, | ||
1889 | SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE, | ||
1890 | 30, /* should be very small, like 0 :-) */ &ok); | ||
1891 | |||
1892 | if (!ok) | ||
1893 | return ((int)n); | ||
1894 | if (n > 0) { | ||
1895 | /* should contain no data */ | ||
1896 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1897 | SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); | ||
1898 | return (-1); | ||
1899 | } | ||
1900 | ret = 1; | ||
1901 | return (ret); | ||
1902 | } | ||
1903 | |||
1904 | int | ||
1905 | ssl3_send_client_key_exchange(SSL *s) | ||
1906 | { | ||
1907 | unsigned char *p, *q; | ||
1908 | int n; | ||
1909 | unsigned long alg_k; | ||
1910 | EVP_PKEY *pkey = NULL; | ||
1911 | EC_KEY *clnt_ecdh = NULL; | ||
1912 | const EC_POINT *srvr_ecpoint = NULL; | ||
1913 | EVP_PKEY *srvr_pub_pkey = NULL; | ||
1914 | unsigned char *encodedPoint = NULL; | ||
1915 | int encoded_pt_len = 0; | ||
1916 | BN_CTX *bn_ctx = NULL; | ||
1917 | |||
1918 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) { | ||
1919 | p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_KEY_EXCHANGE); | ||
1920 | |||
1921 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
1922 | |||
1923 | if (s->session->sess_cert == NULL) { | ||
1924 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1925 | SSL_AD_UNEXPECTED_MESSAGE); | ||
1926 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1927 | ERR_R_INTERNAL_ERROR); | ||
1928 | goto err; | ||
1929 | } | ||
1930 | |||
1931 | if (alg_k & SSL_kRSA) { | ||
1932 | RSA *rsa; | ||
1933 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | ||
1934 | |||
1935 | pkey = X509_get_pubkey( | ||
1936 | s->session->sess_cert->peer_pkeys[ | ||
1937 | SSL_PKEY_RSA_ENC].x509); | ||
1938 | if ((pkey == NULL) || | ||
1939 | (pkey->type != EVP_PKEY_RSA) || | ||
1940 | (pkey->pkey.rsa == NULL)) { | ||
1941 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1942 | ERR_R_INTERNAL_ERROR); | ||
1943 | EVP_PKEY_free(pkey); | ||
1944 | goto err; | ||
1945 | } | ||
1946 | rsa = pkey->pkey.rsa; | ||
1947 | EVP_PKEY_free(pkey); | ||
1948 | |||
1949 | tmp_buf[0] = s->client_version >> 8; | ||
1950 | tmp_buf[1] = s->client_version & 0xff; | ||
1951 | arc4random_buf(&tmp_buf[2], sizeof(tmp_buf) - 2); | ||
1952 | |||
1953 | s->session->master_key_length = sizeof tmp_buf; | ||
1954 | |||
1955 | q = p; | ||
1956 | /* Fix buf for TLS and beyond */ | ||
1957 | if (s->version > SSL3_VERSION) | ||
1958 | p += 2; | ||
1959 | n = RSA_public_encrypt(sizeof tmp_buf, | ||
1960 | tmp_buf, p, rsa, RSA_PKCS1_PADDING); | ||
1961 | if (n <= 0) { | ||
1962 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1963 | SSL_R_BAD_RSA_ENCRYPT); | ||
1964 | goto err; | ||
1965 | } | ||
1966 | |||
1967 | /* Fix buf for TLS and beyond */ | ||
1968 | if (s->version > SSL3_VERSION) { | ||
1969 | s2n(n, q); | ||
1970 | n += 2; | ||
1971 | } | ||
1972 | |||
1973 | s->session->master_key_length = | ||
1974 | s->method->ssl3_enc->generate_master_secret( | ||
1975 | s, s->session->master_key, tmp_buf, sizeof tmp_buf); | ||
1976 | OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); | ||
1977 | } else if (alg_k & SSL_kDHE) { | ||
1978 | DH *dh_srvr, *dh_clnt; | ||
1979 | |||
1980 | /* Ensure that we have an ephemeral key for DHE. */ | ||
1981 | if (s->session->sess_cert->peer_dh_tmp == NULL) { | ||
1982 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1983 | SSL_AD_HANDSHAKE_FAILURE); | ||
1984 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1985 | SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); | ||
1986 | goto err; | ||
1987 | } | ||
1988 | dh_srvr = s->session->sess_cert->peer_dh_tmp; | ||
1989 | |||
1990 | /* Generate a new random key. */ | ||
1991 | if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { | ||
1992 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1993 | ERR_R_DH_LIB); | ||
1994 | goto err; | ||
1995 | } | ||
1996 | if (!DH_generate_key(dh_clnt)) { | ||
1997 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1998 | ERR_R_DH_LIB); | ||
1999 | DH_free(dh_clnt); | ||
2000 | goto err; | ||
2001 | } | ||
2002 | |||
2003 | /* | ||
2004 | * Use the 'p' output buffer for the DH key, but | ||
2005 | * make sure to clear it out afterwards. | ||
2006 | */ | ||
2007 | n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt); | ||
2008 | |||
2009 | if (n <= 0) { | ||
2010 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2011 | ERR_R_DH_LIB); | ||
2012 | DH_free(dh_clnt); | ||
2013 | goto err; | ||
2014 | } | ||
2015 | |||
2016 | /* Generate master key from the result. */ | ||
2017 | s->session->master_key_length = | ||
2018 | s->method->ssl3_enc->generate_master_secret(s, | ||
2019 | s->session->master_key, p, n); | ||
2020 | |||
2021 | /* Clean up. */ | ||
2022 | memset(p, 0, n); | ||
2023 | |||
2024 | /* Send off the data. */ | ||
2025 | n = BN_num_bytes(dh_clnt->pub_key); | ||
2026 | s2n(n, p); | ||
2027 | BN_bn2bin(dh_clnt->pub_key, p); | ||
2028 | n += 2; | ||
2029 | |||
2030 | DH_free(dh_clnt); | ||
2031 | |||
2032 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | ||
2033 | } else if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) { | ||
2034 | const EC_GROUP *srvr_group = NULL; | ||
2035 | EC_KEY *tkey; | ||
2036 | int ecdh_clnt_cert = 0; | ||
2037 | int field_size = 0; | ||
2038 | |||
2039 | /* | ||
2040 | * Did we send out the client's ECDH share for use | ||
2041 | * in premaster computation as part of client | ||
2042 | * certificate? If so, set ecdh_clnt_cert to 1. | ||
2043 | */ | ||
2044 | if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && | ||
2045 | (s->cert != NULL)) { | ||
2046 | /* | ||
2047 | * XXX: For now, we do not support client | ||
2048 | * authentication using ECDH certificates. | ||
2049 | * To add such support, one needs to add | ||
2050 | * code that checks for appropriate | ||
2051 | * conditions and sets ecdh_clnt_cert to 1. | ||
2052 | * For example, the cert have an ECC | ||
2053 | * key on the same curve as the server's | ||
2054 | * and the key should be authorized for | ||
2055 | * key agreement. | ||
2056 | * | ||
2057 | * One also needs to add code in ssl3_connect | ||
2058 | * to skip sending the certificate verify | ||
2059 | * message. | ||
2060 | * | ||
2061 | * if ((s->cert->key->privatekey != NULL) && | ||
2062 | * (s->cert->key->privatekey->type == | ||
2063 | * EVP_PKEY_EC) && ...) | ||
2064 | * ecdh_clnt_cert = 1; | ||
2065 | */ | ||
2066 | } | ||
2067 | |||
2068 | /* Ensure that we have an ephemeral key for ECDHE. */ | ||
2069 | if ((alg_k & SSL_kECDHE) && | ||
2070 | s->session->sess_cert->peer_ecdh_tmp == NULL) { | ||
2071 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
2072 | SSL_AD_HANDSHAKE_FAILURE); | ||
2073 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2074 | ERR_R_INTERNAL_ERROR); | ||
2075 | goto err; | ||
2076 | } | ||
2077 | tkey = s->session->sess_cert->peer_ecdh_tmp; | ||
2078 | |||
2079 | if (alg_k & (SSL_kECDHr|SSL_kECDHe)) { | ||
2080 | /* Get the Server Public Key from Cert */ | ||
2081 | srvr_pub_pkey = X509_get_pubkey(s->session-> \ | ||
2082 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); | ||
2083 | if (srvr_pub_pkey != NULL && | ||
2084 | srvr_pub_pkey->type == EVP_PKEY_EC) | ||
2085 | tkey = srvr_pub_pkey->pkey.ec; | ||
2086 | } | ||
2087 | |||
2088 | if (tkey == NULL) { | ||
2089 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2090 | ERR_R_INTERNAL_ERROR); | ||
2091 | goto err; | ||
2092 | } | ||
2093 | |||
2094 | srvr_group = EC_KEY_get0_group(tkey); | ||
2095 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); | ||
2096 | |||
2097 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { | ||
2098 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2099 | ERR_R_INTERNAL_ERROR); | ||
2100 | goto err; | ||
2101 | } | ||
2102 | |||
2103 | if ((clnt_ecdh = EC_KEY_new()) == NULL) { | ||
2104 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2105 | ERR_R_MALLOC_FAILURE); | ||
2106 | goto err; | ||
2107 | } | ||
2108 | |||
2109 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { | ||
2110 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2111 | ERR_R_EC_LIB); | ||
2112 | goto err; | ||
2113 | } | ||
2114 | if (ecdh_clnt_cert) { | ||
2115 | /* | ||
2116 | * Reuse key info from our certificate | ||
2117 | * We only need our private key to perform | ||
2118 | * the ECDH computation. | ||
2119 | */ | ||
2120 | const BIGNUM *priv_key; | ||
2121 | tkey = s->cert->key->privatekey->pkey.ec; | ||
2122 | priv_key = EC_KEY_get0_private_key(tkey); | ||
2123 | if (priv_key == NULL) { | ||
2124 | SSLerr( | ||
2125 | SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2126 | ERR_R_MALLOC_FAILURE); | ||
2127 | goto err; | ||
2128 | } | ||
2129 | if (!EC_KEY_set_private_key(clnt_ecdh, | ||
2130 | priv_key)) { | ||
2131 | SSLerr( | ||
2132 | SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2133 | ERR_R_EC_LIB); | ||
2134 | goto err; | ||
2135 | } | ||
2136 | } else { | ||
2137 | /* Generate a new ECDH key pair */ | ||
2138 | if (!(EC_KEY_generate_key(clnt_ecdh))) { | ||
2139 | SSLerr( | ||
2140 | SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2141 | ERR_R_ECDH_LIB); | ||
2142 | goto err; | ||
2143 | } | ||
2144 | } | ||
2145 | |||
2146 | /* | ||
2147 | * Use the 'p' output buffer for the ECDH key, but | ||
2148 | * make sure to clear it out afterwards. | ||
2149 | */ | ||
2150 | field_size = EC_GROUP_get_degree(srvr_group); | ||
2151 | if (field_size <= 0) { | ||
2152 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2153 | ERR_R_ECDH_LIB); | ||
2154 | goto err; | ||
2155 | } | ||
2156 | n = ECDH_compute_key(p, (field_size + 7)/8, | ||
2157 | srvr_ecpoint, clnt_ecdh, NULL); | ||
2158 | if (n <= 0) { | ||
2159 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2160 | ERR_R_ECDH_LIB); | ||
2161 | goto err; | ||
2162 | } | ||
2163 | |||
2164 | /* generate master key from the result */ | ||
2165 | s->session->master_key_length = s->method->ssl3_enc \ | ||
2166 | -> generate_master_secret(s, | ||
2167 | s->session->master_key, p, n); | ||
2168 | |||
2169 | memset(p, 0, n); /* clean up */ | ||
2170 | |||
2171 | if (ecdh_clnt_cert) { | ||
2172 | /* Send empty client key exch message. */ | ||
2173 | n = 0; | ||
2174 | } else { | ||
2175 | /* | ||
2176 | * First check the size of encoding and | ||
2177 | * allocate memory accordingly. | ||
2178 | */ | ||
2179 | encoded_pt_len = EC_POINT_point2oct( | ||
2180 | srvr_group, | ||
2181 | EC_KEY_get0_public_key(clnt_ecdh), | ||
2182 | POINT_CONVERSION_UNCOMPRESSED, | ||
2183 | NULL, 0, NULL); | ||
2184 | |||
2185 | encodedPoint = malloc(encoded_pt_len); | ||
2186 | |||
2187 | bn_ctx = BN_CTX_new(); | ||
2188 | if ((encodedPoint == NULL) || | ||
2189 | (bn_ctx == NULL)) { | ||
2190 | SSLerr( | ||
2191 | SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2192 | ERR_R_MALLOC_FAILURE); | ||
2193 | goto err; | ||
2194 | } | ||
2195 | |||
2196 | /* Encode the public key */ | ||
2197 | n = EC_POINT_point2oct(srvr_group, | ||
2198 | EC_KEY_get0_public_key(clnt_ecdh), | ||
2199 | POINT_CONVERSION_UNCOMPRESSED, | ||
2200 | encodedPoint, encoded_pt_len, bn_ctx); | ||
2201 | |||
2202 | *p = n; /* length of encoded point */ | ||
2203 | /* Encoded point will be copied here */ | ||
2204 | p += 1; | ||
2205 | |||
2206 | /* copy the point */ | ||
2207 | memcpy((unsigned char *)p, encodedPoint, n); | ||
2208 | /* increment n to account for length field */ | ||
2209 | n += 1; | ||
2210 | |||
2211 | } | ||
2212 | |||
2213 | /* Free allocated memory */ | ||
2214 | BN_CTX_free(bn_ctx); | ||
2215 | free(encodedPoint); | ||
2216 | EC_KEY_free(clnt_ecdh); | ||
2217 | EVP_PKEY_free(srvr_pub_pkey); | ||
2218 | } else if (alg_k & SSL_kGOST) { | ||
2219 | /* GOST key exchange message creation */ | ||
2220 | EVP_PKEY_CTX *pkey_ctx; | ||
2221 | X509 *peer_cert; | ||
2222 | |||
2223 | size_t msglen; | ||
2224 | unsigned int md_len; | ||
2225 | unsigned char premaster_secret[32], shared_ukm[32], | ||
2226 | tmp[256]; | ||
2227 | EVP_MD_CTX *ukm_hash; | ||
2228 | EVP_PKEY *pub_key; | ||
2229 | int nid; | ||
2230 | |||
2231 | /* Get server sertificate PKEY and create ctx from it */ | ||
2232 | peer_cert = s->session->sess_cert->peer_pkeys[SSL_PKEY_GOST01].x509; | ||
2233 | if (!peer_cert) { | ||
2234 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2235 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | ||
2236 | goto err; | ||
2237 | } | ||
2238 | |||
2239 | pkey_ctx = EVP_PKEY_CTX_new( | ||
2240 | pub_key = X509_get_pubkey(peer_cert), | ||
2241 | NULL); | ||
2242 | /* | ||
2243 | * If we have send a certificate, and certificate key | ||
2244 | * parameters match those of server certificate, use | ||
2245 | * certificate key for key exchange. | ||
2246 | * Otherwise, generate ephemeral key pair. | ||
2247 | */ | ||
2248 | |||
2249 | EVP_PKEY_encrypt_init(pkey_ctx); | ||
2250 | /* Generate session key. */ | ||
2251 | arc4random_buf(premaster_secret, 32); | ||
2252 | /* | ||
2253 | * If we have client certificate, use its secret | ||
2254 | * as peer key. | ||
2255 | */ | ||
2256 | if (s->s3->tmp.cert_req && s->cert->key->privatekey) { | ||
2257 | if (EVP_PKEY_derive_set_peer(pkey_ctx, | ||
2258 | s->cert->key->privatekey) <=0) { | ||
2259 | /* | ||
2260 | * If there was an error - | ||
2261 | * just ignore it. Ephemeral key | ||
2262 | * would be used | ||
2263 | */ | ||
2264 | ERR_clear_error(); | ||
2265 | } | ||
2266 | } | ||
2267 | /* | ||
2268 | * Compute shared IV and store it in algorithm-specific | ||
2269 | * context data | ||
2270 | */ | ||
2271 | ukm_hash = EVP_MD_CTX_create(); | ||
2272 | if (ukm_hash == NULL) { | ||
2273 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2274 | ERR_R_MALLOC_FAILURE); | ||
2275 | goto err; | ||
2276 | } | ||
2277 | |||
2278 | if (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_GOST94) | ||
2279 | nid = NID_id_GostR3411_94; | ||
2280 | else | ||
2281 | nid = NID_id_tc26_gost3411_2012_256; | ||
2282 | if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid))) | ||
2283 | goto err; | ||
2284 | EVP_DigestUpdate(ukm_hash, | ||
2285 | s->s3->client_random, SSL3_RANDOM_SIZE); | ||
2286 | EVP_DigestUpdate(ukm_hash, | ||
2287 | s->s3->server_random, SSL3_RANDOM_SIZE); | ||
2288 | EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); | ||
2289 | EVP_MD_CTX_destroy(ukm_hash); | ||
2290 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | ||
2291 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { | ||
2292 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2293 | SSL_R_LIBRARY_BUG); | ||
2294 | goto err; | ||
2295 | } | ||
2296 | /* | ||
2297 | * Make GOST keytransport blob message, | ||
2298 | * encapsulate it into sequence. | ||
2299 | */ | ||
2300 | *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; | ||
2301 | msglen = 255; | ||
2302 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, | ||
2303 | premaster_secret, 32) < 0) { | ||
2304 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2305 | SSL_R_LIBRARY_BUG); | ||
2306 | goto err; | ||
2307 | } | ||
2308 | if (msglen >= 0x80) { | ||
2309 | *(p++) = 0x81; | ||
2310 | *(p++) = msglen & 0xff; | ||
2311 | n = msglen + 3; | ||
2312 | } else { | ||
2313 | *(p++) = msglen & 0xff; | ||
2314 | n = msglen + 2; | ||
2315 | } | ||
2316 | memcpy(p, tmp, msglen); | ||
2317 | /* Check if pubkey from client certificate was used. */ | ||
2318 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, | ||
2319 | EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { | ||
2320 | /* Set flag "skip certificate verify". */ | ||
2321 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | ||
2322 | } | ||
2323 | EVP_PKEY_CTX_free(pkey_ctx); | ||
2324 | s->session->master_key_length = | ||
2325 | s->method->ssl3_enc->generate_master_secret(s, | ||
2326 | s->session->master_key, premaster_secret, 32); | ||
2327 | EVP_PKEY_free(pub_key); | ||
2328 | |||
2329 | } else { | ||
2330 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
2331 | SSL_AD_HANDSHAKE_FAILURE); | ||
2332 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2333 | ERR_R_INTERNAL_ERROR); | ||
2334 | goto err; | ||
2335 | } | ||
2336 | |||
2337 | s->state = SSL3_ST_CW_KEY_EXCH_B; | ||
2338 | |||
2339 | ssl3_handshake_msg_finish(s, n); | ||
2340 | } | ||
2341 | |||
2342 | /* SSL3_ST_CW_KEY_EXCH_B */ | ||
2343 | return (ssl3_handshake_write(s)); | ||
2344 | |||
2345 | err: | ||
2346 | BN_CTX_free(bn_ctx); | ||
2347 | free(encodedPoint); | ||
2348 | EC_KEY_free(clnt_ecdh); | ||
2349 | EVP_PKEY_free(srvr_pub_pkey); | ||
2350 | return (-1); | ||
2351 | } | ||
2352 | |||
2353 | int | ||
2354 | ssl3_send_client_verify(SSL *s) | ||
2355 | { | ||
2356 | unsigned char *p; | ||
2357 | unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | ||
2358 | EVP_PKEY *pkey; | ||
2359 | EVP_PKEY_CTX *pctx = NULL; | ||
2360 | EVP_MD_CTX mctx; | ||
2361 | unsigned u = 0; | ||
2362 | unsigned long n; | ||
2363 | int j; | ||
2364 | |||
2365 | EVP_MD_CTX_init(&mctx); | ||
2366 | |||
2367 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) { | ||
2368 | p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_VERIFY); | ||
2369 | |||
2370 | /* | ||
2371 | * Create context from key and test if sha1 is allowed as | ||
2372 | * digest. | ||
2373 | */ | ||
2374 | pkey = s->cert->key->privatekey; | ||
2375 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
2376 | EVP_PKEY_sign_init(pctx); | ||
2377 | if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { | ||
2378 | if (!SSL_USE_SIGALGS(s)) | ||
2379 | s->method->ssl3_enc->cert_verify_mac(s, | ||
2380 | NID_sha1, &(data[MD5_DIGEST_LENGTH])); | ||
2381 | } else { | ||
2382 | ERR_clear_error(); | ||
2383 | } | ||
2384 | /* | ||
2385 | * For TLS v1.2 send signature algorithm and signature | ||
2386 | * using agreed digest and cached handshake records. | ||
2387 | */ | ||
2388 | if (SSL_USE_SIGALGS(s)) { | ||
2389 | long hdatalen = 0; | ||
2390 | void *hdata; | ||
2391 | const EVP_MD *md = s->cert->key->digest; | ||
2392 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, | ||
2393 | &hdata); | ||
2394 | if (hdatalen <= 0 || | ||
2395 | !tls12_get_sigandhash(p, pkey, md)) { | ||
2396 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2397 | ERR_R_INTERNAL_ERROR); | ||
2398 | goto err; | ||
2399 | } | ||
2400 | p += 2; | ||
2401 | if (!EVP_SignInit_ex(&mctx, md, NULL) || | ||
2402 | !EVP_SignUpdate(&mctx, hdata, hdatalen) || | ||
2403 | !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { | ||
2404 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2405 | ERR_R_EVP_LIB); | ||
2406 | goto err; | ||
2407 | } | ||
2408 | s2n(u, p); | ||
2409 | n = u + 4; | ||
2410 | if (!ssl3_digest_cached_records(s)) | ||
2411 | goto err; | ||
2412 | } else if (pkey->type == EVP_PKEY_RSA) { | ||
2413 | s->method->ssl3_enc->cert_verify_mac( | ||
2414 | s, NID_md5, &(data[0])); | ||
2415 | if (RSA_sign(NID_md5_sha1, data, | ||
2416 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]), | ||
2417 | &u, pkey->pkey.rsa) <= 0 ) { | ||
2418 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2419 | ERR_R_RSA_LIB); | ||
2420 | goto err; | ||
2421 | } | ||
2422 | s2n(u, p); | ||
2423 | n = u + 2; | ||
2424 | } else if (pkey->type == EVP_PKEY_DSA) { | ||
2425 | if (!DSA_sign(pkey->save_type, | ||
2426 | &(data[MD5_DIGEST_LENGTH]), | ||
2427 | SHA_DIGEST_LENGTH, &(p[2]), | ||
2428 | (unsigned int *)&j, pkey->pkey.dsa)) { | ||
2429 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2430 | ERR_R_DSA_LIB); | ||
2431 | goto err; | ||
2432 | } | ||
2433 | s2n(j, p); | ||
2434 | n = j + 2; | ||
2435 | } else if (pkey->type == EVP_PKEY_EC) { | ||
2436 | if (!ECDSA_sign(pkey->save_type, | ||
2437 | &(data[MD5_DIGEST_LENGTH]), | ||
2438 | SHA_DIGEST_LENGTH, &(p[2]), | ||
2439 | (unsigned int *)&j, pkey->pkey.ec)) { | ||
2440 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2441 | ERR_R_ECDSA_LIB); | ||
2442 | goto err; | ||
2443 | } | ||
2444 | s2n(j, p); | ||
2445 | n = j + 2; | ||
2446 | #ifndef OPENSSL_NO_GOST | ||
2447 | } else if (pkey->type == NID_id_GostR3410_94 || | ||
2448 | pkey->type == NID_id_GostR3410_2001) { | ||
2449 | unsigned char signbuf[128]; | ||
2450 | long hdatalen = 0; | ||
2451 | void *hdata; | ||
2452 | const EVP_MD *md; | ||
2453 | int nid; | ||
2454 | size_t sigsize; | ||
2455 | |||
2456 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | ||
2457 | if (hdatalen <= 0) { | ||
2458 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2459 | ERR_R_INTERNAL_ERROR); | ||
2460 | goto err; | ||
2461 | } | ||
2462 | if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) || | ||
2463 | !(md = EVP_get_digestbynid(nid))) { | ||
2464 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2465 | ERR_R_EVP_LIB); | ||
2466 | goto err; | ||
2467 | } | ||
2468 | if (!EVP_DigestInit_ex(&mctx, md, NULL) || | ||
2469 | !EVP_DigestUpdate(&mctx, hdata, hdatalen) || | ||
2470 | !EVP_DigestFinal(&mctx, signbuf, &u) || | ||
2471 | (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) || | ||
2472 | (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
2473 | EVP_PKEY_CTRL_GOST_SIG_FORMAT, | ||
2474 | GOST_SIG_FORMAT_RS_LE, | ||
2475 | NULL) <= 0) || | ||
2476 | (EVP_PKEY_sign(pctx, &(p[2]), &sigsize, | ||
2477 | signbuf, u) <= 0)) { | ||
2478 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2479 | ERR_R_EVP_LIB); | ||
2480 | goto err; | ||
2481 | } | ||
2482 | if (!ssl3_digest_cached_records(s)) | ||
2483 | goto err; | ||
2484 | j = sigsize; | ||
2485 | s2n(j, p); | ||
2486 | n = j + 2; | ||
2487 | #endif | ||
2488 | } else { | ||
2489 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2490 | ERR_R_INTERNAL_ERROR); | ||
2491 | goto err; | ||
2492 | } | ||
2493 | |||
2494 | s->state = SSL3_ST_CW_CERT_VRFY_B; | ||
2495 | |||
2496 | ssl3_handshake_msg_finish(s, n); | ||
2497 | } | ||
2498 | |||
2499 | EVP_MD_CTX_cleanup(&mctx); | ||
2500 | EVP_PKEY_CTX_free(pctx); | ||
2501 | |||
2502 | return (ssl3_handshake_write(s)); | ||
2503 | |||
2504 | err: | ||
2505 | EVP_MD_CTX_cleanup(&mctx); | ||
2506 | EVP_PKEY_CTX_free(pctx); | ||
2507 | return (-1); | ||
2508 | } | ||
2509 | |||
2510 | int | ||
2511 | ssl3_send_client_certificate(SSL *s) | ||
2512 | { | ||
2513 | X509 *x509 = NULL; | ||
2514 | EVP_PKEY *pkey = NULL; | ||
2515 | int i; | ||
2516 | unsigned long l; | ||
2517 | |||
2518 | if (s->state == SSL3_ST_CW_CERT_A) { | ||
2519 | if ((s->cert == NULL) || (s->cert->key->x509 == NULL) || | ||
2520 | (s->cert->key->privatekey == NULL)) | ||
2521 | s->state = SSL3_ST_CW_CERT_B; | ||
2522 | else | ||
2523 | s->state = SSL3_ST_CW_CERT_C; | ||
2524 | } | ||
2525 | |||
2526 | /* We need to get a client cert */ | ||
2527 | if (s->state == SSL3_ST_CW_CERT_B) { | ||
2528 | /* | ||
2529 | * If we get an error, we need to | ||
2530 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); | ||
2531 | * We then get retied later | ||
2532 | */ | ||
2533 | i = ssl_do_client_cert_cb(s, &x509, &pkey); | ||
2534 | if (i < 0) { | ||
2535 | s->rwstate = SSL_X509_LOOKUP; | ||
2536 | return (-1); | ||
2537 | } | ||
2538 | s->rwstate = SSL_NOTHING; | ||
2539 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | ||
2540 | s->state = SSL3_ST_CW_CERT_B; | ||
2541 | if (!SSL_use_certificate(s, x509) || | ||
2542 | !SSL_use_PrivateKey(s, pkey)) | ||
2543 | i = 0; | ||
2544 | } else if (i == 1) { | ||
2545 | i = 0; | ||
2546 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, | ||
2547 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); | ||
2548 | } | ||
2549 | |||
2550 | if (x509 != NULL) | ||
2551 | X509_free(x509); | ||
2552 | EVP_PKEY_free(pkey); | ||
2553 | if (i == 0) { | ||
2554 | if (s->version == SSL3_VERSION) { | ||
2555 | s->s3->tmp.cert_req = 0; | ||
2556 | ssl3_send_alert(s, SSL3_AL_WARNING, | ||
2557 | SSL_AD_NO_CERTIFICATE); | ||
2558 | return (1); | ||
2559 | } else { | ||
2560 | s->s3->tmp.cert_req = 2; | ||
2561 | } | ||
2562 | } | ||
2563 | |||
2564 | /* Ok, we have a cert */ | ||
2565 | s->state = SSL3_ST_CW_CERT_C; | ||
2566 | } | ||
2567 | |||
2568 | if (s->state == SSL3_ST_CW_CERT_C) { | ||
2569 | s->state = SSL3_ST_CW_CERT_D; | ||
2570 | l = ssl3_output_cert_chain(s, | ||
2571 | (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key->x509); | ||
2572 | s->init_num = (int)l; | ||
2573 | s->init_off = 0; | ||
2574 | } | ||
2575 | /* SSL3_ST_CW_CERT_D */ | ||
2576 | return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); | ||
2577 | } | ||
2578 | |||
2579 | #define has_bits(i,m) (((i)&(m)) == (m)) | ||
2580 | |||
2581 | int | ||
2582 | ssl3_check_cert_and_algorithm(SSL *s) | ||
2583 | { | ||
2584 | int i, idx; | ||
2585 | long alg_k, alg_a; | ||
2586 | EVP_PKEY *pkey = NULL; | ||
2587 | SESS_CERT *sc; | ||
2588 | DH *dh; | ||
2589 | |||
2590 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
2591 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
2592 | |||
2593 | /* We don't have a certificate. */ | ||
2594 | if (alg_a & SSL_aNULL) | ||
2595 | return (1); | ||
2596 | |||
2597 | sc = s->session->sess_cert; | ||
2598 | if (sc == NULL) { | ||
2599 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2600 | ERR_R_INTERNAL_ERROR); | ||
2601 | goto err; | ||
2602 | } | ||
2603 | dh = s->session->sess_cert->peer_dh_tmp; | ||
2604 | |||
2605 | /* This is the passed certificate. */ | ||
2606 | |||
2607 | idx = sc->peer_cert_type; | ||
2608 | if (idx == SSL_PKEY_ECC) { | ||
2609 | if (ssl_check_srvr_ecc_cert_and_alg( | ||
2610 | sc->peer_pkeys[idx].x509, s) == 0) { | ||
2611 | /* check failed */ | ||
2612 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2613 | SSL_R_BAD_ECC_CERT); | ||
2614 | goto f_err; | ||
2615 | } else { | ||
2616 | return (1); | ||
2617 | } | ||
2618 | } | ||
2619 | pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); | ||
2620 | i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); | ||
2621 | EVP_PKEY_free(pkey); | ||
2622 | |||
2623 | /* Check that we have a certificate if we require one. */ | ||
2624 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) { | ||
2625 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2626 | SSL_R_MISSING_RSA_SIGNING_CERT); | ||
2627 | goto f_err; | ||
2628 | } else if ((alg_a & SSL_aDSS) && | ||
2629 | !has_bits(i, EVP_PK_DSA|EVP_PKT_SIGN)) { | ||
2630 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2631 | SSL_R_MISSING_DSA_SIGNING_CERT); | ||
2632 | goto f_err; | ||
2633 | } | ||
2634 | if ((alg_k & SSL_kRSA) && | ||
2635 | !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) { | ||
2636 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2637 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | ||
2638 | goto f_err; | ||
2639 | } | ||
2640 | if ((alg_k & SSL_kDHE) && | ||
2641 | !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) { | ||
2642 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2643 | SSL_R_MISSING_DH_KEY); | ||
2644 | goto f_err; | ||
2645 | } | ||
2646 | |||
2647 | return (1); | ||
2648 | f_err: | ||
2649 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | ||
2650 | err: | ||
2651 | return (0); | ||
2652 | } | ||
2653 | |||
2654 | int | ||
2655 | ssl3_send_next_proto(SSL *s) | ||
2656 | { | ||
2657 | unsigned int len, padding_len; | ||
2658 | unsigned char *d, *p; | ||
2659 | |||
2660 | if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { | ||
2661 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEXT_PROTO); | ||
2662 | |||
2663 | len = s->next_proto_negotiated_len; | ||
2664 | padding_len = 32 - ((len + 2) % 32); | ||
2665 | *(p++) = len; | ||
2666 | memcpy(p, s->next_proto_negotiated, len); | ||
2667 | p += len; | ||
2668 | *(p++) = padding_len; | ||
2669 | memset(p, 0, padding_len); | ||
2670 | p += padding_len; | ||
2671 | |||
2672 | ssl3_handshake_msg_finish(s, p - d); | ||
2673 | |||
2674 | s->state = SSL3_ST_CW_NEXT_PROTO_B; | ||
2675 | } | ||
2676 | |||
2677 | return (ssl3_handshake_write(s)); | ||
2678 | } | ||
2679 | |||
2680 | /* | ||
2681 | * Check to see if handshake is full or resumed. Usually this is just a | ||
2682 | * case of checking to see if a cache hit has occurred. In the case of | ||
2683 | * session tickets we have to check the next message to be sure. | ||
2684 | */ | ||
2685 | |||
2686 | int | ||
2687 | ssl3_check_finished(SSL *s) | ||
2688 | { | ||
2689 | int ok; | ||
2690 | long n; | ||
2691 | |||
2692 | /* If we have no ticket it cannot be a resumed session. */ | ||
2693 | if (!s->session->tlsext_tick) | ||
2694 | return (1); | ||
2695 | /* this function is called when we really expect a Certificate | ||
2696 | * message, so permit appropriate message length */ | ||
2697 | n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, | ||
2698 | SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok); | ||
2699 | if (!ok) | ||
2700 | return ((int)n); | ||
2701 | s->s3->tmp.reuse_message = 1; | ||
2702 | if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) || | ||
2703 | (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET)) | ||
2704 | return (2); | ||
2705 | |||
2706 | return (1); | ||
2707 | } | ||
2708 | |||
2709 | int | ||
2710 | ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) | ||
2711 | { | ||
2712 | int i = 0; | ||
2713 | |||
2714 | #ifndef OPENSSL_NO_ENGINE | ||
2715 | if (s->ctx->client_cert_engine) { | ||
2716 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | ||
2717 | SSL_get_client_CA_list(s), | ||
2718 | px509, ppkey, NULL, NULL, NULL); | ||
2719 | if (i != 0) | ||
2720 | return (i); | ||
2721 | } | ||
2722 | #endif | ||
2723 | if (s->ctx->client_cert_cb) | ||
2724 | i = s->ctx->client_cert_cb(s, px509, ppkey); | ||
2725 | return (i); | ||
2726 | } | ||