blob: 124c763b56fa5f9ad5445ec2a7f345eb9f4702d1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
134 *
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
138 *
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
143 *
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
149 */
150
David Benjamin81ea0bf2014-11-23 04:20:17 -0500151#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
155#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/rand.h>
158#include <openssl/obj.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400159#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/evp.h>
161#include <openssl/mem.h>
162#include <openssl/md5.h>
163#include <openssl/dh.h>
164#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400166#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin2ee94aa2015-04-07 22:38:30 -0400168#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700169#include "../crypto/dh/internal.h"
170
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley1bea1732014-12-17 19:06:57 -0800172int ssl3_connect(SSL *s) {
173 BUF_MEM *buf = NULL;
174 void (*cb)(const SSL *ssl, int type, int val) = NULL;
175 int ret = -1;
176 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500177
Adam Langley1bea1732014-12-17 19:06:57 -0800178 assert(s->handshake_func == ssl3_connect);
179 assert(!s->server);
180 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley1bea1732014-12-17 19:06:57 -0800182 ERR_clear_error();
183 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley1bea1732014-12-17 19:06:57 -0800185 if (s->info_callback != NULL) {
186 cb = s->info_callback;
187 } else if (s->ctx->info_callback != NULL) {
188 cb = s->ctx->info_callback;
189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley1bea1732014-12-17 19:06:57 -0800191 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley1bea1732014-12-17 19:06:57 -0800193 for (;;) {
194 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley1bea1732014-12-17 19:06:57 -0800196 switch (s->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800197 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500198 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800199 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley1bea1732014-12-17 19:06:57 -0800202 if (s->init_buf == NULL) {
203 buf = BUF_MEM_new();
204 if (buf == NULL ||
205 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
206 ret = -1;
207 goto end;
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langley1bea1732014-12-17 19:06:57 -0800210 s->init_buf = buf;
211 buf = NULL;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin6a08da22015-05-08 22:58:12 -0400214 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800215 ret = -1;
216 goto end;
217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langley1bea1732014-12-17 19:06:57 -0800219 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley1bea1732014-12-17 19:06:57 -0800221 if (!ssl3_init_finished_mac(s)) {
222 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
223 ret = -1;
224 goto end;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley1bea1732014-12-17 19:06:57 -0800227 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800228 s->init_num = 0;
229 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langley1bea1732014-12-17 19:06:57 -0800231 case SSL3_ST_CW_CLNT_HELLO_A:
232 case SSL3_ST_CW_CLNT_HELLO_B:
233 s->shutdown = 0;
234 ret = ssl3_send_client_hello(s);
235 if (ret <= 0) {
236 goto end;
237 }
238 s->state = SSL3_ST_CR_SRVR_HELLO_A;
239 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley1bea1732014-12-17 19:06:57 -0800241 /* turn on buffering for the next lot of output */
242 if (s->bbio != s->wbio) {
243 s->wbio = BIO_push(s->bbio, s->wbio);
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langley1bea1732014-12-17 19:06:57 -0800246 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley1bea1732014-12-17 19:06:57 -0800248 case SSL3_ST_CR_SRVR_HELLO_A:
249 case SSL3_ST_CR_SRVR_HELLO_B:
250 ret = ssl3_get_server_hello(s);
251 if (ret <= 0) {
252 goto end;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley1bea1732014-12-17 19:06:57 -0800255 if (s->hit) {
256 s->state = SSL3_ST_CR_CHANGE;
257 if (s->tlsext_ticket_expected) {
258 /* receive renewed session ticket */
259 s->state = SSL3_ST_CR_SESSION_TICKET_A;
260 }
261 } else {
262 s->state = SSL3_ST_CR_CERT_A;
263 }
264 s->init_num = 0;
265 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400266
Adam Langley1bea1732014-12-17 19:06:57 -0800267 case SSL3_ST_CR_CERT_A:
268 case SSL3_ST_CR_CERT_B:
269 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
270 ret = ssl3_get_server_certificate(s);
271 if (ret <= 0) {
272 goto end;
273 }
274 if (s->s3->tmp.certificate_status_expected) {
275 s->state = SSL3_ST_CR_CERT_STATUS_A;
276 } else {
277 s->state = SSL3_ST_CR_KEY_EXCH_A;
278 }
279 } else {
280 skip = 1;
281 s->state = SSL3_ST_CR_KEY_EXCH_A;
282 }
283 s->init_num = 0;
284 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langley1bea1732014-12-17 19:06:57 -0800286 case SSL3_ST_CR_KEY_EXCH_A:
287 case SSL3_ST_CR_KEY_EXCH_B:
288 ret = ssl3_get_server_key_exchange(s);
289 if (ret <= 0) {
290 goto end;
291 }
292 s->state = SSL3_ST_CR_CERT_REQ_A;
293 s->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800294 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley1bea1732014-12-17 19:06:57 -0800296 case SSL3_ST_CR_CERT_REQ_A:
297 case SSL3_ST_CR_CERT_REQ_B:
298 ret = ssl3_get_certificate_request(s);
299 if (ret <= 0) {
300 goto end;
301 }
302 s->state = SSL3_ST_CR_SRVR_DONE_A;
303 s->init_num = 0;
304 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley1bea1732014-12-17 19:06:57 -0800306 case SSL3_ST_CR_SRVR_DONE_A:
307 case SSL3_ST_CR_SRVR_DONE_B:
308 ret = ssl3_get_server_done(s);
309 if (ret <= 0) {
310 goto end;
311 }
312 if (s->s3->tmp.cert_req) {
313 s->state = SSL3_ST_CW_CERT_A;
314 } else {
315 s->state = SSL3_ST_CW_KEY_EXCH_A;
316 }
317 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CW_CERT_A:
322 case SSL3_ST_CW_CERT_B:
323 case SSL3_ST_CW_CERT_C:
324 case SSL3_ST_CW_CERT_D:
325 ret = ssl3_send_client_certificate(s);
326 if (ret <= 0) {
327 goto end;
328 }
329 s->state = SSL3_ST_CW_KEY_EXCH_A;
330 s->init_num = 0;
331 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley1bea1732014-12-17 19:06:57 -0800333 case SSL3_ST_CW_KEY_EXCH_A:
334 case SSL3_ST_CW_KEY_EXCH_B:
335 ret = ssl3_send_client_key_exchange(s);
336 if (ret <= 0) {
337 goto end;
338 }
339 /* For TLS, cert_req is set to 2, so a cert chain
340 * of nothing is sent, but no verify packet is sent */
341 if (s->s3->tmp.cert_req == 1) {
342 s->state = SSL3_ST_CW_CERT_VRFY_A;
343 } else {
344 s->state = SSL3_ST_CW_CHANGE_A;
345 s->s3->change_cipher_spec = 0;
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley1bea1732014-12-17 19:06:57 -0800348 s->init_num = 0;
349 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langley1bea1732014-12-17 19:06:57 -0800351 case SSL3_ST_CW_CERT_VRFY_A:
352 case SSL3_ST_CW_CERT_VRFY_B:
353 ret = ssl3_send_cert_verify(s);
354 if (ret <= 0) {
355 goto end;
356 }
357 s->state = SSL3_ST_CW_CHANGE_A;
358 s->init_num = 0;
359 s->s3->change_cipher_spec = 0;
360 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langley1bea1732014-12-17 19:06:57 -0800362 case SSL3_ST_CW_CHANGE_A:
363 case SSL3_ST_CW_CHANGE_B:
364 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
365 SSL3_ST_CW_CHANGE_B);
366 if (ret <= 0) {
367 goto end;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langley1bea1732014-12-17 19:06:57 -0800370 s->state = SSL3_ST_CW_FINISHED_A;
371 if (s->s3->tlsext_channel_id_valid) {
372 s->state = SSL3_ST_CW_CHANNEL_ID_A;
373 }
374 if (s->s3->next_proto_neg_seen) {
375 s->state = SSL3_ST_CW_NEXT_PROTO_A;
376 }
377 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langley1bea1732014-12-17 19:06:57 -0800379 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400380 if (!s->enc_method->setup_key_block(s) ||
381 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800382 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
383 ret = -1;
384 goto end;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langley1bea1732014-12-17 19:06:57 -0800387 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley1bea1732014-12-17 19:06:57 -0800389 case SSL3_ST_CW_NEXT_PROTO_A:
390 case SSL3_ST_CW_NEXT_PROTO_B:
391 ret = ssl3_send_next_proto(s);
392 if (ret <= 0) {
393 goto end;
394 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700395
Adam Langley1bea1732014-12-17 19:06:57 -0800396 if (s->s3->tlsext_channel_id_valid) {
397 s->state = SSL3_ST_CW_CHANNEL_ID_A;
398 } else {
399 s->state = SSL3_ST_CW_FINISHED_A;
400 }
401 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langley1bea1732014-12-17 19:06:57 -0800403 case SSL3_ST_CW_CHANNEL_ID_A:
404 case SSL3_ST_CW_CHANNEL_ID_B:
405 ret = ssl3_send_channel_id(s);
406 if (ret <= 0) {
407 goto end;
408 }
409 s->state = SSL3_ST_CW_FINISHED_A;
410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langley1bea1732014-12-17 19:06:57 -0800412 case SSL3_ST_CW_FINISHED_A:
413 case SSL3_ST_CW_FINISHED_B:
414 ret =
415 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
416 s->enc_method->client_finished_label,
417 s->enc_method->client_finished_label_len);
418 if (ret <= 0) {
419 goto end;
420 }
421 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langley1bea1732014-12-17 19:06:57 -0800423 if (s->hit) {
424 s->s3->tmp.next_state = SSL_ST_OK;
425 } else {
426 /* This is a non-resumption handshake. If it involves ChannelID, then
427 * record the handshake hashes at this point in the session so that
428 * any resumption of this session with ChannelID can sign those
429 * hashes. */
430 if (s->s3->tlsext_channel_id_new) {
431 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500432 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800433 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500434 }
Adam Langley1bea1732014-12-17 19:06:57 -0800435 }
David Benjamined7c4752015-02-16 19:16:46 -0500436 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
437 ssl3_can_false_start(s) &&
438 /* No False Start on renegotiation (would complicate the state
439 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400440 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500441 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800442 } else {
443 /* Allow NewSessionTicket if ticket expected */
444 if (s->tlsext_ticket_expected) {
445 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
446 } else {
447 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
448 }
449 }
450 }
451 s->init_num = 0;
452 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langley1bea1732014-12-17 19:06:57 -0800454 case SSL3_ST_CR_SESSION_TICKET_A:
455 case SSL3_ST_CR_SESSION_TICKET_B:
456 ret = ssl3_get_new_session_ticket(s);
457 if (ret <= 0) {
458 goto end;
459 }
460 s->state = SSL3_ST_CR_CHANGE;
461 s->init_num = 0;
462 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langley1bea1732014-12-17 19:06:57 -0800464 case SSL3_ST_CR_CERT_STATUS_A:
465 case SSL3_ST_CR_CERT_STATUS_B:
466 ret = ssl3_get_cert_status(s);
467 if (ret <= 0) {
468 goto end;
469 }
470 s->state = SSL3_ST_CR_KEY_EXCH_A;
471 s->init_num = 0;
472 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400473
Adam Langley1bea1732014-12-17 19:06:57 -0800474 case SSL3_ST_CR_CHANGE:
475 /* At this point, the next message must be entirely behind a
476 * ChangeCipherSpec. */
477 if (!ssl3_expect_change_cipher_spec(s)) {
478 ret = -1;
479 goto end;
480 }
481 s->state = SSL3_ST_CR_FINISHED_A;
482 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langley1bea1732014-12-17 19:06:57 -0800484 case SSL3_ST_CR_FINISHED_A:
485 case SSL3_ST_CR_FINISHED_B:
486 ret =
487 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
488 if (ret <= 0) {
489 goto end;
490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langley1bea1732014-12-17 19:06:57 -0800492 if (s->hit) {
493 s->state = SSL3_ST_CW_CHANGE_A;
494 } else {
495 s->state = SSL_ST_OK;
496 }
497 s->init_num = 0;
498 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langley1bea1732014-12-17 19:06:57 -0800500 case SSL3_ST_CW_FLUSH:
501 s->rwstate = SSL_WRITING;
502 if (BIO_flush(s->wbio) <= 0) {
503 ret = -1;
504 goto end;
505 }
506 s->rwstate = SSL_NOTHING;
507 s->state = s->s3->tmp.next_state;
508 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700509
David Benjamined7c4752015-02-16 19:16:46 -0500510 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800511 /* Allow NewSessionTicket if ticket expected */
512 if (s->tlsext_ticket_expected) {
513 s->state = SSL3_ST_CR_SESSION_TICKET_A;
514 } else {
515 s->state = SSL3_ST_CR_CHANGE;
516 }
David Benjamined7c4752015-02-16 19:16:46 -0500517 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700518
Adam Langley1bea1732014-12-17 19:06:57 -0800519 ssl_free_wbio_buffer(s);
520 ret = 1;
521 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langley1bea1732014-12-17 19:06:57 -0800523 case SSL_ST_OK:
524 /* clean a few things up */
525 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin2755a3e2015-04-22 16:17:58 -0400527 BUF_MEM_free(s->init_buf);
528 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langley1bea1732014-12-17 19:06:57 -0800530 /* Remove write buffering now. */
531 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langley1bea1732014-12-17 19:06:57 -0800533 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500534 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400535 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langley1bea1732014-12-17 19:06:57 -0800537 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langley1bea1732014-12-17 19:06:57 -0800539 ret = 1;
540 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langley1bea1732014-12-17 19:06:57 -0800542 if (cb != NULL) {
543 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langley1bea1732014-12-17 19:06:57 -0800546 goto end;
547
548 default:
549 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
550 ret = -1;
551 goto end;
552 }
553
554 if (!s->s3->tmp.reuse_message && !skip) {
555 if (cb != NULL && s->state != state) {
556 new_state = s->state;
557 s->state = state;
558 cb(s, SSL_CB_CONNECT_LOOP, 1);
559 s->state = new_state;
560 }
561 }
562 skip = 0;
563 }
564
Adam Langley95c29f32014-06-20 12:00:00 -0700565end:
Adam Langley1bea1732014-12-17 19:06:57 -0800566 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400567 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800568 if (cb != NULL) {
569 cb(s, SSL_CB_CONNECT_EXIT, ret);
570 }
571 return ret;
572}
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langley1bea1732014-12-17 19:06:57 -0800574int ssl3_send_client_hello(SSL *s) {
575 uint8_t *buf, *p, *d;
576 int i;
577 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700578
Adam Langley1bea1732014-12-17 19:06:57 -0800579 buf = (uint8_t *)s->init_buf->data;
580 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
581 if (!s->s3->have_version) {
582 uint16_t max_version = ssl3_get_max_client_version(s);
583 /* Disabling all versions is silly: return an error. */
584 if (max_version == 0) {
585 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
586 goto err;
587 }
588 s->version = max_version;
589 s->client_version = max_version;
590 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500591
Adam Langley1bea1732014-12-17 19:06:57 -0800592 /* If the configured session was created at a version higher than our
593 * maximum version, drop it. */
594 if (s->session &&
595 (s->session->session_id_length == 0 || s->session->not_resumable ||
596 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
597 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
598 SSL_set_session(s, NULL);
599 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500600
Adam Langley1bea1732014-12-17 19:06:57 -0800601 /* else use the pre-loaded session */
602 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langley1bea1732014-12-17 19:06:57 -0800604 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
605 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400606 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400607 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
608 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400609 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800610 }
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langley1bea1732014-12-17 19:06:57 -0800612 /* Do the message type and length last. Note: the final argument to
613 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
614 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langley1bea1732014-12-17 19:06:57 -0800616 /* version indicates the negotiated version: for example from an SSLv2/v3
617 * compatible client hello). The client_version field is the maximum
618 * version we permit and it is also used in RSA encrypted premaster
619 * secrets. Some servers can choke if we initially report a higher version
620 * then renegotiate to a lower one in the premaster secret. This didn't
621 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
622 * or later if the server only supports 1.0.
623 *
624 * Possible scenario with previous logic:
625 * 1. Client hello indicates TLS 1.2
626 * 2. Server hello says TLS 1.0
627 * 3. RSA encrypted premaster secret uses 1.2.
628 * 4. Handhaked proceeds using TLS 1.0.
629 * 5. Server sends hello request to renegotiate.
630 * 6. Client hello indicates TLS v1.0 as we now
631 * know that is maximum server supports.
632 * 7. Server chokes on RSA encrypted premaster secret
633 * containing version 1.0.
634 *
635 * For interoperability it should be OK to always use the maximum version
636 * we support in client hello and then rely on the checking of version to
637 * ensure the servers isn't being inconsistent: for example initially
638 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
639 * using client_version in client hello and not resetting it to the
640 * negotiated version. */
641 *(p++) = s->client_version >> 8;
642 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley1bea1732014-12-17 19:06:57 -0800644 /* Random stuff */
645 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
646 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langley1bea1732014-12-17 19:06:57 -0800648 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400649 if (s->s3->initial_handshake_complete || s->session == NULL) {
650 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800651 i = 0;
652 } else {
653 i = s->session->session_id_length;
654 }
655 *(p++) = i;
656 if (i != 0) {
657 if (i > (int)sizeof(s->session->session_id)) {
658 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
659 goto err;
660 }
661 memcpy(p, s->session->session_id, i);
662 p += i;
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley1bea1732014-12-17 19:06:57 -0800665 /* cookie stuff for DTLS */
666 if (SSL_IS_DTLS(s)) {
667 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
668 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
669 goto err;
670 }
671 *(p++) = s->d1->cookie_len;
672 memcpy(p, s->d1->cookie, s->d1->cookie_len);
673 p += s->d1->cookie_len;
674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langley1bea1732014-12-17 19:06:57 -0800676 /* Ciphers supported */
677 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
678 if (i == 0) {
679 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
680 SSL_R_NO_CIPHERS_AVAILABLE);
681 goto err;
682 }
683 s2n(i, p);
684 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langley1bea1732014-12-17 19:06:57 -0800686 /* COMPRESSION */
687 *(p++) = 1;
688 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langley1bea1732014-12-17 19:06:57 -0800690 /* TLS extensions*/
691 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
692 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
693 goto err;
694 }
695
696 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
697 p - buf);
698 if (p == NULL) {
699 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
700 goto err;
701 }
702
703 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500704 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
705 goto err;
706 }
Adam Langley1bea1732014-12-17 19:06:57 -0800707 s->state = SSL3_ST_CW_CLNT_HELLO_B;
708 }
709
710 /* SSL3_ST_CW_CLNT_HELLO_B */
711 return ssl_do_write(s);
712
Adam Langley95c29f32014-06-20 12:00:00 -0700713err:
Adam Langley1bea1732014-12-17 19:06:57 -0800714 return -1;
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langley1bea1732014-12-17 19:06:57 -0800717int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400718 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800719 const SSL_CIPHER *c;
720 CERT *ct = s->cert;
721 int al = SSL_AD_INTERNAL_ERROR, ok;
722 long n;
723 CBS server_hello, server_random, session_id;
724 uint16_t server_version, cipher_suite;
725 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400726 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langley1bea1732014-12-17 19:06:57 -0800728 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
729 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
730 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500731 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langley1bea1732014-12-17 19:06:57 -0800733 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500734 uint32_t err = ERR_peek_error();
735 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
736 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
737 /* Add a dedicated error code to the queue for a handshake_failure alert
738 * in response to ClientHello. This matches NSS's client behavior and
739 * gives a better error on a (probable) failure to negotiate initial
740 * parameters. Note: this error code comes after the original one.
741 *
742 * See https://crbug.com/446505. */
743 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
744 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
745 }
Adam Langley1bea1732014-12-17 19:06:57 -0800746 return n;
747 }
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langley1bea1732014-12-17 19:06:57 -0800749 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400750
Adam Langley1bea1732014-12-17 19:06:57 -0800751 if (!CBS_get_u16(&server_hello, &server_version) ||
752 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
753 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
754 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
755 !CBS_get_u16(&server_hello, &cipher_suite) ||
756 !CBS_get_u8(&server_hello, &compression_method)) {
757 al = SSL_AD_DECODE_ERROR;
758 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
759 goto f_err;
760 }
David Benjamina03d95d2014-07-12 19:49:07 -0400761
David Benjamine6df0542015-05-12 22:02:08 -0400762 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800763 if (!s->s3->have_version) {
764 if (!ssl3_is_version_enabled(s, server_version)) {
765 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
766 s->version = server_version;
767 /* Mark the version as fixed so the record-layer version is not clamped
768 * to TLS 1.0. */
769 s->s3->have_version = 1;
770 al = SSL_AD_PROTOCOL_VERSION;
771 goto f_err;
772 }
773 s->version = server_version;
774 s->enc_method = ssl3_get_enc_method(server_version);
775 assert(s->enc_method != NULL);
776 /* At this point, the connection's version is known and s->version is
777 * fixed. Begin enforcing the record-layer version. */
778 s->s3->have_version = 1;
779 } else if (server_version != s->version) {
780 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
781 al = SSL_AD_PROTOCOL_VERSION;
782 goto f_err;
783 }
Adam Langley95c29f32014-06-20 12:00:00 -0700784
Adam Langley1bea1732014-12-17 19:06:57 -0800785 /* Copy over the server random. */
786 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langley1bea1732014-12-17 19:06:57 -0800788 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400789 if (!s->s3->initial_handshake_complete && s->session != NULL &&
790 CBS_mem_equal(&session_id, s->session->session_id,
791 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800792 if (s->sid_ctx_length != s->session->sid_ctx_length ||
793 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
794 /* actually a client application bug */
795 al = SSL_AD_ILLEGAL_PARAMETER;
796 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
797 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
798 goto f_err;
799 }
800 s->hit = 1;
801 } else {
802 /* The session wasn't resumed. Create a fresh SSL_SESSION to
803 * fill out. */
804 s->hit = 0;
805 if (!ssl_get_new_session(s, 0)) {
806 goto f_err;
807 }
808 /* Note: session_id could be empty. */
809 s->session->session_id_length = CBS_len(&session_id);
810 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
811 }
David Benjamina03d95d2014-07-12 19:49:07 -0400812
David Benjamina1c90a52015-05-30 17:03:14 -0400813 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800814 if (c == NULL) {
815 /* unknown cipher */
816 al = SSL_AD_ILLEGAL_PARAMETER;
817 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
818 SSL_R_UNKNOWN_CIPHER_RETURNED);
819 goto f_err;
820 }
821 /* ct->mask_ssl was computed from client capabilities. Now
822 * that the final version is known, compute a new mask_ssl. */
823 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
824 mask_ssl = SSL_TLSV1_2;
825 } else {
826 mask_ssl = 0;
827 }
828 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
829 * the server selected it, it's an error. */
830 if ((c->algorithm_ssl & mask_ssl) ||
831 (c->algorithm_mkey & ct->mask_k) ||
832 (c->algorithm_auth & ct->mask_a)) {
833 al = SSL_AD_ILLEGAL_PARAMETER;
834 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
835 goto f_err;
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langley1bea1732014-12-17 19:06:57 -0800838 sk = ssl_get_ciphers_by_id(s);
839 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
840 /* we did not say we would use this cipher */
841 al = SSL_AD_ILLEGAL_PARAMETER;
842 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
843 goto f_err;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
David Benjaminece3de92015-03-16 18:02:20 -0400846 if (s->hit) {
847 if (s->session->cipher != c) {
848 al = SSL_AD_ILLEGAL_PARAMETER;
849 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
850 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
851 goto f_err;
852 }
853 if (s->session->ssl_version != s->version) {
854 al = SSL_AD_ILLEGAL_PARAMETER;
855 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
856 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
857 goto f_err;
858 }
Adam Langley1bea1732014-12-17 19:06:57 -0800859 }
860 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500861
David Benjamin5f04b652015-05-26 17:30:10 -0400862 /* If doing a full handshake with TLS 1.2, the server may request a client
863 * certificate which requires hashing the handshake transcript under a
864 * different hash. Otherwise, release the handshake buffer. */
865 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800866 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
867 goto f_err;
868 }
David Benjamina03d95d2014-07-12 19:49:07 -0400869
Adam Langley1bea1732014-12-17 19:06:57 -0800870 /* Only the NULL compression algorithm is supported. */
871 if (compression_method != 0) {
872 al = SSL_AD_ILLEGAL_PARAMETER;
873 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
874 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
875 goto f_err;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langley1bea1732014-12-17 19:06:57 -0800878 /* TLS extensions */
879 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
880 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
881 goto err;
882 }
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley1bea1732014-12-17 19:06:57 -0800884 /* There should be nothing left over in the record. */
885 if (CBS_len(&server_hello) != 0) {
886 /* wrong packet length */
887 al = SSL_AD_DECODE_ERROR;
888 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
889 goto f_err;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyba5934b2015-06-02 10:50:35 -0700892 if (s->hit &&
893 s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
894 al = SSL_AD_HANDSHAKE_FAILURE;
895 if (s->session->extended_master_secret) {
896 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
897 SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
898 } else {
899 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
900 SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
901 }
902 goto f_err;
903 }
904
Adam Langley1bea1732014-12-17 19:06:57 -0800905 return 1;
906
Adam Langley95c29f32014-06-20 12:00:00 -0700907f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800908 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700909err:
Adam Langley1bea1732014-12-17 19:06:57 -0800910 return -1;
911}
Adam Langley95c29f32014-06-20 12:00:00 -0700912
David Benjamin436bf822015-06-07 19:50:37 -0400913/* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
914 * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
915 * on the error queue. */
916static int ssl3_check_certificate_for_cipher(X509 *leaf,
917 const SSL_CIPHER *cipher) {
918 int ret = 0;
919 EVP_PKEY *pkey = X509_get_pubkey(leaf);
920 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
921 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
922 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
923 goto err;
924 }
925
926 /* Check the certificate's type matches the cipher. */
927 int cert_type = ssl_cert_type(pkey);
928 if (cert_type < 0) {
929 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
930 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
931 goto err;
932 }
933 int expected_type = ssl_cipher_get_cert_index(cipher);
934 assert(expected_type >= 0);
935 if (cert_type != expected_type) {
936 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
937 SSL_R_WRONG_CERTIFICATE_TYPE);
938 goto err;
939 }
940
941 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
942 * be checked in other cases as well? */
943 if (cipher->algorithm_auth & SSL_aECDSA) {
944 /* This call populates the ex_flags field correctly */
945 X509_check_purpose(leaf, -1, 0);
946 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
947 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
948 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
949 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
950 goto err;
951 }
952 }
953
954 ret = 1;
955
956err:
957 EVP_PKEY_free(pkey);
958 return ret;
959}
960
Adam Langley1bea1732014-12-17 19:06:57 -0800961int ssl3_get_server_certificate(SSL *s) {
962 int al, i, ok, ret = -1;
963 unsigned long n;
964 X509 *x = NULL;
965 STACK_OF(X509) *sk = NULL;
966 SESS_CERT *sc;
967 EVP_PKEY *pkey = NULL;
968 CBS cbs, certificate_list;
969 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langley1bea1732014-12-17 19:06:57 -0800971 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400972 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500973 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langley1bea1732014-12-17 19:06:57 -0800975 if (!ok) {
976 return n;
977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langley1bea1732014-12-17 19:06:57 -0800979 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langley1bea1732014-12-17 19:06:57 -0800981 sk = sk_X509_new_null();
982 if (sk == NULL) {
983 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
984 goto err;
985 }
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langley1bea1732014-12-17 19:06:57 -0800987 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400988 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800989 CBS_len(&cbs) != 0) {
990 al = SSL_AD_DECODE_ERROR;
David Benjamin8923c0b2015-06-07 11:42:34 -0400991 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800992 goto f_err;
993 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400994
Adam Langley1bea1732014-12-17 19:06:57 -0800995 while (CBS_len(&certificate_list) > 0) {
996 CBS certificate;
997 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
998 al = SSL_AD_DECODE_ERROR;
999 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1000 SSL_R_CERT_LENGTH_MISMATCH);
1001 goto f_err;
1002 }
1003 data = CBS_data(&certificate);
1004 x = d2i_X509(NULL, &data, CBS_len(&certificate));
1005 if (x == NULL) {
1006 al = SSL_AD_BAD_CERTIFICATE;
1007 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1008 goto f_err;
1009 }
1010 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1011 al = SSL_AD_DECODE_ERROR;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1013 SSL_R_CERT_LENGTH_MISMATCH);
1014 goto f_err;
1015 }
1016 if (!sk_X509_push(sk, x)) {
1017 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1018 goto err;
1019 }
1020 x = NULL;
1021 }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langley1bea1732014-12-17 19:06:57 -08001023 i = ssl_verify_cert_chain(s, sk);
1024 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1025 al = ssl_verify_alarm_type(s->verify_result);
1026 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1027 SSL_R_CERTIFICATE_VERIFY_FAILED);
1028 goto f_err;
1029 }
1030 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001031
David Benjamin436bf822015-06-07 19:50:37 -04001032 X509 *leaf = sk_X509_value(sk, 0);
1033 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1034 al = SSL_AD_ILLEGAL_PARAMETER;
1035 goto f_err;
1036 }
1037
Adam Langley1bea1732014-12-17 19:06:57 -08001038 sc = ssl_sess_cert_new();
1039 if (sc == NULL) {
1040 goto err;
1041 }
Adam Langley95c29f32014-06-20 12:00:00 -07001042
David Benjamin2755a3e2015-04-22 16:17:58 -04001043 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001044 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjamin436bf822015-06-07 19:50:37 -04001046 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1047 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001048 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001049 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001050
David Benjaminb31040d2015-06-07 10:53:32 -04001051 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001052 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001053
David Benjamin2755a3e2015-04-22 16:17:58 -04001054 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001055 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001056
Adam Langley1bea1732014-12-17 19:06:57 -08001057 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langley1bea1732014-12-17 19:06:57 -08001059 ret = 1;
1060
1061 if (0) {
1062 f_err:
1063 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1064 }
1065
1066err:
1067 EVP_PKEY_free(pkey);
1068 X509_free(x);
1069 sk_X509_pop_free(sk, X509_free);
1070 return ret;
1071}
1072
1073int ssl3_get_server_key_exchange(SSL *s) {
1074 EVP_MD_CTX md_ctx;
1075 int al, ok;
1076 long n, alg_k, alg_a;
1077 EVP_PKEY *pkey = NULL;
1078 const EVP_MD *md = NULL;
1079 RSA *rsa = NULL;
1080 DH *dh = NULL;
1081 EC_KEY *ecdh = NULL;
1082 BN_CTX *bn_ctx = NULL;
1083 EC_POINT *srvr_ecpoint = NULL;
1084 CBS server_key_exchange, server_key_exchange_orig, parameter;
1085
1086 /* use same message size as in ssl3_get_certificate_request() as
1087 * ServerKeyExchange message may be skipped */
1088 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1089 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001090 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001091 if (!ok) {
1092 return n;
1093 }
1094
1095 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1096 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1097 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1098 SSL_R_UNEXPECTED_MESSAGE);
1099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1100 return -1;
1101 }
1102
1103 /* In plain PSK ciphersuite, ServerKeyExchange can be
1104 omitted if no identity hint is sent. Set session->sess_cert anyway to
1105 avoid problems later.*/
1106 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1107 /* PSK ciphersuites that also send a Certificate would have already
1108 * initialized |sess_cert|. */
1109 if (s->session->sess_cert == NULL) {
1110 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001111 if (s->session->sess_cert == NULL) {
1112 return -1;
1113 }
Adam Langley1bea1732014-12-17 19:06:57 -08001114 }
1115
1116 /* TODO(davidben): This should be reset in one place with the rest of the
1117 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001118 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1119 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001120 }
1121 s->s3->tmp.reuse_message = 1;
1122 return 1;
1123 }
1124
1125 /* Retain a copy of the original CBS to compute the signature over. */
1126 CBS_init(&server_key_exchange, s->init_msg, n);
1127 server_key_exchange_orig = server_key_exchange;
1128
1129 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001130 DH_free(s->session->sess_cert->peer_dh_tmp);
1131 s->session->sess_cert->peer_dh_tmp = NULL;
1132 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1133 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001134 } else {
1135 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001136 if (s->session->sess_cert == NULL) {
1137 return -1;
1138 }
Adam Langley1bea1732014-12-17 19:06:57 -08001139 }
1140
1141 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1142 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1143 EVP_MD_CTX_init(&md_ctx);
1144
1145 if (alg_a & SSL_aPSK) {
1146 CBS psk_identity_hint;
1147
1148 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1149 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1150 &psk_identity_hint)) {
1151 al = SSL_AD_DECODE_ERROR;
1152 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1153 goto f_err;
1154 }
1155
1156 /* Store PSK identity hint for later use, hint is used in
1157 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1158 * identity hint can be as long as the maximum length of a PSK identity.
1159 * Also do not allow NULL characters; identities are saved as C strings.
1160 *
1161 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1162 * a specific identity. */
1163 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1164 CBS_contains_zero_byte(&psk_identity_hint)) {
1165 al = SSL_AD_HANDSHAKE_FAILURE;
1166 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1167 SSL_R_DATA_LENGTH_TOO_LONG);
1168 goto f_err;
1169 }
1170
1171 /* Save the identity hint as a C string. */
1172 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1173 al = SSL_AD_INTERNAL_ERROR;
1174 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1175 ERR_R_MALLOC_FAILURE);
1176 goto f_err;
1177 }
1178 }
1179
David Benjamin7061e282015-03-19 11:10:48 -04001180 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001181 CBS dh_p, dh_g, dh_Ys;
1182
1183 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1184 CBS_len(&dh_p) == 0 ||
1185 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1186 CBS_len(&dh_g) == 0 ||
1187 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1188 CBS_len(&dh_Ys) == 0) {
1189 al = SSL_AD_DECODE_ERROR;
1190 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1191 goto f_err;
1192 }
1193
1194 dh = DH_new();
1195 if (dh == NULL) {
1196 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1197 goto err;
1198 }
1199
1200 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1201 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1202 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1203 NULL) {
1204 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1205 goto err;
1206 }
1207
Adam Langleya7997f12015-05-14 17:38:50 -07001208 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001209 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1210 SSL_R_BAD_DH_P_LENGTH);
1211 goto err;
1212 }
Adam Langley1bea1732014-12-17 19:06:57 -08001213 s->session->sess_cert->peer_dh_tmp = dh;
1214 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001215 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001216 uint16_t curve_id;
1217 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001218 const EC_GROUP *group;
1219 CBS point;
1220
1221 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1222 * key. Check curve is one of our preferences, if not server has sent an
1223 * invalid curve. */
1224 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1225 al = SSL_AD_DECODE_ERROR;
1226 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1227 goto f_err;
1228 }
1229
1230 curve_nid = tls1_ec_curve_id2nid(curve_id);
1231 if (curve_nid == 0) {
1232 al = SSL_AD_INTERNAL_ERROR;
1233 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1234 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1235 goto f_err;
1236 }
1237
David Benjamin4f7783e2015-03-05 03:05:11 -05001238 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001239 if (ecdh == NULL) {
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001241 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001242 goto err;
1243 }
1244
Adam Langley1bea1732014-12-17 19:06:57 -08001245 group = EC_KEY_get0_group(ecdh);
1246
1247 /* Next, get the encoded ECPoint */
1248 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1249 al = SSL_AD_DECODE_ERROR;
1250 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1251 goto f_err;
1252 }
1253
1254 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1255 ((bn_ctx = BN_CTX_new()) == NULL)) {
1256 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1257 ERR_R_MALLOC_FAILURE);
1258 goto err;
1259 }
1260
1261 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1262 CBS_len(&point), bn_ctx)) {
1263 al = SSL_AD_DECODE_ERROR;
1264 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1265 goto f_err;
1266 }
Adam Langley1bea1732014-12-17 19:06:57 -08001267 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1268 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1269 ecdh = NULL;
1270 BN_CTX_free(bn_ctx);
1271 bn_ctx = NULL;
1272 EC_POINT_free(srvr_ecpoint);
1273 srvr_ecpoint = NULL;
1274 } else if (!(alg_k & SSL_kPSK)) {
1275 al = SSL_AD_UNEXPECTED_MESSAGE;
1276 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1277 SSL_R_UNEXPECTED_MESSAGE);
1278 goto f_err;
1279 }
1280
1281 /* At this point, |server_key_exchange| contains the signature, if any, while
1282 * |server_key_exchange_orig| contains the entire message. From that, derive
1283 * a CBS containing just the parameter. */
1284 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1285 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1286
David Benjaminf4958e72015-06-07 11:00:53 -04001287 /* ServerKeyExchange should be signed by the server's public key. */
1288 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1289 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1290 if (pkey == NULL) {
1291 goto err;
1292 }
Adam Langley1bea1732014-12-17 19:06:57 -08001293
1294 if (SSL_USE_SIGALGS(s)) {
1295 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1296 goto f_err;
1297 }
1298 } else if (pkey->type == EVP_PKEY_RSA) {
1299 md = EVP_md5_sha1();
1300 } else {
1301 md = EVP_sha1();
1302 }
1303
1304 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001305 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001306 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1307 CBS_len(&server_key_exchange) != 0) {
1308 al = SSL_AD_DECODE_ERROR;
1309 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1310 goto f_err;
1311 }
1312
1313 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1314 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1315 SSL3_RANDOM_SIZE) ||
1316 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1317 SSL3_RANDOM_SIZE) ||
1318 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1319 CBS_len(&parameter)) ||
1320 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1321 CBS_len(&signature))) {
1322 /* bad signature */
1323 al = SSL_AD_DECRYPT_ERROR;
1324 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1325 goto f_err;
1326 }
1327 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001328 /* PSK ciphers are the only supported certificate-less ciphers. */
1329 assert(alg_a == SSL_aPSK);
1330
Adam Langley1bea1732014-12-17 19:06:57 -08001331 if (CBS_len(&server_key_exchange) > 0) {
1332 al = SSL_AD_DECODE_ERROR;
1333 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1334 SSL_R_EXTRA_DATA_IN_MESSAGE);
1335 goto f_err;
1336 }
1337 }
1338 EVP_PKEY_free(pkey);
1339 EVP_MD_CTX_cleanup(&md_ctx);
1340 return 1;
1341
Adam Langley95c29f32014-06-20 12:00:00 -07001342f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001343 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001344err:
Adam Langley1bea1732014-12-17 19:06:57 -08001345 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001346 RSA_free(rsa);
1347 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001348 BN_CTX_free(bn_ctx);
1349 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001350 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001351 EVP_MD_CTX_cleanup(&md_ctx);
1352 return -1;
1353}
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langley1bea1732014-12-17 19:06:57 -08001355static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1356 return X509_NAME_cmp(*a, *b);
1357}
Adam Langley95c29f32014-06-20 12:00:00 -07001358
Adam Langley1bea1732014-12-17 19:06:57 -08001359int ssl3_get_certificate_request(SSL *s) {
1360 int ok, ret = 0;
1361 unsigned long n;
1362 X509_NAME *xn = NULL;
1363 STACK_OF(X509_NAME) *ca_sk = NULL;
1364 CBS cbs;
1365 CBS certificate_types;
1366 CBS certificate_authorities;
1367 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001368
Adam Langley1bea1732014-12-17 19:06:57 -08001369 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1370 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001371 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001372
Adam Langley1bea1732014-12-17 19:06:57 -08001373 if (!ok) {
1374 return n;
1375 }
David Benjamin688d8df2014-11-02 23:06:42 -05001376
Adam Langley1bea1732014-12-17 19:06:57 -08001377 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langley1bea1732014-12-17 19:06:57 -08001379 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1380 s->s3->tmp.reuse_message = 1;
1381 /* If we get here we don't need any cached handshake records as we wont be
1382 * doing client auth. */
1383 if (s->s3->handshake_buffer &&
1384 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1385 goto err;
1386 }
1387 return 1;
1388 }
David Benjamined439582014-07-14 19:13:02 -04001389
Adam Langley1bea1732014-12-17 19:06:57 -08001390 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1391 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1392 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1393 SSL_R_WRONG_MESSAGE_TYPE);
1394 goto err;
1395 }
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langley1bea1732014-12-17 19:06:57 -08001397 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langley1bea1732014-12-17 19:06:57 -08001399 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1400 if (ca_sk == NULL) {
1401 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1402 goto err;
1403 }
Adam Langley95c29f32014-06-20 12:00:00 -07001404
Adam Langley1bea1732014-12-17 19:06:57 -08001405 /* get the certificate types */
1406 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1407 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1408 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1409 goto err;
1410 }
David Benjamined439582014-07-14 19:13:02 -04001411
Adam Langley1bea1732014-12-17 19:06:57 -08001412 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1413 &s->s3->tmp.num_certificate_types)) {
1414 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1415 goto err;
1416 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001417
Adam Langley1bea1732014-12-17 19:06:57 -08001418 if (SSL_USE_SIGALGS(s)) {
1419 CBS supported_signature_algorithms;
1420 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1421 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1422 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1423 goto err;
1424 }
David Benjamined439582014-07-14 19:13:02 -04001425
Adam Langley1bea1732014-12-17 19:06:57 -08001426 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1427 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1428 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1429 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1430 goto err;
1431 }
1432 }
David Benjamined439582014-07-14 19:13:02 -04001433
Adam Langley1bea1732014-12-17 19:06:57 -08001434 /* get the CA RDNs */
1435 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1436 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1437 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1438 goto err;
1439 }
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langley1bea1732014-12-17 19:06:57 -08001441 while (CBS_len(&certificate_authorities) > 0) {
1442 CBS distinguished_name;
1443 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1444 &distinguished_name)) {
1445 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1446 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1447 SSL_R_CA_DN_TOO_LONG);
1448 goto err;
1449 }
Adam Langley95c29f32014-06-20 12:00:00 -07001450
Adam Langley1bea1732014-12-17 19:06:57 -08001451 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001452
Adam Langley1bea1732014-12-17 19:06:57 -08001453 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1454 if (xn == NULL) {
1455 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1456 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1457 goto err;
1458 }
Adam Langley95c29f32014-06-20 12:00:00 -07001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1461 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001462 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001463 goto err;
1464 }
Adam Langley95c29f32014-06-20 12:00:00 -07001465
Adam Langley1bea1732014-12-17 19:06:57 -08001466 if (CBS_len(&distinguished_name) != 0) {
1467 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1468 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1469 SSL_R_CA_DN_LENGTH_MISMATCH);
1470 goto err;
1471 }
Adam Langley95c29f32014-06-20 12:00:00 -07001472
Adam Langley1bea1732014-12-17 19:06:57 -08001473 if (!sk_X509_NAME_push(ca_sk, xn)) {
1474 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1475 ERR_R_MALLOC_FAILURE);
1476 goto err;
1477 }
1478 }
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langley1bea1732014-12-17 19:06:57 -08001480 /* we should setup a certificate to return.... */
1481 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001482 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001483 s->s3->tmp.ca_names = ca_sk;
1484 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langley1bea1732014-12-17 19:06:57 -08001486 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langley1bea1732014-12-17 19:06:57 -08001488err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001489 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001490 return ret;
1491}
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langley1bea1732014-12-17 19:06:57 -08001493int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001494 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001495 long n;
1496 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001497
Adam Langley1bea1732014-12-17 19:06:57 -08001498 n = s->method->ssl_get_message(
1499 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001500 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001501
Adam Langley1bea1732014-12-17 19:06:57 -08001502 if (!ok) {
1503 return n;
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langley1bea1732014-12-17 19:06:57 -08001506 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001507
Adam Langley1bea1732014-12-17 19:06:57 -08001508 if (!CBS_get_u32(&new_session_ticket,
1509 &s->session->tlsext_tick_lifetime_hint) ||
1510 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1511 CBS_len(&new_session_ticket) != 0) {
1512 al = SSL_AD_DECODE_ERROR;
1513 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1514 goto f_err;
1515 }
Adam Langley95c29f32014-06-20 12:00:00 -07001516
Adam Langley1bea1732014-12-17 19:06:57 -08001517 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1518 &s->session->tlsext_ticklen)) {
1519 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1520 goto err;
1521 }
Adam Langley95c29f32014-06-20 12:00:00 -07001522
David Benjamin68070622015-02-08 23:44:59 -05001523 /* Generate a session ID for this session based on the session ticket. We use
1524 * the session ID mechanism for detecting ticket resumption. This also fits in
1525 * with assumptions elsewhere in OpenSSL.*/
1526 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1527 &s->session->session_id_length, EVP_sha256(), NULL)) {
1528 goto err;
1529 }
1530
1531 return 1;
David Benjamined439582014-07-14 19:13:02 -04001532
Adam Langley95c29f32014-06-20 12:00:00 -07001533f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001534 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001535err:
Adam Langley1bea1732014-12-17 19:06:57 -08001536 return -1;
1537}
Adam Langley95c29f32014-06-20 12:00:00 -07001538
Adam Langley1bea1732014-12-17 19:06:57 -08001539int ssl3_get_cert_status(SSL *s) {
1540 int ok, al;
1541 long n;
1542 CBS certificate_status, ocsp_response;
1543 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langley1bea1732014-12-17 19:06:57 -08001545 n = s->method->ssl_get_message(
1546 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001547 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001548
Adam Langley1bea1732014-12-17 19:06:57 -08001549 if (!ok) {
1550 return n;
1551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamindcd979f2015-04-20 18:26:52 -04001553 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1554 /* A server may send status_request in ServerHello and then change
1555 * its mind about sending CertificateStatus. */
1556 s->s3->tmp.reuse_message = 1;
1557 return 1;
1558 }
1559
Adam Langley1bea1732014-12-17 19:06:57 -08001560 CBS_init(&certificate_status, s->init_msg, n);
1561 if (!CBS_get_u8(&certificate_status, &status_type) ||
1562 status_type != TLSEXT_STATUSTYPE_ocsp ||
1563 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1564 CBS_len(&ocsp_response) == 0 ||
1565 CBS_len(&certificate_status) != 0) {
1566 al = SSL_AD_DECODE_ERROR;
1567 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1568 goto f_err;
1569 }
Adam Langley95c29f32014-06-20 12:00:00 -07001570
Adam Langley1bea1732014-12-17 19:06:57 -08001571 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1572 &s->session->ocsp_response_length)) {
1573 al = SSL_AD_INTERNAL_ERROR;
1574 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1575 goto f_err;
1576 }
1577 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langley95c29f32014-06-20 12:00:00 -07001579f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001580 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1581 return -1;
1582}
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langley1bea1732014-12-17 19:06:57 -08001584int ssl3_get_server_done(SSL *s) {
1585 int ok;
1586 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langley1bea1732014-12-17 19:06:57 -08001588 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1589 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1590 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001591 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langley1bea1732014-12-17 19:06:57 -08001593 if (!ok) {
1594 return n;
1595 }
David Benjamin46062682014-07-14 19:14:32 -04001596
Adam Langley1bea1732014-12-17 19:06:57 -08001597 if (n > 0) {
1598 /* should contain no data */
1599 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1600 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1601 return -1;
1602 }
David Benjamin46062682014-07-14 19:14:32 -04001603
Adam Langley1bea1732014-12-17 19:06:57 -08001604 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001605}
Adam Langley1258b6a2014-06-20 12:00:00 -07001606
Adam Langley1258b6a2014-06-20 12:00:00 -07001607
Adam Langley1bea1732014-12-17 19:06:57 -08001608int ssl3_send_client_key_exchange(SSL *s) {
1609 uint8_t *p;
1610 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001611 uint32_t alg_k;
1612 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001613 uint8_t *q;
1614 EVP_PKEY *pkey = NULL;
1615 EC_KEY *clnt_ecdh = NULL;
1616 const EC_POINT *srvr_ecpoint = NULL;
1617 EVP_PKEY *srvr_pub_pkey = NULL;
1618 uint8_t *encodedPoint = NULL;
1619 int encoded_pt_len = 0;
1620 BN_CTX *bn_ctx = NULL;
1621 unsigned int psk_len = 0;
1622 uint8_t psk[PSK_MAX_PSK_LEN];
1623 uint8_t *pms = NULL;
1624 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001625
Adam Langley1bea1732014-12-17 19:06:57 -08001626 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1627 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001628
Adam Langley1bea1732014-12-17 19:06:57 -08001629 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1630 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001631
Adam Langley1bea1732014-12-17 19:06:57 -08001632 /* If using a PSK key exchange, prepare the pre-shared key. */
1633 if (alg_a & SSL_aPSK) {
1634 char identity[PSK_MAX_IDENTITY_LEN + 1];
1635 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001636
Adam Langley1bea1732014-12-17 19:06:57 -08001637 if (s->psk_client_callback == NULL) {
1638 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1639 SSL_R_PSK_NO_CLIENT_CB);
1640 goto err;
1641 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001642
Adam Langley1bea1732014-12-17 19:06:57 -08001643 memset(identity, 0, sizeof(identity));
1644 psk_len =
1645 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1646 sizeof(identity), psk, sizeof(psk));
1647 if (psk_len > PSK_MAX_PSK_LEN) {
1648 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1649 ERR_R_INTERNAL_ERROR);
1650 goto err;
1651 } else if (psk_len == 0) {
1652 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1653 SSL_R_PSK_IDENTITY_NOT_FOUND);
1654 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1655 goto err;
1656 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001657
Adam Langley1bea1732014-12-17 19:06:57 -08001658 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1659 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1660 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1661 ERR_R_INTERNAL_ERROR);
1662 goto err;
1663 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001664
David Benjamin2755a3e2015-04-22 16:17:58 -04001665 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001666 s->session->psk_identity = BUF_strdup(identity);
1667 if (s->session->psk_identity == NULL) {
1668 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1669 ERR_R_MALLOC_FAILURE);
1670 goto err;
1671 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001672
Adam Langley1bea1732014-12-17 19:06:57 -08001673 /* Write out psk_identity. */
1674 s2n(identity_len, p);
1675 memcpy(p, identity, identity_len);
1676 p += identity_len;
1677 n = 2 + identity_len;
1678 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001679
Adam Langley1bea1732014-12-17 19:06:57 -08001680 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1681 if (alg_k & SSL_kRSA) {
1682 RSA *rsa;
1683 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001684
Adam Langley1bea1732014-12-17 19:06:57 -08001685 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1686 pms = OPENSSL_malloc(pms_len);
1687 if (pms == NULL) {
1688 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1689 ERR_R_MALLOC_FAILURE);
1690 goto err;
1691 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001692
Adam Langley1bea1732014-12-17 19:06:57 -08001693 if (s->session->sess_cert == NULL) {
1694 /* We should always have a server certificate with SSL_kRSA. */
1695 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1696 ERR_R_INTERNAL_ERROR);
1697 goto err;
1698 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001699
David Benjaminb31040d2015-06-07 10:53:32 -04001700 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001701 if (pkey == NULL ||
1702 pkey->type != EVP_PKEY_RSA ||
1703 pkey->pkey.rsa == NULL) {
1704 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1705 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001706 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001707 goto err;
1708 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001709
Adam Langley1bea1732014-12-17 19:06:57 -08001710 rsa = pkey->pkey.rsa;
1711 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001712
Adam Langley1bea1732014-12-17 19:06:57 -08001713 pms[0] = s->client_version >> 8;
1714 pms[1] = s->client_version & 0xff;
1715 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1716 goto err;
1717 }
1718
1719 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1720
1721 q = p;
1722 /* In TLS and beyond, reserve space for the length prefix. */
1723 if (s->version > SSL3_VERSION) {
1724 p += 2;
1725 n += 2;
1726 }
1727 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1728 RSA_PKCS1_PADDING)) {
1729 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1730 SSL_R_BAD_RSA_ENCRYPT);
1731 goto err;
1732 }
1733 n += enc_pms_len;
1734
1735 /* Log the premaster secret, if logging is enabled. */
1736 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1737 pms_len)) {
1738 goto err;
1739 }
1740
1741 /* Fill in the length prefix. */
1742 if (s->version > SSL3_VERSION) {
1743 s2n(enc_pms_len, q);
1744 }
David Benjamin7061e282015-03-19 11:10:48 -04001745 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001746 DH *dh_srvr, *dh_clnt;
1747 SESS_CERT *scert = s->session->sess_cert;
1748 int dh_len;
1749 size_t pub_len;
1750
1751 if (scert == NULL) {
1752 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1753 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1754 SSL_R_UNEXPECTED_MESSAGE);
1755 goto err;
1756 }
1757
1758 if (scert->peer_dh_tmp == NULL) {
1759 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1760 ERR_R_INTERNAL_ERROR);
1761 goto err;
1762 }
1763 dh_srvr = scert->peer_dh_tmp;
1764
1765 /* generate a new random key */
1766 dh_clnt = DHparams_dup(dh_srvr);
1767 if (dh_clnt == NULL) {
1768 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1769 goto err;
1770 }
1771 if (!DH_generate_key(dh_clnt)) {
1772 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1773 DH_free(dh_clnt);
1774 goto err;
1775 }
1776
1777 pms_len = DH_size(dh_clnt);
1778 pms = OPENSSL_malloc(pms_len);
1779 if (pms == NULL) {
1780 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1781 ERR_R_MALLOC_FAILURE);
1782 DH_free(dh_clnt);
1783 goto err;
1784 }
1785
1786 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1787 if (dh_len <= 0) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1789 DH_free(dh_clnt);
1790 goto err;
1791 }
1792 pms_len = dh_len;
1793
1794 /* send off the data */
1795 pub_len = BN_num_bytes(dh_clnt->pub_key);
1796 s2n(pub_len, p);
1797 BN_bn2bin(dh_clnt->pub_key, p);
1798 n += 2 + pub_len;
1799
1800 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001801 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001802 const EC_GROUP *srvr_group = NULL;
1803 EC_KEY *tkey;
1804 int field_size = 0, ecdh_len;
1805
1806 if (s->session->sess_cert == NULL) {
1807 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1808 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1809 SSL_R_UNEXPECTED_MESSAGE);
1810 goto err;
1811 }
1812
1813 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1814 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1815 ERR_R_INTERNAL_ERROR);
1816 goto err;
1817 }
1818
1819 tkey = s->session->sess_cert->peer_ecdh_tmp;
1820
1821 srvr_group = EC_KEY_get0_group(tkey);
1822 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1823 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1824 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1825 ERR_R_INTERNAL_ERROR);
1826 goto err;
1827 }
1828
1829 clnt_ecdh = EC_KEY_new();
1830 if (clnt_ecdh == NULL) {
1831 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1832 ERR_R_MALLOC_FAILURE);
1833 goto err;
1834 }
1835
1836 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1837 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1838 goto err;
1839 }
1840
1841 /* Generate a new ECDH key pair */
1842 if (!EC_KEY_generate_key(clnt_ecdh)) {
1843 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1844 goto err;
1845 }
1846
1847 field_size = EC_GROUP_get_degree(srvr_group);
1848 if (field_size <= 0) {
1849 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1850 goto err;
1851 }
1852
1853 pms_len = (field_size + 7) / 8;
1854 pms = OPENSSL_malloc(pms_len);
1855 if (pms == NULL) {
1856 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1857 ERR_R_MALLOC_FAILURE);
1858 goto err;
1859 }
1860
1861 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1862 if (ecdh_len <= 0) {
1863 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1864 goto err;
1865 }
1866 pms_len = ecdh_len;
1867
1868 /* First check the size of encoding and allocate memory accordingly. */
1869 encoded_pt_len =
1870 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1871 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1872
1873 encodedPoint =
1874 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1875 bn_ctx = BN_CTX_new();
1876 if (encodedPoint == NULL || bn_ctx == NULL) {
1877 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1878 ERR_R_MALLOC_FAILURE);
1879 goto err;
1880 }
1881
1882 /* Encode the public key */
1883 encoded_pt_len = EC_POINT_point2oct(
1884 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1885 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1886
1887 *p = encoded_pt_len; /* length of encoded point */
1888 /* Encoded point will be copied here */
1889 p += 1;
1890 n += 1;
1891 /* copy the point */
1892 memcpy(p, encodedPoint, encoded_pt_len);
1893 /* increment n to account for length field */
1894 n += encoded_pt_len;
1895
1896 /* Free allocated memory */
1897 BN_CTX_free(bn_ctx);
1898 bn_ctx = NULL;
1899 OPENSSL_free(encodedPoint);
1900 encodedPoint = NULL;
1901 EC_KEY_free(clnt_ecdh);
1902 clnt_ecdh = NULL;
1903 EVP_PKEY_free(srvr_pub_pkey);
1904 srvr_pub_pkey = NULL;
1905 } else if (alg_k & SSL_kPSK) {
1906 /* For plain PSK, other_secret is a block of 0s with the same length as
1907 * the pre-shared key. */
1908 pms_len = psk_len;
1909 pms = OPENSSL_malloc(pms_len);
1910 if (pms == NULL) {
1911 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1912 ERR_R_MALLOC_FAILURE);
1913 goto err;
1914 }
1915 memset(pms, 0, pms_len);
1916 } else {
1917 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1918 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1919 ERR_R_INTERNAL_ERROR);
1920 goto err;
1921 }
1922
1923 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1924 * key. */
1925 if (alg_a & SSL_aPSK) {
1926 CBB cbb, child;
1927 uint8_t *new_pms;
1928 size_t new_pms_len;
1929
1930 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1931 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1932 ERR_R_MALLOC_FAILURE);
1933 goto err;
1934 }
1935 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1936 !CBB_add_bytes(&child, pms, pms_len) ||
1937 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1938 !CBB_add_bytes(&child, psk, psk_len) ||
1939 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1940 CBB_cleanup(&cbb);
1941 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1942 ERR_R_INTERNAL_ERROR);
1943 goto err;
1944 }
1945 OPENSSL_cleanse(pms, pms_len);
1946 OPENSSL_free(pms);
1947 pms = new_pms;
1948 pms_len = new_pms_len;
1949 }
1950
1951 /* The message must be added to the finished hash before calculating the
1952 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001953 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1954 goto err;
1955 }
Adam Langley1bea1732014-12-17 19:06:57 -08001956 s->state = SSL3_ST_CW_KEY_EXCH_B;
1957
1958 s->session->master_key_length = s->enc_method->generate_master_secret(
1959 s, s->session->master_key, pms, pms_len);
1960 if (s->session->master_key_length == 0) {
1961 goto err;
1962 }
1963 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1964 OPENSSL_cleanse(pms, pms_len);
1965 OPENSSL_free(pms);
1966 }
1967
1968 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001969 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001970
1971err:
Adam Langley1bea1732014-12-17 19:06:57 -08001972 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001973 OPENSSL_free(encodedPoint);
1974 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001975 EVP_PKEY_free(srvr_pub_pkey);
1976 if (pms) {
1977 OPENSSL_cleanse(pms, pms_len);
1978 OPENSSL_free(pms);
1979 }
1980 return -1;
1981}
Adam Langley1258b6a2014-06-20 12:00:00 -07001982
Adam Langley1bea1732014-12-17 19:06:57 -08001983int ssl3_send_cert_verify(SSL *s) {
1984 uint8_t *buf, *p;
1985 const EVP_MD *md = NULL;
1986 uint8_t digest[EVP_MAX_MD_SIZE];
1987 size_t digest_length;
1988 EVP_PKEY *pkey;
1989 EVP_PKEY_CTX *pctx = NULL;
1990 size_t signature_length = 0;
1991 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001992
Adam Langley1bea1732014-12-17 19:06:57 -08001993 buf = (uint8_t *)s->init_buf->data;
1994
1995 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1996 p = ssl_handshake_start(s);
1997 pkey = s->cert->key->privatekey;
1998
1999 /* Write out the digest type if needbe. */
2000 if (SSL_USE_SIGALGS(s)) {
2001 md = tls1_choose_signing_digest(s, pkey);
2002 if (!tls12_get_sigandhash(p, pkey, md)) {
2003 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2004 goto err;
2005 }
2006 p += 2;
2007 n += 2;
2008 }
2009
2010 /* Compute the digest. */
2011 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2012 goto err;
2013 }
2014
2015 /* The handshake buffer is no longer necessary. */
2016 if (s->s3->handshake_buffer &&
2017 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2018 goto err;
2019 }
2020
2021 /* Sign the digest. */
2022 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2023 if (pctx == NULL) {
2024 goto err;
2025 }
2026
2027 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2028 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2029 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2030 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2031 goto err;
2032 }
2033
2034 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2035 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2036 goto err;
2037 }
2038
2039 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2040 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2041 goto err;
2042 }
2043
2044 s2n(signature_length, p);
2045 n += signature_length + 2;
2046
David Benjaminfbdfefb2015-02-16 19:33:53 -05002047 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2048 goto err;
2049 }
Adam Langley1bea1732014-12-17 19:06:57 -08002050 s->state = SSL3_ST_CW_CERT_VRFY_B;
2051 }
2052
2053 EVP_PKEY_CTX_free(pctx);
2054 return ssl_do_write(s);
2055
2056err:
2057 EVP_PKEY_CTX_free(pctx);
2058 return -1;
2059}
2060
2061/* ssl3_has_client_certificate returns true if a client certificate is
2062 * configured. */
2063static int ssl3_has_client_certificate(SSL *s) {
2064 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2065}
2066
2067int ssl3_send_client_certificate(SSL *s) {
2068 X509 *x509 = NULL;
2069 EVP_PKEY *pkey = NULL;
2070 int i;
2071
2072 if (s->state == SSL3_ST_CW_CERT_A) {
2073 /* Let cert callback update client certificates if required */
2074 if (s->cert->cert_cb) {
2075 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2076 if (i < 0) {
2077 s->rwstate = SSL_X509_LOOKUP;
2078 return -1;
2079 }
2080 if (i == 0) {
2081 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2082 return 0;
2083 }
2084 s->rwstate = SSL_NOTHING;
2085 }
2086
2087 if (ssl3_has_client_certificate(s)) {
2088 s->state = SSL3_ST_CW_CERT_C;
2089 } else {
2090 s->state = SSL3_ST_CW_CERT_B;
2091 }
2092 }
2093
2094 /* We need to get a client cert */
2095 if (s->state == SSL3_ST_CW_CERT_B) {
2096 /* If we get an error, we need to:
2097 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2098 * We then get retried later */
2099 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2100 if (i < 0) {
2101 s->rwstate = SSL_X509_LOOKUP;
2102 return -1;
2103 }
2104 s->rwstate = SSL_NOTHING;
2105 if (i == 1 && pkey != NULL && x509 != NULL) {
2106 s->state = SSL3_ST_CW_CERT_B;
2107 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2108 i = 0;
2109 }
2110 } else if (i == 1) {
2111 i = 0;
2112 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2113 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2114 }
2115
David Benjamin2755a3e2015-04-22 16:17:58 -04002116 X509_free(x509);
2117 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002118 if (i && !ssl3_has_client_certificate(s)) {
2119 i = 0;
2120 }
2121 if (i == 0) {
2122 if (s->version == SSL3_VERSION) {
2123 s->s3->tmp.cert_req = 0;
2124 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2125 return 1;
2126 } else {
2127 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002128 /* There is no client certificate, so the handshake buffer may be
2129 * released. */
2130 if (s->s3->handshake_buffer &&
2131 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2132 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2133 return -1;
2134 }
Adam Langley1bea1732014-12-17 19:06:57 -08002135 }
2136 }
2137
2138 /* Ok, we have a cert */
2139 s->state = SSL3_ST_CW_CERT_C;
2140 }
2141
2142 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002143 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2144 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2145 return -1;
2146 }
Adam Langley1bea1732014-12-17 19:06:57 -08002147 }
2148
2149 /* SSL3_ST_CW_CERT_D */
2150 return ssl_do_write(s);
2151}
2152
Adam Langley1bea1732014-12-17 19:06:57 -08002153int ssl3_send_next_proto(SSL *s) {
2154 unsigned int len, padding_len;
2155 uint8_t *d, *p;
2156
2157 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2158 len = s->next_proto_negotiated_len;
2159 padding_len = 32 - ((len + 2) % 32);
2160
2161 d = p = ssl_handshake_start(s);
2162 *(p++) = len;
2163 memcpy(p, s->next_proto_negotiated, len);
2164 p += len;
2165 *(p++) = padding_len;
2166 memset(p, 0, padding_len);
2167 p += padding_len;
2168
David Benjaminfbdfefb2015-02-16 19:33:53 -05002169 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2170 return -1;
2171 }
Adam Langley1bea1732014-12-17 19:06:57 -08002172 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2173 }
2174
2175 return ssl_do_write(s);
2176}
2177
2178int ssl3_send_channel_id(SSL *s) {
2179 uint8_t *d;
2180 int ret = -1, public_key_len;
2181 EVP_MD_CTX md_ctx;
2182 size_t sig_len;
2183 ECDSA_SIG *sig = NULL;
2184 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2185
2186 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2187 return ssl_do_write(s);
2188 }
2189
2190 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2191 EVP_PKEY *key = NULL;
2192 s->ctx->channel_id_cb(s, &key);
2193 if (key != NULL) {
2194 s->tlsext_channel_id_private = key;
2195 }
2196 }
2197
2198 if (!s->tlsext_channel_id_private) {
2199 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2200 return -1;
2201 }
2202 s->rwstate = SSL_NOTHING;
2203
2204 d = ssl_handshake_start(s);
2205 if (s->s3->tlsext_channel_id_new) {
2206 s2n(TLSEXT_TYPE_channel_id_new, d);
2207 } else {
2208 s2n(TLSEXT_TYPE_channel_id, d);
2209 }
2210 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2211
2212 EVP_MD_CTX_init(&md_ctx);
2213
2214 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2215 if (public_key_len <= 0) {
2216 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2217 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2218 goto err;
2219 }
2220
2221 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2222 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2223 * field elements as 32-byte, big-endian numbers. */
2224 if (public_key_len != 65) {
2225 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2226 goto err;
2227 }
2228 public_key = OPENSSL_malloc(public_key_len);
2229 if (!public_key) {
2230 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2231 goto err;
2232 }
2233
2234 derp = public_key;
2235 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2236
2237 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2238 s->tlsext_channel_id_private) != 1) {
2239 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2240 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2241 goto err;
2242 }
2243
2244 if (!tls1_channel_id_hash(&md_ctx, s)) {
2245 goto err;
2246 }
2247
2248 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2249 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2250 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2251 goto err;
2252 }
2253
2254 der_sig = OPENSSL_malloc(sig_len);
2255 if (!der_sig) {
2256 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2257 goto err;
2258 }
2259
2260 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2261 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2262 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2263 goto err;
2264 }
2265
2266 derp = der_sig;
2267 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2268 if (sig == NULL) {
2269 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2270 goto err;
2271 }
2272
2273 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2274 memcpy(d, public_key + 1, 64);
2275 d += 64;
2276 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2277 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2278 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2279 goto err;
2280 }
2281
David Benjaminfbdfefb2015-02-16 19:33:53 -05002282 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2283 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2284 goto err;
2285 }
Adam Langley1bea1732014-12-17 19:06:57 -08002286 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2287
2288 ret = ssl_do_write(s);
2289
2290err:
2291 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002292 OPENSSL_free(public_key);
2293 OPENSSL_free(der_sig);
2294 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002295
2296 return ret;
2297}
2298
2299int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2300 int i = 0;
2301 if (s->ctx->client_cert_cb) {
2302 i = s->ctx->client_cert_cb(s, px509, ppkey);
2303 }
2304 return i;
2305}