blob: a7fee6498664e6e1088fcdbd0686310069dd519e [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:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400353 case SSL3_ST_CW_CERT_VRFY_C:
Adam Langley1bea1732014-12-17 19:06:57 -0800354 ret = ssl3_send_cert_verify(s);
355 if (ret <= 0) {
356 goto end;
357 }
358 s->state = SSL3_ST_CW_CHANGE_A;
359 s->init_num = 0;
360 s->s3->change_cipher_spec = 0;
361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langley1bea1732014-12-17 19:06:57 -0800363 case SSL3_ST_CW_CHANGE_A:
364 case SSL3_ST_CW_CHANGE_B:
365 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
366 SSL3_ST_CW_CHANGE_B);
367 if (ret <= 0) {
368 goto end;
369 }
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langley1bea1732014-12-17 19:06:57 -0800371 s->state = SSL3_ST_CW_FINISHED_A;
372 if (s->s3->tlsext_channel_id_valid) {
373 s->state = SSL3_ST_CW_CHANNEL_ID_A;
374 }
375 if (s->s3->next_proto_neg_seen) {
376 s->state = SSL3_ST_CW_NEXT_PROTO_A;
377 }
378 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langley1bea1732014-12-17 19:06:57 -0800380 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400381 if (!s->enc_method->setup_key_block(s) ||
382 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800383 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
384 ret = -1;
385 goto end;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley1bea1732014-12-17 19:06:57 -0800388 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley1bea1732014-12-17 19:06:57 -0800390 case SSL3_ST_CW_NEXT_PROTO_A:
391 case SSL3_ST_CW_NEXT_PROTO_B:
392 ret = ssl3_send_next_proto(s);
393 if (ret <= 0) {
394 goto end;
395 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700396
Adam Langley1bea1732014-12-17 19:06:57 -0800397 if (s->s3->tlsext_channel_id_valid) {
398 s->state = SSL3_ST_CW_CHANNEL_ID_A;
399 } else {
400 s->state = SSL3_ST_CW_FINISHED_A;
401 }
402 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley1bea1732014-12-17 19:06:57 -0800404 case SSL3_ST_CW_CHANNEL_ID_A:
405 case SSL3_ST_CW_CHANNEL_ID_B:
406 ret = ssl3_send_channel_id(s);
407 if (ret <= 0) {
408 goto end;
409 }
410 s->state = SSL3_ST_CW_FINISHED_A;
411 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langley1bea1732014-12-17 19:06:57 -0800413 case SSL3_ST_CW_FINISHED_A:
414 case SSL3_ST_CW_FINISHED_B:
415 ret =
416 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
417 s->enc_method->client_finished_label,
418 s->enc_method->client_finished_label_len);
419 if (ret <= 0) {
420 goto end;
421 }
422 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langley1bea1732014-12-17 19:06:57 -0800424 if (s->hit) {
425 s->s3->tmp.next_state = SSL_ST_OK;
426 } else {
427 /* This is a non-resumption handshake. If it involves ChannelID, then
428 * record the handshake hashes at this point in the session so that
429 * any resumption of this session with ChannelID can sign those
430 * hashes. */
431 if (s->s3->tlsext_channel_id_new) {
432 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500433 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800434 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500435 }
Adam Langley1bea1732014-12-17 19:06:57 -0800436 }
David Benjamined7c4752015-02-16 19:16:46 -0500437 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
438 ssl3_can_false_start(s) &&
439 /* No False Start on renegotiation (would complicate the state
440 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400441 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500442 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800443 } else {
444 /* Allow NewSessionTicket if ticket expected */
445 if (s->tlsext_ticket_expected) {
446 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
447 } else {
448 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
449 }
450 }
451 }
452 s->init_num = 0;
453 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langley1bea1732014-12-17 19:06:57 -0800455 case SSL3_ST_CR_SESSION_TICKET_A:
456 case SSL3_ST_CR_SESSION_TICKET_B:
457 ret = ssl3_get_new_session_ticket(s);
458 if (ret <= 0) {
459 goto end;
460 }
461 s->state = SSL3_ST_CR_CHANGE;
462 s->init_num = 0;
463 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langley1bea1732014-12-17 19:06:57 -0800465 case SSL3_ST_CR_CERT_STATUS_A:
466 case SSL3_ST_CR_CERT_STATUS_B:
467 ret = ssl3_get_cert_status(s);
468 if (ret <= 0) {
469 goto end;
470 }
471 s->state = SSL3_ST_CR_KEY_EXCH_A;
472 s->init_num = 0;
473 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400474
Adam Langley1bea1732014-12-17 19:06:57 -0800475 case SSL3_ST_CR_CHANGE:
476 /* At this point, the next message must be entirely behind a
477 * ChangeCipherSpec. */
478 if (!ssl3_expect_change_cipher_spec(s)) {
479 ret = -1;
480 goto end;
481 }
482 s->state = SSL3_ST_CR_FINISHED_A;
483 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langley1bea1732014-12-17 19:06:57 -0800485 case SSL3_ST_CR_FINISHED_A:
486 case SSL3_ST_CR_FINISHED_B:
487 ret =
488 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
489 if (ret <= 0) {
490 goto end;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langley1bea1732014-12-17 19:06:57 -0800493 if (s->hit) {
494 s->state = SSL3_ST_CW_CHANGE_A;
495 } else {
496 s->state = SSL_ST_OK;
497 }
498 s->init_num = 0;
499 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley1bea1732014-12-17 19:06:57 -0800501 case SSL3_ST_CW_FLUSH:
502 s->rwstate = SSL_WRITING;
503 if (BIO_flush(s->wbio) <= 0) {
504 ret = -1;
505 goto end;
506 }
507 s->rwstate = SSL_NOTHING;
508 s->state = s->s3->tmp.next_state;
509 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700510
David Benjamined7c4752015-02-16 19:16:46 -0500511 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800512 /* Allow NewSessionTicket if ticket expected */
513 if (s->tlsext_ticket_expected) {
514 s->state = SSL3_ST_CR_SESSION_TICKET_A;
515 } else {
516 s->state = SSL3_ST_CR_CHANGE;
517 }
David Benjamined7c4752015-02-16 19:16:46 -0500518 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700519
Adam Langley1bea1732014-12-17 19:06:57 -0800520 ssl_free_wbio_buffer(s);
521 ret = 1;
522 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley1bea1732014-12-17 19:06:57 -0800524 case SSL_ST_OK:
525 /* clean a few things up */
526 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamin2755a3e2015-04-22 16:17:58 -0400528 BUF_MEM_free(s->init_buf);
529 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langley1bea1732014-12-17 19:06:57 -0800531 /* Remove write buffering now. */
532 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langley1bea1732014-12-17 19:06:57 -0800534 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500535 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400536 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langley1bea1732014-12-17 19:06:57 -0800538 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 ret = 1;
541 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley1bea1732014-12-17 19:06:57 -0800543 if (cb != NULL) {
544 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langley1bea1732014-12-17 19:06:57 -0800547 goto end;
548
549 default:
550 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
551 ret = -1;
552 goto end;
553 }
554
555 if (!s->s3->tmp.reuse_message && !skip) {
556 if (cb != NULL && s->state != state) {
557 new_state = s->state;
558 s->state = state;
559 cb(s, SSL_CB_CONNECT_LOOP, 1);
560 s->state = new_state;
561 }
562 }
563 skip = 0;
564 }
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566end:
Adam Langley1bea1732014-12-17 19:06:57 -0800567 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400568 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800569 if (cb != NULL) {
570 cb(s, SSL_CB_CONNECT_EXIT, ret);
571 }
572 return ret;
573}
Adam Langley95c29f32014-06-20 12:00:00 -0700574
Adam Langley1bea1732014-12-17 19:06:57 -0800575int ssl3_send_client_hello(SSL *s) {
576 uint8_t *buf, *p, *d;
577 int i;
578 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langley1bea1732014-12-17 19:06:57 -0800580 buf = (uint8_t *)s->init_buf->data;
581 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
582 if (!s->s3->have_version) {
583 uint16_t max_version = ssl3_get_max_client_version(s);
584 /* Disabling all versions is silly: return an error. */
585 if (max_version == 0) {
586 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
587 goto err;
588 }
589 s->version = max_version;
590 s->client_version = max_version;
591 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500592
Adam Langley1bea1732014-12-17 19:06:57 -0800593 /* If the configured session was created at a version higher than our
594 * maximum version, drop it. */
595 if (s->session &&
596 (s->session->session_id_length == 0 || s->session->not_resumable ||
597 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
598 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
599 SSL_set_session(s, NULL);
600 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500601
Adam Langley1bea1732014-12-17 19:06:57 -0800602 /* else use the pre-loaded session */
603 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langley1bea1732014-12-17 19:06:57 -0800605 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
606 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400607 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400608 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
609 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400610 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langley1bea1732014-12-17 19:06:57 -0800613 /* Do the message type and length last. Note: the final argument to
614 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
615 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langley1bea1732014-12-17 19:06:57 -0800617 /* version indicates the negotiated version: for example from an SSLv2/v3
618 * compatible client hello). The client_version field is the maximum
619 * version we permit and it is also used in RSA encrypted premaster
620 * secrets. Some servers can choke if we initially report a higher version
621 * then renegotiate to a lower one in the premaster secret. This didn't
622 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
623 * or later if the server only supports 1.0.
624 *
625 * Possible scenario with previous logic:
626 * 1. Client hello indicates TLS 1.2
627 * 2. Server hello says TLS 1.0
628 * 3. RSA encrypted premaster secret uses 1.2.
629 * 4. Handhaked proceeds using TLS 1.0.
630 * 5. Server sends hello request to renegotiate.
631 * 6. Client hello indicates TLS v1.0 as we now
632 * know that is maximum server supports.
633 * 7. Server chokes on RSA encrypted premaster secret
634 * containing version 1.0.
635 *
636 * For interoperability it should be OK to always use the maximum version
637 * we support in client hello and then rely on the checking of version to
638 * ensure the servers isn't being inconsistent: for example initially
639 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
640 * using client_version in client hello and not resetting it to the
641 * negotiated version. */
642 *(p++) = s->client_version >> 8;
643 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley1bea1732014-12-17 19:06:57 -0800645 /* Random stuff */
646 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
647 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700648
Adam Langley1bea1732014-12-17 19:06:57 -0800649 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400650 if (s->s3->initial_handshake_complete || s->session == NULL) {
651 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800652 i = 0;
653 } else {
654 i = s->session->session_id_length;
655 }
656 *(p++) = i;
657 if (i != 0) {
658 if (i > (int)sizeof(s->session->session_id)) {
659 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
660 goto err;
661 }
662 memcpy(p, s->session->session_id, i);
663 p += i;
664 }
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langley1bea1732014-12-17 19:06:57 -0800666 /* cookie stuff for DTLS */
667 if (SSL_IS_DTLS(s)) {
668 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
669 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
670 goto err;
671 }
672 *(p++) = s->d1->cookie_len;
673 memcpy(p, s->d1->cookie, s->d1->cookie_len);
674 p += s->d1->cookie_len;
675 }
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langley1bea1732014-12-17 19:06:57 -0800677 /* Ciphers supported */
678 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
679 if (i == 0) {
680 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
681 SSL_R_NO_CIPHERS_AVAILABLE);
682 goto err;
683 }
684 s2n(i, p);
685 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langley1bea1732014-12-17 19:06:57 -0800687 /* COMPRESSION */
688 *(p++) = 1;
689 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langley1bea1732014-12-17 19:06:57 -0800691 /* TLS extensions*/
692 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
693 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
694 goto err;
695 }
696
697 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
698 p - buf);
699 if (p == NULL) {
700 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
701 goto err;
702 }
703
704 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500705 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
706 goto err;
707 }
Adam Langley1bea1732014-12-17 19:06:57 -0800708 s->state = SSL3_ST_CW_CLNT_HELLO_B;
709 }
710
711 /* SSL3_ST_CW_CLNT_HELLO_B */
712 return ssl_do_write(s);
713
Adam Langley95c29f32014-06-20 12:00:00 -0700714err:
Adam Langley1bea1732014-12-17 19:06:57 -0800715 return -1;
716}
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langley1bea1732014-12-17 19:06:57 -0800718int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400719 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800720 const SSL_CIPHER *c;
721 CERT *ct = s->cert;
722 int al = SSL_AD_INTERNAL_ERROR, ok;
723 long n;
724 CBS server_hello, server_random, session_id;
725 uint16_t server_version, cipher_suite;
726 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400727 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langley1bea1732014-12-17 19:06:57 -0800729 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
730 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
731 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500732 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langley1bea1732014-12-17 19:06:57 -0800734 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500735 uint32_t err = ERR_peek_error();
736 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
737 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
738 /* Add a dedicated error code to the queue for a handshake_failure alert
739 * in response to ClientHello. This matches NSS's client behavior and
740 * gives a better error on a (probable) failure to negotiate initial
741 * parameters. Note: this error code comes after the original one.
742 *
743 * See https://crbug.com/446505. */
744 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
745 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
746 }
Adam Langley1bea1732014-12-17 19:06:57 -0800747 return n;
748 }
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langley1bea1732014-12-17 19:06:57 -0800750 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400751
Adam Langley1bea1732014-12-17 19:06:57 -0800752 if (!CBS_get_u16(&server_hello, &server_version) ||
753 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
754 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
755 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
756 !CBS_get_u16(&server_hello, &cipher_suite) ||
757 !CBS_get_u8(&server_hello, &compression_method)) {
758 al = SSL_AD_DECODE_ERROR;
759 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
760 goto f_err;
761 }
David Benjamina03d95d2014-07-12 19:49:07 -0400762
David Benjamine6df0542015-05-12 22:02:08 -0400763 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800764 if (!s->s3->have_version) {
765 if (!ssl3_is_version_enabled(s, server_version)) {
766 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
767 s->version = server_version;
768 /* Mark the version as fixed so the record-layer version is not clamped
769 * to TLS 1.0. */
770 s->s3->have_version = 1;
771 al = SSL_AD_PROTOCOL_VERSION;
772 goto f_err;
773 }
774 s->version = server_version;
775 s->enc_method = ssl3_get_enc_method(server_version);
776 assert(s->enc_method != NULL);
777 /* At this point, the connection's version is known and s->version is
778 * fixed. Begin enforcing the record-layer version. */
779 s->s3->have_version = 1;
780 } else if (server_version != s->version) {
781 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
782 al = SSL_AD_PROTOCOL_VERSION;
783 goto f_err;
784 }
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langley1bea1732014-12-17 19:06:57 -0800786 /* Copy over the server random. */
787 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700788
Adam Langley1bea1732014-12-17 19:06:57 -0800789 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400790 if (!s->s3->initial_handshake_complete && s->session != NULL &&
791 CBS_mem_equal(&session_id, s->session->session_id,
792 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800793 if (s->sid_ctx_length != s->session->sid_ctx_length ||
794 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
795 /* actually a client application bug */
796 al = SSL_AD_ILLEGAL_PARAMETER;
797 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
798 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
799 goto f_err;
800 }
801 s->hit = 1;
802 } else {
803 /* The session wasn't resumed. Create a fresh SSL_SESSION to
804 * fill out. */
805 s->hit = 0;
806 if (!ssl_get_new_session(s, 0)) {
807 goto f_err;
808 }
809 /* Note: session_id could be empty. */
810 s->session->session_id_length = CBS_len(&session_id);
811 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
812 }
David Benjamina03d95d2014-07-12 19:49:07 -0400813
David Benjamina1c90a52015-05-30 17:03:14 -0400814 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800815 if (c == NULL) {
816 /* unknown cipher */
817 al = SSL_AD_ILLEGAL_PARAMETER;
818 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
819 SSL_R_UNKNOWN_CIPHER_RETURNED);
820 goto f_err;
821 }
822 /* ct->mask_ssl was computed from client capabilities. Now
823 * that the final version is known, compute a new mask_ssl. */
824 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
825 mask_ssl = SSL_TLSV1_2;
826 } else {
827 mask_ssl = 0;
828 }
829 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
830 * the server selected it, it's an error. */
831 if ((c->algorithm_ssl & mask_ssl) ||
832 (c->algorithm_mkey & ct->mask_k) ||
833 (c->algorithm_auth & ct->mask_a)) {
834 al = SSL_AD_ILLEGAL_PARAMETER;
835 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
836 goto f_err;
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langley1bea1732014-12-17 19:06:57 -0800839 sk = ssl_get_ciphers_by_id(s);
840 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
841 /* we did not say we would use this cipher */
842 al = SSL_AD_ILLEGAL_PARAMETER;
843 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
844 goto f_err;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
David Benjaminece3de92015-03-16 18:02:20 -0400847 if (s->hit) {
848 if (s->session->cipher != c) {
849 al = SSL_AD_ILLEGAL_PARAMETER;
850 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
851 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
852 goto f_err;
853 }
854 if (s->session->ssl_version != s->version) {
855 al = SSL_AD_ILLEGAL_PARAMETER;
856 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
857 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
858 goto f_err;
859 }
Adam Langley1bea1732014-12-17 19:06:57 -0800860 }
861 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500862
David Benjamin5f04b652015-05-26 17:30:10 -0400863 /* If doing a full handshake with TLS 1.2, the server may request a client
864 * certificate which requires hashing the handshake transcript under a
865 * different hash. Otherwise, release the handshake buffer. */
866 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800867 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
868 goto f_err;
869 }
David Benjamina03d95d2014-07-12 19:49:07 -0400870
Adam Langley1bea1732014-12-17 19:06:57 -0800871 /* Only the NULL compression algorithm is supported. */
872 if (compression_method != 0) {
873 al = SSL_AD_ILLEGAL_PARAMETER;
874 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
875 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
876 goto f_err;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langley1bea1732014-12-17 19:06:57 -0800879 /* TLS extensions */
880 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
881 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
882 goto err;
883 }
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langley1bea1732014-12-17 19:06:57 -0800885 /* There should be nothing left over in the record. */
886 if (CBS_len(&server_hello) != 0) {
887 /* wrong packet length */
888 al = SSL_AD_DECODE_ERROR;
889 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
890 goto f_err;
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyba5934b2015-06-02 10:50:35 -0700893 if (s->hit &&
894 s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
895 al = SSL_AD_HANDSHAKE_FAILURE;
896 if (s->session->extended_master_secret) {
897 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
898 SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
899 } else {
900 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
901 SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
902 }
903 goto f_err;
904 }
905
Adam Langley1bea1732014-12-17 19:06:57 -0800906 return 1;
907
Adam Langley95c29f32014-06-20 12:00:00 -0700908f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800909 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700910err:
Adam Langley1bea1732014-12-17 19:06:57 -0800911 return -1;
912}
Adam Langley95c29f32014-06-20 12:00:00 -0700913
David Benjamin436bf822015-06-07 19:50:37 -0400914/* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
915 * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
916 * on the error queue. */
917static int ssl3_check_certificate_for_cipher(X509 *leaf,
918 const SSL_CIPHER *cipher) {
919 int ret = 0;
920 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400921 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400922 goto err;
923 }
924
925 /* Check the certificate's type matches the cipher. */
926 int cert_type = ssl_cert_type(pkey);
927 if (cert_type < 0) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400928 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400929 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
930 goto err;
931 }
932 int expected_type = ssl_cipher_get_cert_index(cipher);
933 assert(expected_type >= 0);
934 if (cert_type != expected_type) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400935 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400936 SSL_R_WRONG_CERTIFICATE_TYPE);
937 goto err;
938 }
939
940 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
941 * be checked in other cases as well? */
942 if (cipher->algorithm_auth & SSL_aECDSA) {
943 /* This call populates the ex_flags field correctly */
944 X509_check_purpose(leaf, -1, 0);
945 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
946 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400947 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400948 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
949 goto err;
950 }
951 }
952
953 ret = 1;
954
955err:
956 EVP_PKEY_free(pkey);
957 return ret;
958}
959
Adam Langley1bea1732014-12-17 19:06:57 -0800960int ssl3_get_server_certificate(SSL *s) {
961 int al, i, ok, ret = -1;
962 unsigned long n;
963 X509 *x = NULL;
964 STACK_OF(X509) *sk = NULL;
965 SESS_CERT *sc;
966 EVP_PKEY *pkey = NULL;
967 CBS cbs, certificate_list;
968 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700969
Adam Langley1bea1732014-12-17 19:06:57 -0800970 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400971 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500972 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langley1bea1732014-12-17 19:06:57 -0800974 if (!ok) {
975 return n;
976 }
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langley1bea1732014-12-17 19:06:57 -0800978 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langley1bea1732014-12-17 19:06:57 -0800980 sk = sk_X509_new_null();
981 if (sk == NULL) {
982 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
983 goto err;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langley1bea1732014-12-17 19:06:57 -0800986 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400987 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800988 CBS_len(&cbs) != 0) {
989 al = SSL_AD_DECODE_ERROR;
David Benjamin8923c0b2015-06-07 11:42:34 -0400990 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800991 goto f_err;
992 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400993
Adam Langley1bea1732014-12-17 19:06:57 -0800994 while (CBS_len(&certificate_list) > 0) {
995 CBS certificate;
996 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
997 al = SSL_AD_DECODE_ERROR;
998 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
999 SSL_R_CERT_LENGTH_MISMATCH);
1000 goto f_err;
1001 }
1002 data = CBS_data(&certificate);
1003 x = d2i_X509(NULL, &data, CBS_len(&certificate));
1004 if (x == NULL) {
1005 al = SSL_AD_BAD_CERTIFICATE;
1006 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1007 goto f_err;
1008 }
1009 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1010 al = SSL_AD_DECODE_ERROR;
1011 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1012 SSL_R_CERT_LENGTH_MISMATCH);
1013 goto f_err;
1014 }
1015 if (!sk_X509_push(sk, x)) {
1016 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1017 goto err;
1018 }
1019 x = NULL;
1020 }
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langley1bea1732014-12-17 19:06:57 -08001022 i = ssl_verify_cert_chain(s, sk);
1023 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1024 al = ssl_verify_alarm_type(s->verify_result);
1025 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1026 SSL_R_CERTIFICATE_VERIFY_FAILED);
1027 goto f_err;
1028 }
1029 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001030
David Benjamin436bf822015-06-07 19:50:37 -04001031 X509 *leaf = sk_X509_value(sk, 0);
1032 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1033 al = SSL_AD_ILLEGAL_PARAMETER;
1034 goto f_err;
1035 }
1036
Adam Langley1bea1732014-12-17 19:06:57 -08001037 sc = ssl_sess_cert_new();
1038 if (sc == NULL) {
1039 goto err;
1040 }
Adam Langley95c29f32014-06-20 12:00:00 -07001041
David Benjamin2755a3e2015-04-22 16:17:58 -04001042 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001043 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
David Benjamin436bf822015-06-07 19:50:37 -04001045 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1046 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001047 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001048 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
David Benjaminb31040d2015-06-07 10:53:32 -04001050 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001051 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001052
David Benjamin2755a3e2015-04-22 16:17:58 -04001053 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001054 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001055
Adam Langley1bea1732014-12-17 19:06:57 -08001056 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001057
Adam Langley1bea1732014-12-17 19:06:57 -08001058 ret = 1;
1059
1060 if (0) {
1061 f_err:
1062 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1063 }
1064
1065err:
1066 EVP_PKEY_free(pkey);
1067 X509_free(x);
1068 sk_X509_pop_free(sk, X509_free);
1069 return ret;
1070}
1071
1072int ssl3_get_server_key_exchange(SSL *s) {
1073 EVP_MD_CTX md_ctx;
1074 int al, ok;
1075 long n, alg_k, alg_a;
1076 EVP_PKEY *pkey = NULL;
1077 const EVP_MD *md = NULL;
1078 RSA *rsa = NULL;
1079 DH *dh = NULL;
1080 EC_KEY *ecdh = NULL;
1081 BN_CTX *bn_ctx = NULL;
1082 EC_POINT *srvr_ecpoint = NULL;
1083 CBS server_key_exchange, server_key_exchange_orig, parameter;
1084
1085 /* use same message size as in ssl3_get_certificate_request() as
1086 * ServerKeyExchange message may be skipped */
1087 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1088 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001089 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001090 if (!ok) {
1091 return n;
1092 }
1093
1094 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1095 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1096 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1097 SSL_R_UNEXPECTED_MESSAGE);
1098 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1099 return -1;
1100 }
1101
1102 /* In plain PSK ciphersuite, ServerKeyExchange can be
1103 omitted if no identity hint is sent. Set session->sess_cert anyway to
1104 avoid problems later.*/
1105 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1106 /* PSK ciphersuites that also send a Certificate would have already
1107 * initialized |sess_cert|. */
1108 if (s->session->sess_cert == NULL) {
1109 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001110 if (s->session->sess_cert == NULL) {
1111 return -1;
1112 }
Adam Langley1bea1732014-12-17 19:06:57 -08001113 }
1114
1115 /* TODO(davidben): This should be reset in one place with the rest of the
1116 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001117 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1118 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001119 }
1120 s->s3->tmp.reuse_message = 1;
1121 return 1;
1122 }
1123
1124 /* Retain a copy of the original CBS to compute the signature over. */
1125 CBS_init(&server_key_exchange, s->init_msg, n);
1126 server_key_exchange_orig = server_key_exchange;
1127
1128 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001129 DH_free(s->session->sess_cert->peer_dh_tmp);
1130 s->session->sess_cert->peer_dh_tmp = NULL;
1131 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1132 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001133 } else {
1134 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001135 if (s->session->sess_cert == NULL) {
1136 return -1;
1137 }
Adam Langley1bea1732014-12-17 19:06:57 -08001138 }
1139
1140 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1141 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1142 EVP_MD_CTX_init(&md_ctx);
1143
1144 if (alg_a & SSL_aPSK) {
1145 CBS psk_identity_hint;
1146
1147 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1148 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1149 &psk_identity_hint)) {
1150 al = SSL_AD_DECODE_ERROR;
1151 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1152 goto f_err;
1153 }
1154
1155 /* Store PSK identity hint for later use, hint is used in
1156 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1157 * identity hint can be as long as the maximum length of a PSK identity.
1158 * Also do not allow NULL characters; identities are saved as C strings.
1159 *
1160 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1161 * a specific identity. */
1162 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1163 CBS_contains_zero_byte(&psk_identity_hint)) {
1164 al = SSL_AD_HANDSHAKE_FAILURE;
1165 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1166 SSL_R_DATA_LENGTH_TOO_LONG);
1167 goto f_err;
1168 }
1169
1170 /* Save the identity hint as a C string. */
1171 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1172 al = SSL_AD_INTERNAL_ERROR;
1173 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1174 ERR_R_MALLOC_FAILURE);
1175 goto f_err;
1176 }
1177 }
1178
David Benjamin7061e282015-03-19 11:10:48 -04001179 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001180 CBS dh_p, dh_g, dh_Ys;
1181
1182 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1183 CBS_len(&dh_p) == 0 ||
1184 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1185 CBS_len(&dh_g) == 0 ||
1186 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1187 CBS_len(&dh_Ys) == 0) {
1188 al = SSL_AD_DECODE_ERROR;
1189 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1190 goto f_err;
1191 }
1192
1193 dh = DH_new();
1194 if (dh == NULL) {
1195 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1196 goto err;
1197 }
1198
1199 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1200 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1201 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1202 NULL) {
1203 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1204 goto err;
1205 }
1206
Adam Langleya7997f12015-05-14 17:38:50 -07001207 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001208 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1209 SSL_R_BAD_DH_P_LENGTH);
1210 goto err;
1211 }
Adam Langley1bea1732014-12-17 19:06:57 -08001212 s->session->sess_cert->peer_dh_tmp = dh;
1213 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001214 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001215 uint16_t curve_id;
1216 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001217 const EC_GROUP *group;
1218 CBS point;
1219
1220 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1221 * key. Check curve is one of our preferences, if not server has sent an
1222 * invalid curve. */
1223 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1224 al = SSL_AD_DECODE_ERROR;
1225 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1226 goto f_err;
1227 }
1228
1229 curve_nid = tls1_ec_curve_id2nid(curve_id);
1230 if (curve_nid == 0) {
1231 al = SSL_AD_INTERNAL_ERROR;
1232 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1233 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1234 goto f_err;
1235 }
1236
David Benjamin4f7783e2015-03-05 03:05:11 -05001237 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001238 if (ecdh == NULL) {
1239 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001240 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001241 goto err;
1242 }
1243
Adam Langley1bea1732014-12-17 19:06:57 -08001244 group = EC_KEY_get0_group(ecdh);
1245
1246 /* Next, get the encoded ECPoint */
1247 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1248 al = SSL_AD_DECODE_ERROR;
1249 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1250 goto f_err;
1251 }
1252
1253 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1254 ((bn_ctx = BN_CTX_new()) == NULL)) {
1255 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1256 ERR_R_MALLOC_FAILURE);
1257 goto err;
1258 }
1259
1260 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1261 CBS_len(&point), bn_ctx)) {
1262 al = SSL_AD_DECODE_ERROR;
1263 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1264 goto f_err;
1265 }
Adam Langley1bea1732014-12-17 19:06:57 -08001266 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1267 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1268 ecdh = NULL;
1269 BN_CTX_free(bn_ctx);
1270 bn_ctx = NULL;
1271 EC_POINT_free(srvr_ecpoint);
1272 srvr_ecpoint = NULL;
1273 } else if (!(alg_k & SSL_kPSK)) {
1274 al = SSL_AD_UNEXPECTED_MESSAGE;
1275 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1276 SSL_R_UNEXPECTED_MESSAGE);
1277 goto f_err;
1278 }
1279
1280 /* At this point, |server_key_exchange| contains the signature, if any, while
1281 * |server_key_exchange_orig| contains the entire message. From that, derive
1282 * a CBS containing just the parameter. */
1283 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1284 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1285
David Benjaminf4958e72015-06-07 11:00:53 -04001286 /* ServerKeyExchange should be signed by the server's public key. */
1287 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1288 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1289 if (pkey == NULL) {
1290 goto err;
1291 }
Adam Langley1bea1732014-12-17 19:06:57 -08001292
1293 if (SSL_USE_SIGALGS(s)) {
1294 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1295 goto f_err;
1296 }
1297 } else if (pkey->type == EVP_PKEY_RSA) {
1298 md = EVP_md5_sha1();
1299 } else {
1300 md = EVP_sha1();
1301 }
1302
1303 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001304 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001305 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1306 CBS_len(&server_key_exchange) != 0) {
1307 al = SSL_AD_DECODE_ERROR;
1308 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1309 goto f_err;
1310 }
1311
1312 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1313 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1314 SSL3_RANDOM_SIZE) ||
1315 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1316 SSL3_RANDOM_SIZE) ||
1317 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1318 CBS_len(&parameter)) ||
1319 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1320 CBS_len(&signature))) {
1321 /* bad signature */
1322 al = SSL_AD_DECRYPT_ERROR;
1323 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1324 goto f_err;
1325 }
1326 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001327 /* PSK ciphers are the only supported certificate-less ciphers. */
1328 assert(alg_a == SSL_aPSK);
1329
Adam Langley1bea1732014-12-17 19:06:57 -08001330 if (CBS_len(&server_key_exchange) > 0) {
1331 al = SSL_AD_DECODE_ERROR;
1332 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1333 SSL_R_EXTRA_DATA_IN_MESSAGE);
1334 goto f_err;
1335 }
1336 }
1337 EVP_PKEY_free(pkey);
1338 EVP_MD_CTX_cleanup(&md_ctx);
1339 return 1;
1340
Adam Langley95c29f32014-06-20 12:00:00 -07001341f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001342 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001343err:
Adam Langley1bea1732014-12-17 19:06:57 -08001344 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001345 RSA_free(rsa);
1346 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001347 BN_CTX_free(bn_ctx);
1348 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001349 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001350 EVP_MD_CTX_cleanup(&md_ctx);
1351 return -1;
1352}
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langley1bea1732014-12-17 19:06:57 -08001354static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1355 return X509_NAME_cmp(*a, *b);
1356}
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langley1bea1732014-12-17 19:06:57 -08001358int ssl3_get_certificate_request(SSL *s) {
1359 int ok, ret = 0;
1360 unsigned long n;
1361 X509_NAME *xn = NULL;
1362 STACK_OF(X509_NAME) *ca_sk = NULL;
1363 CBS cbs;
1364 CBS certificate_types;
1365 CBS certificate_authorities;
1366 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langley1bea1732014-12-17 19:06:57 -08001368 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1369 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001370 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001371
Adam Langley1bea1732014-12-17 19:06:57 -08001372 if (!ok) {
1373 return n;
1374 }
David Benjamin688d8df2014-11-02 23:06:42 -05001375
Adam Langley1bea1732014-12-17 19:06:57 -08001376 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001377
Adam Langley1bea1732014-12-17 19:06:57 -08001378 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1379 s->s3->tmp.reuse_message = 1;
1380 /* If we get here we don't need any cached handshake records as we wont be
1381 * doing client auth. */
1382 if (s->s3->handshake_buffer &&
1383 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1384 goto err;
1385 }
1386 return 1;
1387 }
David Benjamined439582014-07-14 19:13:02 -04001388
Adam Langley1bea1732014-12-17 19:06:57 -08001389 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1390 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1391 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1392 SSL_R_WRONG_MESSAGE_TYPE);
1393 goto err;
1394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langley1bea1732014-12-17 19:06:57 -08001396 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001397
Adam Langley1bea1732014-12-17 19:06:57 -08001398 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1399 if (ca_sk == NULL) {
1400 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1401 goto err;
1402 }
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 /* get the certificate types */
1405 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1406 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1407 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1408 goto err;
1409 }
David Benjamined439582014-07-14 19:13:02 -04001410
Adam Langley1bea1732014-12-17 19:06:57 -08001411 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1412 &s->s3->tmp.num_certificate_types)) {
1413 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1414 goto err;
1415 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001416
Adam Langley1bea1732014-12-17 19:06:57 -08001417 if (SSL_USE_SIGALGS(s)) {
1418 CBS supported_signature_algorithms;
1419 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1420 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1421 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1422 goto err;
1423 }
David Benjamined439582014-07-14 19:13:02 -04001424
Adam Langley1bea1732014-12-17 19:06:57 -08001425 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1426 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1427 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1428 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1429 goto err;
1430 }
1431 }
David Benjamined439582014-07-14 19:13:02 -04001432
Adam Langley1bea1732014-12-17 19:06:57 -08001433 /* get the CA RDNs */
1434 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1435 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1436 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1437 goto err;
1438 }
Adam Langley95c29f32014-06-20 12:00:00 -07001439
Adam Langley1bea1732014-12-17 19:06:57 -08001440 while (CBS_len(&certificate_authorities) > 0) {
1441 CBS distinguished_name;
1442 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1443 &distinguished_name)) {
1444 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1445 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1446 SSL_R_CA_DN_TOO_LONG);
1447 goto err;
1448 }
Adam Langley95c29f32014-06-20 12:00:00 -07001449
Adam Langley1bea1732014-12-17 19:06:57 -08001450 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001451
Adam Langley1bea1732014-12-17 19:06:57 -08001452 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1453 if (xn == NULL) {
1454 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1455 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1456 goto err;
1457 }
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langley1bea1732014-12-17 19:06:57 -08001459 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1460 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001461 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001462 goto err;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langley1bea1732014-12-17 19:06:57 -08001465 if (CBS_len(&distinguished_name) != 0) {
1466 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1467 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1468 SSL_R_CA_DN_LENGTH_MISMATCH);
1469 goto err;
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langley1bea1732014-12-17 19:06:57 -08001472 if (!sk_X509_NAME_push(ca_sk, xn)) {
1473 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1474 ERR_R_MALLOC_FAILURE);
1475 goto err;
1476 }
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langley1bea1732014-12-17 19:06:57 -08001479 /* we should setup a certificate to return.... */
1480 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001481 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001482 s->s3->tmp.ca_names = ca_sk;
1483 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langley1bea1732014-12-17 19:06:57 -08001485 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langley1bea1732014-12-17 19:06:57 -08001487err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001488 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001489 return ret;
1490}
Adam Langley95c29f32014-06-20 12:00:00 -07001491
Adam Langley1bea1732014-12-17 19:06:57 -08001492int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001493 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001494 long n;
1495 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001496
Adam Langley1bea1732014-12-17 19:06:57 -08001497 n = s->method->ssl_get_message(
1498 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001499 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001500
Adam Langley1bea1732014-12-17 19:06:57 -08001501 if (!ok) {
1502 return n;
1503 }
Adam Langley95c29f32014-06-20 12:00:00 -07001504
David Benjamin95d31822015-06-15 19:53:32 -04001505 if (s->hit) {
1506 /* The server is sending a new ticket for an existing session. Sessions are
1507 * immutable once established, so duplicate all but the ticket of the
1508 * existing session. */
1509 uint8_t *bytes;
1510 size_t bytes_len;
1511 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &bytes, &bytes_len)) {
1512 goto err;
1513 }
1514 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1515 OPENSSL_free(bytes);
1516 if (new_session == NULL) {
1517 /* This should never happen. */
1518 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_INTERNAL_ERROR);
1519 goto err;
1520 }
David Benjamin680ca962015-06-18 12:37:23 -04001521 if (s->session->sess_cert != NULL) {
1522 /* |sess_cert| is not serialized and must be duplicated explicitly. */
1523 assert(new_session->sess_cert == NULL);
1524 new_session->sess_cert = ssl_sess_cert_dup(s->session->sess_cert);
1525 if (new_session->sess_cert == NULL) {
1526 SSL_SESSION_free(new_session);
1527 goto err;
1528 }
1529 }
David Benjamin95d31822015-06-15 19:53:32 -04001530
1531 SSL_SESSION_free(s->session);
1532 s->session = new_session;
1533 }
1534
Adam Langley1bea1732014-12-17 19:06:57 -08001535 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001536
Adam Langley1bea1732014-12-17 19:06:57 -08001537 if (!CBS_get_u32(&new_session_ticket,
1538 &s->session->tlsext_tick_lifetime_hint) ||
1539 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1540 CBS_len(&new_session_ticket) != 0) {
1541 al = SSL_AD_DECODE_ERROR;
1542 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1543 goto f_err;
1544 }
Adam Langley95c29f32014-06-20 12:00:00 -07001545
Adam Langley1bea1732014-12-17 19:06:57 -08001546 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1547 &s->session->tlsext_ticklen)) {
1548 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1549 goto err;
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
David Benjamin68070622015-02-08 23:44:59 -05001552 /* Generate a session ID for this session based on the session ticket. We use
1553 * the session ID mechanism for detecting ticket resumption. This also fits in
1554 * with assumptions elsewhere in OpenSSL.*/
1555 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1556 &s->session->session_id_length, EVP_sha256(), NULL)) {
1557 goto err;
1558 }
1559
1560 return 1;
David Benjamined439582014-07-14 19:13:02 -04001561
Adam Langley95c29f32014-06-20 12:00:00 -07001562f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001563 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001564err:
Adam Langley1bea1732014-12-17 19:06:57 -08001565 return -1;
1566}
Adam Langley95c29f32014-06-20 12:00:00 -07001567
Adam Langley1bea1732014-12-17 19:06:57 -08001568int ssl3_get_cert_status(SSL *s) {
1569 int ok, al;
1570 long n;
1571 CBS certificate_status, ocsp_response;
1572 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langley1bea1732014-12-17 19:06:57 -08001574 n = s->method->ssl_get_message(
1575 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001576 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001577
Adam Langley1bea1732014-12-17 19:06:57 -08001578 if (!ok) {
1579 return n;
1580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581
David Benjamindcd979f2015-04-20 18:26:52 -04001582 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1583 /* A server may send status_request in ServerHello and then change
1584 * its mind about sending CertificateStatus. */
1585 s->s3->tmp.reuse_message = 1;
1586 return 1;
1587 }
1588
Adam Langley1bea1732014-12-17 19:06:57 -08001589 CBS_init(&certificate_status, s->init_msg, n);
1590 if (!CBS_get_u8(&certificate_status, &status_type) ||
1591 status_type != TLSEXT_STATUSTYPE_ocsp ||
1592 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1593 CBS_len(&ocsp_response) == 0 ||
1594 CBS_len(&certificate_status) != 0) {
1595 al = SSL_AD_DECODE_ERROR;
1596 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1597 goto f_err;
1598 }
Adam Langley95c29f32014-06-20 12:00:00 -07001599
Adam Langley1bea1732014-12-17 19:06:57 -08001600 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1601 &s->session->ocsp_response_length)) {
1602 al = SSL_AD_INTERNAL_ERROR;
1603 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1604 goto f_err;
1605 }
1606 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001607
Adam Langley95c29f32014-06-20 12:00:00 -07001608f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001609 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1610 return -1;
1611}
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langley1bea1732014-12-17 19:06:57 -08001613int ssl3_get_server_done(SSL *s) {
1614 int ok;
1615 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langley1bea1732014-12-17 19:06:57 -08001617 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1618 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1619 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001620 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langley1bea1732014-12-17 19:06:57 -08001622 if (!ok) {
1623 return n;
1624 }
David Benjamin46062682014-07-14 19:14:32 -04001625
Adam Langley1bea1732014-12-17 19:06:57 -08001626 if (n > 0) {
1627 /* should contain no data */
1628 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1629 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1630 return -1;
1631 }
David Benjamin46062682014-07-14 19:14:32 -04001632
Adam Langley1bea1732014-12-17 19:06:57 -08001633 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001634}
Adam Langley1258b6a2014-06-20 12:00:00 -07001635
Adam Langley1258b6a2014-06-20 12:00:00 -07001636
Adam Langley1bea1732014-12-17 19:06:57 -08001637int ssl3_send_client_key_exchange(SSL *s) {
1638 uint8_t *p;
1639 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001640 uint32_t alg_k;
1641 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001642 uint8_t *q;
1643 EVP_PKEY *pkey = NULL;
1644 EC_KEY *clnt_ecdh = NULL;
1645 const EC_POINT *srvr_ecpoint = NULL;
1646 EVP_PKEY *srvr_pub_pkey = NULL;
1647 uint8_t *encodedPoint = NULL;
1648 int encoded_pt_len = 0;
1649 BN_CTX *bn_ctx = NULL;
1650 unsigned int psk_len = 0;
1651 uint8_t psk[PSK_MAX_PSK_LEN];
1652 uint8_t *pms = NULL;
1653 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001654
Adam Langley1bea1732014-12-17 19:06:57 -08001655 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1656 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001657
Adam Langley1bea1732014-12-17 19:06:57 -08001658 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1659 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001660
Adam Langley1bea1732014-12-17 19:06:57 -08001661 /* If using a PSK key exchange, prepare the pre-shared key. */
1662 if (alg_a & SSL_aPSK) {
1663 char identity[PSK_MAX_IDENTITY_LEN + 1];
1664 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 if (s->psk_client_callback == NULL) {
1667 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1668 SSL_R_PSK_NO_CLIENT_CB);
1669 goto err;
1670 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001671
Adam Langley1bea1732014-12-17 19:06:57 -08001672 memset(identity, 0, sizeof(identity));
1673 psk_len =
1674 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1675 sizeof(identity), psk, sizeof(psk));
1676 if (psk_len > PSK_MAX_PSK_LEN) {
1677 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1678 ERR_R_INTERNAL_ERROR);
1679 goto err;
1680 } else if (psk_len == 0) {
1681 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1682 SSL_R_PSK_IDENTITY_NOT_FOUND);
1683 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1684 goto err;
1685 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001686
Adam Langley1bea1732014-12-17 19:06:57 -08001687 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1688 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1689 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1690 ERR_R_INTERNAL_ERROR);
1691 goto err;
1692 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001693
David Benjamin2755a3e2015-04-22 16:17:58 -04001694 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001695 s->session->psk_identity = BUF_strdup(identity);
1696 if (s->session->psk_identity == NULL) {
1697 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1698 ERR_R_MALLOC_FAILURE);
1699 goto err;
1700 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001701
Adam Langley1bea1732014-12-17 19:06:57 -08001702 /* Write out psk_identity. */
1703 s2n(identity_len, p);
1704 memcpy(p, identity, identity_len);
1705 p += identity_len;
1706 n = 2 + identity_len;
1707 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001708
Adam Langley1bea1732014-12-17 19:06:57 -08001709 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1710 if (alg_k & SSL_kRSA) {
1711 RSA *rsa;
1712 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001713
Adam Langley1bea1732014-12-17 19:06:57 -08001714 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1715 pms = OPENSSL_malloc(pms_len);
1716 if (pms == NULL) {
1717 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1718 ERR_R_MALLOC_FAILURE);
1719 goto err;
1720 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001721
Adam Langley1bea1732014-12-17 19:06:57 -08001722 if (s->session->sess_cert == NULL) {
1723 /* We should always have a server certificate with SSL_kRSA. */
1724 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1725 ERR_R_INTERNAL_ERROR);
1726 goto err;
1727 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001728
David Benjaminb31040d2015-06-07 10:53:32 -04001729 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001730 if (pkey == NULL ||
1731 pkey->type != EVP_PKEY_RSA ||
1732 pkey->pkey.rsa == NULL) {
1733 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1734 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001735 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001736 goto err;
1737 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001738
Adam Langley1bea1732014-12-17 19:06:57 -08001739 rsa = pkey->pkey.rsa;
1740 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001741
Adam Langley1bea1732014-12-17 19:06:57 -08001742 pms[0] = s->client_version >> 8;
1743 pms[1] = s->client_version & 0xff;
1744 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1745 goto err;
1746 }
1747
1748 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1749
1750 q = p;
1751 /* In TLS and beyond, reserve space for the length prefix. */
1752 if (s->version > SSL3_VERSION) {
1753 p += 2;
1754 n += 2;
1755 }
1756 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1757 RSA_PKCS1_PADDING)) {
1758 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1759 SSL_R_BAD_RSA_ENCRYPT);
1760 goto err;
1761 }
1762 n += enc_pms_len;
1763
1764 /* Log the premaster secret, if logging is enabled. */
1765 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1766 pms_len)) {
1767 goto err;
1768 }
1769
1770 /* Fill in the length prefix. */
1771 if (s->version > SSL3_VERSION) {
1772 s2n(enc_pms_len, q);
1773 }
David Benjamin7061e282015-03-19 11:10:48 -04001774 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001775 DH *dh_srvr, *dh_clnt;
1776 SESS_CERT *scert = s->session->sess_cert;
1777 int dh_len;
1778 size_t pub_len;
1779
1780 if (scert == NULL) {
1781 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1782 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1783 SSL_R_UNEXPECTED_MESSAGE);
1784 goto err;
1785 }
1786
1787 if (scert->peer_dh_tmp == NULL) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1789 ERR_R_INTERNAL_ERROR);
1790 goto err;
1791 }
1792 dh_srvr = scert->peer_dh_tmp;
1793
1794 /* generate a new random key */
1795 dh_clnt = DHparams_dup(dh_srvr);
1796 if (dh_clnt == NULL) {
1797 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1798 goto err;
1799 }
1800 if (!DH_generate_key(dh_clnt)) {
1801 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1802 DH_free(dh_clnt);
1803 goto err;
1804 }
1805
1806 pms_len = DH_size(dh_clnt);
1807 pms = OPENSSL_malloc(pms_len);
1808 if (pms == NULL) {
1809 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1810 ERR_R_MALLOC_FAILURE);
1811 DH_free(dh_clnt);
1812 goto err;
1813 }
1814
1815 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1816 if (dh_len <= 0) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1818 DH_free(dh_clnt);
1819 goto err;
1820 }
1821 pms_len = dh_len;
1822
1823 /* send off the data */
1824 pub_len = BN_num_bytes(dh_clnt->pub_key);
1825 s2n(pub_len, p);
1826 BN_bn2bin(dh_clnt->pub_key, p);
1827 n += 2 + pub_len;
1828
1829 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001830 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001831 const EC_GROUP *srvr_group = NULL;
1832 EC_KEY *tkey;
1833 int field_size = 0, ecdh_len;
1834
1835 if (s->session->sess_cert == NULL) {
1836 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1837 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1838 SSL_R_UNEXPECTED_MESSAGE);
1839 goto err;
1840 }
1841
1842 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1843 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1844 ERR_R_INTERNAL_ERROR);
1845 goto err;
1846 }
1847
1848 tkey = s->session->sess_cert->peer_ecdh_tmp;
1849
1850 srvr_group = EC_KEY_get0_group(tkey);
1851 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1852 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1853 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1854 ERR_R_INTERNAL_ERROR);
1855 goto err;
1856 }
1857
1858 clnt_ecdh = EC_KEY_new();
1859 if (clnt_ecdh == NULL) {
1860 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1861 ERR_R_MALLOC_FAILURE);
1862 goto err;
1863 }
1864
1865 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1866 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1867 goto err;
1868 }
1869
1870 /* Generate a new ECDH key pair */
1871 if (!EC_KEY_generate_key(clnt_ecdh)) {
1872 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1873 goto err;
1874 }
1875
1876 field_size = EC_GROUP_get_degree(srvr_group);
1877 if (field_size <= 0) {
1878 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1879 goto err;
1880 }
1881
1882 pms_len = (field_size + 7) / 8;
1883 pms = OPENSSL_malloc(pms_len);
1884 if (pms == NULL) {
1885 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1886 ERR_R_MALLOC_FAILURE);
1887 goto err;
1888 }
1889
1890 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1891 if (ecdh_len <= 0) {
1892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1893 goto err;
1894 }
1895 pms_len = ecdh_len;
1896
1897 /* First check the size of encoding and allocate memory accordingly. */
1898 encoded_pt_len =
1899 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1900 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1901
1902 encodedPoint =
1903 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1904 bn_ctx = BN_CTX_new();
1905 if (encodedPoint == NULL || bn_ctx == NULL) {
1906 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1907 ERR_R_MALLOC_FAILURE);
1908 goto err;
1909 }
1910
1911 /* Encode the public key */
1912 encoded_pt_len = EC_POINT_point2oct(
1913 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1914 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1915
1916 *p = encoded_pt_len; /* length of encoded point */
1917 /* Encoded point will be copied here */
1918 p += 1;
1919 n += 1;
1920 /* copy the point */
1921 memcpy(p, encodedPoint, encoded_pt_len);
1922 /* increment n to account for length field */
1923 n += encoded_pt_len;
1924
1925 /* Free allocated memory */
1926 BN_CTX_free(bn_ctx);
1927 bn_ctx = NULL;
1928 OPENSSL_free(encodedPoint);
1929 encodedPoint = NULL;
1930 EC_KEY_free(clnt_ecdh);
1931 clnt_ecdh = NULL;
1932 EVP_PKEY_free(srvr_pub_pkey);
1933 srvr_pub_pkey = NULL;
1934 } else if (alg_k & SSL_kPSK) {
1935 /* For plain PSK, other_secret is a block of 0s with the same length as
1936 * the pre-shared key. */
1937 pms_len = psk_len;
1938 pms = OPENSSL_malloc(pms_len);
1939 if (pms == NULL) {
1940 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1941 ERR_R_MALLOC_FAILURE);
1942 goto err;
1943 }
1944 memset(pms, 0, pms_len);
1945 } else {
1946 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1947 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1948 ERR_R_INTERNAL_ERROR);
1949 goto err;
1950 }
1951
1952 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1953 * key. */
1954 if (alg_a & SSL_aPSK) {
1955 CBB cbb, child;
1956 uint8_t *new_pms;
1957 size_t new_pms_len;
1958
1959 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1960 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1961 ERR_R_MALLOC_FAILURE);
1962 goto err;
1963 }
1964 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1965 !CBB_add_bytes(&child, pms, pms_len) ||
1966 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1967 !CBB_add_bytes(&child, psk, psk_len) ||
1968 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1969 CBB_cleanup(&cbb);
1970 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1971 ERR_R_INTERNAL_ERROR);
1972 goto err;
1973 }
1974 OPENSSL_cleanse(pms, pms_len);
1975 OPENSSL_free(pms);
1976 pms = new_pms;
1977 pms_len = new_pms_len;
1978 }
1979
1980 /* The message must be added to the finished hash before calculating the
1981 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001982 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1983 goto err;
1984 }
Adam Langley1bea1732014-12-17 19:06:57 -08001985 s->state = SSL3_ST_CW_KEY_EXCH_B;
1986
1987 s->session->master_key_length = s->enc_method->generate_master_secret(
1988 s, s->session->master_key, pms, pms_len);
1989 if (s->session->master_key_length == 0) {
1990 goto err;
1991 }
1992 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1993 OPENSSL_cleanse(pms, pms_len);
1994 OPENSSL_free(pms);
1995 }
1996
1997 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001998 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001999
2000err:
Adam Langley1bea1732014-12-17 19:06:57 -08002001 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002002 OPENSSL_free(encodedPoint);
2003 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08002004 EVP_PKEY_free(srvr_pub_pkey);
2005 if (pms) {
2006 OPENSSL_cleanse(pms, pms_len);
2007 OPENSSL_free(pms);
2008 }
2009 return -1;
2010}
Adam Langley1258b6a2014-06-20 12:00:00 -07002011
Adam Langley1bea1732014-12-17 19:06:57 -08002012int ssl3_send_cert_verify(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002013 if (s->state == SSL3_ST_CW_CERT_VRFY_A ||
2014 s->state == SSL3_ST_CW_CERT_VRFY_B) {
2015 enum ssl_private_key_result_t sign_result;
2016 uint8_t *p = ssl_handshake_start(s);
2017 size_t signature_length = 0;
2018 unsigned long n = 0;
2019 EVP_PKEY *pkey = s->cert->key->privatekey;
2020 assert(pkey != NULL || s->cert->key_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002021
David Benjaminb4d65fd2015-05-29 17:11:21 -04002022 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2023 uint8_t *buf = (uint8_t *)s->init_buf->data;
2024 const EVP_MD *md = NULL;
2025 uint8_t digest[EVP_MAX_MD_SIZE];
2026 size_t digest_length;
Adam Langley1bea1732014-12-17 19:06:57 -08002027
David Benjaminb4d65fd2015-05-29 17:11:21 -04002028 /* Write out the digest type if need be. */
2029 if (SSL_USE_SIGALGS(s)) {
2030 md = tls1_choose_signing_digest(s, pkey);
2031 if (!tls12_get_sigandhash(s, p, pkey, md)) {
2032 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2033 return -1;
2034 }
2035 p += 2;
2036 n += 2;
Adam Langley1bea1732014-12-17 19:06:57 -08002037 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002038
2039 /* Compute the digest. */
2040 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2041 return -1;
2042 }
2043
2044 /* The handshake buffer is no longer necessary. */
2045 if (s->s3->handshake_buffer &&
2046 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2047 return -1;
2048 }
2049
2050 /* Sign the digest. */
2051 signature_length = ssl_private_key_max_signature_len(s, pkey);
2052 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2053 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify,
2054 SSL_R_DATA_LENGTH_TOO_LONG);
2055 return -1;
2056 }
2057
2058 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2059 sign_result = ssl_private_key_sign(s, pkey, &p[2], &signature_length,
2060 signature_length, md, digest,
2061 digest_length);
2062 } else {
2063 if (SSL_USE_SIGALGS(s)) {
2064 /* The digest has already been selected and written. */
2065 p += 2;
2066 n += 2;
2067 }
2068 signature_length = ssl_private_key_max_signature_len(s, pkey);
2069 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2070 sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length,
2071 signature_length);
Adam Langley1bea1732014-12-17 19:06:57 -08002072 }
2073
David Benjaminb4d65fd2015-05-29 17:11:21 -04002074 if (sign_result == ssl_private_key_retry) {
2075 s->state = SSL3_ST_CW_CERT_VRFY_B;
2076 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002077 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002078 s->rwstate = SSL_NOTHING;
2079 if (sign_result != ssl_private_key_success) {
2080 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002081 }
2082
2083 s2n(signature_length, p);
2084 n += signature_length + 2;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002085 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002086 return -1;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002087 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002088 s->state = SSL3_ST_CW_CERT_VRFY_C;
Adam Langley1bea1732014-12-17 19:06:57 -08002089 }
2090
Adam Langley1bea1732014-12-17 19:06:57 -08002091 return ssl_do_write(s);
Adam Langley1bea1732014-12-17 19:06:57 -08002092}
2093
2094/* ssl3_has_client_certificate returns true if a client certificate is
2095 * configured. */
2096static int ssl3_has_client_certificate(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002097 return s->cert && s->cert->key->x509 && (s->cert->key->privatekey ||
2098 s->cert->key_method);
Adam Langley1bea1732014-12-17 19:06:57 -08002099}
2100
2101int ssl3_send_client_certificate(SSL *s) {
2102 X509 *x509 = NULL;
2103 EVP_PKEY *pkey = NULL;
2104 int i;
2105
2106 if (s->state == SSL3_ST_CW_CERT_A) {
2107 /* Let cert callback update client certificates if required */
2108 if (s->cert->cert_cb) {
2109 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2110 if (i < 0) {
2111 s->rwstate = SSL_X509_LOOKUP;
2112 return -1;
2113 }
2114 if (i == 0) {
2115 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2116 return 0;
2117 }
2118 s->rwstate = SSL_NOTHING;
2119 }
2120
2121 if (ssl3_has_client_certificate(s)) {
2122 s->state = SSL3_ST_CW_CERT_C;
2123 } else {
2124 s->state = SSL3_ST_CW_CERT_B;
2125 }
2126 }
2127
2128 /* We need to get a client cert */
2129 if (s->state == SSL3_ST_CW_CERT_B) {
2130 /* If we get an error, we need to:
2131 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2132 * We then get retried later */
2133 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2134 if (i < 0) {
2135 s->rwstate = SSL_X509_LOOKUP;
2136 return -1;
2137 }
2138 s->rwstate = SSL_NOTHING;
2139 if (i == 1 && pkey != NULL && x509 != NULL) {
2140 s->state = SSL3_ST_CW_CERT_B;
2141 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2142 i = 0;
2143 }
2144 } else if (i == 1) {
2145 i = 0;
2146 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2147 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2148 }
2149
David Benjamin2755a3e2015-04-22 16:17:58 -04002150 X509_free(x509);
2151 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002152 if (i && !ssl3_has_client_certificate(s)) {
2153 i = 0;
2154 }
2155 if (i == 0) {
2156 if (s->version == SSL3_VERSION) {
2157 s->s3->tmp.cert_req = 0;
2158 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2159 return 1;
2160 } else {
2161 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002162 /* There is no client certificate, so the handshake buffer may be
2163 * released. */
2164 if (s->s3->handshake_buffer &&
2165 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2166 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2167 return -1;
2168 }
Adam Langley1bea1732014-12-17 19:06:57 -08002169 }
2170 }
2171
2172 /* Ok, we have a cert */
2173 s->state = SSL3_ST_CW_CERT_C;
2174 }
2175
2176 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002177 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2178 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2179 return -1;
2180 }
Adam Langley1bea1732014-12-17 19:06:57 -08002181 }
2182
2183 /* SSL3_ST_CW_CERT_D */
2184 return ssl_do_write(s);
2185}
2186
Adam Langley1bea1732014-12-17 19:06:57 -08002187int ssl3_send_next_proto(SSL *s) {
2188 unsigned int len, padding_len;
2189 uint8_t *d, *p;
2190
2191 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2192 len = s->next_proto_negotiated_len;
2193 padding_len = 32 - ((len + 2) % 32);
2194
2195 d = p = ssl_handshake_start(s);
2196 *(p++) = len;
2197 memcpy(p, s->next_proto_negotiated, len);
2198 p += len;
2199 *(p++) = padding_len;
2200 memset(p, 0, padding_len);
2201 p += padding_len;
2202
David Benjaminfbdfefb2015-02-16 19:33:53 -05002203 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2204 return -1;
2205 }
Adam Langley1bea1732014-12-17 19:06:57 -08002206 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2207 }
2208
2209 return ssl_do_write(s);
2210}
2211
2212int ssl3_send_channel_id(SSL *s) {
2213 uint8_t *d;
2214 int ret = -1, public_key_len;
2215 EVP_MD_CTX md_ctx;
2216 size_t sig_len;
2217 ECDSA_SIG *sig = NULL;
2218 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2219
2220 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2221 return ssl_do_write(s);
2222 }
2223
2224 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2225 EVP_PKEY *key = NULL;
2226 s->ctx->channel_id_cb(s, &key);
2227 if (key != NULL) {
2228 s->tlsext_channel_id_private = key;
2229 }
2230 }
2231
2232 if (!s->tlsext_channel_id_private) {
2233 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2234 return -1;
2235 }
2236 s->rwstate = SSL_NOTHING;
2237
2238 d = ssl_handshake_start(s);
2239 if (s->s3->tlsext_channel_id_new) {
2240 s2n(TLSEXT_TYPE_channel_id_new, d);
2241 } else {
2242 s2n(TLSEXT_TYPE_channel_id, d);
2243 }
2244 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2245
2246 EVP_MD_CTX_init(&md_ctx);
2247
2248 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2249 if (public_key_len <= 0) {
2250 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2251 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2252 goto err;
2253 }
2254
2255 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2256 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2257 * field elements as 32-byte, big-endian numbers. */
2258 if (public_key_len != 65) {
2259 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2260 goto err;
2261 }
2262 public_key = OPENSSL_malloc(public_key_len);
2263 if (!public_key) {
2264 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2265 goto err;
2266 }
2267
2268 derp = public_key;
2269 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2270
2271 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2272 s->tlsext_channel_id_private) != 1) {
2273 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2274 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2275 goto err;
2276 }
2277
2278 if (!tls1_channel_id_hash(&md_ctx, s)) {
2279 goto err;
2280 }
2281
2282 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2283 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2284 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2285 goto err;
2286 }
2287
2288 der_sig = OPENSSL_malloc(sig_len);
2289 if (!der_sig) {
2290 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2291 goto err;
2292 }
2293
2294 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2295 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2296 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2297 goto err;
2298 }
2299
2300 derp = der_sig;
2301 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2302 if (sig == NULL) {
2303 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2304 goto err;
2305 }
2306
2307 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2308 memcpy(d, public_key + 1, 64);
2309 d += 64;
2310 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2311 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2312 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2313 goto err;
2314 }
2315
David Benjaminfbdfefb2015-02-16 19:33:53 -05002316 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2317 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2318 goto err;
2319 }
Adam Langley1bea1732014-12-17 19:06:57 -08002320 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2321
2322 ret = ssl_do_write(s);
2323
2324err:
2325 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002326 OPENSSL_free(public_key);
2327 OPENSSL_free(der_sig);
2328 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002329
2330 return ret;
2331}
2332
2333int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2334 int i = 0;
2335 if (s->ctx->client_cert_cb) {
2336 i = s->ctx->client_cert_cb(s, px509, ppkey);
2337 }
2338 return i;
2339}