blob: f0c36efde12f92b44abf6b08b7a3764aab9c2fac [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);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400920 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400921 goto err;
922 }
923
924 /* Check the certificate's type matches the cipher. */
925 int cert_type = ssl_cert_type(pkey);
926 if (cert_type < 0) {
927 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
928 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
929 goto err;
930 }
931 int expected_type = ssl_cipher_get_cert_index(cipher);
932 assert(expected_type >= 0);
933 if (cert_type != expected_type) {
934 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
935 SSL_R_WRONG_CERTIFICATE_TYPE);
936 goto err;
937 }
938
939 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
940 * be checked in other cases as well? */
941 if (cipher->algorithm_auth & SSL_aECDSA) {
942 /* This call populates the ex_flags field correctly */
943 X509_check_purpose(leaf, -1, 0);
944 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
945 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
946 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
947 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
948 goto err;
949 }
950 }
951
952 ret = 1;
953
954err:
955 EVP_PKEY_free(pkey);
956 return ret;
957}
958
Adam Langley1bea1732014-12-17 19:06:57 -0800959int ssl3_get_server_certificate(SSL *s) {
960 int al, i, ok, ret = -1;
961 unsigned long n;
962 X509 *x = NULL;
963 STACK_OF(X509) *sk = NULL;
964 SESS_CERT *sc;
965 EVP_PKEY *pkey = NULL;
966 CBS cbs, certificate_list;
967 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langley1bea1732014-12-17 19:06:57 -0800969 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400970 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500971 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700972
Adam Langley1bea1732014-12-17 19:06:57 -0800973 if (!ok) {
974 return n;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langley1bea1732014-12-17 19:06:57 -0800977 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langley1bea1732014-12-17 19:06:57 -0800979 sk = sk_X509_new_null();
980 if (sk == NULL) {
981 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
982 goto err;
983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langley1bea1732014-12-17 19:06:57 -0800985 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400986 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800987 CBS_len(&cbs) != 0) {
988 al = SSL_AD_DECODE_ERROR;
David Benjamin8923c0b2015-06-07 11:42:34 -0400989 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800990 goto f_err;
991 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400992
Adam Langley1bea1732014-12-17 19:06:57 -0800993 while (CBS_len(&certificate_list) > 0) {
994 CBS certificate;
995 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
996 al = SSL_AD_DECODE_ERROR;
997 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
998 SSL_R_CERT_LENGTH_MISMATCH);
999 goto f_err;
1000 }
1001 data = CBS_data(&certificate);
1002 x = d2i_X509(NULL, &data, CBS_len(&certificate));
1003 if (x == NULL) {
1004 al = SSL_AD_BAD_CERTIFICATE;
1005 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1006 goto f_err;
1007 }
1008 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1009 al = SSL_AD_DECODE_ERROR;
1010 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1011 SSL_R_CERT_LENGTH_MISMATCH);
1012 goto f_err;
1013 }
1014 if (!sk_X509_push(sk, x)) {
1015 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1016 goto err;
1017 }
1018 x = NULL;
1019 }
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langley1bea1732014-12-17 19:06:57 -08001021 i = ssl_verify_cert_chain(s, sk);
1022 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1023 al = ssl_verify_alarm_type(s->verify_result);
1024 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1025 SSL_R_CERTIFICATE_VERIFY_FAILED);
1026 goto f_err;
1027 }
1028 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001029
David Benjamin436bf822015-06-07 19:50:37 -04001030 X509 *leaf = sk_X509_value(sk, 0);
1031 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1032 al = SSL_AD_ILLEGAL_PARAMETER;
1033 goto f_err;
1034 }
1035
Adam Langley1bea1732014-12-17 19:06:57 -08001036 sc = ssl_sess_cert_new();
1037 if (sc == NULL) {
1038 goto err;
1039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
David Benjamin2755a3e2015-04-22 16:17:58 -04001041 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001042 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
David Benjamin436bf822015-06-07 19:50:37 -04001044 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1045 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001046 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001047 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001048
David Benjaminb31040d2015-06-07 10:53:32 -04001049 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001050 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001051
David Benjamin2755a3e2015-04-22 16:17:58 -04001052 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001053 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001054
Adam Langley1bea1732014-12-17 19:06:57 -08001055 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langley1bea1732014-12-17 19:06:57 -08001057 ret = 1;
1058
1059 if (0) {
1060 f_err:
1061 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1062 }
1063
1064err:
1065 EVP_PKEY_free(pkey);
1066 X509_free(x);
1067 sk_X509_pop_free(sk, X509_free);
1068 return ret;
1069}
1070
1071int ssl3_get_server_key_exchange(SSL *s) {
1072 EVP_MD_CTX md_ctx;
1073 int al, ok;
1074 long n, alg_k, alg_a;
1075 EVP_PKEY *pkey = NULL;
1076 const EVP_MD *md = NULL;
1077 RSA *rsa = NULL;
1078 DH *dh = NULL;
1079 EC_KEY *ecdh = NULL;
1080 BN_CTX *bn_ctx = NULL;
1081 EC_POINT *srvr_ecpoint = NULL;
1082 CBS server_key_exchange, server_key_exchange_orig, parameter;
1083
1084 /* use same message size as in ssl3_get_certificate_request() as
1085 * ServerKeyExchange message may be skipped */
1086 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1087 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001088 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001089 if (!ok) {
1090 return n;
1091 }
1092
1093 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1094 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1095 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1096 SSL_R_UNEXPECTED_MESSAGE);
1097 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1098 return -1;
1099 }
1100
1101 /* In plain PSK ciphersuite, ServerKeyExchange can be
1102 omitted if no identity hint is sent. Set session->sess_cert anyway to
1103 avoid problems later.*/
1104 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1105 /* PSK ciphersuites that also send a Certificate would have already
1106 * initialized |sess_cert|. */
1107 if (s->session->sess_cert == NULL) {
1108 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001109 if (s->session->sess_cert == NULL) {
1110 return -1;
1111 }
Adam Langley1bea1732014-12-17 19:06:57 -08001112 }
1113
1114 /* TODO(davidben): This should be reset in one place with the rest of the
1115 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001116 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1117 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001118 }
1119 s->s3->tmp.reuse_message = 1;
1120 return 1;
1121 }
1122
1123 /* Retain a copy of the original CBS to compute the signature over. */
1124 CBS_init(&server_key_exchange, s->init_msg, n);
1125 server_key_exchange_orig = server_key_exchange;
1126
1127 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001128 DH_free(s->session->sess_cert->peer_dh_tmp);
1129 s->session->sess_cert->peer_dh_tmp = NULL;
1130 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1131 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001132 } else {
1133 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001134 if (s->session->sess_cert == NULL) {
1135 return -1;
1136 }
Adam Langley1bea1732014-12-17 19:06:57 -08001137 }
1138
1139 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1140 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1141 EVP_MD_CTX_init(&md_ctx);
1142
1143 if (alg_a & SSL_aPSK) {
1144 CBS psk_identity_hint;
1145
1146 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1147 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1148 &psk_identity_hint)) {
1149 al = SSL_AD_DECODE_ERROR;
1150 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1151 goto f_err;
1152 }
1153
1154 /* Store PSK identity hint for later use, hint is used in
1155 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1156 * identity hint can be as long as the maximum length of a PSK identity.
1157 * Also do not allow NULL characters; identities are saved as C strings.
1158 *
1159 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1160 * a specific identity. */
1161 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1162 CBS_contains_zero_byte(&psk_identity_hint)) {
1163 al = SSL_AD_HANDSHAKE_FAILURE;
1164 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1165 SSL_R_DATA_LENGTH_TOO_LONG);
1166 goto f_err;
1167 }
1168
1169 /* Save the identity hint as a C string. */
1170 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1171 al = SSL_AD_INTERNAL_ERROR;
1172 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1173 ERR_R_MALLOC_FAILURE);
1174 goto f_err;
1175 }
1176 }
1177
David Benjamin7061e282015-03-19 11:10:48 -04001178 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001179 CBS dh_p, dh_g, dh_Ys;
1180
1181 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1182 CBS_len(&dh_p) == 0 ||
1183 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1184 CBS_len(&dh_g) == 0 ||
1185 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1186 CBS_len(&dh_Ys) == 0) {
1187 al = SSL_AD_DECODE_ERROR;
1188 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1189 goto f_err;
1190 }
1191
1192 dh = DH_new();
1193 if (dh == NULL) {
1194 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1195 goto err;
1196 }
1197
1198 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1199 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1200 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1201 NULL) {
1202 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1203 goto err;
1204 }
1205
Adam Langleya7997f12015-05-14 17:38:50 -07001206 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001207 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1208 SSL_R_BAD_DH_P_LENGTH);
1209 goto err;
1210 }
Adam Langley1bea1732014-12-17 19:06:57 -08001211 s->session->sess_cert->peer_dh_tmp = dh;
1212 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001213 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001214 uint16_t curve_id;
1215 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001216 const EC_GROUP *group;
1217 CBS point;
1218
1219 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1220 * key. Check curve is one of our preferences, if not server has sent an
1221 * invalid curve. */
1222 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1223 al = SSL_AD_DECODE_ERROR;
1224 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1225 goto f_err;
1226 }
1227
1228 curve_nid = tls1_ec_curve_id2nid(curve_id);
1229 if (curve_nid == 0) {
1230 al = SSL_AD_INTERNAL_ERROR;
1231 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1232 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1233 goto f_err;
1234 }
1235
David Benjamin4f7783e2015-03-05 03:05:11 -05001236 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001237 if (ecdh == NULL) {
1238 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001239 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001240 goto err;
1241 }
1242
Adam Langley1bea1732014-12-17 19:06:57 -08001243 group = EC_KEY_get0_group(ecdh);
1244
1245 /* Next, get the encoded ECPoint */
1246 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1247 al = SSL_AD_DECODE_ERROR;
1248 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1249 goto f_err;
1250 }
1251
1252 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1253 ((bn_ctx = BN_CTX_new()) == NULL)) {
1254 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1255 ERR_R_MALLOC_FAILURE);
1256 goto err;
1257 }
1258
1259 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1260 CBS_len(&point), bn_ctx)) {
1261 al = SSL_AD_DECODE_ERROR;
1262 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1263 goto f_err;
1264 }
Adam Langley1bea1732014-12-17 19:06:57 -08001265 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1266 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1267 ecdh = NULL;
1268 BN_CTX_free(bn_ctx);
1269 bn_ctx = NULL;
1270 EC_POINT_free(srvr_ecpoint);
1271 srvr_ecpoint = NULL;
1272 } else if (!(alg_k & SSL_kPSK)) {
1273 al = SSL_AD_UNEXPECTED_MESSAGE;
1274 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1275 SSL_R_UNEXPECTED_MESSAGE);
1276 goto f_err;
1277 }
1278
1279 /* At this point, |server_key_exchange| contains the signature, if any, while
1280 * |server_key_exchange_orig| contains the entire message. From that, derive
1281 * a CBS containing just the parameter. */
1282 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1283 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1284
David Benjaminf4958e72015-06-07 11:00:53 -04001285 /* ServerKeyExchange should be signed by the server's public key. */
1286 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1287 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1288 if (pkey == NULL) {
1289 goto err;
1290 }
Adam Langley1bea1732014-12-17 19:06:57 -08001291
1292 if (SSL_USE_SIGALGS(s)) {
1293 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1294 goto f_err;
1295 }
1296 } else if (pkey->type == EVP_PKEY_RSA) {
1297 md = EVP_md5_sha1();
1298 } else {
1299 md = EVP_sha1();
1300 }
1301
1302 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001303 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001304 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1305 CBS_len(&server_key_exchange) != 0) {
1306 al = SSL_AD_DECODE_ERROR;
1307 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1308 goto f_err;
1309 }
1310
1311 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1312 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1313 SSL3_RANDOM_SIZE) ||
1314 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1315 SSL3_RANDOM_SIZE) ||
1316 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1317 CBS_len(&parameter)) ||
1318 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1319 CBS_len(&signature))) {
1320 /* bad signature */
1321 al = SSL_AD_DECRYPT_ERROR;
1322 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1323 goto f_err;
1324 }
1325 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001326 /* PSK ciphers are the only supported certificate-less ciphers. */
1327 assert(alg_a == SSL_aPSK);
1328
Adam Langley1bea1732014-12-17 19:06:57 -08001329 if (CBS_len(&server_key_exchange) > 0) {
1330 al = SSL_AD_DECODE_ERROR;
1331 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1332 SSL_R_EXTRA_DATA_IN_MESSAGE);
1333 goto f_err;
1334 }
1335 }
1336 EVP_PKEY_free(pkey);
1337 EVP_MD_CTX_cleanup(&md_ctx);
1338 return 1;
1339
Adam Langley95c29f32014-06-20 12:00:00 -07001340f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001341 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001342err:
Adam Langley1bea1732014-12-17 19:06:57 -08001343 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001344 RSA_free(rsa);
1345 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001346 BN_CTX_free(bn_ctx);
1347 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001348 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001349 EVP_MD_CTX_cleanup(&md_ctx);
1350 return -1;
1351}
Adam Langley95c29f32014-06-20 12:00:00 -07001352
Adam Langley1bea1732014-12-17 19:06:57 -08001353static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1354 return X509_NAME_cmp(*a, *b);
1355}
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langley1bea1732014-12-17 19:06:57 -08001357int ssl3_get_certificate_request(SSL *s) {
1358 int ok, ret = 0;
1359 unsigned long n;
1360 X509_NAME *xn = NULL;
1361 STACK_OF(X509_NAME) *ca_sk = NULL;
1362 CBS cbs;
1363 CBS certificate_types;
1364 CBS certificate_authorities;
1365 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001366
Adam Langley1bea1732014-12-17 19:06:57 -08001367 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1368 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001369 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001370
Adam Langley1bea1732014-12-17 19:06:57 -08001371 if (!ok) {
1372 return n;
1373 }
David Benjamin688d8df2014-11-02 23:06:42 -05001374
Adam Langley1bea1732014-12-17 19:06:57 -08001375 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langley1bea1732014-12-17 19:06:57 -08001377 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1378 s->s3->tmp.reuse_message = 1;
1379 /* If we get here we don't need any cached handshake records as we wont be
1380 * doing client auth. */
1381 if (s->s3->handshake_buffer &&
1382 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1383 goto err;
1384 }
1385 return 1;
1386 }
David Benjamined439582014-07-14 19:13:02 -04001387
Adam Langley1bea1732014-12-17 19:06:57 -08001388 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1389 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1390 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1391 SSL_R_WRONG_MESSAGE_TYPE);
1392 goto err;
1393 }
Adam Langley95c29f32014-06-20 12:00:00 -07001394
Adam Langley1bea1732014-12-17 19:06:57 -08001395 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langley1bea1732014-12-17 19:06:57 -08001397 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1398 if (ca_sk == NULL) {
1399 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1400 goto err;
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langley1bea1732014-12-17 19:06:57 -08001403 /* get the certificate types */
1404 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1405 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1406 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1407 goto err;
1408 }
David Benjamined439582014-07-14 19:13:02 -04001409
Adam Langley1bea1732014-12-17 19:06:57 -08001410 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1411 &s->s3->tmp.num_certificate_types)) {
1412 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1413 goto err;
1414 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001415
Adam Langley1bea1732014-12-17 19:06:57 -08001416 if (SSL_USE_SIGALGS(s)) {
1417 CBS supported_signature_algorithms;
1418 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1420 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1421 goto err;
1422 }
David Benjamined439582014-07-14 19:13:02 -04001423
Adam Langley1bea1732014-12-17 19:06:57 -08001424 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1425 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1426 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1427 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1428 goto err;
1429 }
1430 }
David Benjamined439582014-07-14 19:13:02 -04001431
Adam Langley1bea1732014-12-17 19:06:57 -08001432 /* get the CA RDNs */
1433 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1434 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1435 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1436 goto err;
1437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 while (CBS_len(&certificate_authorities) > 0) {
1440 CBS distinguished_name;
1441 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1442 &distinguished_name)) {
1443 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1444 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1445 SSL_R_CA_DN_TOO_LONG);
1446 goto err;
1447 }
Adam Langley95c29f32014-06-20 12:00:00 -07001448
Adam Langley1bea1732014-12-17 19:06:57 -08001449 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001450
Adam Langley1bea1732014-12-17 19:06:57 -08001451 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1452 if (xn == NULL) {
1453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1454 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1455 goto err;
1456 }
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley1bea1732014-12-17 19:06:57 -08001458 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1459 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001460 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001461 goto err;
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langley1bea1732014-12-17 19:06:57 -08001464 if (CBS_len(&distinguished_name) != 0) {
1465 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1466 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1467 SSL_R_CA_DN_LENGTH_MISMATCH);
1468 goto err;
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
Adam Langley1bea1732014-12-17 19:06:57 -08001471 if (!sk_X509_NAME_push(ca_sk, xn)) {
1472 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1473 ERR_R_MALLOC_FAILURE);
1474 goto err;
1475 }
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langley1bea1732014-12-17 19:06:57 -08001478 /* we should setup a certificate to return.... */
1479 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001480 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001481 s->s3->tmp.ca_names = ca_sk;
1482 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langley1bea1732014-12-17 19:06:57 -08001484 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langley1bea1732014-12-17 19:06:57 -08001486err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001487 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001488 return ret;
1489}
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langley1bea1732014-12-17 19:06:57 -08001491int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001492 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001493 long n;
1494 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001495
Adam Langley1bea1732014-12-17 19:06:57 -08001496 n = s->method->ssl_get_message(
1497 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001498 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langley1bea1732014-12-17 19:06:57 -08001500 if (!ok) {
1501 return n;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
Adam Langley1bea1732014-12-17 19:06:57 -08001504 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001505
Adam Langley1bea1732014-12-17 19:06:57 -08001506 if (!CBS_get_u32(&new_session_ticket,
1507 &s->session->tlsext_tick_lifetime_hint) ||
1508 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1509 CBS_len(&new_session_ticket) != 0) {
1510 al = SSL_AD_DECODE_ERROR;
1511 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1512 goto f_err;
1513 }
Adam Langley95c29f32014-06-20 12:00:00 -07001514
Adam Langley1bea1732014-12-17 19:06:57 -08001515 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1516 &s->session->tlsext_ticklen)) {
1517 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1518 goto err;
1519 }
Adam Langley95c29f32014-06-20 12:00:00 -07001520
David Benjamin68070622015-02-08 23:44:59 -05001521 /* Generate a session ID for this session based on the session ticket. We use
1522 * the session ID mechanism for detecting ticket resumption. This also fits in
1523 * with assumptions elsewhere in OpenSSL.*/
1524 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1525 &s->session->session_id_length, EVP_sha256(), NULL)) {
1526 goto err;
1527 }
1528
1529 return 1;
David Benjamined439582014-07-14 19:13:02 -04001530
Adam Langley95c29f32014-06-20 12:00:00 -07001531f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001532 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001533err:
Adam Langley1bea1732014-12-17 19:06:57 -08001534 return -1;
1535}
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langley1bea1732014-12-17 19:06:57 -08001537int ssl3_get_cert_status(SSL *s) {
1538 int ok, al;
1539 long n;
1540 CBS certificate_status, ocsp_response;
1541 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001542
Adam Langley1bea1732014-12-17 19:06:57 -08001543 n = s->method->ssl_get_message(
1544 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001545 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langley1bea1732014-12-17 19:06:57 -08001547 if (!ok) {
1548 return n;
1549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
David Benjamindcd979f2015-04-20 18:26:52 -04001551 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1552 /* A server may send status_request in ServerHello and then change
1553 * its mind about sending CertificateStatus. */
1554 s->s3->tmp.reuse_message = 1;
1555 return 1;
1556 }
1557
Adam Langley1bea1732014-12-17 19:06:57 -08001558 CBS_init(&certificate_status, s->init_msg, n);
1559 if (!CBS_get_u8(&certificate_status, &status_type) ||
1560 status_type != TLSEXT_STATUSTYPE_ocsp ||
1561 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1562 CBS_len(&ocsp_response) == 0 ||
1563 CBS_len(&certificate_status) != 0) {
1564 al = SSL_AD_DECODE_ERROR;
1565 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1566 goto f_err;
1567 }
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langley1bea1732014-12-17 19:06:57 -08001569 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1570 &s->session->ocsp_response_length)) {
1571 al = SSL_AD_INTERNAL_ERROR;
1572 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1573 goto f_err;
1574 }
1575 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Adam Langley95c29f32014-06-20 12:00:00 -07001577f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001578 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1579 return -1;
1580}
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langley1bea1732014-12-17 19:06:57 -08001582int ssl3_get_server_done(SSL *s) {
1583 int ok;
1584 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langley1bea1732014-12-17 19:06:57 -08001586 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1587 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1588 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001589 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001590
Adam Langley1bea1732014-12-17 19:06:57 -08001591 if (!ok) {
1592 return n;
1593 }
David Benjamin46062682014-07-14 19:14:32 -04001594
Adam Langley1bea1732014-12-17 19:06:57 -08001595 if (n > 0) {
1596 /* should contain no data */
1597 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1598 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1599 return -1;
1600 }
David Benjamin46062682014-07-14 19:14:32 -04001601
Adam Langley1bea1732014-12-17 19:06:57 -08001602 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001603}
Adam Langley1258b6a2014-06-20 12:00:00 -07001604
Adam Langley1258b6a2014-06-20 12:00:00 -07001605
Adam Langley1bea1732014-12-17 19:06:57 -08001606int ssl3_send_client_key_exchange(SSL *s) {
1607 uint8_t *p;
1608 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001609 uint32_t alg_k;
1610 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001611 uint8_t *q;
1612 EVP_PKEY *pkey = NULL;
1613 EC_KEY *clnt_ecdh = NULL;
1614 const EC_POINT *srvr_ecpoint = NULL;
1615 EVP_PKEY *srvr_pub_pkey = NULL;
1616 uint8_t *encodedPoint = NULL;
1617 int encoded_pt_len = 0;
1618 BN_CTX *bn_ctx = NULL;
1619 unsigned int psk_len = 0;
1620 uint8_t psk[PSK_MAX_PSK_LEN];
1621 uint8_t *pms = NULL;
1622 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1625 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1628 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001629
Adam Langley1bea1732014-12-17 19:06:57 -08001630 /* If using a PSK key exchange, prepare the pre-shared key. */
1631 if (alg_a & SSL_aPSK) {
1632 char identity[PSK_MAX_IDENTITY_LEN + 1];
1633 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001634
Adam Langley1bea1732014-12-17 19:06:57 -08001635 if (s->psk_client_callback == NULL) {
1636 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1637 SSL_R_PSK_NO_CLIENT_CB);
1638 goto err;
1639 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001640
Adam Langley1bea1732014-12-17 19:06:57 -08001641 memset(identity, 0, sizeof(identity));
1642 psk_len =
1643 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1644 sizeof(identity), psk, sizeof(psk));
1645 if (psk_len > PSK_MAX_PSK_LEN) {
1646 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1647 ERR_R_INTERNAL_ERROR);
1648 goto err;
1649 } else if (psk_len == 0) {
1650 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1651 SSL_R_PSK_IDENTITY_NOT_FOUND);
1652 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1653 goto err;
1654 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001655
Adam Langley1bea1732014-12-17 19:06:57 -08001656 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1657 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1658 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1659 ERR_R_INTERNAL_ERROR);
1660 goto err;
1661 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
David Benjamin2755a3e2015-04-22 16:17:58 -04001663 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001664 s->session->psk_identity = BUF_strdup(identity);
1665 if (s->session->psk_identity == NULL) {
1666 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1667 ERR_R_MALLOC_FAILURE);
1668 goto err;
1669 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langley1bea1732014-12-17 19:06:57 -08001671 /* Write out psk_identity. */
1672 s2n(identity_len, p);
1673 memcpy(p, identity, identity_len);
1674 p += identity_len;
1675 n = 2 + identity_len;
1676 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001677
Adam Langley1bea1732014-12-17 19:06:57 -08001678 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1679 if (alg_k & SSL_kRSA) {
1680 RSA *rsa;
1681 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001682
Adam Langley1bea1732014-12-17 19:06:57 -08001683 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1684 pms = OPENSSL_malloc(pms_len);
1685 if (pms == NULL) {
1686 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1687 ERR_R_MALLOC_FAILURE);
1688 goto err;
1689 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001690
Adam Langley1bea1732014-12-17 19:06:57 -08001691 if (s->session->sess_cert == NULL) {
1692 /* We should always have a server certificate with SSL_kRSA. */
1693 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1694 ERR_R_INTERNAL_ERROR);
1695 goto err;
1696 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001697
David Benjaminb31040d2015-06-07 10:53:32 -04001698 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001699 if (pkey == NULL ||
1700 pkey->type != EVP_PKEY_RSA ||
1701 pkey->pkey.rsa == NULL) {
1702 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1703 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001704 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001705 goto err;
1706 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001707
Adam Langley1bea1732014-12-17 19:06:57 -08001708 rsa = pkey->pkey.rsa;
1709 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001710
Adam Langley1bea1732014-12-17 19:06:57 -08001711 pms[0] = s->client_version >> 8;
1712 pms[1] = s->client_version & 0xff;
1713 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1714 goto err;
1715 }
1716
1717 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1718
1719 q = p;
1720 /* In TLS and beyond, reserve space for the length prefix. */
1721 if (s->version > SSL3_VERSION) {
1722 p += 2;
1723 n += 2;
1724 }
1725 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1726 RSA_PKCS1_PADDING)) {
1727 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1728 SSL_R_BAD_RSA_ENCRYPT);
1729 goto err;
1730 }
1731 n += enc_pms_len;
1732
1733 /* Log the premaster secret, if logging is enabled. */
1734 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1735 pms_len)) {
1736 goto err;
1737 }
1738
1739 /* Fill in the length prefix. */
1740 if (s->version > SSL3_VERSION) {
1741 s2n(enc_pms_len, q);
1742 }
David Benjamin7061e282015-03-19 11:10:48 -04001743 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001744 DH *dh_srvr, *dh_clnt;
1745 SESS_CERT *scert = s->session->sess_cert;
1746 int dh_len;
1747 size_t pub_len;
1748
1749 if (scert == NULL) {
1750 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1751 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1752 SSL_R_UNEXPECTED_MESSAGE);
1753 goto err;
1754 }
1755
1756 if (scert->peer_dh_tmp == NULL) {
1757 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1758 ERR_R_INTERNAL_ERROR);
1759 goto err;
1760 }
1761 dh_srvr = scert->peer_dh_tmp;
1762
1763 /* generate a new random key */
1764 dh_clnt = DHparams_dup(dh_srvr);
1765 if (dh_clnt == NULL) {
1766 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1767 goto err;
1768 }
1769 if (!DH_generate_key(dh_clnt)) {
1770 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1771 DH_free(dh_clnt);
1772 goto err;
1773 }
1774
1775 pms_len = DH_size(dh_clnt);
1776 pms = OPENSSL_malloc(pms_len);
1777 if (pms == NULL) {
1778 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1779 ERR_R_MALLOC_FAILURE);
1780 DH_free(dh_clnt);
1781 goto err;
1782 }
1783
1784 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1785 if (dh_len <= 0) {
1786 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1787 DH_free(dh_clnt);
1788 goto err;
1789 }
1790 pms_len = dh_len;
1791
1792 /* send off the data */
1793 pub_len = BN_num_bytes(dh_clnt->pub_key);
1794 s2n(pub_len, p);
1795 BN_bn2bin(dh_clnt->pub_key, p);
1796 n += 2 + pub_len;
1797
1798 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001799 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001800 const EC_GROUP *srvr_group = NULL;
1801 EC_KEY *tkey;
1802 int field_size = 0, ecdh_len;
1803
1804 if (s->session->sess_cert == NULL) {
1805 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1806 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1807 SSL_R_UNEXPECTED_MESSAGE);
1808 goto err;
1809 }
1810
1811 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1812 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1813 ERR_R_INTERNAL_ERROR);
1814 goto err;
1815 }
1816
1817 tkey = s->session->sess_cert->peer_ecdh_tmp;
1818
1819 srvr_group = EC_KEY_get0_group(tkey);
1820 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1821 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1822 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1823 ERR_R_INTERNAL_ERROR);
1824 goto err;
1825 }
1826
1827 clnt_ecdh = EC_KEY_new();
1828 if (clnt_ecdh == NULL) {
1829 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1830 ERR_R_MALLOC_FAILURE);
1831 goto err;
1832 }
1833
1834 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1835 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1836 goto err;
1837 }
1838
1839 /* Generate a new ECDH key pair */
1840 if (!EC_KEY_generate_key(clnt_ecdh)) {
1841 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1842 goto err;
1843 }
1844
1845 field_size = EC_GROUP_get_degree(srvr_group);
1846 if (field_size <= 0) {
1847 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1848 goto err;
1849 }
1850
1851 pms_len = (field_size + 7) / 8;
1852 pms = OPENSSL_malloc(pms_len);
1853 if (pms == NULL) {
1854 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1855 ERR_R_MALLOC_FAILURE);
1856 goto err;
1857 }
1858
1859 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1860 if (ecdh_len <= 0) {
1861 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1862 goto err;
1863 }
1864 pms_len = ecdh_len;
1865
1866 /* First check the size of encoding and allocate memory accordingly. */
1867 encoded_pt_len =
1868 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1869 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1870
1871 encodedPoint =
1872 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1873 bn_ctx = BN_CTX_new();
1874 if (encodedPoint == NULL || bn_ctx == NULL) {
1875 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1876 ERR_R_MALLOC_FAILURE);
1877 goto err;
1878 }
1879
1880 /* Encode the public key */
1881 encoded_pt_len = EC_POINT_point2oct(
1882 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1883 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1884
1885 *p = encoded_pt_len; /* length of encoded point */
1886 /* Encoded point will be copied here */
1887 p += 1;
1888 n += 1;
1889 /* copy the point */
1890 memcpy(p, encodedPoint, encoded_pt_len);
1891 /* increment n to account for length field */
1892 n += encoded_pt_len;
1893
1894 /* Free allocated memory */
1895 BN_CTX_free(bn_ctx);
1896 bn_ctx = NULL;
1897 OPENSSL_free(encodedPoint);
1898 encodedPoint = NULL;
1899 EC_KEY_free(clnt_ecdh);
1900 clnt_ecdh = NULL;
1901 EVP_PKEY_free(srvr_pub_pkey);
1902 srvr_pub_pkey = NULL;
1903 } else if (alg_k & SSL_kPSK) {
1904 /* For plain PSK, other_secret is a block of 0s with the same length as
1905 * the pre-shared key. */
1906 pms_len = psk_len;
1907 pms = OPENSSL_malloc(pms_len);
1908 if (pms == NULL) {
1909 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1910 ERR_R_MALLOC_FAILURE);
1911 goto err;
1912 }
1913 memset(pms, 0, pms_len);
1914 } else {
1915 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1916 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1917 ERR_R_INTERNAL_ERROR);
1918 goto err;
1919 }
1920
1921 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1922 * key. */
1923 if (alg_a & SSL_aPSK) {
1924 CBB cbb, child;
1925 uint8_t *new_pms;
1926 size_t new_pms_len;
1927
1928 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1929 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1930 ERR_R_MALLOC_FAILURE);
1931 goto err;
1932 }
1933 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1934 !CBB_add_bytes(&child, pms, pms_len) ||
1935 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1936 !CBB_add_bytes(&child, psk, psk_len) ||
1937 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1938 CBB_cleanup(&cbb);
1939 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1940 ERR_R_INTERNAL_ERROR);
1941 goto err;
1942 }
1943 OPENSSL_cleanse(pms, pms_len);
1944 OPENSSL_free(pms);
1945 pms = new_pms;
1946 pms_len = new_pms_len;
1947 }
1948
1949 /* The message must be added to the finished hash before calculating the
1950 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001951 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1952 goto err;
1953 }
Adam Langley1bea1732014-12-17 19:06:57 -08001954 s->state = SSL3_ST_CW_KEY_EXCH_B;
1955
1956 s->session->master_key_length = s->enc_method->generate_master_secret(
1957 s, s->session->master_key, pms, pms_len);
1958 if (s->session->master_key_length == 0) {
1959 goto err;
1960 }
1961 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1962 OPENSSL_cleanse(pms, pms_len);
1963 OPENSSL_free(pms);
1964 }
1965
1966 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001967 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001968
1969err:
Adam Langley1bea1732014-12-17 19:06:57 -08001970 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001971 OPENSSL_free(encodedPoint);
1972 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001973 EVP_PKEY_free(srvr_pub_pkey);
1974 if (pms) {
1975 OPENSSL_cleanse(pms, pms_len);
1976 OPENSSL_free(pms);
1977 }
1978 return -1;
1979}
Adam Langley1258b6a2014-06-20 12:00:00 -07001980
Adam Langley1bea1732014-12-17 19:06:57 -08001981int ssl3_send_cert_verify(SSL *s) {
1982 uint8_t *buf, *p;
1983 const EVP_MD *md = NULL;
1984 uint8_t digest[EVP_MAX_MD_SIZE];
1985 size_t digest_length;
1986 EVP_PKEY *pkey;
1987 EVP_PKEY_CTX *pctx = NULL;
1988 size_t signature_length = 0;
1989 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langley1bea1732014-12-17 19:06:57 -08001991 buf = (uint8_t *)s->init_buf->data;
1992
1993 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1994 p = ssl_handshake_start(s);
1995 pkey = s->cert->key->privatekey;
1996
1997 /* Write out the digest type if needbe. */
1998 if (SSL_USE_SIGALGS(s)) {
1999 md = tls1_choose_signing_digest(s, pkey);
2000 if (!tls12_get_sigandhash(p, pkey, md)) {
2001 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2002 goto err;
2003 }
2004 p += 2;
2005 n += 2;
2006 }
2007
2008 /* Compute the digest. */
2009 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2010 goto err;
2011 }
2012
2013 /* The handshake buffer is no longer necessary. */
2014 if (s->s3->handshake_buffer &&
2015 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2016 goto err;
2017 }
2018
2019 /* Sign the digest. */
2020 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2021 if (pctx == NULL) {
2022 goto err;
2023 }
2024
2025 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2026 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2027 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2028 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2029 goto err;
2030 }
2031
2032 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2033 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2034 goto err;
2035 }
2036
2037 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2038 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2039 goto err;
2040 }
2041
2042 s2n(signature_length, p);
2043 n += signature_length + 2;
2044
David Benjaminfbdfefb2015-02-16 19:33:53 -05002045 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2046 goto err;
2047 }
Adam Langley1bea1732014-12-17 19:06:57 -08002048 s->state = SSL3_ST_CW_CERT_VRFY_B;
2049 }
2050
2051 EVP_PKEY_CTX_free(pctx);
2052 return ssl_do_write(s);
2053
2054err:
2055 EVP_PKEY_CTX_free(pctx);
2056 return -1;
2057}
2058
2059/* ssl3_has_client_certificate returns true if a client certificate is
2060 * configured. */
2061static int ssl3_has_client_certificate(SSL *s) {
2062 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2063}
2064
2065int ssl3_send_client_certificate(SSL *s) {
2066 X509 *x509 = NULL;
2067 EVP_PKEY *pkey = NULL;
2068 int i;
2069
2070 if (s->state == SSL3_ST_CW_CERT_A) {
2071 /* Let cert callback update client certificates if required */
2072 if (s->cert->cert_cb) {
2073 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2074 if (i < 0) {
2075 s->rwstate = SSL_X509_LOOKUP;
2076 return -1;
2077 }
2078 if (i == 0) {
2079 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2080 return 0;
2081 }
2082 s->rwstate = SSL_NOTHING;
2083 }
2084
2085 if (ssl3_has_client_certificate(s)) {
2086 s->state = SSL3_ST_CW_CERT_C;
2087 } else {
2088 s->state = SSL3_ST_CW_CERT_B;
2089 }
2090 }
2091
2092 /* We need to get a client cert */
2093 if (s->state == SSL3_ST_CW_CERT_B) {
2094 /* If we get an error, we need to:
2095 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2096 * We then get retried later */
2097 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2098 if (i < 0) {
2099 s->rwstate = SSL_X509_LOOKUP;
2100 return -1;
2101 }
2102 s->rwstate = SSL_NOTHING;
2103 if (i == 1 && pkey != NULL && x509 != NULL) {
2104 s->state = SSL3_ST_CW_CERT_B;
2105 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2106 i = 0;
2107 }
2108 } else if (i == 1) {
2109 i = 0;
2110 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2111 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2112 }
2113
David Benjamin2755a3e2015-04-22 16:17:58 -04002114 X509_free(x509);
2115 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002116 if (i && !ssl3_has_client_certificate(s)) {
2117 i = 0;
2118 }
2119 if (i == 0) {
2120 if (s->version == SSL3_VERSION) {
2121 s->s3->tmp.cert_req = 0;
2122 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2123 return 1;
2124 } else {
2125 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002126 /* There is no client certificate, so the handshake buffer may be
2127 * released. */
2128 if (s->s3->handshake_buffer &&
2129 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2130 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2131 return -1;
2132 }
Adam Langley1bea1732014-12-17 19:06:57 -08002133 }
2134 }
2135
2136 /* Ok, we have a cert */
2137 s->state = SSL3_ST_CW_CERT_C;
2138 }
2139
2140 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002141 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2142 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2143 return -1;
2144 }
Adam Langley1bea1732014-12-17 19:06:57 -08002145 }
2146
2147 /* SSL3_ST_CW_CERT_D */
2148 return ssl_do_write(s);
2149}
2150
Adam Langley1bea1732014-12-17 19:06:57 -08002151int ssl3_send_next_proto(SSL *s) {
2152 unsigned int len, padding_len;
2153 uint8_t *d, *p;
2154
2155 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2156 len = s->next_proto_negotiated_len;
2157 padding_len = 32 - ((len + 2) % 32);
2158
2159 d = p = ssl_handshake_start(s);
2160 *(p++) = len;
2161 memcpy(p, s->next_proto_negotiated, len);
2162 p += len;
2163 *(p++) = padding_len;
2164 memset(p, 0, padding_len);
2165 p += padding_len;
2166
David Benjaminfbdfefb2015-02-16 19:33:53 -05002167 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2168 return -1;
2169 }
Adam Langley1bea1732014-12-17 19:06:57 -08002170 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2171 }
2172
2173 return ssl_do_write(s);
2174}
2175
2176int ssl3_send_channel_id(SSL *s) {
2177 uint8_t *d;
2178 int ret = -1, public_key_len;
2179 EVP_MD_CTX md_ctx;
2180 size_t sig_len;
2181 ECDSA_SIG *sig = NULL;
2182 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2183
2184 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2185 return ssl_do_write(s);
2186 }
2187
2188 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2189 EVP_PKEY *key = NULL;
2190 s->ctx->channel_id_cb(s, &key);
2191 if (key != NULL) {
2192 s->tlsext_channel_id_private = key;
2193 }
2194 }
2195
2196 if (!s->tlsext_channel_id_private) {
2197 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2198 return -1;
2199 }
2200 s->rwstate = SSL_NOTHING;
2201
2202 d = ssl_handshake_start(s);
2203 if (s->s3->tlsext_channel_id_new) {
2204 s2n(TLSEXT_TYPE_channel_id_new, d);
2205 } else {
2206 s2n(TLSEXT_TYPE_channel_id, d);
2207 }
2208 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2209
2210 EVP_MD_CTX_init(&md_ctx);
2211
2212 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2213 if (public_key_len <= 0) {
2214 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2215 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2216 goto err;
2217 }
2218
2219 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2220 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2221 * field elements as 32-byte, big-endian numbers. */
2222 if (public_key_len != 65) {
2223 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2224 goto err;
2225 }
2226 public_key = OPENSSL_malloc(public_key_len);
2227 if (!public_key) {
2228 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2229 goto err;
2230 }
2231
2232 derp = public_key;
2233 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2234
2235 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2236 s->tlsext_channel_id_private) != 1) {
2237 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2238 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2239 goto err;
2240 }
2241
2242 if (!tls1_channel_id_hash(&md_ctx, s)) {
2243 goto err;
2244 }
2245
2246 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2247 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2248 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2249 goto err;
2250 }
2251
2252 der_sig = OPENSSL_malloc(sig_len);
2253 if (!der_sig) {
2254 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2255 goto err;
2256 }
2257
2258 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2259 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2260 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2261 goto err;
2262 }
2263
2264 derp = der_sig;
2265 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2266 if (sig == NULL) {
2267 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2268 goto err;
2269 }
2270
2271 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2272 memcpy(d, public_key + 1, 64);
2273 d += 64;
2274 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2275 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2276 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2277 goto err;
2278 }
2279
David Benjaminfbdfefb2015-02-16 19:33:53 -05002280 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2281 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2282 goto err;
2283 }
Adam Langley1bea1732014-12-17 19:06:57 -08002284 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2285
2286 ret = ssl_do_write(s);
2287
2288err:
2289 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002290 OPENSSL_free(public_key);
2291 OPENSSL_free(der_sig);
2292 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002293
2294 return ret;
2295}
2296
2297int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2298 int i = 0;
2299 if (s->ctx->client_cert_cb) {
2300 i = s->ctx->client_cert_cb(s, px509, ppkey);
2301 }
2302 return i;
2303}