blob: a28fd33dd94c54407c7eb519b0a12f907f1bb536 [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>
David Benjamin03973092014-06-24 23:27:17 -0400154#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155#include <openssl/rand.h>
156#include <openssl/obj.h>
157#include <openssl/evp.h>
158#include <openssl/mem.h>
159#include <openssl/md5.h>
160#include <openssl/dh.h>
161#include <openssl/bn.h>
162#include <openssl/engine.h>
163#include <openssl/x509.h>
164
165#include "ssl_locl.h"
166#include "../crypto/dh/internal.h"
167
168static const SSL_METHOD *ssl3_get_client_method(int ver);
169
170static const SSL_METHOD *ssl3_get_client_method(int ver)
171 {
172 if (ver == SSL3_VERSION)
173 return(SSLv3_client_method());
174 else
175 return(NULL);
176 }
177
178IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
179 ssl_undefined_function,
180 ssl3_connect,
181 ssl3_get_client_method)
182
183int ssl3_connect(SSL *s)
184 {
185 BUF_MEM *buf=NULL;
186 void (*cb)(const SSL *ssl,int type,int val)=NULL;
187 int ret= -1;
188 int new_state,state,skip=0;
189
190 ERR_clear_error();
191 ERR_clear_system_error();
192
193 if (s->info_callback != NULL)
194 cb=s->info_callback;
195 else if (s->ctx->info_callback != NULL)
196 cb=s->ctx->info_callback;
197
198 s->in_handshake++;
199 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
200
Adam Langleyadb739e2014-06-20 12:00:00 -0700201 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
202 {
203 /* Send app data along with CCS/Finished */
204 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED;
205 }
206
Adam Langley95c29f32014-06-20 12:00:00 -0700207 for (;;)
208 {
209 state=s->state;
210
211 switch(s->state)
212 {
213 case SSL_ST_RENEGOTIATE:
214 s->renegotiate=1;
215 s->state=SSL_ST_CONNECT;
216 s->ctx->stats.sess_connect_renegotiate++;
217 /* break */
218 case SSL_ST_BEFORE:
219 case SSL_ST_CONNECT:
220 case SSL_ST_BEFORE|SSL_ST_CONNECT:
221 case SSL_ST_OK|SSL_ST_CONNECT:
222
223 s->server=0;
224 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
225
226 if ((s->version & 0xff00 ) != 0x0300)
227 {
228 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
229 ret = -1;
230 goto end;
231 }
232
233 /* s->version=SSL3_VERSION; */
234 s->type=SSL_ST_CONNECT;
235
236 if (s->init_buf == NULL)
237 {
238 if ((buf=BUF_MEM_new()) == NULL)
239 {
240 ret= -1;
241 goto end;
242 }
243 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
244 {
245 ret= -1;
246 goto end;
247 }
248 s->init_buf=buf;
249 buf=NULL;
250 }
251
252 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
253
254 /* setup buffing BIO */
255 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
256
257 /* don't push the buffering BIO quite yet */
258
259 ssl3_init_finished_mac(s);
260
261 s->state=SSL3_ST_CW_CLNT_HELLO_A;
262 s->ctx->stats.sess_connect++;
263 s->init_num=0;
264 break;
265
266 case SSL3_ST_CW_CLNT_HELLO_A:
267 case SSL3_ST_CW_CLNT_HELLO_B:
268
269 s->shutdown=0;
270 ret=ssl3_client_hello(s);
271 if (ret <= 0) goto end;
272 s->state=SSL3_ST_CR_SRVR_HELLO_A;
273 s->init_num=0;
274
275 /* turn on buffering for the next lot of output */
276 if (s->bbio != s->wbio)
277 s->wbio=BIO_push(s->bbio,s->wbio);
278
279 break;
280
281 case SSL3_ST_CR_SRVR_HELLO_A:
282 case SSL3_ST_CR_SRVR_HELLO_B:
283 ret=ssl3_get_server_hello(s);
284 if (ret <= 0) goto end;
285
286 if (s->hit)
287 {
288 s->state=SSL3_ST_CR_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700289 if (s->tlsext_ticket_expected)
290 {
291 /* receive renewed session ticket */
292 s->state=SSL3_ST_CR_SESSION_TICKET_A;
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294 }
295 else
296 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400297 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700298 }
299 s->init_num=0;
300 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400301
Adam Langley95c29f32014-06-20 12:00:00 -0700302 case SSL3_ST_CR_CERT_A:
303 case SSL3_ST_CR_CERT_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700304 ret=ssl3_check_finished(s);
305 if (ret <= 0) goto end;
306 if (ret == 2)
307 {
308 s->hit = 1;
309 if (s->tlsext_ticket_expected)
310 s->state=SSL3_ST_CR_SESSION_TICKET_A;
311 else
312 s->state=SSL3_ST_CR_FINISHED_A;
313 s->init_num=0;
314 break;
315 }
David Benjamine8f3d662014-07-12 01:10:19 -0400316 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700317 {
318 ret=ssl3_get_server_certificate(s);
319 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700320 if (s->tlsext_status_expected)
321 s->state=SSL3_ST_CR_CERT_STATUS_A;
322 else
323 s->state=SSL3_ST_CR_KEY_EXCH_A;
324 }
325 else
326 {
327 skip = 1;
328 s->state=SSL3_ST_CR_KEY_EXCH_A;
329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330 s->init_num=0;
331 break;
332
333 case SSL3_ST_CR_KEY_EXCH_A:
334 case SSL3_ST_CR_KEY_EXCH_B:
335 ret=ssl3_get_key_exchange(s);
336 if (ret <= 0) goto end;
337 s->state=SSL3_ST_CR_CERT_REQ_A;
338 s->init_num=0;
339
340 /* at this point we check that we have the
341 * required stuff from the server */
342 if (!ssl3_check_cert_and_algorithm(s))
343 {
344 ret= -1;
345 goto end;
346 }
347 break;
348
349 case SSL3_ST_CR_CERT_REQ_A:
350 case SSL3_ST_CR_CERT_REQ_B:
351 ret=ssl3_get_certificate_request(s);
352 if (ret <= 0) goto end;
353 s->state=SSL3_ST_CR_SRVR_DONE_A;
354 s->init_num=0;
355 break;
356
357 case SSL3_ST_CR_SRVR_DONE_A:
358 case SSL3_ST_CR_SRVR_DONE_B:
359 ret=ssl3_get_server_done(s);
360 if (ret <= 0) goto end;
361 if (s->s3->tmp.cert_req)
362 s->state=SSL3_ST_CW_CERT_A;
363 else
364 s->state=SSL3_ST_CW_KEY_EXCH_A;
365 s->init_num=0;
366
367 break;
368
369 case SSL3_ST_CW_CERT_A:
370 case SSL3_ST_CW_CERT_B:
371 case SSL3_ST_CW_CERT_C:
372 case SSL3_ST_CW_CERT_D:
373 ret=ssl3_send_client_certificate(s);
374 if (ret <= 0) goto end;
375 s->state=SSL3_ST_CW_KEY_EXCH_A;
376 s->init_num=0;
377 break;
378
379 case SSL3_ST_CW_KEY_EXCH_A:
380 case SSL3_ST_CW_KEY_EXCH_B:
381 ret=ssl3_send_client_key_exchange(s);
382 if (ret <= 0) goto end;
383 /* EAY EAY EAY need to check for DH fix cert
384 * sent back */
385 /* For TLS, cert_req is set to 2, so a cert chain
386 * of nothing is sent, but no verify packet is sent */
387 /* XXX: For now, we do not support client
388 * authentication in ECDH cipher suites with
389 * ECDH (rather than ECDSA) certificates.
390 * We need to skip the certificate verify
391 * message when client's ECDH public key is sent
392 * inside the client certificate.
393 */
394 if (s->s3->tmp.cert_req == 1)
395 {
396 s->state=SSL3_ST_CW_CERT_VRFY_A;
397 }
398 else
399 {
400 s->state=SSL3_ST_CW_CHANGE_A;
401 s->s3->change_cipher_spec=0;
402 }
403 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
404 {
405 s->state=SSL3_ST_CW_CHANGE_A;
406 s->s3->change_cipher_spec=0;
407 }
408
409 s->init_num=0;
410 break;
411
412 case SSL3_ST_CW_CERT_VRFY_A:
413 case SSL3_ST_CW_CERT_VRFY_B:
414 ret=ssl3_send_client_verify(s);
415 if (ret <= 0) goto end;
416 s->state=SSL3_ST_CW_CHANGE_A;
417 s->init_num=0;
418 s->s3->change_cipher_spec=0;
419 break;
420
421 case SSL3_ST_CW_CHANGE_A:
422 case SSL3_ST_CW_CHANGE_B:
423 ret=ssl3_send_change_cipher_spec(s,
424 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
425 if (ret <= 0) goto end;
426
Adam Langley1258b6a2014-06-20 12:00:00 -0700427 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700428 if (s->s3->tlsext_channel_id_valid)
429 s->state=SSL3_ST_CW_CHANNEL_ID_A;
430# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700431 if (s->s3->next_proto_neg_seen)
432 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700433# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700434 s->init_num=0;
435
436 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700437 if (!s->method->ssl3_enc->setup_key_block(s))
438 {
439 ret= -1;
440 goto end;
441 }
442
443 if (!s->method->ssl3_enc->change_cipher_state(s,
444 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
445 {
446 ret= -1;
447 goto end;
448 }
449
450 break;
451
David Benjamin6dbd73d2014-07-03 15:59:49 -0400452#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700453 case SSL3_ST_CW_NEXT_PROTO_A:
454 case SSL3_ST_CW_NEXT_PROTO_B:
455 ret=ssl3_send_next_proto(s);
456 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700457 if (s->s3->tlsext_channel_id_valid)
458 s->state=SSL3_ST_CW_CHANNEL_ID_A;
459 else
460 s->state=SSL3_ST_CW_FINISHED_A;
461 break;
462#endif
463
Adam Langley1258b6a2014-06-20 12:00:00 -0700464 case SSL3_ST_CW_CHANNEL_ID_A:
465 case SSL3_ST_CW_CHANNEL_ID_B:
466 ret=ssl3_send_channel_id(s);
467 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700468 s->state=SSL3_ST_CW_FINISHED_A;
469 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700470
471 case SSL3_ST_CW_FINISHED_A:
472 case SSL3_ST_CW_FINISHED_B:
473 ret=ssl3_send_finished(s,
474 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
475 s->method->ssl3_enc->client_finished_label,
476 s->method->ssl3_enc->client_finished_label_len);
477 if (ret <= 0) goto end;
Adam Langleya433cbd2014-06-20 12:00:00 -0700478 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700479 s->state=SSL3_ST_CW_FLUSH;
480
481 /* clear flags */
482 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
483 if (s->hit)
484 {
485 s->s3->tmp.next_state=SSL_ST_OK;
486 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
487 {
488 s->state=SSL_ST_OK;
489 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
490 s->s3->delay_buf_pop_ret=0;
491 }
492 }
493 else
494 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700495 /* This is a non-resumption handshake. If it
496 * involves ChannelID, then record the
497 * handshake hashes at this point in the
498 * session so that any resumption of this
499 * session with ChannelID can sign those
500 * hashes. */
501 if (s->s3->tlsext_channel_id_new)
502 {
503 ret = tls1_record_handshake_hashes_for_channel_id(s);
504 if (ret <= 0)
505 goto end;
506 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700507 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
508 && ssl3_can_cutthrough(s)
509 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
510 )
511 {
512 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
513 {
514 s->state=SSL3_ST_CUTTHROUGH_COMPLETE;
515 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
516 s->s3->delay_buf_pop_ret=0;
517 }
518 else
519 {
520 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
521 }
522 }
Adam Langley95c29f32014-06-20 12:00:00 -0700523 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700524 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700525 /* Allow NewSessionTicket if ticket expected */
526 if (s->tlsext_ticket_expected)
527 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
528 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700529 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531 }
532 s->init_num=0;
533 break;
534
Adam Langley95c29f32014-06-20 12:00:00 -0700535 case SSL3_ST_CR_SESSION_TICKET_A:
536 case SSL3_ST_CR_SESSION_TICKET_B:
537 ret=ssl3_get_new_session_ticket(s);
538 if (ret <= 0) goto end;
539 s->state=SSL3_ST_CR_FINISHED_A;
540 s->init_num=0;
541 break;
542
543 case SSL3_ST_CR_CERT_STATUS_A:
544 case SSL3_ST_CR_CERT_STATUS_B:
545 ret=ssl3_get_cert_status(s);
546 if (ret <= 0) goto end;
547 s->state=SSL3_ST_CR_KEY_EXCH_A;
548 s->init_num=0;
549 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
551 case SSL3_ST_CR_FINISHED_A:
552 case SSL3_ST_CR_FINISHED_B:
553
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700554 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700555 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
556 SSL3_ST_CR_FINISHED_B);
557 if (ret <= 0) goto end;
558
559 if (s->hit)
560 s->state=SSL3_ST_CW_CHANGE_A;
561 else
562 s->state=SSL_ST_OK;
563 s->init_num=0;
564 break;
565
566 case SSL3_ST_CW_FLUSH:
567 s->rwstate=SSL_WRITING;
568 if (BIO_flush(s->wbio) <= 0)
569 {
570 ret= -1;
571 goto end;
572 }
573 s->rwstate=SSL_NOTHING;
574 s->state=s->s3->tmp.next_state;
575 break;
576
Adam Langleyadb739e2014-06-20 12:00:00 -0700577 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700578 /* Allow NewSessionTicket if ticket expected */
579 if (s->tlsext_ticket_expected)
580 s->state=SSL3_ST_CR_SESSION_TICKET_A;
581 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700582 s->state=SSL3_ST_CR_FINISHED_A;
583
584 /* SSL_write() will take care of flushing buffered data if
585 * DELAY_CLIENT_FINISHED is set.
586 */
587 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED))
588 ssl_free_wbio_buffer(s);
589 ret = 1;
590 goto end;
591 /* break; */
592
Adam Langley95c29f32014-06-20 12:00:00 -0700593 case SSL_ST_OK:
594 /* clean a few things up */
595 ssl3_cleanup_key_block(s);
596
597 if (s->init_buf != NULL)
598 {
599 BUF_MEM_free(s->init_buf);
600 s->init_buf=NULL;
601 }
602
603 /* If we are not 'joining' the last two packets,
604 * remove the buffering now */
605 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
606 ssl_free_wbio_buffer(s);
607 /* else do it later in ssl3_write */
608
609 s->init_num=0;
610 s->renegotiate=0;
611 s->new_session=0;
612
613 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
614 if (s->hit) s->ctx->stats.sess_hit++;
615
616 ret=1;
617 /* s->server=0; */
618 s->handshake_func=ssl3_connect;
619 s->ctx->stats.sess_connect_good++;
620
621 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
622
623 goto end;
624 /* break; */
625
626 default:
627 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
628 ret= -1;
629 goto end;
630 /* break; */
631 }
632
633 /* did we do anything */
634 if (!s->s3->tmp.reuse_message && !skip)
635 {
636 if (s->debug)
637 {
638 if ((ret=BIO_flush(s->wbio)) <= 0)
639 goto end;
640 }
641
642 if ((cb != NULL) && (s->state != state))
643 {
644 new_state=s->state;
645 s->state=state;
646 cb(s,SSL_CB_CONNECT_LOOP,1);
647 s->state=new_state;
648 }
649 }
650 skip=0;
651 }
652end:
653 s->in_handshake--;
654 if (buf != NULL)
655 BUF_MEM_free(buf);
656 if (cb != NULL)
657 cb(s,SSL_CB_CONNECT_EXIT,ret);
658 return(ret);
659 }
660
661
662int ssl3_client_hello(SSL *s)
663 {
664 unsigned char *buf;
665 unsigned char *p,*d;
666 int i;
667 unsigned long l;
668
669 buf=(unsigned char *)s->init_buf->data;
670 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
671 {
672 SSL_SESSION *sess = s->session;
673 if ((sess == NULL) ||
674 (sess->ssl_version != s->version) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700675 (!sess->session_id_length && !sess->tlsext_tick) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700676 (sess->not_resumable))
677 {
678 if (!ssl_get_new_session(s,0))
679 goto err;
680 }
681 if (s->method->version == DTLS_ANY_VERSION)
682 {
683 /* Determine which DTLS version to use */
684 int options = s->options;
685 /* If DTLS 1.2 disabled correct the version number */
686 if (options & SSL_OP_NO_DTLSv1_2)
687 {
688 if (tls1_suiteb(s))
689 {
690 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
691 goto err;
692 }
693 /* Disabling all versions is silly: return an
694 * error.
695 */
696 if (options & SSL_OP_NO_DTLSv1)
697 {
698 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
699 goto err;
700 }
701 /* Update method so we don't use any DTLS 1.2
702 * features.
703 */
704 s->method = DTLSv1_client_method();
705 s->version = DTLS1_VERSION;
706 }
707 else
708 {
709 /* We only support one version: update method */
710 if (options & SSL_OP_NO_DTLSv1)
711 s->method = DTLSv1_2_client_method();
712 s->version = DTLS1_2_VERSION;
713 }
714 s->client_version = s->version;
715 }
716 /* else use the pre-loaded session */
717
718 p=s->s3->client_random;
719
720 /* for DTLS if client_random is initialized, reuse it, we are
721 * required to use same upon reply to HelloVerify */
722 if (SSL_IS_DTLS(s))
723 {
724 size_t idx;
725 i = 1;
726 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
727 {
728 if (p[idx])
729 {
730 i = 0;
731 break;
732 }
733 }
734 }
735 else
736 i = 1;
737
738 if (i)
739 ssl_fill_hello_random(s, 0, p,
740 sizeof(s->s3->client_random));
741
Adam Langleyb0c235e2014-06-20 12:00:00 -0700742 /* Do the message type and length last.
743 * Note: the final argument to ssl_add_clienthello_tlsext below
744 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700745 d=p= ssl_handshake_start(s);
746
747 /* version indicates the negotiated version: for example from
748 * an SSLv2/v3 compatible client hello). The client_version
749 * field is the maximum version we permit and it is also
750 * used in RSA encrypted premaster secrets. Some servers can
751 * choke if we initially report a higher version then
752 * renegotiate to a lower one in the premaster secret. This
753 * didn't happen with TLS 1.0 as most servers supported it
754 * but it can with TLS 1.1 or later if the server only supports
755 * 1.0.
756 *
757 * Possible scenario with previous logic:
758 * 1. Client hello indicates TLS 1.2
759 * 2. Server hello says TLS 1.0
760 * 3. RSA encrypted premaster secret uses 1.2.
761 * 4. Handhaked proceeds using TLS 1.0.
762 * 5. Server sends hello request to renegotiate.
763 * 6. Client hello indicates TLS v1.0 as we now
764 * know that is maximum server supports.
765 * 7. Server chokes on RSA encrypted premaster secret
766 * containing version 1.0.
767 *
768 * For interoperability it should be OK to always use the
769 * maximum version we support in client hello and then rely
770 * on the checking of version to ensure the servers isn't
771 * being inconsistent: for example initially negotiating with
772 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
773 * client_version in client hello and not resetting it to
774 * the negotiated version.
775 */
776#if 0
777 *(p++)=s->version>>8;
778 *(p++)=s->version&0xff;
779 s->client_version=s->version;
780#else
781 *(p++)=s->client_version>>8;
782 *(p++)=s->client_version&0xff;
783#endif
784
785 /* Random stuff */
786 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
787 p+=SSL3_RANDOM_SIZE;
788
789 /* Session ID */
790 if (s->new_session)
791 i=0;
792 else
793 i=s->session->session_id_length;
794 *(p++)=i;
795 if (i != 0)
796 {
797 if (i > (int)sizeof(s->session->session_id))
798 {
799 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
800 goto err;
801 }
802 memcpy(p,s->session->session_id,i);
803 p+=i;
804 }
805
806 /* cookie stuff for DTLS */
807 if (SSL_IS_DTLS(s))
808 {
809 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
810 {
811 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
812 goto err;
813 }
814 *(p++) = s->d1->cookie_len;
815 memcpy(p, s->d1->cookie, s->d1->cookie_len);
816 p += s->d1->cookie_len;
817 }
818
819 /* Ciphers supported */
820 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
821 if (i == 0)
822 {
823 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
824 goto err;
825 }
826#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
827 /* Some servers hang if client hello > 256 bytes
828 * as hack workaround chop number of supported ciphers
829 * to keep it well below this if we use TLS v1.2
830 */
831 if (TLS1_get_version(s) >= TLS1_2_VERSION
832 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
833 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
834#endif
835 s2n(i,p);
836 p+=i;
837
838 /* COMPRESSION */
839 *(p++)=1;
840 *(p++)=0; /* Add the NULL method */
841
Adam Langley95c29f32014-06-20 12:00:00 -0700842 /* TLS extensions*/
843 if (ssl_prepare_clienthello_tlsext(s) <= 0)
844 {
845 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
846 goto err;
847 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700848 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700849 {
850 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
851 goto err;
852 }
Adam Langley95c29f32014-06-20 12:00:00 -0700853
854 l= p-d;
855 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
856 s->state=SSL3_ST_CW_CLNT_HELLO_B;
857 }
858
859 /* SSL3_ST_CW_CLNT_HELLO_B */
860 return ssl_do_write(s);
861err:
862 return(-1);
863 }
864
865int ssl3_get_server_hello(SSL *s)
866 {
867 STACK_OF(SSL_CIPHER) *sk;
868 const SSL_CIPHER *c;
869 CERT *ct = s->cert;
870 unsigned char *p,*d;
871 int al=SSL_AD_INTERNAL_ERROR,ok;
872 unsigned int j;
873 long n;
David Benjamin03973092014-06-24 23:27:17 -0400874 CBS cbs;
Adam Langley95c29f32014-06-20 12:00:00 -0700875 /* Hello verify request and/or server hello version may not
876 * match so set first packet if we're negotiating version.
877 */
878 if (SSL_IS_DTLS(s))
879 s->first_packet = 1;
880
881 n=s->method->ssl_get_message(s,
882 SSL3_ST_CR_SRVR_HELLO_A,
883 SSL3_ST_CR_SRVR_HELLO_B,
884 -1,
885 20000, /* ?? */
886 &ok);
887
888 if (!ok) return((int)n);
889
890 if (SSL_IS_DTLS(s))
891 {
892 s->first_packet = 0;
893 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
894 {
895 if ( s->d1->send_cookie == 0)
896 {
897 s->s3->tmp.reuse_message = 1;
898 return 1;
899 }
900 else /* already sent a cookie */
901 {
902 al=SSL_AD_UNEXPECTED_MESSAGE;
903 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
904 goto f_err;
905 }
906 }
907 }
908
909 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
910 {
911 al=SSL_AD_UNEXPECTED_MESSAGE;
912 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
913 goto f_err;
914 }
915
916 d=p=(unsigned char *)s->init_msg;
917 if (s->method->version == DTLS_ANY_VERSION)
918 {
919 /* Work out correct protocol version to use */
920 int hversion = (p[0] << 8)|p[1];
921 int options = s->options;
922 if (hversion == DTLS1_2_VERSION
923 && !(options & SSL_OP_NO_DTLSv1_2))
924 s->method = DTLSv1_2_client_method();
925 else if (tls1_suiteb(s))
926 {
927 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
928 s->version = hversion;
929 al = SSL_AD_PROTOCOL_VERSION;
930 goto f_err;
931 }
932 else if (hversion == DTLS1_VERSION
933 && !(options & SSL_OP_NO_DTLSv1))
934 s->method = DTLSv1_client_method();
935 else
936 {
937 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
938 s->version = hversion;
939 al = SSL_AD_PROTOCOL_VERSION;
940 goto f_err;
941 }
942 s->version = s->client_version = s->method->version;
943 }
944
945 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
946 {
947 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
948 s->version=(s->version&0xff00)|p[1];
949 al=SSL_AD_PROTOCOL_VERSION;
950 goto f_err;
951 }
952 p+=2;
953
954 /* load the server hello data */
955 /* load the server random */
956 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
957 p+=SSL3_RANDOM_SIZE;
958
959 /* get the session-id */
960 j= *(p++);
961
962 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
963 {
964 al=SSL_AD_ILLEGAL_PARAMETER;
965 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_SSL3_SESSION_ID_TOO_LONG);
966 goto f_err;
967 }
968
Adam Langley95c29f32014-06-20 12:00:00 -0700969 /* check if we want to resume the session based on external pre-shared secret */
970 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
971 {
972 SSL_CIPHER *pref_cipher=NULL;
973 s->session->master_key_length=sizeof(s->session->master_key);
974 if (s->tls_session_secret_cb(s, s->session->master_key,
975 &s->session->master_key_length,
976 NULL, &pref_cipher,
977 s->tls_session_secret_cb_arg))
978 {
979 s->session->cipher = pref_cipher ?
980 pref_cipher : ssl_get_cipher_by_char(s, p+j);
Adam Langley6acf4762014-06-20 12:00:00 -0700981 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700982 }
983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
985 if (j != 0 && j == s->session->session_id_length
986 && memcmp(p,s->session->session_id,j) == 0)
987 {
988 if(s->sid_ctx_length != s->session->sid_ctx_length
989 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
990 {
991 /* actually a client application bug */
992 al=SSL_AD_ILLEGAL_PARAMETER;
993 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
994 goto f_err;
995 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700996 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700997 s->hit=1;
998 }
999 else /* a miss or crap from the other end */
1000 {
1001 /* If we were trying for session-id reuse, make a new
1002 * SSL_SESSION so we don't stuff up other people */
1003 s->hit=0;
1004 if (s->session->session_id_length > 0)
1005 {
1006 if (!ssl_get_new_session(s,0))
1007 {
1008 goto f_err;
1009 }
1010 }
1011 s->session->session_id_length=j;
1012 memcpy(s->session->session_id,p,j); /* j could be 0 */
1013 }
1014 p+=j;
1015 c=ssl_get_cipher_by_char(s,p);
1016 if (c == NULL)
1017 {
1018 /* unknown cipher */
1019 al=SSL_AD_ILLEGAL_PARAMETER;
1020 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
1021 goto f_err;
1022 }
1023 /* If it is a disabled cipher we didn't send it in client hello,
1024 * so return an error.
1025 */
1026 if (c->algorithm_ssl & ct->mask_ssl ||
1027 c->algorithm_mkey & ct->mask_k ||
1028 c->algorithm_auth & ct->mask_a)
1029 {
1030 al=SSL_AD_ILLEGAL_PARAMETER;
1031 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1032 goto f_err;
1033 }
1034 p+=ssl_put_cipher_by_char(s,NULL,NULL);
1035
1036 sk=ssl_get_ciphers_by_id(s);
1037 if (!sk_SSL_CIPHER_find(sk, NULL, c))
1038 {
1039 /* we did not say we would use this cipher */
1040 al=SSL_AD_ILLEGAL_PARAMETER;
1041 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1042 goto f_err;
1043 }
1044
1045 /* Depending on the session caching (internal/external), the cipher
1046 and/or cipher_id values may not be set. Make sure that
1047 cipher_id is set and use it for comparison. */
1048 if (s->session->cipher)
1049 s->session->cipher_id = s->session->cipher->id;
1050 if (s->hit && (s->session->cipher_id != c->id))
1051 {
1052/* Workaround is now obsolete */
1053#if 0
1054 if (!(s->options &
1055 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1056#endif
1057 {
1058 al=SSL_AD_ILLEGAL_PARAMETER;
1059 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1060 goto f_err;
1061 }
1062 }
1063 s->s3->tmp.new_cipher=c;
1064 /* Don't digest cached records if no sigalgs: we may need them for
1065 * client authentication.
1066 */
1067 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1068 goto f_err;
1069 /* lets get the compression algorithm */
1070 /* COMPRESSION */
1071 if (*(p++) != 0)
1072 {
1073 al=SSL_AD_ILLEGAL_PARAMETER;
1074 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1075 goto f_err;
1076 }
Adam Langley95c29f32014-06-20 12:00:00 -07001077
David Benjamin03973092014-06-24 23:27:17 -04001078 /* TODO(fork): Port the rest of this function to CBS. */
1079 CBS_init(&cbs, p, d + n - p);
Adam Langley95c29f32014-06-20 12:00:00 -07001080 /* TLS extensions*/
David Benjamin03973092014-06-24 23:27:17 -04001081 if (!ssl_parse_serverhello_tlsext(s, &cbs))
Adam Langley95c29f32014-06-20 12:00:00 -07001082 {
1083 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
1084 goto err;
1085 }
Adam Langley95c29f32014-06-20 12:00:00 -07001086
David Benjamin03973092014-06-24 23:27:17 -04001087 /* There should be nothing left over in the record. */
1088 if (CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001089 {
1090 /* wrong packet length */
1091 al=SSL_AD_DECODE_ERROR;
1092 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
1093 goto f_err;
1094 }
1095
1096 return(1);
1097f_err:
1098 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1099err:
1100 return(-1);
1101 }
1102
1103int ssl3_get_server_certificate(SSL *s)
1104 {
1105 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001106 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001107 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001108 STACK_OF(X509) *sk=NULL;
1109 SESS_CERT *sc;
1110 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001111 CBS cbs, certificate_list;
1112 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001113
1114 n=s->method->ssl_get_message(s,
1115 SSL3_ST_CR_CERT_A,
1116 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001117 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001118 s->max_cert_list,
1119 &ok);
1120
1121 if (!ok) return((int)n);
1122
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001123 CBS_init(&cbs, (uint8_t *)s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001124
1125 if ((sk=sk_X509_new_null()) == NULL)
1126 {
1127 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1128 goto err;
1129 }
1130
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001131 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1132 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001133 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001134 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001135 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1136 goto f_err;
1137 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001138
1139 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001140 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001141 CBS certificate;
1142 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001143 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001144 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001145 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1146 goto f_err;
1147 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001148 data = CBS_data(&certificate);
1149 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001150 if (x == NULL)
1151 {
1152 al=SSL_AD_BAD_CERTIFICATE;
1153 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1154 goto f_err;
1155 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001156 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001157 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001158 al = SSL_AD_INTERNAL_ERROR;
1159 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1160 goto f_err;
1161 }
1162 if (CBS_len(&certificate) != 0)
1163 {
1164 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001165 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1166 goto f_err;
1167 }
1168 if (!sk_X509_push(sk,x))
1169 {
1170 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1171 goto err;
1172 }
1173 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001174 }
1175
1176 i=ssl_verify_cert_chain(s,sk);
1177 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1178 )
1179 {
1180 al=ssl_verify_alarm_type(s->verify_result);
1181 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1182 goto f_err;
1183 }
1184 ERR_clear_error(); /* but we keep s->verify_result */
1185
1186 sc=ssl_sess_cert_new();
1187 if (sc == NULL) goto err;
1188
1189 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1190 s->session->sess_cert=sc;
1191
1192 sc->cert_chain=sk;
1193 /* Inconsistency alert: cert_chain does include the peer's
1194 * certificate, which we don't include in s3_srvr.c */
1195 x=sk_X509_value(sk,0);
1196 sk=NULL;
1197 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1198
1199 pkey=X509_get_pubkey(x);
1200
David Benjamind26aea62014-07-12 00:13:56 -04001201 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001202 {
1203 x=NULL;
1204 al=SSL3_AL_FATAL;
1205 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1206 goto f_err;
1207 }
1208
1209 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001210 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001211 {
1212 x=NULL;
1213 al=SSL3_AL_FATAL;
1214 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1215 goto f_err;
1216 }
1217
David Benjamind26aea62014-07-12 00:13:56 -04001218 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1219 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001220 {
David Benjamind26aea62014-07-12 00:13:56 -04001221 x=NULL;
1222 al=SSL_AD_ILLEGAL_PARAMETER;
1223 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1224 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001225 }
David Benjamind26aea62014-07-12 00:13:56 -04001226 sc->peer_cert_type=i;
1227 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1228 /* Why would the following ever happen?
1229 * We just created sc a couple of lines ago. */
1230 if (sc->peer_pkeys[i].x509 != NULL)
1231 X509_free(sc->peer_pkeys[i].x509);
1232 sc->peer_pkeys[i].x509=x;
1233 sc->peer_key= &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001234
David Benjamind26aea62014-07-12 00:13:56 -04001235 if (s->session->peer != NULL)
1236 X509_free(s->session->peer);
1237 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1238 s->session->peer=x;
1239
Adam Langley95c29f32014-06-20 12:00:00 -07001240 s->session->verify_result = s->verify_result;
1241
1242 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001243 ret=1;
1244 if (0)
1245 {
1246f_err:
1247 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1248 }
1249err:
1250 EVP_PKEY_free(pkey);
1251 X509_free(x);
1252 sk_X509_pop_free(sk,X509_free);
1253 return(ret);
1254 }
1255
1256int ssl3_get_key_exchange(SSL *s)
1257 {
Adam Langley95c29f32014-06-20 12:00:00 -07001258 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
Adam Langley95c29f32014-06-20 12:00:00 -07001259 EVP_MD_CTX md_ctx;
1260 unsigned char *param,*p;
1261 int al,i,j,param_len,ok;
1262 long n,alg_k,alg_a;
1263 EVP_PKEY *pkey=NULL;
1264 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001265 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001266#ifndef OPENSSL_NO_DH
1267 DH *dh=NULL;
1268#endif
1269#ifndef OPENSSL_NO_ECDH
1270 EC_KEY *ecdh = NULL;
1271 BN_CTX *bn_ctx = NULL;
1272 EC_POINT *srvr_ecpoint = NULL;
1273 int curve_nid = 0;
1274 int encoded_pt_len = 0;
1275#endif
1276
1277 /* use same message size as in ssl3_get_certificate_request()
1278 * as ServerKeyExchange message may be skipped */
1279 n=s->method->ssl_get_message(s,
1280 SSL3_ST_CR_KEY_EXCH_A,
1281 SSL3_ST_CR_KEY_EXCH_B,
1282 -1,
1283 s->max_cert_list,
1284 &ok);
1285 if (!ok) return((int)n);
1286
1287 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1288 {
David Benjamin9c651c92014-07-12 13:27:45 -04001289 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1290 {
1291 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1292 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1293 return -1;
1294 }
1295
Adam Langley95c29f32014-06-20 12:00:00 -07001296#ifndef OPENSSL_NO_PSK
1297 /* In plain PSK ciphersuite, ServerKeyExchange can be
1298 omitted if no identity hint is sent. Set
1299 session->sess_cert anyway to avoid problems
1300 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001301 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001302 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001303 /* PSK ciphersuites that also send a
1304 * Certificate would have already initialized
1305 * |sess_cert|. */
1306 if (s->session->sess_cert == NULL)
1307 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001308 if (s->session->psk_identity_hint)
1309 {
1310 OPENSSL_free(s->session->psk_identity_hint);
1311 s->session->psk_identity_hint = NULL;
1312 }
Adam Langley95c29f32014-06-20 12:00:00 -07001313 }
1314#endif
1315 s->s3->tmp.reuse_message=1;
1316 return(1);
1317 }
1318
1319 param=p=(unsigned char *)s->init_msg;
1320 if (s->session->sess_cert != NULL)
1321 {
Adam Langley95c29f32014-06-20 12:00:00 -07001322 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1323 {
1324 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1325 s->session->sess_cert->peer_rsa_tmp=NULL;
1326 }
Adam Langley95c29f32014-06-20 12:00:00 -07001327#ifndef OPENSSL_NO_DH
1328 if (s->session->sess_cert->peer_dh_tmp)
1329 {
1330 DH_free(s->session->sess_cert->peer_dh_tmp);
1331 s->session->sess_cert->peer_dh_tmp=NULL;
1332 }
1333#endif
1334#ifndef OPENSSL_NO_ECDH
1335 if (s->session->sess_cert->peer_ecdh_tmp)
1336 {
1337 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1338 s->session->sess_cert->peer_ecdh_tmp=NULL;
1339 }
1340#endif
1341 }
1342 else
1343 {
1344 s->session->sess_cert=ssl_sess_cert_new();
1345 }
1346
1347 param_len=0;
1348 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1349 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1350 EVP_MD_CTX_init(&md_ctx);
1351
1352#ifndef OPENSSL_NO_PSK
Adam Langleyc26c8022014-06-20 12:00:00 -07001353 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001354 {
1355 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1356
1357 al=SSL_AD_HANDSHAKE_FAILURE;
1358 n2s(p,i);
1359 param_len=i+2;
Adam Langley0289c732014-06-20 12:00:00 -07001360 if (s->session->psk_identity_hint)
1361 {
1362 OPENSSL_free(s->session->psk_identity_hint);
1363 s->session->psk_identity_hint = NULL;
1364 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001365 if (i != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001366 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001367 /* Store PSK identity hint for later use, hint is used
1368 * in ssl3_send_client_key_exchange. Assume that the
1369 * maximum length of a PSK identity hint can be as
1370 * long as the maximum length of a PSK identity. */
1371 if (i > PSK_MAX_IDENTITY_LEN)
1372 {
1373 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
1374 goto f_err;
1375 }
1376 if (param_len > n)
1377 {
1378 al=SSL_AD_DECODE_ERROR;
1379 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1380 goto f_err;
1381 }
1382 /* If received PSK identity hint contains NULL
1383 * characters, the hint is truncated from the first
1384 * NULL. p may not be ending with NULL, so create a
1385 * NULL-terminated string. */
1386 memcpy(tmp_id_hint, p, i);
1387 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
Adam Langley0289c732014-06-20 12:00:00 -07001388 s->session->psk_identity_hint = BUF_strdup(tmp_id_hint);
1389 if (s->session->psk_identity_hint == NULL)
Adam Langleyc26c8022014-06-20 12:00:00 -07001390 {
1391 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1392 goto f_err;
1393 }
1394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
1396 p+=i;
1397 n-=param_len;
1398 }
Adam Langley95c29f32014-06-20 12:00:00 -07001399#endif /* !OPENSSL_NO_PSK */
Adam Langleyc26c8022014-06-20 12:00:00 -07001400
1401 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001402 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001403 {
1404 if ((rsa=RSA_new()) == NULL)
1405 {
1406 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1407 goto err;
1408 }
1409 n2s(p,i);
1410 param_len=i+2;
1411 if (param_len > n)
1412 {
1413 al=SSL_AD_DECODE_ERROR;
1414 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_MODULUS_LENGTH);
1415 goto f_err;
1416 }
1417 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1418 {
1419 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1420 goto err;
1421 }
1422 p+=i;
1423
1424 n2s(p,i);
1425 param_len+=i+2;
1426 if (param_len > n)
1427 {
1428 al=SSL_AD_DECODE_ERROR;
1429 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_E_LENGTH);
1430 goto f_err;
1431 }
1432 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1433 {
1434 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1435 goto err;
1436 }
1437 p+=i;
1438 n-=param_len;
1439
1440 /* this should be because we are using an export cipher */
1441 if (alg_a & SSL_aRSA)
1442 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1443 else
1444 {
1445 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1446 goto err;
1447 }
1448 s->session->sess_cert->peer_rsa_tmp=rsa;
1449 rsa=NULL;
1450 }
Adam Langley95c29f32014-06-20 12:00:00 -07001451#ifndef OPENSSL_NO_DH
1452 else if (alg_k & SSL_kEDH)
1453 {
1454 if ((dh=DH_new()) == NULL)
1455 {
1456 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_DH_LIB);
1457 goto err;
1458 }
1459 n2s(p,i);
1460 param_len=i+2;
1461 if (param_len > n)
1462 {
1463 al=SSL_AD_DECODE_ERROR;
1464 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1465 goto f_err;
1466 }
1467 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1468 {
1469 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1470 goto err;
1471 }
1472 p+=i;
1473
1474 n2s(p,i);
1475 param_len+=i+2;
1476 if (param_len > n)
1477 {
1478 al=SSL_AD_DECODE_ERROR;
1479 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_G_LENGTH);
1480 goto f_err;
1481 }
1482 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1483 {
1484 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1485 goto err;
1486 }
1487 p+=i;
1488
1489 n2s(p,i);
1490 param_len+=i+2;
1491 if (param_len > n)
1492 {
1493 al=SSL_AD_DECODE_ERROR;
1494 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1495 goto f_err;
1496 }
1497 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1498 {
1499 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1500 goto err;
1501 }
1502 p+=i;
1503 n-=param_len;
1504
Adam Langley95c29f32014-06-20 12:00:00 -07001505 if (alg_a & SSL_aRSA)
1506 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001507#ifndef OPENSSL_NO_DSA
1508 else if (alg_a & SSL_aDSS)
1509 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1510#endif
1511 /* else anonymous DH, so no certificate or pkey. */
1512
1513 s->session->sess_cert->peer_dh_tmp=dh;
1514 dh=NULL;
1515 }
1516 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1517 {
1518 al=SSL_AD_ILLEGAL_PARAMETER;
1519 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1520 goto f_err;
1521 }
1522#endif /* !OPENSSL_NO_DH */
1523
1524#ifndef OPENSSL_NO_ECDH
1525 else if (alg_k & SSL_kEECDH)
1526 {
1527 EC_GROUP *ngroup;
1528 const EC_GROUP *group;
1529
1530 if ((ecdh=EC_KEY_new()) == NULL)
1531 {
1532 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1533 goto err;
1534 }
1535
1536 /* Extract elliptic curve parameters and the
1537 * server's ephemeral ECDH public key.
1538 * Keep accumulating lengths of various components in
1539 * param_len and make sure it never exceeds n.
1540 */
1541
1542 /* XXX: For now we only support named (not generic) curves
1543 * and the ECParameters in this case is just three bytes.
1544 */
1545 param_len=3;
1546 /* Check curve is one of our prefrences, if not server has
1547 * sent an invalid curve.
1548 */
1549 if (!tls1_check_curve(s, p, param_len))
1550 {
1551 al=SSL_AD_DECODE_ERROR;
1552 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_CURVE);
1553 goto f_err;
1554 }
1555
1556 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)
1557 {
1558 al=SSL_AD_INTERNAL_ERROR;
1559 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1560 goto f_err;
1561 }
1562
1563 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1564 if (ngroup == NULL)
1565 {
1566 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1567 goto err;
1568 }
1569 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1570 {
1571 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1572 goto err;
1573 }
1574 EC_GROUP_free(ngroup);
1575
1576 group = EC_KEY_get0_group(ecdh);
1577
1578 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1579 (EC_GROUP_get_degree(group) > 163))
1580 {
1581 al=SSL_AD_EXPORT_RESTRICTION;
1582 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1583 goto f_err;
1584 }
1585
1586 p+=3;
1587
1588 /* Next, get the encoded ECPoint */
1589 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1590 ((bn_ctx = BN_CTX_new()) == NULL))
1591 {
1592 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1593 goto err;
1594 }
1595
1596 encoded_pt_len = *p; /* length of encoded point */
1597 p+=1;
1598 param_len += (1 + encoded_pt_len);
1599 if ((param_len > n) ||
1600 (EC_POINT_oct2point(group, srvr_ecpoint,
1601 p, encoded_pt_len, bn_ctx) == 0))
1602 {
1603 al=SSL_AD_DECODE_ERROR;
1604 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_ECPOINT);
1605 goto f_err;
1606 }
1607
1608 n-=param_len;
1609 p+=encoded_pt_len;
1610
1611 /* The ECC/TLS specification does not mention
1612 * the use of DSA to sign ECParameters in the server
1613 * key exchange message. We do support RSA and ECDSA.
1614 */
1615 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001616 else if (alg_a & SSL_aRSA)
1617 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001618#ifndef OPENSSL_NO_ECDSA
1619 else if (alg_a & SSL_aECDSA)
1620 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1621#endif
1622 /* else anonymous ECDH, so no certificate or pkey. */
1623 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1624 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1625 ecdh=NULL;
1626 BN_CTX_free(bn_ctx);
1627 bn_ctx = NULL;
1628 EC_POINT_free(srvr_ecpoint);
1629 srvr_ecpoint = NULL;
1630 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001631#endif /* !OPENSSL_NO_ECDH */
1632
1633 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001634 {
1635 al=SSL_AD_UNEXPECTED_MESSAGE;
1636 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1637 goto f_err;
1638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639
1640 /* p points to the next byte, there are 'n' bytes left */
1641
1642 /* if it was signed, check the signature */
1643 if (pkey != NULL)
1644 {
1645 if (SSL_USE_SIGALGS(s))
1646 {
1647 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1648 if (rv == -1)
1649 goto err;
1650 else if (rv == 0)
1651 {
1652 al = SSL_AD_DECODE_ERROR;
1653 goto f_err;
1654 }
1655#ifdef SSL_DEBUG
1656fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1657#endif
1658 p += 2;
1659 n -= 2;
1660 }
1661 else
1662 md = EVP_sha1();
1663
1664 n2s(p,i);
1665 n-=2;
1666 j=EVP_PKEY_size(pkey);
1667
1668 if ((i != n) || (n > j) || (n <= 0))
1669 {
1670 /* wrong packet length */
1671 al=SSL_AD_DECODE_ERROR;
1672 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_SIGNATURE_LENGTH);
1673 goto f_err;
1674 }
1675
Adam Langley95c29f32014-06-20 12:00:00 -07001676 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1677 {
1678 int num;
1679
1680 j=0;
1681 q=md_buf;
1682 for (num=2; num > 0; num--)
1683 {
1684 EVP_DigestInit_ex(&md_ctx,(num == 2)
1685 ?s->ctx->md5:s->ctx->sha1, NULL);
1686 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1687 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1688 EVP_DigestUpdate(&md_ctx,param,param_len);
1689 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1690 q+=i;
1691 j+=i;
1692 }
1693 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1694 pkey->pkey.rsa);
1695 if (i < 0)
1696 {
1697 al=SSL_AD_DECRYPT_ERROR;
1698 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_DECRYPT);
1699 goto f_err;
1700 }
1701 if (i == 0)
1702 {
1703 /* bad signature */
1704 al=SSL_AD_DECRYPT_ERROR;
1705 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1706 goto f_err;
1707 }
1708 }
1709 else
Adam Langley95c29f32014-06-20 12:00:00 -07001710 {
1711 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1712 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1713 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1714 EVP_VerifyUpdate(&md_ctx,param,param_len);
1715 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1716 {
1717 /* bad signature */
1718 al=SSL_AD_DECRYPT_ERROR;
1719 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1720 goto f_err;
1721 }
1722 }
1723 }
1724 else
1725 {
David Benjamine8f3d662014-07-12 01:10:19 -04001726 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001727 {
1728 /* Might be wrong key type, check it */
1729 if (ssl3_check_cert_and_algorithm(s))
1730 /* Otherwise this shouldn't happen */
1731 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1732 goto err;
1733 }
1734 /* still data left over */
1735 if (n != 0)
1736 {
1737 al=SSL_AD_DECODE_ERROR;
1738 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
1739 goto f_err;
1740 }
1741 }
1742 EVP_PKEY_free(pkey);
1743 EVP_MD_CTX_cleanup(&md_ctx);
1744 return(1);
1745f_err:
1746 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1747err:
1748 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001749 if (rsa != NULL)
1750 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001751#ifndef OPENSSL_NO_DH
1752 if (dh != NULL)
1753 DH_free(dh);
1754#endif
1755#ifndef OPENSSL_NO_ECDH
1756 BN_CTX_free(bn_ctx);
1757 EC_POINT_free(srvr_ecpoint);
1758 if (ecdh != NULL)
1759 EC_KEY_free(ecdh);
1760#endif
1761 EVP_MD_CTX_cleanup(&md_ctx);
1762 return(-1);
1763 }
1764
1765static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1766 {
1767 return(X509_NAME_cmp(*a,*b));
1768 }
1769
1770int ssl3_get_certificate_request(SSL *s)
1771 {
1772 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001773 unsigned long n;
1774 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001775 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001776 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001777 CBS cbs;
1778 CBS certificate_types;
1779 CBS certificate_authorities;
1780 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001781
1782 n=s->method->ssl_get_message(s,
1783 SSL3_ST_CR_CERT_REQ_A,
1784 SSL3_ST_CR_CERT_REQ_B,
1785 -1,
1786 s->max_cert_list,
1787 &ok);
1788
1789 if (!ok) return((int)n);
1790
1791 s->s3->tmp.cert_req=0;
1792
1793 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1794 {
1795 s->s3->tmp.reuse_message=1;
1796 /* If we get here we don't need any cached handshake records
1797 * as we wont be doing client auth.
1798 */
1799 if (s->s3->handshake_buffer)
1800 {
1801 if (!ssl3_digest_cached_records(s))
1802 goto err;
1803 }
1804 return(1);
1805 }
1806
1807 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1808 {
1809 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1810 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1811 goto err;
1812 }
1813
1814 /* TLS does not like anon-DH with client cert */
1815 if (s->version > SSL3_VERSION)
1816 {
1817 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1818 {
1819 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1820 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1821 goto err;
1822 }
1823 }
1824
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001825 CBS_init(&cbs, (uint8_t *)s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001826
1827 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1828 if (ca_sk == NULL)
1829 {
1830 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1831 goto err;
1832 }
1833
1834 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001835 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001836 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001837 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1838 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1839 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001840 }
David Benjamin676d1e72014-07-08 14:34:10 -04001841 if (!CBS_stow(&certificate_types,
1842 &s->s3->tmp.certificate_types,
1843 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001844 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001845 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1846 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848 if (SSL_USE_SIGALGS(s))
1849 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001850 CBS supported_signature_algorithms;
1851 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001852 {
1853 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001854 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001855 goto err;
1856 }
1857 /* Clear certificate digests and validity flags */
1858 for (i = 0; i < SSL_PKEY_NUM; i++)
1859 {
1860 s->cert->pkeys[i].digest = NULL;
1861 s->cert->pkeys[i].valid_flags = 0;
1862 }
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001863 if (!tls1_process_sigalgs(s,
1864 CBS_data(&supported_signature_algorithms),
1865 CBS_len(&supported_signature_algorithms)))
Adam Langley95c29f32014-06-20 12:00:00 -07001866 {
1867 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1868 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1869 goto err;
1870 }
Adam Langley95c29f32014-06-20 12:00:00 -07001871 }
1872
1873 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001874 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001875 {
1876 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1877 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1878 goto err;
1879 }
1880
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001881 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001882 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001883 CBS distinguished_name;
1884 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001885 {
Adam Langley95c29f32014-06-20 12:00:00 -07001886 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1887 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1888 goto err;
1889 }
1890
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001891 data = CBS_data(&distinguished_name);
1892 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001893 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001894 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1895 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1896 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001897 }
1898
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001899 if (CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
1900 {
1901 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1902 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1903 goto err;
1904 }
1905 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001906 {
1907 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1908 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1909 goto err;
1910 }
1911 if (!sk_X509_NAME_push(ca_sk,xn))
1912 {
1913 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1914 goto err;
1915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916 }
1917
Adam Langley95c29f32014-06-20 12:00:00 -07001918 /* we should setup a certificate to return.... */
1919 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001920 if (s->s3->tmp.ca_names != NULL)
1921 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1922 s->s3->tmp.ca_names=ca_sk;
1923 ca_sk=NULL;
1924
1925 ret=1;
1926err:
1927 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1928 return(ret);
1929 }
1930
Adam Langley95c29f32014-06-20 12:00:00 -07001931int ssl3_get_new_session_ticket(SSL *s)
1932 {
1933 int ok,al,ret=0, ticklen;
1934 long n;
1935 const unsigned char *p;
1936 unsigned char *d;
1937
1938 n=s->method->ssl_get_message(s,
1939 SSL3_ST_CR_SESSION_TICKET_A,
1940 SSL3_ST_CR_SESSION_TICKET_B,
1941 -1,
1942 16384,
1943 &ok);
1944
1945 if (!ok)
1946 return((int)n);
1947
1948 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1949 {
1950 s->s3->tmp.reuse_message=1;
1951 return(1);
1952 }
1953 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1954 {
1955 al=SSL_AD_UNEXPECTED_MESSAGE;
1956 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE);
1957 goto f_err;
1958 }
1959 if (n < 6)
1960 {
1961 /* need at least ticket_lifetime_hint + ticket length */
1962 al = SSL_AD_DECODE_ERROR;
1963 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1964 goto f_err;
1965 }
1966
1967 p=d=(unsigned char *)s->init_msg;
1968 n2l(p, s->session->tlsext_tick_lifetime_hint);
1969 n2s(p, ticklen);
1970 /* ticket_lifetime_hint + ticket_length + ticket */
1971 if (ticklen + 6 != n)
1972 {
1973 al = SSL_AD_DECODE_ERROR;
1974 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1975 goto f_err;
1976 }
1977 if (s->session->tlsext_tick)
1978 {
1979 OPENSSL_free(s->session->tlsext_tick);
1980 s->session->tlsext_ticklen = 0;
1981 }
1982 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1983 if (!s->session->tlsext_tick)
1984 {
1985 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1986 goto err;
1987 }
1988 memcpy(s->session->tlsext_tick, p, ticklen);
1989 s->session->tlsext_ticklen = ticklen;
1990 /* There are two ways to detect a resumed ticket sesion.
1991 * One is to set an appropriate session ID and then the server
1992 * must return a match in ServerHello. This allows the normal
1993 * client session ID matching to work and we know much
1994 * earlier that the ticket has been accepted.
1995 *
1996 * The other way is to set zero length session ID when the
1997 * ticket is presented and rely on the handshake to determine
1998 * session resumption.
1999 *
2000 * We choose the former approach because this fits in with
2001 * assumptions elsewhere in OpenSSL. The session ID is set
2002 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2003 * ticket.
2004 */
2005 EVP_Digest(p, ticklen,
2006 s->session->session_id, &s->session->session_id_length,
2007#ifndef OPENSSL_NO_SHA256
2008 EVP_sha256(), NULL);
2009#else
2010 EVP_sha1(), NULL);
2011#endif
2012 ret=1;
2013 return(ret);
2014f_err:
2015 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2016err:
2017 return(-1);
2018 }
2019
2020int ssl3_get_cert_status(SSL *s)
2021 {
2022 int ok, al;
2023 unsigned long resplen,n;
2024 const unsigned char *p;
2025
2026 n=s->method->ssl_get_message(s,
2027 SSL3_ST_CR_CERT_STATUS_A,
2028 SSL3_ST_CR_CERT_STATUS_B,
2029 SSL3_MT_CERTIFICATE_STATUS,
2030 16384,
2031 &ok);
2032
2033 if (!ok) return((int)n);
2034 if (n < 4)
2035 {
2036 /* need at least status type + length */
2037 al = SSL_AD_DECODE_ERROR;
2038 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2039 goto f_err;
2040 }
2041 p = (unsigned char *)s->init_msg;
2042 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2043 {
2044 al = SSL_AD_DECODE_ERROR;
2045 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_UNSUPPORTED_STATUS_TYPE);
2046 goto f_err;
2047 }
2048 n2l3(p, resplen);
2049 if (resplen + 4 != n)
2050 {
2051 al = SSL_AD_DECODE_ERROR;
2052 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2053 goto f_err;
2054 }
2055 if (s->tlsext_ocsp_resp)
2056 OPENSSL_free(s->tlsext_ocsp_resp);
2057 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2058 if (!s->tlsext_ocsp_resp)
2059 {
2060 al = SSL_AD_INTERNAL_ERROR;
2061 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2062 goto f_err;
2063 }
2064 s->tlsext_ocsp_resplen = resplen;
2065 if (s->ctx->tlsext_status_cb)
2066 {
2067 int ret;
2068 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2069 if (ret == 0)
2070 {
2071 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2072 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
2073 goto f_err;
2074 }
2075 if (ret < 0)
2076 {
2077 al = SSL_AD_INTERNAL_ERROR;
2078 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2079 goto f_err;
2080 }
2081 }
2082 return 1;
2083f_err:
2084 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2085 return(-1);
2086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
2088int ssl3_get_server_done(SSL *s)
2089 {
2090 int ok,ret=0;
2091 long n;
2092
2093 n=s->method->ssl_get_message(s,
2094 SSL3_ST_CR_SRVR_DONE_A,
2095 SSL3_ST_CR_SRVR_DONE_B,
2096 SSL3_MT_SERVER_DONE,
2097 30, /* should be very small, like 0 :-) */
2098 &ok);
2099
2100 if (!ok) return((int)n);
2101 if (n > 0)
2102 {
2103 /* should contain no data */
2104 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2105 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
2106 return -1;
2107 }
2108 ret=1;
2109 return(ret);
2110 }
2111
2112
2113int ssl3_send_client_key_exchange(SSL *s)
2114 {
2115 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07002116 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002117 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07002118 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002119 unsigned char *q;
2120 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002121#ifndef OPENSSL_NO_ECDH
2122 EC_KEY *clnt_ecdh = NULL;
2123 const EC_POINT *srvr_ecpoint = NULL;
2124 EVP_PKEY *srvr_pub_pkey = NULL;
2125 unsigned char *encodedPoint = NULL;
2126 int encoded_pt_len = 0;
2127 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002128#ifndef OPENSSL_NO_PSK
2129 unsigned int psk_len = 0;
2130 unsigned char psk[PSK_MAX_PSK_LEN];
2131#endif /* OPENSSL_NO_PSK */
2132#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002133
2134 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2135 {
2136 p = ssl_handshake_start(s);
2137
2138 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002139 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2140
2141#ifndef OPENSSL_NO_PSK
2142 if (alg_a & SSL_aPSK)
2143 {
Adam Langley01797e32014-06-20 12:00:00 -07002144 char identity[PSK_MAX_IDENTITY_LEN + 1];
2145 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002146 unsigned char *t = NULL;
2147 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2148 unsigned int pre_ms_len = 0;
2149 int psk_err = 1;
2150
2151 n = 0;
2152 if (s->psk_client_callback == NULL)
2153 {
2154 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2155 goto err;
2156 }
2157
Adam Langley01797e32014-06-20 12:00:00 -07002158 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002159 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002160 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002161 if (psk_len > PSK_MAX_PSK_LEN)
2162 {
2163 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2164 goto psk_err;
2165 }
2166 else if (psk_len == 0)
2167 {
2168 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2169 goto psk_err;
2170 }
Adam Langley01797e32014-06-20 12:00:00 -07002171 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2172 if (identity_len > PSK_MAX_IDENTITY_LEN)
2173 {
2174 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2175 goto psk_err;
2176 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002177
2178 if (!(alg_k & SSL_kEECDH))
2179 {
2180 /* Create the shared secret now if we're not using ECDHE-PSK.*/
2181 pre_ms_len = 2+psk_len+2+psk_len;
2182 t = pre_ms;
2183 s2n(psk_len, t);
2184 memset(t, 0, psk_len);
2185 t+=psk_len;
2186 s2n(psk_len, t);
2187 memcpy(t, psk, psk_len);
2188
2189 s->session->master_key_length =
2190 s->method->ssl3_enc->generate_master_secret(s,
2191 s->session->master_key,
2192 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002193 s2n(identity_len, p);
2194 memcpy(p, identity, identity_len);
2195 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002196 }
2197
Adam Langleyc26c8022014-06-20 12:00:00 -07002198 if (s->session->psk_identity != NULL)
2199 OPENSSL_free(s->session->psk_identity);
2200 s->session->psk_identity = BUF_strdup(identity);
2201 if (s->session->psk_identity == NULL)
2202 {
2203 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2204 goto psk_err;
2205 }
2206 psk_err = 0;
2207 psk_err:
2208 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2209 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2210 if (psk_err != 0)
2211 {
2212 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2213 goto err;
2214 }
2215 }
2216#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002217
2218 /* Fool emacs indentation */
2219 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002220 else if (alg_k & SSL_kRSA)
2221 {
2222 RSA *rsa;
2223 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2224
2225 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2226 rsa=s->session->sess_cert->peer_rsa_tmp;
2227 else
2228 {
2229 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2230 if ((pkey == NULL) ||
2231 (pkey->type != EVP_PKEY_RSA) ||
2232 (pkey->pkey.rsa == NULL))
2233 {
2234 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2235 goto err;
2236 }
2237 rsa=pkey->pkey.rsa;
2238 EVP_PKEY_free(pkey);
2239 }
2240
2241 tmp_buf[0]=s->client_version>>8;
2242 tmp_buf[1]=s->client_version&0xff;
2243 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2244 goto err;
2245
2246 s->session->master_key_length=sizeof tmp_buf;
2247
2248 q=p;
2249 /* Fix buf for TLS and beyond */
2250 if (s->version > SSL3_VERSION)
2251 p+=2;
2252 n=RSA_public_encrypt(sizeof tmp_buf,
2253 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2254#ifdef PKCS1_CHECK
2255 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2256 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2257#endif
2258 if (n <= 0)
2259 {
2260 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2261 goto err;
2262 }
2263
2264 /* Fix buf for TLS and beyond */
2265 if (s->version > SSL3_VERSION)
2266 {
2267 s2n(n,q);
2268 n+=2;
2269 }
2270
2271 s->session->master_key_length=
2272 s->method->ssl3_enc->generate_master_secret(s,
2273 s->session->master_key,
2274 tmp_buf,sizeof tmp_buf);
2275 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2276 }
Adam Langley95c29f32014-06-20 12:00:00 -07002277#ifndef OPENSSL_NO_DH
2278 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2279 {
2280 DH *dh_srvr,*dh_clnt;
2281 SESS_CERT *scert = s->session->sess_cert;
2282
2283 if (scert == NULL)
2284 {
2285 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2286 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2287 goto err;
2288 }
2289
2290 if (scert->peer_dh_tmp != NULL)
2291 dh_srvr=scert->peer_dh_tmp;
2292 else
2293 {
2294 /* we get them from the cert */
2295 int idx = scert->peer_cert_type;
2296 EVP_PKEY *spkey = NULL;
2297 dh_srvr = NULL;
2298 if (idx >= 0)
2299 spkey = X509_get_pubkey(
2300 scert->peer_pkeys[idx].x509);
2301 if (spkey)
2302 {
2303 dh_srvr = EVP_PKEY_get1_DH(spkey);
2304 EVP_PKEY_free(spkey);
2305 }
2306 if (dh_srvr == NULL)
2307 {
2308 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2309 goto err;
2310 }
2311 }
2312 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2313 {
2314 /* Use client certificate key */
2315 EVP_PKEY *clkey = s->cert->key->privatekey;
2316 dh_clnt = NULL;
2317 if (clkey)
2318 dh_clnt = EVP_PKEY_get1_DH(clkey);
2319 if (dh_clnt == NULL)
2320 {
2321 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2322 goto err;
2323 }
2324 }
2325 else
2326 {
2327 /* generate a new random key */
2328 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2329 {
2330 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2331 goto err;
2332 }
2333 if (!DH_generate_key(dh_clnt))
2334 {
2335 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2336 DH_free(dh_clnt);
2337 goto err;
2338 }
2339 }
2340
2341 /* use the 'p' output buffer for the DH key, but
2342 * make sure to clear it out afterwards */
2343
2344 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2345 if (scert->peer_dh_tmp == NULL)
2346 DH_free(dh_srvr);
2347
2348 if (n <= 0)
2349 {
2350 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2351 DH_free(dh_clnt);
2352 goto err;
2353 }
2354
2355 /* generate master key from the result */
2356 s->session->master_key_length=
2357 s->method->ssl3_enc->generate_master_secret(s,
2358 s->session->master_key,p,n);
2359 /* clean up */
2360 memset(p,0,n);
2361
2362 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2363 n = 0;
2364 else
2365 {
2366 /* send off the data */
2367 n=BN_num_bytes(dh_clnt->pub_key);
2368 s2n(n,p);
2369 BN_bn2bin(dh_clnt->pub_key,p);
2370 n+=2;
2371 }
2372
2373 DH_free(dh_clnt);
2374
2375 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2376 }
2377#endif
2378
Adam Langleyc26c8022014-06-20 12:00:00 -07002379#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002380 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2381 {
2382 const EC_GROUP *srvr_group = NULL;
2383 EC_KEY *tkey;
2384 int ecdh_clnt_cert = 0;
2385 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002386#ifndef OPENSSL_NO_PSK
2387 unsigned char *pre_ms;
2388 unsigned char *t;
2389 unsigned int pre_ms_len;
2390 unsigned int i;
2391#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyd06eddd2014-06-20 12:00:00 -07002393 if (s->session->sess_cert == NULL)
2394 {
2395 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2396 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2397 goto err;
2398 }
2399
Adam Langley95c29f32014-06-20 12:00:00 -07002400 /* Did we send out the client's
2401 * ECDH share for use in premaster
2402 * computation as part of client certificate?
2403 * If so, set ecdh_clnt_cert to 1.
2404 */
2405 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2406 {
2407 /* XXX: For now, we do not support client
2408 * authentication using ECDH certificates.
2409 * To add such support, one needs to add
2410 * code that checks for appropriate
2411 * conditions and sets ecdh_clnt_cert to 1.
2412 * For example, the cert have an ECC
2413 * key on the same curve as the server's
2414 * and the key should be authorized for
2415 * key agreement.
2416 *
2417 * One also needs to add code in ssl3_connect
2418 * to skip sending the certificate verify
2419 * message.
2420 *
2421 * if ((s->cert->key->privatekey != NULL) &&
2422 * (s->cert->key->privatekey->type ==
2423 * EVP_PKEY_EC) && ...)
2424 * ecdh_clnt_cert = 1;
2425 */
2426 }
2427
2428 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2429 {
2430 tkey = s->session->sess_cert->peer_ecdh_tmp;
2431 }
2432 else
2433 {
2434 /* Get the Server Public Key from Cert */
2435 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2436 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2437 if ((srvr_pub_pkey == NULL) ||
2438 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2439 (srvr_pub_pkey->pkey.ec == NULL))
2440 {
2441 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2442 goto err;
2443 }
2444
2445 tkey = srvr_pub_pkey->pkey.ec;
2446 }
2447
2448 srvr_group = EC_KEY_get0_group(tkey);
2449 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2450
2451 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2452 {
2453 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2454 goto err;
2455 }
2456
2457 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2458 {
2459 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2460 goto err;
2461 }
2462
2463 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2464 {
2465 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2466 goto err;
2467 }
2468 if (ecdh_clnt_cert)
2469 {
2470 /* Reuse key info from our certificate
2471 * We only need our private key to perform
2472 * the ECDH computation.
2473 */
2474 const BIGNUM *priv_key;
2475 tkey = s->cert->key->privatekey->pkey.ec;
2476 priv_key = EC_KEY_get0_private_key(tkey);
2477 if (priv_key == NULL)
2478 {
2479 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2480 goto err;
2481 }
2482 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2483 {
2484 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2485 goto err;
2486 }
2487 }
2488 else
2489 {
2490 /* Generate a new ECDH key pair */
2491 if (!(EC_KEY_generate_key(clnt_ecdh)))
2492 {
2493 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2494 goto err;
2495 }
2496 }
2497
2498 /* use the 'p' output buffer for the ECDH key, but
2499 * make sure to clear it out afterwards
2500 */
2501
2502 field_size = EC_GROUP_get_degree(srvr_group);
2503 if (field_size <= 0)
2504 {
2505 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2506 goto err;
2507 }
2508 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2509 if (n <= 0)
2510 {
2511 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2512 goto err;
2513 }
2514
Adam Langleyc26c8022014-06-20 12:00:00 -07002515#ifndef OPENSSL_NO_PSK
2516 /* ECDHE PSK ciphersuites from RFC 5489 */
2517 if ((alg_a & SSL_aPSK) && psk_len != 0)
2518 {
2519 pre_ms_len = 2+psk_len+2+n;
2520 pre_ms = OPENSSL_malloc(pre_ms_len);
2521 if (pre_ms == NULL)
2522 {
2523 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2524 goto err;
2525 }
2526 memset(pre_ms, 0, pre_ms_len);
2527 t = pre_ms;
2528 s2n(psk_len, t);
2529 memcpy(t, psk, psk_len);
2530 t += psk_len;
2531 s2n(n, t);
2532 memcpy(t, p, n);
2533 s->session->master_key_length = s->method->ssl3_enc \
2534 -> generate_master_secret(s,
2535 s->session->master_key, pre_ms, pre_ms_len);
2536 OPENSSL_cleanse(pre_ms, pre_ms_len);
2537 OPENSSL_free(pre_ms);
2538 }
2539#endif /* OPENSSL_NO_PSK */
2540 if (!(alg_a & SSL_aPSK))
2541 {
2542 /* generate master key from the result */
2543 s->session->master_key_length = s->method->ssl3_enc \
2544 -> generate_master_secret(s,
2545 s->session->master_key, p, n);
2546 }
Adam Langley95c29f32014-06-20 12:00:00 -07002547 memset(p, 0, n); /* clean up */
2548
Adam Langleyc26c8022014-06-20 12:00:00 -07002549 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002550 {
2551 /* Send empty client key exch message */
2552 n = 0;
2553 }
2554 else
2555 {
2556 /* First check the size of encoding and
2557 * allocate memory accordingly.
2558 */
2559 encoded_pt_len =
2560 EC_POINT_point2oct(srvr_group,
2561 EC_KEY_get0_public_key(clnt_ecdh),
2562 POINT_CONVERSION_UNCOMPRESSED,
2563 NULL, 0, NULL);
2564
2565 encodedPoint = (unsigned char *)
2566 OPENSSL_malloc(encoded_pt_len *
2567 sizeof(unsigned char));
2568 bn_ctx = BN_CTX_new();
2569 if ((encodedPoint == NULL) ||
2570 (bn_ctx == NULL))
2571 {
2572 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2573 goto err;
2574 }
2575
2576 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002577 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2578 EC_KEY_get0_public_key(clnt_ecdh),
2579 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002580 encodedPoint, encoded_pt_len, bn_ctx);
2581
Adam Langleyc26c8022014-06-20 12:00:00 -07002582 n = 0;
2583#ifndef OPENSSL_NO_PSK
2584 if ((alg_a & SSL_aPSK) && psk_len != 0)
2585 {
2586 i = strlen(s->session->psk_identity);
2587 s2n(i, p);
2588 memcpy(p, s->session->psk_identity, i);
2589 p += i;
2590 n = i + 2;
2591 }
2592#endif
2593
2594 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002595 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002596 p += 1;
2597 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002598 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002599 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002600 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002601 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002602 }
2603
2604 /* Free allocated memory */
2605 BN_CTX_free(bn_ctx);
2606 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002607 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002608 EC_KEY_free(clnt_ecdh);
2609 EVP_PKEY_free(srvr_pub_pkey);
2610 }
2611#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002612 else if (alg_k & SSL_kGOST)
Adam Langley95c29f32014-06-20 12:00:00 -07002613 {
2614 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED);
2615 goto err;
2616 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002617 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002618 {
2619 ssl3_send_alert(s, SSL3_AL_FATAL,
2620 SSL_AD_HANDSHAKE_FAILURE);
2621 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2622 goto err;
2623 }
2624
2625 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2626 s->state=SSL3_ST_CW_KEY_EXCH_B;
2627 }
2628
2629 /* SSL3_ST_CW_KEY_EXCH_B */
2630 return ssl_do_write(s);
2631err:
2632#ifndef OPENSSL_NO_ECDH
2633 BN_CTX_free(bn_ctx);
2634 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2635 if (clnt_ecdh != NULL)
2636 EC_KEY_free(clnt_ecdh);
2637 EVP_PKEY_free(srvr_pub_pkey);
2638#endif
2639 return(-1);
2640 }
2641
2642int ssl3_send_client_verify(SSL *s)
2643 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002644 unsigned char *buf, *p;
2645 const EVP_MD *md;
2646 uint8_t digest[EVP_MAX_MD_SIZE];
2647 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002648 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002649 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002650 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002651 size_t signature_length = 0;
2652 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002653
2654 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002655 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002656
2657 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2658 {
2659 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002660 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002661 /* For TLS v1.2 send signature algorithm and signature using
2662 * agreed digest and cached handshake records. Otherwise, use
2663 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002664 */
2665 if (SSL_USE_SIGALGS(s))
2666 {
2667 long hdatalen = 0;
2668 char *hdata;
David Benjamin0e2908a2014-07-09 15:22:43 -04002669 md = s->cert->key->digest;
Adam Langley95c29f32014-06-20 12:00:00 -07002670 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2671 &hdata);
2672 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
2673 {
2674 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2675 goto err;
2676 }
2677 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002678 n += 2;
2679 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2680 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2681 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002682 {
2683 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2684 goto err;
2685 }
Adam Langley95c29f32014-06-20 12:00:00 -07002686 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002687 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002688 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002689 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002690 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002691 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2692 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2693 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2694 * a raw RSA signature, rather than wrapping in a
2695 * DigestInfo. */
2696 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002697 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002698 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002699 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002700 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2701 digest_length = SHA_DIGEST_LENGTH;
2702 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002703 }
2704 else
Adam Langleyaed23062014-06-20 12:00:00 -07002705 {
Adam Langley95c29f32014-06-20 12:00:00 -07002706 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2707 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002708 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002709
2710 /* Sign the digest. */
2711 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2712 if (pctx == NULL)
2713 goto err;
2714
2715 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2716 if (EVP_PKEY_sign_init(pctx) != 1 ||
2717 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2718 EVP_PKEY_sign(pctx, NULL, &signature_length,
2719 digest, digest_length) != 1)
2720 {
2721 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2722 goto err;
2723 }
2724
2725 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2726 {
2727 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2728 goto err;
2729 }
2730
2731 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2732 digest, digest_length) != 1)
2733 {
2734 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2735 goto err;
2736 }
2737
2738 s2n(signature_length, p);
2739 n += signature_length + 2;
2740
2741 /* Now that client auth is completed, we no longer need cached
2742 * handshake records and can digest them. */
2743 if (SSL_USE_SIGALGS(s))
2744 {
2745 if (!ssl3_digest_cached_records(s))
2746 goto err;
2747 }
2748
Adam Langley95c29f32014-06-20 12:00:00 -07002749 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2750 s->state=SSL3_ST_CW_CERT_VRFY_B;
2751 }
2752 EVP_MD_CTX_cleanup(&mctx);
2753 EVP_PKEY_CTX_free(pctx);
2754 return ssl_do_write(s);
2755err:
2756 EVP_MD_CTX_cleanup(&mctx);
2757 EVP_PKEY_CTX_free(pctx);
2758 return(-1);
2759 }
2760
2761/* Check a certificate can be used for client authentication. Currently
2762 * check cert exists, if we have a suitable digest for TLS 1.2 if
2763 * static DH client certificates can be used and optionally checks
2764 * suitability for Suite B.
2765 */
2766static int ssl3_check_client_certificate(SSL *s)
2767 {
2768 unsigned long alg_k;
2769 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2770 return 0;
2771 /* If no suitable signature algorithm can't use certificate */
2772 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2773 return 0;
2774 /* If strict mode check suitability of chain before using it.
2775 * This also adjusts suite B digest if necessary.
2776 */
2777 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2778 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2779 return 0;
2780 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2781 /* See if we can use client certificate for fixed DH */
2782 if (alg_k & (SSL_kDHr|SSL_kDHd))
2783 {
2784 SESS_CERT *scert = s->session->sess_cert;
2785 int i = scert->peer_cert_type;
2786 EVP_PKEY *clkey = NULL, *spkey = NULL;
2787 clkey = s->cert->key->privatekey;
2788 /* If client key not DH assume it can be used */
2789 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2790 return 1;
2791 if (i >= 0)
2792 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2793 if (spkey)
2794 {
2795 /* Compare server and client parameters */
2796 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2797 EVP_PKEY_free(spkey);
2798 if (i != 1)
2799 return 0;
2800 }
2801 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2802 }
2803 return 1;
2804 }
2805
2806int ssl3_send_client_certificate(SSL *s)
2807 {
2808 X509 *x509=NULL;
2809 EVP_PKEY *pkey=NULL;
2810 int i;
2811
2812 if (s->state == SSL3_ST_CW_CERT_A)
2813 {
2814 /* Let cert callback update client certificates if required */
2815 if (s->cert->cert_cb)
2816 {
2817 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2818 if (i < 0)
2819 {
2820 s->rwstate=SSL_X509_LOOKUP;
2821 return -1;
2822 }
2823 if (i == 0)
2824 {
2825 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2826 return 0;
2827 }
2828 s->rwstate=SSL_NOTHING;
2829 }
2830 if (ssl3_check_client_certificate(s))
2831 s->state=SSL3_ST_CW_CERT_C;
2832 else
2833 s->state=SSL3_ST_CW_CERT_B;
2834 }
2835
2836 /* We need to get a client cert */
2837 if (s->state == SSL3_ST_CW_CERT_B)
2838 {
2839 /* If we get an error, we need to
2840 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2841 * We then get retied later */
2842 i=0;
2843 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2844 if (i < 0)
2845 {
2846 s->rwstate=SSL_X509_LOOKUP;
2847 return(-1);
2848 }
2849 s->rwstate=SSL_NOTHING;
2850 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2851 {
2852 s->state=SSL3_ST_CW_CERT_B;
2853 if ( !SSL_use_certificate(s,x509) ||
2854 !SSL_use_PrivateKey(s,pkey))
2855 i=0;
2856 }
2857 else if (i == 1)
2858 {
2859 i=0;
2860 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2861 }
2862
2863 if (x509 != NULL) X509_free(x509);
2864 if (pkey != NULL) EVP_PKEY_free(pkey);
2865 if (i && !ssl3_check_client_certificate(s))
2866 i = 0;
2867 if (i == 0)
2868 {
2869 if (s->version == SSL3_VERSION)
2870 {
2871 s->s3->tmp.cert_req=0;
2872 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2873 return(1);
2874 }
2875 else
2876 {
2877 s->s3->tmp.cert_req=2;
2878 }
2879 }
2880
2881 /* Ok, we have a cert */
2882 s->state=SSL3_ST_CW_CERT_C;
2883 }
2884
2885 if (s->state == SSL3_ST_CW_CERT_C)
2886 {
2887 s->state=SSL3_ST_CW_CERT_D;
2888 ssl3_output_cert_chain(s,
2889 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2890 }
2891 /* SSL3_ST_CW_CERT_D */
2892 return ssl_do_write(s);
2893 }
2894
2895#define has_bits(i,m) (((i)&(m)) == (m))
2896
2897int ssl3_check_cert_and_algorithm(SSL *s)
2898 {
2899 int i,idx;
2900 long alg_k,alg_a;
2901 EVP_PKEY *pkey=NULL;
2902 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002903 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002904#ifndef OPENSSL_NO_DH
2905 DH *dh;
2906#endif
2907
David Benjamine8f3d662014-07-12 01:10:19 -04002908 /* we don't have a certificate */
2909 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2910 return 1;
2911
Adam Langley95c29f32014-06-20 12:00:00 -07002912 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2913 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2914
Adam Langley95c29f32014-06-20 12:00:00 -07002915 sc=s->session->sess_cert;
2916 if (sc == NULL)
2917 {
2918 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2919 goto err;
2920 }
2921
Adam Langley95c29f32014-06-20 12:00:00 -07002922 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002923#ifndef OPENSSL_NO_DH
2924 dh=s->session->sess_cert->peer_dh_tmp;
2925#endif
2926
2927 /* This is the passed certificate */
2928
2929 idx=sc->peer_cert_type;
2930#ifndef OPENSSL_NO_ECDH
2931 if (idx == SSL_PKEY_ECC)
2932 {
2933 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2934 s) == 0)
2935 { /* check failed */
2936 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
2937 goto f_err;
2938 }
2939 else
2940 {
2941 return 1;
2942 }
2943 }
2944 else if (alg_a & SSL_aECDSA)
2945 {
2946 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
2947 goto f_err;
2948 }
2949 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2950 {
2951 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
2952 goto f_err;
2953 }
2954#endif
2955 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2956 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2957 EVP_PKEY_free(pkey);
2958
2959
2960 /* Check that we have a certificate if we require one */
2961 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2962 {
2963 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
2964 goto f_err;
2965 }
2966#ifndef OPENSSL_NO_DSA
2967 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2968 {
2969 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
2970 goto f_err;
2971 }
2972#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002973 if ((alg_k & SSL_kRSA) &&
2974 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2975 {
2976 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2977 goto f_err;
2978 }
Adam Langley95c29f32014-06-20 12:00:00 -07002979#ifndef OPENSSL_NO_DH
2980 if ((alg_k & SSL_kEDH) &&
2981 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2982 {
2983 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
2984 goto f_err;
2985 }
2986 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2987 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2988 {
2989 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
2990 goto f_err;
2991 }
2992#ifndef OPENSSL_NO_DSA
2993 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2994 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2995 {
2996 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
2997 goto f_err;
2998 }
2999#endif
3000#endif
3001
3002 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
3003 {
Adam Langley95c29f32014-06-20 12:00:00 -07003004 if (alg_k & SSL_kRSA)
3005 {
3006 if (rsa == NULL
3007 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3008 {
3009 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3010 goto f_err;
3011 }
3012 }
3013 else
Adam Langley95c29f32014-06-20 12:00:00 -07003014#ifndef OPENSSL_NO_DH
3015 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
3016 {
3017 if (dh == NULL
3018 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3019 {
3020 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3021 goto f_err;
3022 }
3023 }
3024 else
3025#endif
3026 {
3027 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3028 goto f_err;
3029 }
3030 }
3031 return(1);
3032f_err:
3033 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3034err:
3035 return(0);
3036 }
3037
Adam Langley1258b6a2014-06-20 12:00:00 -07003038# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07003039int ssl3_send_next_proto(SSL *s)
3040 {
3041 unsigned int len, padding_len;
3042 unsigned char *d;
3043
3044 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3045 {
3046 len = s->next_proto_negotiated_len;
3047 padding_len = 32 - ((len + 2) % 32);
3048 d = (unsigned char *)s->init_buf->data;
3049 d[4] = len;
3050 memcpy(d + 5, s->next_proto_negotiated, len);
3051 d[5 + len] = padding_len;
3052 memset(d + 6 + len, 0, padding_len);
3053 *(d++)=SSL3_MT_NEXT_PROTO;
3054 l2n3(2 + len + padding_len, d);
3055 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3056 s->init_num = 4 + 2 + len + padding_len;
3057 s->init_off = 0;
3058 }
3059
3060 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3061}
Adam Langley1258b6a2014-06-20 12:00:00 -07003062
3063# endif /* !OPENSSL_NO_NEXTPROTONEG */
3064
3065int ssl3_send_channel_id(SSL *s)
3066 {
3067 unsigned char *d;
3068 int ret = -1, public_key_len;
3069 EVP_MD_CTX md_ctx;
3070 size_t sig_len;
3071 ECDSA_SIG *sig = NULL;
3072 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3073
3074 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3075 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3076
3077 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3078 {
3079 EVP_PKEY *key = NULL;
3080 s->ctx->channel_id_cb(s, &key);
3081 if (key != NULL)
3082 {
3083 s->tlsext_channel_id_private = key;
3084 }
3085 }
3086 if (!s->tlsext_channel_id_private)
3087 {
3088 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3089 return (-1);
3090 }
3091 s->rwstate=SSL_NOTHING;
3092
3093 d = (unsigned char *)s->init_buf->data;
3094 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3095 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3096 if (s->s3->tlsext_channel_id_new)
3097 s2n(TLSEXT_TYPE_channel_id_new, d);
3098 else
3099 s2n(TLSEXT_TYPE_channel_id, d);
3100 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3101
3102 EVP_MD_CTX_init(&md_ctx);
3103
3104 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3105 if (public_key_len <= 0)
3106 {
3107 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
3108 goto err;
3109 }
3110 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3111 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3112 * field elements as 32-byte, big-endian numbers. */
3113 if (public_key_len != 65)
3114 {
3115 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
3116 goto err;
3117 }
3118 public_key = OPENSSL_malloc(public_key_len);
3119 if (!public_key)
3120 {
3121 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3122 goto err;
3123 }
3124
3125 derp = public_key;
3126 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3127
3128 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3129 s->tlsext_channel_id_private) != 1)
3130 {
3131 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
3132 goto err;
3133 }
3134
3135 if (!tls1_channel_id_hash(&md_ctx, s))
3136 goto err;
3137
3138 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3139 {
3140 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3141 goto err;
3142 }
3143
3144 der_sig = OPENSSL_malloc(sig_len);
3145 if (!der_sig)
3146 {
3147 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3148 goto err;
3149 }
3150
3151 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3152 {
3153 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3154 goto err;
3155 }
3156
3157 derp = der_sig;
3158 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
3159 if (sig == NULL)
3160 {
3161 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
3162 goto err;
3163 }
3164
3165 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3166 memcpy(d, public_key + 1, 64);
3167 d += 64;
3168 memset(d, 0, 2 * 32);
3169 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3170 d += 32;
3171 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3172 d += 32;
3173
3174 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3175 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3176 s->init_off = 0;
3177
3178 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3179
3180err:
3181 EVP_MD_CTX_cleanup(&md_ctx);
3182 if (public_key)
3183 OPENSSL_free(public_key);
3184 if (der_sig)
3185 OPENSSL_free(der_sig);
3186 if (sig)
3187 ECDSA_SIG_free(sig);
3188
3189 return ret;
3190 }
Adam Langley95c29f32014-06-20 12:00:00 -07003191
3192/* Check to see if handshake is full or resumed. Usually this is just a
3193 * case of checking to see if a cache hit has occurred. In the case of
3194 * session tickets we have to check the next message to be sure.
3195 */
3196
Adam Langley95c29f32014-06-20 12:00:00 -07003197int ssl3_check_finished(SSL *s)
3198 {
3199 int ok;
3200 long n;
3201 /* If we have no ticket it cannot be a resumed session. */
3202 if (!s->session->tlsext_tick)
3203 return 1;
3204 /* this function is called when we really expect a Certificate
3205 * message, so permit appropriate message length */
3206 n=s->method->ssl_get_message(s,
3207 SSL3_ST_CR_CERT_A,
3208 SSL3_ST_CR_CERT_B,
3209 -1,
3210 s->max_cert_list,
3211 &ok);
3212 if (!ok) return((int)n);
3213 s->s3->tmp.reuse_message = 1;
3214 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3215 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3216 return 2;
3217
3218 return 1;
3219 }
Adam Langley95c29f32014-06-20 12:00:00 -07003220
3221int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3222 {
3223 int i = 0;
3224 /* TODO(fork): remove */
3225#if 0
3226#ifndef OPENSSL_NO_ENGINE
3227 if (s->ctx->client_cert_engine)
3228 {
3229 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3230 SSL_get_client_CA_list(s),
3231 px509, ppkey, NULL, NULL, NULL);
3232 if (i != 0)
3233 return i;
3234 }
3235#endif
3236#endif
3237 if (s->ctx->client_cert_cb)
3238 i = s->ctx->client_cert_cb(s,px509,ppkey);
3239 return i;
3240 }