blob: e590832c67d0e0beeef7023af0a32c60c8edb36c [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 {
David Benjamine044c3d2014-07-14 19:14:12 -04001908 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001909 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001910 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001911
1912 n=s->method->ssl_get_message(s,
1913 SSL3_ST_CR_SESSION_TICKET_A,
1914 SSL3_ST_CR_SESSION_TICKET_B,
1915 -1,
1916 16384,
1917 &ok);
1918
1919 if (!ok)
1920 return((int)n);
1921
1922 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1923 {
1924 s->s3->tmp.reuse_message=1;
1925 return(1);
1926 }
1927 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1928 {
1929 al=SSL_AD_UNEXPECTED_MESSAGE;
1930 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE);
1931 goto f_err;
1932 }
David Benjamine044c3d2014-07-14 19:14:12 -04001933
1934 CBS_init(&new_session_ticket, s->init_msg, n);
1935
1936 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1937 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1938 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001939 {
Adam Langley95c29f32014-06-20 12:00:00 -07001940 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001941 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001942 goto f_err;
1943 }
1944
David Benjamine044c3d2014-07-14 19:14:12 -04001945 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001946 {
1947 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1948 goto err;
1949 }
David Benjamine044c3d2014-07-14 19:14:12 -04001950
Adam Langley95c29f32014-06-20 12:00:00 -07001951 /* There are two ways to detect a resumed ticket sesion.
1952 * One is to set an appropriate session ID and then the server
1953 * must return a match in ServerHello. This allows the normal
1954 * client session ID matching to work and we know much
1955 * earlier that the ticket has been accepted.
1956 *
1957 * The other way is to set zero length session ID when the
1958 * ticket is presented and rely on the handshake to determine
1959 * session resumption.
1960 *
1961 * We choose the former approach because this fits in with
1962 * assumptions elsewhere in OpenSSL. The session ID is set
1963 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1964 * ticket.
1965 */
David Benjamine044c3d2014-07-14 19:14:12 -04001966 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001967 s->session->session_id, &s->session->session_id_length,
1968#ifndef OPENSSL_NO_SHA256
1969 EVP_sha256(), NULL);
1970#else
1971 EVP_sha1(), NULL);
1972#endif
1973 ret=1;
1974 return(ret);
1975f_err:
1976 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1977err:
1978 return(-1);
1979 }
1980
1981int ssl3_get_cert_status(SSL *s)
1982 {
1983 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001984 long n;
1985 CBS certificate_status, ocsp_response;
1986 uint8_t status_type;
1987 size_t resplen;
Adam Langley95c29f32014-06-20 12:00:00 -07001988
1989 n=s->method->ssl_get_message(s,
1990 SSL3_ST_CR_CERT_STATUS_A,
1991 SSL3_ST_CR_CERT_STATUS_B,
1992 SSL3_MT_CERTIFICATE_STATUS,
1993 16384,
1994 &ok);
1995
1996 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001997
1998 CBS_init(&certificate_status, s->init_msg, n);
1999 if (!CBS_get_u8(&certificate_status, &status_type) ||
2000 status_type != TLSEXT_STATUSTYPE_ocsp ||
2001 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
2002 CBS_len(&ocsp_response) == 0 ||
2003 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002004 {
2005 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04002006 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002007 goto f_err;
2008 }
David Benjamin46062682014-07-14 19:14:32 -04002009
2010 /* TODO(davidben): Make tlsext_ocsp_resplen a
2011 * size_t. Currently it uses -1 to signal no response. The
2012 * spec does not allow ocsp_response to be zero-length, so
2013 * using 0 should be fine. */
2014 if (!CBS_stow(&ocsp_response, &s->tlsext_ocsp_resp, &resplen))
Adam Langley95c29f32014-06-20 12:00:00 -07002015 {
2016 al = SSL_AD_INTERNAL_ERROR;
2017 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2018 goto f_err;
2019 }
2020 s->tlsext_ocsp_resplen = resplen;
2021 if (s->ctx->tlsext_status_cb)
2022 {
2023 int ret;
2024 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2025 if (ret == 0)
2026 {
2027 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2028 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
2029 goto f_err;
2030 }
2031 if (ret < 0)
2032 {
2033 al = SSL_AD_INTERNAL_ERROR;
2034 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2035 goto f_err;
2036 }
2037 }
2038 return 1;
2039f_err:
2040 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2041 return(-1);
2042 }
Adam Langley95c29f32014-06-20 12:00:00 -07002043
2044int ssl3_get_server_done(SSL *s)
2045 {
2046 int ok,ret=0;
2047 long n;
2048
2049 n=s->method->ssl_get_message(s,
2050 SSL3_ST_CR_SRVR_DONE_A,
2051 SSL3_ST_CR_SRVR_DONE_B,
2052 SSL3_MT_SERVER_DONE,
2053 30, /* should be very small, like 0 :-) */
2054 &ok);
2055
2056 if (!ok) return((int)n);
2057 if (n > 0)
2058 {
2059 /* should contain no data */
2060 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2061 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
2062 return -1;
2063 }
2064 ret=1;
2065 return(ret);
2066 }
2067
2068
2069int ssl3_send_client_key_exchange(SSL *s)
2070 {
2071 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07002072 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002073 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07002074 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002075 unsigned char *q;
2076 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002077#ifndef OPENSSL_NO_ECDH
2078 EC_KEY *clnt_ecdh = NULL;
2079 const EC_POINT *srvr_ecpoint = NULL;
2080 EVP_PKEY *srvr_pub_pkey = NULL;
2081 unsigned char *encodedPoint = NULL;
2082 int encoded_pt_len = 0;
2083 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002084#ifndef OPENSSL_NO_PSK
2085 unsigned int psk_len = 0;
2086 unsigned char psk[PSK_MAX_PSK_LEN];
2087#endif /* OPENSSL_NO_PSK */
2088#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002089
2090 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2091 {
2092 p = ssl_handshake_start(s);
2093
2094 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002095 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2096
2097#ifndef OPENSSL_NO_PSK
2098 if (alg_a & SSL_aPSK)
2099 {
Adam Langley01797e32014-06-20 12:00:00 -07002100 char identity[PSK_MAX_IDENTITY_LEN + 1];
2101 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002102 unsigned char *t = NULL;
2103 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2104 unsigned int pre_ms_len = 0;
2105 int psk_err = 1;
2106
2107 n = 0;
2108 if (s->psk_client_callback == NULL)
2109 {
2110 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2111 goto err;
2112 }
2113
Adam Langley01797e32014-06-20 12:00:00 -07002114 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002115 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002116 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002117 if (psk_len > PSK_MAX_PSK_LEN)
2118 {
2119 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2120 goto psk_err;
2121 }
2122 else if (psk_len == 0)
2123 {
2124 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2125 goto psk_err;
2126 }
Adam Langley01797e32014-06-20 12:00:00 -07002127 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2128 if (identity_len > PSK_MAX_IDENTITY_LEN)
2129 {
2130 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2131 goto psk_err;
2132 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002133
2134 if (!(alg_k & SSL_kEECDH))
2135 {
2136 /* Create the shared secret now if we're not using ECDHE-PSK.*/
2137 pre_ms_len = 2+psk_len+2+psk_len;
2138 t = pre_ms;
2139 s2n(psk_len, t);
2140 memset(t, 0, psk_len);
2141 t+=psk_len;
2142 s2n(psk_len, t);
2143 memcpy(t, psk, psk_len);
2144
2145 s->session->master_key_length =
2146 s->method->ssl3_enc->generate_master_secret(s,
2147 s->session->master_key,
2148 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002149 s2n(identity_len, p);
2150 memcpy(p, identity, identity_len);
2151 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002152 }
2153
Adam Langleyc26c8022014-06-20 12:00:00 -07002154 if (s->session->psk_identity != NULL)
2155 OPENSSL_free(s->session->psk_identity);
2156 s->session->psk_identity = BUF_strdup(identity);
2157 if (s->session->psk_identity == NULL)
2158 {
2159 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2160 goto psk_err;
2161 }
2162 psk_err = 0;
2163 psk_err:
2164 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2165 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2166 if (psk_err != 0)
2167 {
2168 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2169 goto err;
2170 }
2171 }
2172#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002173
2174 /* Fool emacs indentation */
2175 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002176 else if (alg_k & SSL_kRSA)
2177 {
2178 RSA *rsa;
2179 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2180
2181 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2182 rsa=s->session->sess_cert->peer_rsa_tmp;
2183 else
2184 {
2185 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2186 if ((pkey == NULL) ||
2187 (pkey->type != EVP_PKEY_RSA) ||
2188 (pkey->pkey.rsa == NULL))
2189 {
2190 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2191 goto err;
2192 }
2193 rsa=pkey->pkey.rsa;
2194 EVP_PKEY_free(pkey);
2195 }
2196
2197 tmp_buf[0]=s->client_version>>8;
2198 tmp_buf[1]=s->client_version&0xff;
2199 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2200 goto err;
2201
2202 s->session->master_key_length=sizeof tmp_buf;
2203
2204 q=p;
2205 /* Fix buf for TLS and beyond */
2206 if (s->version > SSL3_VERSION)
2207 p+=2;
2208 n=RSA_public_encrypt(sizeof tmp_buf,
2209 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2210#ifdef PKCS1_CHECK
2211 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2212 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2213#endif
2214 if (n <= 0)
2215 {
2216 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2217 goto err;
2218 }
2219
2220 /* Fix buf for TLS and beyond */
2221 if (s->version > SSL3_VERSION)
2222 {
2223 s2n(n,q);
2224 n+=2;
2225 }
2226
2227 s->session->master_key_length=
2228 s->method->ssl3_enc->generate_master_secret(s,
2229 s->session->master_key,
2230 tmp_buf,sizeof tmp_buf);
2231 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2232 }
Adam Langley95c29f32014-06-20 12:00:00 -07002233#ifndef OPENSSL_NO_DH
2234 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2235 {
2236 DH *dh_srvr,*dh_clnt;
2237 SESS_CERT *scert = s->session->sess_cert;
2238
2239 if (scert == NULL)
2240 {
2241 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2242 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2243 goto err;
2244 }
2245
2246 if (scert->peer_dh_tmp != NULL)
2247 dh_srvr=scert->peer_dh_tmp;
2248 else
2249 {
2250 /* we get them from the cert */
2251 int idx = scert->peer_cert_type;
2252 EVP_PKEY *spkey = NULL;
2253 dh_srvr = NULL;
2254 if (idx >= 0)
2255 spkey = X509_get_pubkey(
2256 scert->peer_pkeys[idx].x509);
2257 if (spkey)
2258 {
2259 dh_srvr = EVP_PKEY_get1_DH(spkey);
2260 EVP_PKEY_free(spkey);
2261 }
2262 if (dh_srvr == NULL)
2263 {
2264 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2265 goto err;
2266 }
2267 }
2268 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2269 {
2270 /* Use client certificate key */
2271 EVP_PKEY *clkey = s->cert->key->privatekey;
2272 dh_clnt = NULL;
2273 if (clkey)
2274 dh_clnt = EVP_PKEY_get1_DH(clkey);
2275 if (dh_clnt == NULL)
2276 {
2277 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2278 goto err;
2279 }
2280 }
2281 else
2282 {
2283 /* generate a new random key */
2284 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2285 {
2286 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2287 goto err;
2288 }
2289 if (!DH_generate_key(dh_clnt))
2290 {
2291 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2292 DH_free(dh_clnt);
2293 goto err;
2294 }
2295 }
2296
2297 /* use the 'p' output buffer for the DH key, but
2298 * make sure to clear it out afterwards */
2299
2300 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2301 if (scert->peer_dh_tmp == NULL)
2302 DH_free(dh_srvr);
2303
2304 if (n <= 0)
2305 {
2306 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2307 DH_free(dh_clnt);
2308 goto err;
2309 }
2310
2311 /* generate master key from the result */
2312 s->session->master_key_length=
2313 s->method->ssl3_enc->generate_master_secret(s,
2314 s->session->master_key,p,n);
2315 /* clean up */
2316 memset(p,0,n);
2317
2318 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2319 n = 0;
2320 else
2321 {
2322 /* send off the data */
2323 n=BN_num_bytes(dh_clnt->pub_key);
2324 s2n(n,p);
2325 BN_bn2bin(dh_clnt->pub_key,p);
2326 n+=2;
2327 }
2328
2329 DH_free(dh_clnt);
2330
2331 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2332 }
2333#endif
2334
Adam Langleyc26c8022014-06-20 12:00:00 -07002335#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002336 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2337 {
2338 const EC_GROUP *srvr_group = NULL;
2339 EC_KEY *tkey;
2340 int ecdh_clnt_cert = 0;
2341 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002342#ifndef OPENSSL_NO_PSK
2343 unsigned char *pre_ms;
2344 unsigned char *t;
2345 unsigned int pre_ms_len;
2346 unsigned int i;
2347#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyd06eddd2014-06-20 12:00:00 -07002349 if (s->session->sess_cert == NULL)
2350 {
2351 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2352 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2353 goto err;
2354 }
2355
Adam Langley95c29f32014-06-20 12:00:00 -07002356 /* Did we send out the client's
2357 * ECDH share for use in premaster
2358 * computation as part of client certificate?
2359 * If so, set ecdh_clnt_cert to 1.
2360 */
2361 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2362 {
2363 /* XXX: For now, we do not support client
2364 * authentication using ECDH certificates.
2365 * To add such support, one needs to add
2366 * code that checks for appropriate
2367 * conditions and sets ecdh_clnt_cert to 1.
2368 * For example, the cert have an ECC
2369 * key on the same curve as the server's
2370 * and the key should be authorized for
2371 * key agreement.
2372 *
2373 * One also needs to add code in ssl3_connect
2374 * to skip sending the certificate verify
2375 * message.
2376 *
2377 * if ((s->cert->key->privatekey != NULL) &&
2378 * (s->cert->key->privatekey->type ==
2379 * EVP_PKEY_EC) && ...)
2380 * ecdh_clnt_cert = 1;
2381 */
2382 }
2383
2384 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2385 {
2386 tkey = s->session->sess_cert->peer_ecdh_tmp;
2387 }
2388 else
2389 {
2390 /* Get the Server Public Key from Cert */
2391 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2392 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2393 if ((srvr_pub_pkey == NULL) ||
2394 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2395 (srvr_pub_pkey->pkey.ec == NULL))
2396 {
2397 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2398 goto err;
2399 }
2400
2401 tkey = srvr_pub_pkey->pkey.ec;
2402 }
2403
2404 srvr_group = EC_KEY_get0_group(tkey);
2405 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2406
2407 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2408 {
2409 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2410 goto err;
2411 }
2412
2413 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2414 {
2415 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2416 goto err;
2417 }
2418
2419 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2420 {
2421 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2422 goto err;
2423 }
2424 if (ecdh_clnt_cert)
2425 {
2426 /* Reuse key info from our certificate
2427 * We only need our private key to perform
2428 * the ECDH computation.
2429 */
2430 const BIGNUM *priv_key;
2431 tkey = s->cert->key->privatekey->pkey.ec;
2432 priv_key = EC_KEY_get0_private_key(tkey);
2433 if (priv_key == NULL)
2434 {
2435 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2436 goto err;
2437 }
2438 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2439 {
2440 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2441 goto err;
2442 }
2443 }
2444 else
2445 {
2446 /* Generate a new ECDH key pair */
2447 if (!(EC_KEY_generate_key(clnt_ecdh)))
2448 {
2449 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2450 goto err;
2451 }
2452 }
2453
2454 /* use the 'p' output buffer for the ECDH key, but
2455 * make sure to clear it out afterwards
2456 */
2457
2458 field_size = EC_GROUP_get_degree(srvr_group);
2459 if (field_size <= 0)
2460 {
2461 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2462 goto err;
2463 }
2464 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2465 if (n <= 0)
2466 {
2467 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2468 goto err;
2469 }
2470
Adam Langleyc26c8022014-06-20 12:00:00 -07002471#ifndef OPENSSL_NO_PSK
2472 /* ECDHE PSK ciphersuites from RFC 5489 */
2473 if ((alg_a & SSL_aPSK) && psk_len != 0)
2474 {
2475 pre_ms_len = 2+psk_len+2+n;
2476 pre_ms = OPENSSL_malloc(pre_ms_len);
2477 if (pre_ms == NULL)
2478 {
2479 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2480 goto err;
2481 }
2482 memset(pre_ms, 0, pre_ms_len);
2483 t = pre_ms;
2484 s2n(psk_len, t);
2485 memcpy(t, psk, psk_len);
2486 t += psk_len;
2487 s2n(n, t);
2488 memcpy(t, p, n);
2489 s->session->master_key_length = s->method->ssl3_enc \
2490 -> generate_master_secret(s,
2491 s->session->master_key, pre_ms, pre_ms_len);
2492 OPENSSL_cleanse(pre_ms, pre_ms_len);
2493 OPENSSL_free(pre_ms);
2494 }
2495#endif /* OPENSSL_NO_PSK */
2496 if (!(alg_a & SSL_aPSK))
2497 {
2498 /* generate master key from the result */
2499 s->session->master_key_length = s->method->ssl3_enc \
2500 -> generate_master_secret(s,
2501 s->session->master_key, p, n);
2502 }
Adam Langley95c29f32014-06-20 12:00:00 -07002503 memset(p, 0, n); /* clean up */
2504
Adam Langleyc26c8022014-06-20 12:00:00 -07002505 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002506 {
2507 /* Send empty client key exch message */
2508 n = 0;
2509 }
2510 else
2511 {
2512 /* First check the size of encoding and
2513 * allocate memory accordingly.
2514 */
2515 encoded_pt_len =
2516 EC_POINT_point2oct(srvr_group,
2517 EC_KEY_get0_public_key(clnt_ecdh),
2518 POINT_CONVERSION_UNCOMPRESSED,
2519 NULL, 0, NULL);
2520
2521 encodedPoint = (unsigned char *)
2522 OPENSSL_malloc(encoded_pt_len *
2523 sizeof(unsigned char));
2524 bn_ctx = BN_CTX_new();
2525 if ((encodedPoint == NULL) ||
2526 (bn_ctx == NULL))
2527 {
2528 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2529 goto err;
2530 }
2531
2532 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002533 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2534 EC_KEY_get0_public_key(clnt_ecdh),
2535 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002536 encodedPoint, encoded_pt_len, bn_ctx);
2537
Adam Langleyc26c8022014-06-20 12:00:00 -07002538 n = 0;
2539#ifndef OPENSSL_NO_PSK
2540 if ((alg_a & SSL_aPSK) && psk_len != 0)
2541 {
2542 i = strlen(s->session->psk_identity);
2543 s2n(i, p);
2544 memcpy(p, s->session->psk_identity, i);
2545 p += i;
2546 n = i + 2;
2547 }
2548#endif
2549
2550 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002551 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002552 p += 1;
2553 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002554 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002555 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002556 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002557 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002558 }
2559
2560 /* Free allocated memory */
2561 BN_CTX_free(bn_ctx);
2562 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002563 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002564 EC_KEY_free(clnt_ecdh);
2565 EVP_PKEY_free(srvr_pub_pkey);
2566 }
2567#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002568 else if (alg_k & SSL_kGOST)
Adam Langley95c29f32014-06-20 12:00:00 -07002569 {
2570 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED);
2571 goto err;
2572 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002573 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002574 {
2575 ssl3_send_alert(s, SSL3_AL_FATAL,
2576 SSL_AD_HANDSHAKE_FAILURE);
2577 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2578 goto err;
2579 }
2580
2581 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2582 s->state=SSL3_ST_CW_KEY_EXCH_B;
2583 }
2584
2585 /* SSL3_ST_CW_KEY_EXCH_B */
2586 return ssl_do_write(s);
2587err:
2588#ifndef OPENSSL_NO_ECDH
2589 BN_CTX_free(bn_ctx);
2590 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2591 if (clnt_ecdh != NULL)
2592 EC_KEY_free(clnt_ecdh);
2593 EVP_PKEY_free(srvr_pub_pkey);
2594#endif
2595 return(-1);
2596 }
2597
2598int ssl3_send_client_verify(SSL *s)
2599 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002600 unsigned char *buf, *p;
2601 const EVP_MD *md;
2602 uint8_t digest[EVP_MAX_MD_SIZE];
2603 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002604 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002605 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002606 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002607 size_t signature_length = 0;
2608 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002609
2610 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002611 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002612
2613 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2614 {
2615 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002616 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002617 /* For TLS v1.2 send signature algorithm and signature using
2618 * agreed digest and cached handshake records. Otherwise, use
2619 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002620 */
2621 if (SSL_USE_SIGALGS(s))
2622 {
2623 long hdatalen = 0;
2624 char *hdata;
David Benjamin0e2908a2014-07-09 15:22:43 -04002625 md = s->cert->key->digest;
Adam Langley95c29f32014-06-20 12:00:00 -07002626 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2627 &hdata);
2628 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
2629 {
2630 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2631 goto err;
2632 }
2633 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002634 n += 2;
2635 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2636 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2637 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002638 {
2639 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2640 goto err;
2641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002643 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002644 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002645 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002646 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002647 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2648 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2649 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2650 * a raw RSA signature, rather than wrapping in a
2651 * DigestInfo. */
2652 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002653 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002654 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002655 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002656 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2657 digest_length = SHA_DIGEST_LENGTH;
2658 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002659 }
2660 else
Adam Langleyaed23062014-06-20 12:00:00 -07002661 {
Adam Langley95c29f32014-06-20 12:00:00 -07002662 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2663 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002664 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002665
2666 /* Sign the digest. */
2667 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2668 if (pctx == NULL)
2669 goto err;
2670
2671 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2672 if (EVP_PKEY_sign_init(pctx) != 1 ||
2673 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2674 EVP_PKEY_sign(pctx, NULL, &signature_length,
2675 digest, digest_length) != 1)
2676 {
2677 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2678 goto err;
2679 }
2680
2681 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2682 {
2683 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2684 goto err;
2685 }
2686
2687 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2688 digest, digest_length) != 1)
2689 {
2690 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2691 goto err;
2692 }
2693
2694 s2n(signature_length, p);
2695 n += signature_length + 2;
2696
2697 /* Now that client auth is completed, we no longer need cached
2698 * handshake records and can digest them. */
2699 if (SSL_USE_SIGALGS(s))
2700 {
2701 if (!ssl3_digest_cached_records(s))
2702 goto err;
2703 }
2704
Adam Langley95c29f32014-06-20 12:00:00 -07002705 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2706 s->state=SSL3_ST_CW_CERT_VRFY_B;
2707 }
2708 EVP_MD_CTX_cleanup(&mctx);
2709 EVP_PKEY_CTX_free(pctx);
2710 return ssl_do_write(s);
2711err:
2712 EVP_MD_CTX_cleanup(&mctx);
2713 EVP_PKEY_CTX_free(pctx);
2714 return(-1);
2715 }
2716
2717/* Check a certificate can be used for client authentication. Currently
2718 * check cert exists, if we have a suitable digest for TLS 1.2 if
2719 * static DH client certificates can be used and optionally checks
2720 * suitability for Suite B.
2721 */
2722static int ssl3_check_client_certificate(SSL *s)
2723 {
2724 unsigned long alg_k;
2725 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2726 return 0;
2727 /* If no suitable signature algorithm can't use certificate */
2728 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2729 return 0;
2730 /* If strict mode check suitability of chain before using it.
2731 * This also adjusts suite B digest if necessary.
2732 */
2733 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2734 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2735 return 0;
2736 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2737 /* See if we can use client certificate for fixed DH */
2738 if (alg_k & (SSL_kDHr|SSL_kDHd))
2739 {
2740 SESS_CERT *scert = s->session->sess_cert;
2741 int i = scert->peer_cert_type;
2742 EVP_PKEY *clkey = NULL, *spkey = NULL;
2743 clkey = s->cert->key->privatekey;
2744 /* If client key not DH assume it can be used */
2745 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2746 return 1;
2747 if (i >= 0)
2748 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2749 if (spkey)
2750 {
2751 /* Compare server and client parameters */
2752 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2753 EVP_PKEY_free(spkey);
2754 if (i != 1)
2755 return 0;
2756 }
2757 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2758 }
2759 return 1;
2760 }
2761
2762int ssl3_send_client_certificate(SSL *s)
2763 {
2764 X509 *x509=NULL;
2765 EVP_PKEY *pkey=NULL;
2766 int i;
2767
2768 if (s->state == SSL3_ST_CW_CERT_A)
2769 {
2770 /* Let cert callback update client certificates if required */
2771 if (s->cert->cert_cb)
2772 {
2773 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2774 if (i < 0)
2775 {
2776 s->rwstate=SSL_X509_LOOKUP;
2777 return -1;
2778 }
2779 if (i == 0)
2780 {
2781 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2782 return 0;
2783 }
2784 s->rwstate=SSL_NOTHING;
2785 }
2786 if (ssl3_check_client_certificate(s))
2787 s->state=SSL3_ST_CW_CERT_C;
2788 else
2789 s->state=SSL3_ST_CW_CERT_B;
2790 }
2791
2792 /* We need to get a client cert */
2793 if (s->state == SSL3_ST_CW_CERT_B)
2794 {
2795 /* If we get an error, we need to
2796 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2797 * We then get retied later */
2798 i=0;
2799 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2800 if (i < 0)
2801 {
2802 s->rwstate=SSL_X509_LOOKUP;
2803 return(-1);
2804 }
2805 s->rwstate=SSL_NOTHING;
2806 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2807 {
2808 s->state=SSL3_ST_CW_CERT_B;
2809 if ( !SSL_use_certificate(s,x509) ||
2810 !SSL_use_PrivateKey(s,pkey))
2811 i=0;
2812 }
2813 else if (i == 1)
2814 {
2815 i=0;
2816 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2817 }
2818
2819 if (x509 != NULL) X509_free(x509);
2820 if (pkey != NULL) EVP_PKEY_free(pkey);
2821 if (i && !ssl3_check_client_certificate(s))
2822 i = 0;
2823 if (i == 0)
2824 {
2825 if (s->version == SSL3_VERSION)
2826 {
2827 s->s3->tmp.cert_req=0;
2828 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2829 return(1);
2830 }
2831 else
2832 {
2833 s->s3->tmp.cert_req=2;
2834 }
2835 }
2836
2837 /* Ok, we have a cert */
2838 s->state=SSL3_ST_CW_CERT_C;
2839 }
2840
2841 if (s->state == SSL3_ST_CW_CERT_C)
2842 {
2843 s->state=SSL3_ST_CW_CERT_D;
2844 ssl3_output_cert_chain(s,
2845 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2846 }
2847 /* SSL3_ST_CW_CERT_D */
2848 return ssl_do_write(s);
2849 }
2850
2851#define has_bits(i,m) (((i)&(m)) == (m))
2852
2853int ssl3_check_cert_and_algorithm(SSL *s)
2854 {
2855 int i,idx;
2856 long alg_k,alg_a;
2857 EVP_PKEY *pkey=NULL;
2858 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002859 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002860#ifndef OPENSSL_NO_DH
2861 DH *dh;
2862#endif
2863
David Benjamine8f3d662014-07-12 01:10:19 -04002864 /* we don't have a certificate */
2865 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2866 return 1;
2867
Adam Langley95c29f32014-06-20 12:00:00 -07002868 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2869 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2870
Adam Langley95c29f32014-06-20 12:00:00 -07002871 sc=s->session->sess_cert;
2872 if (sc == NULL)
2873 {
2874 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2875 goto err;
2876 }
2877
Adam Langley95c29f32014-06-20 12:00:00 -07002878 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002879#ifndef OPENSSL_NO_DH
2880 dh=s->session->sess_cert->peer_dh_tmp;
2881#endif
2882
2883 /* This is the passed certificate */
2884
2885 idx=sc->peer_cert_type;
2886#ifndef OPENSSL_NO_ECDH
2887 if (idx == SSL_PKEY_ECC)
2888 {
2889 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2890 s) == 0)
2891 { /* check failed */
2892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
2893 goto f_err;
2894 }
2895 else
2896 {
2897 return 1;
2898 }
2899 }
2900 else if (alg_a & SSL_aECDSA)
2901 {
2902 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
2903 goto f_err;
2904 }
2905 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2906 {
2907 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
2908 goto f_err;
2909 }
2910#endif
2911 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2912 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2913 EVP_PKEY_free(pkey);
2914
2915
2916 /* Check that we have a certificate if we require one */
2917 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2918 {
2919 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
2920 goto f_err;
2921 }
2922#ifndef OPENSSL_NO_DSA
2923 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2924 {
2925 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
2926 goto f_err;
2927 }
2928#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002929 if ((alg_k & SSL_kRSA) &&
2930 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2931 {
2932 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2933 goto f_err;
2934 }
Adam Langley95c29f32014-06-20 12:00:00 -07002935#ifndef OPENSSL_NO_DH
2936 if ((alg_k & SSL_kEDH) &&
2937 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2938 {
2939 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
2940 goto f_err;
2941 }
2942 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2943 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2944 {
2945 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
2946 goto f_err;
2947 }
2948#ifndef OPENSSL_NO_DSA
2949 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2950 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2951 {
2952 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
2953 goto f_err;
2954 }
2955#endif
2956#endif
2957
2958 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2959 {
Adam Langley95c29f32014-06-20 12:00:00 -07002960 if (alg_k & SSL_kRSA)
2961 {
2962 if (rsa == NULL
2963 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2964 {
2965 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2966 goto f_err;
2967 }
2968 }
2969 else
Adam Langley95c29f32014-06-20 12:00:00 -07002970#ifndef OPENSSL_NO_DH
2971 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2972 {
2973 if (dh == NULL
2974 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2975 {
2976 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2977 goto f_err;
2978 }
2979 }
2980 else
2981#endif
2982 {
2983 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2984 goto f_err;
2985 }
2986 }
2987 return(1);
2988f_err:
2989 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2990err:
2991 return(0);
2992 }
2993
Adam Langley1258b6a2014-06-20 12:00:00 -07002994# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07002995int ssl3_send_next_proto(SSL *s)
2996 {
2997 unsigned int len, padding_len;
2998 unsigned char *d;
2999
3000 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3001 {
3002 len = s->next_proto_negotiated_len;
3003 padding_len = 32 - ((len + 2) % 32);
3004 d = (unsigned char *)s->init_buf->data;
3005 d[4] = len;
3006 memcpy(d + 5, s->next_proto_negotiated, len);
3007 d[5 + len] = padding_len;
3008 memset(d + 6 + len, 0, padding_len);
3009 *(d++)=SSL3_MT_NEXT_PROTO;
3010 l2n3(2 + len + padding_len, d);
3011 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3012 s->init_num = 4 + 2 + len + padding_len;
3013 s->init_off = 0;
3014 }
3015
3016 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3017}
Adam Langley1258b6a2014-06-20 12:00:00 -07003018
3019# endif /* !OPENSSL_NO_NEXTPROTONEG */
3020
3021int ssl3_send_channel_id(SSL *s)
3022 {
3023 unsigned char *d;
3024 int ret = -1, public_key_len;
3025 EVP_MD_CTX md_ctx;
3026 size_t sig_len;
3027 ECDSA_SIG *sig = NULL;
3028 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3029
3030 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3031 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3032
3033 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3034 {
3035 EVP_PKEY *key = NULL;
3036 s->ctx->channel_id_cb(s, &key);
3037 if (key != NULL)
3038 {
3039 s->tlsext_channel_id_private = key;
3040 }
3041 }
3042 if (!s->tlsext_channel_id_private)
3043 {
3044 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3045 return (-1);
3046 }
3047 s->rwstate=SSL_NOTHING;
3048
3049 d = (unsigned char *)s->init_buf->data;
3050 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3051 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3052 if (s->s3->tlsext_channel_id_new)
3053 s2n(TLSEXT_TYPE_channel_id_new, d);
3054 else
3055 s2n(TLSEXT_TYPE_channel_id, d);
3056 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3057
3058 EVP_MD_CTX_init(&md_ctx);
3059
3060 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3061 if (public_key_len <= 0)
3062 {
3063 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
3064 goto err;
3065 }
3066 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3067 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3068 * field elements as 32-byte, big-endian numbers. */
3069 if (public_key_len != 65)
3070 {
3071 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
3072 goto err;
3073 }
3074 public_key = OPENSSL_malloc(public_key_len);
3075 if (!public_key)
3076 {
3077 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3078 goto err;
3079 }
3080
3081 derp = public_key;
3082 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3083
3084 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3085 s->tlsext_channel_id_private) != 1)
3086 {
3087 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
3088 goto err;
3089 }
3090
3091 if (!tls1_channel_id_hash(&md_ctx, s))
3092 goto err;
3093
3094 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3095 {
3096 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3097 goto err;
3098 }
3099
3100 der_sig = OPENSSL_malloc(sig_len);
3101 if (!der_sig)
3102 {
3103 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3104 goto err;
3105 }
3106
3107 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3108 {
3109 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3110 goto err;
3111 }
3112
3113 derp = der_sig;
3114 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
3115 if (sig == NULL)
3116 {
3117 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
3118 goto err;
3119 }
3120
3121 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3122 memcpy(d, public_key + 1, 64);
3123 d += 64;
3124 memset(d, 0, 2 * 32);
3125 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3126 d += 32;
3127 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3128 d += 32;
3129
3130 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3131 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3132 s->init_off = 0;
3133
3134 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3135
3136err:
3137 EVP_MD_CTX_cleanup(&md_ctx);
3138 if (public_key)
3139 OPENSSL_free(public_key);
3140 if (der_sig)
3141 OPENSSL_free(der_sig);
3142 if (sig)
3143 ECDSA_SIG_free(sig);
3144
3145 return ret;
3146 }
Adam Langley95c29f32014-06-20 12:00:00 -07003147
3148/* Check to see if handshake is full or resumed. Usually this is just a
3149 * case of checking to see if a cache hit has occurred. In the case of
3150 * session tickets we have to check the next message to be sure.
3151 */
3152
Adam Langley95c29f32014-06-20 12:00:00 -07003153int ssl3_check_finished(SSL *s)
3154 {
3155 int ok;
3156 long n;
3157 /* If we have no ticket it cannot be a resumed session. */
3158 if (!s->session->tlsext_tick)
3159 return 1;
3160 /* this function is called when we really expect a Certificate
3161 * message, so permit appropriate message length */
3162 n=s->method->ssl_get_message(s,
3163 SSL3_ST_CR_CERT_A,
3164 SSL3_ST_CR_CERT_B,
3165 -1,
3166 s->max_cert_list,
3167 &ok);
3168 if (!ok) return((int)n);
3169 s->s3->tmp.reuse_message = 1;
3170 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3171 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3172 return 2;
3173
3174 return 1;
3175 }
Adam Langley95c29f32014-06-20 12:00:00 -07003176
3177int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3178 {
3179 int i = 0;
3180 /* TODO(fork): remove */
3181#if 0
3182#ifndef OPENSSL_NO_ENGINE
3183 if (s->ctx->client_cert_engine)
3184 {
3185 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3186 SSL_get_client_CA_list(s),
3187 px509, ppkey, NULL, NULL, NULL);
3188 if (i != 0)
3189 return i;
3190 }
3191#endif
3192#endif
3193 if (s->ctx->client_cert_cb)
3194 i = s->ctx->client_cert_cb(s,px509,ppkey);
3195 return i;
3196 }