blob: 00b374cf7aed9234a1b0ad324498ff7ceb2705f3 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
158#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.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>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400166#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/rand.h>
168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_get_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400187
David Benjamince8c9d22016-11-14 10:45:16 +0900188int ssl3_accept(SSL_HANDSHAKE *hs) {
189 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400190 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400192 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 assert(ssl->handshake_func == ssl3_accept);
195 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 for (;;) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500198 state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
David Benjamincb0c29f2016-12-12 17:00:50 -0500202 hs->state = SSL_ST_ACCEPT;
David Benjaminfc0c9d92016-09-01 01:33:33 -0400203 skip = 1;
204 break;
205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400207 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin0d56f882015-12-19 17:05:56 -0500209 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400210 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 ret = -1;
212 goto end;
213 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500214
David Benjamincb0c29f2016-12-12 17:00:50 -0500215 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 case SSL3_ST_SR_CLNT_HELLO_A:
219 case SSL3_ST_SR_CLNT_HELLO_B:
220 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin25fe85b2016-08-09 20:00:32 -0400221 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin4eb95cc2016-11-16 17:08:23 +0900222 case SSL3_ST_SR_CLNT_HELLO_E:
David Benjaminc3c88822016-11-14 10:32:04 +0900223 ret = ssl3_get_client_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500224 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400225 break;
226 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 if (ret <= 0) {
228 goto end;
229 }
David Benjamindf50eec2016-06-07 16:49:42 -0400230 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500231 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 case SSL3_ST_SW_SRVR_HELLO_A:
235 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900236 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (ret <= 0) {
238 goto end;
239 }
Steven Valdez87eab492016-06-27 16:34:59 -0400240 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500241 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500243 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 case SSL3_ST_SW_CERT_A:
248 case SSL3_ST_SW_CERT_B:
David Benjamin3d458dc2016-09-12 22:40:27 +0000249 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900250 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (ret <= 0) {
252 goto end;
253 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 } else {
255 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500257 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100260 case SSL3_ST_SW_CERT_STATUS_A:
261 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900262 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900263 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400264 if (ret <= 0) {
265 goto end;
266 }
267 } else {
268 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100269 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500270 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100271 break;
272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 case SSL3_ST_SW_KEY_EXCH_A:
274 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700275 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500276 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamindf50eec2016-06-07 16:49:42 -0400278 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500279 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
280 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900281 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500282 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500284 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 } else {
286 skip = 1;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288
David Benjamincb0c29f2016-12-12 17:00:50 -0500289 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 case SSL3_ST_SW_CERT_REQ_A:
293 case SSL3_ST_SW_CERT_REQ_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900294 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900295 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 if (ret <= 0) {
297 goto end;
298 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400299 } else {
300 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500302 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 case SSL3_ST_SW_SRVR_DONE_A:
306 case SSL3_ST_SW_SRVR_DONE_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900307 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 if (ret <= 0) {
309 goto end;
310 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500311 hs->next_state = SSL3_ST_SR_CERT_A;
312 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900316 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900317 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (ret <= 0) {
319 goto end;
320 }
321 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500322 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 case SSL3_ST_SR_KEY_EXCH_A:
326 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900327 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 if (ret <= 0) {
329 goto end;
330 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500331 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700333
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900335 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 if (ret <= 0) {
337 goto end;
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
David Benjamincb0c29f2016-12-12 17:00:50 -0500340 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamina41280d2015-11-26 02:16:49 -0500343 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400344 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500345 if (ret <= 0) {
346 goto end;
347 }
Adam Langley95c29f32014-06-20 12:00:00 -0700348
David Benjamin67739722016-11-17 17:03:59 +0900349 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 ret = -1;
351 goto end;
352 }
David Benjamina41280d2015-11-26 02:16:49 -0500353
David Benjamincb0c29f2016-12-12 17:00:50 -0500354 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900358 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900359 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400360 if (ret <= 0) {
361 goto end;
362 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400364 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500366 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400370 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900371 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400372 if (ret <= 0) {
373 goto end;
374 }
375 } else {
376 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500378 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900382 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (ret <= 0) {
384 goto end;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
David Benjamindf50eec2016-06-07 16:49:42 -0400387 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400388 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500389 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500391 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 }
David Benjamindf50eec2016-06-07 16:49:42 -0400393
Steven Valdez87eab492016-06-27 16:34:59 -0400394 /* If this is a full handshake with ChannelID then record the handshake
395 * hashes in |ssl->s3->new_session| in case we need them to verify a
396 * ChannelID signature on a resumption of this session in the future. */
397 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500398 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 if (ret <= 0) {
400 goto end;
401 }
402 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 case SSL3_ST_SW_SESSION_TICKET_A:
406 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900407 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900408 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400409 if (ret <= 0) {
410 goto end;
411 }
412 } else {
413 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500415 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
David Benjamin352d0a92016-06-28 11:22:02 -0400418 case SSL3_ST_SW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500419 if (!ssl->method->add_change_cipher_spec(ssl) ||
420 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 ret = -1;
422 goto end;
423 }
David Benjamindaf207a2017-01-03 18:37:41 -0500424
425 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 case SSL3_ST_SW_FINISHED_A:
429 case SSL3_ST_SW_FINISHED_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900430 ret = ssl3_send_finished(hs, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500431 SSL3_ST_SW_FINISHED_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 if (ret <= 0) {
433 goto end;
434 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500435 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400436 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500437 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500439 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
David Benjamin9f1dc822016-06-07 17:03:46 -0400443 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500444 ret = ssl->method->flush_flight(ssl);
445 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400446 goto end;
447 }
448
David Benjamincb0c29f2016-12-12 17:00:50 -0500449 hs->state = hs->next_state;
450 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400451 ssl->method->expect_flight(ssl);
452 }
453 break;
454
Steven Valdez143e8b32016-07-11 13:19:03 -0400455 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900456 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400457 if (ret <= 0) {
458 goto end;
459 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500460 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400461 break;
462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400464 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Steven Valdez87eab492016-06-27 16:34:59 -0400466 /* If we aren't retaining peer certificates then we can discard it
467 * now. */
468 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900469 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800470 X509_free(ssl->s3->new_session->x509_peer);
471 ssl->s3->new_session->x509_peer = NULL;
472 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
473 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400474 }
475
476 SSL_SESSION_free(ssl->s3->established_session);
477 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400478 SSL_SESSION_up_ref(ssl->session);
479 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400480 } else {
481 ssl->s3->established_session = ssl->s3->new_session;
482 ssl->s3->established_session->not_resumable = 0;
483 ssl->s3->new_session = NULL;
484 }
485
David Benjamin78476f62016-11-12 11:20:55 +0900486 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900487 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900488
David Benjamin4e9cc712016-06-01 20:16:03 -0400489 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 ret = 1;
491 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 default:
David Benjamin3570d732015-06-29 00:28:17 -0400494 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 ret = -1;
496 goto end;
497 }
Adam Langley95c29f32014-06-20 12:00:00 -0700498
David Benjamincb0c29f2016-12-12 17:00:50 -0500499 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
500 int new_state = hs->state;
501 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400502 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500503 hs->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 }
505 skip = 0;
506 }
507
Adam Langley95c29f32014-06-20 12:00:00 -0700508end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400509 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 return ret;
511}
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamin731058e2016-12-03 23:15:13 -0500513int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
514 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400515 CBS cipher_suites;
516 CBS_init(&cipher_suites, client_hello->cipher_suites,
517 client_hello->cipher_suites_len);
518
519 while (CBS_len(&cipher_suites) > 0) {
520 uint16_t got_id;
521 if (!CBS_get_u16(&cipher_suites, &got_id)) {
522 return 0;
523 }
524
525 if (got_id == id) {
526 return 1;
527 }
528 }
529
530 return 0;
531}
532
David Benjaminf04c2e92016-12-06 13:35:25 -0500533static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500534 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500535 SSL *const ssl = hs->ssl;
David Benjamin25fe85b2016-08-09 20:00:32 -0400536 uint16_t min_version, max_version;
537 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
538 *out_alert = SSL_AD_PROTOCOL_VERSION;
539 return 0;
540 }
541
David Benjamin2dc02042016-09-19 19:57:37 -0400542 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400543 /* Check supported_versions extension if it is present. */
544 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500545 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
546 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400547 CBS versions;
548 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
549 CBS_len(&supported_versions) != 0 ||
550 CBS_len(&versions) == 0) {
551 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
552 *out_alert = SSL_AD_DECODE_ERROR;
553 return 0;
554 }
555
Brian Smithf85d3232016-10-28 10:34:06 -1000556 /* Choose the newest commonly-supported version advertised by the client.
557 * The client orders the versions according to its preferences, but we're
558 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400559 int found_version = 0;
560 while (CBS_len(&versions) != 0) {
561 uint16_t ext_version;
562 if (!CBS_get_u16(&versions, &ext_version)) {
563 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
564 *out_alert = SSL_AD_DECODE_ERROR;
565 return 0;
566 }
567 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
568 continue;
569 }
570 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000571 ext_version <= max_version &&
572 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400573 version = ext_version;
574 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400575 }
576 }
577
578 if (!found_version) {
579 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400580 }
581 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400582 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
583 * do not use this mechanism. */
584 if (SSL_is_dtls(ssl)) {
585 if (client_hello->version <= DTLS1_2_VERSION) {
586 version = TLS1_2_VERSION;
587 } else if (client_hello->version <= DTLS1_VERSION) {
588 version = TLS1_1_VERSION;
589 } else {
590 goto unsupported_protocol;
591 }
592 } else {
593 if (client_hello->version >= TLS1_2_VERSION) {
594 version = TLS1_2_VERSION;
595 } else if (client_hello->version >= TLS1_1_VERSION) {
596 version = TLS1_1_VERSION;
597 } else if (client_hello->version >= TLS1_VERSION) {
598 version = TLS1_VERSION;
599 } else if (client_hello->version >= SSL3_VERSION) {
600 version = SSL3_VERSION;
601 } else {
602 goto unsupported_protocol;
603 }
David Benjamin2dc02042016-09-19 19:57:37 -0400604 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400605
Steven Valdezfdd10992016-09-15 16:27:05 -0400606 /* Apply our minimum and maximum version. */
607 if (version > max_version) {
608 version = max_version;
609 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400610
Steven Valdezfdd10992016-09-15 16:27:05 -0400611 if (version < min_version) {
612 goto unsupported_protocol;
613 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400614 }
615
616 /* Handle FALLBACK_SCSV. */
617 if (ssl_client_cipher_list_contains_cipher(client_hello,
618 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
619 version < max_version) {
620 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
621 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
622 return 0;
623 }
624
David Benjaminf04c2e92016-12-06 13:35:25 -0500625 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400626 ssl->version = ssl->method->version_to_wire(version);
627 ssl->s3->enc_method = ssl3_get_enc_method(version);
628 assert(ssl->s3->enc_method != NULL);
629
630 /* At this point, the connection's version is known and |ssl->version| is
631 * fixed. Begin enforcing the record-layer version. */
632 ssl->s3->have_version = 1;
633
634 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400635
636unsupported_protocol:
637 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
638 *out_alert = SSL_AD_PROTOCOL_VERSION;
639 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400640}
641
David Benjamin2578b292016-12-03 23:19:55 -0500642static STACK_OF(SSL_CIPHER) *
643 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
644 CBS cipher_suites;
645 CBS_init(&cipher_suites, client_hello->cipher_suites,
646 client_hello->cipher_suites_len);
647
648 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
649 if (sk == NULL) {
650 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
651 goto err;
652 }
653
654 while (CBS_len(&cipher_suites) > 0) {
655 uint16_t cipher_suite;
656
657 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
658 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
659 goto err;
660 }
661
662 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
663 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
664 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
665 goto err;
666 }
667 }
668
669 return sk;
670
671err:
672 sk_SSL_CIPHER_free(sk);
673 return NULL;
674}
675
676/* ssl_get_compatible_server_ciphers determines the key exchange and
677 * authentication cipher suite masks compatible with the server configuration
678 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
679 * exchange mask and |*out_mask_a| to the authentication mask. */
680static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
681 uint32_t *out_mask_k,
682 uint32_t *out_mask_a) {
683 SSL *const ssl = hs->ssl;
684 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
685 *out_mask_k = SSL_kGENERIC;
686 *out_mask_a = SSL_aGENERIC;
687 return;
688 }
689
690 uint32_t mask_k = 0;
691 uint32_t mask_a = 0;
692
693 if (ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl)) {
694 int type = ssl_private_key_type(ssl);
695 if (type == NID_rsaEncryption) {
696 mask_k |= SSL_kRSA;
697 mask_a |= SSL_aRSA;
698 } else if (ssl_is_ecdsa_key_type(type)) {
699 mask_a |= SSL_aECDSA;
700 }
701 }
702
703 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
704 mask_k |= SSL_kDHE;
705 }
706
707 /* Check for a shared group to consider ECDHE ciphers. */
708 uint16_t unused;
709 if (tls1_get_shared_group(hs, &unused)) {
710 mask_k |= SSL_kECDHE;
711 }
712
David Benjamin2578b292016-12-03 23:19:55 -0500713 /* PSK requires a server callback. */
714 if (ssl->psk_server_callback != NULL) {
715 mask_k |= SSL_kPSK;
716 mask_a |= SSL_aPSK;
717 }
718
719 *out_mask_k = mask_k;
720 *out_mask_a = mask_a;
721}
722
723static const SSL_CIPHER *ssl3_choose_cipher(
724 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
725 const struct ssl_cipher_preference_list_st *server_pref) {
726 SSL *const ssl = hs->ssl;
727 const SSL_CIPHER *c, *ret = NULL;
728 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
729 int ok;
730 size_t cipher_index;
731 uint32_t alg_k, alg_a, mask_k, mask_a;
732 /* in_group_flags will either be NULL, or will point to an array of bytes
733 * which indicate equal-preference groups in the |prio| stack. See the
734 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
735 * struct. */
736 const uint8_t *in_group_flags;
737 /* group_min contains the minimal index so far found in a group, or -1 if no
738 * such value exists yet. */
739 int group_min = -1;
740
741 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
742 if (clnt == NULL) {
743 return NULL;
744 }
745
746 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
747 prio = srvr;
748 in_group_flags = server_pref->in_group_flags;
749 allow = clnt;
750 } else {
751 prio = clnt;
752 in_group_flags = NULL;
753 allow = srvr;
754 }
755
756 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
757
758 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
759 c = sk_SSL_CIPHER_value(prio, i);
760
761 ok = 1;
762
763 /* Check the TLS version. */
764 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
765 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
766 ok = 0;
767 }
768
769 alg_k = c->algorithm_mkey;
770 alg_a = c->algorithm_auth;
771
772 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
773
774 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
775 if (in_group_flags != NULL && in_group_flags[i] == 1) {
776 /* This element of |prio| is in a group. Update the minimum index found
777 * so far and continue looking. */
778 if (group_min == -1 || (size_t)group_min > cipher_index) {
779 group_min = cipher_index;
780 }
781 } else {
782 if (group_min != -1 && (size_t)group_min < cipher_index) {
783 cipher_index = group_min;
784 }
785 ret = sk_SSL_CIPHER_value(allow, cipher_index);
786 break;
787 }
788 }
789
790 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
791 /* We are about to leave a group, but we found a match in it, so that's
792 * our answer. */
793 ret = sk_SSL_CIPHER_value(allow, group_min);
794 break;
795 }
796 }
797
798 sk_SSL_CIPHER_free(clnt);
799 return ret;
800}
801
David Benjaminc3c88822016-11-14 10:32:04 +0900802static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
803 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400804 uint8_t al = SSL_AD_INTERNAL_ERROR;
805 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400806 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700807
David Benjamincb0c29f2016-12-12 17:00:50 -0500808 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjamine14ff062016-08-09 16:21:24 -0400809 /* The first time around, read the ClientHello. */
810 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
811 ssl_hash_message);
812 if (msg_ret <= 0) {
813 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700814 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400815
David Benjamincb0c29f2016-12-12 17:00:50 -0500816 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 }
Adam Langley95c29f32014-06-20 12:00:00 -0700818
David Benjamin731058e2016-12-03 23:15:13 -0500819 SSL_CLIENT_HELLO client_hello;
820 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
821 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400823 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 goto f_err;
825 }
Adam Langley95c29f32014-06-20 12:00:00 -0700826
David Benjamincb0c29f2016-12-12 17:00:50 -0500827 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjamine14ff062016-08-09 16:21:24 -0400828 /* Run the early callback. */
829 if (ssl->ctx->select_certificate_cb != NULL) {
830 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
831 case 0:
832 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
833 goto err;
834
835 case -1:
836 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400837 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
839 goto f_err;
840
841 default:
842 /* fallthrough */;
843 }
844 }
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000845 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamine14ff062016-08-09 16:21:24 -0400846 }
847
David Benjamin25fe85b2016-08-09 20:00:32 -0400848 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500849 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500850 if (!negotiate_version(hs, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 goto f_err;
852 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400853
David Benjamin25fe85b2016-08-09 20:00:32 -0400854 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500855 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900856 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400857 return 1;
858 }
859 }
860
David Benjamincb0c29f2016-12-12 17:00:50 -0500861 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400862 /* Load the client random. */
863 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
864 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
865 return -1;
866 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500867 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
868 client_hello.random_len);
David Benjamin25fe85b2016-08-09 20:00:32 -0400869
David Benjamin4eb95cc2016-11-16 17:08:23 +0900870 /* Only null compression is supported. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500871 if (OPENSSL_memchr(client_hello.compression_methods, 0,
872 client_hello.compression_methods_len) == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900873 al = SSL_AD_ILLEGAL_PARAMETER;
874 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
875 goto f_err;
876 }
877
878 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500879 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900880 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
881 goto err;
882 }
883
David Benjamincb0c29f2016-12-12 17:00:50 -0500884 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900885 }
886
David Benjamincb0c29f2016-12-12 17:00:50 -0500887 if (hs->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400888 /* Call |cert_cb| to update server certificates if required. */
889 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500890 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 if (rv == 0) {
892 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400893 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 goto f_err;
895 }
896 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500897 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 goto err;
899 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800901
David Benjamin650aa1c2016-12-20 18:55:16 -0500902 if (!ssl_auto_chain_if_needed(ssl)) {
903 goto err;
904 }
905
David Benjaminf01f42a2016-11-16 19:05:33 +0900906 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
907 * certificate is finalized. */
908 ssl->s3->tmp.new_cipher =
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900909 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjaminf01f42a2016-11-16 19:05:33 +0900910 if (ssl->s3->tmp.new_cipher == NULL) {
911 al = SSL_AD_HANDSHAKE_FAILURE;
912 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
913 goto f_err;
914 }
915
David Benjamincb0c29f2016-12-12 17:00:50 -0500916 hs->state = SSL3_ST_SR_CLNT_HELLO_E;
David Benjamin34202b92016-11-16 19:07:53 +0900917 }
918
David Benjamincb0c29f2016-12-12 17:00:50 -0500919 assert(hs->state == SSL3_ST_SR_CLNT_HELLO_E);
David Benjamin34202b92016-11-16 19:07:53 +0900920
921 /* Determine whether we are doing session resumption. */
922 int tickets_supported = 0, renew_ticket = 0;
923 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
924 &client_hello)) {
925 case ssl_session_success:
926 break;
927 case ssl_session_error:
928 goto err;
929 case ssl_session_retry:
930 ssl->rwstate = SSL_PENDING_SESSION;
931 goto err;
932 }
933
934 if (session != NULL) {
935 if (session->extended_master_secret &&
936 !ssl->s3->tmp.extended_master_secret) {
937 /* A ClientHello without EMS that attempts to resume a session with EMS
938 * is fatal to the connection. */
939 al = SSL_AD_HANDSHAKE_FAILURE;
940 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
941 goto f_err;
942 }
943
944 if (!ssl_session_is_resumable(ssl, session) ||
945 /* If the client offers the EMS extension, but the previous session
946 * didn't use it, then negotiate a new session. */
947 ssl->s3->tmp.extended_master_secret !=
948 session->extended_master_secret) {
949 SSL_SESSION_free(session);
950 session = NULL;
951 }
952 }
953
954 if (session != NULL) {
955 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900956 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900957 ssl->session = session;
958 session = NULL;
959 ssl->s3->session_reused = 1;
960 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900961 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900962 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900963 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900964 goto err;
965 }
966
967 /* Clear the session ID if we want the session to be single-use. */
968 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
969 ssl->s3->new_session->session_id_length = 0;
970 }
971 }
972
973 if (ssl->ctx->dos_protection_cb != NULL &&
974 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
975 /* Connection rejected for DOS reasons. */
976 al = SSL_AD_INTERNAL_ERROR;
977 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
978 goto f_err;
979 }
980
David Benjaminf01f42a2016-11-16 19:05:33 +0900981 if (ssl->session == NULL) {
982 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400983
David Benjamin4eb95cc2016-11-16 17:08:23 +0900984 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900985 if (hs->hostname != NULL) {
986 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +0900987 if (ssl->s3->new_session->tlsext_hostname == NULL) {
988 al = SSL_AD_INTERNAL_ERROR;
989 goto f_err;
990 }
991 }
992
David Benjamin5c1ce292015-05-26 16:59:43 -0400993 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900994 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400995 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500996 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
997 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900998 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400999 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001000 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001001 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +09001002 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001003 }
Adam Langley37646832016-08-01 16:16:46 -07001004
David Benjaminc3c88822016-11-14 10:32:04 +09001005 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001006 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001007 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001008 ssl->s3->new_session->verify_result = X509_V_OK;
1009 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 }
1011
David Benjaminf01f42a2016-11-16 19:05:33 +09001012 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1013 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001014 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001015 goto f_err;
1016 }
1017
David Benjaminf01f42a2016-11-16 19:05:33 +09001018 /* Now that all parameters are known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -05001019 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001020 goto f_err;
1021 }
1022
Steven Valdez2b8415e2016-06-30 13:27:23 -04001023 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001024 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -05001025 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 }
1027
David Benjamin0bd71eb2015-12-01 14:14:40 -05001028 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001029
1030 if (0) {
1031 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001032 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 }
1034
Adam Langley95c29f32014-06-20 12:00:00 -07001035err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001036 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 return ret;
1038}
Adam Langley95c29f32014-06-20 12:00:00 -07001039
David Benjaminc3c88822016-11-14 10:32:04 +09001040static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1041 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001042 if (hs->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -04001043 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjamincb0c29f2016-12-12 17:00:50 -05001046 assert(hs->state == SSL3_ST_SW_SRVR_HELLO_A);
David Benjamin56380462015-10-10 14:59:09 -04001047
1048 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1049 * known attack while we fix ChannelID itself. */
1050 if (ssl->s3->tlsext_channel_id_valid &&
1051 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1052 ssl->s3->tlsext_channel_id_valid = 0;
1053 }
1054
1055 /* If this is a resumption and the original handshake didn't support
1056 * ChannelID then we didn't record the original handshake hashes in the
1057 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001058 if (ssl->session != NULL &&
1059 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001060 ssl->s3->tlsext_channel_id_valid = 0;
1061 }
1062
David Benjamin721e8b72016-08-03 13:13:17 -04001063 struct timeval now;
1064 ssl_get_current_time(ssl, &now);
1065 ssl->s3->server_random[0] = now.tv_sec >> 24;
1066 ssl->s3->server_random[1] = now.tv_sec >> 16;
1067 ssl->s3->server_random[2] = now.tv_sec >> 8;
1068 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001069 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001070 return -1;
1071 }
1072
David Benjamin55108632016-08-11 22:01:18 -04001073 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1074 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001075
Steven Valdez87eab492016-06-27 16:34:59 -04001076 const SSL_SESSION *session = ssl->s3->new_session;
1077 if (ssl->session != NULL) {
1078 session = ssl->session;
1079 }
1080
David Benjamin75836432016-06-17 18:48:29 -04001081 CBB cbb, body, session_id;
1082 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1083 !CBB_add_u16(&body, ssl->version) ||
1084 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1085 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001086 !CBB_add_bytes(&session_id, session->session_id,
1087 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -04001088 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1089 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001090 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001091 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001092 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1093 CBB_cleanup(&cbb);
1094 return -1;
1095 }
1096
David Benjamincb0c29f2016-12-12 17:00:50 -05001097 hs->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -04001098 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001099}
Adam Langley95c29f32014-06-20 12:00:00 -07001100
David Benjaminc3c88822016-11-14 10:32:04 +09001101static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1102 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001103 if (hs->state == SSL3_ST_SW_CERT_B) {
David Benjamin75836432016-06-17 18:48:29 -04001104 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001105 }
1106
David Benjamin32a66d52016-07-13 22:03:11 -04001107 if (!ssl_has_certificate(ssl)) {
1108 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
1109 return 0;
1110 }
1111
David Benjamin75836432016-06-17 18:48:29 -04001112 if (!ssl3_output_cert_chain(ssl)) {
1113 return 0;
1114 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001115 hs->state = SSL3_ST_SW_CERT_B;
David Benjamin75836432016-06-17 18:48:29 -04001116 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001117}
1118
David Benjaminc3c88822016-11-14 10:32:04 +09001119static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1120 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001121 if (hs->state == SSL3_ST_SW_CERT_STATUS_B) {
David Benjamin75836432016-06-17 18:48:29 -04001122 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001123 }
1124
David Benjamin75836432016-06-17 18:48:29 -04001125 CBB cbb, body, ocsp_response;
1126 if (!ssl->method->init_message(ssl, &cbb, &body,
1127 SSL3_MT_CERTIFICATE_STATUS) ||
1128 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1129 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001130 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1131 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001132 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001133 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1134 CBB_cleanup(&cbb);
1135 return -1;
1136 }
1137
David Benjamincb0c29f2016-12-12 17:00:50 -05001138 hs->state = SSL3_ST_SW_CERT_STATUS_B;
David Benjamin75836432016-06-17 18:48:29 -04001139 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001140}
1141
David Benjaminc3c88822016-11-14 10:32:04 +09001142static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1143 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001144 if (hs->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjamin75836432016-06-17 18:48:29 -04001145 return ssl->method->write_message(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001146 }
1147
David Benjamin2a0b3912015-12-18 01:01:21 -05001148 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001149 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001150
David Benjaminc42acee2016-06-17 17:47:58 -04001151 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001152 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001153 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1154 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001155
David Benjaminc42acee2016-06-17 17:47:58 -04001156 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1157 if (!CBB_init(&cbb, 128)) {
1158 goto err;
1159 }
1160
David Benjamin2a0b3912015-12-18 01:01:21 -05001161 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001163 size_t len =
1164 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1165 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1166 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1167 len)) {
1168 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
David Benjamin7061e282015-03-19 11:10:48 -04001172 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001173 /* Determine the group to use. */
1174 DH *params = ssl->cert->dh_tmp;
1175 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1176 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001178 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001179 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001180 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 goto err;
1182 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001183
David Benjamin974c7ba2015-12-19 16:58:04 -05001184 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001185 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001186 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 goto err;
1188 }
David Benjamin9f226a52015-04-26 22:12:32 -04001189
David Benjaminc3c88822016-11-14 10:32:04 +09001190 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001191 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001192 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001193 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001194 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001195 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001196 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001197 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 }
David Benjamin7061e282015-03-19 11:10:48 -04001199 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001200 /* Determine the group to use. */
1201 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001202 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001203 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001204 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 goto err;
1206 }
David Benjamin4882a6c2016-12-11 02:48:12 -05001207 ssl->s3->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001208
David Benjamin4298d772015-12-19 00:18:25 -05001209 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001210 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001211 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001212 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001213 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001214 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001215 goto err;
1216 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001217 } else {
1218 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 }
Adam Langley95c29f32014-06-20 12:00:00 -07001220
David Benjaminc3c88822016-11-14 10:32:04 +09001221 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001222 goto err;
1223 }
David Benjaminc42acee2016-06-17 17:47:58 -04001224 }
1225
1226 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001227 CBB body;
1228 if (!ssl->method->init_message(ssl, &cbb, &body,
1229 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001230 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001231 goto err;
1232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
David Benjamin2a0b3912015-12-18 01:01:21 -05001234 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001235 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001236 if (!ssl_has_private_key(ssl)) {
1237 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 goto err;
1239 }
Adam Langley95c29f32014-06-20 12:00:00 -07001240
David Benjaminc42acee2016-06-17 17:47:58 -04001241 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001242 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001243 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001244 goto err;
1245 }
David Benjaminc42acee2016-06-17 17:47:58 -04001246 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001247 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001248 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1249 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1250 goto err;
1251 }
David Benjaminc42acee2016-06-17 17:47:58 -04001252 }
1253
1254 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001255 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001256 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001257 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001258 !CBB_reserve(&child, &ptr, max_sig_len)) {
1259 goto err;
1260 }
1261
David Benjamin2a0b3912015-12-18 01:01:21 -05001262 size_t sig_len;
1263 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001264 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001265 CBB transcript;
1266 uint8_t *transcript_data;
1267 size_t transcript_len;
1268 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001269 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1270 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1271 SSL3_RANDOM_SIZE) ||
1272 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1273 SSL3_RANDOM_SIZE) ||
1274 !CBB_add_bytes(&transcript, hs->server_params,
1275 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001276 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1277 CBB_cleanup(&transcript);
1278 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1279 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001280 goto err;
1281 }
1282
Steven Valdezf0451ca2016-06-29 13:16:27 -04001283 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001284 signature_algorithm, transcript_data,
1285 transcript_len);
1286 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001287 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001288 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001289 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001291
1292 switch (sign_result) {
1293 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001294 if (!CBB_did_write(&child, sig_len)) {
1295 goto err;
1296 }
1297 break;
1298 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001299 goto err;
1300 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001301 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001302 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001303 goto err;
1304 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001305 }
1306
David Benjamindaf207a2017-01-03 18:37:41 -05001307 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001308 goto err;
1309 }
David Benjaminc42acee2016-06-17 17:47:58 -04001310
David Benjaminc3c88822016-11-14 10:32:04 +09001311 OPENSSL_free(hs->server_params);
1312 hs->server_params = NULL;
1313 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001314
David Benjamincb0c29f2016-12-12 17:00:50 -05001315 hs->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001316 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001317
Adam Langley95c29f32014-06-20 12:00:00 -07001318err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001319 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 return -1;
1321}
Adam Langley95c29f32014-06-20 12:00:00 -07001322
David Benjamin75836432016-06-17 18:48:29 -04001323static int add_cert_types(SSL *ssl, CBB *cbb) {
1324 /* Get configured signature algorithms. */
1325 int have_rsa_sign = 0;
1326 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001327 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001328 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1329 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001330 switch (sig_algs[i]) {
1331 case SSL_SIGN_RSA_PKCS1_SHA512:
1332 case SSL_SIGN_RSA_PKCS1_SHA384:
1333 case SSL_SIGN_RSA_PKCS1_SHA256:
1334 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001335 have_rsa_sign = 1;
1336 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001337
Steven Valdez02563852016-06-23 13:33:05 -04001338 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1339 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1340 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1341 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001342 have_ecdsa_sign = 1;
1343 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 }
Adam Langley95c29f32014-06-20 12:00:00 -07001346
David Benjamin75836432016-06-17 18:48:29 -04001347 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1348 return 0;
1349 }
1350
1351 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1352 * check for SSL_kECDH or SSL_kECDHE. */
1353 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1354 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1355 return 0;
1356 }
1357
1358 return 1;
1359}
1360
David Benjaminc3c88822016-11-14 10:32:04 +09001361static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1362 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001363 if (hs->state == SSL3_ST_SW_CERT_REQ_B) {
David Benjamin75836432016-06-17 18:48:29 -04001364 return ssl->method->write_message(ssl);
1365 }
1366
David Benjamin32a66d52016-07-13 22:03:11 -04001367 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001368 if (!ssl->method->init_message(ssl, &cbb, &body,
1369 SSL3_MT_CERTIFICATE_REQUEST) ||
1370 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1371 !add_cert_types(ssl, &cert_types)) {
1372 goto err;
1373 }
1374
1375 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001376 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001377 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001378 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001379 goto err;
1380 }
Steven Valdez02563852016-06-23 13:33:05 -04001381
David Benjamin0fc37ef2016-08-17 15:29:46 -04001382 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001383 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1384 goto err;
1385 }
1386 }
David Benjamin75836432016-06-17 18:48:29 -04001387 }
1388
David Benjamin32a66d52016-07-13 22:03:11 -04001389 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001390 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001391 goto err;
1392 }
1393
David Benjamincb0c29f2016-12-12 17:00:50 -05001394 hs->state = SSL3_ST_SW_CERT_REQ_B;
David Benjamin75836432016-06-17 18:48:29 -04001395 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001396
Adam Langley95c29f32014-06-20 12:00:00 -07001397err:
David Benjamin75836432016-06-17 18:48:29 -04001398 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1399 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 return -1;
1401}
Adam Langley95c29f32014-06-20 12:00:00 -07001402
David Benjaminc3c88822016-11-14 10:32:04 +09001403static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1404 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001405 if (hs->state == SSL3_ST_SW_SRVR_DONE_B) {
David Benjamin75836432016-06-17 18:48:29 -04001406 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001407 }
1408
David Benjamin75836432016-06-17 18:48:29 -04001409 CBB cbb, body;
1410 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001411 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001412 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1413 CBB_cleanup(&cbb);
1414 return -1;
1415 }
1416
David Benjamincb0c29f2016-12-12 17:00:50 -05001417 hs->state = SSL3_ST_SW_SRVR_DONE_B;
David Benjamin75836432016-06-17 18:48:29 -04001418 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001419}
1420
David Benjaminc3c88822016-11-14 10:32:04 +09001421static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1422 SSL *const ssl = hs->ssl;
1423 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001424
David Benjamin09eb6552016-07-08 14:32:11 -07001425 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1426 if (msg_ret <= 0) {
1427 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001428 }
1429
1430 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1431 if (ssl->version == SSL3_VERSION &&
1432 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001433 /* In SSL 3.0, the Certificate message is omitted to signal no
1434 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001435 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001436 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001437 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1438 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001439 }
1440
Adam Langley37646832016-08-01 16:16:46 -07001441 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001442 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001443 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001444 ssl->s3->tmp.reuse_message = 1;
1445 return 1;
1446 }
1447
David Benjamin9f1dc822016-06-07 17:03:46 -04001448 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001449 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1450 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001451 }
1452
David Benjamin5c900c82016-07-13 23:03:26 -04001453 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001454 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001455
1456 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001457 EVP_PKEY_free(hs->peer_pubkey);
1458 hs->peer_pubkey = NULL;
David Benjamin5c900c82016-07-13 23:03:26 -04001459 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001460 ssl->s3->new_session->certs =
Adam Langleyd5157222016-12-12 11:37:43 -08001461 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1462 ssl->retain_only_sha256_of_client_certs
1463 ? ssl->s3->new_session->peer_sha256
1464 : NULL,
Adam Langleyd519bf62016-12-12 11:16:44 -08001465 &certificate_msg, ssl->ctx->pool);
Adam Langley68e71242016-12-12 11:06:16 -08001466 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001467 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001468 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001469 }
1470
Adam Langley68e71242016-12-12 11:06:16 -08001471 if (CBS_len(&certificate_msg) != 0 ||
1472 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001473 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001474 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001475 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001476 }
1477
Adam Langley68e71242016-12-12 11:06:16 -08001478 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001479 /* No client certificate so the handshake buffer may be discarded. */
1480 ssl3_free_handshake_buffer(ssl);
1481
David Benjamine455e512016-08-01 20:11:13 -04001482 /* In SSL 3.0, sending no certificate is signaled by omitting the
1483 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001484 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001485 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001486 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001487 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001488 }
1489
1490 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001491 /* Fail for TLS only if we required a certificate */
1492 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001493 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001494 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001495 }
Adam Langley37646832016-08-01 16:16:46 -07001496
1497 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001498 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001499 ssl->s3->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001500 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001501 }
1502
Adam Langley68e71242016-12-12 11:06:16 -08001503 /* The hash will have been filled in. */
1504 if (ssl->retain_only_sha256_of_client_certs) {
1505 ssl->s3->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001506 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001507
Adam Langley68e71242016-12-12 11:06:16 -08001508 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1509 ssl->s3->new_session->x509_chain)) {
1510 return -1;
1511 }
David Benjamin5c900c82016-07-13 23:03:26 -04001512 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001513}
1514
David Benjaminc3c88822016-11-14 10:32:04 +09001515static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1516 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001517 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001519 uint32_t alg_k;
1520 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001521 uint8_t *premaster_secret = NULL;
1522 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001524
David Benjamin4298d772015-12-19 00:18:25 -05001525 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001527
David Benjamincb0c29f2016-12-12 17:00:50 -05001528 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001529 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1530 ssl_hash_message);
1531 if (ret <= 0) {
1532 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001533 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 }
Adam Langley95c29f32014-06-20 12:00:00 -07001535
David Benjamin0d56f882015-12-19 17:05:56 -05001536 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1537 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1538 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001539
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 /* If using a PSK key exchange, prepare the pre-shared key. */
1541 if (alg_a & SSL_aPSK) {
1542 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001543
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1545 * then this is the only field in the message. */
1546 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1547 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001548 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 al = SSL_AD_DECODE_ERROR;
1550 goto f_err;
1551 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001552
David Benjamin0d56f882015-12-19 17:05:56 -05001553 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001554 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 al = SSL_AD_INTERNAL_ERROR;
1556 goto f_err;
1557 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001558
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1560 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001561 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 al = SSL_AD_ILLEGAL_PARAMETER;
1563 goto f_err;
1564 }
David Benjamin35c02302014-07-13 04:14:59 -04001565
Steven Valdez87eab492016-06-27 16:34:59 -04001566 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001568 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 goto f_err;
1570 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001571
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001573 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1574 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001576 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 al = SSL_AD_INTERNAL_ERROR;
1578 goto f_err;
1579 } else if (psk_len == 0) {
1580 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001581 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1583 goto f_err;
1584 }
1585 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 /* Depending on the key exchange method, compute |premaster_secret| and
1588 * |premaster_secret_len|. */
1589 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001591 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 decrypt_buf = OPENSSL_malloc(rsa_size);
1593 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001594 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 goto err;
1596 }
David Benjamin35c02302014-07-13 04:14:59 -04001597
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001598 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001599 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001600 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001601 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001602 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001603 al = SSL_AD_HANDSHAKE_FAILURE;
1604 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1605 goto f_err;
1606 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001607 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001608 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001609 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1610 &encrypted_premaster_secret) ||
1611 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001612 al = SSL_AD_DECODE_ERROR;
1613 OPENSSL_PUT_ERROR(SSL,
1614 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1615 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001616 }
1617 } else {
1618 encrypted_premaster_secret = client_key_exchange;
1619 }
1620
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001621 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1622 * timing-sensitive code below. */
1623 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001624 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001625 CBS_data(&encrypted_premaster_secret),
1626 CBS_len(&encrypted_premaster_secret));
1627 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001628 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001629 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001630 decrypt_result =
1631 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001633
1634 switch (decrypt_result) {
1635 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001636 break;
1637 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001638 goto err;
1639 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001640 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001641 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001642 goto err;
1643 }
1644
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001645 if (decrypt_len != rsa_size) {
1646 al = SSL_AD_DECRYPT_ERROR;
1647 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1648 goto f_err;
1649 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001650
David Benjamin3f5b43d2015-12-01 19:31:24 -05001651 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1652 * section 7.4.7.1. */
1653 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1654 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001656 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 goto err;
1658 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001659 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001660 goto err;
1661 }
1662
David Benjamin3f5b43d2015-12-01 19:31:24 -05001663 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1664 * publicly invalid. */
1665 if (decrypt_len < 11 + premaster_secret_len) {
1666 al = SSL_AD_DECRYPT_ERROR;
1667 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1668 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 }
David Benjamin35c02302014-07-13 04:14:59 -04001670
David Benjamin3f5b43d2015-12-01 19:31:24 -05001671 /* Check the padding. See RFC 3447, section 7.2.2. */
1672 size_t padding_len = decrypt_len - premaster_secret_len;
1673 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1674 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001675 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001676 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1677 }
1678 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1679
1680 /* The premaster secret must begin with |client_version|. This too must be
1681 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1682 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001683 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001684 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001685 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001686
1687 /* Select, in constant time, either the decrypted premaster or the random
1688 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001689 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001690 premaster_secret[i] = constant_time_select_8(
1691 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1692 }
1693
1694 OPENSSL_free(decrypt_buf);
1695 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001696 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001697 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001698 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001699 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001700 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001702 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 goto f_err;
1704 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001705
David Benjamin4298d772015-12-19 00:18:25 -05001706 /* Compute the premaster. */
1707 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001708 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001709 &premaster_secret_len, &alert, CBS_data(&peer_key),
1710 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001711 al = alert;
1712 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 }
Adam Langley95c29f32014-06-20 12:00:00 -07001714
David Benjamin4298d772015-12-19 00:18:25 -05001715 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001716 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 } else if (alg_k & SSL_kPSK) {
1718 /* For plain PSK, other_secret is a block of 0s with the same length as the
1719 * pre-shared key. */
1720 premaster_secret_len = psk_len;
1721 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1722 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001723 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 goto err;
1725 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001726 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 } else {
1728 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001729 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 goto f_err;
1731 }
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1734 * pre-shared key. */
1735 if (alg_a & SSL_aPSK) {
1736 CBB new_premaster, child;
1737 uint8_t *new_data;
1738 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001739
David Benjamina8653202015-06-28 01:26:10 -04001740 CBB_zero(&new_premaster);
1741 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1742 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1744 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1745 !CBB_add_bytes(&child, psk, psk_len) ||
1746 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001747 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 CBB_cleanup(&new_premaster);
1749 goto err;
1750 }
David Benjamin14c83e72014-07-13 04:54:57 -04001751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1753 OPENSSL_free(premaster_secret);
1754 premaster_secret = new_data;
1755 premaster_secret_len = new_len;
1756 }
David Benjamin14c83e72014-07-13 04:54:57 -04001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001759 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1760 ssl, ssl->s3->new_session->master_key, premaster_secret,
1761 premaster_secret_len);
1762 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 goto err;
1764 }
Steven Valdez87eab492016-06-27 16:34:59 -04001765 ssl->s3->new_session->extended_master_secret =
1766 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1769 OPENSSL_free(premaster_secret);
1770 return 1;
1771
Adam Langley95c29f32014-06-20 12:00:00 -07001772f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001773 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001774err:
David Benjamin4298d772015-12-19 00:18:25 -05001775 if (premaster_secret != NULL) {
1776 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 OPENSSL_free(premaster_secret);
1778 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001779 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 return -1;
1782}
Adam Langley95c29f32014-06-20 12:00:00 -07001783
David Benjaminc3c88822016-11-14 10:32:04 +09001784static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1785 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001786 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 /* Only RSA and ECDSA client certificates are supported, so a
1790 * CertificateVerify is required if and only if there's a client certificate.
1791 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001792 if (hs->peer_pubkey == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001793 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 return 1;
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
David Benjamin09eb6552016-07-08 14:32:11 -07001797 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1798 ssl_dont_hash_message);
1799 if (msg_ret <= 0) {
1800 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001801 }
David Benjaminef865502014-08-24 02:48:34 -04001802
David Benjamin09eb6552016-07-08 14:32:11 -07001803 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001806 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001807 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001808 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001809 al = SSL_AD_DECODE_ERROR;
1810 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1811 goto f_err;
1812 }
David Benjamin887c3002016-07-08 16:15:32 -07001813 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001814 goto f_err;
1815 }
David Benjaminf1050fd2016-12-13 20:05:36 -05001816 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001817 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001818 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001819 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001820 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001821 } else {
1822 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1823 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1824 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 }
David Benjamin854dd652014-08-26 00:32:30 -04001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 /* Parse and verify the signature. */
1828 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1829 CBS_len(&certificate_verify) != 0) {
1830 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001831 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001832 goto f_err;
1833 }
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Steven Valdez2b8415e2016-06-30 13:27:23 -04001835 int sig_ok;
1836 /* The SSL3 construction for CertificateVerify does not decompose into a
1837 * single final digest and signature, and must be special-cased. */
1838 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001839 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001840 uint8_t digest[EVP_MAX_MD_SIZE];
1841 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001842 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001843 signature_algorithm)) {
1844 goto err;
1845 }
1846
Adam Langleyd5157222016-12-12 11:37:43 -08001847 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001848 sig_ok = pctx != NULL &&
1849 EVP_PKEY_verify_init(pctx) &&
1850 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1851 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1852 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001853 EVP_PKEY_CTX_free(pctx);
1854 } else {
1855 sig_ok = ssl_public_key_verify(
1856 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001857 hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001858 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001860
David Benjaminbf82aed2016-03-01 22:57:40 -05001861#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1862 sig_ok = 1;
1863 ERR_clear_error();
1864#endif
1865 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001866 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001867 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 goto f_err;
1869 }
1870
Steven Valdez2b8415e2016-06-30 13:27:23 -04001871 /* The handshake buffer is no longer necessary, and we may hash the current
1872 * message.*/
1873 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001874 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001875 goto err;
1876 }
1877
Adam Langleyd5157222016-12-12 11:37:43 -08001878 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001879
Adam Langleyd5157222016-12-12 11:37:43 -08001880f_err:
1881 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001882err:
Adam Langleyd5157222016-12-12 11:37:43 -08001883 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001884}
Adam Langley95c29f32014-06-20 12:00:00 -07001885
Adam Langley95c29f32014-06-20 12:00:00 -07001886/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1887 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001888static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1889 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001890 int ret =
1891 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1892 if (ret <= 0) {
1893 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
David Benjamin09eb6552016-07-08 14:32:11 -07001896 CBS next_protocol, selected_protocol, padding;
1897 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1899 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001900 CBS_len(&next_protocol) != 0) {
1901 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1902 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1903 return 0;
1904 }
1905
1906 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001907 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 return 0;
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 return 1;
1912}
Adam Langley95c29f32014-06-20 12:00:00 -07001913
Adam Langley1258b6a2014-06-20 12:00:00 -07001914/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001915static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1916 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001917 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1918 ssl_dont_hash_message);
1919 if (msg_ret <= 0) {
1920 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001922
Nick Harper60a85cb2016-09-23 16:25:11 -07001923 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001924 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 return -1;
1926 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001927 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001928}
David Benjamin9f1dc822016-06-07 17:03:46 -04001929
David Benjaminc3c88822016-11-14 10:32:04 +09001930static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1931 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001932 if (hs->state == SSL3_ST_SW_SESSION_TICKET_B) {
David Benjamin75836432016-06-17 18:48:29 -04001933 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001934 }
1935
David Benjamin123db572016-11-03 16:59:25 -04001936 const SSL_SESSION *session;
1937 SSL_SESSION *session_copy = NULL;
1938 if (ssl->session == NULL) {
1939 /* Fix the timeout to measure from the ticket issuance time. */
1940 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1941 session = ssl->s3->new_session;
1942 } else {
1943 /* We are renewing an existing session. Duplicate the session to adjust the
1944 * timeout. */
1945 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1946 if (session_copy == NULL) {
1947 return -1;
1948 }
1949
1950 ssl_session_refresh_time(ssl, session_copy);
1951 session = session_copy;
1952 }
1953
David Benjamin75836432016-06-17 18:48:29 -04001954 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001955 int ok =
1956 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1957 CBB_add_u32(&body, session->timeout) &&
1958 CBB_add_u16_length_prefixed(&body, &ticket) &&
1959 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001960 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001961
1962 SSL_SESSION_free(session_copy);
1963 CBB_cleanup(&cbb);
1964
1965 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001966 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001967 }
1968
David Benjamincb0c29f2016-12-12 17:00:50 -05001969 hs->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001970 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001971}