blob: 3165805a101f41c2bdd2c905b02a222e6ef6fbc0 [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
David Benjamin51b1f742014-07-12 16:31:12 -0400916 d = p = s->init_msg;
Adam Langley95c29f32014-06-20 12:00:00 -0700917 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
David Benjamin51b1f742014-07-12 16:31:12 -04001123 CBS_init(&cbs, 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 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001259 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001260 long n,alg_k,alg_a;
1261 EVP_PKEY *pkey=NULL;
1262 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001263 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001264#ifndef OPENSSL_NO_DH
1265 DH *dh=NULL;
1266#endif
1267#ifndef OPENSSL_NO_ECDH
1268 EC_KEY *ecdh = NULL;
1269 BN_CTX *bn_ctx = NULL;
1270 EC_POINT *srvr_ecpoint = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001271#endif
David Benjamined439582014-07-14 19:13:02 -04001272 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001273
1274 /* use same message size as in ssl3_get_certificate_request()
1275 * as ServerKeyExchange message may be skipped */
1276 n=s->method->ssl_get_message(s,
1277 SSL3_ST_CR_KEY_EXCH_A,
1278 SSL3_ST_CR_KEY_EXCH_B,
1279 -1,
1280 s->max_cert_list,
1281 &ok);
1282 if (!ok) return((int)n);
1283
1284 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1285 {
David Benjamin9c651c92014-07-12 13:27:45 -04001286 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1287 {
1288 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1289 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1290 return -1;
1291 }
1292
Adam Langley95c29f32014-06-20 12:00:00 -07001293#ifndef OPENSSL_NO_PSK
1294 /* In plain PSK ciphersuite, ServerKeyExchange can be
1295 omitted if no identity hint is sent. Set
1296 session->sess_cert anyway to avoid problems
1297 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001298 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001299 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001300 /* PSK ciphersuites that also send a
1301 * Certificate would have already initialized
1302 * |sess_cert|. */
1303 if (s->session->sess_cert == NULL)
1304 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001305 if (s->session->psk_identity_hint)
1306 {
1307 OPENSSL_free(s->session->psk_identity_hint);
1308 s->session->psk_identity_hint = NULL;
1309 }
Adam Langley95c29f32014-06-20 12:00:00 -07001310 }
1311#endif
1312 s->s3->tmp.reuse_message=1;
1313 return(1);
1314 }
1315
David Benjamined439582014-07-14 19:13:02 -04001316 /* Retain a copy of the original CBS to compute the signature
1317 * over. */
1318 CBS_init(&server_key_exchange, s->init_msg, n);
1319 server_key_exchange_orig = server_key_exchange;
1320
Adam Langley95c29f32014-06-20 12:00:00 -07001321 if (s->session->sess_cert != NULL)
1322 {
Adam Langley95c29f32014-06-20 12:00:00 -07001323 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1324 {
1325 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1326 s->session->sess_cert->peer_rsa_tmp=NULL;
1327 }
Adam Langley95c29f32014-06-20 12:00:00 -07001328#ifndef OPENSSL_NO_DH
1329 if (s->session->sess_cert->peer_dh_tmp)
1330 {
1331 DH_free(s->session->sess_cert->peer_dh_tmp);
1332 s->session->sess_cert->peer_dh_tmp=NULL;
1333 }
1334#endif
1335#ifndef OPENSSL_NO_ECDH
1336 if (s->session->sess_cert->peer_ecdh_tmp)
1337 {
1338 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1339 s->session->sess_cert->peer_ecdh_tmp=NULL;
1340 }
1341#endif
1342 }
1343 else
1344 {
1345 s->session->sess_cert=ssl_sess_cert_new();
1346 }
1347
Adam Langley95c29f32014-06-20 12:00:00 -07001348 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 {
David Benjamined439582014-07-14 19:13:02 -04001355 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001356
David Benjamined439582014-07-14 19:13:02 -04001357 /* Each of the PSK key exchanges begins with a
1358 * psk_identity_hint. */
1359 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001360 {
David Benjamined439582014-07-14 19:13:02 -04001361 al = SSL_AD_DECODE_ERROR;
1362 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
1363 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001364 }
Adam Langley95c29f32014-06-20 12:00:00 -07001365
David Benjamined439582014-07-14 19:13:02 -04001366 /* Store PSK identity hint for later use, hint is used in
1367 * ssl3_send_client_key_exchange. Assume that the maximum
1368 * length of a PSK identity hint can be as long as the maximum
1369 * length of a PSK identity. Also do not allow NULL
1370 * characters; identities are saved as C strings.
1371 *
1372 * TODO(davidben): Should invalid hints be ignored? It's a hint
1373 * rather than a specific identity. */
1374 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1375 CBS_contains_zero_byte(&psk_identity_hint))
1376 {
1377 al = SSL_AD_HANDSHAKE_FAILURE;
1378 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
1379 goto f_err;
1380 }
1381
1382 /* Save the identity hint as a C string. */
1383 if (!CBS_strdup(&psk_identity_hint, &s->session->psk_identity_hint))
1384 {
1385 al = SSL_AD_HANDSHAKE_FAILURE;
1386 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1387 goto f_err;
1388 }
Adam Langley95c29f32014-06-20 12:00:00 -07001389 }
Adam Langley95c29f32014-06-20 12:00:00 -07001390#endif /* !OPENSSL_NO_PSK */
Adam Langleyc26c8022014-06-20 12:00:00 -07001391
1392 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001393 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001394 {
David Benjamined439582014-07-14 19:13:02 -04001395 CBS rsa_modulus, rsa_exponent;
1396
1397 /* TODO(davidben): This was originally for export
1398 * reasons. Do we still need to support it? */
1399
1400 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_modulus) ||
1401 CBS_len(&rsa_modulus) == 0 ||
1402 !CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_exponent) ||
1403 CBS_len(&rsa_exponent) == 0)
1404 {
1405 al = SSL_AD_DECODE_ERROR;
1406 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
1407 goto f_err;
1408 }
1409
Adam Langley95c29f32014-06-20 12:00:00 -07001410 if ((rsa=RSA_new()) == NULL)
1411 {
1412 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1413 goto err;
1414 }
Adam Langley95c29f32014-06-20 12:00:00 -07001415
David Benjamined439582014-07-14 19:13:02 -04001416 if (!(rsa->n = BN_bin2bn(CBS_data(&rsa_modulus),
1417 CBS_len(&rsa_modulus), rsa->n)))
Adam Langley95c29f32014-06-20 12:00:00 -07001418 {
1419 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1420 goto err;
1421 }
David Benjamined439582014-07-14 19:13:02 -04001422
1423 if (!(rsa->e = BN_bin2bn(CBS_data(&rsa_exponent),
1424 CBS_len(&rsa_exponent), rsa->e)))
1425 {
1426 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1427 goto err;
1428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
1430 /* this should be because we are using an export cipher */
1431 if (alg_a & SSL_aRSA)
1432 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1433 else
1434 {
1435 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1436 goto err;
1437 }
1438 s->session->sess_cert->peer_rsa_tmp=rsa;
1439 rsa=NULL;
1440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441#ifndef OPENSSL_NO_DH
1442 else if (alg_k & SSL_kEDH)
1443 {
David Benjamined439582014-07-14 19:13:02 -04001444 CBS dh_p, dh_g, dh_Ys;
1445
1446 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1447 CBS_len(&dh_p) == 0 ||
1448 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1449 CBS_len(&dh_g) == 0 ||
1450 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1451 CBS_len(&dh_Ys) == 0)
1452 {
1453 al = SSL_AD_DECODE_ERROR;
1454 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
1455 goto f_err;
1456 }
1457
Adam Langley95c29f32014-06-20 12:00:00 -07001458 if ((dh=DH_new()) == NULL)
1459 {
1460 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_DH_LIB);
1461 goto err;
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463
David Benjamined439582014-07-14 19:13:02 -04001464 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001465 {
1466 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1467 goto err;
1468 }
David Benjamined439582014-07-14 19:13:02 -04001469 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001470 {
1471 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1472 goto err;
1473 }
David Benjamined439582014-07-14 19:13:02 -04001474 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1475 {
1476 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1477 goto err;
1478 }
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langley95c29f32014-06-20 12:00:00 -07001480 if (alg_a & SSL_aRSA)
1481 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001482#ifndef OPENSSL_NO_DSA
1483 else if (alg_a & SSL_aDSS)
1484 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1485#endif
1486 /* else anonymous DH, so no certificate or pkey. */
1487
1488 s->session->sess_cert->peer_dh_tmp=dh;
1489 dh=NULL;
1490 }
1491 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1492 {
1493 al=SSL_AD_ILLEGAL_PARAMETER;
1494 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1495 goto f_err;
1496 }
1497#endif /* !OPENSSL_NO_DH */
1498
1499#ifndef OPENSSL_NO_ECDH
1500 else if (alg_k & SSL_kEECDH)
1501 {
David Benjamined439582014-07-14 19:13:02 -04001502 uint16_t curve_id;
1503 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001504 EC_GROUP *ngroup;
1505 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001506 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001507
David Benjamined439582014-07-14 19:13:02 -04001508 /* Extract elliptic curve parameters and the server's
1509 * ephemeral ECDH public key. Check curve is one of
1510 * our preferences, if not server has sent an invalid
1511 * curve.
1512 */
1513 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001514 {
David Benjamined439582014-07-14 19:13:02 -04001515 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001516 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_CURVE);
1517 goto f_err;
1518 }
1519
David Benjamined439582014-07-14 19:13:02 -04001520 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001521 {
1522 al=SSL_AD_INTERNAL_ERROR;
1523 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1524 goto f_err;
1525 }
1526
David Benjamined439582014-07-14 19:13:02 -04001527 if ((ecdh=EC_KEY_new()) == NULL)
1528 {
1529 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1530 goto err;
1531 }
Adam Langley95c29f32014-06-20 12:00:00 -07001532 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1533 if (ngroup == NULL)
1534 {
1535 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1536 goto err;
1537 }
1538 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1539 {
1540 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1541 goto err;
1542 }
1543 EC_GROUP_free(ngroup);
1544
1545 group = EC_KEY_get0_group(ecdh);
1546
1547 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1548 (EC_GROUP_get_degree(group) > 163))
1549 {
1550 al=SSL_AD_EXPORT_RESTRICTION;
1551 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1552 goto f_err;
1553 }
1554
Adam Langley95c29f32014-06-20 12:00:00 -07001555 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001556 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1557 {
1558 al = SSL_AD_DECODE_ERROR;
1559 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
1560 goto f_err;
1561 }
1562
Adam Langley95c29f32014-06-20 12:00:00 -07001563 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1564 ((bn_ctx = BN_CTX_new()) == NULL))
1565 {
1566 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1567 goto err;
1568 }
1569
David Benjamined439582014-07-14 19:13:02 -04001570 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1571 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001572 {
David Benjamined439582014-07-14 19:13:02 -04001573 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001574 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_ECPOINT);
1575 goto f_err;
1576 }
1577
Adam Langley95c29f32014-06-20 12:00:00 -07001578 /* The ECC/TLS specification does not mention
1579 * the use of DSA to sign ECParameters in the server
1580 * key exchange message. We do support RSA and ECDSA.
1581 */
1582 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001583 else if (alg_a & SSL_aRSA)
1584 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001585#ifndef OPENSSL_NO_ECDSA
1586 else if (alg_a & SSL_aECDSA)
1587 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1588#endif
1589 /* else anonymous ECDH, so no certificate or pkey. */
1590 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1591 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1592 ecdh=NULL;
1593 BN_CTX_free(bn_ctx);
1594 bn_ctx = NULL;
1595 EC_POINT_free(srvr_ecpoint);
1596 srvr_ecpoint = NULL;
1597 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001598#endif /* !OPENSSL_NO_ECDH */
1599
1600 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {
1602 al=SSL_AD_UNEXPECTED_MESSAGE;
1603 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1604 goto f_err;
1605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606
David Benjamined439582014-07-14 19:13:02 -04001607 /* At this point, |server_key_exchange| contains the
1608 * signature, if any, while |server_key_exchange_orig|
1609 * contains the entire message. From that, derive a CBS
1610 * containing just the parameter. */
1611 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1612 CBS_len(&server_key_exchange_orig) -
1613 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001614
1615 /* if it was signed, check the signature */
1616 if (pkey != NULL)
1617 {
David Benjamined439582014-07-14 19:13:02 -04001618 CBS signature;
1619
Adam Langley95c29f32014-06-20 12:00:00 -07001620 if (SSL_USE_SIGALGS(s))
1621 {
David Benjamined439582014-07-14 19:13:02 -04001622 int rv;
1623 const uint8_t *sigalg;
1624
1625 /* The first two bytes are the signature and
1626 * algorithm. */
1627 sigalg = CBS_data(&server_key_exchange);
1628 if (!CBS_skip(&server_key_exchange, 2))
1629 {
1630 al = SSL_AD_DECODE_ERROR;
1631 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
1632 goto f_err;
1633 }
1634 rv = tls12_check_peer_sigalg(&md, s, sigalg, pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001635 if (rv == -1)
1636 goto err;
1637 else if (rv == 0)
1638 {
1639 al = SSL_AD_DECODE_ERROR;
1640 goto f_err;
1641 }
Adam Langley95c29f32014-06-20 12:00:00 -07001642 }
1643 else
1644 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001645
David Benjamined439582014-07-14 19:13:02 -04001646 /* The last field in |server_key_exchange| is the
1647 * signature. */
1648 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1649 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001650 {
David Benjamined439582014-07-14 19:13:02 -04001651 al = SSL_AD_DECODE_ERROR;
1652 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001653 goto f_err;
1654 }
1655
Adam Langley95c29f32014-06-20 12:00:00 -07001656 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1657 {
1658 int num;
David Benjamined439582014-07-14 19:13:02 -04001659 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1660 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langley95c29f32014-06-20 12:00:00 -07001662 q=md_buf;
1663 for (num=2; num > 0; num--)
1664 {
David Benjamined439582014-07-14 19:13:02 -04001665 unsigned int digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001666 EVP_DigestInit_ex(&md_ctx,(num == 2)
1667 ?s->ctx->md5:s->ctx->sha1, NULL);
1668 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1669 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001670 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1671 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1672 q += digest_len;
1673 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001674 }
David Benjamined439582014-07-14 19:13:02 -04001675 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1676 CBS_data(&signature), CBS_len(&signature),
1677 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001678 {
David Benjamined439582014-07-14 19:13:02 -04001679 al = SSL_AD_DECRYPT_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001680 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1681 goto f_err;
1682 }
1683 }
1684 else
Adam Langley95c29f32014-06-20 12:00:00 -07001685 {
1686 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1687 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1688 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001689 EVP_VerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1690 if (EVP_VerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature), pkey) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001691 {
1692 /* bad signature */
1693 al=SSL_AD_DECRYPT_ERROR;
1694 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1695 goto f_err;
1696 }
1697 }
1698 }
1699 else
1700 {
David Benjamine8f3d662014-07-12 01:10:19 -04001701 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001702 {
1703 /* Might be wrong key type, check it */
1704 if (ssl3_check_cert_and_algorithm(s))
1705 /* Otherwise this shouldn't happen */
1706 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1707 goto err;
1708 }
1709 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001710 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001711 {
1712 al=SSL_AD_DECODE_ERROR;
1713 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
1714 goto f_err;
1715 }
1716 }
1717 EVP_PKEY_free(pkey);
1718 EVP_MD_CTX_cleanup(&md_ctx);
1719 return(1);
1720f_err:
1721 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1722err:
1723 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001724 if (rsa != NULL)
1725 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001726#ifndef OPENSSL_NO_DH
1727 if (dh != NULL)
1728 DH_free(dh);
1729#endif
1730#ifndef OPENSSL_NO_ECDH
1731 BN_CTX_free(bn_ctx);
1732 EC_POINT_free(srvr_ecpoint);
1733 if (ecdh != NULL)
1734 EC_KEY_free(ecdh);
1735#endif
1736 EVP_MD_CTX_cleanup(&md_ctx);
1737 return(-1);
1738 }
1739
1740static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1741 {
1742 return(X509_NAME_cmp(*a,*b));
1743 }
1744
1745int ssl3_get_certificate_request(SSL *s)
1746 {
1747 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001748 unsigned long n;
1749 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001750 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001751 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001752 CBS cbs;
1753 CBS certificate_types;
1754 CBS certificate_authorities;
1755 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001756
1757 n=s->method->ssl_get_message(s,
1758 SSL3_ST_CR_CERT_REQ_A,
1759 SSL3_ST_CR_CERT_REQ_B,
1760 -1,
1761 s->max_cert_list,
1762 &ok);
1763
1764 if (!ok) return((int)n);
1765
1766 s->s3->tmp.cert_req=0;
1767
1768 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1769 {
1770 s->s3->tmp.reuse_message=1;
1771 /* If we get here we don't need any cached handshake records
1772 * as we wont be doing client auth.
1773 */
1774 if (s->s3->handshake_buffer)
1775 {
1776 if (!ssl3_digest_cached_records(s))
1777 goto err;
1778 }
1779 return(1);
1780 }
1781
1782 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1783 {
1784 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1785 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1786 goto err;
1787 }
1788
1789 /* TLS does not like anon-DH with client cert */
1790 if (s->version > SSL3_VERSION)
1791 {
1792 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1793 {
1794 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1795 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1796 goto err;
1797 }
1798 }
1799
David Benjamin51b1f742014-07-12 16:31:12 -04001800 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001801
1802 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1803 if (ca_sk == NULL)
1804 {
1805 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1806 goto err;
1807 }
1808
1809 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001810 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001811 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001812 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1813 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1814 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001815 }
David Benjamin676d1e72014-07-08 14:34:10 -04001816 if (!CBS_stow(&certificate_types,
1817 &s->s3->tmp.certificate_types,
1818 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001819 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001820 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1821 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823 if (SSL_USE_SIGALGS(s))
1824 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001825 CBS supported_signature_algorithms;
1826 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001827 {
1828 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001829 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001830 goto err;
1831 }
1832 /* Clear certificate digests and validity flags */
1833 for (i = 0; i < SSL_PKEY_NUM; i++)
1834 {
1835 s->cert->pkeys[i].digest = NULL;
1836 s->cert->pkeys[i].valid_flags = 0;
1837 }
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001838 if (!tls1_process_sigalgs(s,
1839 CBS_data(&supported_signature_algorithms),
1840 CBS_len(&supported_signature_algorithms)))
Adam Langley95c29f32014-06-20 12:00:00 -07001841 {
1842 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1843 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1844 goto err;
1845 }
Adam Langley95c29f32014-06-20 12:00:00 -07001846 }
1847
1848 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001849 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001850 {
1851 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1852 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1853 goto err;
1854 }
1855
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001856 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001857 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001858 CBS distinguished_name;
1859 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001860 {
Adam Langley95c29f32014-06-20 12:00:00 -07001861 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1862 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1863 goto err;
1864 }
1865
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001866 data = CBS_data(&distinguished_name);
1867 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001868 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001869 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1870 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1871 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001872 }
1873
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001874 if (CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
1875 {
1876 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1877 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1878 goto err;
1879 }
1880 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001881 {
1882 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1883 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1884 goto err;
1885 }
1886 if (!sk_X509_NAME_push(ca_sk,xn))
1887 {
1888 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1889 goto err;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891 }
1892
Adam Langley95c29f32014-06-20 12:00:00 -07001893 /* we should setup a certificate to return.... */
1894 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001895 if (s->s3->tmp.ca_names != NULL)
1896 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1897 s->s3->tmp.ca_names=ca_sk;
1898 ca_sk=NULL;
1899
1900 ret=1;
1901err:
1902 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1903 return(ret);
1904 }
1905
Adam Langley95c29f32014-06-20 12:00:00 -07001906int ssl3_get_new_session_ticket(SSL *s)
1907 {
1908 int ok,al,ret=0, ticklen;
1909 long n;
1910 const unsigned char *p;
1911 unsigned char *d;
1912
1913 n=s->method->ssl_get_message(s,
1914 SSL3_ST_CR_SESSION_TICKET_A,
1915 SSL3_ST_CR_SESSION_TICKET_B,
1916 -1,
1917 16384,
1918 &ok);
1919
1920 if (!ok)
1921 return((int)n);
1922
1923 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1924 {
1925 s->s3->tmp.reuse_message=1;
1926 return(1);
1927 }
1928 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1929 {
1930 al=SSL_AD_UNEXPECTED_MESSAGE;
1931 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE);
1932 goto f_err;
1933 }
1934 if (n < 6)
1935 {
1936 /* need at least ticket_lifetime_hint + ticket length */
1937 al = SSL_AD_DECODE_ERROR;
1938 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1939 goto f_err;
1940 }
1941
David Benjamin51b1f742014-07-12 16:31:12 -04001942 p = d = s->init_msg;
Adam Langley95c29f32014-06-20 12:00:00 -07001943 n2l(p, s->session->tlsext_tick_lifetime_hint);
1944 n2s(p, ticklen);
1945 /* ticket_lifetime_hint + ticket_length + ticket */
1946 if (ticklen + 6 != n)
1947 {
1948 al = SSL_AD_DECODE_ERROR;
1949 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1950 goto f_err;
1951 }
1952 if (s->session->tlsext_tick)
1953 {
1954 OPENSSL_free(s->session->tlsext_tick);
1955 s->session->tlsext_ticklen = 0;
1956 }
1957 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1958 if (!s->session->tlsext_tick)
1959 {
1960 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1961 goto err;
1962 }
1963 memcpy(s->session->tlsext_tick, p, ticklen);
1964 s->session->tlsext_ticklen = ticklen;
1965 /* There are two ways to detect a resumed ticket sesion.
1966 * One is to set an appropriate session ID and then the server
1967 * must return a match in ServerHello. This allows the normal
1968 * client session ID matching to work and we know much
1969 * earlier that the ticket has been accepted.
1970 *
1971 * The other way is to set zero length session ID when the
1972 * ticket is presented and rely on the handshake to determine
1973 * session resumption.
1974 *
1975 * We choose the former approach because this fits in with
1976 * assumptions elsewhere in OpenSSL. The session ID is set
1977 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1978 * ticket.
1979 */
1980 EVP_Digest(p, ticklen,
1981 s->session->session_id, &s->session->session_id_length,
1982#ifndef OPENSSL_NO_SHA256
1983 EVP_sha256(), NULL);
1984#else
1985 EVP_sha1(), NULL);
1986#endif
1987 ret=1;
1988 return(ret);
1989f_err:
1990 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1991err:
1992 return(-1);
1993 }
1994
1995int ssl3_get_cert_status(SSL *s)
1996 {
1997 int ok, al;
1998 unsigned long resplen,n;
1999 const unsigned char *p;
2000
2001 n=s->method->ssl_get_message(s,
2002 SSL3_ST_CR_CERT_STATUS_A,
2003 SSL3_ST_CR_CERT_STATUS_B,
2004 SSL3_MT_CERTIFICATE_STATUS,
2005 16384,
2006 &ok);
2007
2008 if (!ok) return((int)n);
2009 if (n < 4)
2010 {
2011 /* need at least status type + length */
2012 al = SSL_AD_DECODE_ERROR;
2013 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2014 goto f_err;
2015 }
David Benjamin51b1f742014-07-12 16:31:12 -04002016 p = s->init_msg;
Adam Langley95c29f32014-06-20 12:00:00 -07002017 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2018 {
2019 al = SSL_AD_DECODE_ERROR;
2020 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_UNSUPPORTED_STATUS_TYPE);
2021 goto f_err;
2022 }
2023 n2l3(p, resplen);
2024 if (resplen + 4 != n)
2025 {
2026 al = SSL_AD_DECODE_ERROR;
2027 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2028 goto f_err;
2029 }
2030 if (s->tlsext_ocsp_resp)
2031 OPENSSL_free(s->tlsext_ocsp_resp);
2032 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2033 if (!s->tlsext_ocsp_resp)
2034 {
2035 al = SSL_AD_INTERNAL_ERROR;
2036 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2037 goto f_err;
2038 }
2039 s->tlsext_ocsp_resplen = resplen;
2040 if (s->ctx->tlsext_status_cb)
2041 {
2042 int ret;
2043 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2044 if (ret == 0)
2045 {
2046 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2047 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
2048 goto f_err;
2049 }
2050 if (ret < 0)
2051 {
2052 al = SSL_AD_INTERNAL_ERROR;
2053 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2054 goto f_err;
2055 }
2056 }
2057 return 1;
2058f_err:
2059 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2060 return(-1);
2061 }
Adam Langley95c29f32014-06-20 12:00:00 -07002062
2063int ssl3_get_server_done(SSL *s)
2064 {
2065 int ok,ret=0;
2066 long n;
2067
2068 n=s->method->ssl_get_message(s,
2069 SSL3_ST_CR_SRVR_DONE_A,
2070 SSL3_ST_CR_SRVR_DONE_B,
2071 SSL3_MT_SERVER_DONE,
2072 30, /* should be very small, like 0 :-) */
2073 &ok);
2074
2075 if (!ok) return((int)n);
2076 if (n > 0)
2077 {
2078 /* should contain no data */
2079 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2080 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
2081 return -1;
2082 }
2083 ret=1;
2084 return(ret);
2085 }
2086
2087
2088int ssl3_send_client_key_exchange(SSL *s)
2089 {
2090 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07002091 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002092 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07002093 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002094 unsigned char *q;
2095 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002096#ifndef OPENSSL_NO_ECDH
2097 EC_KEY *clnt_ecdh = NULL;
2098 const EC_POINT *srvr_ecpoint = NULL;
2099 EVP_PKEY *srvr_pub_pkey = NULL;
2100 unsigned char *encodedPoint = NULL;
2101 int encoded_pt_len = 0;
2102 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002103#ifndef OPENSSL_NO_PSK
2104 unsigned int psk_len = 0;
2105 unsigned char psk[PSK_MAX_PSK_LEN];
2106#endif /* OPENSSL_NO_PSK */
2107#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002108
2109 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2110 {
2111 p = ssl_handshake_start(s);
2112
2113 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002114 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2115
2116#ifndef OPENSSL_NO_PSK
2117 if (alg_a & SSL_aPSK)
2118 {
Adam Langley01797e32014-06-20 12:00:00 -07002119 char identity[PSK_MAX_IDENTITY_LEN + 1];
2120 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002121 unsigned char *t = NULL;
2122 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2123 unsigned int pre_ms_len = 0;
2124 int psk_err = 1;
2125
2126 n = 0;
2127 if (s->psk_client_callback == NULL)
2128 {
2129 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2130 goto err;
2131 }
2132
Adam Langley01797e32014-06-20 12:00:00 -07002133 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002134 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002135 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002136 if (psk_len > PSK_MAX_PSK_LEN)
2137 {
2138 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2139 goto psk_err;
2140 }
2141 else if (psk_len == 0)
2142 {
2143 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2144 goto psk_err;
2145 }
Adam Langley01797e32014-06-20 12:00:00 -07002146 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2147 if (identity_len > PSK_MAX_IDENTITY_LEN)
2148 {
2149 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2150 goto psk_err;
2151 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002152
2153 if (!(alg_k & SSL_kEECDH))
2154 {
2155 /* Create the shared secret now if we're not using ECDHE-PSK.*/
2156 pre_ms_len = 2+psk_len+2+psk_len;
2157 t = pre_ms;
2158 s2n(psk_len, t);
2159 memset(t, 0, psk_len);
2160 t+=psk_len;
2161 s2n(psk_len, t);
2162 memcpy(t, psk, psk_len);
2163
2164 s->session->master_key_length =
2165 s->method->ssl3_enc->generate_master_secret(s,
2166 s->session->master_key,
2167 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002168 s2n(identity_len, p);
2169 memcpy(p, identity, identity_len);
2170 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002171 }
2172
Adam Langleyc26c8022014-06-20 12:00:00 -07002173 if (s->session->psk_identity != NULL)
2174 OPENSSL_free(s->session->psk_identity);
2175 s->session->psk_identity = BUF_strdup(identity);
2176 if (s->session->psk_identity == NULL)
2177 {
2178 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2179 goto psk_err;
2180 }
2181 psk_err = 0;
2182 psk_err:
2183 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2184 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2185 if (psk_err != 0)
2186 {
2187 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2188 goto err;
2189 }
2190 }
2191#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002192
2193 /* Fool emacs indentation */
2194 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002195 else if (alg_k & SSL_kRSA)
2196 {
2197 RSA *rsa;
2198 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2199
2200 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2201 rsa=s->session->sess_cert->peer_rsa_tmp;
2202 else
2203 {
2204 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2205 if ((pkey == NULL) ||
2206 (pkey->type != EVP_PKEY_RSA) ||
2207 (pkey->pkey.rsa == NULL))
2208 {
2209 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2210 goto err;
2211 }
2212 rsa=pkey->pkey.rsa;
2213 EVP_PKEY_free(pkey);
2214 }
2215
2216 tmp_buf[0]=s->client_version>>8;
2217 tmp_buf[1]=s->client_version&0xff;
2218 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2219 goto err;
2220
2221 s->session->master_key_length=sizeof tmp_buf;
2222
2223 q=p;
2224 /* Fix buf for TLS and beyond */
2225 if (s->version > SSL3_VERSION)
2226 p+=2;
2227 n=RSA_public_encrypt(sizeof tmp_buf,
2228 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2229#ifdef PKCS1_CHECK
2230 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2231 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2232#endif
2233 if (n <= 0)
2234 {
2235 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2236 goto err;
2237 }
2238
2239 /* Fix buf for TLS and beyond */
2240 if (s->version > SSL3_VERSION)
2241 {
2242 s2n(n,q);
2243 n+=2;
2244 }
2245
2246 s->session->master_key_length=
2247 s->method->ssl3_enc->generate_master_secret(s,
2248 s->session->master_key,
2249 tmp_buf,sizeof tmp_buf);
2250 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252#ifndef OPENSSL_NO_DH
2253 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2254 {
2255 DH *dh_srvr,*dh_clnt;
2256 SESS_CERT *scert = s->session->sess_cert;
2257
2258 if (scert == NULL)
2259 {
2260 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2261 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2262 goto err;
2263 }
2264
2265 if (scert->peer_dh_tmp != NULL)
2266 dh_srvr=scert->peer_dh_tmp;
2267 else
2268 {
2269 /* we get them from the cert */
2270 int idx = scert->peer_cert_type;
2271 EVP_PKEY *spkey = NULL;
2272 dh_srvr = NULL;
2273 if (idx >= 0)
2274 spkey = X509_get_pubkey(
2275 scert->peer_pkeys[idx].x509);
2276 if (spkey)
2277 {
2278 dh_srvr = EVP_PKEY_get1_DH(spkey);
2279 EVP_PKEY_free(spkey);
2280 }
2281 if (dh_srvr == NULL)
2282 {
2283 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2284 goto err;
2285 }
2286 }
2287 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2288 {
2289 /* Use client certificate key */
2290 EVP_PKEY *clkey = s->cert->key->privatekey;
2291 dh_clnt = NULL;
2292 if (clkey)
2293 dh_clnt = EVP_PKEY_get1_DH(clkey);
2294 if (dh_clnt == NULL)
2295 {
2296 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2297 goto err;
2298 }
2299 }
2300 else
2301 {
2302 /* generate a new random key */
2303 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2304 {
2305 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2306 goto err;
2307 }
2308 if (!DH_generate_key(dh_clnt))
2309 {
2310 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2311 DH_free(dh_clnt);
2312 goto err;
2313 }
2314 }
2315
2316 /* use the 'p' output buffer for the DH key, but
2317 * make sure to clear it out afterwards */
2318
2319 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2320 if (scert->peer_dh_tmp == NULL)
2321 DH_free(dh_srvr);
2322
2323 if (n <= 0)
2324 {
2325 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2326 DH_free(dh_clnt);
2327 goto err;
2328 }
2329
2330 /* generate master key from the result */
2331 s->session->master_key_length=
2332 s->method->ssl3_enc->generate_master_secret(s,
2333 s->session->master_key,p,n);
2334 /* clean up */
2335 memset(p,0,n);
2336
2337 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2338 n = 0;
2339 else
2340 {
2341 /* send off the data */
2342 n=BN_num_bytes(dh_clnt->pub_key);
2343 s2n(n,p);
2344 BN_bn2bin(dh_clnt->pub_key,p);
2345 n+=2;
2346 }
2347
2348 DH_free(dh_clnt);
2349
2350 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2351 }
2352#endif
2353
Adam Langleyc26c8022014-06-20 12:00:00 -07002354#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002355 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2356 {
2357 const EC_GROUP *srvr_group = NULL;
2358 EC_KEY *tkey;
2359 int ecdh_clnt_cert = 0;
2360 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002361#ifndef OPENSSL_NO_PSK
2362 unsigned char *pre_ms;
2363 unsigned char *t;
2364 unsigned int pre_ms_len;
2365 unsigned int i;
2366#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyd06eddd2014-06-20 12:00:00 -07002368 if (s->session->sess_cert == NULL)
2369 {
2370 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2371 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2372 goto err;
2373 }
2374
Adam Langley95c29f32014-06-20 12:00:00 -07002375 /* Did we send out the client's
2376 * ECDH share for use in premaster
2377 * computation as part of client certificate?
2378 * If so, set ecdh_clnt_cert to 1.
2379 */
2380 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2381 {
2382 /* XXX: For now, we do not support client
2383 * authentication using ECDH certificates.
2384 * To add such support, one needs to add
2385 * code that checks for appropriate
2386 * conditions and sets ecdh_clnt_cert to 1.
2387 * For example, the cert have an ECC
2388 * key on the same curve as the server's
2389 * and the key should be authorized for
2390 * key agreement.
2391 *
2392 * One also needs to add code in ssl3_connect
2393 * to skip sending the certificate verify
2394 * message.
2395 *
2396 * if ((s->cert->key->privatekey != NULL) &&
2397 * (s->cert->key->privatekey->type ==
2398 * EVP_PKEY_EC) && ...)
2399 * ecdh_clnt_cert = 1;
2400 */
2401 }
2402
2403 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2404 {
2405 tkey = s->session->sess_cert->peer_ecdh_tmp;
2406 }
2407 else
2408 {
2409 /* Get the Server Public Key from Cert */
2410 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2411 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2412 if ((srvr_pub_pkey == NULL) ||
2413 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2414 (srvr_pub_pkey->pkey.ec == NULL))
2415 {
2416 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2417 goto err;
2418 }
2419
2420 tkey = srvr_pub_pkey->pkey.ec;
2421 }
2422
2423 srvr_group = EC_KEY_get0_group(tkey);
2424 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2425
2426 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2427 {
2428 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2429 goto err;
2430 }
2431
2432 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2433 {
2434 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2435 goto err;
2436 }
2437
2438 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2439 {
2440 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2441 goto err;
2442 }
2443 if (ecdh_clnt_cert)
2444 {
2445 /* Reuse key info from our certificate
2446 * We only need our private key to perform
2447 * the ECDH computation.
2448 */
2449 const BIGNUM *priv_key;
2450 tkey = s->cert->key->privatekey->pkey.ec;
2451 priv_key = EC_KEY_get0_private_key(tkey);
2452 if (priv_key == NULL)
2453 {
2454 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2455 goto err;
2456 }
2457 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2458 {
2459 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2460 goto err;
2461 }
2462 }
2463 else
2464 {
2465 /* Generate a new ECDH key pair */
2466 if (!(EC_KEY_generate_key(clnt_ecdh)))
2467 {
2468 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2469 goto err;
2470 }
2471 }
2472
2473 /* use the 'p' output buffer for the ECDH key, but
2474 * make sure to clear it out afterwards
2475 */
2476
2477 field_size = EC_GROUP_get_degree(srvr_group);
2478 if (field_size <= 0)
2479 {
2480 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2481 goto err;
2482 }
2483 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2484 if (n <= 0)
2485 {
2486 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2487 goto err;
2488 }
2489
Adam Langleyc26c8022014-06-20 12:00:00 -07002490#ifndef OPENSSL_NO_PSK
2491 /* ECDHE PSK ciphersuites from RFC 5489 */
2492 if ((alg_a & SSL_aPSK) && psk_len != 0)
2493 {
2494 pre_ms_len = 2+psk_len+2+n;
2495 pre_ms = OPENSSL_malloc(pre_ms_len);
2496 if (pre_ms == NULL)
2497 {
2498 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2499 goto err;
2500 }
2501 memset(pre_ms, 0, pre_ms_len);
2502 t = pre_ms;
2503 s2n(psk_len, t);
2504 memcpy(t, psk, psk_len);
2505 t += psk_len;
2506 s2n(n, t);
2507 memcpy(t, p, n);
2508 s->session->master_key_length = s->method->ssl3_enc \
2509 -> generate_master_secret(s,
2510 s->session->master_key, pre_ms, pre_ms_len);
2511 OPENSSL_cleanse(pre_ms, pre_ms_len);
2512 OPENSSL_free(pre_ms);
2513 }
2514#endif /* OPENSSL_NO_PSK */
2515 if (!(alg_a & SSL_aPSK))
2516 {
2517 /* generate master key from the result */
2518 s->session->master_key_length = s->method->ssl3_enc \
2519 -> generate_master_secret(s,
2520 s->session->master_key, p, n);
2521 }
Adam Langley95c29f32014-06-20 12:00:00 -07002522 memset(p, 0, n); /* clean up */
2523
Adam Langleyc26c8022014-06-20 12:00:00 -07002524 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002525 {
2526 /* Send empty client key exch message */
2527 n = 0;
2528 }
2529 else
2530 {
2531 /* First check the size of encoding and
2532 * allocate memory accordingly.
2533 */
2534 encoded_pt_len =
2535 EC_POINT_point2oct(srvr_group,
2536 EC_KEY_get0_public_key(clnt_ecdh),
2537 POINT_CONVERSION_UNCOMPRESSED,
2538 NULL, 0, NULL);
2539
2540 encodedPoint = (unsigned char *)
2541 OPENSSL_malloc(encoded_pt_len *
2542 sizeof(unsigned char));
2543 bn_ctx = BN_CTX_new();
2544 if ((encodedPoint == NULL) ||
2545 (bn_ctx == NULL))
2546 {
2547 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2548 goto err;
2549 }
2550
2551 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002552 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2553 EC_KEY_get0_public_key(clnt_ecdh),
2554 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002555 encodedPoint, encoded_pt_len, bn_ctx);
2556
Adam Langleyc26c8022014-06-20 12:00:00 -07002557 n = 0;
2558#ifndef OPENSSL_NO_PSK
2559 if ((alg_a & SSL_aPSK) && psk_len != 0)
2560 {
2561 i = strlen(s->session->psk_identity);
2562 s2n(i, p);
2563 memcpy(p, s->session->psk_identity, i);
2564 p += i;
2565 n = i + 2;
2566 }
2567#endif
2568
2569 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002570 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002571 p += 1;
2572 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002573 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002574 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002575 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002576 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002577 }
2578
2579 /* Free allocated memory */
2580 BN_CTX_free(bn_ctx);
2581 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002582 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002583 EC_KEY_free(clnt_ecdh);
2584 EVP_PKEY_free(srvr_pub_pkey);
2585 }
2586#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002587 else if (alg_k & SSL_kGOST)
Adam Langley95c29f32014-06-20 12:00:00 -07002588 {
2589 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED);
2590 goto err;
2591 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002592 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002593 {
2594 ssl3_send_alert(s, SSL3_AL_FATAL,
2595 SSL_AD_HANDSHAKE_FAILURE);
2596 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2597 goto err;
2598 }
2599
2600 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2601 s->state=SSL3_ST_CW_KEY_EXCH_B;
2602 }
2603
2604 /* SSL3_ST_CW_KEY_EXCH_B */
2605 return ssl_do_write(s);
2606err:
2607#ifndef OPENSSL_NO_ECDH
2608 BN_CTX_free(bn_ctx);
2609 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2610 if (clnt_ecdh != NULL)
2611 EC_KEY_free(clnt_ecdh);
2612 EVP_PKEY_free(srvr_pub_pkey);
2613#endif
2614 return(-1);
2615 }
2616
2617int ssl3_send_client_verify(SSL *s)
2618 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002619 unsigned char *buf, *p;
2620 const EVP_MD *md;
2621 uint8_t digest[EVP_MAX_MD_SIZE];
2622 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002623 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002624 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002625 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002626 size_t signature_length = 0;
2627 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002628
2629 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002630 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002631
2632 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2633 {
2634 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002635 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002636 /* For TLS v1.2 send signature algorithm and signature using
2637 * agreed digest and cached handshake records. Otherwise, use
2638 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002639 */
2640 if (SSL_USE_SIGALGS(s))
2641 {
2642 long hdatalen = 0;
2643 char *hdata;
David Benjamin0e2908a2014-07-09 15:22:43 -04002644 md = s->cert->key->digest;
Adam Langley95c29f32014-06-20 12:00:00 -07002645 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2646 &hdata);
2647 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
2648 {
2649 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2650 goto err;
2651 }
2652 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002653 n += 2;
2654 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2655 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2656 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002657 {
2658 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2659 goto err;
2660 }
Adam Langley95c29f32014-06-20 12:00:00 -07002661 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002662 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002663 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002664 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002665 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002666 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2667 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2668 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2669 * a raw RSA signature, rather than wrapping in a
2670 * DigestInfo. */
2671 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002672 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002673 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002674 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002675 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2676 digest_length = SHA_DIGEST_LENGTH;
2677 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002678 }
2679 else
Adam Langleyaed23062014-06-20 12:00:00 -07002680 {
Adam Langley95c29f32014-06-20 12:00:00 -07002681 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2682 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002683 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002684
2685 /* Sign the digest. */
2686 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2687 if (pctx == NULL)
2688 goto err;
2689
2690 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2691 if (EVP_PKEY_sign_init(pctx) != 1 ||
2692 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2693 EVP_PKEY_sign(pctx, NULL, &signature_length,
2694 digest, digest_length) != 1)
2695 {
2696 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2697 goto err;
2698 }
2699
2700 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2701 {
2702 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2703 goto err;
2704 }
2705
2706 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2707 digest, digest_length) != 1)
2708 {
2709 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2710 goto err;
2711 }
2712
2713 s2n(signature_length, p);
2714 n += signature_length + 2;
2715
2716 /* Now that client auth is completed, we no longer need cached
2717 * handshake records and can digest them. */
2718 if (SSL_USE_SIGALGS(s))
2719 {
2720 if (!ssl3_digest_cached_records(s))
2721 goto err;
2722 }
2723
Adam Langley95c29f32014-06-20 12:00:00 -07002724 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2725 s->state=SSL3_ST_CW_CERT_VRFY_B;
2726 }
2727 EVP_MD_CTX_cleanup(&mctx);
2728 EVP_PKEY_CTX_free(pctx);
2729 return ssl_do_write(s);
2730err:
2731 EVP_MD_CTX_cleanup(&mctx);
2732 EVP_PKEY_CTX_free(pctx);
2733 return(-1);
2734 }
2735
2736/* Check a certificate can be used for client authentication. Currently
2737 * check cert exists, if we have a suitable digest for TLS 1.2 if
2738 * static DH client certificates can be used and optionally checks
2739 * suitability for Suite B.
2740 */
2741static int ssl3_check_client_certificate(SSL *s)
2742 {
2743 unsigned long alg_k;
2744 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2745 return 0;
2746 /* If no suitable signature algorithm can't use certificate */
2747 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2748 return 0;
2749 /* If strict mode check suitability of chain before using it.
2750 * This also adjusts suite B digest if necessary.
2751 */
2752 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2753 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2754 return 0;
2755 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2756 /* See if we can use client certificate for fixed DH */
2757 if (alg_k & (SSL_kDHr|SSL_kDHd))
2758 {
2759 SESS_CERT *scert = s->session->sess_cert;
2760 int i = scert->peer_cert_type;
2761 EVP_PKEY *clkey = NULL, *spkey = NULL;
2762 clkey = s->cert->key->privatekey;
2763 /* If client key not DH assume it can be used */
2764 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2765 return 1;
2766 if (i >= 0)
2767 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2768 if (spkey)
2769 {
2770 /* Compare server and client parameters */
2771 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2772 EVP_PKEY_free(spkey);
2773 if (i != 1)
2774 return 0;
2775 }
2776 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2777 }
2778 return 1;
2779 }
2780
2781int ssl3_send_client_certificate(SSL *s)
2782 {
2783 X509 *x509=NULL;
2784 EVP_PKEY *pkey=NULL;
2785 int i;
2786
2787 if (s->state == SSL3_ST_CW_CERT_A)
2788 {
2789 /* Let cert callback update client certificates if required */
2790 if (s->cert->cert_cb)
2791 {
2792 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2793 if (i < 0)
2794 {
2795 s->rwstate=SSL_X509_LOOKUP;
2796 return -1;
2797 }
2798 if (i == 0)
2799 {
2800 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2801 return 0;
2802 }
2803 s->rwstate=SSL_NOTHING;
2804 }
2805 if (ssl3_check_client_certificate(s))
2806 s->state=SSL3_ST_CW_CERT_C;
2807 else
2808 s->state=SSL3_ST_CW_CERT_B;
2809 }
2810
2811 /* We need to get a client cert */
2812 if (s->state == SSL3_ST_CW_CERT_B)
2813 {
2814 /* If we get an error, we need to
2815 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2816 * We then get retied later */
2817 i=0;
2818 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2819 if (i < 0)
2820 {
2821 s->rwstate=SSL_X509_LOOKUP;
2822 return(-1);
2823 }
2824 s->rwstate=SSL_NOTHING;
2825 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2826 {
2827 s->state=SSL3_ST_CW_CERT_B;
2828 if ( !SSL_use_certificate(s,x509) ||
2829 !SSL_use_PrivateKey(s,pkey))
2830 i=0;
2831 }
2832 else if (i == 1)
2833 {
2834 i=0;
2835 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2836 }
2837
2838 if (x509 != NULL) X509_free(x509);
2839 if (pkey != NULL) EVP_PKEY_free(pkey);
2840 if (i && !ssl3_check_client_certificate(s))
2841 i = 0;
2842 if (i == 0)
2843 {
2844 if (s->version == SSL3_VERSION)
2845 {
2846 s->s3->tmp.cert_req=0;
2847 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2848 return(1);
2849 }
2850 else
2851 {
2852 s->s3->tmp.cert_req=2;
2853 }
2854 }
2855
2856 /* Ok, we have a cert */
2857 s->state=SSL3_ST_CW_CERT_C;
2858 }
2859
2860 if (s->state == SSL3_ST_CW_CERT_C)
2861 {
2862 s->state=SSL3_ST_CW_CERT_D;
2863 ssl3_output_cert_chain(s,
2864 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2865 }
2866 /* SSL3_ST_CW_CERT_D */
2867 return ssl_do_write(s);
2868 }
2869
2870#define has_bits(i,m) (((i)&(m)) == (m))
2871
2872int ssl3_check_cert_and_algorithm(SSL *s)
2873 {
2874 int i,idx;
2875 long alg_k,alg_a;
2876 EVP_PKEY *pkey=NULL;
2877 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002878 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002879#ifndef OPENSSL_NO_DH
2880 DH *dh;
2881#endif
2882
David Benjamine8f3d662014-07-12 01:10:19 -04002883 /* we don't have a certificate */
2884 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2885 return 1;
2886
Adam Langley95c29f32014-06-20 12:00:00 -07002887 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2888 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2889
Adam Langley95c29f32014-06-20 12:00:00 -07002890 sc=s->session->sess_cert;
2891 if (sc == NULL)
2892 {
2893 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2894 goto err;
2895 }
2896
Adam Langley95c29f32014-06-20 12:00:00 -07002897 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002898#ifndef OPENSSL_NO_DH
2899 dh=s->session->sess_cert->peer_dh_tmp;
2900#endif
2901
2902 /* This is the passed certificate */
2903
2904 idx=sc->peer_cert_type;
2905#ifndef OPENSSL_NO_ECDH
2906 if (idx == SSL_PKEY_ECC)
2907 {
2908 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2909 s) == 0)
2910 { /* check failed */
2911 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
2912 goto f_err;
2913 }
2914 else
2915 {
2916 return 1;
2917 }
2918 }
2919 else if (alg_a & SSL_aECDSA)
2920 {
2921 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
2922 goto f_err;
2923 }
2924 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2925 {
2926 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
2927 goto f_err;
2928 }
2929#endif
2930 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2931 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2932 EVP_PKEY_free(pkey);
2933
2934
2935 /* Check that we have a certificate if we require one */
2936 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2937 {
2938 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
2939 goto f_err;
2940 }
2941#ifndef OPENSSL_NO_DSA
2942 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2943 {
2944 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
2945 goto f_err;
2946 }
2947#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002948 if ((alg_k & SSL_kRSA) &&
2949 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2950 {
2951 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2952 goto f_err;
2953 }
Adam Langley95c29f32014-06-20 12:00:00 -07002954#ifndef OPENSSL_NO_DH
2955 if ((alg_k & SSL_kEDH) &&
2956 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2957 {
2958 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
2959 goto f_err;
2960 }
2961 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2962 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2963 {
2964 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
2965 goto f_err;
2966 }
2967#ifndef OPENSSL_NO_DSA
2968 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2969 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2970 {
2971 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
2972 goto f_err;
2973 }
2974#endif
2975#endif
2976
2977 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2978 {
Adam Langley95c29f32014-06-20 12:00:00 -07002979 if (alg_k & SSL_kRSA)
2980 {
2981 if (rsa == NULL
2982 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2983 {
2984 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2985 goto f_err;
2986 }
2987 }
2988 else
Adam Langley95c29f32014-06-20 12:00:00 -07002989#ifndef OPENSSL_NO_DH
2990 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2991 {
2992 if (dh == NULL
2993 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2994 {
2995 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2996 goto f_err;
2997 }
2998 }
2999 else
3000#endif
3001 {
3002 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3003 goto f_err;
3004 }
3005 }
3006 return(1);
3007f_err:
3008 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3009err:
3010 return(0);
3011 }
3012
Adam Langley1258b6a2014-06-20 12:00:00 -07003013# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07003014int ssl3_send_next_proto(SSL *s)
3015 {
3016 unsigned int len, padding_len;
3017 unsigned char *d;
3018
3019 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3020 {
3021 len = s->next_proto_negotiated_len;
3022 padding_len = 32 - ((len + 2) % 32);
3023 d = (unsigned char *)s->init_buf->data;
3024 d[4] = len;
3025 memcpy(d + 5, s->next_proto_negotiated, len);
3026 d[5 + len] = padding_len;
3027 memset(d + 6 + len, 0, padding_len);
3028 *(d++)=SSL3_MT_NEXT_PROTO;
3029 l2n3(2 + len + padding_len, d);
3030 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3031 s->init_num = 4 + 2 + len + padding_len;
3032 s->init_off = 0;
3033 }
3034
3035 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3036}
Adam Langley1258b6a2014-06-20 12:00:00 -07003037
3038# endif /* !OPENSSL_NO_NEXTPROTONEG */
3039
3040int ssl3_send_channel_id(SSL *s)
3041 {
3042 unsigned char *d;
3043 int ret = -1, public_key_len;
3044 EVP_MD_CTX md_ctx;
3045 size_t sig_len;
3046 ECDSA_SIG *sig = NULL;
3047 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3048
3049 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3050 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3051
3052 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3053 {
3054 EVP_PKEY *key = NULL;
3055 s->ctx->channel_id_cb(s, &key);
3056 if (key != NULL)
3057 {
3058 s->tlsext_channel_id_private = key;
3059 }
3060 }
3061 if (!s->tlsext_channel_id_private)
3062 {
3063 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3064 return (-1);
3065 }
3066 s->rwstate=SSL_NOTHING;
3067
3068 d = (unsigned char *)s->init_buf->data;
3069 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3070 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3071 if (s->s3->tlsext_channel_id_new)
3072 s2n(TLSEXT_TYPE_channel_id_new, d);
3073 else
3074 s2n(TLSEXT_TYPE_channel_id, d);
3075 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3076
3077 EVP_MD_CTX_init(&md_ctx);
3078
3079 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3080 if (public_key_len <= 0)
3081 {
3082 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
3083 goto err;
3084 }
3085 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3086 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3087 * field elements as 32-byte, big-endian numbers. */
3088 if (public_key_len != 65)
3089 {
3090 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
3091 goto err;
3092 }
3093 public_key = OPENSSL_malloc(public_key_len);
3094 if (!public_key)
3095 {
3096 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3097 goto err;
3098 }
3099
3100 derp = public_key;
3101 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3102
3103 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3104 s->tlsext_channel_id_private) != 1)
3105 {
3106 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
3107 goto err;
3108 }
3109
3110 if (!tls1_channel_id_hash(&md_ctx, s))
3111 goto err;
3112
3113 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3114 {
3115 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3116 goto err;
3117 }
3118
3119 der_sig = OPENSSL_malloc(sig_len);
3120 if (!der_sig)
3121 {
3122 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3123 goto err;
3124 }
3125
3126 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3127 {
3128 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3129 goto err;
3130 }
3131
3132 derp = der_sig;
3133 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
3134 if (sig == NULL)
3135 {
3136 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
3137 goto err;
3138 }
3139
3140 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3141 memcpy(d, public_key + 1, 64);
3142 d += 64;
3143 memset(d, 0, 2 * 32);
3144 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3145 d += 32;
3146 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3147 d += 32;
3148
3149 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3150 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3151 s->init_off = 0;
3152
3153 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3154
3155err:
3156 EVP_MD_CTX_cleanup(&md_ctx);
3157 if (public_key)
3158 OPENSSL_free(public_key);
3159 if (der_sig)
3160 OPENSSL_free(der_sig);
3161 if (sig)
3162 ECDSA_SIG_free(sig);
3163
3164 return ret;
3165 }
Adam Langley95c29f32014-06-20 12:00:00 -07003166
3167/* Check to see if handshake is full or resumed. Usually this is just a
3168 * case of checking to see if a cache hit has occurred. In the case of
3169 * session tickets we have to check the next message to be sure.
3170 */
3171
Adam Langley95c29f32014-06-20 12:00:00 -07003172int ssl3_check_finished(SSL *s)
3173 {
3174 int ok;
3175 long n;
3176 /* If we have no ticket it cannot be a resumed session. */
3177 if (!s->session->tlsext_tick)
3178 return 1;
3179 /* this function is called when we really expect a Certificate
3180 * message, so permit appropriate message length */
3181 n=s->method->ssl_get_message(s,
3182 SSL3_ST_CR_CERT_A,
3183 SSL3_ST_CR_CERT_B,
3184 -1,
3185 s->max_cert_list,
3186 &ok);
3187 if (!ok) return((int)n);
3188 s->s3->tmp.reuse_message = 1;
3189 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3190 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3191 return 2;
3192
3193 return 1;
3194 }
Adam Langley95c29f32014-06-20 12:00:00 -07003195
3196int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3197 {
3198 int i = 0;
3199 /* TODO(fork): remove */
3200#if 0
3201#ifndef OPENSSL_NO_ENGINE
3202 if (s->ctx->client_cert_engine)
3203 {
3204 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3205 SSL_get_client_CA_list(s),
3206 px509, ppkey, NULL, NULL, NULL);
3207 if (i != 0)
3208 return i;
3209 }
3210#endif
3211#endif
3212 if (s->ctx->client_cert_cb)
3213 i = s->ctx->client_cert_cb(s,px509,ppkey);
3214 return i;
3215 }