blob: 8d60d8fc54e9e3168f990c71b97582367794f061 [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
David Benjamin81ea0bf2014-11-23 04:20:17 -0500151#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <stdio.h>
153
154#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/rand.h>
157#include <openssl/obj.h>
158#include <openssl/evp.h>
159#include <openssl/mem.h>
160#include <openssl/md5.h>
161#include <openssl/dh.h>
162#include <openssl/bn.h>
163#include <openssl/engine.h>
164#include <openssl/x509.h>
165
166#include "ssl_locl.h"
167#include "../crypto/dh/internal.h"
168
David Benjamin5b33a5e2014-09-24 16:27:30 -0400169IMPLEMENT_tls_meth_func(TLS1_2_VERSION, TLSv1_2_client_method,
170 ssl_undefined_function,
171 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400172 TLSv1_2_enc_data)
173
174IMPLEMENT_tls_meth_func(TLS1_1_VERSION, TLSv1_1_client_method,
175 ssl_undefined_function,
176 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400177 TLSv1_1_enc_data)
178
179IMPLEMENT_tls_meth_func(TLS1_VERSION, TLSv1_client_method,
180 ssl_undefined_function,
181 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400182 TLSv1_enc_data)
183
David Benjamin00075b82014-09-24 15:48:14 -0400184IMPLEMENT_tls_meth_func(SSL3_VERSION, SSLv3_client_method,
Adam Langley95c29f32014-06-20 12:00:00 -0700185 ssl_undefined_function,
186 ssl3_connect,
David Benjamin00075b82014-09-24 15:48:14 -0400187 SSLv3_enc_data)
Adam Langley95c29f32014-06-20 12:00:00 -0700188
189int ssl3_connect(SSL *s)
190 {
191 BUF_MEM *buf=NULL;
192 void (*cb)(const SSL *ssl,int type,int val)=NULL;
193 int ret= -1;
194 int new_state,state,skip=0;
195
196 ERR_clear_error();
197 ERR_clear_system_error();
198
199 if (s->info_callback != NULL)
200 cb=s->info_callback;
201 else if (s->ctx->info_callback != NULL)
202 cb=s->ctx->info_callback;
203
204 s->in_handshake++;
205 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
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
Adam Langley69a01602014-11-17 17:26:55 -0800259 if (!ssl3_init_finished_mac(s))
260 {
261 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
262 ret = -1;
263 goto end;
264 }
Adam Langley95c29f32014-06-20 12:00:00 -0700265
266 s->state=SSL3_ST_CW_CLNT_HELLO_A;
267 s->ctx->stats.sess_connect++;
268 s->init_num=0;
269 break;
270
271 case SSL3_ST_CW_CLNT_HELLO_A:
272 case SSL3_ST_CW_CLNT_HELLO_B:
273
274 s->shutdown=0;
David Benjamin8da99062014-08-24 12:03:09 -0400275 ret=ssl3_send_client_hello(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700276 if (ret <= 0) goto end;
277 s->state=SSL3_ST_CR_SRVR_HELLO_A;
278 s->init_num=0;
279
280 /* turn on buffering for the next lot of output */
281 if (s->bbio != s->wbio)
282 s->wbio=BIO_push(s->bbio,s->wbio);
283
284 break;
285
286 case SSL3_ST_CR_SRVR_HELLO_A:
287 case SSL3_ST_CR_SRVR_HELLO_B:
288 ret=ssl3_get_server_hello(s);
289 if (ret <= 0) goto end;
290
291 if (s->hit)
292 {
David Benjamincb5abad2014-07-25 12:14:28 -0400293 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700294 if (s->tlsext_ticket_expected)
295 {
296 /* receive renewed session ticket */
297 s->state=SSL3_ST_CR_SESSION_TICKET_A;
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299 }
300 else
301 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400302 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700303 }
304 s->init_num=0;
305 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400306
Adam Langley95c29f32014-06-20 12:00:00 -0700307 case SSL3_ST_CR_CERT_A:
308 case SSL3_ST_CR_CERT_B:
David Benjamine8f3d662014-07-12 01:10:19 -0400309 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700310 {
311 ret=ssl3_get_server_certificate(s);
312 if (ret <= 0) goto end;
David Benjamin6c7aed02014-08-27 16:42:38 -0400313 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -0700314 s->state=SSL3_ST_CR_CERT_STATUS_A;
315 else
316 s->state=SSL3_ST_CR_KEY_EXCH_A;
317 }
318 else
319 {
320 skip = 1;
321 s->state=SSL3_ST_CR_KEY_EXCH_A;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323 s->init_num=0;
324 break;
325
326 case SSL3_ST_CR_KEY_EXCH_A:
327 case SSL3_ST_CR_KEY_EXCH_B:
David Benjamin8f8040d2014-07-14 19:14:46 -0400328 ret=ssl3_get_server_key_exchange(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700329 if (ret <= 0) goto end;
330 s->state=SSL3_ST_CR_CERT_REQ_A;
331 s->init_num=0;
332
333 /* at this point we check that we have the
334 * required stuff from the server */
335 if (!ssl3_check_cert_and_algorithm(s))
336 {
337 ret= -1;
338 goto end;
339 }
340 break;
341
342 case SSL3_ST_CR_CERT_REQ_A:
343 case SSL3_ST_CR_CERT_REQ_B:
344 ret=ssl3_get_certificate_request(s);
345 if (ret <= 0) goto end;
346 s->state=SSL3_ST_CR_SRVR_DONE_A;
347 s->init_num=0;
348 break;
349
350 case SSL3_ST_CR_SRVR_DONE_A:
351 case SSL3_ST_CR_SRVR_DONE_B:
352 ret=ssl3_get_server_done(s);
353 if (ret <= 0) goto end;
354 if (s->s3->tmp.cert_req)
355 s->state=SSL3_ST_CW_CERT_A;
356 else
357 s->state=SSL3_ST_CW_KEY_EXCH_A;
358 s->init_num=0;
359
360 break;
361
362 case SSL3_ST_CW_CERT_A:
363 case SSL3_ST_CW_CERT_B:
364 case SSL3_ST_CW_CERT_C:
365 case SSL3_ST_CW_CERT_D:
366 ret=ssl3_send_client_certificate(s);
367 if (ret <= 0) goto end;
368 s->state=SSL3_ST_CW_KEY_EXCH_A;
369 s->init_num=0;
370 break;
371
372 case SSL3_ST_CW_KEY_EXCH_A:
373 case SSL3_ST_CW_KEY_EXCH_B:
374 ret=ssl3_send_client_key_exchange(s);
375 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700376 /* For TLS, cert_req is set to 2, so a cert chain
377 * of nothing is sent, but no verify packet is sent */
Adam Langley95c29f32014-06-20 12:00:00 -0700378 if (s->s3->tmp.cert_req == 1)
379 {
380 s->state=SSL3_ST_CW_CERT_VRFY_A;
381 }
382 else
383 {
384 s->state=SSL3_ST_CW_CHANGE_A;
385 s->s3->change_cipher_spec=0;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
388 s->init_num=0;
389 break;
390
391 case SSL3_ST_CW_CERT_VRFY_A:
392 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamin8da99062014-08-24 12:03:09 -0400393 ret=ssl3_send_cert_verify(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700394 if (ret <= 0) goto end;
395 s->state=SSL3_ST_CW_CHANGE_A;
396 s->init_num=0;
397 s->s3->change_cipher_spec=0;
398 break;
399
400 case SSL3_ST_CW_CHANGE_A:
401 case SSL3_ST_CW_CHANGE_B:
402 ret=ssl3_send_change_cipher_spec(s,
403 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
404 if (ret <= 0) goto end;
405
Adam Langley1258b6a2014-06-20 12:00:00 -0700406 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700407 if (s->s3->tlsext_channel_id_valid)
408 s->state=SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700409 if (s->s3->next_proto_neg_seen)
410 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700411 s->init_num=0;
412
413 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700414 if (!s->method->ssl3_enc->setup_key_block(s))
415 {
416 ret= -1;
417 goto end;
418 }
419
420 if (!s->method->ssl3_enc->change_cipher_state(s,
421 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
422 {
423 ret= -1;
424 goto end;
425 }
426
427 break;
428
Adam Langley95c29f32014-06-20 12:00:00 -0700429 case SSL3_ST_CW_NEXT_PROTO_A:
430 case SSL3_ST_CW_NEXT_PROTO_B:
431 ret=ssl3_send_next_proto(s);
432 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700433 if (s->s3->tlsext_channel_id_valid)
434 s->state=SSL3_ST_CW_CHANNEL_ID_A;
435 else
436 s->state=SSL3_ST_CW_FINISHED_A;
437 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700438
Adam Langley1258b6a2014-06-20 12:00:00 -0700439 case SSL3_ST_CW_CHANNEL_ID_A:
440 case SSL3_ST_CW_CHANNEL_ID_B:
441 ret=ssl3_send_channel_id(s);
442 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700443 s->state=SSL3_ST_CW_FINISHED_A;
444 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700445
446 case SSL3_ST_CW_FINISHED_A:
447 case SSL3_ST_CW_FINISHED_B:
448 ret=ssl3_send_finished(s,
449 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
450 s->method->ssl3_enc->client_finished_label,
451 s->method->ssl3_enc->client_finished_label_len);
452 if (ret <= 0) goto end;
453 s->state=SSL3_ST_CW_FLUSH;
454
Adam Langley95c29f32014-06-20 12:00:00 -0700455 if (s->hit)
456 {
457 s->s3->tmp.next_state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700458 }
459 else
460 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700461 /* This is a non-resumption handshake. If it
462 * involves ChannelID, then record the
463 * handshake hashes at this point in the
464 * session so that any resumption of this
465 * session with ChannelID can sign those
466 * hashes. */
467 if (s->s3->tlsext_channel_id_new)
468 {
469 ret = tls1_record_handshake_hashes_for_channel_id(s);
470 if (ret <= 0)
471 goto end;
472 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700473 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
474 && ssl3_can_cutthrough(s)
475 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
476 )
477 {
Adam Langley045cc552014-07-16 10:45:57 -0700478 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700479 }
Adam Langley95c29f32014-06-20 12:00:00 -0700480 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700481 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700482 /* Allow NewSessionTicket if ticket expected */
483 if (s->tlsext_ticket_expected)
484 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
485 else
David Benjamincb5abad2014-07-25 12:14:28 -0400486 s->s3->tmp.next_state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700487 }
Adam Langley95c29f32014-06-20 12:00:00 -0700488 }
489 s->init_num=0;
490 break;
491
Adam Langley95c29f32014-06-20 12:00:00 -0700492 case SSL3_ST_CR_SESSION_TICKET_A:
493 case SSL3_ST_CR_SESSION_TICKET_B:
494 ret=ssl3_get_new_session_ticket(s);
495 if (ret <= 0) goto end;
David Benjamincb5abad2014-07-25 12:14:28 -0400496 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700497 s->init_num=0;
498 break;
499
500 case SSL3_ST_CR_CERT_STATUS_A:
501 case SSL3_ST_CR_CERT_STATUS_B:
502 ret=ssl3_get_cert_status(s);
503 if (ret <= 0) goto end;
504 s->state=SSL3_ST_CR_KEY_EXCH_A;
505 s->init_num=0;
506 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamincb5abad2014-07-25 12:14:28 -0400508 case SSL3_ST_CR_CHANGE:
509 /* At this point, the next message must be entirely
510 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400511 if (!ssl3_expect_change_cipher_spec(s))
512 {
513 ret = -1;
514 goto end;
515 }
David Benjamincb5abad2014-07-25 12:14:28 -0400516 s->state = SSL3_ST_CR_FINISHED_A;
517 break;
518
Adam Langley95c29f32014-06-20 12:00:00 -0700519 case SSL3_ST_CR_FINISHED_A:
520 case SSL3_ST_CR_FINISHED_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700521 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
522 SSL3_ST_CR_FINISHED_B);
523 if (ret <= 0) goto end;
524
525 if (s->hit)
526 s->state=SSL3_ST_CW_CHANGE_A;
527 else
528 s->state=SSL_ST_OK;
529 s->init_num=0;
530 break;
531
532 case SSL3_ST_CW_FLUSH:
533 s->rwstate=SSL_WRITING;
534 if (BIO_flush(s->wbio) <= 0)
535 {
536 ret= -1;
537 goto end;
538 }
539 s->rwstate=SSL_NOTHING;
540 s->state=s->s3->tmp.next_state;
541 break;
542
Adam Langleyadb739e2014-06-20 12:00:00 -0700543 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700544 /* Allow NewSessionTicket if ticket expected */
545 if (s->tlsext_ticket_expected)
546 s->state=SSL3_ST_CR_SESSION_TICKET_A;
547 else
David Benjamin7e3305e2014-07-28 14:52:32 -0400548 s->state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700549
Adam Langley045cc552014-07-16 10:45:57 -0700550 ssl_free_wbio_buffer(s);
Adam Langleyadb739e2014-06-20 12:00:00 -0700551 ret = 1;
552 goto end;
553 /* break; */
554
Adam Langley95c29f32014-06-20 12:00:00 -0700555 case SSL_ST_OK:
556 /* clean a few things up */
557 ssl3_cleanup_key_block(s);
558
559 if (s->init_buf != NULL)
560 {
561 BUF_MEM_free(s->init_buf);
562 s->init_buf=NULL;
563 }
564
David Benjamine1b20a02014-11-05 01:28:53 -0500565 /* Remove write buffering now. */
566 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700567
568 s->init_num=0;
569 s->renegotiate=0;
570 s->new_session=0;
571
572 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
573 if (s->hit) s->ctx->stats.sess_hit++;
574
575 ret=1;
576 /* s->server=0; */
577 s->handshake_func=ssl3_connect;
578 s->ctx->stats.sess_connect_good++;
579
580 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
581
582 goto end;
583 /* break; */
584
585 default:
586 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
587 ret= -1;
588 goto end;
589 /* break; */
590 }
591
592 /* did we do anything */
593 if (!s->s3->tmp.reuse_message && !skip)
594 {
595 if (s->debug)
596 {
597 if ((ret=BIO_flush(s->wbio)) <= 0)
598 goto end;
599 }
600
601 if ((cb != NULL) && (s->state != state))
602 {
603 new_state=s->state;
604 s->state=state;
605 cb(s,SSL_CB_CONNECT_LOOP,1);
606 s->state=new_state;
607 }
608 }
609 skip=0;
610 }
611end:
612 s->in_handshake--;
613 if (buf != NULL)
614 BUF_MEM_free(buf);
615 if (cb != NULL)
616 cb(s,SSL_CB_CONNECT_EXIT,ret);
617 return(ret);
618 }
619
620
David Benjamin8da99062014-08-24 12:03:09 -0400621int ssl3_send_client_hello(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700622 {
623 unsigned char *buf;
624 unsigned char *p,*d;
625 int i;
626 unsigned long l;
627
628 buf=(unsigned char *)s->init_buf->data;
629 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
630 {
Adam Langley95c29f32014-06-20 12:00:00 -0700631 if (s->method->version == DTLS_ANY_VERSION)
632 {
633 /* Determine which DTLS version to use */
634 int options = s->options;
635 /* If DTLS 1.2 disabled correct the version number */
636 if (options & SSL_OP_NO_DTLSv1_2)
637 {
Adam Langley95c29f32014-06-20 12:00:00 -0700638 /* Disabling all versions is silly: return an
639 * error.
640 */
641 if (options & SSL_OP_NO_DTLSv1)
642 {
David Benjamin8da99062014-08-24 12:03:09 -0400643 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
Adam Langley95c29f32014-06-20 12:00:00 -0700644 goto err;
645 }
646 /* Update method so we don't use any DTLS 1.2
647 * features.
648 */
649 s->method = DTLSv1_client_method();
650 s->version = DTLS1_VERSION;
651 }
652 else
653 {
654 /* We only support one version: update method */
655 if (options & SSL_OP_NO_DTLSv1)
656 s->method = DTLSv1_2_client_method();
657 s->version = DTLS1_2_VERSION;
658 }
659 s->client_version = s->version;
660 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500661
662 /* If the configured session was created at a version
663 * higher than our maximum version, drop it. */
664 if (s->session &&
665 (s->session->session_id_length == 0 ||
666 s->session->not_resumable ||
667 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
668 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version)))
669 {
670 SSL_set_session(s, NULL);
671 }
672
Adam Langley95c29f32014-06-20 12:00:00 -0700673 /* else use the pre-loaded session */
674
675 p=s->s3->client_random;
676
David Benjaminf2fedef2014-08-16 01:37:34 -0400677 /* If resending the ClientHello in DTLS after a
678 * HelloVerifyRequest, don't renegerate the client_random. The
679 * random must be reused. */
680 if (!SSL_IS_DTLS(s) || !s->d1->send_cookie)
Adam Langley95c29f32014-06-20 12:00:00 -0700681 {
Adam Langley95c29f32014-06-20 12:00:00 -0700682 ssl_fill_hello_random(s, 0, p,
683 sizeof(s->s3->client_random));
David Benjaminf2fedef2014-08-16 01:37:34 -0400684 }
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langleyb0c235e2014-06-20 12:00:00 -0700686 /* Do the message type and length last.
687 * Note: the final argument to ssl_add_clienthello_tlsext below
688 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700689 d=p= ssl_handshake_start(s);
690
691 /* version indicates the negotiated version: for example from
692 * an SSLv2/v3 compatible client hello). The client_version
693 * field is the maximum version we permit and it is also
694 * used in RSA encrypted premaster secrets. Some servers can
695 * choke if we initially report a higher version then
696 * renegotiate to a lower one in the premaster secret. This
697 * didn't happen with TLS 1.0 as most servers supported it
698 * but it can with TLS 1.1 or later if the server only supports
699 * 1.0.
700 *
701 * Possible scenario with previous logic:
702 * 1. Client hello indicates TLS 1.2
703 * 2. Server hello says TLS 1.0
704 * 3. RSA encrypted premaster secret uses 1.2.
705 * 4. Handhaked proceeds using TLS 1.0.
706 * 5. Server sends hello request to renegotiate.
707 * 6. Client hello indicates TLS v1.0 as we now
708 * know that is maximum server supports.
709 * 7. Server chokes on RSA encrypted premaster secret
710 * containing version 1.0.
711 *
712 * For interoperability it should be OK to always use the
713 * maximum version we support in client hello and then rely
714 * on the checking of version to ensure the servers isn't
715 * being inconsistent: for example initially negotiating with
716 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
717 * client_version in client hello and not resetting it to
718 * the negotiated version.
719 */
720#if 0
721 *(p++)=s->version>>8;
722 *(p++)=s->version&0xff;
723 s->client_version=s->version;
724#else
725 *(p++)=s->client_version>>8;
726 *(p++)=s->client_version&0xff;
727#endif
728
729 /* Random stuff */
730 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
731 p+=SSL3_RANDOM_SIZE;
732
733 /* Session ID */
David Benjamin81ea0bf2014-11-23 04:20:17 -0500734 if (s->new_session || s->session == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700735 i=0;
736 else
737 i=s->session->session_id_length;
738 *(p++)=i;
739 if (i != 0)
740 {
741 if (i > (int)sizeof(s->session->session_id))
742 {
David Benjamin8da99062014-08-24 12:03:09 -0400743 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700744 goto err;
745 }
746 memcpy(p,s->session->session_id,i);
747 p+=i;
748 }
749
750 /* cookie stuff for DTLS */
751 if (SSL_IS_DTLS(s))
752 {
753 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
754 {
David Benjamin8da99062014-08-24 12:03:09 -0400755 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700756 goto err;
757 }
758 *(p++) = s->d1->cookie_len;
759 memcpy(p, s->d1->cookie, s->d1->cookie_len);
760 p += s->d1->cookie_len;
761 }
762
763 /* Ciphers supported */
David Benjamin39482a12014-07-20 13:30:15 -0400764 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
Adam Langley95c29f32014-06-20 12:00:00 -0700765 if (i == 0)
766 {
David Benjamin8da99062014-08-24 12:03:09 -0400767 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
Adam Langley95c29f32014-06-20 12:00:00 -0700768 goto err;
769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770 s2n(i,p);
771 p+=i;
772
773 /* COMPRESSION */
774 *(p++)=1;
775 *(p++)=0; /* Add the NULL method */
776
Adam Langley95c29f32014-06-20 12:00:00 -0700777 /* TLS extensions*/
778 if (ssl_prepare_clienthello_tlsext(s) <= 0)
779 {
David Benjamin8da99062014-08-24 12:03:09 -0400780 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -0700781 goto err;
782 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700783 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700784 {
David Benjamin8da99062014-08-24 12:03:09 -0400785 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700786 goto err;
787 }
Adam Langley95c29f32014-06-20 12:00:00 -0700788
789 l= p-d;
790 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
791 s->state=SSL3_ST_CW_CLNT_HELLO_B;
792 }
793
794 /* SSL3_ST_CW_CLNT_HELLO_B */
795 return ssl_do_write(s);
796err:
797 return(-1);
798 }
799
800int ssl3_get_server_hello(SSL *s)
801 {
802 STACK_OF(SSL_CIPHER) *sk;
803 const SSL_CIPHER *c;
804 CERT *ct = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -0700805 int al=SSL_AD_INTERNAL_ERROR,ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700806 long n;
David Benjamina03d95d2014-07-12 19:49:07 -0400807 CBS server_hello, server_random, session_id;
David Benjamin39482a12014-07-20 13:30:15 -0400808 uint16_t server_version, cipher_suite;
David Benjamina03d95d2014-07-12 19:49:07 -0400809 uint8_t compression_method;
David Benjamin39ebf532014-08-31 02:23:49 -0400810 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700811
David Benjamin8b8c0062014-11-23 02:47:52 -0500812 /* DTLS_ANY_VERSION does not sniff the version ahead of time,
813 * so disable the version check. */
814 if (SSL_IS_DTLS(s))
815 s->first_packet = 1;
816
Adam Langley95c29f32014-06-20 12:00:00 -0700817 n=s->method->ssl_get_message(s,
818 SSL3_ST_CR_SRVR_HELLO_A,
819 SSL3_ST_CR_SRVR_HELLO_B,
David Benjaminf2fedef2014-08-16 01:37:34 -0400820 SSL3_MT_SERVER_HELLO,
Adam Langley95c29f32014-06-20 12:00:00 -0700821 20000, /* ?? */
David Benjamin590cbe92014-08-25 21:34:56 -0400822 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -0700823 &ok);
824
David Benjamin8b8c0062014-11-23 02:47:52 -0500825 if (SSL_IS_DTLS(s))
826 s->first_packet = 0;
827
Adam Langley95c29f32014-06-20 12:00:00 -0700828 if (!ok) return((int)n);
829
David Benjamina03d95d2014-07-12 19:49:07 -0400830 CBS_init(&server_hello, s->init_msg, n);
831
832 if (!CBS_get_u16(&server_hello, &server_version) ||
833 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
834 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
David Benjamin39482a12014-07-20 13:30:15 -0400835 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
836 !CBS_get_u16(&server_hello, &cipher_suite) ||
837 !CBS_get_u8(&server_hello, &compression_method))
David Benjamina03d95d2014-07-12 19:49:07 -0400838 {
839 al = SSL_AD_DECODE_ERROR;
840 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
841 goto f_err;
842 }
843
Adam Langley95c29f32014-06-20 12:00:00 -0700844 if (s->method->version == DTLS_ANY_VERSION)
845 {
846 /* Work out correct protocol version to use */
Adam Langley95c29f32014-06-20 12:00:00 -0700847 int options = s->options;
David Benjamina03d95d2014-07-12 19:49:07 -0400848 if (server_version == DTLS1_2_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700849 && !(options & SSL_OP_NO_DTLSv1_2))
850 s->method = DTLSv1_2_client_method();
David Benjamina03d95d2014-07-12 19:49:07 -0400851 else if (server_version == DTLS1_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700852 && !(options & SSL_OP_NO_DTLSv1))
853 s->method = DTLSv1_client_method();
854 else
855 {
856 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400857 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700858 al = SSL_AD_PROTOCOL_VERSION;
859 goto f_err;
860 }
861 s->version = s->client_version = s->method->version;
862 }
863
David Benjamina03d95d2014-07-12 19:49:07 -0400864 if (server_version != s->version)
Adam Langley95c29f32014-06-20 12:00:00 -0700865 {
866 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400867 s->version = (s->version & 0xff00) | (server_version & 0xff);
868 al = SSL_AD_PROTOCOL_VERSION;
Adam Langley95c29f32014-06-20 12:00:00 -0700869 goto f_err;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
David Benjamina03d95d2014-07-12 19:49:07 -0400872 /* Copy over the server random. */
873 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700874
David Benjamin81ea0bf2014-11-23 04:20:17 -0500875 assert(s->session == NULL || s->session->session_id_length > 0);
876 if (s->session != NULL &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400877 CBS_mem_equal(&session_id,
878 s->session->session_id, s->session->session_id_length))
Adam Langley95c29f32014-06-20 12:00:00 -0700879 {
David Benjamina03d95d2014-07-12 19:49:07 -0400880 if(s->sid_ctx_length != s->session->sid_ctx_length
881 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
882 {
883 /* actually a client application bug */
884 al = SSL_AD_ILLEGAL_PARAMETER;
885 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
886 goto f_err;
887 }
David Benjamina03d95d2014-07-12 19:49:07 -0400888 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700889 }
David Benjamind1681e62014-11-20 14:54:14 -0500890 else
Adam Langley95c29f32014-06-20 12:00:00 -0700891 {
David Benjamin81ea0bf2014-11-23 04:20:17 -0500892 /* The session wasn't resumed. Create a fresh SSL_SESSION to
893 * fill out. */
David Benjamind1681e62014-11-20 14:54:14 -0500894 s->hit = 0;
David Benjamin81ea0bf2014-11-23 04:20:17 -0500895 if (!ssl_get_new_session(s, 0))
Adam Langley95c29f32014-06-20 12:00:00 -0700896 {
David Benjamin81ea0bf2014-11-23 04:20:17 -0500897 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700898 }
David Benjamina03d95d2014-07-12 19:49:07 -0400899 /* Note: session_id could be empty. */
900 s->session->session_id_length = CBS_len(&session_id);
901 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
Adam Langley95c29f32014-06-20 12:00:00 -0700902 }
David Benjamina03d95d2014-07-12 19:49:07 -0400903
David Benjamin39482a12014-07-20 13:30:15 -0400904 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -0700905 if (c == NULL)
906 {
907 /* unknown cipher */
David Benjamina03d95d2014-07-12 19:49:07 -0400908 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700909 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
910 goto f_err;
911 }
David Benjamin39ebf532014-08-31 02:23:49 -0400912 /* ct->mask_ssl was computed from client capabilities. Now
913 * that the final version is known, compute a new mask_ssl. */
914 if (!SSL_USE_TLS1_2_CIPHERS(s))
915 mask_ssl = SSL_TLSV1_2;
916 else
917 mask_ssl = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700918 /* If it is a disabled cipher we didn't send it in client hello,
919 * so return an error.
920 */
David Benjamin39ebf532014-08-31 02:23:49 -0400921 if (c->algorithm_ssl & mask_ssl ||
Adam Langley95c29f32014-06-20 12:00:00 -0700922 c->algorithm_mkey & ct->mask_k ||
923 c->algorithm_auth & ct->mask_a)
924 {
925 al=SSL_AD_ILLEGAL_PARAMETER;
926 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
927 goto f_err;
928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
930 sk=ssl_get_ciphers_by_id(s);
931 if (!sk_SSL_CIPHER_find(sk, NULL, c))
932 {
933 /* we did not say we would use this cipher */
934 al=SSL_AD_ILLEGAL_PARAMETER;
935 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
936 goto f_err;
937 }
938
939 /* Depending on the session caching (internal/external), the cipher
940 and/or cipher_id values may not be set. Make sure that
941 cipher_id is set and use it for comparison. */
942 if (s->session->cipher)
943 s->session->cipher_id = s->session->cipher->id;
944 if (s->hit && (s->session->cipher_id != c->id))
945 {
David Benjamina03d95d2014-07-12 19:49:07 -0400946 al = SSL_AD_ILLEGAL_PARAMETER;
947 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
948 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700949 }
950 s->s3->tmp.new_cipher=c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500951
952 /* Most clients also require that the negotiated version match the
953 * session's version if resuming. However OpenSSL has historically not
954 * had the corresponding logic on the server, so this may not be
955 * compatible, depending on other factors. (Whether the ClientHello
956 * version is clamped to the session's version and whether the session
957 * cache is keyed on IP address.)
958 *
959 * TODO(davidben): See if we can still enforce this? Perhaps for the
960 * future TLS 1.3 and forward if this is fixed upstream. */
961
Adam Langley95c29f32014-06-20 12:00:00 -0700962 /* Don't digest cached records if no sigalgs: we may need them for
963 * client authentication.
964 */
Adam Langley75712922014-10-10 16:23:43 -0700965 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley95c29f32014-06-20 12:00:00 -0700966 goto f_err;
David Benjamina03d95d2014-07-12 19:49:07 -0400967
David Benjamina03d95d2014-07-12 19:49:07 -0400968 /* Only the NULL compression algorithm is supported. */
969 if (compression_method != 0)
970 {
971 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700972 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
973 goto f_err;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
David Benjamina03d95d2014-07-12 19:49:07 -0400976 /* TLS extensions */
977 if (!ssl_parse_serverhello_tlsext(s, &server_hello))
Adam Langley95c29f32014-06-20 12:00:00 -0700978 {
979 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
980 goto err;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
David Benjamin03973092014-06-24 23:27:17 -0400983 /* There should be nothing left over in the record. */
David Benjamina03d95d2014-07-12 19:49:07 -0400984 if (CBS_len(&server_hello) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700985 {
986 /* wrong packet length */
987 al=SSL_AD_DECODE_ERROR;
988 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
989 goto f_err;
990 }
991
992 return(1);
993f_err:
994 ssl3_send_alert(s,SSL3_AL_FATAL,al);
995err:
996 return(-1);
997 }
998
999int ssl3_get_server_certificate(SSL *s)
1000 {
1001 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001002 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001003 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001004 STACK_OF(X509) *sk=NULL;
1005 SESS_CERT *sc;
1006 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001007 CBS cbs, certificate_list;
1008 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
1010 n=s->method->ssl_get_message(s,
1011 SSL3_ST_CR_CERT_A,
1012 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001013 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001014 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001015 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001016 &ok);
1017
1018 if (!ok) return((int)n);
1019
David Benjamin51b1f742014-07-12 16:31:12 -04001020 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001021
1022 if ((sk=sk_X509_new_null()) == NULL)
1023 {
1024 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1025 goto err;
1026 }
1027
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001028 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1029 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001030 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001031 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001032 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1033 goto f_err;
1034 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001035
1036 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001037 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001038 CBS certificate;
1039 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001040 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001041 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001042 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1043 goto f_err;
1044 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001045 data = CBS_data(&certificate);
1046 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001047 if (x == NULL)
1048 {
1049 al=SSL_AD_BAD_CERTIFICATE;
1050 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1051 goto f_err;
1052 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001053 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001054 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001055 al = SSL_AD_INTERNAL_ERROR;
1056 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1057 goto f_err;
1058 }
1059 if (CBS_len(&certificate) != 0)
1060 {
1061 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001062 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1063 goto f_err;
1064 }
1065 if (!sk_X509_push(sk,x))
1066 {
1067 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1068 goto err;
1069 }
1070 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001071 }
1072
1073 i=ssl_verify_cert_chain(s,sk);
1074 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1075 )
1076 {
1077 al=ssl_verify_alarm_type(s->verify_result);
1078 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1079 goto f_err;
1080 }
1081 ERR_clear_error(); /* but we keep s->verify_result */
1082
1083 sc=ssl_sess_cert_new();
1084 if (sc == NULL) goto err;
1085
1086 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1087 s->session->sess_cert=sc;
1088
1089 sc->cert_chain=sk;
1090 /* Inconsistency alert: cert_chain does include the peer's
1091 * certificate, which we don't include in s3_srvr.c */
1092 x=sk_X509_value(sk,0);
1093 sk=NULL;
1094 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1095
1096 pkey=X509_get_pubkey(x);
1097
David Benjamind26aea62014-07-12 00:13:56 -04001098 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001099 {
1100 x=NULL;
1101 al=SSL3_AL_FATAL;
1102 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1103 goto f_err;
1104 }
1105
1106 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001107 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001108 {
1109 x=NULL;
1110 al=SSL3_AL_FATAL;
1111 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1112 goto f_err;
1113 }
1114
David Benjamind26aea62014-07-12 00:13:56 -04001115 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1116 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001117 {
David Benjamind26aea62014-07-12 00:13:56 -04001118 x=NULL;
1119 al=SSL_AD_ILLEGAL_PARAMETER;
1120 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1121 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001122 }
David Benjamind26aea62014-07-12 00:13:56 -04001123 sc->peer_cert_type=i;
David Benjamind26aea62014-07-12 00:13:56 -04001124 /* Why would the following ever happen?
1125 * We just created sc a couple of lines ago. */
1126 if (sc->peer_pkeys[i].x509 != NULL)
1127 X509_free(sc->peer_pkeys[i].x509);
David Benjamin150c6172014-08-05 22:22:49 -04001128 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1129 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001130
David Benjamind26aea62014-07-12 00:13:56 -04001131 if (s->session->peer != NULL)
1132 X509_free(s->session->peer);
David Benjamin150c6172014-08-05 22:22:49 -04001133 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001134
Adam Langley95c29f32014-06-20 12:00:00 -07001135 s->session->verify_result = s->verify_result;
1136
1137 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001138 ret=1;
1139 if (0)
1140 {
1141f_err:
1142 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1143 }
1144err:
1145 EVP_PKEY_free(pkey);
1146 X509_free(x);
1147 sk_X509_pop_free(sk,X509_free);
1148 return(ret);
1149 }
1150
David Benjamin8f8040d2014-07-14 19:14:46 -04001151int ssl3_get_server_key_exchange(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07001152 {
Adam Langley95c29f32014-06-20 12:00:00 -07001153 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001154 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001155 long n,alg_k,alg_a;
1156 EVP_PKEY *pkey=NULL;
1157 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001158 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001159 DH *dh=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001160 EC_KEY *ecdh = NULL;
1161 BN_CTX *bn_ctx = NULL;
1162 EC_POINT *srvr_ecpoint = NULL;
David Benjamined439582014-07-14 19:13:02 -04001163 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001164
1165 /* use same message size as in ssl3_get_certificate_request()
1166 * as ServerKeyExchange message may be skipped */
1167 n=s->method->ssl_get_message(s,
1168 SSL3_ST_CR_KEY_EXCH_A,
1169 SSL3_ST_CR_KEY_EXCH_B,
1170 -1,
1171 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001172 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001173 &ok);
1174 if (!ok) return((int)n);
1175
1176 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1177 {
David Benjamin9c651c92014-07-12 13:27:45 -04001178 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1179 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001180 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin9c651c92014-07-12 13:27:45 -04001181 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1182 return -1;
1183 }
1184
Adam Langley95c29f32014-06-20 12:00:00 -07001185 /* In plain PSK ciphersuite, ServerKeyExchange can be
1186 omitted if no identity hint is sent. Set
1187 session->sess_cert anyway to avoid problems
1188 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001189 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001190 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001191 /* PSK ciphersuites that also send a
1192 * Certificate would have already initialized
1193 * |sess_cert|. */
1194 if (s->session->sess_cert == NULL)
1195 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin688d8df2014-11-02 23:06:42 -05001196
1197 /* TODO(davidben): This should be reset in one place
1198 * with the rest of the handshake state. */
1199 if (s->s3->tmp.peer_psk_identity_hint)
Adam Langley0289c732014-06-20 12:00:00 -07001200 {
David Benjamin688d8df2014-11-02 23:06:42 -05001201 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1202 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley0289c732014-06-20 12:00:00 -07001203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205 s->s3->tmp.reuse_message=1;
1206 return(1);
1207 }
1208
David Benjamined439582014-07-14 19:13:02 -04001209 /* Retain a copy of the original CBS to compute the signature
1210 * over. */
1211 CBS_init(&server_key_exchange, s->init_msg, n);
1212 server_key_exchange_orig = server_key_exchange;
1213
Adam Langley95c29f32014-06-20 12:00:00 -07001214 if (s->session->sess_cert != NULL)
1215 {
Adam Langley95c29f32014-06-20 12:00:00 -07001216 if (s->session->sess_cert->peer_dh_tmp)
1217 {
1218 DH_free(s->session->sess_cert->peer_dh_tmp);
1219 s->session->sess_cert->peer_dh_tmp=NULL;
1220 }
Adam Langley95c29f32014-06-20 12:00:00 -07001221 if (s->session->sess_cert->peer_ecdh_tmp)
1222 {
1223 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1224 s->session->sess_cert->peer_ecdh_tmp=NULL;
1225 }
Adam Langley95c29f32014-06-20 12:00:00 -07001226 }
1227 else
1228 {
1229 s->session->sess_cert=ssl_sess_cert_new();
1230 }
1231
Adam Langley95c29f32014-06-20 12:00:00 -07001232 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1233 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1234 EVP_MD_CTX_init(&md_ctx);
1235
Adam Langleyc26c8022014-06-20 12:00:00 -07001236 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001237 {
David Benjamined439582014-07-14 19:13:02 -04001238 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001239
David Benjamined439582014-07-14 19:13:02 -04001240 /* Each of the PSK key exchanges begins with a
1241 * psk_identity_hint. */
1242 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001243 {
David Benjamined439582014-07-14 19:13:02 -04001244 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001245 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001246 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001247 }
Adam Langley95c29f32014-06-20 12:00:00 -07001248
David Benjamined439582014-07-14 19:13:02 -04001249 /* Store PSK identity hint for later use, hint is used in
1250 * ssl3_send_client_key_exchange. Assume that the maximum
1251 * length of a PSK identity hint can be as long as the maximum
1252 * length of a PSK identity. Also do not allow NULL
1253 * characters; identities are saved as C strings.
1254 *
1255 * TODO(davidben): Should invalid hints be ignored? It's a hint
1256 * rather than a specific identity. */
1257 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1258 CBS_contains_zero_byte(&psk_identity_hint))
1259 {
1260 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001261 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamined439582014-07-14 19:13:02 -04001262 goto f_err;
1263 }
1264
1265 /* Save the identity hint as a C string. */
David Benjamin688d8df2014-11-02 23:06:42 -05001266 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint))
David Benjamined439582014-07-14 19:13:02 -04001267 {
David Benjamin688d8df2014-11-02 23:06:42 -05001268 al = SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001269 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001270 goto f_err;
1271 }
Adam Langley95c29f32014-06-20 12:00:00 -07001272 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001273
David Benjamin525a0fe2014-11-08 11:41:12 -05001274 if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001275 {
David Benjamined439582014-07-14 19:13:02 -04001276 CBS dh_p, dh_g, dh_Ys;
1277
1278 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1279 CBS_len(&dh_p) == 0 ||
1280 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1281 CBS_len(&dh_g) == 0 ||
1282 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1283 CBS_len(&dh_Ys) == 0)
1284 {
1285 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001286 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001287 goto f_err;
1288 }
1289
Adam Langley95c29f32014-06-20 12:00:00 -07001290 if ((dh=DH_new()) == NULL)
1291 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001292 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001293 goto err;
1294 }
Adam Langley95c29f32014-06-20 12:00:00 -07001295
David Benjamined439582014-07-14 19:13:02 -04001296 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001297 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001298 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001299 goto err;
1300 }
David Benjamined439582014-07-14 19:13:02 -04001301 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001302 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001303 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001304 goto err;
1305 }
David Benjamined439582014-07-14 19:13:02 -04001306 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1307 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001308 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001309 goto err;
1310 }
Adam Langley95c29f32014-06-20 12:00:00 -07001311
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001312 if (DH_size(dh) < 512/8)
1313 {
1314 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1315 goto err;
1316 }
1317
Adam Langley95c29f32014-06-20 12:00:00 -07001318 if (alg_a & SSL_aRSA)
1319 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001320 /* else anonymous DH, so no certificate or pkey. */
1321
1322 s->session->sess_cert->peer_dh_tmp=dh;
1323 dh=NULL;
1324 }
Adam Langley95c29f32014-06-20 12:00:00 -07001325
Adam Langley95c29f32014-06-20 12:00:00 -07001326 else if (alg_k & SSL_kEECDH)
1327 {
David Benjamined439582014-07-14 19:13:02 -04001328 uint16_t curve_id;
1329 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001330 EC_GROUP *ngroup;
1331 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001332 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001333
David Benjamined439582014-07-14 19:13:02 -04001334 /* Extract elliptic curve parameters and the server's
1335 * ephemeral ECDH public key. Check curve is one of
1336 * our preferences, if not server has sent an invalid
1337 * curve.
1338 */
1339 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001340 {
David Benjamined439582014-07-14 19:13:02 -04001341 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001342 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
Adam Langley95c29f32014-06-20 12:00:00 -07001343 goto f_err;
1344 }
1345
David Benjamined439582014-07-14 19:13:02 -04001346 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001347 {
1348 al=SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001349 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley95c29f32014-06-20 12:00:00 -07001350 goto f_err;
1351 }
1352
David Benjamined439582014-07-14 19:13:02 -04001353 if ((ecdh=EC_KEY_new()) == NULL)
1354 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001355 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001356 goto err;
1357 }
Adam Langley95c29f32014-06-20 12:00:00 -07001358 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1359 if (ngroup == NULL)
1360 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001361 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001362 goto err;
1363 }
1364 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1365 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001366 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001367 goto err;
1368 }
1369 EC_GROUP_free(ngroup);
1370
1371 group = EC_KEY_get0_group(ecdh);
1372
Adam Langley95c29f32014-06-20 12:00:00 -07001373 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001374 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1375 {
1376 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001377 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001378 goto f_err;
1379 }
1380
Adam Langley95c29f32014-06-20 12:00:00 -07001381 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1382 ((bn_ctx = BN_CTX_new()) == NULL))
1383 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001384 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001385 goto err;
1386 }
1387
David Benjamined439582014-07-14 19:13:02 -04001388 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1389 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001390 {
David Benjamined439582014-07-14 19:13:02 -04001391 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001392 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
Adam Langley95c29f32014-06-20 12:00:00 -07001393 goto f_err;
1394 }
1395
Adam Langley95c29f32014-06-20 12:00:00 -07001396 /* The ECC/TLS specification does not mention
1397 * the use of DSA to sign ECParameters in the server
1398 * key exchange message. We do support RSA and ECDSA.
1399 */
1400 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001401 else if (alg_a & SSL_aRSA)
1402 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001403 else if (alg_a & SSL_aECDSA)
1404 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001405 /* else anonymous ECDH, so no certificate or pkey. */
1406 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1407 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1408 ecdh=NULL;
1409 BN_CTX_free(bn_ctx);
1410 bn_ctx = NULL;
1411 EC_POINT_free(srvr_ecpoint);
1412 srvr_ecpoint = NULL;
1413 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001414
1415 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001416 {
1417 al=SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001418 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001419 goto f_err;
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
David Benjamined439582014-07-14 19:13:02 -04001422 /* At this point, |server_key_exchange| contains the
1423 * signature, if any, while |server_key_exchange_orig|
1424 * contains the entire message. From that, derive a CBS
1425 * containing just the parameter. */
1426 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1427 CBS_len(&server_key_exchange_orig) -
1428 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001429
1430 /* if it was signed, check the signature */
1431 if (pkey != NULL)
1432 {
David Benjamined439582014-07-14 19:13:02 -04001433 CBS signature;
1434
Adam Langley95c29f32014-06-20 12:00:00 -07001435 if (SSL_USE_SIGALGS(s))
1436 {
David Benjamin05da6e12014-07-12 20:42:55 -04001437 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
David Benjamined439582014-07-14 19:13:02 -04001438 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001439 }
1440 else
1441 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001442
David Benjamined439582014-07-14 19:13:02 -04001443 /* The last field in |server_key_exchange| is the
1444 * signature. */
1445 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1446 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001447 {
David Benjamined439582014-07-14 19:13:02 -04001448 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001449 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001450 goto f_err;
1451 }
1452
Adam Langley95c29f32014-06-20 12:00:00 -07001453 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1454 {
1455 int num;
David Benjamined439582014-07-14 19:13:02 -04001456 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1457 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langley95c29f32014-06-20 12:00:00 -07001459 q=md_buf;
1460 for (num=2; num > 0; num--)
1461 {
David Benjamined439582014-07-14 19:13:02 -04001462 unsigned int digest_len;
David Benjamine7127782014-08-18 23:15:29 -04001463 EVP_DigestInit_ex(&md_ctx,
1464 (num == 2) ? EVP_md5() : EVP_sha1(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001465 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1466 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001467 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1468 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1469 q += digest_len;
1470 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001471 }
David Benjamined439582014-07-14 19:13:02 -04001472 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1473 CBS_data(&signature), CBS_len(&signature),
1474 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001475 {
David Benjamined439582014-07-14 19:13:02 -04001476 al = SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001477 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001478 goto f_err;
1479 }
1480 }
1481 else
Adam Langley95c29f32014-06-20 12:00:00 -07001482 {
David Benjaminfd617a52014-10-31 00:21:46 -04001483 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1484 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random, SSL3_RANDOM_SIZE) ||
1485 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random, SSL3_RANDOM_SIZE) ||
1486 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter)) ||
1487 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature)))
Adam Langley95c29f32014-06-20 12:00:00 -07001488 {
1489 /* bad signature */
1490 al=SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001491 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001492 goto f_err;
1493 }
1494 }
1495 }
1496 else
1497 {
David Benjamine8f3d662014-07-12 01:10:19 -04001498 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001499 {
1500 /* Might be wrong key type, check it */
1501 if (ssl3_check_cert_and_algorithm(s))
1502 /* Otherwise this shouldn't happen */
David Benjamin8f8040d2014-07-14 19:14:46 -04001503 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001504 goto err;
1505 }
1506 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001507 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001508 {
1509 al=SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001510 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001511 goto f_err;
1512 }
1513 }
1514 EVP_PKEY_free(pkey);
1515 EVP_MD_CTX_cleanup(&md_ctx);
1516 return(1);
1517f_err:
1518 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1519err:
1520 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001521 if (rsa != NULL)
1522 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001523 if (dh != NULL)
1524 DH_free(dh);
Adam Langley95c29f32014-06-20 12:00:00 -07001525 BN_CTX_free(bn_ctx);
1526 EC_POINT_free(srvr_ecpoint);
1527 if (ecdh != NULL)
1528 EC_KEY_free(ecdh);
Adam Langley95c29f32014-06-20 12:00:00 -07001529 EVP_MD_CTX_cleanup(&md_ctx);
1530 return(-1);
1531 }
1532
1533static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1534 {
1535 return(X509_NAME_cmp(*a,*b));
1536 }
1537
1538int ssl3_get_certificate_request(SSL *s)
1539 {
1540 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001541 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001542 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001543 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001544 CBS cbs;
1545 CBS certificate_types;
1546 CBS certificate_authorities;
1547 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001548
1549 n=s->method->ssl_get_message(s,
1550 SSL3_ST_CR_CERT_REQ_A,
1551 SSL3_ST_CR_CERT_REQ_B,
1552 -1,
1553 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001554 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001555 &ok);
1556
1557 if (!ok) return((int)n);
1558
1559 s->s3->tmp.cert_req=0;
1560
1561 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1562 {
1563 s->s3->tmp.reuse_message=1;
1564 /* If we get here we don't need any cached handshake records
1565 * as we wont be doing client auth.
1566 */
1567 if (s->s3->handshake_buffer)
1568 {
Adam Langley75712922014-10-10 16:23:43 -07001569 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley95c29f32014-06-20 12:00:00 -07001570 goto err;
1571 }
1572 return(1);
1573 }
1574
1575 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1576 {
1577 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1578 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1579 goto err;
1580 }
1581
1582 /* TLS does not like anon-DH with client cert */
1583 if (s->version > SSL3_VERSION)
1584 {
1585 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1586 {
1587 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1588 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1589 goto err;
1590 }
1591 }
1592
David Benjamin51b1f742014-07-12 16:31:12 -04001593 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001594
1595 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1596 if (ca_sk == NULL)
1597 {
1598 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1599 goto err;
1600 }
1601
1602 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001603 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001604 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001605 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1606 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1607 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001608 }
David Benjamin676d1e72014-07-08 14:34:10 -04001609 if (!CBS_stow(&certificate_types,
1610 &s->s3->tmp.certificate_types,
1611 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001613 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1614 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616 if (SSL_USE_SIGALGS(s))
1617 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001618 CBS supported_signature_algorithms;
1619 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001620 {
1621 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001622 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001623 goto err;
1624 }
David Benjamincd996942014-07-20 16:23:51 -04001625 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001626 {
1627 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1628 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1629 goto err;
1630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631 }
1632
1633 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001634 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001635 {
1636 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1637 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1638 goto err;
1639 }
1640
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001641 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001642 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001643 CBS distinguished_name;
1644 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001645 {
Adam Langley95c29f32014-06-20 12:00:00 -07001646 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1647 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1648 goto err;
1649 }
1650
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001651 data = CBS_data(&distinguished_name);
1652 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001653 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001654 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1655 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1656 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001657 }
1658
David Benjamin407a10c2014-07-16 12:58:59 -04001659 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001660 {
1661 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1662 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1663 goto err;
1664 }
1665 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001666 {
1667 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1668 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1669 goto err;
1670 }
1671 if (!sk_X509_NAME_push(ca_sk,xn))
1672 {
1673 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1674 goto err;
1675 }
Adam Langley95c29f32014-06-20 12:00:00 -07001676 }
1677
Adam Langley95c29f32014-06-20 12:00:00 -07001678 /* we should setup a certificate to return.... */
1679 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001680 if (s->s3->tmp.ca_names != NULL)
1681 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1682 s->s3->tmp.ca_names=ca_sk;
1683 ca_sk=NULL;
1684
1685 ret=1;
1686err:
1687 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1688 return(ret);
1689 }
1690
Adam Langley95c29f32014-06-20 12:00:00 -07001691int ssl3_get_new_session_ticket(SSL *s)
1692 {
David Benjamine044c3d2014-07-14 19:14:12 -04001693 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001694 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001695 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001696
1697 n=s->method->ssl_get_message(s,
1698 SSL3_ST_CR_SESSION_TICKET_A,
1699 SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin64442872014-07-21 17:43:45 -04001700 SSL3_MT_NEWSESSION_TICKET,
Adam Langley95c29f32014-06-20 12:00:00 -07001701 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001702 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001703 &ok);
1704
1705 if (!ok)
1706 return((int)n);
1707
David Benjamine044c3d2014-07-14 19:14:12 -04001708 CBS_init(&new_session_ticket, s->init_msg, n);
1709
1710 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1711 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1712 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001713 {
Adam Langley95c29f32014-06-20 12:00:00 -07001714 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001715 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001716 goto f_err;
1717 }
1718
David Benjamine044c3d2014-07-14 19:14:12 -04001719 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001720 {
1721 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1722 goto err;
1723 }
David Benjamine044c3d2014-07-14 19:14:12 -04001724
Adam Langley95c29f32014-06-20 12:00:00 -07001725 /* There are two ways to detect a resumed ticket sesion.
1726 * One is to set an appropriate session ID and then the server
1727 * must return a match in ServerHello. This allows the normal
1728 * client session ID matching to work and we know much
1729 * earlier that the ticket has been accepted.
1730 *
1731 * The other way is to set zero length session ID when the
1732 * ticket is presented and rely on the handshake to determine
1733 * session resumption.
1734 *
1735 * We choose the former approach because this fits in with
1736 * assumptions elsewhere in OpenSSL. The session ID is set
1737 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1738 * ticket.
1739 */
David Benjamine044c3d2014-07-14 19:14:12 -04001740 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001741 s->session->session_id, &s->session->session_id_length,
Adam Langley95c29f32014-06-20 12:00:00 -07001742 EVP_sha256(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001743 ret=1;
1744 return(ret);
1745f_err:
1746 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1747err:
1748 return(-1);
1749 }
1750
1751int ssl3_get_cert_status(SSL *s)
1752 {
1753 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001754 long n;
1755 CBS certificate_status, ocsp_response;
1756 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001757
1758 n=s->method->ssl_get_message(s,
1759 SSL3_ST_CR_CERT_STATUS_A,
1760 SSL3_ST_CR_CERT_STATUS_B,
1761 SSL3_MT_CERTIFICATE_STATUS,
1762 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001763 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001764 &ok);
1765
1766 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001767
1768 CBS_init(&certificate_status, s->init_msg, n);
1769 if (!CBS_get_u8(&certificate_status, &status_type) ||
1770 status_type != TLSEXT_STATUSTYPE_ocsp ||
1771 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1772 CBS_len(&ocsp_response) == 0 ||
1773 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001774 {
1775 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04001776 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001777 goto f_err;
1778 }
David Benjamin46062682014-07-14 19:14:32 -04001779
David Benjamin6c7aed02014-08-27 16:42:38 -04001780 if (!CBS_stow(&ocsp_response,
1781 &s->session->ocsp_response, &s->session->ocsp_response_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001782 {
1783 al = SSL_AD_INTERNAL_ERROR;
1784 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1785 goto f_err;
1786 }
Adam Langley95c29f32014-06-20 12:00:00 -07001787 return 1;
1788f_err:
1789 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1790 return(-1);
1791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
1793int ssl3_get_server_done(SSL *s)
1794 {
1795 int ok,ret=0;
1796 long n;
1797
1798 n=s->method->ssl_get_message(s,
1799 SSL3_ST_CR_SRVR_DONE_A,
1800 SSL3_ST_CR_SRVR_DONE_B,
1801 SSL3_MT_SERVER_DONE,
1802 30, /* should be very small, like 0 :-) */
David Benjamin590cbe92014-08-25 21:34:56 -04001803 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001804 &ok);
1805
1806 if (!ok) return((int)n);
1807 if (n > 0)
1808 {
1809 /* should contain no data */
1810 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1811 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1812 return -1;
1813 }
1814 ret=1;
1815 return(ret);
1816 }
1817
1818
1819int ssl3_send_client_key_exchange(SSL *s)
1820 {
1821 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07001822 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001823 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001824 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001825 unsigned char *q;
1826 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001827 EC_KEY *clnt_ecdh = NULL;
1828 const EC_POINT *srvr_ecpoint = NULL;
1829 EVP_PKEY *srvr_pub_pkey = NULL;
1830 unsigned char *encodedPoint = NULL;
1831 int encoded_pt_len = 0;
1832 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001833 unsigned int psk_len = 0;
1834 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langley75712922014-10-10 16:23:43 -07001835 uint8_t *pms = NULL;
1836 size_t pms_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
1838 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1839 {
1840 p = ssl_handshake_start(s);
1841
1842 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07001843 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1844
David Benjamin93d67d32014-10-28 00:53:38 -04001845 /* If using a PSK key exchange, prepare the pre-shared key. */
Adam Langleyc26c8022014-06-20 12:00:00 -07001846 if (alg_a & SSL_aPSK)
1847 {
Adam Langley01797e32014-06-20 12:00:00 -07001848 char identity[PSK_MAX_IDENTITY_LEN + 1];
1849 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001850
Adam Langleyc26c8022014-06-20 12:00:00 -07001851 if (s->psk_client_callback == NULL)
1852 {
1853 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
1854 goto err;
1855 }
1856
Adam Langley01797e32014-06-20 12:00:00 -07001857 memset(identity, 0, sizeof(identity));
David Benjamin688d8df2014-11-02 23:06:42 -05001858 psk_len = s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07001859 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07001860 if (psk_len > PSK_MAX_PSK_LEN)
1861 {
1862 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin93d67d32014-10-28 00:53:38 -04001863 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001864 }
1865 else if (psk_len == 0)
1866 {
1867 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamin93d67d32014-10-28 00:53:38 -04001868 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1869 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001870 }
Adam Langley01797e32014-06-20 12:00:00 -07001871 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1872 if (identity_len > PSK_MAX_IDENTITY_LEN)
1873 {
1874 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin93d67d32014-10-28 00:53:38 -04001875 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001876 }
1877
Adam Langleyc26c8022014-06-20 12:00:00 -07001878 if (s->session->psk_identity != NULL)
1879 OPENSSL_free(s->session->psk_identity);
1880 s->session->psk_identity = BUF_strdup(identity);
1881 if (s->session->psk_identity == NULL)
1882 {
1883 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langleyc26c8022014-06-20 12:00:00 -07001884 goto err;
1885 }
David Benjamin93d67d32014-10-28 00:53:38 -04001886
1887 /* Write out psk_identity. */
1888 s2n(identity_len, p);
1889 memcpy(p, identity, identity_len);
1890 p += identity_len;
1891 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
David Benjamin93d67d32014-10-28 00:53:38 -04001894 /* Depending on the key exchange method, compute |pms|
1895 * and |pms_len|. */
David Benjamin457112e2014-08-27 14:35:09 -04001896 if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001897 {
1898 RSA *rsa;
David Benjamin93d67d32014-10-28 00:53:38 -04001899 size_t enc_pms_len;
Adam Langley75712922014-10-10 16:23:43 -07001900
1901 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1902 pms = OPENSSL_malloc(pms_len);
1903 if (pms == NULL)
1904 {
1905 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
1906 goto err;
1907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
Emilia Käsperc3d79602014-07-24 22:15:29 +02001909 if (s->session->sess_cert == NULL)
1910 {
1911 /* We should always have a server certificate with SSL_kRSA. */
1912 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1913 goto err;
1914 }
1915
David Benjamin525a0fe2014-11-08 11:41:12 -05001916 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1917 if ((pkey == NULL) ||
1918 (pkey->type != EVP_PKEY_RSA) ||
1919 (pkey->pkey.rsa == NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07001920 {
David Benjamin525a0fe2014-11-08 11:41:12 -05001921 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin1df11242014-11-08 11:41:13 -05001922 if (pkey != NULL)
1923 EVP_PKEY_free(pkey);
David Benjamin525a0fe2014-11-08 11:41:12 -05001924 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001925 }
David Benjamin525a0fe2014-11-08 11:41:12 -05001926 rsa=pkey->pkey.rsa;
1927 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langley75712922014-10-10 16:23:43 -07001929 pms[0]=s->client_version>>8;
1930 pms[1]=s->client_version&0xff;
1931 if (RAND_bytes(&pms[2],SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001932 goto err;
1933
Adam Langley75712922014-10-10 16:23:43 -07001934 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -07001935
1936 q=p;
David Benjamin93d67d32014-10-28 00:53:38 -04001937 /* In TLS and beyond, reserve space for the length prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -07001938 if (s->version > SSL3_VERSION)
David Benjamin93d67d32014-10-28 00:53:38 -04001939 {
1940 p += 2;
1941 n += 2;
1942 }
1943 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa),
1944 pms, pms_len, RSA_PKCS1_PADDING))
Adam Langley95c29f32014-06-20 12:00:00 -07001945 {
1946 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
1947 goto err;
1948 }
David Benjamin93d67d32014-10-28 00:53:38 -04001949 n += enc_pms_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001950
David Benjamin859ec3c2014-09-02 16:29:36 -04001951 /* Log the premaster secret, if logging is enabled. */
1952 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx,
David Benjamin93d67d32014-10-28 00:53:38 -04001953 p, enc_pms_len, pms, pms_len))
David Benjamin859ec3c2014-09-02 16:29:36 -04001954 {
1955 goto err;
1956 }
1957
David Benjamin93d67d32014-10-28 00:53:38 -04001958 /* Fill in the length prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -07001959 if (s->version > SSL3_VERSION)
1960 {
David Benjamin93d67d32014-10-28 00:53:38 -04001961 s2n(enc_pms_len, q);
Adam Langley95c29f32014-06-20 12:00:00 -07001962 }
Adam Langley95c29f32014-06-20 12:00:00 -07001963 }
David Benjamin0da0e182014-08-19 16:20:28 -04001964 else if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001965 {
David Benjamin93d67d32014-10-28 00:53:38 -04001966 DH *dh_srvr, *dh_clnt;
Adam Langley95c29f32014-06-20 12:00:00 -07001967 SESS_CERT *scert = s->session->sess_cert;
David Benjamin93d67d32014-10-28 00:53:38 -04001968 int dh_len;
1969 size_t pub_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001970
1971 if (scert == NULL)
1972 {
1973 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1974 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1975 goto err;
1976 }
1977
David Benjamin0da0e182014-08-19 16:20:28 -04001978 if (scert->peer_dh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001979 {
David Benjamin0da0e182014-08-19 16:20:28 -04001980 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1981 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001982 }
David Benjamin0da0e182014-08-19 16:20:28 -04001983 dh_srvr=scert->peer_dh_tmp;
1984
David Benjaminbd30f8e2014-08-19 16:02:38 -04001985 /* generate a new random key */
1986 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001987 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04001988 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1989 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001990 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001991 if (!DH_generate_key(dh_clnt))
Adam Langley95c29f32014-06-20 12:00:00 -07001992 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04001993 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1994 DH_free(dh_clnt);
1995 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001996 }
1997
Adam Langley75712922014-10-10 16:23:43 -07001998 pms_len = DH_size(dh_clnt);
1999 pms = OPENSSL_malloc(pms_len);
2000 if (pms == NULL)
2001 {
2002 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2003 DH_free(dh_clnt);
2004 goto err;
2005 }
Adam Langley95c29f32014-06-20 12:00:00 -07002006
David Benjamin93d67d32014-10-28 00:53:38 -04002007 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2008 if (dh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002009 {
2010 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2011 DH_free(dh_clnt);
2012 goto err;
2013 }
David Benjamin93d67d32014-10-28 00:53:38 -04002014 pms_len = dh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002015
David Benjaminbd30f8e2014-08-19 16:02:38 -04002016 /* send off the data */
David Benjamin93d67d32014-10-28 00:53:38 -04002017 pub_len = BN_num_bytes(dh_clnt->pub_key);
2018 s2n(pub_len, p);
2019 BN_bn2bin(dh_clnt->pub_key, p);
2020 n += 2 + pub_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002021
2022 DH_free(dh_clnt);
Adam Langley95c29f32014-06-20 12:00:00 -07002023 }
Adam Langley95c29f32014-06-20 12:00:00 -07002024
David Benjamin0da0e182014-08-19 16:20:28 -04002025 else if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002026 {
2027 const EC_GROUP *srvr_group = NULL;
2028 EC_KEY *tkey;
David Benjamin93d67d32014-10-28 00:53:38 -04002029 int field_size = 0, ecdh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002030
Adam Langleyd06eddd2014-06-20 12:00:00 -07002031 if (s->session->sess_cert == NULL)
2032 {
2033 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2034 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2035 goto err;
2036 }
2037
David Benjamin0da0e182014-08-19 16:20:28 -04002038 if (s->session->sess_cert->peer_ecdh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002039 {
David Benjamin0da0e182014-08-19 16:20:28 -04002040 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2041 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002042 }
David Benjamin0da0e182014-08-19 16:20:28 -04002043 tkey = s->session->sess_cert->peer_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002044
2045 srvr_group = EC_KEY_get0_group(tkey);
2046 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2047
2048 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2049 {
2050 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2051 goto err;
2052 }
2053
2054 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2055 {
2056 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2057 goto err;
2058 }
2059
2060 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2061 {
2062 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2063 goto err;
2064 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002065 /* Generate a new ECDH key pair */
2066 if (!(EC_KEY_generate_key(clnt_ecdh)))
Adam Langley95c29f32014-06-20 12:00:00 -07002067 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002068 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2069 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002070 }
2071
Adam Langley95c29f32014-06-20 12:00:00 -07002072 field_size = EC_GROUP_get_degree(srvr_group);
2073 if (field_size <= 0)
2074 {
2075 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2076 goto err;
2077 }
David Benjamin93d67d32014-10-28 00:53:38 -04002078
2079 pms_len = (field_size + 7) / 8;
2080 pms = OPENSSL_malloc(pms_len);
2081 if (pms == NULL)
2082 {
2083 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2084 goto err;
2085 }
2086
2087 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
2088 if (ecdh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002089 {
2090 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2091 goto err;
2092 }
David Benjamin93d67d32014-10-28 00:53:38 -04002093 pms_len = ecdh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002094
David Benjaminbd30f8e2014-08-19 16:02:38 -04002095 /* First check the size of encoding and
2096 * allocate memory accordingly.
2097 */
2098 encoded_pt_len =
2099 EC_POINT_point2oct(srvr_group,
Adam Langley95c29f32014-06-20 12:00:00 -07002100 EC_KEY_get0_public_key(clnt_ecdh),
2101 POINT_CONVERSION_UNCOMPRESSED,
2102 NULL, 0, NULL);
2103
David Benjaminbd30f8e2014-08-19 16:02:38 -04002104 encodedPoint = (unsigned char *)
2105 OPENSSL_malloc(encoded_pt_len *
Adam Langley95c29f32014-06-20 12:00:00 -07002106 sizeof(unsigned char));
David Benjaminbd30f8e2014-08-19 16:02:38 -04002107 bn_ctx = BN_CTX_new();
2108 if ((encodedPoint == NULL) ||
2109 (bn_ctx == NULL))
2110 {
2111 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2112 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002113 }
2114
David Benjaminbd30f8e2014-08-19 16:02:38 -04002115 /* Encode the public key */
2116 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2117 EC_KEY_get0_public_key(clnt_ecdh),
2118 POINT_CONVERSION_UNCOMPRESSED,
2119 encodedPoint, encoded_pt_len, bn_ctx);
2120
David Benjaminbd30f8e2014-08-19 16:02:38 -04002121 *p = encoded_pt_len; /* length of encoded point */
2122 /* Encoded point will be copied here */
2123 p += 1;
2124 n += 1;
2125 /* copy the point */
David Benjamin93d67d32014-10-28 00:53:38 -04002126 memcpy(p, encodedPoint, encoded_pt_len);
David Benjaminbd30f8e2014-08-19 16:02:38 -04002127 /* increment n to account for length field */
2128 n += encoded_pt_len;
2129
Adam Langley95c29f32014-06-20 12:00:00 -07002130 /* Free allocated memory */
2131 BN_CTX_free(bn_ctx);
David Benjamin93d67d32014-10-28 00:53:38 -04002132 bn_ctx = NULL;
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07002133 OPENSSL_free(encodedPoint);
David Benjamin93d67d32014-10-28 00:53:38 -04002134 encodedPoint = NULL;
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07002135 EC_KEY_free(clnt_ecdh);
David Benjamin93d67d32014-10-28 00:53:38 -04002136 clnt_ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002137 EVP_PKEY_free(srvr_pub_pkey);
David Benjamin93d67d32014-10-28 00:53:38 -04002138 srvr_pub_pkey = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002139 }
David Benjamin93d67d32014-10-28 00:53:38 -04002140 else if (alg_k & SSL_kPSK)
2141 {
2142 /* For plain PSK, other_secret is a block of 0s with the same
2143 * length as the pre-shared key. */
2144 pms_len = psk_len;
2145 pms = OPENSSL_malloc(pms_len);
2146 if (pms == NULL)
2147 {
2148 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2149 goto err;
2150 }
2151 memset(pms, 0, pms_len);
2152 }
2153 else
Adam Langley95c29f32014-06-20 12:00:00 -07002154 {
2155 ssl3_send_alert(s, SSL3_AL_FATAL,
2156 SSL_AD_HANDSHAKE_FAILURE);
2157 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2158 goto err;
2159 }
2160
David Benjamin93d67d32014-10-28 00:53:38 -04002161 /* For a PSK cipher suite, other_secret is combined
2162 * with the pre-shared key. */
David Benjamin1f10d9c2014-11-08 11:41:14 -05002163 if (alg_a & SSL_aPSK)
David Benjamin93d67d32014-10-28 00:53:38 -04002164 {
2165 CBB cbb, child;
2166 uint8_t *new_pms;
2167 size_t new_pms_len;
2168
2169 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len))
2170 {
2171 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2172 goto err;
2173 }
2174 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
2175 !CBB_add_bytes(&child, pms, pms_len) ||
2176 !CBB_add_u16_length_prefixed(&cbb, &child) ||
2177 !CBB_add_bytes(&child, psk, psk_len) ||
2178 !CBB_finish(&cbb, &new_pms, &new_pms_len))
2179 {
2180 CBB_cleanup(&cbb);
2181 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2182 goto err;
2183 }
2184 OPENSSL_cleanse(pms, pms_len);
2185 OPENSSL_free(pms);
2186 pms = new_pms;
2187 pms_len = new_pms_len;
2188 }
2189
Adam Langley95c29f32014-06-20 12:00:00 -07002190 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2191 s->state=SSL3_ST_CW_KEY_EXCH_B;
Adam Langley75712922014-10-10 16:23:43 -07002192
2193 /* The message must be added to the finished hash before
2194 * calculating the master secret. */
2195 s->method->ssl3_enc->add_to_finished_hash(s);
2196
2197 s->session->master_key_length =
2198 s->method->ssl3_enc->generate_master_secret(s,
2199 s->session->master_key,
2200 pms, pms_len);
2201 if (s->session->master_key_length == 0)
2202 {
2203 goto err;
2204 }
2205 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2206 OPENSSL_cleanse(pms, pms_len);
2207 OPENSSL_free(pms);
Adam Langley95c29f32014-06-20 12:00:00 -07002208 }
2209
2210 /* SSL3_ST_CW_KEY_EXCH_B */
Adam Langley75712922014-10-10 16:23:43 -07002211 /* The message has already been added to the finished hash. */
2212 return s->method->ssl3_enc->do_write(s, dont_add_to_finished_hash);
2213
Adam Langley95c29f32014-06-20 12:00:00 -07002214err:
Adam Langley95c29f32014-06-20 12:00:00 -07002215 BN_CTX_free(bn_ctx);
2216 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2217 if (clnt_ecdh != NULL)
2218 EC_KEY_free(clnt_ecdh);
2219 EVP_PKEY_free(srvr_pub_pkey);
Adam Langley75712922014-10-10 16:23:43 -07002220 if (pms)
David Benjamin93d67d32014-10-28 00:53:38 -04002221 {
2222 OPENSSL_cleanse(pms, pms_len);
Adam Langley75712922014-10-10 16:23:43 -07002223 OPENSSL_free(pms);
David Benjamin93d67d32014-10-28 00:53:38 -04002224 }
Adam Langley75712922014-10-10 16:23:43 -07002225 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002226 }
2227
David Benjamin8da99062014-08-24 12:03:09 -04002228int ssl3_send_cert_verify(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002229 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002230 unsigned char *buf, *p;
David Benjamin854dd652014-08-26 00:32:30 -04002231 const EVP_MD *md = NULL;
David Benjamin0e2908a2014-07-09 15:22:43 -04002232 uint8_t digest[EVP_MAX_MD_SIZE];
David Benjamin854dd652014-08-26 00:32:30 -04002233 size_t digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002234 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002235 EVP_PKEY_CTX *pctx = NULL;
David Benjamin0e2908a2014-07-09 15:22:43 -04002236 size_t signature_length = 0;
2237 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002238
David Benjamin0e2908a2014-07-09 15:22:43 -04002239 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002240
2241 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2242 {
2243 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002244 pkey = s->cert->key->privatekey;
David Benjamin854dd652014-08-26 00:32:30 -04002245
2246 /* Write out the digest type if needbe. */
Adam Langley95c29f32014-06-20 12:00:00 -07002247 if (SSL_USE_SIGALGS(s))
2248 {
David Benjaminec2f27d2014-11-13 19:17:25 -05002249 md = tls1_choose_signing_digest(s, pkey);
David Benjamin854dd652014-08-26 00:32:30 -04002250 if (!tls12_get_sigandhash(p, pkey, md))
Adam Langley95c29f32014-06-20 12:00:00 -07002251 {
David Benjamin8da99062014-08-24 12:03:09 -04002252 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002253 goto err;
2254 }
2255 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002256 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07002257 }
David Benjamin854dd652014-08-26 00:32:30 -04002258
2259 /* Compute the digest. */
2260 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07002261 goto err;
David Benjamin854dd652014-08-26 00:32:30 -04002262
2263 /* The handshake buffer is no longer necessary. */
Adam Langley75712922014-10-10 16:23:43 -07002264 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, free_handshake_buffer))
David Benjamin854dd652014-08-26 00:32:30 -04002265 goto err;
David Benjamin0e2908a2014-07-09 15:22:43 -04002266
2267 /* Sign the digest. */
2268 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2269 if (pctx == NULL)
2270 goto err;
2271
2272 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
David Benjamin854dd652014-08-26 00:32:30 -04002273 if (!EVP_PKEY_sign_init(pctx) ||
2274 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2275 !EVP_PKEY_sign(pctx, NULL, &signature_length,
2276 digest, digest_length))
David Benjamin0e2908a2014-07-09 15:22:43 -04002277 {
David Benjamin8da99062014-08-24 12:03:09 -04002278 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002279 goto err;
2280 }
2281
2282 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2283 {
David Benjamin8da99062014-08-24 12:03:09 -04002284 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin0e2908a2014-07-09 15:22:43 -04002285 goto err;
2286 }
2287
David Benjamin854dd652014-08-26 00:32:30 -04002288 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length,
2289 digest, digest_length))
David Benjamin0e2908a2014-07-09 15:22:43 -04002290 {
David Benjamin8da99062014-08-24 12:03:09 -04002291 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002292 goto err;
2293 }
2294
2295 s2n(signature_length, p);
2296 n += signature_length + 2;
2297
Adam Langley95c29f32014-06-20 12:00:00 -07002298 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2299 s->state=SSL3_ST_CW_CERT_VRFY_B;
2300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301 EVP_PKEY_CTX_free(pctx);
2302 return ssl_do_write(s);
2303err:
Adam Langley95c29f32014-06-20 12:00:00 -07002304 EVP_PKEY_CTX_free(pctx);
2305 return(-1);
2306 }
2307
David Benjaminec2f27d2014-11-13 19:17:25 -05002308/* ssl3_has_client_certificate returns true if a client certificate is
2309 * configured. */
2310static int ssl3_has_client_certificate(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002311 {
David Benjaminec2f27d2014-11-13 19:17:25 -05002312 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
Adam Langley95c29f32014-06-20 12:00:00 -07002313 }
2314
2315int ssl3_send_client_certificate(SSL *s)
2316 {
2317 X509 *x509=NULL;
2318 EVP_PKEY *pkey=NULL;
2319 int i;
2320
2321 if (s->state == SSL3_ST_CW_CERT_A)
2322 {
2323 /* Let cert callback update client certificates if required */
2324 if (s->cert->cert_cb)
2325 {
2326 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2327 if (i < 0)
2328 {
2329 s->rwstate=SSL_X509_LOOKUP;
2330 return -1;
2331 }
2332 if (i == 0)
2333 {
2334 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2335 return 0;
2336 }
2337 s->rwstate=SSL_NOTHING;
2338 }
David Benjaminec2f27d2014-11-13 19:17:25 -05002339 if (ssl3_has_client_certificate(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002340 s->state=SSL3_ST_CW_CERT_C;
2341 else
2342 s->state=SSL3_ST_CW_CERT_B;
2343 }
2344
2345 /* We need to get a client cert */
2346 if (s->state == SSL3_ST_CW_CERT_B)
2347 {
2348 /* If we get an error, we need to
2349 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
David Benjamin7bbeead2014-07-25 03:56:20 -04002350 * We then get retried later */
Adam Langley95c29f32014-06-20 12:00:00 -07002351 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2352 if (i < 0)
2353 {
2354 s->rwstate=SSL_X509_LOOKUP;
2355 return(-1);
2356 }
2357 s->rwstate=SSL_NOTHING;
2358 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2359 {
2360 s->state=SSL3_ST_CW_CERT_B;
2361 if ( !SSL_use_certificate(s,x509) ||
2362 !SSL_use_PrivateKey(s,pkey))
2363 i=0;
2364 }
2365 else if (i == 1)
2366 {
2367 i=0;
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002368 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
Adam Langley95c29f32014-06-20 12:00:00 -07002369 }
2370
2371 if (x509 != NULL) X509_free(x509);
2372 if (pkey != NULL) EVP_PKEY_free(pkey);
David Benjaminec2f27d2014-11-13 19:17:25 -05002373 if (i && !ssl3_has_client_certificate(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002374 i = 0;
2375 if (i == 0)
2376 {
2377 if (s->version == SSL3_VERSION)
2378 {
2379 s->s3->tmp.cert_req=0;
2380 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2381 return(1);
2382 }
2383 else
2384 {
2385 s->s3->tmp.cert_req=2;
2386 }
2387 }
2388
2389 /* Ok, we have a cert */
2390 s->state=SSL3_ST_CW_CERT_C;
2391 }
2392
2393 if (s->state == SSL3_ST_CW_CERT_C)
2394 {
2395 s->state=SSL3_ST_CW_CERT_D;
2396 ssl3_output_cert_chain(s,
2397 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2398 }
2399 /* SSL3_ST_CW_CERT_D */
2400 return ssl_do_write(s);
2401 }
2402
2403#define has_bits(i,m) (((i)&(m)) == (m))
2404
2405int ssl3_check_cert_and_algorithm(SSL *s)
2406 {
2407 int i,idx;
2408 long alg_k,alg_a;
2409 EVP_PKEY *pkey=NULL;
2410 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002411 DH *dh;
Adam Langley95c29f32014-06-20 12:00:00 -07002412
David Benjamine8f3d662014-07-12 01:10:19 -04002413 /* we don't have a certificate */
2414 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2415 return 1;
2416
Adam Langley95c29f32014-06-20 12:00:00 -07002417 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2418 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2419
Adam Langley95c29f32014-06-20 12:00:00 -07002420 sc=s->session->sess_cert;
2421 if (sc == NULL)
2422 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002423 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002424 goto err;
2425 }
2426
Adam Langley95c29f32014-06-20 12:00:00 -07002427 dh=s->session->sess_cert->peer_dh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002428
2429 /* This is the passed certificate */
2430
2431 idx=sc->peer_cert_type;
Adam Langley95c29f32014-06-20 12:00:00 -07002432 if (idx == SSL_PKEY_ECC)
2433 {
2434 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2435 s) == 0)
2436 { /* check failed */
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002437 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002438 goto f_err;
2439 }
2440 else
2441 {
2442 return 1;
2443 }
2444 }
2445 else if (alg_a & SSL_aECDSA)
2446 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002447 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002448 goto f_err;
2449 }
Adam Langley95c29f32014-06-20 12:00:00 -07002450 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2451 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2452 EVP_PKEY_free(pkey);
2453
2454
2455 /* Check that we have a certificate if we require one */
2456 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2457 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002458 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002459 goto f_err;
2460 }
David Benjamin525a0fe2014-11-08 11:41:12 -05002461 if ((alg_k & SSL_kRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_ENC))
Adam Langley95c29f32014-06-20 12:00:00 -07002462 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002463 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002464 goto f_err;
2465 }
Adam Langley95c29f32014-06-20 12:00:00 -07002466 if ((alg_k & SSL_kEDH) &&
2467 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2468 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002469 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -07002470 goto f_err;
2471 }
Adam Langley95c29f32014-06-20 12:00:00 -07002472
Adam Langley95c29f32014-06-20 12:00:00 -07002473 return(1);
2474f_err:
2475 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2476err:
2477 return(0);
2478 }
2479
Adam Langley95c29f32014-06-20 12:00:00 -07002480int ssl3_send_next_proto(SSL *s)
2481 {
2482 unsigned int len, padding_len;
2483 unsigned char *d;
2484
2485 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
2486 {
2487 len = s->next_proto_negotiated_len;
2488 padding_len = 32 - ((len + 2) % 32);
2489 d = (unsigned char *)s->init_buf->data;
2490 d[4] = len;
2491 memcpy(d + 5, s->next_proto_negotiated, len);
2492 d[5 + len] = padding_len;
2493 memset(d + 6 + len, 0, padding_len);
2494 *(d++)=SSL3_MT_NEXT_PROTO;
2495 l2n3(2 + len + padding_len, d);
2496 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2497 s->init_num = 4 + 2 + len + padding_len;
2498 s->init_off = 0;
2499 }
2500
Adam Langley75712922014-10-10 16:23:43 -07002501 return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley95c29f32014-06-20 12:00:00 -07002502}
Adam Langley1258b6a2014-06-20 12:00:00 -07002503
Adam Langley1258b6a2014-06-20 12:00:00 -07002504
2505int ssl3_send_channel_id(SSL *s)
2506 {
2507 unsigned char *d;
2508 int ret = -1, public_key_len;
2509 EVP_MD_CTX md_ctx;
2510 size_t sig_len;
2511 ECDSA_SIG *sig = NULL;
2512 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
2513
2514 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
Adam Langley75712922014-10-10 16:23:43 -07002515 return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley1258b6a2014-06-20 12:00:00 -07002516
2517 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
2518 {
2519 EVP_PKEY *key = NULL;
2520 s->ctx->channel_id_cb(s, &key);
2521 if (key != NULL)
2522 {
2523 s->tlsext_channel_id_private = key;
2524 }
2525 }
2526 if (!s->tlsext_channel_id_private)
2527 {
2528 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
2529 return (-1);
2530 }
2531 s->rwstate=SSL_NOTHING;
2532
2533 d = (unsigned char *)s->init_buf->data;
2534 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
2535 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
2536 if (s->s3->tlsext_channel_id_new)
2537 s2n(TLSEXT_TYPE_channel_id_new, d);
2538 else
2539 s2n(TLSEXT_TYPE_channel_id, d);
2540 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2541
2542 EVP_MD_CTX_init(&md_ctx);
2543
2544 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2545 if (public_key_len <= 0)
2546 {
2547 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2548 goto err;
2549 }
2550 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2551 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2552 * field elements as 32-byte, big-endian numbers. */
2553 if (public_key_len != 65)
2554 {
2555 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2556 goto err;
2557 }
2558 public_key = OPENSSL_malloc(public_key_len);
2559 if (!public_key)
2560 {
2561 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2562 goto err;
2563 }
2564
2565 derp = public_key;
2566 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2567
2568 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2569 s->tlsext_channel_id_private) != 1)
2570 {
2571 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2572 goto err;
2573 }
2574
2575 if (!tls1_channel_id_hash(&md_ctx, s))
2576 goto err;
2577
2578 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
2579 {
2580 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2581 goto err;
2582 }
2583
2584 der_sig = OPENSSL_malloc(sig_len);
2585 if (!der_sig)
2586 {
2587 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2588 goto err;
2589 }
2590
2591 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
2592 {
2593 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2594 goto err;
2595 }
2596
2597 derp = der_sig;
2598 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
2599 if (sig == NULL)
2600 {
2601 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2602 goto err;
2603 }
2604
2605 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2606 memcpy(d, public_key + 1, 64);
2607 d += 64;
David Benjaminf2b32a22014-11-02 17:32:40 -05002608 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2609 !BN_bn2bin_padded(d + 32, 32, sig->s))
2610 {
2611 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2612 goto err;
2613 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002614
2615 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2616 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
2617 s->init_off = 0;
2618
Adam Langley75712922014-10-10 16:23:43 -07002619 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley1258b6a2014-06-20 12:00:00 -07002620
2621err:
2622 EVP_MD_CTX_cleanup(&md_ctx);
2623 if (public_key)
2624 OPENSSL_free(public_key);
2625 if (der_sig)
2626 OPENSSL_free(der_sig);
2627 if (sig)
2628 ECDSA_SIG_free(sig);
2629
2630 return ret;
2631 }
Adam Langley95c29f32014-06-20 12:00:00 -07002632
Adam Langley95c29f32014-06-20 12:00:00 -07002633int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2634 {
2635 int i = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002636 if (s->ctx->client_cert_cb)
2637 i = s->ctx->client_cert_cb(s,px509,ppkey);
2638 return i;
2639 }