blob: 6f47e95c17a201f3b4af697a4a14359228c4c045 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
134 *
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
138 *
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
143 *
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
149 */
150
151#include <stdio.h>
152
153#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400154#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155#include <openssl/rand.h>
156#include <openssl/obj.h>
157#include <openssl/evp.h>
158#include <openssl/mem.h>
159#include <openssl/md5.h>
160#include <openssl/dh.h>
161#include <openssl/bn.h>
162#include <openssl/engine.h>
163#include <openssl/x509.h>
164
165#include "ssl_locl.h"
166#include "../crypto/dh/internal.h"
167
David Benjamin5b33a5e2014-09-24 16:27:30 -0400168IMPLEMENT_tls_meth_func(TLS1_2_VERSION, TLSv1_2_client_method,
169 ssl_undefined_function,
170 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400171 TLSv1_2_enc_data)
172
173IMPLEMENT_tls_meth_func(TLS1_1_VERSION, TLSv1_1_client_method,
174 ssl_undefined_function,
175 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400176 TLSv1_1_enc_data)
177
178IMPLEMENT_tls_meth_func(TLS1_VERSION, TLSv1_client_method,
179 ssl_undefined_function,
180 ssl3_connect,
David Benjamin5b33a5e2014-09-24 16:27:30 -0400181 TLSv1_enc_data)
182
David Benjamin00075b82014-09-24 15:48:14 -0400183IMPLEMENT_tls_meth_func(SSL3_VERSION, SSLv3_client_method,
Adam Langley95c29f32014-06-20 12:00:00 -0700184 ssl_undefined_function,
185 ssl3_connect,
David Benjamin00075b82014-09-24 15:48:14 -0400186 SSLv3_enc_data)
Adam Langley95c29f32014-06-20 12:00:00 -0700187
188int ssl3_connect(SSL *s)
189 {
190 BUF_MEM *buf=NULL;
191 void (*cb)(const SSL *ssl,int type,int val)=NULL;
192 int ret= -1;
193 int new_state,state,skip=0;
194
195 ERR_clear_error();
196 ERR_clear_system_error();
197
198 if (s->info_callback != NULL)
199 cb=s->info_callback;
200 else if (s->ctx->info_callback != NULL)
201 cb=s->ctx->info_callback;
202
203 s->in_handshake++;
204 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
205
Adam Langley95c29f32014-06-20 12:00:00 -0700206 for (;;)
207 {
208 state=s->state;
209
210 switch(s->state)
211 {
212 case SSL_ST_RENEGOTIATE:
213 s->renegotiate=1;
214 s->state=SSL_ST_CONNECT;
215 s->ctx->stats.sess_connect_renegotiate++;
216 /* break */
217 case SSL_ST_BEFORE:
218 case SSL_ST_CONNECT:
219 case SSL_ST_BEFORE|SSL_ST_CONNECT:
220 case SSL_ST_OK|SSL_ST_CONNECT:
221
222 s->server=0;
223 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
224
225 if ((s->version & 0xff00 ) != 0x0300)
226 {
227 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
228 ret = -1;
229 goto end;
230 }
231
232 /* s->version=SSL3_VERSION; */
233 s->type=SSL_ST_CONNECT;
234
235 if (s->init_buf == NULL)
236 {
237 if ((buf=BUF_MEM_new()) == NULL)
238 {
239 ret= -1;
240 goto end;
241 }
242 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
243 {
244 ret= -1;
245 goto end;
246 }
247 s->init_buf=buf;
248 buf=NULL;
249 }
250
251 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
252
253 /* setup buffing BIO */
254 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
255
256 /* don't push the buffering BIO quite yet */
257
Adam Langley69a01602014-11-17 17:26:55 -0800258 if (!ssl3_init_finished_mac(s))
259 {
260 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
261 ret = -1;
262 goto end;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
265 s->state=SSL3_ST_CW_CLNT_HELLO_A;
266 s->ctx->stats.sess_connect++;
267 s->init_num=0;
268 break;
269
270 case SSL3_ST_CW_CLNT_HELLO_A:
271 case SSL3_ST_CW_CLNT_HELLO_B:
272
273 s->shutdown=0;
David Benjamin8da99062014-08-24 12:03:09 -0400274 ret=ssl3_send_client_hello(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700275 if (ret <= 0) goto end;
276 s->state=SSL3_ST_CR_SRVR_HELLO_A;
277 s->init_num=0;
278
279 /* turn on buffering for the next lot of output */
280 if (s->bbio != s->wbio)
281 s->wbio=BIO_push(s->bbio,s->wbio);
282
283 break;
284
285 case SSL3_ST_CR_SRVR_HELLO_A:
286 case SSL3_ST_CR_SRVR_HELLO_B:
287 ret=ssl3_get_server_hello(s);
288 if (ret <= 0) goto end;
289
290 if (s->hit)
291 {
David Benjamincb5abad2014-07-25 12:14:28 -0400292 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700293 if (s->tlsext_ticket_expected)
294 {
295 /* receive renewed session ticket */
296 s->state=SSL3_ST_CR_SESSION_TICKET_A;
297 }
Adam Langley95c29f32014-06-20 12:00:00 -0700298 }
299 else
300 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400301 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700302 }
303 s->init_num=0;
304 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400305
Adam Langley95c29f32014-06-20 12:00:00 -0700306 case SSL3_ST_CR_CERT_A:
307 case SSL3_ST_CR_CERT_B:
David Benjamine8f3d662014-07-12 01:10:19 -0400308 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700309 {
310 ret=ssl3_get_server_certificate(s);
311 if (ret <= 0) goto end;
David Benjamin6c7aed02014-08-27 16:42:38 -0400312 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -0700313 s->state=SSL3_ST_CR_CERT_STATUS_A;
314 else
315 s->state=SSL3_ST_CR_KEY_EXCH_A;
316 }
317 else
318 {
319 skip = 1;
320 s->state=SSL3_ST_CR_KEY_EXCH_A;
321 }
Adam Langley95c29f32014-06-20 12:00:00 -0700322 s->init_num=0;
323 break;
324
325 case SSL3_ST_CR_KEY_EXCH_A:
326 case SSL3_ST_CR_KEY_EXCH_B:
David Benjamin8f8040d2014-07-14 19:14:46 -0400327 ret=ssl3_get_server_key_exchange(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700328 if (ret <= 0) goto end;
329 s->state=SSL3_ST_CR_CERT_REQ_A;
330 s->init_num=0;
331
332 /* at this point we check that we have the
333 * required stuff from the server */
334 if (!ssl3_check_cert_and_algorithm(s))
335 {
336 ret= -1;
337 goto end;
338 }
339 break;
340
341 case SSL3_ST_CR_CERT_REQ_A:
342 case SSL3_ST_CR_CERT_REQ_B:
343 ret=ssl3_get_certificate_request(s);
344 if (ret <= 0) goto end;
345 s->state=SSL3_ST_CR_SRVR_DONE_A;
346 s->init_num=0;
347 break;
348
349 case SSL3_ST_CR_SRVR_DONE_A:
350 case SSL3_ST_CR_SRVR_DONE_B:
351 ret=ssl3_get_server_done(s);
352 if (ret <= 0) goto end;
353 if (s->s3->tmp.cert_req)
354 s->state=SSL3_ST_CW_CERT_A;
355 else
356 s->state=SSL3_ST_CW_KEY_EXCH_A;
357 s->init_num=0;
358
359 break;
360
361 case SSL3_ST_CW_CERT_A:
362 case SSL3_ST_CW_CERT_B:
363 case SSL3_ST_CW_CERT_C:
364 case SSL3_ST_CW_CERT_D:
365 ret=ssl3_send_client_certificate(s);
366 if (ret <= 0) goto end;
367 s->state=SSL3_ST_CW_KEY_EXCH_A;
368 s->init_num=0;
369 break;
370
371 case SSL3_ST_CW_KEY_EXCH_A:
372 case SSL3_ST_CW_KEY_EXCH_B:
373 ret=ssl3_send_client_key_exchange(s);
374 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700375 /* For TLS, cert_req is set to 2, so a cert chain
376 * of nothing is sent, but no verify packet is sent */
Adam Langley95c29f32014-06-20 12:00:00 -0700377 if (s->s3->tmp.cert_req == 1)
378 {
379 s->state=SSL3_ST_CW_CERT_VRFY_A;
380 }
381 else
382 {
383 s->state=SSL3_ST_CW_CHANGE_A;
384 s->s3->change_cipher_spec=0;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
387 s->init_num=0;
388 break;
389
390 case SSL3_ST_CW_CERT_VRFY_A:
391 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamin8da99062014-08-24 12:03:09 -0400392 ret=ssl3_send_cert_verify(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700393 if (ret <= 0) goto end;
394 s->state=SSL3_ST_CW_CHANGE_A;
395 s->init_num=0;
396 s->s3->change_cipher_spec=0;
397 break;
398
399 case SSL3_ST_CW_CHANGE_A:
400 case SSL3_ST_CW_CHANGE_B:
401 ret=ssl3_send_change_cipher_spec(s,
402 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
403 if (ret <= 0) goto end;
404
Adam Langley1258b6a2014-06-20 12:00:00 -0700405 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700406 if (s->s3->tlsext_channel_id_valid)
407 s->state=SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700408 if (s->s3->next_proto_neg_seen)
409 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700410 s->init_num=0;
411
412 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700413 if (!s->method->ssl3_enc->setup_key_block(s))
414 {
415 ret= -1;
416 goto end;
417 }
418
419 if (!s->method->ssl3_enc->change_cipher_state(s,
420 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
421 {
422 ret= -1;
423 goto end;
424 }
425
426 break;
427
Adam Langley95c29f32014-06-20 12:00:00 -0700428 case SSL3_ST_CW_NEXT_PROTO_A:
429 case SSL3_ST_CW_NEXT_PROTO_B:
430 ret=ssl3_send_next_proto(s);
431 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700432 if (s->s3->tlsext_channel_id_valid)
433 s->state=SSL3_ST_CW_CHANNEL_ID_A;
434 else
435 s->state=SSL3_ST_CW_FINISHED_A;
436 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700437
Adam Langley1258b6a2014-06-20 12:00:00 -0700438 case SSL3_ST_CW_CHANNEL_ID_A:
439 case SSL3_ST_CW_CHANNEL_ID_B:
440 ret=ssl3_send_channel_id(s);
441 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700442 s->state=SSL3_ST_CW_FINISHED_A;
443 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
445 case SSL3_ST_CW_FINISHED_A:
446 case SSL3_ST_CW_FINISHED_B:
447 ret=ssl3_send_finished(s,
448 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
449 s->method->ssl3_enc->client_finished_label,
450 s->method->ssl3_enc->client_finished_label_len);
451 if (ret <= 0) goto end;
452 s->state=SSL3_ST_CW_FLUSH;
453
Adam Langley95c29f32014-06-20 12:00:00 -0700454 if (s->hit)
455 {
456 s->s3->tmp.next_state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700457 }
458 else
459 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700460 /* This is a non-resumption handshake. If it
461 * involves ChannelID, then record the
462 * handshake hashes at this point in the
463 * session so that any resumption of this
464 * session with ChannelID can sign those
465 * hashes. */
466 if (s->s3->tlsext_channel_id_new)
467 {
468 ret = tls1_record_handshake_hashes_for_channel_id(s);
469 if (ret <= 0)
470 goto end;
471 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700472 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
473 && ssl3_can_cutthrough(s)
474 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
475 )
476 {
Adam Langley045cc552014-07-16 10:45:57 -0700477 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700478 }
Adam Langley95c29f32014-06-20 12:00:00 -0700479 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700480 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700481 /* Allow NewSessionTicket if ticket expected */
482 if (s->tlsext_ticket_expected)
483 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
484 else
David Benjamincb5abad2014-07-25 12:14:28 -0400485 s->s3->tmp.next_state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700486 }
Adam Langley95c29f32014-06-20 12:00:00 -0700487 }
488 s->init_num=0;
489 break;
490
Adam Langley95c29f32014-06-20 12:00:00 -0700491 case SSL3_ST_CR_SESSION_TICKET_A:
492 case SSL3_ST_CR_SESSION_TICKET_B:
493 ret=ssl3_get_new_session_ticket(s);
494 if (ret <= 0) goto end;
David Benjamincb5abad2014-07-25 12:14:28 -0400495 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700496 s->init_num=0;
497 break;
498
499 case SSL3_ST_CR_CERT_STATUS_A:
500 case SSL3_ST_CR_CERT_STATUS_B:
501 ret=ssl3_get_cert_status(s);
502 if (ret <= 0) goto end;
503 s->state=SSL3_ST_CR_KEY_EXCH_A;
504 s->init_num=0;
505 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamincb5abad2014-07-25 12:14:28 -0400507 case SSL3_ST_CR_CHANGE:
508 /* At this point, the next message must be entirely
509 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400510 if (!ssl3_expect_change_cipher_spec(s))
511 {
512 ret = -1;
513 goto end;
514 }
David Benjamincb5abad2014-07-25 12:14:28 -0400515 s->state = SSL3_ST_CR_FINISHED_A;
516 break;
517
Adam Langley95c29f32014-06-20 12:00:00 -0700518 case SSL3_ST_CR_FINISHED_A:
519 case SSL3_ST_CR_FINISHED_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700520 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
521 SSL3_ST_CR_FINISHED_B);
522 if (ret <= 0) goto end;
523
524 if (s->hit)
525 s->state=SSL3_ST_CW_CHANGE_A;
526 else
527 s->state=SSL_ST_OK;
528 s->init_num=0;
529 break;
530
531 case SSL3_ST_CW_FLUSH:
532 s->rwstate=SSL_WRITING;
533 if (BIO_flush(s->wbio) <= 0)
534 {
535 ret= -1;
536 goto end;
537 }
538 s->rwstate=SSL_NOTHING;
539 s->state=s->s3->tmp.next_state;
540 break;
541
Adam Langleyadb739e2014-06-20 12:00:00 -0700542 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700543 /* Allow NewSessionTicket if ticket expected */
544 if (s->tlsext_ticket_expected)
545 s->state=SSL3_ST_CR_SESSION_TICKET_A;
546 else
David Benjamin7e3305e2014-07-28 14:52:32 -0400547 s->state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700548
Adam Langley045cc552014-07-16 10:45:57 -0700549 ssl_free_wbio_buffer(s);
Adam Langleyadb739e2014-06-20 12:00:00 -0700550 ret = 1;
551 goto end;
552 /* break; */
553
Adam Langley95c29f32014-06-20 12:00:00 -0700554 case SSL_ST_OK:
555 /* clean a few things up */
556 ssl3_cleanup_key_block(s);
557
558 if (s->init_buf != NULL)
559 {
560 BUF_MEM_free(s->init_buf);
561 s->init_buf=NULL;
562 }
563
David Benjamine1b20a02014-11-05 01:28:53 -0500564 /* Remove write buffering now. */
565 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700566
567 s->init_num=0;
568 s->renegotiate=0;
569 s->new_session=0;
570
571 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
572 if (s->hit) s->ctx->stats.sess_hit++;
573
574 ret=1;
575 /* s->server=0; */
576 s->handshake_func=ssl3_connect;
577 s->ctx->stats.sess_connect_good++;
578
579 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
580
581 goto end;
582 /* break; */
583
584 default:
585 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
586 ret= -1;
587 goto end;
588 /* break; */
589 }
590
591 /* did we do anything */
592 if (!s->s3->tmp.reuse_message && !skip)
593 {
594 if (s->debug)
595 {
596 if ((ret=BIO_flush(s->wbio)) <= 0)
597 goto end;
598 }
599
600 if ((cb != NULL) && (s->state != state))
601 {
602 new_state=s->state;
603 s->state=state;
604 cb(s,SSL_CB_CONNECT_LOOP,1);
605 s->state=new_state;
606 }
607 }
608 skip=0;
609 }
610end:
611 s->in_handshake--;
612 if (buf != NULL)
613 BUF_MEM_free(buf);
614 if (cb != NULL)
615 cb(s,SSL_CB_CONNECT_EXIT,ret);
616 return(ret);
617 }
618
619
David Benjamin8da99062014-08-24 12:03:09 -0400620int ssl3_send_client_hello(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700621 {
622 unsigned char *buf;
623 unsigned char *p,*d;
624 int i;
625 unsigned long l;
626
627 buf=(unsigned char *)s->init_buf->data;
628 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
629 {
630 SSL_SESSION *sess = s->session;
David Benjamin407886f2014-07-21 22:23:50 -0400631 if (sess == NULL ||
632 sess->ssl_version != s->version ||
633 !sess->session_id_length ||
634 sess->not_resumable)
Adam Langley95c29f32014-06-20 12:00:00 -0700635 {
636 if (!ssl_get_new_session(s,0))
637 goto err;
638 }
639 if (s->method->version == DTLS_ANY_VERSION)
640 {
641 /* Determine which DTLS version to use */
642 int options = s->options;
643 /* If DTLS 1.2 disabled correct the version number */
644 if (options & SSL_OP_NO_DTLSv1_2)
645 {
Adam Langley95c29f32014-06-20 12:00:00 -0700646 /* Disabling all versions is silly: return an
647 * error.
648 */
649 if (options & SSL_OP_NO_DTLSv1)
650 {
David Benjamin8da99062014-08-24 12:03:09 -0400651 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
Adam Langley95c29f32014-06-20 12:00:00 -0700652 goto err;
653 }
654 /* Update method so we don't use any DTLS 1.2
655 * features.
656 */
657 s->method = DTLSv1_client_method();
658 s->version = DTLS1_VERSION;
659 }
660 else
661 {
662 /* We only support one version: update method */
663 if (options & SSL_OP_NO_DTLSv1)
664 s->method = DTLSv1_2_client_method();
665 s->version = DTLS1_2_VERSION;
666 }
667 s->client_version = s->version;
668 }
669 /* else use the pre-loaded session */
670
671 p=s->s3->client_random;
672
David Benjaminf2fedef2014-08-16 01:37:34 -0400673 /* If resending the ClientHello in DTLS after a
674 * HelloVerifyRequest, don't renegerate the client_random. The
675 * random must be reused. */
676 if (!SSL_IS_DTLS(s) || !s->d1->send_cookie)
Adam Langley95c29f32014-06-20 12:00:00 -0700677 {
Adam Langley95c29f32014-06-20 12:00:00 -0700678 ssl_fill_hello_random(s, 0, p,
679 sizeof(s->s3->client_random));
David Benjaminf2fedef2014-08-16 01:37:34 -0400680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyb0c235e2014-06-20 12:00:00 -0700682 /* Do the message type and length last.
683 * Note: the final argument to ssl_add_clienthello_tlsext below
684 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700685 d=p= ssl_handshake_start(s);
686
687 /* version indicates the negotiated version: for example from
688 * an SSLv2/v3 compatible client hello). The client_version
689 * field is the maximum version we permit and it is also
690 * used in RSA encrypted premaster secrets. Some servers can
691 * choke if we initially report a higher version then
692 * renegotiate to a lower one in the premaster secret. This
693 * didn't happen with TLS 1.0 as most servers supported it
694 * but it can with TLS 1.1 or later if the server only supports
695 * 1.0.
696 *
697 * Possible scenario with previous logic:
698 * 1. Client hello indicates TLS 1.2
699 * 2. Server hello says TLS 1.0
700 * 3. RSA encrypted premaster secret uses 1.2.
701 * 4. Handhaked proceeds using TLS 1.0.
702 * 5. Server sends hello request to renegotiate.
703 * 6. Client hello indicates TLS v1.0 as we now
704 * know that is maximum server supports.
705 * 7. Server chokes on RSA encrypted premaster secret
706 * containing version 1.0.
707 *
708 * For interoperability it should be OK to always use the
709 * maximum version we support in client hello and then rely
710 * on the checking of version to ensure the servers isn't
711 * being inconsistent: for example initially negotiating with
712 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
713 * client_version in client hello and not resetting it to
714 * the negotiated version.
715 */
716#if 0
717 *(p++)=s->version>>8;
718 *(p++)=s->version&0xff;
719 s->client_version=s->version;
720#else
721 *(p++)=s->client_version>>8;
722 *(p++)=s->client_version&0xff;
723#endif
724
725 /* Random stuff */
726 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
727 p+=SSL3_RANDOM_SIZE;
728
729 /* Session ID */
730 if (s->new_session)
731 i=0;
732 else
733 i=s->session->session_id_length;
734 *(p++)=i;
735 if (i != 0)
736 {
737 if (i > (int)sizeof(s->session->session_id))
738 {
David Benjamin8da99062014-08-24 12:03:09 -0400739 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700740 goto err;
741 }
742 memcpy(p,s->session->session_id,i);
743 p+=i;
744 }
745
746 /* cookie stuff for DTLS */
747 if (SSL_IS_DTLS(s))
748 {
749 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
750 {
David Benjamin8da99062014-08-24 12:03:09 -0400751 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700752 goto err;
753 }
754 *(p++) = s->d1->cookie_len;
755 memcpy(p, s->d1->cookie, s->d1->cookie_len);
756 p += s->d1->cookie_len;
757 }
758
759 /* Ciphers supported */
David Benjamin39482a12014-07-20 13:30:15 -0400760 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
Adam Langley95c29f32014-06-20 12:00:00 -0700761 if (i == 0)
762 {
David Benjamin8da99062014-08-24 12:03:09 -0400763 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
Adam Langley95c29f32014-06-20 12:00:00 -0700764 goto err;
765 }
Adam Langley95c29f32014-06-20 12:00:00 -0700766 s2n(i,p);
767 p+=i;
768
769 /* COMPRESSION */
770 *(p++)=1;
771 *(p++)=0; /* Add the NULL method */
772
Adam Langley95c29f32014-06-20 12:00:00 -0700773 /* TLS extensions*/
774 if (ssl_prepare_clienthello_tlsext(s) <= 0)
775 {
David Benjamin8da99062014-08-24 12:03:09 -0400776 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -0700777 goto err;
778 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700779 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700780 {
David Benjamin8da99062014-08-24 12:03:09 -0400781 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700782 goto err;
783 }
Adam Langley95c29f32014-06-20 12:00:00 -0700784
785 l= p-d;
786 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
787 s->state=SSL3_ST_CW_CLNT_HELLO_B;
788 }
789
790 /* SSL3_ST_CW_CLNT_HELLO_B */
791 return ssl_do_write(s);
792err:
793 return(-1);
794 }
795
796int ssl3_get_server_hello(SSL *s)
797 {
798 STACK_OF(SSL_CIPHER) *sk;
799 const SSL_CIPHER *c;
800 CERT *ct = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -0700801 int al=SSL_AD_INTERNAL_ERROR,ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700802 long n;
David Benjamina03d95d2014-07-12 19:49:07 -0400803 CBS server_hello, server_random, session_id;
David Benjamin39482a12014-07-20 13:30:15 -0400804 uint16_t server_version, cipher_suite;
David Benjamina03d95d2014-07-12 19:49:07 -0400805 uint8_t compression_method;
David Benjamin39ebf532014-08-31 02:23:49 -0400806 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700807
David Benjamin8b8c0062014-11-23 02:47:52 -0500808 /* DTLS_ANY_VERSION does not sniff the version ahead of time,
809 * so disable the version check. */
810 if (SSL_IS_DTLS(s))
811 s->first_packet = 1;
812
Adam Langley95c29f32014-06-20 12:00:00 -0700813 n=s->method->ssl_get_message(s,
814 SSL3_ST_CR_SRVR_HELLO_A,
815 SSL3_ST_CR_SRVR_HELLO_B,
David Benjaminf2fedef2014-08-16 01:37:34 -0400816 SSL3_MT_SERVER_HELLO,
Adam Langley95c29f32014-06-20 12:00:00 -0700817 20000, /* ?? */
David Benjamin590cbe92014-08-25 21:34:56 -0400818 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -0700819 &ok);
820
David Benjamin8b8c0062014-11-23 02:47:52 -0500821 if (SSL_IS_DTLS(s))
822 s->first_packet = 0;
823
Adam Langley95c29f32014-06-20 12:00:00 -0700824 if (!ok) return((int)n);
825
David Benjamina03d95d2014-07-12 19:49:07 -0400826 CBS_init(&server_hello, s->init_msg, n);
827
828 if (!CBS_get_u16(&server_hello, &server_version) ||
829 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
830 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
David Benjamin39482a12014-07-20 13:30:15 -0400831 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
832 !CBS_get_u16(&server_hello, &cipher_suite) ||
833 !CBS_get_u8(&server_hello, &compression_method))
David Benjamina03d95d2014-07-12 19:49:07 -0400834 {
835 al = SSL_AD_DECODE_ERROR;
836 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
837 goto f_err;
838 }
839
Adam Langley95c29f32014-06-20 12:00:00 -0700840 if (s->method->version == DTLS_ANY_VERSION)
841 {
842 /* Work out correct protocol version to use */
Adam Langley95c29f32014-06-20 12:00:00 -0700843 int options = s->options;
David Benjamina03d95d2014-07-12 19:49:07 -0400844 if (server_version == DTLS1_2_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700845 && !(options & SSL_OP_NO_DTLSv1_2))
846 s->method = DTLSv1_2_client_method();
David Benjamina03d95d2014-07-12 19:49:07 -0400847 else if (server_version == DTLS1_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700848 && !(options & SSL_OP_NO_DTLSv1))
849 s->method = DTLSv1_client_method();
850 else
851 {
852 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400853 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700854 al = SSL_AD_PROTOCOL_VERSION;
855 goto f_err;
856 }
857 s->version = s->client_version = s->method->version;
858 }
859
David Benjamina03d95d2014-07-12 19:49:07 -0400860 if (server_version != s->version)
Adam Langley95c29f32014-06-20 12:00:00 -0700861 {
862 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400863 s->version = (s->version & 0xff00) | (server_version & 0xff);
864 al = SSL_AD_PROTOCOL_VERSION;
Adam Langley95c29f32014-06-20 12:00:00 -0700865 goto f_err;
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
David Benjamina03d95d2014-07-12 19:49:07 -0400868 /* Copy over the server random. */
869 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700870
David Benjamind1681e62014-11-20 14:54:14 -0500871 if (CBS_len(&session_id) != 0 &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400872 CBS_mem_equal(&session_id,
873 s->session->session_id, s->session->session_id_length))
Adam Langley95c29f32014-06-20 12:00:00 -0700874 {
David Benjamina03d95d2014-07-12 19:49:07 -0400875 if(s->sid_ctx_length != s->session->sid_ctx_length
876 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
877 {
878 /* actually a client application bug */
879 al = SSL_AD_ILLEGAL_PARAMETER;
880 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
881 goto f_err;
882 }
David Benjamina03d95d2014-07-12 19:49:07 -0400883 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700884 }
David Benjamind1681e62014-11-20 14:54:14 -0500885 else
Adam Langley95c29f32014-06-20 12:00:00 -0700886 {
David Benjamind1681e62014-11-20 14:54:14 -0500887 /* The session wasn't resumed. */
888 s->hit = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700889 /* If we were trying for session-id reuse, make a new
890 * SSL_SESSION so we don't stuff up other people */
Adam Langley95c29f32014-06-20 12:00:00 -0700891 if (s->session->session_id_length > 0)
892 {
893 if (!ssl_get_new_session(s,0))
894 {
895 goto f_err;
896 }
897 }
David Benjamina03d95d2014-07-12 19:49:07 -0400898 /* Note: session_id could be empty. */
899 s->session->session_id_length = CBS_len(&session_id);
900 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
Adam Langley95c29f32014-06-20 12:00:00 -0700901 }
David Benjamina03d95d2014-07-12 19:49:07 -0400902
David Benjamin39482a12014-07-20 13:30:15 -0400903 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -0700904 if (c == NULL)
905 {
906 /* unknown cipher */
David Benjamina03d95d2014-07-12 19:49:07 -0400907 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700908 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
909 goto f_err;
910 }
David Benjamin39ebf532014-08-31 02:23:49 -0400911 /* ct->mask_ssl was computed from client capabilities. Now
912 * that the final version is known, compute a new mask_ssl. */
913 if (!SSL_USE_TLS1_2_CIPHERS(s))
914 mask_ssl = SSL_TLSV1_2;
915 else
916 mask_ssl = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700917 /* If it is a disabled cipher we didn't send it in client hello,
918 * so return an error.
919 */
David Benjamin39ebf532014-08-31 02:23:49 -0400920 if (c->algorithm_ssl & mask_ssl ||
Adam Langley95c29f32014-06-20 12:00:00 -0700921 c->algorithm_mkey & ct->mask_k ||
922 c->algorithm_auth & ct->mask_a)
923 {
924 al=SSL_AD_ILLEGAL_PARAMETER;
925 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
926 goto f_err;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
929 sk=ssl_get_ciphers_by_id(s);
930 if (!sk_SSL_CIPHER_find(sk, NULL, c))
931 {
932 /* we did not say we would use this cipher */
933 al=SSL_AD_ILLEGAL_PARAMETER;
934 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
935 goto f_err;
936 }
937
938 /* Depending on the session caching (internal/external), the cipher
939 and/or cipher_id values may not be set. Make sure that
940 cipher_id is set and use it for comparison. */
941 if (s->session->cipher)
942 s->session->cipher_id = s->session->cipher->id;
943 if (s->hit && (s->session->cipher_id != c->id))
944 {
David Benjamina03d95d2014-07-12 19:49:07 -0400945 al = SSL_AD_ILLEGAL_PARAMETER;
946 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
947 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700948 }
949 s->s3->tmp.new_cipher=c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500950
951 /* Most clients also require that the negotiated version match the
952 * session's version if resuming. However OpenSSL has historically not
953 * had the corresponding logic on the server, so this may not be
954 * compatible, depending on other factors. (Whether the ClientHello
955 * version is clamped to the session's version and whether the session
956 * cache is keyed on IP address.)
957 *
958 * TODO(davidben): See if we can still enforce this? Perhaps for the
959 * future TLS 1.3 and forward if this is fixed upstream. */
960
Adam Langley95c29f32014-06-20 12:00:00 -0700961 /* Don't digest cached records if no sigalgs: we may need them for
962 * client authentication.
963 */
Adam Langley75712922014-10-10 16:23:43 -0700964 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley95c29f32014-06-20 12:00:00 -0700965 goto f_err;
David Benjamina03d95d2014-07-12 19:49:07 -0400966
David Benjamina03d95d2014-07-12 19:49:07 -0400967 /* Only the NULL compression algorithm is supported. */
968 if (compression_method != 0)
969 {
970 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700971 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
972 goto f_err;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
David Benjamina03d95d2014-07-12 19:49:07 -0400975 /* TLS extensions */
976 if (!ssl_parse_serverhello_tlsext(s, &server_hello))
Adam Langley95c29f32014-06-20 12:00:00 -0700977 {
978 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
979 goto err;
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
David Benjamin03973092014-06-24 23:27:17 -0400982 /* There should be nothing left over in the record. */
David Benjamina03d95d2014-07-12 19:49:07 -0400983 if (CBS_len(&server_hello) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700984 {
985 /* wrong packet length */
986 al=SSL_AD_DECODE_ERROR;
987 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
988 goto f_err;
989 }
990
991 return(1);
992f_err:
993 ssl3_send_alert(s,SSL3_AL_FATAL,al);
994err:
995 return(-1);
996 }
997
998int ssl3_get_server_certificate(SSL *s)
999 {
1000 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001001 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001002 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001003 STACK_OF(X509) *sk=NULL;
1004 SESS_CERT *sc;
1005 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001006 CBS cbs, certificate_list;
1007 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
1009 n=s->method->ssl_get_message(s,
1010 SSL3_ST_CR_CERT_A,
1011 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001012 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001013 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001014 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001015 &ok);
1016
1017 if (!ok) return((int)n);
1018
David Benjamin51b1f742014-07-12 16:31:12 -04001019 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001020
1021 if ((sk=sk_X509_new_null()) == NULL)
1022 {
1023 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1024 goto err;
1025 }
1026
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001027 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1028 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001029 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001030 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001031 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1032 goto f_err;
1033 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001034
1035 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001036 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001037 CBS certificate;
1038 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001039 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001040 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001041 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1042 goto f_err;
1043 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001044 data = CBS_data(&certificate);
1045 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001046 if (x == NULL)
1047 {
1048 al=SSL_AD_BAD_CERTIFICATE;
1049 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1050 goto f_err;
1051 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001052 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001053 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001054 al = SSL_AD_INTERNAL_ERROR;
1055 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1056 goto f_err;
1057 }
1058 if (CBS_len(&certificate) != 0)
1059 {
1060 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001061 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1062 goto f_err;
1063 }
1064 if (!sk_X509_push(sk,x))
1065 {
1066 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1067 goto err;
1068 }
1069 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001070 }
1071
1072 i=ssl_verify_cert_chain(s,sk);
1073 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1074 )
1075 {
1076 al=ssl_verify_alarm_type(s->verify_result);
1077 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1078 goto f_err;
1079 }
1080 ERR_clear_error(); /* but we keep s->verify_result */
1081
1082 sc=ssl_sess_cert_new();
1083 if (sc == NULL) goto err;
1084
1085 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1086 s->session->sess_cert=sc;
1087
1088 sc->cert_chain=sk;
1089 /* Inconsistency alert: cert_chain does include the peer's
1090 * certificate, which we don't include in s3_srvr.c */
1091 x=sk_X509_value(sk,0);
1092 sk=NULL;
1093 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1094
1095 pkey=X509_get_pubkey(x);
1096
David Benjamind26aea62014-07-12 00:13:56 -04001097 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001098 {
1099 x=NULL;
1100 al=SSL3_AL_FATAL;
1101 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1102 goto f_err;
1103 }
1104
1105 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001106 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001107 {
1108 x=NULL;
1109 al=SSL3_AL_FATAL;
1110 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1111 goto f_err;
1112 }
1113
David Benjamind26aea62014-07-12 00:13:56 -04001114 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1115 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001116 {
David Benjamind26aea62014-07-12 00:13:56 -04001117 x=NULL;
1118 al=SSL_AD_ILLEGAL_PARAMETER;
1119 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1120 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001121 }
David Benjamind26aea62014-07-12 00:13:56 -04001122 sc->peer_cert_type=i;
David Benjamind26aea62014-07-12 00:13:56 -04001123 /* Why would the following ever happen?
1124 * We just created sc a couple of lines ago. */
1125 if (sc->peer_pkeys[i].x509 != NULL)
1126 X509_free(sc->peer_pkeys[i].x509);
David Benjamin150c6172014-08-05 22:22:49 -04001127 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1128 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001129
David Benjamind26aea62014-07-12 00:13:56 -04001130 if (s->session->peer != NULL)
1131 X509_free(s->session->peer);
David Benjamin150c6172014-08-05 22:22:49 -04001132 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001133
Adam Langley95c29f32014-06-20 12:00:00 -07001134 s->session->verify_result = s->verify_result;
1135
1136 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001137 ret=1;
1138 if (0)
1139 {
1140f_err:
1141 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1142 }
1143err:
1144 EVP_PKEY_free(pkey);
1145 X509_free(x);
1146 sk_X509_pop_free(sk,X509_free);
1147 return(ret);
1148 }
1149
David Benjamin8f8040d2014-07-14 19:14:46 -04001150int ssl3_get_server_key_exchange(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07001151 {
Adam Langley95c29f32014-06-20 12:00:00 -07001152 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001153 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001154 long n,alg_k,alg_a;
1155 EVP_PKEY *pkey=NULL;
1156 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001157 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001158 DH *dh=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001159 EC_KEY *ecdh = NULL;
1160 BN_CTX *bn_ctx = NULL;
1161 EC_POINT *srvr_ecpoint = NULL;
David Benjamined439582014-07-14 19:13:02 -04001162 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001163
1164 /* use same message size as in ssl3_get_certificate_request()
1165 * as ServerKeyExchange message may be skipped */
1166 n=s->method->ssl_get_message(s,
1167 SSL3_ST_CR_KEY_EXCH_A,
1168 SSL3_ST_CR_KEY_EXCH_B,
1169 -1,
1170 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001171 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001172 &ok);
1173 if (!ok) return((int)n);
1174
1175 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1176 {
David Benjamin9c651c92014-07-12 13:27:45 -04001177 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1178 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin9c651c92014-07-12 13:27:45 -04001180 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1181 return -1;
1182 }
1183
Adam Langley95c29f32014-06-20 12:00:00 -07001184 /* In plain PSK ciphersuite, ServerKeyExchange can be
1185 omitted if no identity hint is sent. Set
1186 session->sess_cert anyway to avoid problems
1187 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001188 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001189 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001190 /* PSK ciphersuites that also send a
1191 * Certificate would have already initialized
1192 * |sess_cert|. */
1193 if (s->session->sess_cert == NULL)
1194 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin688d8df2014-11-02 23:06:42 -05001195
1196 /* TODO(davidben): This should be reset in one place
1197 * with the rest of the handshake state. */
1198 if (s->s3->tmp.peer_psk_identity_hint)
Adam Langley0289c732014-06-20 12:00:00 -07001199 {
David Benjamin688d8df2014-11-02 23:06:42 -05001200 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1201 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley0289c732014-06-20 12:00:00 -07001202 }
Adam Langley95c29f32014-06-20 12:00:00 -07001203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204 s->s3->tmp.reuse_message=1;
1205 return(1);
1206 }
1207
David Benjamined439582014-07-14 19:13:02 -04001208 /* Retain a copy of the original CBS to compute the signature
1209 * over. */
1210 CBS_init(&server_key_exchange, s->init_msg, n);
1211 server_key_exchange_orig = server_key_exchange;
1212
Adam Langley95c29f32014-06-20 12:00:00 -07001213 if (s->session->sess_cert != NULL)
1214 {
Adam Langley95c29f32014-06-20 12:00:00 -07001215 if (s->session->sess_cert->peer_dh_tmp)
1216 {
1217 DH_free(s->session->sess_cert->peer_dh_tmp);
1218 s->session->sess_cert->peer_dh_tmp=NULL;
1219 }
Adam Langley95c29f32014-06-20 12:00:00 -07001220 if (s->session->sess_cert->peer_ecdh_tmp)
1221 {
1222 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1223 s->session->sess_cert->peer_ecdh_tmp=NULL;
1224 }
Adam Langley95c29f32014-06-20 12:00:00 -07001225 }
1226 else
1227 {
1228 s->session->sess_cert=ssl_sess_cert_new();
1229 }
1230
Adam Langley95c29f32014-06-20 12:00:00 -07001231 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1232 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1233 EVP_MD_CTX_init(&md_ctx);
1234
Adam Langleyc26c8022014-06-20 12:00:00 -07001235 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001236 {
David Benjamined439582014-07-14 19:13:02 -04001237 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001238
David Benjamined439582014-07-14 19:13:02 -04001239 /* Each of the PSK key exchanges begins with a
1240 * psk_identity_hint. */
1241 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001242 {
David Benjamined439582014-07-14 19:13:02 -04001243 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001244 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001245 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001246 }
Adam Langley95c29f32014-06-20 12:00:00 -07001247
David Benjamined439582014-07-14 19:13:02 -04001248 /* Store PSK identity hint for later use, hint is used in
1249 * ssl3_send_client_key_exchange. Assume that the maximum
1250 * length of a PSK identity hint can be as long as the maximum
1251 * length of a PSK identity. Also do not allow NULL
1252 * characters; identities are saved as C strings.
1253 *
1254 * TODO(davidben): Should invalid hints be ignored? It's a hint
1255 * rather than a specific identity. */
1256 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1257 CBS_contains_zero_byte(&psk_identity_hint))
1258 {
1259 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001260 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamined439582014-07-14 19:13:02 -04001261 goto f_err;
1262 }
1263
1264 /* Save the identity hint as a C string. */
David Benjamin688d8df2014-11-02 23:06:42 -05001265 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint))
David Benjamined439582014-07-14 19:13:02 -04001266 {
David Benjamin688d8df2014-11-02 23:06:42 -05001267 al = SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001268 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001269 goto f_err;
1270 }
Adam Langley95c29f32014-06-20 12:00:00 -07001271 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001272
David Benjamin525a0fe2014-11-08 11:41:12 -05001273 if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001274 {
David Benjamined439582014-07-14 19:13:02 -04001275 CBS dh_p, dh_g, dh_Ys;
1276
1277 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1278 CBS_len(&dh_p) == 0 ||
1279 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1280 CBS_len(&dh_g) == 0 ||
1281 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1282 CBS_len(&dh_Ys) == 0)
1283 {
1284 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001285 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001286 goto f_err;
1287 }
1288
Adam Langley95c29f32014-06-20 12:00:00 -07001289 if ((dh=DH_new()) == NULL)
1290 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001291 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001292 goto err;
1293 }
Adam Langley95c29f32014-06-20 12:00:00 -07001294
David Benjamined439582014-07-14 19:13:02 -04001295 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001296 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001297 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001298 goto err;
1299 }
David Benjamined439582014-07-14 19:13:02 -04001300 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001301 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001302 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001303 goto err;
1304 }
David Benjamined439582014-07-14 19:13:02 -04001305 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1306 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001307 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001308 goto err;
1309 }
Adam Langley95c29f32014-06-20 12:00:00 -07001310
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001311 if (DH_size(dh) < 512/8)
1312 {
1313 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1314 goto err;
1315 }
1316
Adam Langley95c29f32014-06-20 12:00:00 -07001317 if (alg_a & SSL_aRSA)
1318 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001319 /* else anonymous DH, so no certificate or pkey. */
1320
1321 s->session->sess_cert->peer_dh_tmp=dh;
1322 dh=NULL;
1323 }
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langley95c29f32014-06-20 12:00:00 -07001325 else if (alg_k & SSL_kEECDH)
1326 {
David Benjamined439582014-07-14 19:13:02 -04001327 uint16_t curve_id;
1328 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001329 EC_GROUP *ngroup;
1330 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001331 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001332
David Benjamined439582014-07-14 19:13:02 -04001333 /* Extract elliptic curve parameters and the server's
1334 * ephemeral ECDH public key. Check curve is one of
1335 * our preferences, if not server has sent an invalid
1336 * curve.
1337 */
1338 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001339 {
David Benjamined439582014-07-14 19:13:02 -04001340 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001341 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
Adam Langley95c29f32014-06-20 12:00:00 -07001342 goto f_err;
1343 }
1344
David Benjamined439582014-07-14 19:13:02 -04001345 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001346 {
1347 al=SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001348 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley95c29f32014-06-20 12:00:00 -07001349 goto f_err;
1350 }
1351
David Benjamined439582014-07-14 19:13:02 -04001352 if ((ecdh=EC_KEY_new()) == NULL)
1353 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001354 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001355 goto err;
1356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1358 if (ngroup == NULL)
1359 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001360 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001361 goto err;
1362 }
1363 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1364 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001365 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001366 goto err;
1367 }
1368 EC_GROUP_free(ngroup);
1369
1370 group = EC_KEY_get0_group(ecdh);
1371
Adam Langley95c29f32014-06-20 12:00:00 -07001372 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001373 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1374 {
1375 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001376 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001377 goto f_err;
1378 }
1379
Adam Langley95c29f32014-06-20 12:00:00 -07001380 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1381 ((bn_ctx = BN_CTX_new()) == NULL))
1382 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001383 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001384 goto err;
1385 }
1386
David Benjamined439582014-07-14 19:13:02 -04001387 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1388 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001389 {
David Benjamined439582014-07-14 19:13:02 -04001390 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001391 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
Adam Langley95c29f32014-06-20 12:00:00 -07001392 goto f_err;
1393 }
1394
Adam Langley95c29f32014-06-20 12:00:00 -07001395 /* The ECC/TLS specification does not mention
1396 * the use of DSA to sign ECParameters in the server
1397 * key exchange message. We do support RSA and ECDSA.
1398 */
1399 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001400 else if (alg_a & SSL_aRSA)
1401 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001402 else if (alg_a & SSL_aECDSA)
1403 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001404 /* else anonymous ECDH, so no certificate or pkey. */
1405 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1406 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1407 ecdh=NULL;
1408 BN_CTX_free(bn_ctx);
1409 bn_ctx = NULL;
1410 EC_POINT_free(srvr_ecpoint);
1411 srvr_ecpoint = NULL;
1412 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001413
1414 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001415 {
1416 al=SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001417 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001418 goto f_err;
1419 }
Adam Langley95c29f32014-06-20 12:00:00 -07001420
David Benjamined439582014-07-14 19:13:02 -04001421 /* At this point, |server_key_exchange| contains the
1422 * signature, if any, while |server_key_exchange_orig|
1423 * contains the entire message. From that, derive a CBS
1424 * containing just the parameter. */
1425 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1426 CBS_len(&server_key_exchange_orig) -
1427 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001428
1429 /* if it was signed, check the signature */
1430 if (pkey != NULL)
1431 {
David Benjamined439582014-07-14 19:13:02 -04001432 CBS signature;
1433
Adam Langley95c29f32014-06-20 12:00:00 -07001434 if (SSL_USE_SIGALGS(s))
1435 {
David Benjamin05da6e12014-07-12 20:42:55 -04001436 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
David Benjamined439582014-07-14 19:13:02 -04001437 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001438 }
1439 else
1440 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001441
David Benjamined439582014-07-14 19:13:02 -04001442 /* The last field in |server_key_exchange| is the
1443 * signature. */
1444 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1445 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001446 {
David Benjamined439582014-07-14 19:13:02 -04001447 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001448 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001449 goto f_err;
1450 }
1451
Adam Langley95c29f32014-06-20 12:00:00 -07001452 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1453 {
1454 int num;
David Benjamined439582014-07-14 19:13:02 -04001455 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1456 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley95c29f32014-06-20 12:00:00 -07001458 q=md_buf;
1459 for (num=2; num > 0; num--)
1460 {
David Benjamined439582014-07-14 19:13:02 -04001461 unsigned int digest_len;
David Benjamine7127782014-08-18 23:15:29 -04001462 EVP_DigestInit_ex(&md_ctx,
1463 (num == 2) ? EVP_md5() : EVP_sha1(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001464 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1465 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001466 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1467 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1468 q += digest_len;
1469 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001470 }
David Benjamined439582014-07-14 19:13:02 -04001471 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1472 CBS_data(&signature), CBS_len(&signature),
1473 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001474 {
David Benjamined439582014-07-14 19:13:02 -04001475 al = SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001476 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001477 goto f_err;
1478 }
1479 }
1480 else
Adam Langley95c29f32014-06-20 12:00:00 -07001481 {
David Benjaminfd617a52014-10-31 00:21:46 -04001482 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1483 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random, SSL3_RANDOM_SIZE) ||
1484 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random, SSL3_RANDOM_SIZE) ||
1485 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter)) ||
1486 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature)))
Adam Langley95c29f32014-06-20 12:00:00 -07001487 {
1488 /* bad signature */
1489 al=SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001490 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001491 goto f_err;
1492 }
1493 }
1494 }
1495 else
1496 {
David Benjamine8f3d662014-07-12 01:10:19 -04001497 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001498 {
1499 /* Might be wrong key type, check it */
1500 if (ssl3_check_cert_and_algorithm(s))
1501 /* Otherwise this shouldn't happen */
David Benjamin8f8040d2014-07-14 19:14:46 -04001502 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001503 goto err;
1504 }
1505 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001506 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001507 {
1508 al=SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001509 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001510 goto f_err;
1511 }
1512 }
1513 EVP_PKEY_free(pkey);
1514 EVP_MD_CTX_cleanup(&md_ctx);
1515 return(1);
1516f_err:
1517 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1518err:
1519 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001520 if (rsa != NULL)
1521 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001522 if (dh != NULL)
1523 DH_free(dh);
Adam Langley95c29f32014-06-20 12:00:00 -07001524 BN_CTX_free(bn_ctx);
1525 EC_POINT_free(srvr_ecpoint);
1526 if (ecdh != NULL)
1527 EC_KEY_free(ecdh);
Adam Langley95c29f32014-06-20 12:00:00 -07001528 EVP_MD_CTX_cleanup(&md_ctx);
1529 return(-1);
1530 }
1531
1532static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1533 {
1534 return(X509_NAME_cmp(*a,*b));
1535 }
1536
1537int ssl3_get_certificate_request(SSL *s)
1538 {
1539 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001540 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001541 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001542 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001543 CBS cbs;
1544 CBS certificate_types;
1545 CBS certificate_authorities;
1546 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001547
1548 n=s->method->ssl_get_message(s,
1549 SSL3_ST_CR_CERT_REQ_A,
1550 SSL3_ST_CR_CERT_REQ_B,
1551 -1,
1552 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001553 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001554 &ok);
1555
1556 if (!ok) return((int)n);
1557
1558 s->s3->tmp.cert_req=0;
1559
1560 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1561 {
1562 s->s3->tmp.reuse_message=1;
1563 /* If we get here we don't need any cached handshake records
1564 * as we wont be doing client auth.
1565 */
1566 if (s->s3->handshake_buffer)
1567 {
Adam Langley75712922014-10-10 16:23:43 -07001568 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley95c29f32014-06-20 12:00:00 -07001569 goto err;
1570 }
1571 return(1);
1572 }
1573
1574 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1575 {
1576 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1577 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1578 goto err;
1579 }
1580
1581 /* TLS does not like anon-DH with client cert */
1582 if (s->version > SSL3_VERSION)
1583 {
1584 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1585 {
1586 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1587 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1588 goto err;
1589 }
1590 }
1591
David Benjamin51b1f742014-07-12 16:31:12 -04001592 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001593
1594 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1595 if (ca_sk == NULL)
1596 {
1597 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1598 goto err;
1599 }
1600
1601 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001602 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001603 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001604 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1605 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1606 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001607 }
David Benjamin676d1e72014-07-08 14:34:10 -04001608 if (!CBS_stow(&certificate_types,
1609 &s->s3->tmp.certificate_types,
1610 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001611 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001612 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1613 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001614 }
Adam Langley95c29f32014-06-20 12:00:00 -07001615 if (SSL_USE_SIGALGS(s))
1616 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001617 CBS supported_signature_algorithms;
1618 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001619 {
1620 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001621 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001622 goto err;
1623 }
David Benjamincd996942014-07-20 16:23:51 -04001624 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001625 {
1626 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1627 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1628 goto err;
1629 }
Adam Langley95c29f32014-06-20 12:00:00 -07001630 }
1631
1632 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001633 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001634 {
1635 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1636 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1637 goto err;
1638 }
1639
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001640 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001641 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001642 CBS distinguished_name;
1643 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001644 {
Adam Langley95c29f32014-06-20 12:00:00 -07001645 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1646 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1647 goto err;
1648 }
1649
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001650 data = CBS_data(&distinguished_name);
1651 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001652 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001653 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1654 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1655 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001656 }
1657
David Benjamin407a10c2014-07-16 12:58:59 -04001658 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001659 {
1660 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1661 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1662 goto err;
1663 }
1664 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001665 {
1666 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1667 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1668 goto err;
1669 }
1670 if (!sk_X509_NAME_push(ca_sk,xn))
1671 {
1672 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1673 goto err;
1674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675 }
1676
Adam Langley95c29f32014-06-20 12:00:00 -07001677 /* we should setup a certificate to return.... */
1678 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001679 if (s->s3->tmp.ca_names != NULL)
1680 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1681 s->s3->tmp.ca_names=ca_sk;
1682 ca_sk=NULL;
1683
1684 ret=1;
1685err:
1686 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1687 return(ret);
1688 }
1689
Adam Langley95c29f32014-06-20 12:00:00 -07001690int ssl3_get_new_session_ticket(SSL *s)
1691 {
David Benjamine044c3d2014-07-14 19:14:12 -04001692 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001693 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001694 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001695
1696 n=s->method->ssl_get_message(s,
1697 SSL3_ST_CR_SESSION_TICKET_A,
1698 SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin64442872014-07-21 17:43:45 -04001699 SSL3_MT_NEWSESSION_TICKET,
Adam Langley95c29f32014-06-20 12:00:00 -07001700 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001701 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001702 &ok);
1703
1704 if (!ok)
1705 return((int)n);
1706
David Benjamine044c3d2014-07-14 19:14:12 -04001707 CBS_init(&new_session_ticket, s->init_msg, n);
1708
1709 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1710 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1711 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001712 {
Adam Langley95c29f32014-06-20 12:00:00 -07001713 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001714 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001715 goto f_err;
1716 }
1717
David Benjamine044c3d2014-07-14 19:14:12 -04001718 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001719 {
1720 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1721 goto err;
1722 }
David Benjamine044c3d2014-07-14 19:14:12 -04001723
Adam Langley95c29f32014-06-20 12:00:00 -07001724 /* There are two ways to detect a resumed ticket sesion.
1725 * One is to set an appropriate session ID and then the server
1726 * must return a match in ServerHello. This allows the normal
1727 * client session ID matching to work and we know much
1728 * earlier that the ticket has been accepted.
1729 *
1730 * The other way is to set zero length session ID when the
1731 * ticket is presented and rely on the handshake to determine
1732 * session resumption.
1733 *
1734 * We choose the former approach because this fits in with
1735 * assumptions elsewhere in OpenSSL. The session ID is set
1736 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1737 * ticket.
1738 */
David Benjamine044c3d2014-07-14 19:14:12 -04001739 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001740 s->session->session_id, &s->session->session_id_length,
Adam Langley95c29f32014-06-20 12:00:00 -07001741 EVP_sha256(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001742 ret=1;
1743 return(ret);
1744f_err:
1745 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1746err:
1747 return(-1);
1748 }
1749
1750int ssl3_get_cert_status(SSL *s)
1751 {
1752 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001753 long n;
1754 CBS certificate_status, ocsp_response;
1755 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001756
1757 n=s->method->ssl_get_message(s,
1758 SSL3_ST_CR_CERT_STATUS_A,
1759 SSL3_ST_CR_CERT_STATUS_B,
1760 SSL3_MT_CERTIFICATE_STATUS,
1761 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001762 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001763 &ok);
1764
1765 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001766
1767 CBS_init(&certificate_status, s->init_msg, n);
1768 if (!CBS_get_u8(&certificate_status, &status_type) ||
1769 status_type != TLSEXT_STATUSTYPE_ocsp ||
1770 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1771 CBS_len(&ocsp_response) == 0 ||
1772 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001773 {
1774 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04001775 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001776 goto f_err;
1777 }
David Benjamin46062682014-07-14 19:14:32 -04001778
David Benjamin6c7aed02014-08-27 16:42:38 -04001779 if (!CBS_stow(&ocsp_response,
1780 &s->session->ocsp_response, &s->session->ocsp_response_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001781 {
1782 al = SSL_AD_INTERNAL_ERROR;
1783 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1784 goto f_err;
1785 }
Adam Langley95c29f32014-06-20 12:00:00 -07001786 return 1;
1787f_err:
1788 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1789 return(-1);
1790 }
Adam Langley95c29f32014-06-20 12:00:00 -07001791
1792int ssl3_get_server_done(SSL *s)
1793 {
1794 int ok,ret=0;
1795 long n;
1796
1797 n=s->method->ssl_get_message(s,
1798 SSL3_ST_CR_SRVR_DONE_A,
1799 SSL3_ST_CR_SRVR_DONE_B,
1800 SSL3_MT_SERVER_DONE,
1801 30, /* should be very small, like 0 :-) */
David Benjamin590cbe92014-08-25 21:34:56 -04001802 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001803 &ok);
1804
1805 if (!ok) return((int)n);
1806 if (n > 0)
1807 {
1808 /* should contain no data */
1809 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1810 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1811 return -1;
1812 }
1813 ret=1;
1814 return(ret);
1815 }
1816
1817
1818int ssl3_send_client_key_exchange(SSL *s)
1819 {
1820 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07001821 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001822 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001823 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001824 unsigned char *q;
1825 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001826 EC_KEY *clnt_ecdh = NULL;
1827 const EC_POINT *srvr_ecpoint = NULL;
1828 EVP_PKEY *srvr_pub_pkey = NULL;
1829 unsigned char *encodedPoint = NULL;
1830 int encoded_pt_len = 0;
1831 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001832 unsigned int psk_len = 0;
1833 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langley75712922014-10-10 16:23:43 -07001834 uint8_t *pms = NULL;
1835 size_t pms_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001836
1837 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1838 {
1839 p = ssl_handshake_start(s);
1840
1841 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07001842 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1843
David Benjamin93d67d32014-10-28 00:53:38 -04001844 /* If using a PSK key exchange, prepare the pre-shared key. */
Adam Langleyc26c8022014-06-20 12:00:00 -07001845 if (alg_a & SSL_aPSK)
1846 {
Adam Langley01797e32014-06-20 12:00:00 -07001847 char identity[PSK_MAX_IDENTITY_LEN + 1];
1848 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001849
Adam Langleyc26c8022014-06-20 12:00:00 -07001850 if (s->psk_client_callback == NULL)
1851 {
1852 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
1853 goto err;
1854 }
1855
Adam Langley01797e32014-06-20 12:00:00 -07001856 memset(identity, 0, sizeof(identity));
David Benjamin688d8df2014-11-02 23:06:42 -05001857 psk_len = s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07001858 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07001859 if (psk_len > PSK_MAX_PSK_LEN)
1860 {
1861 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin93d67d32014-10-28 00:53:38 -04001862 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001863 }
1864 else if (psk_len == 0)
1865 {
1866 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamin93d67d32014-10-28 00:53:38 -04001867 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1868 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001869 }
Adam Langley01797e32014-06-20 12:00:00 -07001870 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1871 if (identity_len > PSK_MAX_IDENTITY_LEN)
1872 {
1873 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin93d67d32014-10-28 00:53:38 -04001874 goto err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001875 }
1876
Adam Langleyc26c8022014-06-20 12:00:00 -07001877 if (s->session->psk_identity != NULL)
1878 OPENSSL_free(s->session->psk_identity);
1879 s->session->psk_identity = BUF_strdup(identity);
1880 if (s->session->psk_identity == NULL)
1881 {
1882 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langleyc26c8022014-06-20 12:00:00 -07001883 goto err;
1884 }
David Benjamin93d67d32014-10-28 00:53:38 -04001885
1886 /* Write out psk_identity. */
1887 s2n(identity_len, p);
1888 memcpy(p, identity, identity_len);
1889 p += identity_len;
1890 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001891 }
Adam Langley95c29f32014-06-20 12:00:00 -07001892
David Benjamin93d67d32014-10-28 00:53:38 -04001893 /* Depending on the key exchange method, compute |pms|
1894 * and |pms_len|. */
David Benjamin457112e2014-08-27 14:35:09 -04001895 if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001896 {
1897 RSA *rsa;
David Benjamin93d67d32014-10-28 00:53:38 -04001898 size_t enc_pms_len;
Adam Langley75712922014-10-10 16:23:43 -07001899
1900 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1901 pms = OPENSSL_malloc(pms_len);
1902 if (pms == NULL)
1903 {
1904 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
1905 goto err;
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Emilia Käsperc3d79602014-07-24 22:15:29 +02001908 if (s->session->sess_cert == NULL)
1909 {
1910 /* We should always have a server certificate with SSL_kRSA. */
1911 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1912 goto err;
1913 }
1914
David Benjamin525a0fe2014-11-08 11:41:12 -05001915 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1916 if ((pkey == NULL) ||
1917 (pkey->type != EVP_PKEY_RSA) ||
1918 (pkey->pkey.rsa == NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07001919 {
David Benjamin525a0fe2014-11-08 11:41:12 -05001920 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin1df11242014-11-08 11:41:13 -05001921 if (pkey != NULL)
1922 EVP_PKEY_free(pkey);
David Benjamin525a0fe2014-11-08 11:41:12 -05001923 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001924 }
David Benjamin525a0fe2014-11-08 11:41:12 -05001925 rsa=pkey->pkey.rsa;
1926 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langley75712922014-10-10 16:23:43 -07001928 pms[0]=s->client_version>>8;
1929 pms[1]=s->client_version&0xff;
1930 if (RAND_bytes(&pms[2],SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001931 goto err;
1932
Adam Langley75712922014-10-10 16:23:43 -07001933 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -07001934
1935 q=p;
David Benjamin93d67d32014-10-28 00:53:38 -04001936 /* In TLS and beyond, reserve space for the length prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -07001937 if (s->version > SSL3_VERSION)
David Benjamin93d67d32014-10-28 00:53:38 -04001938 {
1939 p += 2;
1940 n += 2;
1941 }
1942 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa),
1943 pms, pms_len, RSA_PKCS1_PADDING))
Adam Langley95c29f32014-06-20 12:00:00 -07001944 {
1945 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
1946 goto err;
1947 }
David Benjamin93d67d32014-10-28 00:53:38 -04001948 n += enc_pms_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001949
David Benjamin859ec3c2014-09-02 16:29:36 -04001950 /* Log the premaster secret, if logging is enabled. */
1951 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx,
David Benjamin93d67d32014-10-28 00:53:38 -04001952 p, enc_pms_len, pms, pms_len))
David Benjamin859ec3c2014-09-02 16:29:36 -04001953 {
1954 goto err;
1955 }
1956
David Benjamin93d67d32014-10-28 00:53:38 -04001957 /* Fill in the length prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -07001958 if (s->version > SSL3_VERSION)
1959 {
David Benjamin93d67d32014-10-28 00:53:38 -04001960 s2n(enc_pms_len, q);
Adam Langley95c29f32014-06-20 12:00:00 -07001961 }
Adam Langley95c29f32014-06-20 12:00:00 -07001962 }
David Benjamin0da0e182014-08-19 16:20:28 -04001963 else if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001964 {
David Benjamin93d67d32014-10-28 00:53:38 -04001965 DH *dh_srvr, *dh_clnt;
Adam Langley95c29f32014-06-20 12:00:00 -07001966 SESS_CERT *scert = s->session->sess_cert;
David Benjamin93d67d32014-10-28 00:53:38 -04001967 int dh_len;
1968 size_t pub_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001969
1970 if (scert == NULL)
1971 {
1972 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1973 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1974 goto err;
1975 }
1976
David Benjamin0da0e182014-08-19 16:20:28 -04001977 if (scert->peer_dh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001978 {
David Benjamin0da0e182014-08-19 16:20:28 -04001979 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1980 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001981 }
David Benjamin0da0e182014-08-19 16:20:28 -04001982 dh_srvr=scert->peer_dh_tmp;
1983
David Benjaminbd30f8e2014-08-19 16:02:38 -04001984 /* generate a new random key */
1985 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001986 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04001987 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1988 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001989 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001990 if (!DH_generate_key(dh_clnt))
Adam Langley95c29f32014-06-20 12:00:00 -07001991 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04001992 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1993 DH_free(dh_clnt);
1994 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001995 }
1996
Adam Langley75712922014-10-10 16:23:43 -07001997 pms_len = DH_size(dh_clnt);
1998 pms = OPENSSL_malloc(pms_len);
1999 if (pms == NULL)
2000 {
2001 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2002 DH_free(dh_clnt);
2003 goto err;
2004 }
Adam Langley95c29f32014-06-20 12:00:00 -07002005
David Benjamin93d67d32014-10-28 00:53:38 -04002006 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2007 if (dh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002008 {
2009 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2010 DH_free(dh_clnt);
2011 goto err;
2012 }
David Benjamin93d67d32014-10-28 00:53:38 -04002013 pms_len = dh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002014
David Benjaminbd30f8e2014-08-19 16:02:38 -04002015 /* send off the data */
David Benjamin93d67d32014-10-28 00:53:38 -04002016 pub_len = BN_num_bytes(dh_clnt->pub_key);
2017 s2n(pub_len, p);
2018 BN_bn2bin(dh_clnt->pub_key, p);
2019 n += 2 + pub_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002020
2021 DH_free(dh_clnt);
Adam Langley95c29f32014-06-20 12:00:00 -07002022 }
Adam Langley95c29f32014-06-20 12:00:00 -07002023
David Benjamin0da0e182014-08-19 16:20:28 -04002024 else if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002025 {
2026 const EC_GROUP *srvr_group = NULL;
2027 EC_KEY *tkey;
David Benjamin93d67d32014-10-28 00:53:38 -04002028 int field_size = 0, ecdh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002029
Adam Langleyd06eddd2014-06-20 12:00:00 -07002030 if (s->session->sess_cert == NULL)
2031 {
2032 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2033 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2034 goto err;
2035 }
2036
David Benjamin0da0e182014-08-19 16:20:28 -04002037 if (s->session->sess_cert->peer_ecdh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002038 {
David Benjamin0da0e182014-08-19 16:20:28 -04002039 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2040 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002041 }
David Benjamin0da0e182014-08-19 16:20:28 -04002042 tkey = s->session->sess_cert->peer_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002043
2044 srvr_group = EC_KEY_get0_group(tkey);
2045 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2046
2047 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2048 {
2049 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2050 goto err;
2051 }
2052
2053 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2054 {
2055 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2056 goto err;
2057 }
2058
2059 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2060 {
2061 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2062 goto err;
2063 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002064 /* Generate a new ECDH key pair */
2065 if (!(EC_KEY_generate_key(clnt_ecdh)))
Adam Langley95c29f32014-06-20 12:00:00 -07002066 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002067 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2068 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002069 }
2070
Adam Langley95c29f32014-06-20 12:00:00 -07002071 field_size = EC_GROUP_get_degree(srvr_group);
2072 if (field_size <= 0)
2073 {
2074 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2075 goto err;
2076 }
David Benjamin93d67d32014-10-28 00:53:38 -04002077
2078 pms_len = (field_size + 7) / 8;
2079 pms = OPENSSL_malloc(pms_len);
2080 if (pms == NULL)
2081 {
2082 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2083 goto err;
2084 }
2085
2086 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
2087 if (ecdh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002088 {
2089 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2090 goto err;
2091 }
David Benjamin93d67d32014-10-28 00:53:38 -04002092 pms_len = ecdh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002093
David Benjaminbd30f8e2014-08-19 16:02:38 -04002094 /* First check the size of encoding and
2095 * allocate memory accordingly.
2096 */
2097 encoded_pt_len =
2098 EC_POINT_point2oct(srvr_group,
Adam Langley95c29f32014-06-20 12:00:00 -07002099 EC_KEY_get0_public_key(clnt_ecdh),
2100 POINT_CONVERSION_UNCOMPRESSED,
2101 NULL, 0, NULL);
2102
David Benjaminbd30f8e2014-08-19 16:02:38 -04002103 encodedPoint = (unsigned char *)
2104 OPENSSL_malloc(encoded_pt_len *
Adam Langley95c29f32014-06-20 12:00:00 -07002105 sizeof(unsigned char));
David Benjaminbd30f8e2014-08-19 16:02:38 -04002106 bn_ctx = BN_CTX_new();
2107 if ((encodedPoint == NULL) ||
2108 (bn_ctx == NULL))
2109 {
2110 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2111 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002112 }
2113
David Benjaminbd30f8e2014-08-19 16:02:38 -04002114 /* Encode the public key */
2115 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2116 EC_KEY_get0_public_key(clnt_ecdh),
2117 POINT_CONVERSION_UNCOMPRESSED,
2118 encodedPoint, encoded_pt_len, bn_ctx);
2119
David Benjaminbd30f8e2014-08-19 16:02:38 -04002120 *p = encoded_pt_len; /* length of encoded point */
2121 /* Encoded point will be copied here */
2122 p += 1;
2123 n += 1;
2124 /* copy the point */
David Benjamin93d67d32014-10-28 00:53:38 -04002125 memcpy(p, encodedPoint, encoded_pt_len);
David Benjaminbd30f8e2014-08-19 16:02:38 -04002126 /* increment n to account for length field */
2127 n += encoded_pt_len;
2128
Adam Langley95c29f32014-06-20 12:00:00 -07002129 /* Free allocated memory */
2130 BN_CTX_free(bn_ctx);
David Benjamin93d67d32014-10-28 00:53:38 -04002131 bn_ctx = NULL;
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07002132 OPENSSL_free(encodedPoint);
David Benjamin93d67d32014-10-28 00:53:38 -04002133 encodedPoint = NULL;
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07002134 EC_KEY_free(clnt_ecdh);
David Benjamin93d67d32014-10-28 00:53:38 -04002135 clnt_ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002136 EVP_PKEY_free(srvr_pub_pkey);
David Benjamin93d67d32014-10-28 00:53:38 -04002137 srvr_pub_pkey = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002138 }
David Benjamin93d67d32014-10-28 00:53:38 -04002139 else if (alg_k & SSL_kPSK)
2140 {
2141 /* For plain PSK, other_secret is a block of 0s with the same
2142 * length as the pre-shared key. */
2143 pms_len = psk_len;
2144 pms = OPENSSL_malloc(pms_len);
2145 if (pms == NULL)
2146 {
2147 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2148 goto err;
2149 }
2150 memset(pms, 0, pms_len);
2151 }
2152 else
Adam Langley95c29f32014-06-20 12:00:00 -07002153 {
2154 ssl3_send_alert(s, SSL3_AL_FATAL,
2155 SSL_AD_HANDSHAKE_FAILURE);
2156 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2157 goto err;
2158 }
2159
David Benjamin93d67d32014-10-28 00:53:38 -04002160 /* For a PSK cipher suite, other_secret is combined
2161 * with the pre-shared key. */
David Benjamin1f10d9c2014-11-08 11:41:14 -05002162 if (alg_a & SSL_aPSK)
David Benjamin93d67d32014-10-28 00:53:38 -04002163 {
2164 CBB cbb, child;
2165 uint8_t *new_pms;
2166 size_t new_pms_len;
2167
2168 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len))
2169 {
2170 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2171 goto err;
2172 }
2173 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
2174 !CBB_add_bytes(&child, pms, pms_len) ||
2175 !CBB_add_u16_length_prefixed(&cbb, &child) ||
2176 !CBB_add_bytes(&child, psk, psk_len) ||
2177 !CBB_finish(&cbb, &new_pms, &new_pms_len))
2178 {
2179 CBB_cleanup(&cbb);
2180 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2181 goto err;
2182 }
2183 OPENSSL_cleanse(pms, pms_len);
2184 OPENSSL_free(pms);
2185 pms = new_pms;
2186 pms_len = new_pms_len;
2187 }
2188
Adam Langley95c29f32014-06-20 12:00:00 -07002189 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2190 s->state=SSL3_ST_CW_KEY_EXCH_B;
Adam Langley75712922014-10-10 16:23:43 -07002191
2192 /* The message must be added to the finished hash before
2193 * calculating the master secret. */
2194 s->method->ssl3_enc->add_to_finished_hash(s);
2195
2196 s->session->master_key_length =
2197 s->method->ssl3_enc->generate_master_secret(s,
2198 s->session->master_key,
2199 pms, pms_len);
2200 if (s->session->master_key_length == 0)
2201 {
2202 goto err;
2203 }
2204 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2205 OPENSSL_cleanse(pms, pms_len);
2206 OPENSSL_free(pms);
Adam Langley95c29f32014-06-20 12:00:00 -07002207 }
2208
2209 /* SSL3_ST_CW_KEY_EXCH_B */
Adam Langley75712922014-10-10 16:23:43 -07002210 /* The message has already been added to the finished hash. */
2211 return s->method->ssl3_enc->do_write(s, dont_add_to_finished_hash);
2212
Adam Langley95c29f32014-06-20 12:00:00 -07002213err:
Adam Langley95c29f32014-06-20 12:00:00 -07002214 BN_CTX_free(bn_ctx);
2215 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2216 if (clnt_ecdh != NULL)
2217 EC_KEY_free(clnt_ecdh);
2218 EVP_PKEY_free(srvr_pub_pkey);
Adam Langley75712922014-10-10 16:23:43 -07002219 if (pms)
David Benjamin93d67d32014-10-28 00:53:38 -04002220 {
2221 OPENSSL_cleanse(pms, pms_len);
Adam Langley75712922014-10-10 16:23:43 -07002222 OPENSSL_free(pms);
David Benjamin93d67d32014-10-28 00:53:38 -04002223 }
Adam Langley75712922014-10-10 16:23:43 -07002224 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002225 }
2226
David Benjamin8da99062014-08-24 12:03:09 -04002227int ssl3_send_cert_verify(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002228 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002229 unsigned char *buf, *p;
David Benjamin854dd652014-08-26 00:32:30 -04002230 const EVP_MD *md = NULL;
David Benjamin0e2908a2014-07-09 15:22:43 -04002231 uint8_t digest[EVP_MAX_MD_SIZE];
David Benjamin854dd652014-08-26 00:32:30 -04002232 size_t digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002233 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002234 EVP_PKEY_CTX *pctx = NULL;
David Benjamin0e2908a2014-07-09 15:22:43 -04002235 size_t signature_length = 0;
2236 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002237
David Benjamin0e2908a2014-07-09 15:22:43 -04002238 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002239
2240 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2241 {
2242 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002243 pkey = s->cert->key->privatekey;
David Benjamin854dd652014-08-26 00:32:30 -04002244
2245 /* Write out the digest type if needbe. */
Adam Langley95c29f32014-06-20 12:00:00 -07002246 if (SSL_USE_SIGALGS(s))
2247 {
David Benjaminec2f27d2014-11-13 19:17:25 -05002248 md = tls1_choose_signing_digest(s, pkey);
David Benjamin854dd652014-08-26 00:32:30 -04002249 if (!tls12_get_sigandhash(p, pkey, md))
Adam Langley95c29f32014-06-20 12:00:00 -07002250 {
David Benjamin8da99062014-08-24 12:03:09 -04002251 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002252 goto err;
2253 }
2254 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002255 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07002256 }
David Benjamin854dd652014-08-26 00:32:30 -04002257
2258 /* Compute the digest. */
2259 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07002260 goto err;
David Benjamin854dd652014-08-26 00:32:30 -04002261
2262 /* The handshake buffer is no longer necessary. */
Adam Langley75712922014-10-10 16:23:43 -07002263 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, free_handshake_buffer))
David Benjamin854dd652014-08-26 00:32:30 -04002264 goto err;
David Benjamin0e2908a2014-07-09 15:22:43 -04002265
2266 /* Sign the digest. */
2267 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2268 if (pctx == NULL)
2269 goto err;
2270
2271 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
David Benjamin854dd652014-08-26 00:32:30 -04002272 if (!EVP_PKEY_sign_init(pctx) ||
2273 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2274 !EVP_PKEY_sign(pctx, NULL, &signature_length,
2275 digest, digest_length))
David Benjamin0e2908a2014-07-09 15:22:43 -04002276 {
David Benjamin8da99062014-08-24 12:03:09 -04002277 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002278 goto err;
2279 }
2280
2281 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2282 {
David Benjamin8da99062014-08-24 12:03:09 -04002283 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin0e2908a2014-07-09 15:22:43 -04002284 goto err;
2285 }
2286
David Benjamin854dd652014-08-26 00:32:30 -04002287 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length,
2288 digest, digest_length))
David Benjamin0e2908a2014-07-09 15:22:43 -04002289 {
David Benjamin8da99062014-08-24 12:03:09 -04002290 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002291 goto err;
2292 }
2293
2294 s2n(signature_length, p);
2295 n += signature_length + 2;
2296
Adam Langley95c29f32014-06-20 12:00:00 -07002297 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2298 s->state=SSL3_ST_CW_CERT_VRFY_B;
2299 }
Adam Langley95c29f32014-06-20 12:00:00 -07002300 EVP_PKEY_CTX_free(pctx);
2301 return ssl_do_write(s);
2302err:
Adam Langley95c29f32014-06-20 12:00:00 -07002303 EVP_PKEY_CTX_free(pctx);
2304 return(-1);
2305 }
2306
David Benjaminec2f27d2014-11-13 19:17:25 -05002307/* ssl3_has_client_certificate returns true if a client certificate is
2308 * configured. */
2309static int ssl3_has_client_certificate(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002310 {
David Benjaminec2f27d2014-11-13 19:17:25 -05002311 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
Adam Langley95c29f32014-06-20 12:00:00 -07002312 }
2313
2314int ssl3_send_client_certificate(SSL *s)
2315 {
2316 X509 *x509=NULL;
2317 EVP_PKEY *pkey=NULL;
2318 int i;
2319
2320 if (s->state == SSL3_ST_CW_CERT_A)
2321 {
2322 /* Let cert callback update client certificates if required */
2323 if (s->cert->cert_cb)
2324 {
2325 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2326 if (i < 0)
2327 {
2328 s->rwstate=SSL_X509_LOOKUP;
2329 return -1;
2330 }
2331 if (i == 0)
2332 {
2333 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2334 return 0;
2335 }
2336 s->rwstate=SSL_NOTHING;
2337 }
David Benjaminec2f27d2014-11-13 19:17:25 -05002338 if (ssl3_has_client_certificate(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002339 s->state=SSL3_ST_CW_CERT_C;
2340 else
2341 s->state=SSL3_ST_CW_CERT_B;
2342 }
2343
2344 /* We need to get a client cert */
2345 if (s->state == SSL3_ST_CW_CERT_B)
2346 {
2347 /* If we get an error, we need to
2348 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
David Benjamin7bbeead2014-07-25 03:56:20 -04002349 * We then get retried later */
Adam Langley95c29f32014-06-20 12:00:00 -07002350 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2351 if (i < 0)
2352 {
2353 s->rwstate=SSL_X509_LOOKUP;
2354 return(-1);
2355 }
2356 s->rwstate=SSL_NOTHING;
2357 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2358 {
2359 s->state=SSL3_ST_CW_CERT_B;
2360 if ( !SSL_use_certificate(s,x509) ||
2361 !SSL_use_PrivateKey(s,pkey))
2362 i=0;
2363 }
2364 else if (i == 1)
2365 {
2366 i=0;
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002367 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
Adam Langley95c29f32014-06-20 12:00:00 -07002368 }
2369
2370 if (x509 != NULL) X509_free(x509);
2371 if (pkey != NULL) EVP_PKEY_free(pkey);
David Benjaminec2f27d2014-11-13 19:17:25 -05002372 if (i && !ssl3_has_client_certificate(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002373 i = 0;
2374 if (i == 0)
2375 {
2376 if (s->version == SSL3_VERSION)
2377 {
2378 s->s3->tmp.cert_req=0;
2379 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2380 return(1);
2381 }
2382 else
2383 {
2384 s->s3->tmp.cert_req=2;
2385 }
2386 }
2387
2388 /* Ok, we have a cert */
2389 s->state=SSL3_ST_CW_CERT_C;
2390 }
2391
2392 if (s->state == SSL3_ST_CW_CERT_C)
2393 {
2394 s->state=SSL3_ST_CW_CERT_D;
2395 ssl3_output_cert_chain(s,
2396 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2397 }
2398 /* SSL3_ST_CW_CERT_D */
2399 return ssl_do_write(s);
2400 }
2401
2402#define has_bits(i,m) (((i)&(m)) == (m))
2403
2404int ssl3_check_cert_and_algorithm(SSL *s)
2405 {
2406 int i,idx;
2407 long alg_k,alg_a;
2408 EVP_PKEY *pkey=NULL;
2409 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002410 DH *dh;
Adam Langley95c29f32014-06-20 12:00:00 -07002411
David Benjamine8f3d662014-07-12 01:10:19 -04002412 /* we don't have a certificate */
2413 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2414 return 1;
2415
Adam Langley95c29f32014-06-20 12:00:00 -07002416 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2417 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2418
Adam Langley95c29f32014-06-20 12:00:00 -07002419 sc=s->session->sess_cert;
2420 if (sc == NULL)
2421 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002422 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002423 goto err;
2424 }
2425
Adam Langley95c29f32014-06-20 12:00:00 -07002426 dh=s->session->sess_cert->peer_dh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002427
2428 /* This is the passed certificate */
2429
2430 idx=sc->peer_cert_type;
Adam Langley95c29f32014-06-20 12:00:00 -07002431 if (idx == SSL_PKEY_ECC)
2432 {
2433 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2434 s) == 0)
2435 { /* check failed */
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002436 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002437 goto f_err;
2438 }
2439 else
2440 {
2441 return 1;
2442 }
2443 }
2444 else if (alg_a & SSL_aECDSA)
2445 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002446 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002447 goto f_err;
2448 }
Adam Langley95c29f32014-06-20 12:00:00 -07002449 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2450 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2451 EVP_PKEY_free(pkey);
2452
2453
2454 /* Check that we have a certificate if we require one */
2455 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2456 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002457 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002458 goto f_err;
2459 }
David Benjamin525a0fe2014-11-08 11:41:12 -05002460 if ((alg_k & SSL_kRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_ENC))
Adam Langley95c29f32014-06-20 12:00:00 -07002461 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002462 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002463 goto f_err;
2464 }
Adam Langley95c29f32014-06-20 12:00:00 -07002465 if ((alg_k & SSL_kEDH) &&
2466 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2467 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002468 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -07002469 goto f_err;
2470 }
Adam Langley95c29f32014-06-20 12:00:00 -07002471
Adam Langley95c29f32014-06-20 12:00:00 -07002472 return(1);
2473f_err:
2474 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2475err:
2476 return(0);
2477 }
2478
Adam Langley95c29f32014-06-20 12:00:00 -07002479int ssl3_send_next_proto(SSL *s)
2480 {
2481 unsigned int len, padding_len;
2482 unsigned char *d;
2483
2484 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
2485 {
2486 len = s->next_proto_negotiated_len;
2487 padding_len = 32 - ((len + 2) % 32);
2488 d = (unsigned char *)s->init_buf->data;
2489 d[4] = len;
2490 memcpy(d + 5, s->next_proto_negotiated, len);
2491 d[5 + len] = padding_len;
2492 memset(d + 6 + len, 0, padding_len);
2493 *(d++)=SSL3_MT_NEXT_PROTO;
2494 l2n3(2 + len + padding_len, d);
2495 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2496 s->init_num = 4 + 2 + len + padding_len;
2497 s->init_off = 0;
2498 }
2499
Adam Langley75712922014-10-10 16:23:43 -07002500 return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley95c29f32014-06-20 12:00:00 -07002501}
Adam Langley1258b6a2014-06-20 12:00:00 -07002502
Adam Langley1258b6a2014-06-20 12:00:00 -07002503
2504int ssl3_send_channel_id(SSL *s)
2505 {
2506 unsigned char *d;
2507 int ret = -1, public_key_len;
2508 EVP_MD_CTX md_ctx;
2509 size_t sig_len;
2510 ECDSA_SIG *sig = NULL;
2511 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
2512
2513 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
Adam Langley75712922014-10-10 16:23:43 -07002514 return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley1258b6a2014-06-20 12:00:00 -07002515
2516 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
2517 {
2518 EVP_PKEY *key = NULL;
2519 s->ctx->channel_id_cb(s, &key);
2520 if (key != NULL)
2521 {
2522 s->tlsext_channel_id_private = key;
2523 }
2524 }
2525 if (!s->tlsext_channel_id_private)
2526 {
2527 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
2528 return (-1);
2529 }
2530 s->rwstate=SSL_NOTHING;
2531
2532 d = (unsigned char *)s->init_buf->data;
2533 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
2534 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
2535 if (s->s3->tlsext_channel_id_new)
2536 s2n(TLSEXT_TYPE_channel_id_new, d);
2537 else
2538 s2n(TLSEXT_TYPE_channel_id, d);
2539 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2540
2541 EVP_MD_CTX_init(&md_ctx);
2542
2543 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2544 if (public_key_len <= 0)
2545 {
2546 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2547 goto err;
2548 }
2549 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2550 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2551 * field elements as 32-byte, big-endian numbers. */
2552 if (public_key_len != 65)
2553 {
2554 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2555 goto err;
2556 }
2557 public_key = OPENSSL_malloc(public_key_len);
2558 if (!public_key)
2559 {
2560 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2561 goto err;
2562 }
2563
2564 derp = public_key;
2565 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2566
2567 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2568 s->tlsext_channel_id_private) != 1)
2569 {
2570 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2571 goto err;
2572 }
2573
2574 if (!tls1_channel_id_hash(&md_ctx, s))
2575 goto err;
2576
2577 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
2578 {
2579 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2580 goto err;
2581 }
2582
2583 der_sig = OPENSSL_malloc(sig_len);
2584 if (!der_sig)
2585 {
2586 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2587 goto err;
2588 }
2589
2590 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
2591 {
2592 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2593 goto err;
2594 }
2595
2596 derp = der_sig;
2597 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
2598 if (sig == NULL)
2599 {
2600 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2601 goto err;
2602 }
2603
2604 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2605 memcpy(d, public_key + 1, 64);
2606 d += 64;
David Benjaminf2b32a22014-11-02 17:32:40 -05002607 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2608 !BN_bn2bin_padded(d + 32, 32, sig->s))
2609 {
2610 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2611 goto err;
2612 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002613
2614 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2615 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
2616 s->init_off = 0;
2617
Adam Langley75712922014-10-10 16:23:43 -07002618 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash);
Adam Langley1258b6a2014-06-20 12:00:00 -07002619
2620err:
2621 EVP_MD_CTX_cleanup(&md_ctx);
2622 if (public_key)
2623 OPENSSL_free(public_key);
2624 if (der_sig)
2625 OPENSSL_free(der_sig);
2626 if (sig)
2627 ECDSA_SIG_free(sig);
2628
2629 return ret;
2630 }
Adam Langley95c29f32014-06-20 12:00:00 -07002631
Adam Langley95c29f32014-06-20 12:00:00 -07002632int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2633 {
2634 int i = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002635 if (s->ctx->client_cert_cb)
2636 i = s->ctx->client_cert_cb(s,px509,ppkey);
2637 return i;
2638 }