blob: 58f92cea22e83ffed482cb2209bc09d06b016981 [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)) {
David Benjamin3570d732015-06-29 00:28:17 -0400224 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800225 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. */
Adam Langley49c7af12015-07-10 14:33:46 -0700433 ret = tls1_record_handshake_hashes_for_channel_id(s);
434 if (ret <= 0) {
435 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800436 }
David Benjamined7c4752015-02-16 19:16:46 -0500437 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
438 ssl3_can_false_start(s) &&
439 /* No False Start on renegotiation (would complicate the state
440 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400441 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500442 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800443 } else {
444 /* Allow NewSessionTicket if ticket expected */
445 if (s->tlsext_ticket_expected) {
446 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
447 } else {
448 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
449 }
450 }
451 }
452 s->init_num = 0;
453 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langley1bea1732014-12-17 19:06:57 -0800455 case SSL3_ST_CR_SESSION_TICKET_A:
456 case SSL3_ST_CR_SESSION_TICKET_B:
457 ret = ssl3_get_new_session_ticket(s);
458 if (ret <= 0) {
459 goto end;
460 }
461 s->state = SSL3_ST_CR_CHANGE;
462 s->init_num = 0;
463 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langley1bea1732014-12-17 19:06:57 -0800465 case SSL3_ST_CR_CERT_STATUS_A:
466 case SSL3_ST_CR_CERT_STATUS_B:
467 ret = ssl3_get_cert_status(s);
468 if (ret <= 0) {
469 goto end;
470 }
471 s->state = SSL3_ST_CR_KEY_EXCH_A;
472 s->init_num = 0;
473 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400474
Adam Langley1bea1732014-12-17 19:06:57 -0800475 case SSL3_ST_CR_CHANGE:
476 /* At this point, the next message must be entirely behind a
477 * ChangeCipherSpec. */
478 if (!ssl3_expect_change_cipher_spec(s)) {
479 ret = -1;
480 goto end;
481 }
482 s->state = SSL3_ST_CR_FINISHED_A;
483 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langley1bea1732014-12-17 19:06:57 -0800485 case SSL3_ST_CR_FINISHED_A:
486 case SSL3_ST_CR_FINISHED_B:
487 ret =
488 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
489 if (ret <= 0) {
490 goto end;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langley1bea1732014-12-17 19:06:57 -0800493 if (s->hit) {
494 s->state = SSL3_ST_CW_CHANGE_A;
495 } else {
496 s->state = SSL_ST_OK;
497 }
498 s->init_num = 0;
499 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley1bea1732014-12-17 19:06:57 -0800501 case SSL3_ST_CW_FLUSH:
502 s->rwstate = SSL_WRITING;
503 if (BIO_flush(s->wbio) <= 0) {
504 ret = -1;
505 goto end;
506 }
507 s->rwstate = SSL_NOTHING;
508 s->state = s->s3->tmp.next_state;
509 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700510
David Benjamined7c4752015-02-16 19:16:46 -0500511 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800512 /* Allow NewSessionTicket if ticket expected */
513 if (s->tlsext_ticket_expected) {
514 s->state = SSL3_ST_CR_SESSION_TICKET_A;
515 } else {
516 s->state = SSL3_ST_CR_CHANGE;
517 }
David Benjamined7c4752015-02-16 19:16:46 -0500518 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700519
Adam Langley1bea1732014-12-17 19:06:57 -0800520 ssl_free_wbio_buffer(s);
521 ret = 1;
522 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley1bea1732014-12-17 19:06:57 -0800524 case SSL_ST_OK:
525 /* clean a few things up */
526 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamin2755a3e2015-04-22 16:17:58 -0400528 BUF_MEM_free(s->init_buf);
529 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langley1bea1732014-12-17 19:06:57 -0800531 /* Remove write buffering now. */
532 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjaminba4594a2015-06-18 18:36:15 -0400534 const int is_initial_handshake = !s->s3->initial_handshake_complete;
535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500537 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400538 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjaminba4594a2015-06-18 18:36:15 -0400540 if (is_initial_handshake) {
541 /* Renegotiations do not participate in session resumption. */
542 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley1bea1732014-12-17 19:06:57 -0800545 ret = 1;
546 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langley1bea1732014-12-17 19:06:57 -0800548 if (cb != NULL) {
549 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langley1bea1732014-12-17 19:06:57 -0800552 goto end;
553
554 default:
David Benjamin3570d732015-06-29 00:28:17 -0400555 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800556 ret = -1;
557 goto end;
558 }
559
560 if (!s->s3->tmp.reuse_message && !skip) {
561 if (cb != NULL && s->state != state) {
562 new_state = s->state;
563 s->state = state;
564 cb(s, SSL_CB_CONNECT_LOOP, 1);
565 s->state = new_state;
566 }
567 }
568 skip = 0;
569 }
570
Adam Langley95c29f32014-06-20 12:00:00 -0700571end:
Adam Langley1bea1732014-12-17 19:06:57 -0800572 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400573 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800574 if (cb != NULL) {
575 cb(s, SSL_CB_CONNECT_EXIT, ret);
576 }
577 return ret;
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langley1bea1732014-12-17 19:06:57 -0800580int ssl3_send_client_hello(SSL *s) {
581 uint8_t *buf, *p, *d;
582 int i;
583 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langley1bea1732014-12-17 19:06:57 -0800585 buf = (uint8_t *)s->init_buf->data;
586 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
587 if (!s->s3->have_version) {
588 uint16_t max_version = ssl3_get_max_client_version(s);
589 /* Disabling all versions is silly: return an error. */
590 if (max_version == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400591 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800592 goto err;
593 }
594 s->version = max_version;
595 s->client_version = max_version;
596 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500597
Adam Langley1bea1732014-12-17 19:06:57 -0800598 /* If the configured session was created at a version higher than our
599 * maximum version, drop it. */
600 if (s->session &&
601 (s->session->session_id_length == 0 || s->session->not_resumable ||
602 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
603 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
604 SSL_set_session(s, NULL);
605 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500606
Adam Langley1bea1732014-12-17 19:06:57 -0800607 /* else use the pre-loaded session */
608 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley1bea1732014-12-17 19:06:57 -0800610 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
611 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400612 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400613 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
614 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400615 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langley1bea1732014-12-17 19:06:57 -0800618 /* Do the message type and length last. Note: the final argument to
619 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
620 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langley1bea1732014-12-17 19:06:57 -0800622 /* version indicates the negotiated version: for example from an SSLv2/v3
623 * compatible client hello). The client_version field is the maximum
624 * version we permit and it is also used in RSA encrypted premaster
625 * secrets. Some servers can choke if we initially report a higher version
626 * then renegotiate to a lower one in the premaster secret. This didn't
627 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
628 * or later if the server only supports 1.0.
629 *
630 * Possible scenario with previous logic:
631 * 1. Client hello indicates TLS 1.2
632 * 2. Server hello says TLS 1.0
633 * 3. RSA encrypted premaster secret uses 1.2.
634 * 4. Handhaked proceeds using TLS 1.0.
635 * 5. Server sends hello request to renegotiate.
636 * 6. Client hello indicates TLS v1.0 as we now
637 * know that is maximum server supports.
638 * 7. Server chokes on RSA encrypted premaster secret
639 * containing version 1.0.
640 *
641 * For interoperability it should be OK to always use the maximum version
642 * we support in client hello and then rely on the checking of version to
643 * ensure the servers isn't being inconsistent: for example initially
644 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
645 * using client_version in client hello and not resetting it to the
646 * negotiated version. */
647 *(p++) = s->client_version >> 8;
648 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langley1bea1732014-12-17 19:06:57 -0800650 /* Random stuff */
651 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
652 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langley1bea1732014-12-17 19:06:57 -0800654 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400655 if (s->s3->initial_handshake_complete || s->session == NULL) {
656 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800657 i = 0;
658 } else {
659 i = s->session->session_id_length;
660 }
661 *(p++) = i;
662 if (i != 0) {
663 if (i > (int)sizeof(s->session->session_id)) {
David Benjamin3570d732015-06-29 00:28:17 -0400664 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800665 goto err;
666 }
667 memcpy(p, s->session->session_id, i);
668 p += i;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langley1bea1732014-12-17 19:06:57 -0800671 /* cookie stuff for DTLS */
672 if (SSL_IS_DTLS(s)) {
673 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
David Benjamin3570d732015-06-29 00:28:17 -0400674 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800675 goto err;
676 }
677 *(p++) = s->d1->cookie_len;
678 memcpy(p, s->d1->cookie, s->d1->cookie_len);
679 p += s->d1->cookie_len;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langley1bea1732014-12-17 19:06:57 -0800682 /* Ciphers supported */
683 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
684 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400685 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
Adam Langley1bea1732014-12-17 19:06:57 -0800686 goto err;
687 }
688 s2n(i, p);
689 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langley1bea1732014-12-17 19:06:57 -0800691 /* COMPRESSION */
692 *(p++) = 1;
693 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langley1bea1732014-12-17 19:06:57 -0800695 /* TLS extensions*/
696 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400697 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800698 goto err;
699 }
700
701 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
702 p - buf);
703 if (p == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400704 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800705 goto err;
706 }
707
708 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500709 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
710 goto err;
711 }
Adam Langley1bea1732014-12-17 19:06:57 -0800712 s->state = SSL3_ST_CW_CLNT_HELLO_B;
713 }
714
715 /* SSL3_ST_CW_CLNT_HELLO_B */
716 return ssl_do_write(s);
717
Adam Langley95c29f32014-06-20 12:00:00 -0700718err:
Adam Langley1bea1732014-12-17 19:06:57 -0800719 return -1;
720}
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langley1bea1732014-12-17 19:06:57 -0800722int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400723 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800724 const SSL_CIPHER *c;
725 CERT *ct = s->cert;
726 int al = SSL_AD_INTERNAL_ERROR, ok;
727 long n;
728 CBS server_hello, server_random, session_id;
729 uint16_t server_version, cipher_suite;
730 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400731 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langley1bea1732014-12-17 19:06:57 -0800733 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
734 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
735 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500736 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langley1bea1732014-12-17 19:06:57 -0800738 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500739 uint32_t err = ERR_peek_error();
740 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
741 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
742 /* Add a dedicated error code to the queue for a handshake_failure alert
743 * in response to ClientHello. This matches NSS's client behavior and
744 * gives a better error on a (probable) failure to negotiate initial
745 * parameters. Note: this error code comes after the original one.
746 *
747 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400748 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500749 }
Adam Langley1bea1732014-12-17 19:06:57 -0800750 return n;
751 }
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langley1bea1732014-12-17 19:06:57 -0800753 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400754
Adam Langley1bea1732014-12-17 19:06:57 -0800755 if (!CBS_get_u16(&server_hello, &server_version) ||
756 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
757 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
758 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
759 !CBS_get_u16(&server_hello, &cipher_suite) ||
760 !CBS_get_u8(&server_hello, &compression_method)) {
761 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400762 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800763 goto f_err;
764 }
David Benjamina03d95d2014-07-12 19:49:07 -0400765
David Benjamine6df0542015-05-12 22:02:08 -0400766 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800767 if (!s->s3->have_version) {
768 if (!ssl3_is_version_enabled(s, server_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400769 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
Adam Langley1bea1732014-12-17 19:06:57 -0800770 s->version = server_version;
771 /* Mark the version as fixed so the record-layer version is not clamped
772 * to TLS 1.0. */
773 s->s3->have_version = 1;
774 al = SSL_AD_PROTOCOL_VERSION;
775 goto f_err;
776 }
777 s->version = server_version;
778 s->enc_method = ssl3_get_enc_method(server_version);
779 assert(s->enc_method != NULL);
780 /* At this point, the connection's version is known and s->version is
781 * fixed. Begin enforcing the record-layer version. */
782 s->s3->have_version = 1;
783 } else if (server_version != s->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400784 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800785 al = SSL_AD_PROTOCOL_VERSION;
786 goto f_err;
787 }
Adam Langley95c29f32014-06-20 12:00:00 -0700788
Adam Langley1bea1732014-12-17 19:06:57 -0800789 /* Copy over the server random. */
790 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langley1bea1732014-12-17 19:06:57 -0800792 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400793 if (!s->s3->initial_handshake_complete && s->session != NULL &&
794 CBS_mem_equal(&session_id, s->session->session_id,
795 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800796 if (s->sid_ctx_length != s->session->sid_ctx_length ||
797 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
798 /* actually a client application bug */
799 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400800 OPENSSL_PUT_ERROR(SSL,
Adam Langley1bea1732014-12-17 19:06:57 -0800801 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
802 goto f_err;
803 }
804 s->hit = 1;
805 } else {
806 /* The session wasn't resumed. Create a fresh SSL_SESSION to
807 * fill out. */
808 s->hit = 0;
809 if (!ssl_get_new_session(s, 0)) {
810 goto f_err;
811 }
812 /* Note: session_id could be empty. */
813 s->session->session_id_length = CBS_len(&session_id);
814 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
815 }
David Benjamina03d95d2014-07-12 19:49:07 -0400816
David Benjamina1c90a52015-05-30 17:03:14 -0400817 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800818 if (c == NULL) {
819 /* unknown cipher */
820 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800822 goto f_err;
823 }
824 /* ct->mask_ssl was computed from client capabilities. Now
825 * that the final version is known, compute a new mask_ssl. */
826 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
827 mask_ssl = SSL_TLSV1_2;
828 } else {
829 mask_ssl = 0;
830 }
831 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
832 * the server selected it, it's an error. */
833 if ((c->algorithm_ssl & mask_ssl) ||
834 (c->algorithm_mkey & ct->mask_k) ||
835 (c->algorithm_auth & ct->mask_a)) {
836 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400837 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800838 goto f_err;
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langley1bea1732014-12-17 19:06:57 -0800841 sk = ssl_get_ciphers_by_id(s);
842 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
843 /* we did not say we would use this cipher */
844 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400845 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800846 goto f_err;
847 }
Adam Langley95c29f32014-06-20 12:00:00 -0700848
David Benjaminece3de92015-03-16 18:02:20 -0400849 if (s->hit) {
850 if (s->session->cipher != c) {
851 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400852 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400853 goto f_err;
854 }
855 if (s->session->ssl_version != s->version) {
856 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400857 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400858 goto f_err;
859 }
Adam Langley1bea1732014-12-17 19:06:57 -0800860 }
861 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500862
David Benjamin5f04b652015-05-26 17:30:10 -0400863 /* If doing a full handshake with TLS 1.2, the server may request a client
864 * certificate which requires hashing the handshake transcript under a
865 * different hash. Otherwise, release the handshake buffer. */
866 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800867 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
868 goto f_err;
869 }
David Benjamina03d95d2014-07-12 19:49:07 -0400870
Adam Langley1bea1732014-12-17 19:06:57 -0800871 /* Only the NULL compression algorithm is supported. */
872 if (compression_method != 0) {
873 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400874 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800875 goto f_err;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langley1bea1732014-12-17 19:06:57 -0800878 /* TLS extensions */
879 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400880 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800881 goto err;
882 }
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley1bea1732014-12-17 19:06:57 -0800884 /* There should be nothing left over in the record. */
885 if (CBS_len(&server_hello) != 0) {
886 /* wrong packet length */
887 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400888 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -0800889 goto f_err;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyba5934b2015-06-02 10:50:35 -0700892 if (s->hit &&
893 s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
894 al = SSL_AD_HANDSHAKE_FAILURE;
895 if (s->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400896 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700897 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400898 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700899 }
900 goto f_err;
901 }
902
Adam Langley1bea1732014-12-17 19:06:57 -0800903 return 1;
904
Adam Langley95c29f32014-06-20 12:00:00 -0700905f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800906 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700907err:
Adam Langley1bea1732014-12-17 19:06:57 -0800908 return -1;
909}
Adam Langley95c29f32014-06-20 12:00:00 -0700910
David Benjamin436bf822015-06-07 19:50:37 -0400911/* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
912 * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
913 * on the error queue. */
914static int ssl3_check_certificate_for_cipher(X509 *leaf,
915 const SSL_CIPHER *cipher) {
916 int ret = 0;
917 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400918 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400919 goto err;
920 }
921
922 /* Check the certificate's type matches the cipher. */
David Benjamind1d80782015-07-05 11:54:09 -0400923 int expected_type = ssl_cipher_get_key_type(cipher);
924 assert(expected_type != EVP_PKEY_NONE);
925 if (pkey->type != expected_type) {
David Benjamin3570d732015-06-29 00:28:17 -0400926 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
David Benjamin436bf822015-06-07 19:50:37 -0400927 goto err;
928 }
929
930 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
931 * be checked in other cases as well? */
932 if (cipher->algorithm_auth & SSL_aECDSA) {
933 /* This call populates the ex_flags field correctly */
934 X509_check_purpose(leaf, -1, 0);
935 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
936 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjamin3570d732015-06-29 00:28:17 -0400937 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
David Benjamin436bf822015-06-07 19:50:37 -0400938 goto err;
939 }
940 }
941
942 ret = 1;
943
944err:
945 EVP_PKEY_free(pkey);
946 return ret;
947}
948
Adam Langley1bea1732014-12-17 19:06:57 -0800949int ssl3_get_server_certificate(SSL *s) {
950 int al, i, ok, ret = -1;
951 unsigned long n;
952 X509 *x = NULL;
953 STACK_OF(X509) *sk = NULL;
954 SESS_CERT *sc;
955 EVP_PKEY *pkey = NULL;
956 CBS cbs, certificate_list;
957 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langley1bea1732014-12-17 19:06:57 -0800959 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400960 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500961 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langley1bea1732014-12-17 19:06:57 -0800963 if (!ok) {
964 return n;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langley1bea1732014-12-17 19:06:57 -0800967 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langley1bea1732014-12-17 19:06:57 -0800969 sk = sk_X509_new_null();
970 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400971 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -0800972 goto err;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langley1bea1732014-12-17 19:06:57 -0800975 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400976 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800977 CBS_len(&cbs) != 0) {
978 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400979 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800980 goto f_err;
981 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400982
Adam Langley1bea1732014-12-17 19:06:57 -0800983 while (CBS_len(&certificate_list) > 0) {
984 CBS certificate;
985 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
986 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400987 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -0800988 goto f_err;
989 }
990 data = CBS_data(&certificate);
991 x = d2i_X509(NULL, &data, CBS_len(&certificate));
992 if (x == NULL) {
993 al = SSL_AD_BAD_CERTIFICATE;
David Benjamin3570d732015-06-29 00:28:17 -0400994 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -0800995 goto f_err;
996 }
997 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
998 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400999 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001000 goto f_err;
1001 }
1002 if (!sk_X509_push(sk, x)) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001004 goto err;
1005 }
1006 x = NULL;
1007 }
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langley1bea1732014-12-17 19:06:57 -08001009 i = ssl_verify_cert_chain(s, sk);
1010 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1011 al = ssl_verify_alarm_type(s->verify_result);
David Benjamin3570d732015-06-29 00:28:17 -04001012 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langley1bea1732014-12-17 19:06:57 -08001013 goto f_err;
1014 }
1015 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001016
David Benjamin436bf822015-06-07 19:50:37 -04001017 X509 *leaf = sk_X509_value(sk, 0);
1018 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1019 al = SSL_AD_ILLEGAL_PARAMETER;
1020 goto f_err;
1021 }
1022
Adam Langley1bea1732014-12-17 19:06:57 -08001023 sc = ssl_sess_cert_new();
1024 if (sc == NULL) {
1025 goto err;
1026 }
Adam Langley95c29f32014-06-20 12:00:00 -07001027
David Benjamin2755a3e2015-04-22 16:17:58 -04001028 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001029 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001030
David Benjamin436bf822015-06-07 19:50:37 -04001031 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1032 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001033 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001034 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001035
David Benjaminb31040d2015-06-07 10:53:32 -04001036 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001037 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001038
David Benjamin2755a3e2015-04-22 16:17:58 -04001039 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001040 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001041
Adam Langley1bea1732014-12-17 19:06:57 -08001042 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langley1bea1732014-12-17 19:06:57 -08001044 ret = 1;
1045
1046 if (0) {
1047 f_err:
1048 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1049 }
1050
1051err:
1052 EVP_PKEY_free(pkey);
1053 X509_free(x);
1054 sk_X509_pop_free(sk, X509_free);
1055 return ret;
1056}
1057
1058int ssl3_get_server_key_exchange(SSL *s) {
1059 EVP_MD_CTX md_ctx;
1060 int al, ok;
1061 long n, alg_k, alg_a;
1062 EVP_PKEY *pkey = NULL;
1063 const EVP_MD *md = NULL;
1064 RSA *rsa = NULL;
1065 DH *dh = NULL;
1066 EC_KEY *ecdh = NULL;
1067 BN_CTX *bn_ctx = NULL;
1068 EC_POINT *srvr_ecpoint = NULL;
1069 CBS server_key_exchange, server_key_exchange_orig, parameter;
1070
1071 /* use same message size as in ssl3_get_certificate_request() as
1072 * ServerKeyExchange message may be skipped */
1073 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1074 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001075 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001076 if (!ok) {
1077 return n;
1078 }
1079
1080 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1081 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001082 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001083 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1084 return -1;
1085 }
1086
1087 /* In plain PSK ciphersuite, ServerKeyExchange can be
1088 omitted if no identity hint is sent. Set session->sess_cert anyway to
1089 avoid problems later.*/
1090 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1091 /* PSK ciphersuites that also send a Certificate would have already
1092 * initialized |sess_cert|. */
1093 if (s->session->sess_cert == NULL) {
1094 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001095 if (s->session->sess_cert == NULL) {
1096 return -1;
1097 }
Adam Langley1bea1732014-12-17 19:06:57 -08001098 }
1099
1100 /* TODO(davidben): This should be reset in one place with the rest of the
1101 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001102 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1103 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001104 }
1105 s->s3->tmp.reuse_message = 1;
1106 return 1;
1107 }
1108
1109 /* Retain a copy of the original CBS to compute the signature over. */
1110 CBS_init(&server_key_exchange, s->init_msg, n);
1111 server_key_exchange_orig = server_key_exchange;
1112
1113 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001114 DH_free(s->session->sess_cert->peer_dh_tmp);
1115 s->session->sess_cert->peer_dh_tmp = NULL;
1116 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1117 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001118 } else {
1119 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001120 if (s->session->sess_cert == NULL) {
1121 return -1;
1122 }
Adam Langley1bea1732014-12-17 19:06:57 -08001123 }
1124
1125 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1126 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1127 EVP_MD_CTX_init(&md_ctx);
1128
1129 if (alg_a & SSL_aPSK) {
1130 CBS psk_identity_hint;
1131
1132 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1133 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1134 &psk_identity_hint)) {
1135 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001136 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001137 goto f_err;
1138 }
1139
1140 /* Store PSK identity hint for later use, hint is used in
1141 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1142 * identity hint can be as long as the maximum length of a PSK identity.
1143 * Also do not allow NULL characters; identities are saved as C strings.
1144 *
1145 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1146 * a specific identity. */
1147 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1148 CBS_contains_zero_byte(&psk_identity_hint)) {
1149 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001150 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001151 goto f_err;
1152 }
1153
1154 /* Save the identity hint as a C string. */
1155 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1156 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001157 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001158 goto f_err;
1159 }
1160 }
1161
David Benjamin7061e282015-03-19 11:10:48 -04001162 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001163 CBS dh_p, dh_g, dh_Ys;
1164
1165 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1166 CBS_len(&dh_p) == 0 ||
1167 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1168 CBS_len(&dh_g) == 0 ||
1169 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1170 CBS_len(&dh_Ys) == 0) {
1171 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001172 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001173 goto f_err;
1174 }
1175
1176 dh = DH_new();
1177 if (dh == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001178 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001179 goto err;
1180 }
1181
1182 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1183 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1184 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1185 NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001186 OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001187 goto err;
1188 }
1189
Adam Langleya7997f12015-05-14 17:38:50 -07001190 if (DH_num_bits(dh) < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001191 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001192 goto err;
1193 }
Adam Langley1bea1732014-12-17 19:06:57 -08001194 s->session->sess_cert->peer_dh_tmp = dh;
1195 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001196 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001197 uint16_t curve_id;
1198 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001199 const EC_GROUP *group;
1200 CBS point;
1201
1202 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1203 * key. Check curve is one of our preferences, if not server has sent an
1204 * invalid curve. */
1205 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1206 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001207 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001208 goto f_err;
1209 }
1210
1211 curve_nid = tls1_ec_curve_id2nid(curve_id);
1212 if (curve_nid == 0) {
1213 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001214 OPENSSL_PUT_ERROR(SSL, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley1bea1732014-12-17 19:06:57 -08001215 goto f_err;
1216 }
1217
David Benjamin4f7783e2015-03-05 03:05:11 -05001218 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001219 if (ecdh == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001220 OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001221 goto err;
1222 }
1223
Adam Langley1bea1732014-12-17 19:06:57 -08001224 group = EC_KEY_get0_group(ecdh);
1225
1226 /* Next, get the encoded ECPoint */
1227 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1228 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001230 goto f_err;
1231 }
1232
1233 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1234 ((bn_ctx = BN_CTX_new()) == NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -04001235 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001236 goto err;
1237 }
1238
1239 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1240 CBS_len(&point), bn_ctx)) {
1241 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001242 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
Adam Langley1bea1732014-12-17 19:06:57 -08001243 goto f_err;
1244 }
Adam Langley1bea1732014-12-17 19:06:57 -08001245 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1246 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1247 ecdh = NULL;
1248 BN_CTX_free(bn_ctx);
1249 bn_ctx = NULL;
1250 EC_POINT_free(srvr_ecpoint);
1251 srvr_ecpoint = NULL;
1252 } else if (!(alg_k & SSL_kPSK)) {
1253 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001254 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001255 goto f_err;
1256 }
1257
1258 /* At this point, |server_key_exchange| contains the signature, if any, while
1259 * |server_key_exchange_orig| contains the entire message. From that, derive
1260 * a CBS containing just the parameter. */
1261 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1262 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1263
David Benjaminf4958e72015-06-07 11:00:53 -04001264 /* ServerKeyExchange should be signed by the server's public key. */
1265 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1266 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1267 if (pkey == NULL) {
1268 goto err;
1269 }
Adam Langley1bea1732014-12-17 19:06:57 -08001270
1271 if (SSL_USE_SIGALGS(s)) {
1272 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1273 goto f_err;
1274 }
1275 } else if (pkey->type == EVP_PKEY_RSA) {
1276 md = EVP_md5_sha1();
1277 } else {
1278 md = EVP_sha1();
1279 }
1280
1281 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001282 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001283 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1284 CBS_len(&server_key_exchange) != 0) {
1285 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001286 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001287 goto f_err;
1288 }
1289
1290 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1291 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1292 SSL3_RANDOM_SIZE) ||
1293 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1294 SSL3_RANDOM_SIZE) ||
1295 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1296 CBS_len(&parameter)) ||
1297 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1298 CBS_len(&signature))) {
1299 /* bad signature */
1300 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001301 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001302 goto f_err;
1303 }
1304 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001305 /* PSK ciphers are the only supported certificate-less ciphers. */
1306 assert(alg_a == SSL_aPSK);
1307
Adam Langley1bea1732014-12-17 19:06:57 -08001308 if (CBS_len(&server_key_exchange) > 0) {
1309 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001310 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001311 goto f_err;
1312 }
1313 }
1314 EVP_PKEY_free(pkey);
1315 EVP_MD_CTX_cleanup(&md_ctx);
1316 return 1;
1317
Adam Langley95c29f32014-06-20 12:00:00 -07001318f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001319 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001320err:
Adam Langley1bea1732014-12-17 19:06:57 -08001321 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001322 RSA_free(rsa);
1323 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001324 BN_CTX_free(bn_ctx);
1325 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001326 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001327 EVP_MD_CTX_cleanup(&md_ctx);
1328 return -1;
1329}
Adam Langley95c29f32014-06-20 12:00:00 -07001330
Adam Langley1bea1732014-12-17 19:06:57 -08001331static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1332 return X509_NAME_cmp(*a, *b);
1333}
Adam Langley95c29f32014-06-20 12:00:00 -07001334
Adam Langley1bea1732014-12-17 19:06:57 -08001335int ssl3_get_certificate_request(SSL *s) {
1336 int ok, ret = 0;
1337 unsigned long n;
1338 X509_NAME *xn = NULL;
1339 STACK_OF(X509_NAME) *ca_sk = NULL;
1340 CBS cbs;
1341 CBS certificate_types;
1342 CBS certificate_authorities;
1343 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001344
Adam Langley1bea1732014-12-17 19:06:57 -08001345 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1346 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001347 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001348
Adam Langley1bea1732014-12-17 19:06:57 -08001349 if (!ok) {
1350 return n;
1351 }
David Benjamin688d8df2014-11-02 23:06:42 -05001352
Adam Langley1bea1732014-12-17 19:06:57 -08001353 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001354
Adam Langley1bea1732014-12-17 19:06:57 -08001355 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1356 s->s3->tmp.reuse_message = 1;
1357 /* If we get here we don't need any cached handshake records as we wont be
1358 * doing client auth. */
1359 if (s->s3->handshake_buffer &&
1360 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1361 goto err;
1362 }
1363 return 1;
1364 }
David Benjamined439582014-07-14 19:13:02 -04001365
Adam Langley1bea1732014-12-17 19:06:57 -08001366 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1367 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin3570d732015-06-29 00:28:17 -04001368 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
Adam Langley1bea1732014-12-17 19:06:57 -08001369 goto err;
1370 }
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langley1bea1732014-12-17 19:06:57 -08001372 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langley1bea1732014-12-17 19:06:57 -08001374 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1375 if (ca_sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001376 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001377 goto err;
1378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380 /* get the certificate types */
1381 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1382 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001383 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001384 goto err;
1385 }
David Benjamined439582014-07-14 19:13:02 -04001386
Adam Langley1bea1732014-12-17 19:06:57 -08001387 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1388 &s->s3->tmp.num_certificate_types)) {
1389 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1390 goto err;
1391 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001392
Adam Langley1bea1732014-12-17 19:06:57 -08001393 if (SSL_USE_SIGALGS(s)) {
1394 CBS supported_signature_algorithms;
1395 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1396 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001397 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001398 goto err;
1399 }
David Benjamined439582014-07-14 19:13:02 -04001400
Adam Langley1bea1732014-12-17 19:06:57 -08001401 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1402 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001403 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001404 goto err;
1405 }
1406 }
David Benjamined439582014-07-14 19:13:02 -04001407
Adam Langley1bea1732014-12-17 19:06:57 -08001408 /* get the CA RDNs */
1409 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1410 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001411 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001412 goto err;
1413 }
Adam Langley95c29f32014-06-20 12:00:00 -07001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 while (CBS_len(&certificate_authorities) > 0) {
1416 CBS distinguished_name;
1417 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1418 &distinguished_name)) {
1419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001420 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001421 goto err;
1422 }
Adam Langley95c29f32014-06-20 12:00:00 -07001423
Adam Langley1bea1732014-12-17 19:06:57 -08001424 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001425
Adam Langley1bea1732014-12-17 19:06:57 -08001426 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1427 if (xn == NULL) {
1428 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001429 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001430 goto err;
1431 }
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langley1bea1732014-12-17 19:06:57 -08001433 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1434 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001435 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001436 goto err;
1437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 if (CBS_len(&distinguished_name) != 0) {
1440 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001441 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001442 goto err;
1443 }
Adam Langley95c29f32014-06-20 12:00:00 -07001444
Adam Langley1bea1732014-12-17 19:06:57 -08001445 if (!sk_X509_NAME_push(ca_sk, xn)) {
David Benjamin3570d732015-06-29 00:28:17 -04001446 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001447 goto err;
1448 }
1449 }
Adam Langley95c29f32014-06-20 12:00:00 -07001450
Adam Langley1bea1732014-12-17 19:06:57 -08001451 /* we should setup a certificate to return.... */
1452 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001453 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001454 s->s3->tmp.ca_names = ca_sk;
1455 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langley1bea1732014-12-17 19:06:57 -08001457 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langley1bea1732014-12-17 19:06:57 -08001459err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001460 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001461 return ret;
1462}
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langley1bea1732014-12-17 19:06:57 -08001464int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001465 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001466 long n;
1467 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001468
Adam Langley1bea1732014-12-17 19:06:57 -08001469 n = s->method->ssl_get_message(
1470 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001471 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001472
Adam Langley1bea1732014-12-17 19:06:57 -08001473 if (!ok) {
1474 return n;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
David Benjamin95d31822015-06-15 19:53:32 -04001477 if (s->hit) {
1478 /* The server is sending a new ticket for an existing session. Sessions are
1479 * immutable once established, so duplicate all but the ticket of the
1480 * existing session. */
1481 uint8_t *bytes;
1482 size_t bytes_len;
1483 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &bytes, &bytes_len)) {
1484 goto err;
1485 }
1486 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1487 OPENSSL_free(bytes);
1488 if (new_session == NULL) {
1489 /* This should never happen. */
David Benjamin3570d732015-06-29 00:28:17 -04001490 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin95d31822015-06-15 19:53:32 -04001491 goto err;
1492 }
David Benjamin680ca962015-06-18 12:37:23 -04001493 if (s->session->sess_cert != NULL) {
1494 /* |sess_cert| is not serialized and must be duplicated explicitly. */
1495 assert(new_session->sess_cert == NULL);
1496 new_session->sess_cert = ssl_sess_cert_dup(s->session->sess_cert);
1497 if (new_session->sess_cert == NULL) {
1498 SSL_SESSION_free(new_session);
1499 goto err;
1500 }
1501 }
David Benjamin95d31822015-06-15 19:53:32 -04001502
1503 SSL_SESSION_free(s->session);
1504 s->session = new_session;
1505 }
1506
Adam Langley1bea1732014-12-17 19:06:57 -08001507 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001508
Adam Langley1bea1732014-12-17 19:06:57 -08001509 if (!CBS_get_u32(&new_session_ticket,
1510 &s->session->tlsext_tick_lifetime_hint) ||
1511 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1512 CBS_len(&new_session_ticket) != 0) {
1513 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001514 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001515 goto f_err;
1516 }
Adam Langley95c29f32014-06-20 12:00:00 -07001517
Adam Langley1bea1732014-12-17 19:06:57 -08001518 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1519 &s->session->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -04001520 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001521 goto err;
1522 }
Adam Langley95c29f32014-06-20 12:00:00 -07001523
David Benjamin68070622015-02-08 23:44:59 -05001524 /* Generate a session ID for this session based on the session ticket. We use
1525 * the session ID mechanism for detecting ticket resumption. This also fits in
1526 * with assumptions elsewhere in OpenSSL.*/
1527 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1528 &s->session->session_id_length, EVP_sha256(), NULL)) {
1529 goto err;
1530 }
1531
1532 return 1;
David Benjamined439582014-07-14 19:13:02 -04001533
Adam Langley95c29f32014-06-20 12:00:00 -07001534f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001535 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001536err:
Adam Langley1bea1732014-12-17 19:06:57 -08001537 return -1;
1538}
Adam Langley95c29f32014-06-20 12:00:00 -07001539
Adam Langley1bea1732014-12-17 19:06:57 -08001540int ssl3_get_cert_status(SSL *s) {
1541 int ok, al;
1542 long n;
1543 CBS certificate_status, ocsp_response;
1544 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001545
Adam Langley1bea1732014-12-17 19:06:57 -08001546 n = s->method->ssl_get_message(
1547 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001548 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001549
Adam Langley1bea1732014-12-17 19:06:57 -08001550 if (!ok) {
1551 return n;
1552 }
Adam Langley95c29f32014-06-20 12:00:00 -07001553
David Benjamindcd979f2015-04-20 18:26:52 -04001554 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1555 /* A server may send status_request in ServerHello and then change
1556 * its mind about sending CertificateStatus. */
1557 s->s3->tmp.reuse_message = 1;
1558 return 1;
1559 }
1560
Adam Langley1bea1732014-12-17 19:06:57 -08001561 CBS_init(&certificate_status, s->init_msg, n);
1562 if (!CBS_get_u8(&certificate_status, &status_type) ||
1563 status_type != TLSEXT_STATUSTYPE_ocsp ||
1564 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1565 CBS_len(&ocsp_response) == 0 ||
1566 CBS_len(&certificate_status) != 0) {
1567 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001568 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001569 goto f_err;
1570 }
Adam Langley95c29f32014-06-20 12:00:00 -07001571
Adam Langley1bea1732014-12-17 19:06:57 -08001572 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1573 &s->session->ocsp_response_length)) {
1574 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001575 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001576 goto f_err;
1577 }
1578 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langley95c29f32014-06-20 12:00:00 -07001580f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001581 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1582 return -1;
1583}
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585int ssl3_get_server_done(SSL *s) {
1586 int ok;
1587 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langley1bea1732014-12-17 19:06:57 -08001589 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1590 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1591 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001592 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langley1bea1732014-12-17 19:06:57 -08001594 if (!ok) {
1595 return n;
1596 }
David Benjamin46062682014-07-14 19:14:32 -04001597
Adam Langley1bea1732014-12-17 19:06:57 -08001598 if (n > 0) {
1599 /* should contain no data */
1600 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001601 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
Adam Langley1bea1732014-12-17 19:06:57 -08001602 return -1;
1603 }
David Benjamin46062682014-07-14 19:14:32 -04001604
Adam Langley1bea1732014-12-17 19:06:57 -08001605 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001606}
Adam Langley1258b6a2014-06-20 12:00:00 -07001607
Adam Langley1258b6a2014-06-20 12:00:00 -07001608
Adam Langley1bea1732014-12-17 19:06:57 -08001609int ssl3_send_client_key_exchange(SSL *s) {
1610 uint8_t *p;
1611 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001612 uint32_t alg_k;
1613 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001614 uint8_t *q;
1615 EVP_PKEY *pkey = NULL;
1616 EC_KEY *clnt_ecdh = NULL;
1617 const EC_POINT *srvr_ecpoint = NULL;
1618 EVP_PKEY *srvr_pub_pkey = NULL;
1619 uint8_t *encodedPoint = NULL;
1620 int encoded_pt_len = 0;
1621 BN_CTX *bn_ctx = NULL;
1622 unsigned int psk_len = 0;
1623 uint8_t psk[PSK_MAX_PSK_LEN];
1624 uint8_t *pms = NULL;
1625 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1628 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001629
Adam Langley1bea1732014-12-17 19:06:57 -08001630 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1631 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001632
Adam Langley1bea1732014-12-17 19:06:57 -08001633 /* If using a PSK key exchange, prepare the pre-shared key. */
1634 if (alg_a & SSL_aPSK) {
1635 char identity[PSK_MAX_IDENTITY_LEN + 1];
1636 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001637
Adam Langley1bea1732014-12-17 19:06:57 -08001638 if (s->psk_client_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001639 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
Adam Langley1bea1732014-12-17 19:06:57 -08001640 goto err;
1641 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001642
Adam Langley1bea1732014-12-17 19:06:57 -08001643 memset(identity, 0, sizeof(identity));
1644 psk_len =
1645 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1646 sizeof(identity), psk, sizeof(psk));
1647 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001648 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001649 goto err;
1650 } else if (psk_len == 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001651 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langley1bea1732014-12-17 19:06:57 -08001652 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1653 goto err;
1654 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001655
Adam Langley1bea1732014-12-17 19:06:57 -08001656 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1657 if (identity_len > PSK_MAX_IDENTITY_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001658 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001659 goto err;
1660 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001661
David Benjamin2755a3e2015-04-22 16:17:58 -04001662 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001663 s->session->psk_identity = BUF_strdup(identity);
1664 if (s->session->psk_identity == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001665 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001666 goto err;
1667 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001668
Adam Langley1bea1732014-12-17 19:06:57 -08001669 /* Write out psk_identity. */
1670 s2n(identity_len, p);
1671 memcpy(p, identity, identity_len);
1672 p += identity_len;
1673 n = 2 + identity_len;
1674 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001675
Adam Langley1bea1732014-12-17 19:06:57 -08001676 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1677 if (alg_k & SSL_kRSA) {
1678 RSA *rsa;
1679 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001680
Adam Langley1bea1732014-12-17 19:06:57 -08001681 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1682 pms = OPENSSL_malloc(pms_len);
1683 if (pms == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001685 goto err;
1686 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001687
Adam Langley1bea1732014-12-17 19:06:57 -08001688 if (s->session->sess_cert == NULL) {
1689 /* We should always have a server certificate with SSL_kRSA. */
David Benjamin3570d732015-06-29 00:28:17 -04001690 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001691 goto err;
1692 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001693
David Benjaminb31040d2015-06-07 10:53:32 -04001694 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001695 if (pkey == NULL ||
1696 pkey->type != EVP_PKEY_RSA ||
1697 pkey->pkey.rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001698 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001699 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001700 goto err;
1701 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001702
Adam Langley1bea1732014-12-17 19:06:57 -08001703 rsa = pkey->pkey.rsa;
1704 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001705
Adam Langley1bea1732014-12-17 19:06:57 -08001706 pms[0] = s->client_version >> 8;
1707 pms[1] = s->client_version & 0xff;
1708 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1709 goto err;
1710 }
1711
1712 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1713
1714 q = p;
1715 /* In TLS and beyond, reserve space for the length prefix. */
1716 if (s->version > SSL3_VERSION) {
1717 p += 2;
1718 n += 2;
1719 }
1720 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1721 RSA_PKCS1_PADDING)) {
David Benjamin3570d732015-06-29 00:28:17 -04001722 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_RSA_ENCRYPT);
Adam Langley1bea1732014-12-17 19:06:57 -08001723 goto err;
1724 }
1725 n += enc_pms_len;
1726
1727 /* Log the premaster secret, if logging is enabled. */
1728 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1729 pms_len)) {
1730 goto err;
1731 }
1732
1733 /* Fill in the length prefix. */
1734 if (s->version > SSL3_VERSION) {
1735 s2n(enc_pms_len, q);
1736 }
David Benjamin7061e282015-03-19 11:10:48 -04001737 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001738 DH *dh_srvr, *dh_clnt;
1739 SESS_CERT *scert = s->session->sess_cert;
1740 int dh_len;
1741 size_t pub_len;
1742
1743 if (scert == NULL) {
1744 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin3570d732015-06-29 00:28:17 -04001745 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001746 goto err;
1747 }
1748
1749 if (scert->peer_dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001750 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001751 goto err;
1752 }
1753 dh_srvr = scert->peer_dh_tmp;
1754
1755 /* generate a new random key */
1756 dh_clnt = DHparams_dup(dh_srvr);
1757 if (dh_clnt == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001758 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001759 goto err;
1760 }
1761 if (!DH_generate_key(dh_clnt)) {
David Benjamin3570d732015-06-29 00:28:17 -04001762 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001763 DH_free(dh_clnt);
1764 goto err;
1765 }
1766
1767 pms_len = DH_size(dh_clnt);
1768 pms = OPENSSL_malloc(pms_len);
1769 if (pms == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001770 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001771 DH_free(dh_clnt);
1772 goto err;
1773 }
1774
1775 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1776 if (dh_len <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001777 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001778 DH_free(dh_clnt);
1779 goto err;
1780 }
1781 pms_len = dh_len;
1782
1783 /* send off the data */
1784 pub_len = BN_num_bytes(dh_clnt->pub_key);
1785 s2n(pub_len, p);
1786 BN_bn2bin(dh_clnt->pub_key, p);
1787 n += 2 + pub_len;
1788
1789 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001790 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001791 const EC_GROUP *srvr_group = NULL;
1792 EC_KEY *tkey;
1793 int field_size = 0, ecdh_len;
1794
1795 if (s->session->sess_cert == NULL) {
1796 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin3570d732015-06-29 00:28:17 -04001797 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001798 goto err;
1799 }
1800
1801 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001802 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001803 goto err;
1804 }
1805
1806 tkey = s->session->sess_cert->peer_ecdh_tmp;
1807
1808 srvr_group = EC_KEY_get0_group(tkey);
1809 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1810 if (srvr_group == NULL || srvr_ecpoint == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001811 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001812 goto err;
1813 }
1814
1815 clnt_ecdh = EC_KEY_new();
1816 if (clnt_ecdh == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001817 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001818 goto err;
1819 }
1820
1821 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
David Benjamin3570d732015-06-29 00:28:17 -04001822 OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001823 goto err;
1824 }
1825
1826 /* Generate a new ECDH key pair */
1827 if (!EC_KEY_generate_key(clnt_ecdh)) {
David Benjamin3570d732015-06-29 00:28:17 -04001828 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001829 goto err;
1830 }
1831
1832 field_size = EC_GROUP_get_degree(srvr_group);
1833 if (field_size <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001834 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001835 goto err;
1836 }
1837
1838 pms_len = (field_size + 7) / 8;
1839 pms = OPENSSL_malloc(pms_len);
1840 if (pms == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001841 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001842 goto err;
1843 }
1844
1845 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1846 if (ecdh_len <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001847 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001848 goto err;
1849 }
1850 pms_len = ecdh_len;
1851
1852 /* First check the size of encoding and allocate memory accordingly. */
1853 encoded_pt_len =
1854 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1855 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1856
1857 encodedPoint =
1858 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1859 bn_ctx = BN_CTX_new();
1860 if (encodedPoint == NULL || bn_ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001861 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001862 goto err;
1863 }
1864
1865 /* Encode the public key */
1866 encoded_pt_len = EC_POINT_point2oct(
1867 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1868 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1869
1870 *p = encoded_pt_len; /* length of encoded point */
1871 /* Encoded point will be copied here */
1872 p += 1;
1873 n += 1;
1874 /* copy the point */
1875 memcpy(p, encodedPoint, encoded_pt_len);
1876 /* increment n to account for length field */
1877 n += encoded_pt_len;
1878
1879 /* Free allocated memory */
1880 BN_CTX_free(bn_ctx);
1881 bn_ctx = NULL;
1882 OPENSSL_free(encodedPoint);
1883 encodedPoint = NULL;
1884 EC_KEY_free(clnt_ecdh);
1885 clnt_ecdh = NULL;
1886 EVP_PKEY_free(srvr_pub_pkey);
1887 srvr_pub_pkey = NULL;
1888 } else if (alg_k & SSL_kPSK) {
1889 /* For plain PSK, other_secret is a block of 0s with the same length as
1890 * the pre-shared key. */
1891 pms_len = psk_len;
1892 pms = OPENSSL_malloc(pms_len);
1893 if (pms == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001894 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001895 goto err;
1896 }
1897 memset(pms, 0, pms_len);
1898 } else {
1899 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin3570d732015-06-29 00:28:17 -04001900 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001901 goto err;
1902 }
1903
1904 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1905 * key. */
1906 if (alg_a & SSL_aPSK) {
1907 CBB cbb, child;
1908 uint8_t *new_pms;
1909 size_t new_pms_len;
1910
David Benjamina8653202015-06-28 01:26:10 -04001911 CBB_zero(&cbb);
1912 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len) ||
1913 !CBB_add_u16_length_prefixed(&cbb, &child) ||
Adam Langley1bea1732014-12-17 19:06:57 -08001914 !CBB_add_bytes(&child, pms, pms_len) ||
1915 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1916 !CBB_add_bytes(&child, psk, psk_len) ||
1917 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1918 CBB_cleanup(&cbb);
David Benjamin3570d732015-06-29 00:28:17 -04001919 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001920 goto err;
1921 }
1922 OPENSSL_cleanse(pms, pms_len);
1923 OPENSSL_free(pms);
1924 pms = new_pms;
1925 pms_len = new_pms_len;
1926 }
1927
1928 /* The message must be added to the finished hash before calculating the
1929 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001930 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1931 goto err;
1932 }
Adam Langley1bea1732014-12-17 19:06:57 -08001933 s->state = SSL3_ST_CW_KEY_EXCH_B;
1934
1935 s->session->master_key_length = s->enc_method->generate_master_secret(
1936 s, s->session->master_key, pms, pms_len);
1937 if (s->session->master_key_length == 0) {
1938 goto err;
1939 }
1940 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1941 OPENSSL_cleanse(pms, pms_len);
1942 OPENSSL_free(pms);
1943 }
1944
1945 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001946 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001947
1948err:
Adam Langley1bea1732014-12-17 19:06:57 -08001949 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001950 OPENSSL_free(encodedPoint);
1951 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001952 EVP_PKEY_free(srvr_pub_pkey);
1953 if (pms) {
1954 OPENSSL_cleanse(pms, pms_len);
1955 OPENSSL_free(pms);
1956 }
1957 return -1;
1958}
Adam Langley1258b6a2014-06-20 12:00:00 -07001959
Adam Langley1bea1732014-12-17 19:06:57 -08001960int ssl3_send_cert_verify(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04001961 if (s->state == SSL3_ST_CW_CERT_VRFY_A ||
1962 s->state == SSL3_ST_CW_CERT_VRFY_B) {
1963 enum ssl_private_key_result_t sign_result;
1964 uint8_t *p = ssl_handshake_start(s);
1965 size_t signature_length = 0;
1966 unsigned long n = 0;
David Benjamind1d80782015-07-05 11:54:09 -04001967 assert(s->cert->privatekey != NULL || s->cert->key_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001968
David Benjaminb4d65fd2015-05-29 17:11:21 -04001969 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1970 uint8_t *buf = (uint8_t *)s->init_buf->data;
1971 const EVP_MD *md = NULL;
1972 uint8_t digest[EVP_MAX_MD_SIZE];
1973 size_t digest_length;
Adam Langley1bea1732014-12-17 19:06:57 -08001974
David Benjaminb4d65fd2015-05-29 17:11:21 -04001975 /* Write out the digest type if need be. */
1976 if (SSL_USE_SIGALGS(s)) {
David Benjamind1d80782015-07-05 11:54:09 -04001977 md = tls1_choose_signing_digest(s);
1978 if (!tls12_get_sigandhash(s, p, md)) {
David Benjamin3570d732015-06-29 00:28:17 -04001979 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb4d65fd2015-05-29 17:11:21 -04001980 return -1;
1981 }
1982 p += 2;
1983 n += 2;
Adam Langley1bea1732014-12-17 19:06:57 -08001984 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04001985
1986 /* Compute the digest. */
David Benjamind1d80782015-07-05 11:54:09 -04001987 const int pkey_type = ssl_private_key_type(s);
David Benjamin396a4412015-07-05 01:38:09 -04001988 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey_type)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04001989 return -1;
1990 }
1991
1992 /* The handshake buffer is no longer necessary. */
1993 if (s->s3->handshake_buffer &&
1994 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1995 return -1;
1996 }
1997
1998 /* Sign the digest. */
David Benjamind1d80782015-07-05 11:54:09 -04001999 signature_length = ssl_private_key_max_signature_len(s);
David Benjaminb4d65fd2015-05-29 17:11:21 -04002000 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -04002001 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjaminb4d65fd2015-05-29 17:11:21 -04002002 return -1;
2003 }
2004
2005 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind1d80782015-07-05 11:54:09 -04002006 sign_result = ssl_private_key_sign(s, &p[2], &signature_length,
David Benjaminb4d65fd2015-05-29 17:11:21 -04002007 signature_length, md, digest,
2008 digest_length);
2009 } else {
2010 if (SSL_USE_SIGALGS(s)) {
2011 /* The digest has already been selected and written. */
2012 p += 2;
2013 n += 2;
2014 }
David Benjamind1d80782015-07-05 11:54:09 -04002015 signature_length = ssl_private_key_max_signature_len(s);
David Benjaminb4d65fd2015-05-29 17:11:21 -04002016 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2017 sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length,
2018 signature_length);
Adam Langley1bea1732014-12-17 19:06:57 -08002019 }
2020
David Benjaminb4d65fd2015-05-29 17:11:21 -04002021 if (sign_result == ssl_private_key_retry) {
2022 s->state = SSL3_ST_CW_CERT_VRFY_B;
2023 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002024 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002025 s->rwstate = SSL_NOTHING;
2026 if (sign_result != ssl_private_key_success) {
2027 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002028 }
2029
2030 s2n(signature_length, p);
2031 n += signature_length + 2;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002032 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002033 return -1;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002034 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002035 s->state = SSL3_ST_CW_CERT_VRFY_C;
Adam Langley1bea1732014-12-17 19:06:57 -08002036 }
2037
Adam Langley1bea1732014-12-17 19:06:57 -08002038 return ssl_do_write(s);
Adam Langley1bea1732014-12-17 19:06:57 -08002039}
2040
2041/* ssl3_has_client_certificate returns true if a client certificate is
2042 * configured. */
David Benjamind1d80782015-07-05 11:54:09 -04002043static int ssl3_has_client_certificate(SSL *ssl) {
2044 return ssl->cert && ssl->cert->x509 && (ssl->cert->privatekey ||
2045 ssl->cert->key_method);
Adam Langley1bea1732014-12-17 19:06:57 -08002046}
2047
2048int ssl3_send_client_certificate(SSL *s) {
2049 X509 *x509 = NULL;
2050 EVP_PKEY *pkey = NULL;
2051 int i;
2052
2053 if (s->state == SSL3_ST_CW_CERT_A) {
2054 /* Let cert callback update client certificates if required */
2055 if (s->cert->cert_cb) {
2056 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2057 if (i < 0) {
2058 s->rwstate = SSL_X509_LOOKUP;
2059 return -1;
2060 }
2061 if (i == 0) {
2062 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2063 return 0;
2064 }
2065 s->rwstate = SSL_NOTHING;
2066 }
2067
2068 if (ssl3_has_client_certificate(s)) {
2069 s->state = SSL3_ST_CW_CERT_C;
2070 } else {
2071 s->state = SSL3_ST_CW_CERT_B;
2072 }
2073 }
2074
2075 /* We need to get a client cert */
2076 if (s->state == SSL3_ST_CW_CERT_B) {
2077 /* If we get an error, we need to:
2078 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2079 * We then get retried later */
2080 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2081 if (i < 0) {
2082 s->rwstate = SSL_X509_LOOKUP;
2083 return -1;
2084 }
2085 s->rwstate = SSL_NOTHING;
2086 if (i == 1 && pkey != NULL && x509 != NULL) {
2087 s->state = SSL3_ST_CW_CERT_B;
2088 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2089 i = 0;
2090 }
2091 } else if (i == 1) {
2092 i = 0;
David Benjamin3570d732015-06-29 00:28:17 -04002093 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
Adam Langley1bea1732014-12-17 19:06:57 -08002094 }
2095
David Benjamin2755a3e2015-04-22 16:17:58 -04002096 X509_free(x509);
2097 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002098 if (i && !ssl3_has_client_certificate(s)) {
2099 i = 0;
2100 }
2101 if (i == 0) {
2102 if (s->version == SSL3_VERSION) {
2103 s->s3->tmp.cert_req = 0;
2104 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2105 return 1;
2106 } else {
2107 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002108 /* There is no client certificate, so the handshake buffer may be
2109 * released. */
2110 if (s->s3->handshake_buffer &&
2111 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2112 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2113 return -1;
2114 }
Adam Langley1bea1732014-12-17 19:06:57 -08002115 }
2116 }
2117
2118 /* Ok, we have a cert */
2119 s->state = SSL3_ST_CW_CERT_C;
2120 }
2121
2122 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamind1d80782015-07-05 11:54:09 -04002123 if (s->s3->tmp.cert_req == 2) {
2124 /* Send an empty Certificate message. */
2125 uint8_t *p = ssl_handshake_start(s);
2126 l2n3(0, p);
2127 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, 3)) {
2128 return -1;
2129 }
2130 } else if (!ssl3_output_cert_chain(s)) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002131 return -1;
2132 }
David Benjamin67dc8382015-07-05 00:03:32 -04002133 s->state = SSL3_ST_CW_CERT_D;
Adam Langley1bea1732014-12-17 19:06:57 -08002134 }
2135
2136 /* SSL3_ST_CW_CERT_D */
2137 return ssl_do_write(s);
2138}
2139
Adam Langley1bea1732014-12-17 19:06:57 -08002140int ssl3_send_next_proto(SSL *s) {
2141 unsigned int len, padding_len;
2142 uint8_t *d, *p;
2143
2144 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2145 len = s->next_proto_negotiated_len;
2146 padding_len = 32 - ((len + 2) % 32);
2147
2148 d = p = ssl_handshake_start(s);
2149 *(p++) = len;
2150 memcpy(p, s->next_proto_negotiated, len);
2151 p += len;
2152 *(p++) = padding_len;
2153 memset(p, 0, padding_len);
2154 p += padding_len;
2155
David Benjaminfbdfefb2015-02-16 19:33:53 -05002156 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2157 return -1;
2158 }
Adam Langley1bea1732014-12-17 19:06:57 -08002159 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2160 }
2161
2162 return ssl_do_write(s);
2163}
2164
2165int ssl3_send_channel_id(SSL *s) {
2166 uint8_t *d;
2167 int ret = -1, public_key_len;
2168 EVP_MD_CTX md_ctx;
Adam Langley1bea1732014-12-17 19:06:57 -08002169 ECDSA_SIG *sig = NULL;
2170 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2171
2172 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2173 return ssl_do_write(s);
2174 }
2175
2176 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2177 EVP_PKEY *key = NULL;
2178 s->ctx->channel_id_cb(s, &key);
2179 if (key != NULL) {
2180 s->tlsext_channel_id_private = key;
2181 }
2182 }
2183
2184 if (!s->tlsext_channel_id_private) {
2185 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2186 return -1;
2187 }
2188 s->rwstate = SSL_NOTHING;
2189
David Benjamin961ad6a2015-06-12 01:40:23 -04002190 if (EVP_PKEY_id(s->tlsext_channel_id_private) != EVP_PKEY_EC) {
David Benjamin3570d732015-06-29 00:28:17 -04002191 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin961ad6a2015-06-12 01:40:23 -04002192 return -1;
2193 }
2194 EC_KEY *ec_key = s->tlsext_channel_id_private->pkey.ec;
2195
Adam Langley1bea1732014-12-17 19:06:57 -08002196 d = ssl_handshake_start(s);
Adam Langley49c7af12015-07-10 14:33:46 -07002197 s2n(TLSEXT_TYPE_channel_id, d);
Adam Langley1bea1732014-12-17 19:06:57 -08002198 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2199
2200 EVP_MD_CTX_init(&md_ctx);
2201
David Benjamin961ad6a2015-06-12 01:40:23 -04002202 public_key_len = i2o_ECPublicKey(ec_key, NULL);
Adam Langley1bea1732014-12-17 19:06:57 -08002203 if (public_key_len <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002204 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
Adam Langley1bea1732014-12-17 19:06:57 -08002205 goto err;
2206 }
2207
David Benjamin961ad6a2015-06-12 01:40:23 -04002208 /* i2o_ECPublicKey will produce an ANSI X9.62 public key which, for a
Adam Langley1bea1732014-12-17 19:06:57 -08002209 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2210 * field elements as 32-byte, big-endian numbers. */
2211 if (public_key_len != 65) {
David Benjamin3570d732015-06-29 00:28:17 -04002212 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
Adam Langley1bea1732014-12-17 19:06:57 -08002213 goto err;
2214 }
2215 public_key = OPENSSL_malloc(public_key_len);
2216 if (!public_key) {
David Benjamin3570d732015-06-29 00:28:17 -04002217 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08002218 goto err;
2219 }
2220
2221 derp = public_key;
David Benjamin961ad6a2015-06-12 01:40:23 -04002222 i2o_ECPublicKey(ec_key, &derp);
Adam Langley1bea1732014-12-17 19:06:57 -08002223
David Benjamin961ad6a2015-06-12 01:40:23 -04002224 uint8_t digest[EVP_MAX_MD_SIZE];
2225 unsigned digest_len;
2226 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2227 !tls1_channel_id_hash(&md_ctx, s) ||
2228 !EVP_DigestFinal_ex(&md_ctx, digest, &digest_len)) {
Adam Langley1bea1732014-12-17 19:06:57 -08002229 goto err;
2230 }
2231
David Benjamin961ad6a2015-06-12 01:40:23 -04002232 sig = ECDSA_do_sign(digest, digest_len, ec_key);
Adam Langley1bea1732014-12-17 19:06:57 -08002233 if (sig == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08002234 goto err;
2235 }
2236
2237 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2238 memcpy(d, public_key + 1, 64);
2239 d += 64;
2240 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2241 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
David Benjamin3570d732015-06-29 00:28:17 -04002242 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08002243 goto err;
2244 }
2245
David Benjaminfbdfefb2015-02-16 19:33:53 -05002246 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2247 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2248 goto err;
2249 }
Adam Langley1bea1732014-12-17 19:06:57 -08002250 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2251
2252 ret = ssl_do_write(s);
2253
2254err:
2255 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002256 OPENSSL_free(public_key);
2257 OPENSSL_free(der_sig);
2258 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002259
2260 return ret;
2261}
2262
2263int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2264 int i = 0;
2265 if (s->ctx->client_cert_cb) {
2266 i = s->ctx->client_cert_cb(s, px509, ppkey);
2267 }
2268 return i;
2269}