blob: 48d4a553f11d41cffc864710d41886b2814513a6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
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 <openssl/buf.h>
154#include <openssl/rand.h>
155#include <openssl/obj.h>
156#include <openssl/evp.h>
157#include <openssl/mem.h>
158#include <openssl/md5.h>
159#include <openssl/dh.h>
160#include <openssl/bn.h>
161#include <openssl/engine.h>
162#include <openssl/x509.h>
163
164#include "ssl_locl.h"
165#include "../crypto/dh/internal.h"
166
167static const SSL_METHOD *ssl3_get_client_method(int ver);
168
169static const SSL_METHOD *ssl3_get_client_method(int ver)
170 {
171 if (ver == SSL3_VERSION)
172 return(SSLv3_client_method());
173 else
174 return(NULL);
175 }
176
177IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
178 ssl_undefined_function,
179 ssl3_connect,
180 ssl3_get_client_method)
181
182int ssl3_connect(SSL *s)
183 {
184 BUF_MEM *buf=NULL;
185 void (*cb)(const SSL *ssl,int type,int val)=NULL;
186 int ret= -1;
187 int new_state,state,skip=0;
188
189 ERR_clear_error();
190 ERR_clear_system_error();
191
192 if (s->info_callback != NULL)
193 cb=s->info_callback;
194 else if (s->ctx->info_callback != NULL)
195 cb=s->ctx->info_callback;
196
197 s->in_handshake++;
198 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
199
200#ifndef OPENSSL_NO_HEARTBEATS
201 /* If we're awaiting a HeartbeatResponse, pretend we
202 * already got and don't await it anymore, because
203 * Heartbeats don't make sense during handshakes anyway.
204 */
205 if (s->tlsext_hb_pending)
206 {
207 s->tlsext_hb_pending = 0;
208 s->tlsext_hb_seq++;
209 }
210#endif
211
Adam Langleyadb739e2014-06-20 12:00:00 -0700212 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
213 {
214 /* Send app data along with CCS/Finished */
215 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED;
216 }
217
Adam Langley95c29f32014-06-20 12:00:00 -0700218 for (;;)
219 {
220 state=s->state;
221
222 switch(s->state)
223 {
224 case SSL_ST_RENEGOTIATE:
225 s->renegotiate=1;
226 s->state=SSL_ST_CONNECT;
227 s->ctx->stats.sess_connect_renegotiate++;
228 /* break */
229 case SSL_ST_BEFORE:
230 case SSL_ST_CONNECT:
231 case SSL_ST_BEFORE|SSL_ST_CONNECT:
232 case SSL_ST_OK|SSL_ST_CONNECT:
233
234 s->server=0;
235 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
236
237 if ((s->version & 0xff00 ) != 0x0300)
238 {
239 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
240 ret = -1;
241 goto end;
242 }
243
244 /* s->version=SSL3_VERSION; */
245 s->type=SSL_ST_CONNECT;
246
247 if (s->init_buf == NULL)
248 {
249 if ((buf=BUF_MEM_new()) == NULL)
250 {
251 ret= -1;
252 goto end;
253 }
254 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
255 {
256 ret= -1;
257 goto end;
258 }
259 s->init_buf=buf;
260 buf=NULL;
261 }
262
263 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
264
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
267
268 /* don't push the buffering BIO quite yet */
269
270 ssl3_init_finished_mac(s);
271
272 s->state=SSL3_ST_CW_CLNT_HELLO_A;
273 s->ctx->stats.sess_connect++;
274 s->init_num=0;
275 break;
276
277 case SSL3_ST_CW_CLNT_HELLO_A:
278 case SSL3_ST_CW_CLNT_HELLO_B:
279
280 s->shutdown=0;
281 ret=ssl3_client_hello(s);
282 if (ret <= 0) goto end;
283 s->state=SSL3_ST_CR_SRVR_HELLO_A;
284 s->init_num=0;
285
286 /* turn on buffering for the next lot of output */
287 if (s->bbio != s->wbio)
288 s->wbio=BIO_push(s->bbio,s->wbio);
289
290 break;
291
292 case SSL3_ST_CR_SRVR_HELLO_A:
293 case SSL3_ST_CR_SRVR_HELLO_B:
294 ret=ssl3_get_server_hello(s);
295 if (ret <= 0) goto end;
296
297 if (s->hit)
298 {
299 s->state=SSL3_ST_CR_FINISHED_A;
300#ifndef OPENSSL_NO_TLSEXT
301 if (s->tlsext_ticket_expected)
302 {
303 /* receive renewed session ticket */
304 s->state=SSL3_ST_CR_SESSION_TICKET_A;
305 }
306#endif
307 }
308 else
309 {
310#ifndef OPENSSL_NO_TLSEXT
311 /* The server hello indicated that
312 * an audit proof would follow. */
313 if (s->s3->tlsext_authz_server_promised)
314 s->state=SSL3_ST_CR_SUPPLEMENTAL_DATA_A;
315 else
316#endif
317 s->state=SSL3_ST_CR_CERT_A;
318 }
319 s->init_num=0;
320 break;
321#ifndef OPENSSL_NO_TLSEXT
322 case SSL3_ST_CR_SUPPLEMENTAL_DATA_A:
323 case SSL3_ST_CR_SUPPLEMENTAL_DATA_B:
324 ret = tls1_get_server_supplemental_data(s);
325 if (ret <= 0) goto end;
326 s->state=SSL3_ST_CR_CERT_A;
327 s->init_num = 0;
328 break;
329#endif
330 case SSL3_ST_CR_CERT_A:
331 case SSL3_ST_CR_CERT_B:
332#ifndef OPENSSL_NO_TLSEXT
333 ret=ssl3_check_finished(s);
334 if (ret <= 0) goto end;
335 if (ret == 2)
336 {
337 s->hit = 1;
338 if (s->tlsext_ticket_expected)
339 s->state=SSL3_ST_CR_SESSION_TICKET_A;
340 else
341 s->state=SSL3_ST_CR_FINISHED_A;
342 s->init_num=0;
343 break;
344 }
345#endif
346 /* Check if it is anon DH/ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -0700347 /* or non-RSA PSK */
Adam Langley95c29f32014-06-20 12:00:00 -0700348 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
Adam Langleyc26c8022014-06-20 12:00:00 -0700349 !((s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) &&
350 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -0700351 {
352 ret=ssl3_get_server_certificate(s);
353 if (ret <= 0) goto end;
354#ifndef OPENSSL_NO_TLSEXT
355 if (s->tlsext_status_expected)
356 s->state=SSL3_ST_CR_CERT_STATUS_A;
357 else
358 s->state=SSL3_ST_CR_KEY_EXCH_A;
359 }
360 else
361 {
362 skip = 1;
363 s->state=SSL3_ST_CR_KEY_EXCH_A;
364 }
365#else
366 }
367 else
368 skip=1;
369
370 s->state=SSL3_ST_CR_KEY_EXCH_A;
371#endif
372 s->init_num=0;
373 break;
374
375 case SSL3_ST_CR_KEY_EXCH_A:
376 case SSL3_ST_CR_KEY_EXCH_B:
377 ret=ssl3_get_key_exchange(s);
378 if (ret <= 0) goto end;
379 s->state=SSL3_ST_CR_CERT_REQ_A;
380 s->init_num=0;
381
382 /* at this point we check that we have the
383 * required stuff from the server */
384 if (!ssl3_check_cert_and_algorithm(s))
385 {
386 ret= -1;
387 goto end;
388 }
389 break;
390
391 case SSL3_ST_CR_CERT_REQ_A:
392 case SSL3_ST_CR_CERT_REQ_B:
393 ret=ssl3_get_certificate_request(s);
394 if (ret <= 0) goto end;
395 s->state=SSL3_ST_CR_SRVR_DONE_A;
396 s->init_num=0;
397 break;
398
399 case SSL3_ST_CR_SRVR_DONE_A:
400 case SSL3_ST_CR_SRVR_DONE_B:
401 ret=ssl3_get_server_done(s);
402 if (ret <= 0) goto end;
403 if (s->s3->tmp.cert_req)
404 s->state=SSL3_ST_CW_CERT_A;
405 else
406 s->state=SSL3_ST_CW_KEY_EXCH_A;
407 s->init_num=0;
408
409 break;
410
411 case SSL3_ST_CW_CERT_A:
412 case SSL3_ST_CW_CERT_B:
413 case SSL3_ST_CW_CERT_C:
414 case SSL3_ST_CW_CERT_D:
415 ret=ssl3_send_client_certificate(s);
416 if (ret <= 0) goto end;
417 s->state=SSL3_ST_CW_KEY_EXCH_A;
418 s->init_num=0;
419 break;
420
421 case SSL3_ST_CW_KEY_EXCH_A:
422 case SSL3_ST_CW_KEY_EXCH_B:
423 ret=ssl3_send_client_key_exchange(s);
424 if (ret <= 0) goto end;
425 /* EAY EAY EAY need to check for DH fix cert
426 * sent back */
427 /* For TLS, cert_req is set to 2, so a cert chain
428 * of nothing is sent, but no verify packet is sent */
429 /* XXX: For now, we do not support client
430 * authentication in ECDH cipher suites with
431 * ECDH (rather than ECDSA) certificates.
432 * We need to skip the certificate verify
433 * message when client's ECDH public key is sent
434 * inside the client certificate.
435 */
436 if (s->s3->tmp.cert_req == 1)
437 {
438 s->state=SSL3_ST_CW_CERT_VRFY_A;
439 }
440 else
441 {
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 {
447 s->state=SSL3_ST_CW_CHANGE_A;
448 s->s3->change_cipher_spec=0;
449 }
450
451 s->init_num=0;
452 break;
453
454 case SSL3_ST_CW_CERT_VRFY_A:
455 case SSL3_ST_CW_CERT_VRFY_B:
456 ret=ssl3_send_client_verify(s);
457 if (ret <= 0) 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) goto end;
468
Adam Langley1258b6a2014-06-20 12:00:00 -0700469 s->state=SSL3_ST_CW_FINISHED_A;
470#if !defined(OPENSSL_NO_TLSEXT)
471 if (s->s3->tlsext_channel_id_valid)
472 s->state=SSL3_ST_CW_CHANNEL_ID_A;
473# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700474 if (s->s3->next_proto_neg_seen)
475 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700476# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700477#endif
478 s->init_num=0;
479
480 s->session->cipher=s->s3->tmp.new_cipher;
481 s->session->compress_meth=0;
482 if (!s->method->ssl3_enc->setup_key_block(s))
483 {
484 ret= -1;
485 goto end;
486 }
487
488 if (!s->method->ssl3_enc->change_cipher_state(s,
489 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
490 {
491 ret= -1;
492 goto end;
493 }
494
495 break;
496
497#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
498 case SSL3_ST_CW_NEXT_PROTO_A:
499 case SSL3_ST_CW_NEXT_PROTO_B:
500 ret=ssl3_send_next_proto(s);
501 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700502 if (s->s3->tlsext_channel_id_valid)
503 s->state=SSL3_ST_CW_CHANNEL_ID_A;
504 else
505 s->state=SSL3_ST_CW_FINISHED_A;
506 break;
507#endif
508
509#if !defined(OPENSSL_NO_TLSEXT)
510 case SSL3_ST_CW_CHANNEL_ID_A:
511 case SSL3_ST_CW_CHANNEL_ID_B:
512 ret=ssl3_send_channel_id(s);
513 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700514 s->state=SSL3_ST_CW_FINISHED_A;
515 break;
516#endif
517
518 case SSL3_ST_CW_FINISHED_A:
519 case SSL3_ST_CW_FINISHED_B:
520 ret=ssl3_send_finished(s,
521 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
522 s->method->ssl3_enc->client_finished_label,
523 s->method->ssl3_enc->client_finished_label_len);
524 if (ret <= 0) goto end;
525 s->state=SSL3_ST_CW_FLUSH;
526
527 /* clear flags */
528 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
529 if (s->hit)
530 {
531 s->s3->tmp.next_state=SSL_ST_OK;
532 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
533 {
534 s->state=SSL_ST_OK;
535 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
536 s->s3->delay_buf_pop_ret=0;
537 }
538 }
539 else
540 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700541 /* This is a non-resumption handshake. If it
542 * involves ChannelID, then record the
543 * handshake hashes at this point in the
544 * session so that any resumption of this
545 * session with ChannelID can sign those
546 * hashes. */
547 if (s->s3->tlsext_channel_id_new)
548 {
549 ret = tls1_record_handshake_hashes_for_channel_id(s);
550 if (ret <= 0)
551 goto end;
552 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700553 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
554 && ssl3_can_cutthrough(s)
555 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
556 )
557 {
558 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
559 {
560 s->state=SSL3_ST_CUTTHROUGH_COMPLETE;
561 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
562 s->s3->delay_buf_pop_ret=0;
563 }
564 else
565 {
566 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
567 }
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700570 {
571#ifndef OPENSSL_NO_TLSEXT
572 /* Allow NewSessionTicket if ticket expected */
573 if (s->tlsext_ticket_expected)
574 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
575 else
Adam Langley95c29f32014-06-20 12:00:00 -0700576#endif
Adam Langleyadb739e2014-06-20 12:00:00 -0700577 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
578 }
Adam Langley95c29f32014-06-20 12:00:00 -0700579 }
580 s->init_num=0;
581 break;
582
583#ifndef OPENSSL_NO_TLSEXT
584 case SSL3_ST_CR_SESSION_TICKET_A:
585 case SSL3_ST_CR_SESSION_TICKET_B:
586 ret=ssl3_get_new_session_ticket(s);
587 if (ret <= 0) goto end;
588 s->state=SSL3_ST_CR_FINISHED_A;
589 s->init_num=0;
590 break;
591
592 case SSL3_ST_CR_CERT_STATUS_A:
593 case SSL3_ST_CR_CERT_STATUS_B:
594 ret=ssl3_get_cert_status(s);
595 if (ret <= 0) goto end;
596 s->state=SSL3_ST_CR_KEY_EXCH_A;
597 s->init_num=0;
598 break;
599#endif
600
601 case SSL3_ST_CR_FINISHED_A:
602 case SSL3_ST_CR_FINISHED_B:
603
604 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
605 SSL3_ST_CR_FINISHED_B);
606 if (ret <= 0) goto end;
607
608 if (s->hit)
609 s->state=SSL3_ST_CW_CHANGE_A;
610 else
611 s->state=SSL_ST_OK;
612 s->init_num=0;
613 break;
614
615 case SSL3_ST_CW_FLUSH:
616 s->rwstate=SSL_WRITING;
617 if (BIO_flush(s->wbio) <= 0)
618 {
619 ret= -1;
620 goto end;
621 }
622 s->rwstate=SSL_NOTHING;
623 s->state=s->s3->tmp.next_state;
624 break;
625
Adam Langleyadb739e2014-06-20 12:00:00 -0700626 case SSL3_ST_CUTTHROUGH_COMPLETE:
627#ifndef OPENSSL_NO_TLSEXT
628 /* Allow NewSessionTicket if ticket expected */
629 if (s->tlsext_ticket_expected)
630 s->state=SSL3_ST_CR_SESSION_TICKET_A;
631 else
632#endif
633 s->state=SSL3_ST_CR_FINISHED_A;
634
635 /* SSL_write() will take care of flushing buffered data if
636 * DELAY_CLIENT_FINISHED is set.
637 */
638 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED))
639 ssl_free_wbio_buffer(s);
640 ret = 1;
641 goto end;
642 /* break; */
643
Adam Langley95c29f32014-06-20 12:00:00 -0700644 case SSL_ST_OK:
645 /* clean a few things up */
646 ssl3_cleanup_key_block(s);
647
648 if (s->init_buf != NULL)
649 {
650 BUF_MEM_free(s->init_buf);
651 s->init_buf=NULL;
652 }
653
654 /* If we are not 'joining' the last two packets,
655 * remove the buffering now */
656 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
657 ssl_free_wbio_buffer(s);
658 /* else do it later in ssl3_write */
659
660 s->init_num=0;
661 s->renegotiate=0;
662 s->new_session=0;
663
664 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
665 if (s->hit) s->ctx->stats.sess_hit++;
666
667 ret=1;
668 /* s->server=0; */
669 s->handshake_func=ssl3_connect;
670 s->ctx->stats.sess_connect_good++;
671
672 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
673
674 goto end;
675 /* break; */
676
677 default:
678 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
679 ret= -1;
680 goto end;
681 /* break; */
682 }
683
684 /* did we do anything */
685 if (!s->s3->tmp.reuse_message && !skip)
686 {
687 if (s->debug)
688 {
689 if ((ret=BIO_flush(s->wbio)) <= 0)
690 goto end;
691 }
692
693 if ((cb != NULL) && (s->state != state))
694 {
695 new_state=s->state;
696 s->state=state;
697 cb(s,SSL_CB_CONNECT_LOOP,1);
698 s->state=new_state;
699 }
700 }
701 skip=0;
702 }
703end:
704 s->in_handshake--;
705 if (buf != NULL)
706 BUF_MEM_free(buf);
707 if (cb != NULL)
708 cb(s,SSL_CB_CONNECT_EXIT,ret);
709 return(ret);
710 }
711
712
713int ssl3_client_hello(SSL *s)
714 {
715 unsigned char *buf;
716 unsigned char *p,*d;
717 int i;
718 unsigned long l;
719
720 buf=(unsigned char *)s->init_buf->data;
721 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
722 {
723 SSL_SESSION *sess = s->session;
724 if ((sess == NULL) ||
725 (sess->ssl_version != s->version) ||
726#ifdef OPENSSL_NO_TLSEXT
727 !sess->session_id_length ||
728#else
729 (!sess->session_id_length && !sess->tlsext_tick) ||
730#endif
731 (sess->not_resumable))
732 {
733 if (!ssl_get_new_session(s,0))
734 goto err;
735 }
736 if (s->method->version == DTLS_ANY_VERSION)
737 {
738 /* Determine which DTLS version to use */
739 int options = s->options;
740 /* If DTLS 1.2 disabled correct the version number */
741 if (options & SSL_OP_NO_DTLSv1_2)
742 {
743 if (tls1_suiteb(s))
744 {
745 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
746 goto err;
747 }
748 /* Disabling all versions is silly: return an
749 * error.
750 */
751 if (options & SSL_OP_NO_DTLSv1)
752 {
753 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
754 goto err;
755 }
756 /* Update method so we don't use any DTLS 1.2
757 * features.
758 */
759 s->method = DTLSv1_client_method();
760 s->version = DTLS1_VERSION;
761 }
762 else
763 {
764 /* We only support one version: update method */
765 if (options & SSL_OP_NO_DTLSv1)
766 s->method = DTLSv1_2_client_method();
767 s->version = DTLS1_2_VERSION;
768 }
769 s->client_version = s->version;
770 }
771 /* else use the pre-loaded session */
772
773 p=s->s3->client_random;
774
775 /* for DTLS if client_random is initialized, reuse it, we are
776 * required to use same upon reply to HelloVerify */
777 if (SSL_IS_DTLS(s))
778 {
779 size_t idx;
780 i = 1;
781 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
782 {
783 if (p[idx])
784 {
785 i = 0;
786 break;
787 }
788 }
789 }
790 else
791 i = 1;
792
793 if (i)
794 ssl_fill_hello_random(s, 0, p,
795 sizeof(s->s3->client_random));
796
797 /* Do the message type and length last */
798 d=p= ssl_handshake_start(s);
799
800 /* version indicates the negotiated version: for example from
801 * an SSLv2/v3 compatible client hello). The client_version
802 * field is the maximum version we permit and it is also
803 * used in RSA encrypted premaster secrets. Some servers can
804 * choke if we initially report a higher version then
805 * renegotiate to a lower one in the premaster secret. This
806 * didn't happen with TLS 1.0 as most servers supported it
807 * but it can with TLS 1.1 or later if the server only supports
808 * 1.0.
809 *
810 * Possible scenario with previous logic:
811 * 1. Client hello indicates TLS 1.2
812 * 2. Server hello says TLS 1.0
813 * 3. RSA encrypted premaster secret uses 1.2.
814 * 4. Handhaked proceeds using TLS 1.0.
815 * 5. Server sends hello request to renegotiate.
816 * 6. Client hello indicates TLS v1.0 as we now
817 * know that is maximum server supports.
818 * 7. Server chokes on RSA encrypted premaster secret
819 * containing version 1.0.
820 *
821 * For interoperability it should be OK to always use the
822 * maximum version we support in client hello and then rely
823 * on the checking of version to ensure the servers isn't
824 * being inconsistent: for example initially negotiating with
825 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
826 * client_version in client hello and not resetting it to
827 * the negotiated version.
828 */
829#if 0
830 *(p++)=s->version>>8;
831 *(p++)=s->version&0xff;
832 s->client_version=s->version;
833#else
834 *(p++)=s->client_version>>8;
835 *(p++)=s->client_version&0xff;
836#endif
837
838 /* Random stuff */
839 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
840 p+=SSL3_RANDOM_SIZE;
841
842 /* Session ID */
843 if (s->new_session)
844 i=0;
845 else
846 i=s->session->session_id_length;
847 *(p++)=i;
848 if (i != 0)
849 {
850 if (i > (int)sizeof(s->session->session_id))
851 {
852 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
853 goto err;
854 }
855 memcpy(p,s->session->session_id,i);
856 p+=i;
857 }
858
859 /* cookie stuff for DTLS */
860 if (SSL_IS_DTLS(s))
861 {
862 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
863 {
864 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
865 goto err;
866 }
867 *(p++) = s->d1->cookie_len;
868 memcpy(p, s->d1->cookie, s->d1->cookie_len);
869 p += s->d1->cookie_len;
870 }
871
872 /* Ciphers supported */
873 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
874 if (i == 0)
875 {
876 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
877 goto err;
878 }
879#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
880 /* Some servers hang if client hello > 256 bytes
881 * as hack workaround chop number of supported ciphers
882 * to keep it well below this if we use TLS v1.2
883 */
884 if (TLS1_get_version(s) >= TLS1_2_VERSION
885 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
886 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
887#endif
888 s2n(i,p);
889 p+=i;
890
891 /* COMPRESSION */
892 *(p++)=1;
893 *(p++)=0; /* Add the NULL method */
894
895#ifndef OPENSSL_NO_TLSEXT
896 /* TLS extensions*/
897 if (ssl_prepare_clienthello_tlsext(s) <= 0)
898 {
899 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
900 goto err;
901 }
902 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
903 {
904 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
905 goto err;
906 }
907#endif
908
909 l= p-d;
910 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
911 s->state=SSL3_ST_CW_CLNT_HELLO_B;
912 }
913
914 /* SSL3_ST_CW_CLNT_HELLO_B */
915 return ssl_do_write(s);
916err:
917 return(-1);
918 }
919
920int ssl3_get_server_hello(SSL *s)
921 {
922 STACK_OF(SSL_CIPHER) *sk;
923 const SSL_CIPHER *c;
924 CERT *ct = s->cert;
925 unsigned char *p,*d;
926 int al=SSL_AD_INTERNAL_ERROR,ok;
927 unsigned int j;
928 long n;
929 /* Hello verify request and/or server hello version may not
930 * match so set first packet if we're negotiating version.
931 */
932 if (SSL_IS_DTLS(s))
933 s->first_packet = 1;
934
935 n=s->method->ssl_get_message(s,
936 SSL3_ST_CR_SRVR_HELLO_A,
937 SSL3_ST_CR_SRVR_HELLO_B,
938 -1,
939 20000, /* ?? */
940 &ok);
941
942 if (!ok) return((int)n);
943
944 if (SSL_IS_DTLS(s))
945 {
946 s->first_packet = 0;
947 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
948 {
949 if ( s->d1->send_cookie == 0)
950 {
951 s->s3->tmp.reuse_message = 1;
952 return 1;
953 }
954 else /* already sent a cookie */
955 {
956 al=SSL_AD_UNEXPECTED_MESSAGE;
957 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
958 goto f_err;
959 }
960 }
961 }
962
963 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
964 {
965 al=SSL_AD_UNEXPECTED_MESSAGE;
966 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
967 goto f_err;
968 }
969
970 d=p=(unsigned char *)s->init_msg;
971 if (s->method->version == DTLS_ANY_VERSION)
972 {
973 /* Work out correct protocol version to use */
974 int hversion = (p[0] << 8)|p[1];
975 int options = s->options;
976 if (hversion == DTLS1_2_VERSION
977 && !(options & SSL_OP_NO_DTLSv1_2))
978 s->method = DTLSv1_2_client_method();
979 else if (tls1_suiteb(s))
980 {
981 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
982 s->version = hversion;
983 al = SSL_AD_PROTOCOL_VERSION;
984 goto f_err;
985 }
986 else if (hversion == DTLS1_VERSION
987 && !(options & SSL_OP_NO_DTLSv1))
988 s->method = DTLSv1_client_method();
989 else
990 {
991 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
992 s->version = hversion;
993 al = SSL_AD_PROTOCOL_VERSION;
994 goto f_err;
995 }
996 s->version = s->client_version = s->method->version;
997 }
998
999 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
1000 {
1001 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
1002 s->version=(s->version&0xff00)|p[1];
1003 al=SSL_AD_PROTOCOL_VERSION;
1004 goto f_err;
1005 }
1006 p+=2;
1007
1008 /* load the server hello data */
1009 /* load the server random */
1010 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
1011 p+=SSL3_RANDOM_SIZE;
1012
1013 /* get the session-id */
1014 j= *(p++);
1015
1016 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
1017 {
1018 al=SSL_AD_ILLEGAL_PARAMETER;
1019 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1020 goto f_err;
1021 }
1022
1023#ifndef OPENSSL_NO_TLSEXT
1024 /* check if we want to resume the session based on external pre-shared secret */
1025 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1026 {
1027 SSL_CIPHER *pref_cipher=NULL;
1028 s->session->master_key_length=sizeof(s->session->master_key);
1029 if (s->tls_session_secret_cb(s, s->session->master_key,
1030 &s->session->master_key_length,
1031 NULL, &pref_cipher,
1032 s->tls_session_secret_cb_arg))
1033 {
1034 s->session->cipher = pref_cipher ?
1035 pref_cipher : ssl_get_cipher_by_char(s, p+j);
1036 }
1037 }
1038#endif /* OPENSSL_NO_TLSEXT */
1039
1040 if (j != 0 && j == s->session->session_id_length
1041 && memcmp(p,s->session->session_id,j) == 0)
1042 {
1043 if(s->sid_ctx_length != s->session->sid_ctx_length
1044 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
1045 {
1046 /* actually a client application bug */
1047 al=SSL_AD_ILLEGAL_PARAMETER;
1048 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1049 goto f_err;
1050 }
1051 s->hit=1;
1052 }
1053 else /* a miss or crap from the other end */
1054 {
1055 /* If we were trying for session-id reuse, make a new
1056 * SSL_SESSION so we don't stuff up other people */
1057 s->hit=0;
1058 if (s->session->session_id_length > 0)
1059 {
1060 if (!ssl_get_new_session(s,0))
1061 {
1062 goto f_err;
1063 }
1064 }
1065 s->session->session_id_length=j;
1066 memcpy(s->session->session_id,p,j); /* j could be 0 */
1067 }
1068 p+=j;
1069 c=ssl_get_cipher_by_char(s,p);
1070 if (c == NULL)
1071 {
1072 /* unknown cipher */
1073 al=SSL_AD_ILLEGAL_PARAMETER;
1074 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
1075 goto f_err;
1076 }
1077 /* If it is a disabled cipher we didn't send it in client hello,
1078 * so return an error.
1079 */
1080 if (c->algorithm_ssl & ct->mask_ssl ||
1081 c->algorithm_mkey & ct->mask_k ||
1082 c->algorithm_auth & ct->mask_a)
1083 {
1084 al=SSL_AD_ILLEGAL_PARAMETER;
1085 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1086 goto f_err;
1087 }
1088 p+=ssl_put_cipher_by_char(s,NULL,NULL);
1089
1090 sk=ssl_get_ciphers_by_id(s);
1091 if (!sk_SSL_CIPHER_find(sk, NULL, c))
1092 {
1093 /* we did not say we would use this cipher */
1094 al=SSL_AD_ILLEGAL_PARAMETER;
1095 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1096 goto f_err;
1097 }
1098
1099 /* Depending on the session caching (internal/external), the cipher
1100 and/or cipher_id values may not be set. Make sure that
1101 cipher_id is set and use it for comparison. */
1102 if (s->session->cipher)
1103 s->session->cipher_id = s->session->cipher->id;
1104 if (s->hit && (s->session->cipher_id != c->id))
1105 {
1106/* Workaround is now obsolete */
1107#if 0
1108 if (!(s->options &
1109 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1110#endif
1111 {
1112 al=SSL_AD_ILLEGAL_PARAMETER;
1113 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1114 goto f_err;
1115 }
1116 }
1117 s->s3->tmp.new_cipher=c;
1118 /* Don't digest cached records if no sigalgs: we may need them for
1119 * client authentication.
1120 */
1121 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1122 goto f_err;
1123 /* lets get the compression algorithm */
1124 /* COMPRESSION */
1125 if (*(p++) != 0)
1126 {
1127 al=SSL_AD_ILLEGAL_PARAMETER;
1128 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1129 goto f_err;
1130 }
1131 /* If compression is disabled we'd better not try to resume a session
1132 * using compression.
1133 */
1134 if (s->session->compress_meth != 0)
1135 {
1136 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_INCONSISTENT_COMPRESSION);
1137 goto f_err;
1138 }
1139
1140#ifndef OPENSSL_NO_TLSEXT
1141 /* TLS extensions*/
1142 if (!ssl_parse_serverhello_tlsext(s,&p,d,n))
1143 {
1144 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
1145 goto err;
1146 }
1147#endif
1148
1149 if (p != (d+n))
1150 {
1151 /* wrong packet length */
1152 al=SSL_AD_DECODE_ERROR;
1153 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
1154 goto f_err;
1155 }
1156
1157 return(1);
1158f_err:
1159 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1160err:
1161 return(-1);
1162 }
1163
1164int ssl3_get_server_certificate(SSL *s)
1165 {
1166 int al,i,ok,ret= -1;
1167 unsigned long n,nc,llen,l;
1168 X509 *x=NULL;
1169 const unsigned char *q,*p;
1170 unsigned char *d;
1171 STACK_OF(X509) *sk=NULL;
1172 SESS_CERT *sc;
1173 EVP_PKEY *pkey=NULL;
1174 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
1175
1176 n=s->method->ssl_get_message(s,
1177 SSL3_ST_CR_CERT_A,
1178 SSL3_ST_CR_CERT_B,
1179 -1,
1180 s->max_cert_list,
1181 &ok);
1182
1183 if (!ok) return((int)n);
1184
1185 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1186 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1187 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
1188 {
1189 s->s3->tmp.reuse_message=1;
1190 return(1);
1191 }
1192
1193 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1194 {
1195 al=SSL_AD_UNEXPECTED_MESSAGE;
1196 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_BAD_MESSAGE_TYPE);
1197 goto f_err;
1198 }
1199 p=d=(unsigned char *)s->init_msg;
1200
1201 if ((sk=sk_X509_new_null()) == NULL)
1202 {
1203 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1204 goto err;
1205 }
1206
1207 n2l3(p,llen);
1208 if (llen+3 != n)
1209 {
1210 al=SSL_AD_DECODE_ERROR;
1211 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1212 goto f_err;
1213 }
1214 for (nc=0; nc<llen; )
1215 {
1216 n2l3(p,l);
1217 if ((l+nc+3) > llen)
1218 {
1219 al=SSL_AD_DECODE_ERROR;
1220 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1221 goto f_err;
1222 }
1223
1224 q=p;
1225 x=d2i_X509(NULL,&q,l);
1226 if (x == NULL)
1227 {
1228 al=SSL_AD_BAD_CERTIFICATE;
1229 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1230 goto f_err;
1231 }
1232 if (q != (p+l))
1233 {
1234 al=SSL_AD_DECODE_ERROR;
1235 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1236 goto f_err;
1237 }
1238 if (!sk_X509_push(sk,x))
1239 {
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1241 goto err;
1242 }
1243 x=NULL;
1244 nc+=l+3;
1245 p=q;
1246 }
1247
1248 i=ssl_verify_cert_chain(s,sk);
1249 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1250 )
1251 {
1252 al=ssl_verify_alarm_type(s->verify_result);
1253 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1254 goto f_err;
1255 }
1256 ERR_clear_error(); /* but we keep s->verify_result */
1257
1258 sc=ssl_sess_cert_new();
1259 if (sc == NULL) goto err;
1260
1261 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1262 s->session->sess_cert=sc;
1263
1264 sc->cert_chain=sk;
1265 /* Inconsistency alert: cert_chain does include the peer's
1266 * certificate, which we don't include in s3_srvr.c */
1267 x=sk_X509_value(sk,0);
1268 sk=NULL;
1269 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1270
1271 pkey=X509_get_pubkey(x);
1272
1273 /* VRS: allow null cert if auth == KRB5 */
1274 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1275 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1276 ? 0 : 1;
1277
1278#ifdef KSSL_DEBUG
1279 printf("pkey,x = %p, %p\n", pkey,x);
1280 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1281 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1282 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1283#endif /* KSSL_DEBUG */
1284
1285 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1286 {
1287 x=NULL;
1288 al=SSL3_AL_FATAL;
1289 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1290 goto f_err;
1291 }
1292
1293 i=ssl_cert_type(x,pkey);
1294 if (need_cert && i < 0)
1295 {
1296 x=NULL;
1297 al=SSL3_AL_FATAL;
1298 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1299 goto f_err;
1300 }
1301
1302 if (need_cert)
1303 {
1304 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1305 if (exp_idx >= 0 && i != exp_idx)
1306 {
1307 x=NULL;
1308 al=SSL_AD_ILLEGAL_PARAMETER;
1309 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1310 goto f_err;
1311 }
1312 sc->peer_cert_type=i;
1313 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1314 /* Why would the following ever happen?
1315 * We just created sc a couple of lines ago. */
1316 if (sc->peer_pkeys[i].x509 != NULL)
1317 X509_free(sc->peer_pkeys[i].x509);
1318 sc->peer_pkeys[i].x509=x;
1319 sc->peer_key= &(sc->peer_pkeys[i]);
1320
1321 if (s->session->peer != NULL)
1322 X509_free(s->session->peer);
1323 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1324 s->session->peer=x;
1325 }
1326 else
1327 {
1328 sc->peer_cert_type=i;
1329 sc->peer_key= NULL;
1330
1331 if (s->session->peer != NULL)
1332 X509_free(s->session->peer);
1333 s->session->peer=NULL;
1334 }
1335 s->session->verify_result = s->verify_result;
1336
1337 x=NULL;
1338#ifndef OPENSSL_NO_TLSEXT
1339 /* Check the audit proof. */
1340 if (s->ctx->tlsext_authz_server_audit_proof_cb)
1341 {
1342 ret = s->ctx->tlsext_authz_server_audit_proof_cb(s,
1343 s->ctx->tlsext_authz_server_audit_proof_cb_arg);
1344 if (ret <= 0)
1345 {
1346 al = SSL_AD_BAD_CERTIFICATE;
1347 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_INVALID_AUDIT_PROOF);
1348 goto f_err;
1349 }
1350 }
1351
1352#endif
1353 ret=1;
1354 if (0)
1355 {
1356f_err:
1357 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1358 }
1359err:
1360 EVP_PKEY_free(pkey);
1361 X509_free(x);
1362 sk_X509_pop_free(sk,X509_free);
1363 return(ret);
1364 }
1365
1366int ssl3_get_key_exchange(SSL *s)
1367 {
1368#ifndef OPENSSL_NO_RSA
1369 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1370#endif
1371 EVP_MD_CTX md_ctx;
1372 unsigned char *param,*p;
1373 int al,i,j,param_len,ok;
1374 long n,alg_k,alg_a;
1375 EVP_PKEY *pkey=NULL;
1376 const EVP_MD *md = NULL;
1377#ifndef OPENSSL_NO_RSA
1378 RSA *rsa=NULL;
1379#endif
1380#ifndef OPENSSL_NO_DH
1381 DH *dh=NULL;
1382#endif
1383#ifndef OPENSSL_NO_ECDH
1384 EC_KEY *ecdh = NULL;
1385 BN_CTX *bn_ctx = NULL;
1386 EC_POINT *srvr_ecpoint = NULL;
1387 int curve_nid = 0;
1388 int encoded_pt_len = 0;
1389#endif
1390
1391 /* use same message size as in ssl3_get_certificate_request()
1392 * as ServerKeyExchange message may be skipped */
1393 n=s->method->ssl_get_message(s,
1394 SSL3_ST_CR_KEY_EXCH_A,
1395 SSL3_ST_CR_KEY_EXCH_B,
1396 -1,
1397 s->max_cert_list,
1398 &ok);
1399 if (!ok) return((int)n);
1400
1401 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1402 {
1403#ifndef OPENSSL_NO_PSK
1404 /* In plain PSK ciphersuite, ServerKeyExchange can be
1405 omitted if no identity hint is sent. Set
1406 session->sess_cert anyway to avoid problems
1407 later.*/
Adam Langleyc26c8022014-06-20 12:00:00 -07001408 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001409 {
1410 s->session->sess_cert=ssl_sess_cert_new();
1411 if (s->ctx->psk_identity_hint)
1412 OPENSSL_free(s->ctx->psk_identity_hint);
1413 s->ctx->psk_identity_hint = NULL;
1414 }
1415#endif
1416 s->s3->tmp.reuse_message=1;
1417 return(1);
1418 }
1419
1420 param=p=(unsigned char *)s->init_msg;
1421 if (s->session->sess_cert != NULL)
1422 {
1423#ifndef OPENSSL_NO_RSA
1424 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1425 {
1426 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1427 s->session->sess_cert->peer_rsa_tmp=NULL;
1428 }
1429#endif
1430#ifndef OPENSSL_NO_DH
1431 if (s->session->sess_cert->peer_dh_tmp)
1432 {
1433 DH_free(s->session->sess_cert->peer_dh_tmp);
1434 s->session->sess_cert->peer_dh_tmp=NULL;
1435 }
1436#endif
1437#ifndef OPENSSL_NO_ECDH
1438 if (s->session->sess_cert->peer_ecdh_tmp)
1439 {
1440 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1441 s->session->sess_cert->peer_ecdh_tmp=NULL;
1442 }
1443#endif
1444 }
1445 else
1446 {
1447 s->session->sess_cert=ssl_sess_cert_new();
1448 }
1449
1450 param_len=0;
1451 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1452 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1453 EVP_MD_CTX_init(&md_ctx);
1454
1455#ifndef OPENSSL_NO_PSK
Adam Langleyc26c8022014-06-20 12:00:00 -07001456 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001457 {
1458 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1459
1460 al=SSL_AD_HANDSHAKE_FAILURE;
1461 n2s(p,i);
1462 param_len=i+2;
Adam Langleyc26c8022014-06-20 12:00:00 -07001463 if (s->ctx->psk_identity_hint)
Adam Langley95c29f32014-06-20 12:00:00 -07001464 OPENSSL_free(s->ctx->psk_identity_hint);
Adam Langleyc26c8022014-06-20 12:00:00 -07001465 s->ctx->psk_identity_hint = NULL;
1466 if (i != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001467 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001468 /* Store PSK identity hint for later use, hint is used
1469 * in ssl3_send_client_key_exchange. Assume that the
1470 * maximum length of a PSK identity hint can be as
1471 * long as the maximum length of a PSK identity. */
1472 if (i > PSK_MAX_IDENTITY_LEN)
1473 {
1474 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
1475 goto f_err;
1476 }
1477 if (param_len > n)
1478 {
1479 al=SSL_AD_DECODE_ERROR;
1480 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1481 goto f_err;
1482 }
1483 /* If received PSK identity hint contains NULL
1484 * characters, the hint is truncated from the first
1485 * NULL. p may not be ending with NULL, so create a
1486 * NULL-terminated string. */
1487 memcpy(tmp_id_hint, p, i);
1488 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1489 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1490 if (s->ctx->psk_identity_hint == NULL)
1491 {
1492 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1493 goto f_err;
1494 }
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
1497 p+=i;
1498 n-=param_len;
1499 }
Adam Langley95c29f32014-06-20 12:00:00 -07001500#endif /* !OPENSSL_NO_PSK */
Adam Langleyc26c8022014-06-20 12:00:00 -07001501
1502 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07001503#ifndef OPENSSL_NO_RSA
Adam Langleyc26c8022014-06-20 12:00:00 -07001504 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001505 {
1506 if ((rsa=RSA_new()) == NULL)
1507 {
1508 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1509 goto err;
1510 }
1511 n2s(p,i);
1512 param_len=i+2;
1513 if (param_len > n)
1514 {
1515 al=SSL_AD_DECODE_ERROR;
1516 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_MODULUS_LENGTH);
1517 goto f_err;
1518 }
1519 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1520 {
1521 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1522 goto err;
1523 }
1524 p+=i;
1525
1526 n2s(p,i);
1527 param_len+=i+2;
1528 if (param_len > n)
1529 {
1530 al=SSL_AD_DECODE_ERROR;
1531 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_E_LENGTH);
1532 goto f_err;
1533 }
1534 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1535 {
1536 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1537 goto err;
1538 }
1539 p+=i;
1540 n-=param_len;
1541
1542 /* this should be because we are using an export cipher */
1543 if (alg_a & SSL_aRSA)
1544 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1545 else
1546 {
1547 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1548 goto err;
1549 }
1550 s->session->sess_cert->peer_rsa_tmp=rsa;
1551 rsa=NULL;
1552 }
Adam Langley95c29f32014-06-20 12:00:00 -07001553#endif
1554#ifndef OPENSSL_NO_DH
1555 else if (alg_k & SSL_kEDH)
1556 {
1557 if ((dh=DH_new()) == NULL)
1558 {
1559 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_DH_LIB);
1560 goto err;
1561 }
1562 n2s(p,i);
1563 param_len=i+2;
1564 if (param_len > n)
1565 {
1566 al=SSL_AD_DECODE_ERROR;
1567 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1568 goto f_err;
1569 }
1570 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1571 {
1572 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1573 goto err;
1574 }
1575 p+=i;
1576
1577 n2s(p,i);
1578 param_len+=i+2;
1579 if (param_len > n)
1580 {
1581 al=SSL_AD_DECODE_ERROR;
1582 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_G_LENGTH);
1583 goto f_err;
1584 }
1585 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1586 {
1587 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1588 goto err;
1589 }
1590 p+=i;
1591
1592 n2s(p,i);
1593 param_len+=i+2;
1594 if (param_len > n)
1595 {
1596 al=SSL_AD_DECODE_ERROR;
1597 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1598 goto f_err;
1599 }
1600 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1601 {
1602 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1603 goto err;
1604 }
1605 p+=i;
1606 n-=param_len;
1607
1608#ifndef OPENSSL_NO_RSA
1609 if (alg_a & SSL_aRSA)
1610 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1611#else
1612 if (0)
1613 ;
1614#endif
1615#ifndef OPENSSL_NO_DSA
1616 else if (alg_a & SSL_aDSS)
1617 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1618#endif
1619 /* else anonymous DH, so no certificate or pkey. */
1620
1621 s->session->sess_cert->peer_dh_tmp=dh;
1622 dh=NULL;
1623 }
1624 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1625 {
1626 al=SSL_AD_ILLEGAL_PARAMETER;
1627 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1628 goto f_err;
1629 }
1630#endif /* !OPENSSL_NO_DH */
1631
1632#ifndef OPENSSL_NO_ECDH
1633 else if (alg_k & SSL_kEECDH)
1634 {
1635 EC_GROUP *ngroup;
1636 const EC_GROUP *group;
1637
1638 if ((ecdh=EC_KEY_new()) == NULL)
1639 {
1640 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1641 goto err;
1642 }
1643
1644 /* Extract elliptic curve parameters and the
1645 * server's ephemeral ECDH public key.
1646 * Keep accumulating lengths of various components in
1647 * param_len and make sure it never exceeds n.
1648 */
1649
1650 /* XXX: For now we only support named (not generic) curves
1651 * and the ECParameters in this case is just three bytes.
1652 */
1653 param_len=3;
1654 /* Check curve is one of our prefrences, if not server has
1655 * sent an invalid curve.
1656 */
1657 if (!tls1_check_curve(s, p, param_len))
1658 {
1659 al=SSL_AD_DECODE_ERROR;
1660 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_CURVE);
1661 goto f_err;
1662 }
1663
1664 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)
1665 {
1666 al=SSL_AD_INTERNAL_ERROR;
1667 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1668 goto f_err;
1669 }
1670
1671 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1672 if (ngroup == NULL)
1673 {
1674 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1675 goto err;
1676 }
1677 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1678 {
1679 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1680 goto err;
1681 }
1682 EC_GROUP_free(ngroup);
1683
1684 group = EC_KEY_get0_group(ecdh);
1685
1686 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1687 (EC_GROUP_get_degree(group) > 163))
1688 {
1689 al=SSL_AD_EXPORT_RESTRICTION;
1690 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1691 goto f_err;
1692 }
1693
1694 p+=3;
1695
1696 /* Next, get the encoded ECPoint */
1697 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1698 ((bn_ctx = BN_CTX_new()) == NULL))
1699 {
1700 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1701 goto err;
1702 }
1703
1704 encoded_pt_len = *p; /* length of encoded point */
1705 p+=1;
1706 param_len += (1 + encoded_pt_len);
1707 if ((param_len > n) ||
1708 (EC_POINT_oct2point(group, srvr_ecpoint,
1709 p, encoded_pt_len, bn_ctx) == 0))
1710 {
1711 al=SSL_AD_DECODE_ERROR;
1712 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_ECPOINT);
1713 goto f_err;
1714 }
1715
1716 n-=param_len;
1717 p+=encoded_pt_len;
1718
1719 /* The ECC/TLS specification does not mention
1720 * the use of DSA to sign ECParameters in the server
1721 * key exchange message. We do support RSA and ECDSA.
1722 */
1723 if (0) ;
1724#ifndef OPENSSL_NO_RSA
1725 else if (alg_a & SSL_aRSA)
1726 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1727#endif
1728#ifndef OPENSSL_NO_ECDSA
1729 else if (alg_a & SSL_aECDSA)
1730 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1731#endif
1732 /* else anonymous ECDH, so no certificate or pkey. */
1733 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1734 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1735 ecdh=NULL;
1736 BN_CTX_free(bn_ctx);
1737 bn_ctx = NULL;
1738 EC_POINT_free(srvr_ecpoint);
1739 srvr_ecpoint = NULL;
1740 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001741#endif /* !OPENSSL_NO_ECDH */
1742
1743 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001744 {
1745 al=SSL_AD_UNEXPECTED_MESSAGE;
1746 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1747 goto f_err;
1748 }
Adam Langley95c29f32014-06-20 12:00:00 -07001749
1750 /* p points to the next byte, there are 'n' bytes left */
1751
1752 /* if it was signed, check the signature */
1753 if (pkey != NULL)
1754 {
1755 if (SSL_USE_SIGALGS(s))
1756 {
1757 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1758 if (rv == -1)
1759 goto err;
1760 else if (rv == 0)
1761 {
1762 al = SSL_AD_DECODE_ERROR;
1763 goto f_err;
1764 }
1765#ifdef SSL_DEBUG
1766fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1767#endif
1768 p += 2;
1769 n -= 2;
1770 }
1771 else
1772 md = EVP_sha1();
1773
1774 n2s(p,i);
1775 n-=2;
1776 j=EVP_PKEY_size(pkey);
1777
1778 if ((i != n) || (n > j) || (n <= 0))
1779 {
1780 /* wrong packet length */
1781 al=SSL_AD_DECODE_ERROR;
1782 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_SIGNATURE_LENGTH);
1783 goto f_err;
1784 }
1785
1786#ifndef OPENSSL_NO_RSA
1787 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1788 {
1789 int num;
1790
1791 j=0;
1792 q=md_buf;
1793 for (num=2; num > 0; num--)
1794 {
1795 EVP_DigestInit_ex(&md_ctx,(num == 2)
1796 ?s->ctx->md5:s->ctx->sha1, NULL);
1797 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1798 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1799 EVP_DigestUpdate(&md_ctx,param,param_len);
1800 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1801 q+=i;
1802 j+=i;
1803 }
1804 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1805 pkey->pkey.rsa);
1806 if (i < 0)
1807 {
1808 al=SSL_AD_DECRYPT_ERROR;
1809 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_DECRYPT);
1810 goto f_err;
1811 }
1812 if (i == 0)
1813 {
1814 /* bad signature */
1815 al=SSL_AD_DECRYPT_ERROR;
1816 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1817 goto f_err;
1818 }
1819 }
1820 else
1821#endif
1822 {
1823 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1824 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1825 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1826 EVP_VerifyUpdate(&md_ctx,param,param_len);
1827 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1828 {
1829 /* bad signature */
1830 al=SSL_AD_DECRYPT_ERROR;
1831 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1832 goto f_err;
1833 }
1834 }
1835 }
1836 else
1837 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001838 if (!(alg_a & SSL_aNULL) &&
1839 /* Among PSK ciphers only RSA_PSK needs a public key */
1840 !((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07001841 {
1842 /* Might be wrong key type, check it */
1843 if (ssl3_check_cert_and_algorithm(s))
1844 /* Otherwise this shouldn't happen */
1845 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1846 goto err;
1847 }
1848 /* still data left over */
1849 if (n != 0)
1850 {
1851 al=SSL_AD_DECODE_ERROR;
1852 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
1853 goto f_err;
1854 }
1855 }
1856 EVP_PKEY_free(pkey);
1857 EVP_MD_CTX_cleanup(&md_ctx);
1858 return(1);
1859f_err:
1860 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1861err:
1862 EVP_PKEY_free(pkey);
1863#ifndef OPENSSL_NO_RSA
1864 if (rsa != NULL)
1865 RSA_free(rsa);
1866#endif
1867#ifndef OPENSSL_NO_DH
1868 if (dh != NULL)
1869 DH_free(dh);
1870#endif
1871#ifndef OPENSSL_NO_ECDH
1872 BN_CTX_free(bn_ctx);
1873 EC_POINT_free(srvr_ecpoint);
1874 if (ecdh != NULL)
1875 EC_KEY_free(ecdh);
1876#endif
1877 EVP_MD_CTX_cleanup(&md_ctx);
1878 return(-1);
1879 }
1880
1881static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1882 {
1883 return(X509_NAME_cmp(*a,*b));
1884 }
1885
1886int ssl3_get_certificate_request(SSL *s)
1887 {
1888 int ok,ret=0;
1889 unsigned long n,nc,l;
1890 unsigned int llen, ctype_num,i;
1891 X509_NAME *xn=NULL;
1892 const unsigned char *p,*q;
1893 unsigned char *d;
1894 STACK_OF(X509_NAME) *ca_sk=NULL;
1895
1896 n=s->method->ssl_get_message(s,
1897 SSL3_ST_CR_CERT_REQ_A,
1898 SSL3_ST_CR_CERT_REQ_B,
1899 -1,
1900 s->max_cert_list,
1901 &ok);
1902
1903 if (!ok) return((int)n);
1904
1905 s->s3->tmp.cert_req=0;
1906
1907 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1908 {
1909 s->s3->tmp.reuse_message=1;
1910 /* If we get here we don't need any cached handshake records
1911 * as we wont be doing client auth.
1912 */
1913 if (s->s3->handshake_buffer)
1914 {
1915 if (!ssl3_digest_cached_records(s))
1916 goto err;
1917 }
1918 return(1);
1919 }
1920
1921 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1922 {
1923 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1924 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1925 goto err;
1926 }
1927
1928 /* TLS does not like anon-DH with client cert */
1929 if (s->version > SSL3_VERSION)
1930 {
1931 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1932 {
1933 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1934 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1935 goto err;
1936 }
1937 }
1938
1939 p=d=(unsigned char *)s->init_msg;
1940
1941 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1942 if (ca_sk == NULL)
1943 {
1944 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1945 goto err;
1946 }
1947
1948 /* get the certificate types */
1949 ctype_num= *(p++);
1950 if (s->cert->ctypes)
1951 {
1952 OPENSSL_free(s->cert->ctypes);
1953 s->cert->ctypes = NULL;
1954 }
1955 if (ctype_num > SSL3_CT_NUMBER)
1956 {
1957 /* If we exceed static buffer copy all to cert structure */
1958 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1959 memcpy(s->cert->ctypes, p, ctype_num);
1960 s->cert->ctype_num = (size_t)ctype_num;
1961 ctype_num=SSL3_CT_NUMBER;
1962 }
1963 for (i=0; i<ctype_num; i++)
1964 s->s3->tmp.ctype[i]= p[i];
1965 p+=p[-1];
1966 if (SSL_USE_SIGALGS(s))
1967 {
1968 n2s(p, llen);
1969 /* Check we have enough room for signature algorithms and
1970 * following length value.
1971 */
1972 if ((unsigned long)(p - d + llen + 2) > n)
1973 {
1974 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1975 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DATA_LENGTH_TOO_LONG);
1976 goto err;
1977 }
1978 /* Clear certificate digests and validity flags */
1979 for (i = 0; i < SSL_PKEY_NUM; i++)
1980 {
1981 s->cert->pkeys[i].digest = NULL;
1982 s->cert->pkeys[i].valid_flags = 0;
1983 }
1984 if ((llen & 1) || !tls1_process_sigalgs(s, p, llen))
1985 {
1986 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1987 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1988 goto err;
1989 }
1990 p += llen;
1991 }
1992
1993 /* get the CA RDNs */
1994 n2s(p,llen);
1995#if 0
1996{
1997FILE *out;
1998out=fopen("/tmp/vsign.der","w");
1999fwrite(p,1,llen,out);
2000fclose(out);
2001}
2002#endif
2003
2004 if ((unsigned long)(p - d + llen) != n)
2005 {
2006 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2007 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
2008 goto err;
2009 }
2010
2011 for (nc=0; nc<llen; )
2012 {
2013 n2s(p,l);
2014 if ((l+nc+2) > llen)
2015 {
2016 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2017 goto cont; /* netscape bugs */
2018 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2019 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
2020 goto err;
2021 }
2022
2023 q=p;
2024
2025 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
2026 {
2027 /* If netscape tolerance is on, ignore errors */
2028 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2029 goto cont;
2030 else
2031 {
2032 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2033 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
2034 goto err;
2035 }
2036 }
2037
2038 if (q != (p+l))
2039 {
2040 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2041 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
2042 goto err;
2043 }
2044 if (!sk_X509_NAME_push(ca_sk,xn))
2045 {
2046 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
2047 goto err;
2048 }
2049
2050 p+=l;
2051 nc+=l+2;
2052 }
2053
2054 if (0)
2055 {
2056cont:
2057 ERR_clear_error();
2058 }
2059
2060 /* we should setup a certificate to return.... */
2061 s->s3->tmp.cert_req=1;
2062 s->s3->tmp.ctype_num=ctype_num;
2063 if (s->s3->tmp.ca_names != NULL)
2064 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
2065 s->s3->tmp.ca_names=ca_sk;
2066 ca_sk=NULL;
2067
2068 ret=1;
2069err:
2070 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
2071 return(ret);
2072 }
2073
2074#ifndef OPENSSL_NO_TLSEXT
2075int ssl3_get_new_session_ticket(SSL *s)
2076 {
2077 int ok,al,ret=0, ticklen;
2078 long n;
2079 const unsigned char *p;
2080 unsigned char *d;
2081
2082 n=s->method->ssl_get_message(s,
2083 SSL3_ST_CR_SESSION_TICKET_A,
2084 SSL3_ST_CR_SESSION_TICKET_B,
2085 -1,
2086 16384,
2087 &ok);
2088
2089 if (!ok)
2090 return((int)n);
2091
2092 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
2093 {
2094 s->s3->tmp.reuse_message=1;
2095 return(1);
2096 }
2097 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
2098 {
2099 al=SSL_AD_UNEXPECTED_MESSAGE;
2100 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE);
2101 goto f_err;
2102 }
2103 if (n < 6)
2104 {
2105 /* need at least ticket_lifetime_hint + ticket length */
2106 al = SSL_AD_DECODE_ERROR;
2107 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
2108 goto f_err;
2109 }
2110
2111 p=d=(unsigned char *)s->init_msg;
2112 n2l(p, s->session->tlsext_tick_lifetime_hint);
2113 n2s(p, ticklen);
2114 /* ticket_lifetime_hint + ticket_length + ticket */
2115 if (ticklen + 6 != n)
2116 {
2117 al = SSL_AD_DECODE_ERROR;
2118 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
2119 goto f_err;
2120 }
2121 if (s->session->tlsext_tick)
2122 {
2123 OPENSSL_free(s->session->tlsext_tick);
2124 s->session->tlsext_ticklen = 0;
2125 }
2126 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2127 if (!s->session->tlsext_tick)
2128 {
2129 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
2130 goto err;
2131 }
2132 memcpy(s->session->tlsext_tick, p, ticklen);
2133 s->session->tlsext_ticklen = ticklen;
2134 /* There are two ways to detect a resumed ticket sesion.
2135 * One is to set an appropriate session ID and then the server
2136 * must return a match in ServerHello. This allows the normal
2137 * client session ID matching to work and we know much
2138 * earlier that the ticket has been accepted.
2139 *
2140 * The other way is to set zero length session ID when the
2141 * ticket is presented and rely on the handshake to determine
2142 * session resumption.
2143 *
2144 * We choose the former approach because this fits in with
2145 * assumptions elsewhere in OpenSSL. The session ID is set
2146 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2147 * ticket.
2148 */
2149 EVP_Digest(p, ticklen,
2150 s->session->session_id, &s->session->session_id_length,
2151#ifndef OPENSSL_NO_SHA256
2152 EVP_sha256(), NULL);
2153#else
2154 EVP_sha1(), NULL);
2155#endif
2156 ret=1;
2157 return(ret);
2158f_err:
2159 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2160err:
2161 return(-1);
2162 }
2163
2164int ssl3_get_cert_status(SSL *s)
2165 {
2166 int ok, al;
2167 unsigned long resplen,n;
2168 const unsigned char *p;
2169
2170 n=s->method->ssl_get_message(s,
2171 SSL3_ST_CR_CERT_STATUS_A,
2172 SSL3_ST_CR_CERT_STATUS_B,
2173 SSL3_MT_CERTIFICATE_STATUS,
2174 16384,
2175 &ok);
2176
2177 if (!ok) return((int)n);
2178 if (n < 4)
2179 {
2180 /* need at least status type + length */
2181 al = SSL_AD_DECODE_ERROR;
2182 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2183 goto f_err;
2184 }
2185 p = (unsigned char *)s->init_msg;
2186 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2187 {
2188 al = SSL_AD_DECODE_ERROR;
2189 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_UNSUPPORTED_STATUS_TYPE);
2190 goto f_err;
2191 }
2192 n2l3(p, resplen);
2193 if (resplen + 4 != n)
2194 {
2195 al = SSL_AD_DECODE_ERROR;
2196 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2197 goto f_err;
2198 }
2199 if (s->tlsext_ocsp_resp)
2200 OPENSSL_free(s->tlsext_ocsp_resp);
2201 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2202 if (!s->tlsext_ocsp_resp)
2203 {
2204 al = SSL_AD_INTERNAL_ERROR;
2205 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2206 goto f_err;
2207 }
2208 s->tlsext_ocsp_resplen = resplen;
2209 if (s->ctx->tlsext_status_cb)
2210 {
2211 int ret;
2212 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2213 if (ret == 0)
2214 {
2215 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2216 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
2217 goto f_err;
2218 }
2219 if (ret < 0)
2220 {
2221 al = SSL_AD_INTERNAL_ERROR;
2222 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2223 goto f_err;
2224 }
2225 }
2226 return 1;
2227f_err:
2228 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2229 return(-1);
2230 }
2231#endif
2232
2233int ssl3_get_server_done(SSL *s)
2234 {
2235 int ok,ret=0;
2236 long n;
2237
2238 n=s->method->ssl_get_message(s,
2239 SSL3_ST_CR_SRVR_DONE_A,
2240 SSL3_ST_CR_SRVR_DONE_B,
2241 SSL3_MT_SERVER_DONE,
2242 30, /* should be very small, like 0 :-) */
2243 &ok);
2244
2245 if (!ok) return((int)n);
2246 if (n > 0)
2247 {
2248 /* should contain no data */
2249 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2250 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
2251 return -1;
2252 }
2253 ret=1;
2254 return(ret);
2255 }
2256
2257
2258int ssl3_send_client_key_exchange(SSL *s)
2259 {
2260 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07002261 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002262 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07002263 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002264#ifndef OPENSSL_NO_RSA
2265 unsigned char *q;
2266 EVP_PKEY *pkey=NULL;
2267#endif
2268#ifndef OPENSSL_NO_ECDH
2269 EC_KEY *clnt_ecdh = NULL;
2270 const EC_POINT *srvr_ecpoint = NULL;
2271 EVP_PKEY *srvr_pub_pkey = NULL;
2272 unsigned char *encodedPoint = NULL;
2273 int encoded_pt_len = 0;
2274 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002275#ifndef OPENSSL_NO_PSK
2276 unsigned int psk_len = 0;
2277 unsigned char psk[PSK_MAX_PSK_LEN];
2278#endif /* OPENSSL_NO_PSK */
2279#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002280
2281 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2282 {
2283 p = ssl_handshake_start(s);
2284
2285 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002286 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2287
2288#ifndef OPENSSL_NO_PSK
2289 if (alg_a & SSL_aPSK)
2290 {
2291 char identity[PSK_MAX_IDENTITY_LEN];
2292 unsigned char *t = NULL;
2293 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2294 unsigned int pre_ms_len = 0;
2295 int psk_err = 1;
2296
2297 n = 0;
2298 if (s->psk_client_callback == NULL)
2299 {
2300 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2301 goto err;
2302 }
2303
2304 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2305 identity, PSK_MAX_IDENTITY_LEN, psk, sizeof(psk));
2306 if (psk_len > PSK_MAX_PSK_LEN)
2307 {
2308 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2309 goto psk_err;
2310 }
2311 else if (psk_len == 0)
2312 {
2313 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2314 goto psk_err;
2315 }
2316
2317 if (!(alg_k & SSL_kEECDH))
2318 {
2319 /* Create the shared secret now if we're not using ECDHE-PSK.*/
2320 pre_ms_len = 2+psk_len+2+psk_len;
2321 t = pre_ms;
2322 s2n(psk_len, t);
2323 memset(t, 0, psk_len);
2324 t+=psk_len;
2325 s2n(psk_len, t);
2326 memcpy(t, psk, psk_len);
2327
2328 s->session->master_key_length =
2329 s->method->ssl3_enc->generate_master_secret(s,
2330 s->session->master_key,
2331 pre_ms, pre_ms_len);
2332 n = strlen(identity);
2333 s2n(n, p);
2334 memcpy(p, identity, n);
2335 n += 2;
2336 }
2337
2338 if (s->session->psk_identity_hint != NULL)
2339 OPENSSL_free(s->session->psk_identity_hint);
2340 s->session->psk_identity_hint = NULL;
2341 if (s->ctx->psk_identity_hint)
2342 {
2343 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2344 if (s->ctx->psk_identity_hint != NULL &&
2345 s->session->psk_identity_hint == NULL)
2346 {
2347 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2348 goto psk_err;
2349 }
2350 }
2351
2352 if (s->session->psk_identity != NULL)
2353 OPENSSL_free(s->session->psk_identity);
2354 s->session->psk_identity = BUF_strdup(identity);
2355 if (s->session->psk_identity == NULL)
2356 {
2357 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2358 goto psk_err;
2359 }
2360 psk_err = 0;
2361 psk_err:
2362 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2363 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2364 if (psk_err != 0)
2365 {
2366 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2367 goto err;
2368 }
2369 }
2370#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002371
2372 /* Fool emacs indentation */
2373 if (0) {}
2374#ifndef OPENSSL_NO_RSA
2375 else if (alg_k & SSL_kRSA)
2376 {
2377 RSA *rsa;
2378 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2379
2380 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2381 rsa=s->session->sess_cert->peer_rsa_tmp;
2382 else
2383 {
2384 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2385 if ((pkey == NULL) ||
2386 (pkey->type != EVP_PKEY_RSA) ||
2387 (pkey->pkey.rsa == NULL))
2388 {
2389 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2390 goto err;
2391 }
2392 rsa=pkey->pkey.rsa;
2393 EVP_PKEY_free(pkey);
2394 }
2395
2396 tmp_buf[0]=s->client_version>>8;
2397 tmp_buf[1]=s->client_version&0xff;
2398 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2399 goto err;
2400
2401 s->session->master_key_length=sizeof tmp_buf;
2402
2403 q=p;
2404 /* Fix buf for TLS and beyond */
2405 if (s->version > SSL3_VERSION)
2406 p+=2;
2407 n=RSA_public_encrypt(sizeof tmp_buf,
2408 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2409#ifdef PKCS1_CHECK
2410 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2411 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2412#endif
2413 if (n <= 0)
2414 {
2415 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2416 goto err;
2417 }
2418
2419 /* Fix buf for TLS and beyond */
2420 if (s->version > SSL3_VERSION)
2421 {
2422 s2n(n,q);
2423 n+=2;
2424 }
2425
2426 s->session->master_key_length=
2427 s->method->ssl3_enc->generate_master_secret(s,
2428 s->session->master_key,
2429 tmp_buf,sizeof tmp_buf);
2430 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2431 }
2432#endif
2433#ifndef OPENSSL_NO_DH
2434 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2435 {
2436 DH *dh_srvr,*dh_clnt;
2437 SESS_CERT *scert = s->session->sess_cert;
2438
2439 if (scert == NULL)
2440 {
2441 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2442 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2443 goto err;
2444 }
2445
2446 if (scert->peer_dh_tmp != NULL)
2447 dh_srvr=scert->peer_dh_tmp;
2448 else
2449 {
2450 /* we get them from the cert */
2451 int idx = scert->peer_cert_type;
2452 EVP_PKEY *spkey = NULL;
2453 dh_srvr = NULL;
2454 if (idx >= 0)
2455 spkey = X509_get_pubkey(
2456 scert->peer_pkeys[idx].x509);
2457 if (spkey)
2458 {
2459 dh_srvr = EVP_PKEY_get1_DH(spkey);
2460 EVP_PKEY_free(spkey);
2461 }
2462 if (dh_srvr == NULL)
2463 {
2464 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2465 goto err;
2466 }
2467 }
2468 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2469 {
2470 /* Use client certificate key */
2471 EVP_PKEY *clkey = s->cert->key->privatekey;
2472 dh_clnt = NULL;
2473 if (clkey)
2474 dh_clnt = EVP_PKEY_get1_DH(clkey);
2475 if (dh_clnt == NULL)
2476 {
2477 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2478 goto err;
2479 }
2480 }
2481 else
2482 {
2483 /* generate a new random key */
2484 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2485 {
2486 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2487 goto err;
2488 }
2489 if (!DH_generate_key(dh_clnt))
2490 {
2491 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2492 DH_free(dh_clnt);
2493 goto err;
2494 }
2495 }
2496
2497 /* use the 'p' output buffer for the DH key, but
2498 * make sure to clear it out afterwards */
2499
2500 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2501 if (scert->peer_dh_tmp == NULL)
2502 DH_free(dh_srvr);
2503
2504 if (n <= 0)
2505 {
2506 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2507 DH_free(dh_clnt);
2508 goto err;
2509 }
2510
2511 /* generate master key from the result */
2512 s->session->master_key_length=
2513 s->method->ssl3_enc->generate_master_secret(s,
2514 s->session->master_key,p,n);
2515 /* clean up */
2516 memset(p,0,n);
2517
2518 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2519 n = 0;
2520 else
2521 {
2522 /* send off the data */
2523 n=BN_num_bytes(dh_clnt->pub_key);
2524 s2n(n,p);
2525 BN_bn2bin(dh_clnt->pub_key,p);
2526 n+=2;
2527 }
2528
2529 DH_free(dh_clnt);
2530
2531 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2532 }
2533#endif
2534
Adam Langleyc26c8022014-06-20 12:00:00 -07002535#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002536 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2537 {
2538 const EC_GROUP *srvr_group = NULL;
2539 EC_KEY *tkey;
2540 int ecdh_clnt_cert = 0;
2541 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002542#ifndef OPENSSL_NO_PSK
2543 unsigned char *pre_ms;
2544 unsigned char *t;
2545 unsigned int pre_ms_len;
2546 unsigned int i;
2547#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002548
2549 /* Did we send out the client's
2550 * ECDH share for use in premaster
2551 * computation as part of client certificate?
2552 * If so, set ecdh_clnt_cert to 1.
2553 */
2554 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2555 {
2556 /* XXX: For now, we do not support client
2557 * authentication using ECDH certificates.
2558 * To add such support, one needs to add
2559 * code that checks for appropriate
2560 * conditions and sets ecdh_clnt_cert to 1.
2561 * For example, the cert have an ECC
2562 * key on the same curve as the server's
2563 * and the key should be authorized for
2564 * key agreement.
2565 *
2566 * One also needs to add code in ssl3_connect
2567 * to skip sending the certificate verify
2568 * message.
2569 *
2570 * if ((s->cert->key->privatekey != NULL) &&
2571 * (s->cert->key->privatekey->type ==
2572 * EVP_PKEY_EC) && ...)
2573 * ecdh_clnt_cert = 1;
2574 */
2575 }
2576
2577 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2578 {
2579 tkey = s->session->sess_cert->peer_ecdh_tmp;
2580 }
2581 else
2582 {
2583 /* Get the Server Public Key from Cert */
2584 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2585 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2586 if ((srvr_pub_pkey == NULL) ||
2587 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2588 (srvr_pub_pkey->pkey.ec == NULL))
2589 {
2590 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2591 goto err;
2592 }
2593
2594 tkey = srvr_pub_pkey->pkey.ec;
2595 }
2596
2597 srvr_group = EC_KEY_get0_group(tkey);
2598 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2599
2600 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2601 {
2602 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2603 goto err;
2604 }
2605
2606 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2607 {
2608 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2609 goto err;
2610 }
2611
2612 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2613 {
2614 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2615 goto err;
2616 }
2617 if (ecdh_clnt_cert)
2618 {
2619 /* Reuse key info from our certificate
2620 * We only need our private key to perform
2621 * the ECDH computation.
2622 */
2623 const BIGNUM *priv_key;
2624 tkey = s->cert->key->privatekey->pkey.ec;
2625 priv_key = EC_KEY_get0_private_key(tkey);
2626 if (priv_key == NULL)
2627 {
2628 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2629 goto err;
2630 }
2631 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2632 {
2633 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2634 goto err;
2635 }
2636 }
2637 else
2638 {
2639 /* Generate a new ECDH key pair */
2640 if (!(EC_KEY_generate_key(clnt_ecdh)))
2641 {
2642 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2643 goto err;
2644 }
2645 }
2646
2647 /* use the 'p' output buffer for the ECDH key, but
2648 * make sure to clear it out afterwards
2649 */
2650
2651 field_size = EC_GROUP_get_degree(srvr_group);
2652 if (field_size <= 0)
2653 {
2654 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2655 goto err;
2656 }
2657 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2658 if (n <= 0)
2659 {
2660 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2661 goto err;
2662 }
2663
Adam Langleyc26c8022014-06-20 12:00:00 -07002664#ifndef OPENSSL_NO_PSK
2665 /* ECDHE PSK ciphersuites from RFC 5489 */
2666 if ((alg_a & SSL_aPSK) && psk_len != 0)
2667 {
2668 pre_ms_len = 2+psk_len+2+n;
2669 pre_ms = OPENSSL_malloc(pre_ms_len);
2670 if (pre_ms == NULL)
2671 {
2672 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2673 goto err;
2674 }
2675 memset(pre_ms, 0, pre_ms_len);
2676 t = pre_ms;
2677 s2n(psk_len, t);
2678 memcpy(t, psk, psk_len);
2679 t += psk_len;
2680 s2n(n, t);
2681 memcpy(t, p, n);
2682 s->session->master_key_length = s->method->ssl3_enc \
2683 -> generate_master_secret(s,
2684 s->session->master_key, pre_ms, pre_ms_len);
2685 OPENSSL_cleanse(pre_ms, pre_ms_len);
2686 OPENSSL_free(pre_ms);
2687 }
2688#endif /* OPENSSL_NO_PSK */
2689 if (!(alg_a & SSL_aPSK))
2690 {
2691 /* generate master key from the result */
2692 s->session->master_key_length = s->method->ssl3_enc \
2693 -> generate_master_secret(s,
2694 s->session->master_key, p, n);
2695 }
Adam Langley95c29f32014-06-20 12:00:00 -07002696 memset(p, 0, n); /* clean up */
2697
Adam Langleyc26c8022014-06-20 12:00:00 -07002698 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002699 {
2700 /* Send empty client key exch message */
2701 n = 0;
2702 }
2703 else
2704 {
2705 /* First check the size of encoding and
2706 * allocate memory accordingly.
2707 */
2708 encoded_pt_len =
2709 EC_POINT_point2oct(srvr_group,
2710 EC_KEY_get0_public_key(clnt_ecdh),
2711 POINT_CONVERSION_UNCOMPRESSED,
2712 NULL, 0, NULL);
2713
2714 encodedPoint = (unsigned char *)
2715 OPENSSL_malloc(encoded_pt_len *
2716 sizeof(unsigned char));
2717 bn_ctx = BN_CTX_new();
2718 if ((encodedPoint == NULL) ||
2719 (bn_ctx == NULL))
2720 {
2721 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2722 goto err;
2723 }
2724
2725 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002726 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2727 EC_KEY_get0_public_key(clnt_ecdh),
2728 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002729 encodedPoint, encoded_pt_len, bn_ctx);
2730
Adam Langleyc26c8022014-06-20 12:00:00 -07002731 n = 0;
2732#ifndef OPENSSL_NO_PSK
2733 if ((alg_a & SSL_aPSK) && psk_len != 0)
2734 {
2735 i = strlen(s->session->psk_identity);
2736 s2n(i, p);
2737 memcpy(p, s->session->psk_identity, i);
2738 p += i;
2739 n = i + 2;
2740 }
2741#endif
2742
2743 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002744 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002745 p += 1;
2746 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002747 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002748 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002749 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002750 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002751 }
2752
2753 /* Free allocated memory */
2754 BN_CTX_free(bn_ctx);
2755 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002756 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002757 EC_KEY_free(clnt_ecdh);
2758 EVP_PKEY_free(srvr_pub_pkey);
2759 }
2760#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002761 else if (alg_k & SSL_kGOST)
Adam Langley95c29f32014-06-20 12:00:00 -07002762 {
2763 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED);
2764 goto err;
2765 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002766 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002767 {
2768 ssl3_send_alert(s, SSL3_AL_FATAL,
2769 SSL_AD_HANDSHAKE_FAILURE);
2770 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2771 goto err;
2772 }
2773
2774 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2775 s->state=SSL3_ST_CW_KEY_EXCH_B;
2776 }
2777
2778 /* SSL3_ST_CW_KEY_EXCH_B */
2779 return ssl_do_write(s);
2780err:
2781#ifndef OPENSSL_NO_ECDH
2782 BN_CTX_free(bn_ctx);
2783 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2784 if (clnt_ecdh != NULL)
2785 EC_KEY_free(clnt_ecdh);
2786 EVP_PKEY_free(srvr_pub_pkey);
2787#endif
2788 return(-1);
2789 }
2790
2791int ssl3_send_client_verify(SSL *s)
2792 {
2793 unsigned char *p;
2794 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
2795 EVP_PKEY *pkey;
2796 EVP_PKEY_CTX *pctx=NULL;
2797 EVP_MD_CTX mctx;
2798 unsigned u=0;
2799 unsigned long n;
2800 int j;
2801
2802 EVP_MD_CTX_init(&mctx);
2803
2804 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2805 {
2806 p= ssl_handshake_start(s);
2807 pkey=s->cert->key->privatekey;
2808/* Create context from key and test if sha1 is allowed as digest */
2809 pctx = EVP_PKEY_CTX_new(pkey,NULL);
2810 EVP_PKEY_sign_init(pctx);
2811 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
2812 {
2813 if (!SSL_USE_SIGALGS(s))
2814 s->method->ssl3_enc->cert_verify_mac(s,
2815 NID_sha1,
2816 &(data[MD5_DIGEST_LENGTH]));
2817 }
2818 else
2819 {
2820 ERR_clear_error();
2821 }
2822 /* For TLS v1.2 send signature algorithm and signature
2823 * using agreed digest and cached handshake records.
2824 */
2825 if (SSL_USE_SIGALGS(s))
2826 {
2827 long hdatalen = 0;
2828 char *hdata;
2829 const EVP_MD *md = s->cert->key->digest;
2830 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2831 &hdata);
2832 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
2833 {
2834 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2835 goto err;
2836 }
2837 p += 2;
2838#ifdef SSL_DEBUG
2839 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2840 EVP_MD_name(md));
2841#endif
2842 if (!EVP_SignInit_ex(&mctx, md, NULL)
2843 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2844 || !EVP_SignFinal(&mctx, p + 2, &u, pkey))
2845 {
2846 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2847 goto err;
2848 }
2849 s2n(u,p);
2850 n = u + 4;
2851 if (!ssl3_digest_cached_records(s))
2852 goto err;
2853 }
2854 else
2855#ifndef OPENSSL_NO_RSA
2856 if (pkey->type == EVP_PKEY_RSA)
2857 {
2858 s->method->ssl3_enc->cert_verify_mac(s,
2859 NID_md5,
2860 &(data[0]));
2861 if (RSA_sign(NID_md5_sha1, data,
2862 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2863 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
2864 {
2865 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_RSA_LIB);
2866 goto err;
2867 }
2868 s2n(u,p);
2869 n=u+2;
2870 }
2871 else
2872#endif
2873#ifndef OPENSSL_NO_DSA
2874 if (pkey->type == EVP_PKEY_DSA)
2875 {
2876 if (!DSA_sign(pkey->save_type,
2877 &(data[MD5_DIGEST_LENGTH]),
2878 SHA_DIGEST_LENGTH,&(p[2]),
2879 (unsigned int *)&j,pkey->pkey.dsa))
2880 {
2881 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_DSA_LIB);
2882 goto err;
2883 }
2884 s2n(j,p);
2885 n=j+2;
2886 }
2887 else
2888#endif
2889#ifndef OPENSSL_NO_ECDSA
2890 if (pkey->type == EVP_PKEY_EC)
2891 {
2892 if (!ECDSA_sign(pkey->save_type,
2893 &(data[MD5_DIGEST_LENGTH]),
2894 SHA_DIGEST_LENGTH,&(p[2]),
2895 (unsigned int *)&j,pkey->pkey.ec))
2896 {
2897 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_ECDSA_LIB);
2898 goto err;
2899 }
2900 s2n(j,p);
2901 n=j+2;
2902 }
2903 else
2904#endif
2905 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
2906 {
2907 unsigned char signbuf[64];
2908 int i;
2909 size_t sigsize=64;
2910 s->method->ssl3_enc->cert_verify_mac(s,
2911 NID_id_GostR3411_94,
2912 data);
2913 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2914 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2915 goto err;
2916 }
2917 for (i=63,j=0; i>=0; j++, i--) {
2918 p[2+j]=signbuf[i];
2919 }
2920 s2n(j,p);
2921 n=j+2;
2922 }
2923 else
2924 {
2925 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2926 goto err;
2927 }
2928 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2929 s->state=SSL3_ST_CW_CERT_VRFY_B;
2930 }
2931 EVP_MD_CTX_cleanup(&mctx);
2932 EVP_PKEY_CTX_free(pctx);
2933 return ssl_do_write(s);
2934err:
2935 EVP_MD_CTX_cleanup(&mctx);
2936 EVP_PKEY_CTX_free(pctx);
2937 return(-1);
2938 }
2939
2940/* Check a certificate can be used for client authentication. Currently
2941 * check cert exists, if we have a suitable digest for TLS 1.2 if
2942 * static DH client certificates can be used and optionally checks
2943 * suitability for Suite B.
2944 */
2945static int ssl3_check_client_certificate(SSL *s)
2946 {
2947 unsigned long alg_k;
2948 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2949 return 0;
2950 /* If no suitable signature algorithm can't use certificate */
2951 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2952 return 0;
2953 /* If strict mode check suitability of chain before using it.
2954 * This also adjusts suite B digest if necessary.
2955 */
2956 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2957 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2958 return 0;
2959 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2960 /* See if we can use client certificate for fixed DH */
2961 if (alg_k & (SSL_kDHr|SSL_kDHd))
2962 {
2963 SESS_CERT *scert = s->session->sess_cert;
2964 int i = scert->peer_cert_type;
2965 EVP_PKEY *clkey = NULL, *spkey = NULL;
2966 clkey = s->cert->key->privatekey;
2967 /* If client key not DH assume it can be used */
2968 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2969 return 1;
2970 if (i >= 0)
2971 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2972 if (spkey)
2973 {
2974 /* Compare server and client parameters */
2975 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2976 EVP_PKEY_free(spkey);
2977 if (i != 1)
2978 return 0;
2979 }
2980 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2981 }
2982 return 1;
2983 }
2984
2985int ssl3_send_client_certificate(SSL *s)
2986 {
2987 X509 *x509=NULL;
2988 EVP_PKEY *pkey=NULL;
2989 int i;
2990
2991 if (s->state == SSL3_ST_CW_CERT_A)
2992 {
2993 /* Let cert callback update client certificates if required */
2994 if (s->cert->cert_cb)
2995 {
2996 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2997 if (i < 0)
2998 {
2999 s->rwstate=SSL_X509_LOOKUP;
3000 return -1;
3001 }
3002 if (i == 0)
3003 {
3004 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
3005 return 0;
3006 }
3007 s->rwstate=SSL_NOTHING;
3008 }
3009 if (ssl3_check_client_certificate(s))
3010 s->state=SSL3_ST_CW_CERT_C;
3011 else
3012 s->state=SSL3_ST_CW_CERT_B;
3013 }
3014
3015 /* We need to get a client cert */
3016 if (s->state == SSL3_ST_CW_CERT_B)
3017 {
3018 /* If we get an error, we need to
3019 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
3020 * We then get retied later */
3021 i=0;
3022 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3023 if (i < 0)
3024 {
3025 s->rwstate=SSL_X509_LOOKUP;
3026 return(-1);
3027 }
3028 s->rwstate=SSL_NOTHING;
3029 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
3030 {
3031 s->state=SSL3_ST_CW_CERT_B;
3032 if ( !SSL_use_certificate(s,x509) ||
3033 !SSL_use_PrivateKey(s,pkey))
3034 i=0;
3035 }
3036 else if (i == 1)
3037 {
3038 i=0;
3039 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3040 }
3041
3042 if (x509 != NULL) X509_free(x509);
3043 if (pkey != NULL) EVP_PKEY_free(pkey);
3044 if (i && !ssl3_check_client_certificate(s))
3045 i = 0;
3046 if (i == 0)
3047 {
3048 if (s->version == SSL3_VERSION)
3049 {
3050 s->s3->tmp.cert_req=0;
3051 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
3052 return(1);
3053 }
3054 else
3055 {
3056 s->s3->tmp.cert_req=2;
3057 }
3058 }
3059
3060 /* Ok, we have a cert */
3061 s->state=SSL3_ST_CW_CERT_C;
3062 }
3063
3064 if (s->state == SSL3_ST_CW_CERT_C)
3065 {
3066 s->state=SSL3_ST_CW_CERT_D;
3067 ssl3_output_cert_chain(s,
3068 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
3069 }
3070 /* SSL3_ST_CW_CERT_D */
3071 return ssl_do_write(s);
3072 }
3073
3074#define has_bits(i,m) (((i)&(m)) == (m))
3075
3076int ssl3_check_cert_and_algorithm(SSL *s)
3077 {
3078 int i,idx;
3079 long alg_k,alg_a;
3080 EVP_PKEY *pkey=NULL;
3081 SESS_CERT *sc;
3082#ifndef OPENSSL_NO_RSA
3083 RSA *rsa;
3084#endif
3085#ifndef OPENSSL_NO_DH
3086 DH *dh;
3087#endif
3088
3089 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
3090 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
3091
3092 /* we don't have a certificate */
Adam Langleyc26c8022014-06-20 12:00:00 -07003093 if ((alg_a & (SSL_aNULL|SSL_aKRB5)) || ((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07003094 return(1);
3095
3096 sc=s->session->sess_cert;
3097 if (sc == NULL)
3098 {
3099 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
3100 goto err;
3101 }
3102
3103#ifndef OPENSSL_NO_RSA
3104 rsa=s->session->sess_cert->peer_rsa_tmp;
3105#endif
3106#ifndef OPENSSL_NO_DH
3107 dh=s->session->sess_cert->peer_dh_tmp;
3108#endif
3109
3110 /* This is the passed certificate */
3111
3112 idx=sc->peer_cert_type;
3113#ifndef OPENSSL_NO_ECDH
3114 if (idx == SSL_PKEY_ECC)
3115 {
3116 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
3117 s) == 0)
3118 { /* check failed */
3119 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
3120 goto f_err;
3121 }
3122 else
3123 {
3124 return 1;
3125 }
3126 }
3127 else if (alg_a & SSL_aECDSA)
3128 {
3129 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
3130 goto f_err;
3131 }
3132 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
3133 {
3134 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
3135 goto f_err;
3136 }
3137#endif
3138 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
3139 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
3140 EVP_PKEY_free(pkey);
3141
3142
3143 /* Check that we have a certificate if we require one */
3144 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
3145 {
3146 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
3147 goto f_err;
3148 }
3149#ifndef OPENSSL_NO_DSA
3150 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
3151 {
3152 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
3153 goto f_err;
3154 }
3155#endif
3156#ifndef OPENSSL_NO_RSA
3157 if ((alg_k & SSL_kRSA) &&
3158 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
3159 {
3160 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3161 goto f_err;
3162 }
3163#endif
3164#ifndef OPENSSL_NO_DH
3165 if ((alg_k & SSL_kEDH) &&
3166 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
3167 {
3168 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
3169 goto f_err;
3170 }
3171 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3172 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
3173 {
3174 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
3175 goto f_err;
3176 }
3177#ifndef OPENSSL_NO_DSA
3178 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3179 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
3180 {
3181 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
3182 goto f_err;
3183 }
3184#endif
3185#endif
3186
3187 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
3188 {
3189#ifndef OPENSSL_NO_RSA
3190 if (alg_k & SSL_kRSA)
3191 {
3192 if (rsa == NULL
3193 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3194 {
3195 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3196 goto f_err;
3197 }
3198 }
3199 else
3200#endif
3201#ifndef OPENSSL_NO_DH
3202 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
3203 {
3204 if (dh == NULL
3205 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3206 {
3207 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3208 goto f_err;
3209 }
3210 }
3211 else
3212#endif
3213 {
3214 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3215 goto f_err;
3216 }
3217 }
3218 return(1);
3219f_err:
3220 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3221err:
3222 return(0);
3223 }
3224
Adam Langley1258b6a2014-06-20 12:00:00 -07003225#if !defined(OPENSSL_NO_TLSEXT)
3226# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07003227int ssl3_send_next_proto(SSL *s)
3228 {
3229 unsigned int len, padding_len;
3230 unsigned char *d;
3231
3232 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3233 {
3234 len = s->next_proto_negotiated_len;
3235 padding_len = 32 - ((len + 2) % 32);
3236 d = (unsigned char *)s->init_buf->data;
3237 d[4] = len;
3238 memcpy(d + 5, s->next_proto_negotiated, len);
3239 d[5 + len] = padding_len;
3240 memset(d + 6 + len, 0, padding_len);
3241 *(d++)=SSL3_MT_NEXT_PROTO;
3242 l2n3(2 + len + padding_len, d);
3243 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3244 s->init_num = 4 + 2 + len + padding_len;
3245 s->init_off = 0;
3246 }
3247
3248 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3249}
Adam Langley1258b6a2014-06-20 12:00:00 -07003250
3251# endif /* !OPENSSL_NO_NEXTPROTONEG */
3252
3253int ssl3_send_channel_id(SSL *s)
3254 {
3255 unsigned char *d;
3256 int ret = -1, public_key_len;
3257 EVP_MD_CTX md_ctx;
3258 size_t sig_len;
3259 ECDSA_SIG *sig = NULL;
3260 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3261
3262 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3263 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3264
3265 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3266 {
3267 EVP_PKEY *key = NULL;
3268 s->ctx->channel_id_cb(s, &key);
3269 if (key != NULL)
3270 {
3271 s->tlsext_channel_id_private = key;
3272 }
3273 }
3274 if (!s->tlsext_channel_id_private)
3275 {
3276 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3277 return (-1);
3278 }
3279 s->rwstate=SSL_NOTHING;
3280
3281 d = (unsigned char *)s->init_buf->data;
3282 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3283 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3284 if (s->s3->tlsext_channel_id_new)
3285 s2n(TLSEXT_TYPE_channel_id_new, d);
3286 else
3287 s2n(TLSEXT_TYPE_channel_id, d);
3288 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3289
3290 EVP_MD_CTX_init(&md_ctx);
3291
3292 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3293 if (public_key_len <= 0)
3294 {
3295 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
3296 goto err;
3297 }
3298 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3299 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3300 * field elements as 32-byte, big-endian numbers. */
3301 if (public_key_len != 65)
3302 {
3303 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
3304 goto err;
3305 }
3306 public_key = OPENSSL_malloc(public_key_len);
3307 if (!public_key)
3308 {
3309 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3310 goto err;
3311 }
3312
3313 derp = public_key;
3314 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3315
3316 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3317 s->tlsext_channel_id_private) != 1)
3318 {
3319 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
3320 goto err;
3321 }
3322
3323 if (!tls1_channel_id_hash(&md_ctx, s))
3324 goto err;
3325
3326 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3327 {
3328 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3329 goto err;
3330 }
3331
3332 der_sig = OPENSSL_malloc(sig_len);
3333 if (!der_sig)
3334 {
3335 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3336 goto err;
3337 }
3338
3339 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3340 {
3341 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3342 goto err;
3343 }
3344
3345 derp = der_sig;
3346 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
3347 if (sig == NULL)
3348 {
3349 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
3350 goto err;
3351 }
3352
3353 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3354 memcpy(d, public_key + 1, 64);
3355 d += 64;
3356 memset(d, 0, 2 * 32);
3357 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3358 d += 32;
3359 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3360 d += 32;
3361
3362 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3363 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3364 s->init_off = 0;
3365
3366 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3367
3368err:
3369 EVP_MD_CTX_cleanup(&md_ctx);
3370 if (public_key)
3371 OPENSSL_free(public_key);
3372 if (der_sig)
3373 OPENSSL_free(der_sig);
3374 if (sig)
3375 ECDSA_SIG_free(sig);
3376
3377 return ret;
3378 }
3379#endif /* !OPENSSL_NO_TLSEXT */
Adam Langley95c29f32014-06-20 12:00:00 -07003380
3381/* Check to see if handshake is full or resumed. Usually this is just a
3382 * case of checking to see if a cache hit has occurred. In the case of
3383 * session tickets we have to check the next message to be sure.
3384 */
3385
3386#ifndef OPENSSL_NO_TLSEXT
3387int ssl3_check_finished(SSL *s)
3388 {
3389 int ok;
3390 long n;
3391 /* If we have no ticket it cannot be a resumed session. */
3392 if (!s->session->tlsext_tick)
3393 return 1;
3394 /* this function is called when we really expect a Certificate
3395 * message, so permit appropriate message length */
3396 n=s->method->ssl_get_message(s,
3397 SSL3_ST_CR_CERT_A,
3398 SSL3_ST_CR_CERT_B,
3399 -1,
3400 s->max_cert_list,
3401 &ok);
3402 if (!ok) return((int)n);
3403 s->s3->tmp.reuse_message = 1;
3404 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3405 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3406 return 2;
3407
3408 return 1;
3409 }
3410#endif
3411
3412int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3413 {
3414 int i = 0;
3415 /* TODO(fork): remove */
3416#if 0
3417#ifndef OPENSSL_NO_ENGINE
3418 if (s->ctx->client_cert_engine)
3419 {
3420 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3421 SSL_get_client_CA_list(s),
3422 px509, ppkey, NULL, NULL, NULL);
3423 if (i != 0)
3424 return i;
3425 }
3426#endif
3427#endif
3428 if (s->ctx->client_cert_cb)
3429 i = s->ctx->client_cert_cb(s,px509,ppkey);
3430 return i;
3431 }
3432
3433#ifndef OPENSSL_NO_TLSEXT
3434int tls1_get_server_supplemental_data(SSL *s)
3435 {
3436 int al;
3437 int ok;
3438 unsigned long supp_data_len, authz_data_len;
3439 long n;
3440 unsigned short supp_data_type, authz_data_type, proof_len;
3441 const unsigned char *p;
3442 unsigned char *new_proof;
3443
3444 n=s->method->ssl_get_message(s,
3445 SSL3_ST_CR_SUPPLEMENTAL_DATA_A,
3446 SSL3_ST_CR_SUPPLEMENTAL_DATA_B,
3447 SSL3_MT_SUPPLEMENTAL_DATA,
3448 /* use default limit */
3449 TLSEXT_MAXLEN_supplemental_data,
3450 &ok);
3451
3452 if (!ok) return((int)n);
3453
3454 p = (unsigned char *)s->init_msg;
3455
3456 /* The message cannot be empty */
3457 if (n < 3)
3458 {
3459 al = SSL_AD_DECODE_ERROR;
3460 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH);
3461 goto f_err;
3462 }
3463 /* Length of supplemental data */
3464 n2l3(p,supp_data_len);
3465 n -= 3;
3466 /* We must have at least one supplemental data entry
3467 * with type (1 byte) and length (2 bytes). */
3468 if (supp_data_len != (unsigned long) n || n < 4)
3469 {
3470 al = SSL_AD_DECODE_ERROR;
3471 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH);
3472 goto f_err;
3473 }
3474 /* Supplemental data type: must be authz_data */
3475 n2s(p,supp_data_type);
3476 n -= 2;
3477 if (supp_data_type != TLSEXT_SUPPLEMENTALDATATYPE_authz_data)
3478 {
3479 al = SSL_AD_UNEXPECTED_MESSAGE;
3480 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_SUPPLEMENTAL_DATA_TYPE);
3481 goto f_err;
3482 }
3483 /* Authz data length */
3484 n2s(p, authz_data_len);
3485 n -= 2;
3486 if (authz_data_len != (unsigned long) n || n < 1)
3487 {
3488 al = SSL_AD_DECODE_ERROR;
3489 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH);
3490 goto f_err;
3491 }
3492 /* Authz data type: must be audit_proof */
3493 authz_data_type = *(p++);
3494 n -= 1;
3495 if (authz_data_type != TLSEXT_AUTHZDATAFORMAT_audit_proof)
3496 {
3497 al=SSL_AD_UNEXPECTED_MESSAGE;
3498 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_AUTHZ_DATA_TYPE);
3499 goto f_err;
3500 }
3501 /* We have a proof: read its length */
3502 if (n < 2)
3503 {
3504 al = SSL_AD_DECODE_ERROR;
3505 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH);
3506 goto f_err;
3507 }
3508 n2s(p, proof_len);
3509 n -= 2;
3510 if (proof_len != (unsigned long) n)
3511 {
3512 al = SSL_AD_DECODE_ERROR;
3513 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH);
3514 goto f_err;
3515 }
3516 /* Store the proof */
3517 new_proof = OPENSSL_realloc(s->session->audit_proof,
3518 proof_len);
3519 if (new_proof == NULL)
3520 {
3521 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
3522 return 0;
3523 }
3524 s->session->audit_proof_length = proof_len;
3525 s->session->audit_proof = new_proof;
3526 memcpy(s->session->audit_proof, p, proof_len);
3527
3528 /* Got the proof, but can't verify it yet. */
3529 return 1;
3530f_err:
3531 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3532 return -1;
3533 }
3534#endif