blob: b5c35f03704d7c18292406eacebfb5fc27b4fb4b [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
David Benjamin961ad6a2015-06-12 01:40:23 -0400155#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400157#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400158#include <openssl/dh.h>
159#include <openssl/ec_key.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400164#include <openssl/mem.h>
165#include <openssl/obj.h>
166#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400168#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171#include "../crypto/dh/internal.h"
172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
Adam Langley1bea1732014-12-17 19:06:57 -0800174int ssl3_connect(SSL *s) {
175 BUF_MEM *buf = NULL;
176 void (*cb)(const SSL *ssl, int type, int val) = NULL;
177 int ret = -1;
178 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500179
Adam Langley1bea1732014-12-17 19:06:57 -0800180 assert(s->handshake_func == ssl3_connect);
181 assert(!s->server);
182 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley1bea1732014-12-17 19:06:57 -0800184 ERR_clear_error();
185 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langley1bea1732014-12-17 19:06:57 -0800187 if (s->info_callback != NULL) {
188 cb = s->info_callback;
189 } else if (s->ctx->info_callback != NULL) {
190 cb = s->ctx->info_callback;
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley1bea1732014-12-17 19:06:57 -0800193 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley1bea1732014-12-17 19:06:57 -0800195 for (;;) {
196 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley1bea1732014-12-17 19:06:57 -0800198 switch (s->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800199 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500200 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800201 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley1bea1732014-12-17 19:06:57 -0800204 if (s->init_buf == NULL) {
205 buf = BUF_MEM_new();
206 if (buf == NULL ||
207 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
208 ret = -1;
209 goto end;
210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
Adam Langley1bea1732014-12-17 19:06:57 -0800212 s->init_buf = buf;
213 buf = NULL;
214 }
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin6a08da22015-05-08 22:58:12 -0400216 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800217 ret = -1;
218 goto end;
219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley1bea1732014-12-17 19:06:57 -0800221 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley1bea1732014-12-17 19:06:57 -0800223 if (!ssl3_init_finished_mac(s)) {
224 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
225 ret = -1;
226 goto end;
227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley1bea1732014-12-17 19:06:57 -0800229 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800230 s->init_num = 0;
231 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley1bea1732014-12-17 19:06:57 -0800233 case SSL3_ST_CW_CLNT_HELLO_A:
234 case SSL3_ST_CW_CLNT_HELLO_B:
235 s->shutdown = 0;
236 ret = ssl3_send_client_hello(s);
237 if (ret <= 0) {
238 goto end;
239 }
240 s->state = SSL3_ST_CR_SRVR_HELLO_A;
241 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langley1bea1732014-12-17 19:06:57 -0800243 /* turn on buffering for the next lot of output */
244 if (s->bbio != s->wbio) {
245 s->wbio = BIO_push(s->bbio, s->wbio);
246 }
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley1bea1732014-12-17 19:06:57 -0800248 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langley1bea1732014-12-17 19:06:57 -0800250 case SSL3_ST_CR_SRVR_HELLO_A:
251 case SSL3_ST_CR_SRVR_HELLO_B:
252 ret = ssl3_get_server_hello(s);
253 if (ret <= 0) {
254 goto end;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langley1bea1732014-12-17 19:06:57 -0800257 if (s->hit) {
258 s->state = SSL3_ST_CR_CHANGE;
259 if (s->tlsext_ticket_expected) {
260 /* receive renewed session ticket */
261 s->state = SSL3_ST_CR_SESSION_TICKET_A;
262 }
263 } else {
264 s->state = SSL3_ST_CR_CERT_A;
265 }
266 s->init_num = 0;
267 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400268
Adam Langley1bea1732014-12-17 19:06:57 -0800269 case SSL3_ST_CR_CERT_A:
270 case SSL3_ST_CR_CERT_B:
271 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
272 ret = ssl3_get_server_certificate(s);
273 if (ret <= 0) {
274 goto end;
275 }
276 if (s->s3->tmp.certificate_status_expected) {
277 s->state = SSL3_ST_CR_CERT_STATUS_A;
278 } else {
279 s->state = SSL3_ST_CR_KEY_EXCH_A;
280 }
281 } else {
282 skip = 1;
283 s->state = SSL3_ST_CR_KEY_EXCH_A;
284 }
285 s->init_num = 0;
286 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley1bea1732014-12-17 19:06:57 -0800288 case SSL3_ST_CR_KEY_EXCH_A:
289 case SSL3_ST_CR_KEY_EXCH_B:
290 ret = ssl3_get_server_key_exchange(s);
291 if (ret <= 0) {
292 goto end;
293 }
294 s->state = SSL3_ST_CR_CERT_REQ_A;
295 s->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley1bea1732014-12-17 19:06:57 -0800298 case SSL3_ST_CR_CERT_REQ_A:
299 case SSL3_ST_CR_CERT_REQ_B:
300 ret = ssl3_get_certificate_request(s);
301 if (ret <= 0) {
302 goto end;
303 }
304 s->state = SSL3_ST_CR_SRVR_DONE_A;
305 s->init_num = 0;
306 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley1bea1732014-12-17 19:06:57 -0800308 case SSL3_ST_CR_SRVR_DONE_A:
309 case SSL3_ST_CR_SRVR_DONE_B:
310 ret = ssl3_get_server_done(s);
311 if (ret <= 0) {
312 goto end;
313 }
314 if (s->s3->tmp.cert_req) {
315 s->state = SSL3_ST_CW_CERT_A;
316 } else {
317 s->state = SSL3_ST_CW_KEY_EXCH_A;
318 }
319 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700322
Adam Langley1bea1732014-12-17 19:06:57 -0800323 case SSL3_ST_CW_CERT_A:
324 case SSL3_ST_CW_CERT_B:
325 case SSL3_ST_CW_CERT_C:
326 case SSL3_ST_CW_CERT_D:
327 ret = ssl3_send_client_certificate(s);
328 if (ret <= 0) {
329 goto end;
330 }
331 s->state = SSL3_ST_CW_KEY_EXCH_A;
332 s->init_num = 0;
333 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley1bea1732014-12-17 19:06:57 -0800335 case SSL3_ST_CW_KEY_EXCH_A:
336 case SSL3_ST_CW_KEY_EXCH_B:
337 ret = ssl3_send_client_key_exchange(s);
338 if (ret <= 0) {
339 goto end;
340 }
341 /* For TLS, cert_req is set to 2, so a cert chain
342 * of nothing is sent, but no verify packet is sent */
343 if (s->s3->tmp.cert_req == 1) {
344 s->state = SSL3_ST_CW_CERT_VRFY_A;
345 } else {
346 s->state = SSL3_ST_CW_CHANGE_A;
347 s->s3->change_cipher_spec = 0;
348 }
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langley1bea1732014-12-17 19:06:57 -0800350 s->init_num = 0;
351 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley1bea1732014-12-17 19:06:57 -0800353 case SSL3_ST_CW_CERT_VRFY_A:
354 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400355 case SSL3_ST_CW_CERT_VRFY_C:
Adam Langley1bea1732014-12-17 19:06:57 -0800356 ret = ssl3_send_cert_verify(s);
357 if (ret <= 0) {
358 goto end;
359 }
360 s->state = SSL3_ST_CW_CHANGE_A;
361 s->init_num = 0;
362 s->s3->change_cipher_spec = 0;
363 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langley1bea1732014-12-17 19:06:57 -0800365 case SSL3_ST_CW_CHANGE_A:
366 case SSL3_ST_CW_CHANGE_B:
367 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
368 SSL3_ST_CW_CHANGE_B);
369 if (ret <= 0) {
370 goto end;
371 }
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langley1bea1732014-12-17 19:06:57 -0800373 s->state = SSL3_ST_CW_FINISHED_A;
374 if (s->s3->tlsext_channel_id_valid) {
375 s->state = SSL3_ST_CW_CHANNEL_ID_A;
376 }
377 if (s->s3->next_proto_neg_seen) {
378 s->state = SSL3_ST_CW_NEXT_PROTO_A;
379 }
380 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley1bea1732014-12-17 19:06:57 -0800382 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400383 if (!s->enc_method->setup_key_block(s) ||
384 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800385 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
386 ret = -1;
387 goto end;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley1bea1732014-12-17 19:06:57 -0800390 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley1bea1732014-12-17 19:06:57 -0800392 case SSL3_ST_CW_NEXT_PROTO_A:
393 case SSL3_ST_CW_NEXT_PROTO_B:
394 ret = ssl3_send_next_proto(s);
395 if (ret <= 0) {
396 goto end;
397 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700398
Adam Langley1bea1732014-12-17 19:06:57 -0800399 if (s->s3->tlsext_channel_id_valid) {
400 s->state = SSL3_ST_CW_CHANNEL_ID_A;
401 } else {
402 s->state = SSL3_ST_CW_FINISHED_A;
403 }
404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley1bea1732014-12-17 19:06:57 -0800406 case SSL3_ST_CW_CHANNEL_ID_A:
407 case SSL3_ST_CW_CHANNEL_ID_B:
408 ret = ssl3_send_channel_id(s);
409 if (ret <= 0) {
410 goto end;
411 }
412 s->state = SSL3_ST_CW_FINISHED_A;
413 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langley1bea1732014-12-17 19:06:57 -0800415 case SSL3_ST_CW_FINISHED_A:
416 case SSL3_ST_CW_FINISHED_B:
417 ret =
418 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
419 s->enc_method->client_finished_label,
420 s->enc_method->client_finished_label_len);
421 if (ret <= 0) {
422 goto end;
423 }
424 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langley1bea1732014-12-17 19:06:57 -0800426 if (s->hit) {
427 s->s3->tmp.next_state = SSL_ST_OK;
428 } else {
429 /* This is a non-resumption handshake. If it involves ChannelID, then
430 * record the handshake hashes at this point in the session so that
431 * any resumption of this session with ChannelID can sign those
432 * hashes. */
433 if (s->s3->tlsext_channel_id_new) {
434 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500435 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800436 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500437 }
Adam Langley1bea1732014-12-17 19:06:57 -0800438 }
David Benjamined7c4752015-02-16 19:16:46 -0500439 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
440 ssl3_can_false_start(s) &&
441 /* No False Start on renegotiation (would complicate the state
442 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400443 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500444 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800445 } else {
446 /* Allow NewSessionTicket if ticket expected */
447 if (s->tlsext_ticket_expected) {
448 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
449 } else {
450 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
451 }
452 }
453 }
454 s->init_num = 0;
455 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langley1bea1732014-12-17 19:06:57 -0800457 case SSL3_ST_CR_SESSION_TICKET_A:
458 case SSL3_ST_CR_SESSION_TICKET_B:
459 ret = ssl3_get_new_session_ticket(s);
460 if (ret <= 0) {
461 goto end;
462 }
463 s->state = SSL3_ST_CR_CHANGE;
464 s->init_num = 0;
465 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langley1bea1732014-12-17 19:06:57 -0800467 case SSL3_ST_CR_CERT_STATUS_A:
468 case SSL3_ST_CR_CERT_STATUS_B:
469 ret = ssl3_get_cert_status(s);
470 if (ret <= 0) {
471 goto end;
472 }
473 s->state = SSL3_ST_CR_KEY_EXCH_A;
474 s->init_num = 0;
475 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400476
Adam Langley1bea1732014-12-17 19:06:57 -0800477 case SSL3_ST_CR_CHANGE:
478 /* At this point, the next message must be entirely behind a
479 * ChangeCipherSpec. */
480 if (!ssl3_expect_change_cipher_spec(s)) {
481 ret = -1;
482 goto end;
483 }
484 s->state = SSL3_ST_CR_FINISHED_A;
485 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700486
Adam Langley1bea1732014-12-17 19:06:57 -0800487 case SSL3_ST_CR_FINISHED_A:
488 case SSL3_ST_CR_FINISHED_B:
489 ret =
490 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
491 if (ret <= 0) {
492 goto end;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langley1bea1732014-12-17 19:06:57 -0800495 if (s->hit) {
496 s->state = SSL3_ST_CW_CHANGE_A;
497 } else {
498 s->state = SSL_ST_OK;
499 }
500 s->init_num = 0;
501 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley1bea1732014-12-17 19:06:57 -0800503 case SSL3_ST_CW_FLUSH:
504 s->rwstate = SSL_WRITING;
505 if (BIO_flush(s->wbio) <= 0) {
506 ret = -1;
507 goto end;
508 }
509 s->rwstate = SSL_NOTHING;
510 s->state = s->s3->tmp.next_state;
511 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700512
David Benjamined7c4752015-02-16 19:16:46 -0500513 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800514 /* Allow NewSessionTicket if ticket expected */
515 if (s->tlsext_ticket_expected) {
516 s->state = SSL3_ST_CR_SESSION_TICKET_A;
517 } else {
518 s->state = SSL3_ST_CR_CHANGE;
519 }
David Benjamined7c4752015-02-16 19:16:46 -0500520 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700521
Adam Langley1bea1732014-12-17 19:06:57 -0800522 ssl_free_wbio_buffer(s);
523 ret = 1;
524 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley1bea1732014-12-17 19:06:57 -0800526 case SSL_ST_OK:
527 /* clean a few things up */
528 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700529
David Benjamin2755a3e2015-04-22 16:17:58 -0400530 BUF_MEM_free(s->init_buf);
531 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langley1bea1732014-12-17 19:06:57 -0800533 /* Remove write buffering now. */
534 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjaminba4594a2015-06-18 18:36:15 -0400536 const int is_initial_handshake = !s->s3->initial_handshake_complete;
537
Adam Langley1bea1732014-12-17 19:06:57 -0800538 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500539 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400540 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjaminba4594a2015-06-18 18:36:15 -0400542 if (is_initial_handshake) {
543 /* Renegotiations do not participate in session resumption. */
544 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langley1bea1732014-12-17 19:06:57 -0800547 ret = 1;
548 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langley1bea1732014-12-17 19:06:57 -0800550 if (cb != NULL) {
551 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langley1bea1732014-12-17 19:06:57 -0800554 goto end;
555
556 default:
557 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
558 ret = -1;
559 goto end;
560 }
561
562 if (!s->s3->tmp.reuse_message && !skip) {
563 if (cb != NULL && s->state != state) {
564 new_state = s->state;
565 s->state = state;
566 cb(s, SSL_CB_CONNECT_LOOP, 1);
567 s->state = new_state;
568 }
569 }
570 skip = 0;
571 }
572
Adam Langley95c29f32014-06-20 12:00:00 -0700573end:
Adam Langley1bea1732014-12-17 19:06:57 -0800574 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400575 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800576 if (cb != NULL) {
577 cb(s, SSL_CB_CONNECT_EXIT, ret);
578 }
579 return ret;
580}
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langley1bea1732014-12-17 19:06:57 -0800582int ssl3_send_client_hello(SSL *s) {
583 uint8_t *buf, *p, *d;
584 int i;
585 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langley1bea1732014-12-17 19:06:57 -0800587 buf = (uint8_t *)s->init_buf->data;
588 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
589 if (!s->s3->have_version) {
590 uint16_t max_version = ssl3_get_max_client_version(s);
591 /* Disabling all versions is silly: return an error. */
592 if (max_version == 0) {
593 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
594 goto err;
595 }
596 s->version = max_version;
597 s->client_version = max_version;
598 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500599
Adam Langley1bea1732014-12-17 19:06:57 -0800600 /* If the configured session was created at a version higher than our
601 * maximum version, drop it. */
602 if (s->session &&
603 (s->session->session_id_length == 0 || s->session->not_resumable ||
604 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
605 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
606 SSL_set_session(s, NULL);
607 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500608
Adam Langley1bea1732014-12-17 19:06:57 -0800609 /* else use the pre-loaded session */
610 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langley1bea1732014-12-17 19:06:57 -0800612 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
613 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400614 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400615 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
616 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400617 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langley1bea1732014-12-17 19:06:57 -0800620 /* Do the message type and length last. Note: the final argument to
621 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
622 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langley1bea1732014-12-17 19:06:57 -0800624 /* version indicates the negotiated version: for example from an SSLv2/v3
625 * compatible client hello). The client_version field is the maximum
626 * version we permit and it is also used in RSA encrypted premaster
627 * secrets. Some servers can choke if we initially report a higher version
628 * then renegotiate to a lower one in the premaster secret. This didn't
629 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
630 * or later if the server only supports 1.0.
631 *
632 * Possible scenario with previous logic:
633 * 1. Client hello indicates TLS 1.2
634 * 2. Server hello says TLS 1.0
635 * 3. RSA encrypted premaster secret uses 1.2.
636 * 4. Handhaked proceeds using TLS 1.0.
637 * 5. Server sends hello request to renegotiate.
638 * 6. Client hello indicates TLS v1.0 as we now
639 * know that is maximum server supports.
640 * 7. Server chokes on RSA encrypted premaster secret
641 * containing version 1.0.
642 *
643 * For interoperability it should be OK to always use the maximum version
644 * we support in client hello and then rely on the checking of version to
645 * ensure the servers isn't being inconsistent: for example initially
646 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
647 * using client_version in client hello and not resetting it to the
648 * negotiated version. */
649 *(p++) = s->client_version >> 8;
650 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langley1bea1732014-12-17 19:06:57 -0800652 /* Random stuff */
653 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
654 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langley1bea1732014-12-17 19:06:57 -0800656 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400657 if (s->s3->initial_handshake_complete || s->session == NULL) {
658 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800659 i = 0;
660 } else {
661 i = s->session->session_id_length;
662 }
663 *(p++) = i;
664 if (i != 0) {
665 if (i > (int)sizeof(s->session->session_id)) {
666 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
667 goto err;
668 }
669 memcpy(p, s->session->session_id, i);
670 p += i;
671 }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langley1bea1732014-12-17 19:06:57 -0800673 /* cookie stuff for DTLS */
674 if (SSL_IS_DTLS(s)) {
675 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
676 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
677 goto err;
678 }
679 *(p++) = s->d1->cookie_len;
680 memcpy(p, s->d1->cookie, s->d1->cookie_len);
681 p += s->d1->cookie_len;
682 }
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langley1bea1732014-12-17 19:06:57 -0800684 /* Ciphers supported */
685 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
686 if (i == 0) {
687 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
688 SSL_R_NO_CIPHERS_AVAILABLE);
689 goto err;
690 }
691 s2n(i, p);
692 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langley1bea1732014-12-17 19:06:57 -0800694 /* COMPRESSION */
695 *(p++) = 1;
696 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langley1bea1732014-12-17 19:06:57 -0800698 /* TLS extensions*/
699 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
700 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
701 goto err;
702 }
703
704 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
705 p - buf);
706 if (p == NULL) {
707 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
708 goto err;
709 }
710
711 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500712 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
713 goto err;
714 }
Adam Langley1bea1732014-12-17 19:06:57 -0800715 s->state = SSL3_ST_CW_CLNT_HELLO_B;
716 }
717
718 /* SSL3_ST_CW_CLNT_HELLO_B */
719 return ssl_do_write(s);
720
Adam Langley95c29f32014-06-20 12:00:00 -0700721err:
Adam Langley1bea1732014-12-17 19:06:57 -0800722 return -1;
723}
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langley1bea1732014-12-17 19:06:57 -0800725int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400726 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800727 const SSL_CIPHER *c;
728 CERT *ct = s->cert;
729 int al = SSL_AD_INTERNAL_ERROR, ok;
730 long n;
731 CBS server_hello, server_random, session_id;
732 uint16_t server_version, cipher_suite;
733 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400734 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700735
Adam Langley1bea1732014-12-17 19:06:57 -0800736 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
737 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
738 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500739 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langley1bea1732014-12-17 19:06:57 -0800741 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500742 uint32_t err = ERR_peek_error();
743 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
744 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
745 /* Add a dedicated error code to the queue for a handshake_failure alert
746 * in response to ClientHello. This matches NSS's client behavior and
747 * gives a better error on a (probable) failure to negotiate initial
748 * parameters. Note: this error code comes after the original one.
749 *
750 * See https://crbug.com/446505. */
751 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
752 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
753 }
Adam Langley1bea1732014-12-17 19:06:57 -0800754 return n;
755 }
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langley1bea1732014-12-17 19:06:57 -0800757 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400758
Adam Langley1bea1732014-12-17 19:06:57 -0800759 if (!CBS_get_u16(&server_hello, &server_version) ||
760 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
761 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
762 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
763 !CBS_get_u16(&server_hello, &cipher_suite) ||
764 !CBS_get_u8(&server_hello, &compression_method)) {
765 al = SSL_AD_DECODE_ERROR;
766 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
767 goto f_err;
768 }
David Benjamina03d95d2014-07-12 19:49:07 -0400769
David Benjamine6df0542015-05-12 22:02:08 -0400770 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800771 if (!s->s3->have_version) {
772 if (!ssl3_is_version_enabled(s, server_version)) {
773 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
774 s->version = server_version;
775 /* Mark the version as fixed so the record-layer version is not clamped
776 * to TLS 1.0. */
777 s->s3->have_version = 1;
778 al = SSL_AD_PROTOCOL_VERSION;
779 goto f_err;
780 }
781 s->version = server_version;
782 s->enc_method = ssl3_get_enc_method(server_version);
783 assert(s->enc_method != NULL);
784 /* At this point, the connection's version is known and s->version is
785 * fixed. Begin enforcing the record-layer version. */
786 s->s3->have_version = 1;
787 } else if (server_version != s->version) {
788 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
789 al = SSL_AD_PROTOCOL_VERSION;
790 goto f_err;
791 }
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langley1bea1732014-12-17 19:06:57 -0800793 /* Copy over the server random. */
794 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langley1bea1732014-12-17 19:06:57 -0800796 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400797 if (!s->s3->initial_handshake_complete && s->session != NULL &&
798 CBS_mem_equal(&session_id, s->session->session_id,
799 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800800 if (s->sid_ctx_length != s->session->sid_ctx_length ||
801 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
802 /* actually a client application bug */
803 al = SSL_AD_ILLEGAL_PARAMETER;
804 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
805 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
806 goto f_err;
807 }
808 s->hit = 1;
809 } else {
810 /* The session wasn't resumed. Create a fresh SSL_SESSION to
811 * fill out. */
812 s->hit = 0;
813 if (!ssl_get_new_session(s, 0)) {
814 goto f_err;
815 }
816 /* Note: session_id could be empty. */
817 s->session->session_id_length = CBS_len(&session_id);
818 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
819 }
David Benjamina03d95d2014-07-12 19:49:07 -0400820
David Benjamina1c90a52015-05-30 17:03:14 -0400821 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800822 if (c == NULL) {
823 /* unknown cipher */
824 al = SSL_AD_ILLEGAL_PARAMETER;
825 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
826 SSL_R_UNKNOWN_CIPHER_RETURNED);
827 goto f_err;
828 }
829 /* ct->mask_ssl was computed from client capabilities. Now
830 * that the final version is known, compute a new mask_ssl. */
831 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
832 mask_ssl = SSL_TLSV1_2;
833 } else {
834 mask_ssl = 0;
835 }
836 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
837 * the server selected it, it's an error. */
838 if ((c->algorithm_ssl & mask_ssl) ||
839 (c->algorithm_mkey & ct->mask_k) ||
840 (c->algorithm_auth & ct->mask_a)) {
841 al = SSL_AD_ILLEGAL_PARAMETER;
842 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
843 goto f_err;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langley1bea1732014-12-17 19:06:57 -0800846 sk = ssl_get_ciphers_by_id(s);
847 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
848 /* we did not say we would use this cipher */
849 al = SSL_AD_ILLEGAL_PARAMETER;
850 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
851 goto f_err;
852 }
Adam Langley95c29f32014-06-20 12:00:00 -0700853
David Benjaminece3de92015-03-16 18:02:20 -0400854 if (s->hit) {
855 if (s->session->cipher != c) {
856 al = SSL_AD_ILLEGAL_PARAMETER;
857 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
858 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
859 goto f_err;
860 }
861 if (s->session->ssl_version != s->version) {
862 al = SSL_AD_ILLEGAL_PARAMETER;
863 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
864 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
865 goto f_err;
866 }
Adam Langley1bea1732014-12-17 19:06:57 -0800867 }
868 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500869
David Benjamin5f04b652015-05-26 17:30:10 -0400870 /* If doing a full handshake with TLS 1.2, the server may request a client
871 * certificate which requires hashing the handshake transcript under a
872 * different hash. Otherwise, release the handshake buffer. */
873 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800874 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
875 goto f_err;
876 }
David Benjamina03d95d2014-07-12 19:49:07 -0400877
Adam Langley1bea1732014-12-17 19:06:57 -0800878 /* Only the NULL compression algorithm is supported. */
879 if (compression_method != 0) {
880 al = SSL_AD_ILLEGAL_PARAMETER;
881 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
882 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
883 goto f_err;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langley1bea1732014-12-17 19:06:57 -0800886 /* TLS extensions */
887 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
888 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
889 goto err;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langley1bea1732014-12-17 19:06:57 -0800892 /* There should be nothing left over in the record. */
893 if (CBS_len(&server_hello) != 0) {
894 /* wrong packet length */
895 al = SSL_AD_DECODE_ERROR;
896 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
897 goto f_err;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyba5934b2015-06-02 10:50:35 -0700900 if (s->hit &&
901 s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
902 al = SSL_AD_HANDSHAKE_FAILURE;
903 if (s->session->extended_master_secret) {
904 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
905 SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
906 } else {
907 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
908 SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
909 }
910 goto f_err;
911 }
912
Adam Langley1bea1732014-12-17 19:06:57 -0800913 return 1;
914
Adam Langley95c29f32014-06-20 12:00:00 -0700915f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800916 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700917err:
Adam Langley1bea1732014-12-17 19:06:57 -0800918 return -1;
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
David Benjamin436bf822015-06-07 19:50:37 -0400921/* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
922 * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
923 * on the error queue. */
924static int ssl3_check_certificate_for_cipher(X509 *leaf,
925 const SSL_CIPHER *cipher) {
926 int ret = 0;
927 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400928 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400929 goto err;
930 }
931
932 /* Check the certificate's type matches the cipher. */
933 int cert_type = ssl_cert_type(pkey);
934 if (cert_type < 0) {
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_UNKNOWN_CERTIFICATE_TYPE);
937 goto err;
938 }
939 int expected_type = ssl_cipher_get_cert_index(cipher);
940 assert(expected_type >= 0);
941 if (cert_type != expected_type) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400942 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400943 SSL_R_WRONG_CERTIFICATE_TYPE);
944 goto err;
945 }
946
947 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
948 * be checked in other cases as well? */
949 if (cipher->algorithm_auth & SSL_aECDSA) {
950 /* This call populates the ex_flags field correctly */
951 X509_check_purpose(leaf, -1, 0);
952 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
953 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400954 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400955 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
956 goto err;
957 }
958 }
959
960 ret = 1;
961
962err:
963 EVP_PKEY_free(pkey);
964 return ret;
965}
966
Adam Langley1bea1732014-12-17 19:06:57 -0800967int ssl3_get_server_certificate(SSL *s) {
968 int al, i, ok, ret = -1;
969 unsigned long n;
970 X509 *x = NULL;
971 STACK_OF(X509) *sk = NULL;
972 SESS_CERT *sc;
973 EVP_PKEY *pkey = NULL;
974 CBS cbs, certificate_list;
975 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langley1bea1732014-12-17 19:06:57 -0800977 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400978 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500979 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langley1bea1732014-12-17 19:06:57 -0800981 if (!ok) {
982 return n;
983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langley1bea1732014-12-17 19:06:57 -0800985 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langley1bea1732014-12-17 19:06:57 -0800987 sk = sk_X509_new_null();
988 if (sk == NULL) {
989 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
990 goto err;
991 }
Adam Langley95c29f32014-06-20 12:00:00 -0700992
Adam Langley1bea1732014-12-17 19:06:57 -0800993 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400994 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800995 CBS_len(&cbs) != 0) {
996 al = SSL_AD_DECODE_ERROR;
David Benjamin8923c0b2015-06-07 11:42:34 -0400997 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800998 goto f_err;
999 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001000
Adam Langley1bea1732014-12-17 19:06:57 -08001001 while (CBS_len(&certificate_list) > 0) {
1002 CBS certificate;
1003 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1004 al = SSL_AD_DECODE_ERROR;
1005 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1006 SSL_R_CERT_LENGTH_MISMATCH);
1007 goto f_err;
1008 }
1009 data = CBS_data(&certificate);
1010 x = d2i_X509(NULL, &data, CBS_len(&certificate));
1011 if (x == NULL) {
1012 al = SSL_AD_BAD_CERTIFICATE;
1013 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1014 goto f_err;
1015 }
1016 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1017 al = SSL_AD_DECODE_ERROR;
1018 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1019 SSL_R_CERT_LENGTH_MISMATCH);
1020 goto f_err;
1021 }
1022 if (!sk_X509_push(sk, x)) {
1023 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1024 goto err;
1025 }
1026 x = NULL;
1027 }
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langley1bea1732014-12-17 19:06:57 -08001029 i = ssl_verify_cert_chain(s, sk);
1030 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1031 al = ssl_verify_alarm_type(s->verify_result);
1032 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1033 SSL_R_CERTIFICATE_VERIFY_FAILED);
1034 goto f_err;
1035 }
1036 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001037
David Benjamin436bf822015-06-07 19:50:37 -04001038 X509 *leaf = sk_X509_value(sk, 0);
1039 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1040 al = SSL_AD_ILLEGAL_PARAMETER;
1041 goto f_err;
1042 }
1043
Adam Langley1bea1732014-12-17 19:06:57 -08001044 sc = ssl_sess_cert_new();
1045 if (sc == NULL) {
1046 goto err;
1047 }
Adam Langley95c29f32014-06-20 12:00:00 -07001048
David Benjamin2755a3e2015-04-22 16:17:58 -04001049 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001050 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001051
David Benjamin436bf822015-06-07 19:50:37 -04001052 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1053 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001054 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001055 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001056
David Benjaminb31040d2015-06-07 10:53:32 -04001057 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001058 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001059
David Benjamin2755a3e2015-04-22 16:17:58 -04001060 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001061 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001062
Adam Langley1bea1732014-12-17 19:06:57 -08001063 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langley1bea1732014-12-17 19:06:57 -08001065 ret = 1;
1066
1067 if (0) {
1068 f_err:
1069 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1070 }
1071
1072err:
1073 EVP_PKEY_free(pkey);
1074 X509_free(x);
1075 sk_X509_pop_free(sk, X509_free);
1076 return ret;
1077}
1078
1079int ssl3_get_server_key_exchange(SSL *s) {
1080 EVP_MD_CTX md_ctx;
1081 int al, ok;
1082 long n, alg_k, alg_a;
1083 EVP_PKEY *pkey = NULL;
1084 const EVP_MD *md = NULL;
1085 RSA *rsa = NULL;
1086 DH *dh = NULL;
1087 EC_KEY *ecdh = NULL;
1088 BN_CTX *bn_ctx = NULL;
1089 EC_POINT *srvr_ecpoint = NULL;
1090 CBS server_key_exchange, server_key_exchange_orig, parameter;
1091
1092 /* use same message size as in ssl3_get_certificate_request() as
1093 * ServerKeyExchange message may be skipped */
1094 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1095 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001096 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001097 if (!ok) {
1098 return n;
1099 }
1100
1101 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1102 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1103 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1104 SSL_R_UNEXPECTED_MESSAGE);
1105 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1106 return -1;
1107 }
1108
1109 /* In plain PSK ciphersuite, ServerKeyExchange can be
1110 omitted if no identity hint is sent. Set session->sess_cert anyway to
1111 avoid problems later.*/
1112 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1113 /* PSK ciphersuites that also send a Certificate would have already
1114 * initialized |sess_cert|. */
1115 if (s->session->sess_cert == NULL) {
1116 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001117 if (s->session->sess_cert == NULL) {
1118 return -1;
1119 }
Adam Langley1bea1732014-12-17 19:06:57 -08001120 }
1121
1122 /* TODO(davidben): This should be reset in one place with the rest of the
1123 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001124 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1125 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001126 }
1127 s->s3->tmp.reuse_message = 1;
1128 return 1;
1129 }
1130
1131 /* Retain a copy of the original CBS to compute the signature over. */
1132 CBS_init(&server_key_exchange, s->init_msg, n);
1133 server_key_exchange_orig = server_key_exchange;
1134
1135 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001136 DH_free(s->session->sess_cert->peer_dh_tmp);
1137 s->session->sess_cert->peer_dh_tmp = NULL;
1138 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1139 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001140 } else {
1141 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001142 if (s->session->sess_cert == NULL) {
1143 return -1;
1144 }
Adam Langley1bea1732014-12-17 19:06:57 -08001145 }
1146
1147 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1148 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1149 EVP_MD_CTX_init(&md_ctx);
1150
1151 if (alg_a & SSL_aPSK) {
1152 CBS psk_identity_hint;
1153
1154 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1155 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1156 &psk_identity_hint)) {
1157 al = SSL_AD_DECODE_ERROR;
1158 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1159 goto f_err;
1160 }
1161
1162 /* Store PSK identity hint for later use, hint is used in
1163 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1164 * identity hint can be as long as the maximum length of a PSK identity.
1165 * Also do not allow NULL characters; identities are saved as C strings.
1166 *
1167 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1168 * a specific identity. */
1169 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1170 CBS_contains_zero_byte(&psk_identity_hint)) {
1171 al = SSL_AD_HANDSHAKE_FAILURE;
1172 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1173 SSL_R_DATA_LENGTH_TOO_LONG);
1174 goto f_err;
1175 }
1176
1177 /* Save the identity hint as a C string. */
1178 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1179 al = SSL_AD_INTERNAL_ERROR;
1180 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1181 ERR_R_MALLOC_FAILURE);
1182 goto f_err;
1183 }
1184 }
1185
David Benjamin7061e282015-03-19 11:10:48 -04001186 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001187 CBS dh_p, dh_g, dh_Ys;
1188
1189 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1190 CBS_len(&dh_p) == 0 ||
1191 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1192 CBS_len(&dh_g) == 0 ||
1193 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1194 CBS_len(&dh_Ys) == 0) {
1195 al = SSL_AD_DECODE_ERROR;
1196 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1197 goto f_err;
1198 }
1199
1200 dh = DH_new();
1201 if (dh == NULL) {
1202 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1203 goto err;
1204 }
1205
1206 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1207 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1208 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1209 NULL) {
1210 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1211 goto err;
1212 }
1213
Adam Langleya7997f12015-05-14 17:38:50 -07001214 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001215 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1216 SSL_R_BAD_DH_P_LENGTH);
1217 goto err;
1218 }
Adam Langley1bea1732014-12-17 19:06:57 -08001219 s->session->sess_cert->peer_dh_tmp = dh;
1220 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001221 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001222 uint16_t curve_id;
1223 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001224 const EC_GROUP *group;
1225 CBS point;
1226
1227 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1228 * key. Check curve is one of our preferences, if not server has sent an
1229 * invalid curve. */
1230 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1231 al = SSL_AD_DECODE_ERROR;
1232 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1233 goto f_err;
1234 }
1235
1236 curve_nid = tls1_ec_curve_id2nid(curve_id);
1237 if (curve_nid == 0) {
1238 al = SSL_AD_INTERNAL_ERROR;
1239 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1240 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1241 goto f_err;
1242 }
1243
David Benjamin4f7783e2015-03-05 03:05:11 -05001244 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001245 if (ecdh == NULL) {
1246 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001247 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001248 goto err;
1249 }
1250
Adam Langley1bea1732014-12-17 19:06:57 -08001251 group = EC_KEY_get0_group(ecdh);
1252
1253 /* Next, get the encoded ECPoint */
1254 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1255 al = SSL_AD_DECODE_ERROR;
1256 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1257 goto f_err;
1258 }
1259
1260 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1261 ((bn_ctx = BN_CTX_new()) == NULL)) {
1262 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1263 ERR_R_MALLOC_FAILURE);
1264 goto err;
1265 }
1266
1267 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1268 CBS_len(&point), bn_ctx)) {
1269 al = SSL_AD_DECODE_ERROR;
1270 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1271 goto f_err;
1272 }
Adam Langley1bea1732014-12-17 19:06:57 -08001273 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1274 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1275 ecdh = NULL;
1276 BN_CTX_free(bn_ctx);
1277 bn_ctx = NULL;
1278 EC_POINT_free(srvr_ecpoint);
1279 srvr_ecpoint = NULL;
1280 } else if (!(alg_k & SSL_kPSK)) {
1281 al = SSL_AD_UNEXPECTED_MESSAGE;
1282 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1283 SSL_R_UNEXPECTED_MESSAGE);
1284 goto f_err;
1285 }
1286
1287 /* At this point, |server_key_exchange| contains the signature, if any, while
1288 * |server_key_exchange_orig| contains the entire message. From that, derive
1289 * a CBS containing just the parameter. */
1290 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1291 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1292
David Benjaminf4958e72015-06-07 11:00:53 -04001293 /* ServerKeyExchange should be signed by the server's public key. */
1294 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1295 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1296 if (pkey == NULL) {
1297 goto err;
1298 }
Adam Langley1bea1732014-12-17 19:06:57 -08001299
1300 if (SSL_USE_SIGALGS(s)) {
1301 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1302 goto f_err;
1303 }
1304 } else if (pkey->type == EVP_PKEY_RSA) {
1305 md = EVP_md5_sha1();
1306 } else {
1307 md = EVP_sha1();
1308 }
1309
1310 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001311 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001312 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1313 CBS_len(&server_key_exchange) != 0) {
1314 al = SSL_AD_DECODE_ERROR;
1315 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1316 goto f_err;
1317 }
1318
1319 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1320 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1321 SSL3_RANDOM_SIZE) ||
1322 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1323 SSL3_RANDOM_SIZE) ||
1324 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1325 CBS_len(&parameter)) ||
1326 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1327 CBS_len(&signature))) {
1328 /* bad signature */
1329 al = SSL_AD_DECRYPT_ERROR;
1330 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1331 goto f_err;
1332 }
1333 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001334 /* PSK ciphers are the only supported certificate-less ciphers. */
1335 assert(alg_a == SSL_aPSK);
1336
Adam Langley1bea1732014-12-17 19:06:57 -08001337 if (CBS_len(&server_key_exchange) > 0) {
1338 al = SSL_AD_DECODE_ERROR;
1339 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1340 SSL_R_EXTRA_DATA_IN_MESSAGE);
1341 goto f_err;
1342 }
1343 }
1344 EVP_PKEY_free(pkey);
1345 EVP_MD_CTX_cleanup(&md_ctx);
1346 return 1;
1347
Adam Langley95c29f32014-06-20 12:00:00 -07001348f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001349 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001350err:
Adam Langley1bea1732014-12-17 19:06:57 -08001351 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001352 RSA_free(rsa);
1353 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001354 BN_CTX_free(bn_ctx);
1355 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001356 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001357 EVP_MD_CTX_cleanup(&md_ctx);
1358 return -1;
1359}
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langley1bea1732014-12-17 19:06:57 -08001361static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1362 return X509_NAME_cmp(*a, *b);
1363}
Adam Langley95c29f32014-06-20 12:00:00 -07001364
Adam Langley1bea1732014-12-17 19:06:57 -08001365int ssl3_get_certificate_request(SSL *s) {
1366 int ok, ret = 0;
1367 unsigned long n;
1368 X509_NAME *xn = NULL;
1369 STACK_OF(X509_NAME) *ca_sk = NULL;
1370 CBS cbs;
1371 CBS certificate_types;
1372 CBS certificate_authorities;
1373 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001374
Adam Langley1bea1732014-12-17 19:06:57 -08001375 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1376 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001377 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001378
Adam Langley1bea1732014-12-17 19:06:57 -08001379 if (!ok) {
1380 return n;
1381 }
David Benjamin688d8df2014-11-02 23:06:42 -05001382
Adam Langley1bea1732014-12-17 19:06:57 -08001383 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001384
Adam Langley1bea1732014-12-17 19:06:57 -08001385 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1386 s->s3->tmp.reuse_message = 1;
1387 /* If we get here we don't need any cached handshake records as we wont be
1388 * doing client auth. */
1389 if (s->s3->handshake_buffer &&
1390 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1391 goto err;
1392 }
1393 return 1;
1394 }
David Benjamined439582014-07-14 19:13:02 -04001395
Adam Langley1bea1732014-12-17 19:06:57 -08001396 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1397 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1398 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1399 SSL_R_WRONG_MESSAGE_TYPE);
1400 goto err;
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langley1bea1732014-12-17 19:06:57 -08001403 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001404
Adam Langley1bea1732014-12-17 19:06:57 -08001405 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1406 if (ca_sk == NULL) {
1407 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1408 goto err;
1409 }
Adam Langley95c29f32014-06-20 12:00:00 -07001410
Adam Langley1bea1732014-12-17 19:06:57 -08001411 /* get the certificate types */
1412 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1413 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1414 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1415 goto err;
1416 }
David Benjamined439582014-07-14 19:13:02 -04001417
Adam Langley1bea1732014-12-17 19:06:57 -08001418 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1419 &s->s3->tmp.num_certificate_types)) {
1420 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1421 goto err;
1422 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001423
Adam Langley1bea1732014-12-17 19:06:57 -08001424 if (SSL_USE_SIGALGS(s)) {
1425 CBS supported_signature_algorithms;
1426 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1427 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1428 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1429 goto err;
1430 }
David Benjamined439582014-07-14 19:13:02 -04001431
Adam Langley1bea1732014-12-17 19:06:57 -08001432 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1433 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1434 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1435 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1436 goto err;
1437 }
1438 }
David Benjamined439582014-07-14 19:13:02 -04001439
Adam Langley1bea1732014-12-17 19:06:57 -08001440 /* get the CA RDNs */
1441 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1442 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1443 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1444 goto err;
1445 }
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langley1bea1732014-12-17 19:06:57 -08001447 while (CBS_len(&certificate_authorities) > 0) {
1448 CBS distinguished_name;
1449 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1450 &distinguished_name)) {
1451 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1452 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1453 SSL_R_CA_DN_TOO_LONG);
1454 goto err;
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langley1bea1732014-12-17 19:06:57 -08001457 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001458
Adam Langley1bea1732014-12-17 19:06:57 -08001459 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1460 if (xn == NULL) {
1461 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1462 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1463 goto err;
1464 }
Adam Langley95c29f32014-06-20 12:00:00 -07001465
Adam Langley1bea1732014-12-17 19:06:57 -08001466 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1467 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001468 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001469 goto err;
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langley1bea1732014-12-17 19:06:57 -08001472 if (CBS_len(&distinguished_name) != 0) {
1473 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1474 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1475 SSL_R_CA_DN_LENGTH_MISMATCH);
1476 goto err;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langley1bea1732014-12-17 19:06:57 -08001479 if (!sk_X509_NAME_push(ca_sk, xn)) {
1480 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1481 ERR_R_MALLOC_FAILURE);
1482 goto err;
1483 }
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langley1bea1732014-12-17 19:06:57 -08001486 /* we should setup a certificate to return.... */
1487 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001488 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001489 s->s3->tmp.ca_names = ca_sk;
1490 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001491
Adam Langley1bea1732014-12-17 19:06:57 -08001492 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langley1bea1732014-12-17 19:06:57 -08001494err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001495 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001496 return ret;
1497}
Adam Langley95c29f32014-06-20 12:00:00 -07001498
Adam Langley1bea1732014-12-17 19:06:57 -08001499int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001500 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001501 long n;
1502 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001503
Adam Langley1bea1732014-12-17 19:06:57 -08001504 n = s->method->ssl_get_message(
1505 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001506 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001507
Adam Langley1bea1732014-12-17 19:06:57 -08001508 if (!ok) {
1509 return n;
1510 }
Adam Langley95c29f32014-06-20 12:00:00 -07001511
David Benjamin95d31822015-06-15 19:53:32 -04001512 if (s->hit) {
1513 /* The server is sending a new ticket for an existing session. Sessions are
1514 * immutable once established, so duplicate all but the ticket of the
1515 * existing session. */
1516 uint8_t *bytes;
1517 size_t bytes_len;
1518 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &bytes, &bytes_len)) {
1519 goto err;
1520 }
1521 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1522 OPENSSL_free(bytes);
1523 if (new_session == NULL) {
1524 /* This should never happen. */
1525 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_INTERNAL_ERROR);
1526 goto err;
1527 }
David Benjamin680ca962015-06-18 12:37:23 -04001528 if (s->session->sess_cert != NULL) {
1529 /* |sess_cert| is not serialized and must be duplicated explicitly. */
1530 assert(new_session->sess_cert == NULL);
1531 new_session->sess_cert = ssl_sess_cert_dup(s->session->sess_cert);
1532 if (new_session->sess_cert == NULL) {
1533 SSL_SESSION_free(new_session);
1534 goto err;
1535 }
1536 }
David Benjamin95d31822015-06-15 19:53:32 -04001537
1538 SSL_SESSION_free(s->session);
1539 s->session = new_session;
1540 }
1541
Adam Langley1bea1732014-12-17 19:06:57 -08001542 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001543
Adam Langley1bea1732014-12-17 19:06:57 -08001544 if (!CBS_get_u32(&new_session_ticket,
1545 &s->session->tlsext_tick_lifetime_hint) ||
1546 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1547 CBS_len(&new_session_ticket) != 0) {
1548 al = SSL_AD_DECODE_ERROR;
1549 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1550 goto f_err;
1551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langley1bea1732014-12-17 19:06:57 -08001553 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1554 &s->session->tlsext_ticklen)) {
1555 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1556 goto err;
1557 }
Adam Langley95c29f32014-06-20 12:00:00 -07001558
David Benjamin68070622015-02-08 23:44:59 -05001559 /* Generate a session ID for this session based on the session ticket. We use
1560 * the session ID mechanism for detecting ticket resumption. This also fits in
1561 * with assumptions elsewhere in OpenSSL.*/
1562 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1563 &s->session->session_id_length, EVP_sha256(), NULL)) {
1564 goto err;
1565 }
1566
1567 return 1;
David Benjamined439582014-07-14 19:13:02 -04001568
Adam Langley95c29f32014-06-20 12:00:00 -07001569f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001570 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001571err:
Adam Langley1bea1732014-12-17 19:06:57 -08001572 return -1;
1573}
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langley1bea1732014-12-17 19:06:57 -08001575int ssl3_get_cert_status(SSL *s) {
1576 int ok, al;
1577 long n;
1578 CBS certificate_status, ocsp_response;
1579 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001580
Adam Langley1bea1732014-12-17 19:06:57 -08001581 n = s->method->ssl_get_message(
1582 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001583 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585 if (!ok) {
1586 return n;
1587 }
Adam Langley95c29f32014-06-20 12:00:00 -07001588
David Benjamindcd979f2015-04-20 18:26:52 -04001589 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1590 /* A server may send status_request in ServerHello and then change
1591 * its mind about sending CertificateStatus. */
1592 s->s3->tmp.reuse_message = 1;
1593 return 1;
1594 }
1595
Adam Langley1bea1732014-12-17 19:06:57 -08001596 CBS_init(&certificate_status, s->init_msg, n);
1597 if (!CBS_get_u8(&certificate_status, &status_type) ||
1598 status_type != TLSEXT_STATUSTYPE_ocsp ||
1599 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1600 CBS_len(&ocsp_response) == 0 ||
1601 CBS_len(&certificate_status) != 0) {
1602 al = SSL_AD_DECODE_ERROR;
1603 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1604 goto f_err;
1605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606
Adam Langley1bea1732014-12-17 19:06:57 -08001607 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1608 &s->session->ocsp_response_length)) {
1609 al = SSL_AD_INTERNAL_ERROR;
1610 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1611 goto f_err;
1612 }
1613 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001614
Adam Langley95c29f32014-06-20 12:00:00 -07001615f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001616 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1617 return -1;
1618}
Adam Langley95c29f32014-06-20 12:00:00 -07001619
Adam Langley1bea1732014-12-17 19:06:57 -08001620int ssl3_get_server_done(SSL *s) {
1621 int ok;
1622 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1625 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1626 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001627 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001628
Adam Langley1bea1732014-12-17 19:06:57 -08001629 if (!ok) {
1630 return n;
1631 }
David Benjamin46062682014-07-14 19:14:32 -04001632
Adam Langley1bea1732014-12-17 19:06:57 -08001633 if (n > 0) {
1634 /* should contain no data */
1635 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1636 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1637 return -1;
1638 }
David Benjamin46062682014-07-14 19:14:32 -04001639
Adam Langley1bea1732014-12-17 19:06:57 -08001640 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001641}
Adam Langley1258b6a2014-06-20 12:00:00 -07001642
Adam Langley1258b6a2014-06-20 12:00:00 -07001643
Adam Langley1bea1732014-12-17 19:06:57 -08001644int ssl3_send_client_key_exchange(SSL *s) {
1645 uint8_t *p;
1646 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001647 uint32_t alg_k;
1648 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001649 uint8_t *q;
1650 EVP_PKEY *pkey = NULL;
1651 EC_KEY *clnt_ecdh = NULL;
1652 const EC_POINT *srvr_ecpoint = NULL;
1653 EVP_PKEY *srvr_pub_pkey = NULL;
1654 uint8_t *encodedPoint = NULL;
1655 int encoded_pt_len = 0;
1656 BN_CTX *bn_ctx = NULL;
1657 unsigned int psk_len = 0;
1658 uint8_t psk[PSK_MAX_PSK_LEN];
1659 uint8_t *pms = NULL;
1660 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001661
Adam Langley1bea1732014-12-17 19:06:57 -08001662 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1663 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001664
Adam Langley1bea1732014-12-17 19:06:57 -08001665 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1666 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001667
Adam Langley1bea1732014-12-17 19:06:57 -08001668 /* If using a PSK key exchange, prepare the pre-shared key. */
1669 if (alg_a & SSL_aPSK) {
1670 char identity[PSK_MAX_IDENTITY_LEN + 1];
1671 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001672
Adam Langley1bea1732014-12-17 19:06:57 -08001673 if (s->psk_client_callback == NULL) {
1674 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1675 SSL_R_PSK_NO_CLIENT_CB);
1676 goto err;
1677 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001678
Adam Langley1bea1732014-12-17 19:06:57 -08001679 memset(identity, 0, sizeof(identity));
1680 psk_len =
1681 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1682 sizeof(identity), psk, sizeof(psk));
1683 if (psk_len > PSK_MAX_PSK_LEN) {
1684 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1685 ERR_R_INTERNAL_ERROR);
1686 goto err;
1687 } else if (psk_len == 0) {
1688 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1689 SSL_R_PSK_IDENTITY_NOT_FOUND);
1690 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1691 goto err;
1692 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001693
Adam Langley1bea1732014-12-17 19:06:57 -08001694 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1695 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1696 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1697 ERR_R_INTERNAL_ERROR);
1698 goto err;
1699 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001700
David Benjamin2755a3e2015-04-22 16:17:58 -04001701 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001702 s->session->psk_identity = BUF_strdup(identity);
1703 if (s->session->psk_identity == NULL) {
1704 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1705 ERR_R_MALLOC_FAILURE);
1706 goto err;
1707 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001708
Adam Langley1bea1732014-12-17 19:06:57 -08001709 /* Write out psk_identity. */
1710 s2n(identity_len, p);
1711 memcpy(p, identity, identity_len);
1712 p += identity_len;
1713 n = 2 + identity_len;
1714 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001715
Adam Langley1bea1732014-12-17 19:06:57 -08001716 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1717 if (alg_k & SSL_kRSA) {
1718 RSA *rsa;
1719 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001720
Adam Langley1bea1732014-12-17 19:06:57 -08001721 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1722 pms = OPENSSL_malloc(pms_len);
1723 if (pms == NULL) {
1724 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1725 ERR_R_MALLOC_FAILURE);
1726 goto err;
1727 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001728
Adam Langley1bea1732014-12-17 19:06:57 -08001729 if (s->session->sess_cert == NULL) {
1730 /* We should always have a server certificate with SSL_kRSA. */
1731 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1732 ERR_R_INTERNAL_ERROR);
1733 goto err;
1734 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001735
David Benjaminb31040d2015-06-07 10:53:32 -04001736 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001737 if (pkey == NULL ||
1738 pkey->type != EVP_PKEY_RSA ||
1739 pkey->pkey.rsa == NULL) {
1740 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1741 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001742 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001743 goto err;
1744 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001745
Adam Langley1bea1732014-12-17 19:06:57 -08001746 rsa = pkey->pkey.rsa;
1747 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001748
Adam Langley1bea1732014-12-17 19:06:57 -08001749 pms[0] = s->client_version >> 8;
1750 pms[1] = s->client_version & 0xff;
1751 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1752 goto err;
1753 }
1754
1755 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1756
1757 q = p;
1758 /* In TLS and beyond, reserve space for the length prefix. */
1759 if (s->version > SSL3_VERSION) {
1760 p += 2;
1761 n += 2;
1762 }
1763 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1764 RSA_PKCS1_PADDING)) {
1765 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1766 SSL_R_BAD_RSA_ENCRYPT);
1767 goto err;
1768 }
1769 n += enc_pms_len;
1770
1771 /* Log the premaster secret, if logging is enabled. */
1772 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1773 pms_len)) {
1774 goto err;
1775 }
1776
1777 /* Fill in the length prefix. */
1778 if (s->version > SSL3_VERSION) {
1779 s2n(enc_pms_len, q);
1780 }
David Benjamin7061e282015-03-19 11:10:48 -04001781 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001782 DH *dh_srvr, *dh_clnt;
1783 SESS_CERT *scert = s->session->sess_cert;
1784 int dh_len;
1785 size_t pub_len;
1786
1787 if (scert == NULL) {
1788 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1789 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1790 SSL_R_UNEXPECTED_MESSAGE);
1791 goto err;
1792 }
1793
1794 if (scert->peer_dh_tmp == NULL) {
1795 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1796 ERR_R_INTERNAL_ERROR);
1797 goto err;
1798 }
1799 dh_srvr = scert->peer_dh_tmp;
1800
1801 /* generate a new random key */
1802 dh_clnt = DHparams_dup(dh_srvr);
1803 if (dh_clnt == NULL) {
1804 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1805 goto err;
1806 }
1807 if (!DH_generate_key(dh_clnt)) {
1808 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1809 DH_free(dh_clnt);
1810 goto err;
1811 }
1812
1813 pms_len = DH_size(dh_clnt);
1814 pms = OPENSSL_malloc(pms_len);
1815 if (pms == NULL) {
1816 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1817 ERR_R_MALLOC_FAILURE);
1818 DH_free(dh_clnt);
1819 goto err;
1820 }
1821
1822 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1823 if (dh_len <= 0) {
1824 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1825 DH_free(dh_clnt);
1826 goto err;
1827 }
1828 pms_len = dh_len;
1829
1830 /* send off the data */
1831 pub_len = BN_num_bytes(dh_clnt->pub_key);
1832 s2n(pub_len, p);
1833 BN_bn2bin(dh_clnt->pub_key, p);
1834 n += 2 + pub_len;
1835
1836 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001837 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001838 const EC_GROUP *srvr_group = NULL;
1839 EC_KEY *tkey;
1840 int field_size = 0, ecdh_len;
1841
1842 if (s->session->sess_cert == NULL) {
1843 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1844 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1845 SSL_R_UNEXPECTED_MESSAGE);
1846 goto err;
1847 }
1848
1849 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1850 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1851 ERR_R_INTERNAL_ERROR);
1852 goto err;
1853 }
1854
1855 tkey = s->session->sess_cert->peer_ecdh_tmp;
1856
1857 srvr_group = EC_KEY_get0_group(tkey);
1858 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1859 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1860 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1861 ERR_R_INTERNAL_ERROR);
1862 goto err;
1863 }
1864
1865 clnt_ecdh = EC_KEY_new();
1866 if (clnt_ecdh == NULL) {
1867 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1868 ERR_R_MALLOC_FAILURE);
1869 goto err;
1870 }
1871
1872 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1873 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1874 goto err;
1875 }
1876
1877 /* Generate a new ECDH key pair */
1878 if (!EC_KEY_generate_key(clnt_ecdh)) {
1879 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1880 goto err;
1881 }
1882
1883 field_size = EC_GROUP_get_degree(srvr_group);
1884 if (field_size <= 0) {
1885 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1886 goto err;
1887 }
1888
1889 pms_len = (field_size + 7) / 8;
1890 pms = OPENSSL_malloc(pms_len);
1891 if (pms == NULL) {
1892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1893 ERR_R_MALLOC_FAILURE);
1894 goto err;
1895 }
1896
1897 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1898 if (ecdh_len <= 0) {
1899 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1900 goto err;
1901 }
1902 pms_len = ecdh_len;
1903
1904 /* First check the size of encoding and allocate memory accordingly. */
1905 encoded_pt_len =
1906 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1907 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1908
1909 encodedPoint =
1910 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1911 bn_ctx = BN_CTX_new();
1912 if (encodedPoint == NULL || bn_ctx == NULL) {
1913 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1914 ERR_R_MALLOC_FAILURE);
1915 goto err;
1916 }
1917
1918 /* Encode the public key */
1919 encoded_pt_len = EC_POINT_point2oct(
1920 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1921 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1922
1923 *p = encoded_pt_len; /* length of encoded point */
1924 /* Encoded point will be copied here */
1925 p += 1;
1926 n += 1;
1927 /* copy the point */
1928 memcpy(p, encodedPoint, encoded_pt_len);
1929 /* increment n to account for length field */
1930 n += encoded_pt_len;
1931
1932 /* Free allocated memory */
1933 BN_CTX_free(bn_ctx);
1934 bn_ctx = NULL;
1935 OPENSSL_free(encodedPoint);
1936 encodedPoint = NULL;
1937 EC_KEY_free(clnt_ecdh);
1938 clnt_ecdh = NULL;
1939 EVP_PKEY_free(srvr_pub_pkey);
1940 srvr_pub_pkey = NULL;
1941 } else if (alg_k & SSL_kPSK) {
1942 /* For plain PSK, other_secret is a block of 0s with the same length as
1943 * the pre-shared key. */
1944 pms_len = psk_len;
1945 pms = OPENSSL_malloc(pms_len);
1946 if (pms == NULL) {
1947 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1948 ERR_R_MALLOC_FAILURE);
1949 goto err;
1950 }
1951 memset(pms, 0, pms_len);
1952 } else {
1953 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1954 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1955 ERR_R_INTERNAL_ERROR);
1956 goto err;
1957 }
1958
1959 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1960 * key. */
1961 if (alg_a & SSL_aPSK) {
1962 CBB cbb, child;
1963 uint8_t *new_pms;
1964 size_t new_pms_len;
1965
David Benjamina8653202015-06-28 01:26:10 -04001966 CBB_zero(&cbb);
1967 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len) ||
1968 !CBB_add_u16_length_prefixed(&cbb, &child) ||
Adam Langley1bea1732014-12-17 19:06:57 -08001969 !CBB_add_bytes(&child, pms, pms_len) ||
1970 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1971 !CBB_add_bytes(&child, psk, psk_len) ||
1972 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1973 CBB_cleanup(&cbb);
1974 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
David Benjamina8653202015-06-28 01:26:10 -04001975 ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001976 goto err;
1977 }
1978 OPENSSL_cleanse(pms, pms_len);
1979 OPENSSL_free(pms);
1980 pms = new_pms;
1981 pms_len = new_pms_len;
1982 }
1983
1984 /* The message must be added to the finished hash before calculating the
1985 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001986 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1987 goto err;
1988 }
Adam Langley1bea1732014-12-17 19:06:57 -08001989 s->state = SSL3_ST_CW_KEY_EXCH_B;
1990
1991 s->session->master_key_length = s->enc_method->generate_master_secret(
1992 s, s->session->master_key, pms, pms_len);
1993 if (s->session->master_key_length == 0) {
1994 goto err;
1995 }
1996 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1997 OPENSSL_cleanse(pms, pms_len);
1998 OPENSSL_free(pms);
1999 }
2000
2001 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05002002 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002003
2004err:
Adam Langley1bea1732014-12-17 19:06:57 -08002005 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002006 OPENSSL_free(encodedPoint);
2007 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08002008 EVP_PKEY_free(srvr_pub_pkey);
2009 if (pms) {
2010 OPENSSL_cleanse(pms, pms_len);
2011 OPENSSL_free(pms);
2012 }
2013 return -1;
2014}
Adam Langley1258b6a2014-06-20 12:00:00 -07002015
Adam Langley1bea1732014-12-17 19:06:57 -08002016int ssl3_send_cert_verify(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002017 if (s->state == SSL3_ST_CW_CERT_VRFY_A ||
2018 s->state == SSL3_ST_CW_CERT_VRFY_B) {
2019 enum ssl_private_key_result_t sign_result;
2020 uint8_t *p = ssl_handshake_start(s);
2021 size_t signature_length = 0;
2022 unsigned long n = 0;
2023 EVP_PKEY *pkey = s->cert->key->privatekey;
2024 assert(pkey != NULL || s->cert->key_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002025
David Benjaminb4d65fd2015-05-29 17:11:21 -04002026 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2027 uint8_t *buf = (uint8_t *)s->init_buf->data;
2028 const EVP_MD *md = NULL;
2029 uint8_t digest[EVP_MAX_MD_SIZE];
2030 size_t digest_length;
Adam Langley1bea1732014-12-17 19:06:57 -08002031
David Benjaminb4d65fd2015-05-29 17:11:21 -04002032 /* Write out the digest type if need be. */
2033 if (SSL_USE_SIGALGS(s)) {
2034 md = tls1_choose_signing_digest(s, pkey);
2035 if (!tls12_get_sigandhash(s, p, pkey, md)) {
2036 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2037 return -1;
2038 }
2039 p += 2;
2040 n += 2;
Adam Langley1bea1732014-12-17 19:06:57 -08002041 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002042
2043 /* Compute the digest. */
2044 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2045 return -1;
2046 }
2047
2048 /* The handshake buffer is no longer necessary. */
2049 if (s->s3->handshake_buffer &&
2050 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2051 return -1;
2052 }
2053
2054 /* Sign the digest. */
2055 signature_length = ssl_private_key_max_signature_len(s, pkey);
2056 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2057 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify,
2058 SSL_R_DATA_LENGTH_TOO_LONG);
2059 return -1;
2060 }
2061
2062 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2063 sign_result = ssl_private_key_sign(s, pkey, &p[2], &signature_length,
2064 signature_length, md, digest,
2065 digest_length);
2066 } else {
2067 if (SSL_USE_SIGALGS(s)) {
2068 /* The digest has already been selected and written. */
2069 p += 2;
2070 n += 2;
2071 }
2072 signature_length = ssl_private_key_max_signature_len(s, pkey);
2073 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2074 sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length,
2075 signature_length);
Adam Langley1bea1732014-12-17 19:06:57 -08002076 }
2077
David Benjaminb4d65fd2015-05-29 17:11:21 -04002078 if (sign_result == ssl_private_key_retry) {
2079 s->state = SSL3_ST_CW_CERT_VRFY_B;
2080 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002081 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002082 s->rwstate = SSL_NOTHING;
2083 if (sign_result != ssl_private_key_success) {
2084 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002085 }
2086
2087 s2n(signature_length, p);
2088 n += signature_length + 2;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002089 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002090 return -1;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002091 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002092 s->state = SSL3_ST_CW_CERT_VRFY_C;
Adam Langley1bea1732014-12-17 19:06:57 -08002093 }
2094
Adam Langley1bea1732014-12-17 19:06:57 -08002095 return ssl_do_write(s);
Adam Langley1bea1732014-12-17 19:06:57 -08002096}
2097
2098/* ssl3_has_client_certificate returns true if a client certificate is
2099 * configured. */
2100static int ssl3_has_client_certificate(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002101 return s->cert && s->cert->key->x509 && (s->cert->key->privatekey ||
2102 s->cert->key_method);
Adam Langley1bea1732014-12-17 19:06:57 -08002103}
2104
2105int ssl3_send_client_certificate(SSL *s) {
2106 X509 *x509 = NULL;
2107 EVP_PKEY *pkey = NULL;
2108 int i;
2109
2110 if (s->state == SSL3_ST_CW_CERT_A) {
2111 /* Let cert callback update client certificates if required */
2112 if (s->cert->cert_cb) {
2113 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2114 if (i < 0) {
2115 s->rwstate = SSL_X509_LOOKUP;
2116 return -1;
2117 }
2118 if (i == 0) {
2119 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2120 return 0;
2121 }
2122 s->rwstate = SSL_NOTHING;
2123 }
2124
2125 if (ssl3_has_client_certificate(s)) {
2126 s->state = SSL3_ST_CW_CERT_C;
2127 } else {
2128 s->state = SSL3_ST_CW_CERT_B;
2129 }
2130 }
2131
2132 /* We need to get a client cert */
2133 if (s->state == SSL3_ST_CW_CERT_B) {
2134 /* If we get an error, we need to:
2135 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2136 * We then get retried later */
2137 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2138 if (i < 0) {
2139 s->rwstate = SSL_X509_LOOKUP;
2140 return -1;
2141 }
2142 s->rwstate = SSL_NOTHING;
2143 if (i == 1 && pkey != NULL && x509 != NULL) {
2144 s->state = SSL3_ST_CW_CERT_B;
2145 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2146 i = 0;
2147 }
2148 } else if (i == 1) {
2149 i = 0;
2150 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2151 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2152 }
2153
David Benjamin2755a3e2015-04-22 16:17:58 -04002154 X509_free(x509);
2155 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002156 if (i && !ssl3_has_client_certificate(s)) {
2157 i = 0;
2158 }
2159 if (i == 0) {
2160 if (s->version == SSL3_VERSION) {
2161 s->s3->tmp.cert_req = 0;
2162 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2163 return 1;
2164 } else {
2165 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002166 /* There is no client certificate, so the handshake buffer may be
2167 * released. */
2168 if (s->s3->handshake_buffer &&
2169 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2170 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2171 return -1;
2172 }
Adam Langley1bea1732014-12-17 19:06:57 -08002173 }
2174 }
2175
2176 /* Ok, we have a cert */
2177 s->state = SSL3_ST_CW_CERT_C;
2178 }
2179
2180 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002181 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2182 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2183 return -1;
2184 }
Adam Langley1bea1732014-12-17 19:06:57 -08002185 }
2186
2187 /* SSL3_ST_CW_CERT_D */
2188 return ssl_do_write(s);
2189}
2190
Adam Langley1bea1732014-12-17 19:06:57 -08002191int ssl3_send_next_proto(SSL *s) {
2192 unsigned int len, padding_len;
2193 uint8_t *d, *p;
2194
2195 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2196 len = s->next_proto_negotiated_len;
2197 padding_len = 32 - ((len + 2) % 32);
2198
2199 d = p = ssl_handshake_start(s);
2200 *(p++) = len;
2201 memcpy(p, s->next_proto_negotiated, len);
2202 p += len;
2203 *(p++) = padding_len;
2204 memset(p, 0, padding_len);
2205 p += padding_len;
2206
David Benjaminfbdfefb2015-02-16 19:33:53 -05002207 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2208 return -1;
2209 }
Adam Langley1bea1732014-12-17 19:06:57 -08002210 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2211 }
2212
2213 return ssl_do_write(s);
2214}
2215
2216int ssl3_send_channel_id(SSL *s) {
2217 uint8_t *d;
2218 int ret = -1, public_key_len;
2219 EVP_MD_CTX md_ctx;
Adam Langley1bea1732014-12-17 19:06:57 -08002220 ECDSA_SIG *sig = NULL;
2221 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2222
2223 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2224 return ssl_do_write(s);
2225 }
2226
2227 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2228 EVP_PKEY *key = NULL;
2229 s->ctx->channel_id_cb(s, &key);
2230 if (key != NULL) {
2231 s->tlsext_channel_id_private = key;
2232 }
2233 }
2234
2235 if (!s->tlsext_channel_id_private) {
2236 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2237 return -1;
2238 }
2239 s->rwstate = SSL_NOTHING;
2240
David Benjamin961ad6a2015-06-12 01:40:23 -04002241 if (EVP_PKEY_id(s->tlsext_channel_id_private) != EVP_PKEY_EC) {
2242 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2243 return -1;
2244 }
2245 EC_KEY *ec_key = s->tlsext_channel_id_private->pkey.ec;
2246
Adam Langley1bea1732014-12-17 19:06:57 -08002247 d = ssl_handshake_start(s);
2248 if (s->s3->tlsext_channel_id_new) {
2249 s2n(TLSEXT_TYPE_channel_id_new, d);
2250 } else {
2251 s2n(TLSEXT_TYPE_channel_id, d);
2252 }
2253 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2254
2255 EVP_MD_CTX_init(&md_ctx);
2256
David Benjamin961ad6a2015-06-12 01:40:23 -04002257 public_key_len = i2o_ECPublicKey(ec_key, NULL);
Adam Langley1bea1732014-12-17 19:06:57 -08002258 if (public_key_len <= 0) {
2259 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2260 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2261 goto err;
2262 }
2263
David Benjamin961ad6a2015-06-12 01:40:23 -04002264 /* i2o_ECPublicKey will produce an ANSI X9.62 public key which, for a
Adam Langley1bea1732014-12-17 19:06:57 -08002265 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2266 * field elements as 32-byte, big-endian numbers. */
2267 if (public_key_len != 65) {
2268 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2269 goto err;
2270 }
2271 public_key = OPENSSL_malloc(public_key_len);
2272 if (!public_key) {
2273 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2274 goto err;
2275 }
2276
2277 derp = public_key;
David Benjamin961ad6a2015-06-12 01:40:23 -04002278 i2o_ECPublicKey(ec_key, &derp);
Adam Langley1bea1732014-12-17 19:06:57 -08002279
David Benjamin961ad6a2015-06-12 01:40:23 -04002280 uint8_t digest[EVP_MAX_MD_SIZE];
2281 unsigned digest_len;
2282 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2283 !tls1_channel_id_hash(&md_ctx, s) ||
2284 !EVP_DigestFinal_ex(&md_ctx, digest, &digest_len)) {
Adam Langley1bea1732014-12-17 19:06:57 -08002285 goto err;
2286 }
2287
David Benjamin961ad6a2015-06-12 01:40:23 -04002288 sig = ECDSA_do_sign(digest, digest_len, ec_key);
Adam Langley1bea1732014-12-17 19:06:57 -08002289 if (sig == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08002290 goto err;
2291 }
2292
2293 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2294 memcpy(d, public_key + 1, 64);
2295 d += 64;
2296 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2297 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2298 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2299 goto err;
2300 }
2301
David Benjaminfbdfefb2015-02-16 19:33:53 -05002302 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2303 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2304 goto err;
2305 }
Adam Langley1bea1732014-12-17 19:06:57 -08002306 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2307
2308 ret = ssl_do_write(s);
2309
2310err:
2311 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002312 OPENSSL_free(public_key);
2313 OPENSSL_free(der_sig);
2314 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002315
2316 return ret;
2317}
2318
2319int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2320 int i = 0;
2321 if (s->ctx->client_cert_cb) {
2322 i = s->ctx->client_cert_cb(s, px509, ppkey);
2323 }
2324 return i;
2325}