blob: 33ffa7ea7ad80cbf8252d3dc3d7493354f1c80db [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:
David Benjaminc3c88822016-11-14 10:32:04 +0900235 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (ret <= 0) {
237 goto end;
238 }
Steven Valdez87eab492016-06-27 16:34:59 -0400239 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500240 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500242 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 case SSL3_ST_SW_CERT_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000247 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900248 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 if (ret <= 0) {
250 goto end;
251 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 } else {
253 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500255 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700257
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100258 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900259 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900260 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400261 if (ret <= 0) {
262 goto end;
263 }
264 } else {
265 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100266 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100268 break;
269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 case SSL3_ST_SW_KEY_EXCH_A:
271 case SSL3_ST_SW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500272 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamindf50eec2016-06-07 16:49:42 -0400274 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500275 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
276 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900277 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500278 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500280 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 } else {
282 skip = 1;
283 }
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamincb0c29f2016-12-12 17:00:50 -0500285 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 case SSL3_ST_SW_CERT_REQ_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900289 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900290 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 if (ret <= 0) {
292 goto end;
293 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400294 } else {
295 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500297 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 case SSL3_ST_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900301 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (ret <= 0) {
303 goto end;
304 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500305 hs->next_state = SSL3_ST_SR_CERT_A;
306 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900310 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900311 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 if (ret <= 0) {
313 goto end;
314 }
315 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500316 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 case SSL3_ST_SR_KEY_EXCH_A:
320 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900321 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 if (ret <= 0) {
323 goto end;
324 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500325 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900329 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (ret <= 0) {
331 goto end;
332 }
Adam Langley95c29f32014-06-20 12:00:00 -0700333
David Benjamincb0c29f2016-12-12 17:00:50 -0500334 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700336
David Benjamina41280d2015-11-26 02:16:49 -0500337 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400338 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500339 if (ret <= 0) {
340 goto end;
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamin67739722016-11-17 17:03:59 +0900343 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 ret = -1;
345 goto end;
346 }
David Benjamina41280d2015-11-26 02:16:49 -0500347
David Benjamincb0c29f2016-12-12 17:00:50 -0500348 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900352 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900353 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400354 if (ret <= 0) {
355 goto end;
356 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400358 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500360 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400364 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900365 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400366 if (ret <= 0) {
367 goto end;
368 }
369 } else {
370 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500372 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900376 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 if (ret <= 0) {
378 goto end;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
David Benjamindf50eec2016-06-07 16:49:42 -0400381 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400382 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500383 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500385 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 }
David Benjamindf50eec2016-06-07 16:49:42 -0400387
Steven Valdez87eab492016-06-27 16:34:59 -0400388 /* If this is a full handshake with ChannelID then record the handshake
389 * hashes in |ssl->s3->new_session| in case we need them to verify a
390 * ChannelID signature on a resumption of this session in the future. */
391 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500392 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 if (ret <= 0) {
394 goto end;
395 }
396 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900400 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900401 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400402 if (ret <= 0) {
403 goto end;
404 }
405 } else {
406 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500408 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
David Benjamin352d0a92016-06-28 11:22:02 -0400411 case SSL3_ST_SW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500412 if (!ssl->method->add_change_cipher_spec(ssl) ||
413 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 ret = -1;
415 goto end;
416 }
David Benjamindaf207a2017-01-03 18:37:41 -0500417
418 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 case SSL3_ST_SW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500422 ret = ssl3_send_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 if (ret <= 0) {
424 goto end;
425 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500426 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400427 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500428 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500430 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamin9f1dc822016-06-07 17:03:46 -0400434 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500435 ret = ssl->method->flush_flight(ssl);
436 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400437 goto end;
438 }
439
David Benjamincb0c29f2016-12-12 17:00:50 -0500440 hs->state = hs->next_state;
441 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400442 ssl->method->expect_flight(ssl);
443 }
444 break;
445
Steven Valdez143e8b32016-07-11 13:19:03 -0400446 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900447 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400448 if (ret <= 0) {
449 goto end;
450 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500451 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400452 break;
453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400455 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Steven Valdez87eab492016-06-27 16:34:59 -0400457 /* If we aren't retaining peer certificates then we can discard it
458 * now. */
459 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900460 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800461 X509_free(ssl->s3->new_session->x509_peer);
462 ssl->s3->new_session->x509_peer = NULL;
463 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
464 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400465 }
466
467 SSL_SESSION_free(ssl->s3->established_session);
468 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400469 SSL_SESSION_up_ref(ssl->session);
470 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400471 } else {
472 ssl->s3->established_session = ssl->s3->new_session;
473 ssl->s3->established_session->not_resumable = 0;
474 ssl->s3->new_session = NULL;
475 }
476
David Benjamin78476f62016-11-12 11:20:55 +0900477 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900478 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900479
David Benjamin4e9cc712016-06-01 20:16:03 -0400480 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 ret = 1;
482 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 default:
David Benjamin3570d732015-06-29 00:28:17 -0400485 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 ret = -1;
487 goto end;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjamincb0c29f2016-12-12 17:00:50 -0500490 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
491 int new_state = hs->state;
492 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400493 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500494 hs->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 }
496 skip = 0;
497 }
498
Adam Langley95c29f32014-06-20 12:00:00 -0700499end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400500 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 return ret;
502}
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin731058e2016-12-03 23:15:13 -0500504int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
505 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400506 CBS cipher_suites;
507 CBS_init(&cipher_suites, client_hello->cipher_suites,
508 client_hello->cipher_suites_len);
509
510 while (CBS_len(&cipher_suites) > 0) {
511 uint16_t got_id;
512 if (!CBS_get_u16(&cipher_suites, &got_id)) {
513 return 0;
514 }
515
516 if (got_id == id) {
517 return 1;
518 }
519 }
520
521 return 0;
522}
523
David Benjaminf04c2e92016-12-06 13:35:25 -0500524static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500525 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500526 SSL *const ssl = hs->ssl;
David Benjamin25fe85b2016-08-09 20:00:32 -0400527 uint16_t min_version, max_version;
528 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
529 *out_alert = SSL_AD_PROTOCOL_VERSION;
530 return 0;
531 }
532
David Benjamin2dc02042016-09-19 19:57:37 -0400533 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400534 /* Check supported_versions extension if it is present. */
535 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500536 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
537 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400538 CBS versions;
539 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
540 CBS_len(&supported_versions) != 0 ||
541 CBS_len(&versions) == 0) {
542 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
543 *out_alert = SSL_AD_DECODE_ERROR;
544 return 0;
545 }
546
Brian Smithf85d3232016-10-28 10:34:06 -1000547 /* Choose the newest commonly-supported version advertised by the client.
548 * The client orders the versions according to its preferences, but we're
549 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400550 int found_version = 0;
551 while (CBS_len(&versions) != 0) {
552 uint16_t ext_version;
553 if (!CBS_get_u16(&versions, &ext_version)) {
554 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
555 *out_alert = SSL_AD_DECODE_ERROR;
556 return 0;
557 }
558 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
559 continue;
560 }
561 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000562 ext_version <= max_version &&
563 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400564 version = ext_version;
565 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400566 }
567 }
568
569 if (!found_version) {
570 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400571 }
572 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400573 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
574 * do not use this mechanism. */
575 if (SSL_is_dtls(ssl)) {
576 if (client_hello->version <= DTLS1_2_VERSION) {
577 version = TLS1_2_VERSION;
578 } else if (client_hello->version <= DTLS1_VERSION) {
579 version = TLS1_1_VERSION;
580 } else {
581 goto unsupported_protocol;
582 }
583 } else {
584 if (client_hello->version >= TLS1_2_VERSION) {
585 version = TLS1_2_VERSION;
586 } else if (client_hello->version >= TLS1_1_VERSION) {
587 version = TLS1_1_VERSION;
588 } else if (client_hello->version >= TLS1_VERSION) {
589 version = TLS1_VERSION;
590 } else if (client_hello->version >= SSL3_VERSION) {
591 version = SSL3_VERSION;
592 } else {
593 goto unsupported_protocol;
594 }
David Benjamin2dc02042016-09-19 19:57:37 -0400595 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400596
Steven Valdezfdd10992016-09-15 16:27:05 -0400597 /* Apply our minimum and maximum version. */
598 if (version > max_version) {
599 version = max_version;
600 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400601
Steven Valdezfdd10992016-09-15 16:27:05 -0400602 if (version < min_version) {
603 goto unsupported_protocol;
604 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400605 }
606
607 /* Handle FALLBACK_SCSV. */
608 if (ssl_client_cipher_list_contains_cipher(client_hello,
609 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
610 version < max_version) {
611 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
612 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
613 return 0;
614 }
615
David Benjaminf04c2e92016-12-06 13:35:25 -0500616 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400617 ssl->version = ssl->method->version_to_wire(version);
618 ssl->s3->enc_method = ssl3_get_enc_method(version);
619 assert(ssl->s3->enc_method != NULL);
620
621 /* At this point, the connection's version is known and |ssl->version| is
622 * fixed. Begin enforcing the record-layer version. */
623 ssl->s3->have_version = 1;
624
625 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400626
627unsupported_protocol:
628 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
629 *out_alert = SSL_AD_PROTOCOL_VERSION;
630 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400631}
632
David Benjamin2578b292016-12-03 23:19:55 -0500633static STACK_OF(SSL_CIPHER) *
634 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
635 CBS cipher_suites;
636 CBS_init(&cipher_suites, client_hello->cipher_suites,
637 client_hello->cipher_suites_len);
638
639 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
640 if (sk == NULL) {
641 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
642 goto err;
643 }
644
645 while (CBS_len(&cipher_suites) > 0) {
646 uint16_t cipher_suite;
647
648 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
649 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
650 goto err;
651 }
652
653 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
654 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
655 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
656 goto err;
657 }
658 }
659
660 return sk;
661
662err:
663 sk_SSL_CIPHER_free(sk);
664 return NULL;
665}
666
667/* ssl_get_compatible_server_ciphers determines the key exchange and
668 * authentication cipher suite masks compatible with the server configuration
669 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
670 * exchange mask and |*out_mask_a| to the authentication mask. */
671static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
672 uint32_t *out_mask_k,
673 uint32_t *out_mask_a) {
674 SSL *const ssl = hs->ssl;
675 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
676 *out_mask_k = SSL_kGENERIC;
677 *out_mask_a = SSL_aGENERIC;
678 return;
679 }
680
681 uint32_t mask_k = 0;
682 uint32_t mask_a = 0;
683
684 if (ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl)) {
685 int type = ssl_private_key_type(ssl);
686 if (type == NID_rsaEncryption) {
687 mask_k |= SSL_kRSA;
688 mask_a |= SSL_aRSA;
689 } else if (ssl_is_ecdsa_key_type(type)) {
690 mask_a |= SSL_aECDSA;
691 }
692 }
693
694 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
695 mask_k |= SSL_kDHE;
696 }
697
698 /* Check for a shared group to consider ECDHE ciphers. */
699 uint16_t unused;
700 if (tls1_get_shared_group(hs, &unused)) {
701 mask_k |= SSL_kECDHE;
702 }
703
David Benjamin2578b292016-12-03 23:19:55 -0500704 /* PSK requires a server callback. */
705 if (ssl->psk_server_callback != NULL) {
706 mask_k |= SSL_kPSK;
707 mask_a |= SSL_aPSK;
708 }
709
710 *out_mask_k = mask_k;
711 *out_mask_a = mask_a;
712}
713
714static const SSL_CIPHER *ssl3_choose_cipher(
715 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
716 const struct ssl_cipher_preference_list_st *server_pref) {
717 SSL *const ssl = hs->ssl;
718 const SSL_CIPHER *c, *ret = NULL;
719 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
720 int ok;
721 size_t cipher_index;
722 uint32_t alg_k, alg_a, mask_k, mask_a;
723 /* in_group_flags will either be NULL, or will point to an array of bytes
724 * which indicate equal-preference groups in the |prio| stack. See the
725 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
726 * struct. */
727 const uint8_t *in_group_flags;
728 /* group_min contains the minimal index so far found in a group, or -1 if no
729 * such value exists yet. */
730 int group_min = -1;
731
732 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
733 if (clnt == NULL) {
734 return NULL;
735 }
736
737 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
738 prio = srvr;
739 in_group_flags = server_pref->in_group_flags;
740 allow = clnt;
741 } else {
742 prio = clnt;
743 in_group_flags = NULL;
744 allow = srvr;
745 }
746
747 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
748
749 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
750 c = sk_SSL_CIPHER_value(prio, i);
751
752 ok = 1;
753
754 /* Check the TLS version. */
755 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
756 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
757 ok = 0;
758 }
759
760 alg_k = c->algorithm_mkey;
761 alg_a = c->algorithm_auth;
762
763 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
764
765 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
766 if (in_group_flags != NULL && in_group_flags[i] == 1) {
767 /* This element of |prio| is in a group. Update the minimum index found
768 * so far and continue looking. */
769 if (group_min == -1 || (size_t)group_min > cipher_index) {
770 group_min = cipher_index;
771 }
772 } else {
773 if (group_min != -1 && (size_t)group_min < cipher_index) {
774 cipher_index = group_min;
775 }
776 ret = sk_SSL_CIPHER_value(allow, cipher_index);
777 break;
778 }
779 }
780
781 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
782 /* We are about to leave a group, but we found a match in it, so that's
783 * our answer. */
784 ret = sk_SSL_CIPHER_value(allow, group_min);
785 break;
786 }
787 }
788
789 sk_SSL_CIPHER_free(clnt);
790 return ret;
791}
792
David Benjaminc3c88822016-11-14 10:32:04 +0900793static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
794 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400795 uint8_t al = SSL_AD_INTERNAL_ERROR;
796 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400797 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700798
David Benjamincb0c29f2016-12-12 17:00:50 -0500799 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjamine14ff062016-08-09 16:21:24 -0400800 /* The first time around, read the ClientHello. */
801 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
802 ssl_hash_message);
803 if (msg_ret <= 0) {
804 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700805 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400806
David Benjamincb0c29f2016-12-12 17:00:50 -0500807 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 }
Adam Langley95c29f32014-06-20 12:00:00 -0700809
David Benjamin731058e2016-12-03 23:15:13 -0500810 SSL_CLIENT_HELLO client_hello;
811 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
812 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 goto f_err;
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
David Benjamincb0c29f2016-12-12 17:00:50 -0500818 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjamine14ff062016-08-09 16:21:24 -0400819 /* Run the early callback. */
820 if (ssl->ctx->select_certificate_cb != NULL) {
821 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
822 case 0:
823 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
824 goto err;
825
826 case -1:
827 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400828 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400829 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
830 goto f_err;
831
832 default:
833 /* fallthrough */;
834 }
835 }
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000836 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamine14ff062016-08-09 16:21:24 -0400837 }
838
David Benjamin25fe85b2016-08-09 20:00:32 -0400839 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500840 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500841 if (!negotiate_version(hs, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 goto f_err;
843 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400844
David Benjamin25fe85b2016-08-09 20:00:32 -0400845 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500846 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900847 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400848 return 1;
849 }
850 }
851
David Benjamincb0c29f2016-12-12 17:00:50 -0500852 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400853 /* Load the client random. */
854 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
855 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
856 return -1;
857 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500858 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
859 client_hello.random_len);
David Benjamin25fe85b2016-08-09 20:00:32 -0400860
David Benjamin4eb95cc2016-11-16 17:08:23 +0900861 /* Only null compression is supported. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500862 if (OPENSSL_memchr(client_hello.compression_methods, 0,
863 client_hello.compression_methods_len) == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900864 al = SSL_AD_ILLEGAL_PARAMETER;
865 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
866 goto f_err;
867 }
868
869 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500870 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900871 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
872 goto err;
873 }
874
David Benjamincb0c29f2016-12-12 17:00:50 -0500875 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900876 }
877
David Benjamincb0c29f2016-12-12 17:00:50 -0500878 if (hs->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400879 /* Call |cert_cb| to update server certificates if required. */
880 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500881 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (rv == 0) {
883 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 goto f_err;
886 }
887 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500888 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 goto err;
890 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800892
David Benjamin650aa1c2016-12-20 18:55:16 -0500893 if (!ssl_auto_chain_if_needed(ssl)) {
894 goto err;
895 }
896
David Benjaminf01f42a2016-11-16 19:05:33 +0900897 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
898 * certificate is finalized. */
899 ssl->s3->tmp.new_cipher =
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900900 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjaminf01f42a2016-11-16 19:05:33 +0900901 if (ssl->s3->tmp.new_cipher == NULL) {
902 al = SSL_AD_HANDSHAKE_FAILURE;
903 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
904 goto f_err;
905 }
906
David Benjamincb0c29f2016-12-12 17:00:50 -0500907 hs->state = SSL3_ST_SR_CLNT_HELLO_E;
David Benjamin34202b92016-11-16 19:07:53 +0900908 }
909
David Benjamincb0c29f2016-12-12 17:00:50 -0500910 assert(hs->state == SSL3_ST_SR_CLNT_HELLO_E);
David Benjamin34202b92016-11-16 19:07:53 +0900911
912 /* Determine whether we are doing session resumption. */
913 int tickets_supported = 0, renew_ticket = 0;
914 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
915 &client_hello)) {
916 case ssl_session_success:
917 break;
918 case ssl_session_error:
919 goto err;
920 case ssl_session_retry:
921 ssl->rwstate = SSL_PENDING_SESSION;
922 goto err;
923 }
924
925 if (session != NULL) {
926 if (session->extended_master_secret &&
927 !ssl->s3->tmp.extended_master_secret) {
928 /* A ClientHello without EMS that attempts to resume a session with EMS
929 * is fatal to the connection. */
930 al = SSL_AD_HANDSHAKE_FAILURE;
931 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
932 goto f_err;
933 }
934
935 if (!ssl_session_is_resumable(ssl, session) ||
936 /* If the client offers the EMS extension, but the previous session
937 * didn't use it, then negotiate a new session. */
938 ssl->s3->tmp.extended_master_secret !=
939 session->extended_master_secret) {
940 SSL_SESSION_free(session);
941 session = NULL;
942 }
943 }
944
945 if (session != NULL) {
946 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900947 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900948 ssl->session = session;
949 session = NULL;
950 ssl->s3->session_reused = 1;
951 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900952 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900953 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900954 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900955 goto err;
956 }
957
958 /* Clear the session ID if we want the session to be single-use. */
959 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
960 ssl->s3->new_session->session_id_length = 0;
961 }
962 }
963
964 if (ssl->ctx->dos_protection_cb != NULL &&
965 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
966 /* Connection rejected for DOS reasons. */
967 al = SSL_AD_INTERNAL_ERROR;
968 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
969 goto f_err;
970 }
971
David Benjaminf01f42a2016-11-16 19:05:33 +0900972 if (ssl->session == NULL) {
973 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400974
David Benjamin4eb95cc2016-11-16 17:08:23 +0900975 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900976 if (hs->hostname != NULL) {
977 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +0900978 if (ssl->s3->new_session->tlsext_hostname == NULL) {
979 al = SSL_AD_INTERNAL_ERROR;
980 goto f_err;
981 }
982 }
983
David Benjamin5c1ce292015-05-26 16:59:43 -0400984 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900985 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400986 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500987 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
988 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900989 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400990 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400991 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000992 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900993 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400994 }
Adam Langley37646832016-08-01 16:16:46 -0700995
David Benjaminc3c88822016-11-14 10:32:04 +0900996 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700997 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400998 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -0700999 ssl->s3->new_session->verify_result = X509_V_OK;
1000 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 }
1002
David Benjaminf01f42a2016-11-16 19:05:33 +09001003 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1004 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001005 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001006 goto f_err;
1007 }
1008
David Benjaminf01f42a2016-11-16 19:05:33 +09001009 /* Now that all parameters are known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -05001010 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001011 goto f_err;
1012 }
1013
Steven Valdez2b8415e2016-06-30 13:27:23 -04001014 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001015 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -05001016 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 }
1018
David Benjamin0bd71eb2015-12-01 14:14:40 -05001019 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001020
1021 if (0) {
1022 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001023 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 }
1025
Adam Langley95c29f32014-06-20 12:00:00 -07001026err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001027 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 return ret;
1029}
Adam Langley95c29f32014-06-20 12:00:00 -07001030
David Benjaminc3c88822016-11-14 10:32:04 +09001031static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1032 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001033
1034 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1035 * known attack while we fix ChannelID itself. */
1036 if (ssl->s3->tlsext_channel_id_valid &&
1037 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1038 ssl->s3->tlsext_channel_id_valid = 0;
1039 }
1040
1041 /* If this is a resumption and the original handshake didn't support
1042 * ChannelID then we didn't record the original handshake hashes in the
1043 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001044 if (ssl->session != NULL &&
1045 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001046 ssl->s3->tlsext_channel_id_valid = 0;
1047 }
1048
David Benjamin721e8b72016-08-03 13:13:17 -04001049 struct timeval now;
1050 ssl_get_current_time(ssl, &now);
1051 ssl->s3->server_random[0] = now.tv_sec >> 24;
1052 ssl->s3->server_random[1] = now.tv_sec >> 16;
1053 ssl->s3->server_random[2] = now.tv_sec >> 8;
1054 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001055 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001056 return -1;
1057 }
1058
David Benjamin55108632016-08-11 22:01:18 -04001059 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1060 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001061
Steven Valdez87eab492016-06-27 16:34:59 -04001062 const SSL_SESSION *session = ssl->s3->new_session;
1063 if (ssl->session != NULL) {
1064 session = ssl->session;
1065 }
1066
David Benjamin75836432016-06-17 18:48:29 -04001067 CBB cbb, body, session_id;
1068 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1069 !CBB_add_u16(&body, ssl->version) ||
1070 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1071 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001072 !CBB_add_bytes(&session_id, session->session_id,
1073 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -04001074 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1075 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001076 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001077 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001078 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1079 CBB_cleanup(&cbb);
1080 return -1;
1081 }
1082
David Benjamin16315f72017-01-12 20:02:05 -05001083 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001084}
Adam Langley95c29f32014-06-20 12:00:00 -07001085
David Benjaminc3c88822016-11-14 10:32:04 +09001086static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1087 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001088 if (!ssl_has_certificate(ssl)) {
1089 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin16315f72017-01-12 20:02:05 -05001090 return -1;
David Benjamin32a66d52016-07-13 22:03:11 -04001091 }
1092
David Benjamin75836432016-06-17 18:48:29 -04001093 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin16315f72017-01-12 20:02:05 -05001094 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001095 }
David Benjamin16315f72017-01-12 20:02:05 -05001096 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001097}
1098
David Benjaminc3c88822016-11-14 10:32:04 +09001099static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1100 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001101 CBB cbb, body, ocsp_response;
1102 if (!ssl->method->init_message(ssl, &cbb, &body,
1103 SSL3_MT_CERTIFICATE_STATUS) ||
1104 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1105 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001106 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1107 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001108 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001109 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1110 CBB_cleanup(&cbb);
1111 return -1;
1112 }
1113
David Benjamin16315f72017-01-12 20:02:05 -05001114 return 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001115}
1116
David Benjaminc3c88822016-11-14 10:32:04 +09001117static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1118 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001119 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001120 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001121
David Benjaminc42acee2016-06-17 17:47:58 -04001122 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001123 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001124 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1125 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001126
David Benjaminc42acee2016-06-17 17:47:58 -04001127 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1128 if (!CBB_init(&cbb, 128)) {
1129 goto err;
1130 }
1131
David Benjamin2a0b3912015-12-18 01:01:21 -05001132 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001134 size_t len =
1135 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1136 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1137 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1138 len)) {
1139 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
David Benjamin7061e282015-03-19 11:10:48 -04001143 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001144 /* Determine the group to use. */
1145 DH *params = ssl->cert->dh_tmp;
1146 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1147 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001149 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001150 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001151 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 goto err;
1153 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001154
David Benjamin974c7ba2015-12-19 16:58:04 -05001155 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001156 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001157 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 goto err;
1159 }
David Benjamin9f226a52015-04-26 22:12:32 -04001160
David Benjaminc3c88822016-11-14 10:32:04 +09001161 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001162 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001163 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001164 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001165 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001166 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001167 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001168 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 }
David Benjamin7061e282015-03-19 11:10:48 -04001170 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001171 /* Determine the group to use. */
1172 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001173 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001174 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001175 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 goto err;
1177 }
David Benjamin4882a6c2016-12-11 02:48:12 -05001178 ssl->s3->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001179
David Benjamin4298d772015-12-19 00:18:25 -05001180 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001181 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001182 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001183 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001184 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001185 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001186 goto err;
1187 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001188 } else {
1189 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 }
Adam Langley95c29f32014-06-20 12:00:00 -07001191
David Benjaminc3c88822016-11-14 10:32:04 +09001192 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001193 goto err;
1194 }
David Benjaminc42acee2016-06-17 17:47:58 -04001195 }
1196
1197 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001198 CBB body;
1199 if (!ssl->method->init_message(ssl, &cbb, &body,
1200 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001201 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001202 goto err;
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
David Benjamin2a0b3912015-12-18 01:01:21 -05001205 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001206 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001207 if (!ssl_has_private_key(ssl)) {
1208 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 goto err;
1210 }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
David Benjaminc42acee2016-06-17 17:47:58 -04001212 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001213 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001214 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001215 goto err;
1216 }
David Benjaminc42acee2016-06-17 17:47:58 -04001217 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001218 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001219 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1220 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1221 goto err;
1222 }
David Benjaminc42acee2016-06-17 17:47:58 -04001223 }
1224
1225 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001226 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001227 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001228 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001229 !CBB_reserve(&child, &ptr, max_sig_len)) {
1230 goto err;
1231 }
1232
David Benjamin2a0b3912015-12-18 01:01:21 -05001233 size_t sig_len;
1234 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001235 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001236 CBB transcript;
1237 uint8_t *transcript_data;
1238 size_t transcript_len;
1239 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001240 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1241 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1242 SSL3_RANDOM_SIZE) ||
1243 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1244 SSL3_RANDOM_SIZE) ||
1245 !CBB_add_bytes(&transcript, hs->server_params,
1246 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001247 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1248 CBB_cleanup(&transcript);
1249 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1250 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001251 goto err;
1252 }
1253
Steven Valdezf0451ca2016-06-29 13:16:27 -04001254 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001255 signature_algorithm, transcript_data,
1256 transcript_len);
1257 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001258 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001259 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001260 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001262
1263 switch (sign_result) {
1264 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001265 if (!CBB_did_write(&child, sig_len)) {
1266 goto err;
1267 }
1268 break;
1269 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001270 goto err;
1271 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001272 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001273 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001274 goto err;
1275 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001276 }
1277
David Benjamindaf207a2017-01-03 18:37:41 -05001278 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001279 goto err;
1280 }
David Benjaminc42acee2016-06-17 17:47:58 -04001281
David Benjaminc3c88822016-11-14 10:32:04 +09001282 OPENSSL_free(hs->server_params);
1283 hs->server_params = NULL;
1284 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001285
David Benjamin16315f72017-01-12 20:02:05 -05001286 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001287
Adam Langley95c29f32014-06-20 12:00:00 -07001288err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001289 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 return -1;
1291}
Adam Langley95c29f32014-06-20 12:00:00 -07001292
David Benjamin75836432016-06-17 18:48:29 -04001293static int add_cert_types(SSL *ssl, CBB *cbb) {
1294 /* Get configured signature algorithms. */
1295 int have_rsa_sign = 0;
1296 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001297 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001298 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1299 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001300 switch (sig_algs[i]) {
1301 case SSL_SIGN_RSA_PKCS1_SHA512:
1302 case SSL_SIGN_RSA_PKCS1_SHA384:
1303 case SSL_SIGN_RSA_PKCS1_SHA256:
1304 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001305 have_rsa_sign = 1;
1306 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001307
Steven Valdez02563852016-06-23 13:33:05 -04001308 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1309 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1310 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1311 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001312 have_ecdsa_sign = 1;
1313 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
David Benjamin75836432016-06-17 18:48:29 -04001317 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1318 return 0;
1319 }
1320
1321 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1322 * check for SSL_kECDH or SSL_kECDHE. */
1323 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1324 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1325 return 0;
1326 }
1327
1328 return 1;
1329}
1330
David Benjaminc3c88822016-11-14 10:32:04 +09001331static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1332 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001333 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001334 if (!ssl->method->init_message(ssl, &cbb, &body,
1335 SSL3_MT_CERTIFICATE_REQUEST) ||
1336 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1337 !add_cert_types(ssl, &cert_types)) {
1338 goto err;
1339 }
1340
1341 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001342 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001343 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001344 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001345 goto err;
1346 }
Steven Valdez02563852016-06-23 13:33:05 -04001347
David Benjamin0fc37ef2016-08-17 15:29:46 -04001348 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001349 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1350 goto err;
1351 }
1352 }
David Benjamin75836432016-06-17 18:48:29 -04001353 }
1354
David Benjamin32a66d52016-07-13 22:03:11 -04001355 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001356 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001357 goto err;
1358 }
1359
David Benjamin16315f72017-01-12 20:02:05 -05001360 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001361
Adam Langley95c29f32014-06-20 12:00:00 -07001362err:
David Benjamin75836432016-06-17 18:48:29 -04001363 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1364 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 return -1;
1366}
Adam Langley95c29f32014-06-20 12:00:00 -07001367
David Benjaminc3c88822016-11-14 10:32:04 +09001368static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1369 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001370 CBB cbb, body;
1371 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001372 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001373 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1374 CBB_cleanup(&cbb);
1375 return -1;
1376 }
1377
David Benjamin16315f72017-01-12 20:02:05 -05001378 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001379}
1380
David Benjaminc3c88822016-11-14 10:32:04 +09001381static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1382 SSL *const ssl = hs->ssl;
1383 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001384
David Benjamin09eb6552016-07-08 14:32:11 -07001385 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1386 if (msg_ret <= 0) {
1387 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001388 }
1389
1390 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1391 if (ssl->version == SSL3_VERSION &&
1392 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001393 /* In SSL 3.0, the Certificate message is omitted to signal no
1394 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001395 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001396 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001397 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1398 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001399 }
1400
Adam Langley37646832016-08-01 16:16:46 -07001401 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001402 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001403 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001404 ssl->s3->tmp.reuse_message = 1;
1405 return 1;
1406 }
1407
David Benjamin9f1dc822016-06-07 17:03:46 -04001408 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001409 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1410 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001411 }
1412
David Benjamin5c900c82016-07-13 23:03:26 -04001413 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001414 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001415
1416 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001417 EVP_PKEY_free(hs->peer_pubkey);
1418 hs->peer_pubkey = NULL;
David Benjamin5c900c82016-07-13 23:03:26 -04001419 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001420 ssl->s3->new_session->certs =
Adam Langleyd5157222016-12-12 11:37:43 -08001421 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1422 ssl->retain_only_sha256_of_client_certs
1423 ? ssl->s3->new_session->peer_sha256
1424 : NULL,
Adam Langleyd519bf62016-12-12 11:16:44 -08001425 &certificate_msg, ssl->ctx->pool);
Adam Langley68e71242016-12-12 11:06:16 -08001426 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001427 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001428 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001429 }
1430
Adam Langley68e71242016-12-12 11:06:16 -08001431 if (CBS_len(&certificate_msg) != 0 ||
1432 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001433 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001434 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001435 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001436 }
1437
Adam Langley68e71242016-12-12 11:06:16 -08001438 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001439 /* No client certificate so the handshake buffer may be discarded. */
1440 ssl3_free_handshake_buffer(ssl);
1441
David Benjamine455e512016-08-01 20:11:13 -04001442 /* In SSL 3.0, sending no certificate is signaled by omitting the
1443 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001444 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001445 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001446 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001447 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001448 }
1449
1450 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001451 /* Fail for TLS only if we required a certificate */
1452 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001453 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001454 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001455 }
Adam Langley37646832016-08-01 16:16:46 -07001456
1457 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001458 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001459 ssl->s3->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001460 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001461 }
1462
Adam Langley68e71242016-12-12 11:06:16 -08001463 /* The hash will have been filled in. */
1464 if (ssl->retain_only_sha256_of_client_certs) {
1465 ssl->s3->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001466 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001467
Adam Langley68e71242016-12-12 11:06:16 -08001468 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1469 ssl->s3->new_session->x509_chain)) {
1470 return -1;
1471 }
David Benjamin5c900c82016-07-13 23:03:26 -04001472 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001473}
1474
David Benjaminc3c88822016-11-14 10:32:04 +09001475static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1476 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001477 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001479 uint32_t alg_k;
1480 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 uint8_t *premaster_secret = NULL;
1482 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001483 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001484
David Benjamin4298d772015-12-19 00:18:25 -05001485 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001487
David Benjamincb0c29f2016-12-12 17:00:50 -05001488 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001489 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1490 ssl_hash_message);
1491 if (ret <= 0) {
1492 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001493 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 }
Adam Langley95c29f32014-06-20 12:00:00 -07001495
David Benjamin0d56f882015-12-19 17:05:56 -05001496 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1497 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1498 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 /* If using a PSK key exchange, prepare the pre-shared key. */
1501 if (alg_a & SSL_aPSK) {
1502 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1505 * then this is the only field in the message. */
1506 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1507 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001508 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 al = SSL_AD_DECODE_ERROR;
1510 goto f_err;
1511 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001512
David Benjamin0d56f882015-12-19 17:05:56 -05001513 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001514 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 al = SSL_AD_INTERNAL_ERROR;
1516 goto f_err;
1517 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1520 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001521 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 al = SSL_AD_ILLEGAL_PARAMETER;
1523 goto f_err;
1524 }
David Benjamin35c02302014-07-13 04:14:59 -04001525
Steven Valdez87eab492016-06-27 16:34:59 -04001526 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001528 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 goto f_err;
1530 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001533 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1534 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001536 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 al = SSL_AD_INTERNAL_ERROR;
1538 goto f_err;
1539 } else if (psk_len == 0) {
1540 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001541 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1543 goto f_err;
1544 }
1545 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 /* Depending on the key exchange method, compute |premaster_secret| and
1548 * |premaster_secret_len|. */
1549 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001551 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 decrypt_buf = OPENSSL_malloc(rsa_size);
1553 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001554 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 goto err;
1556 }
David Benjamin35c02302014-07-13 04:14:59 -04001557
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001558 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001559 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001560 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001561 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001562 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001563 al = SSL_AD_HANDSHAKE_FAILURE;
1564 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1565 goto f_err;
1566 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001567 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001568 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001569 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1570 &encrypted_premaster_secret) ||
1571 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001572 al = SSL_AD_DECODE_ERROR;
1573 OPENSSL_PUT_ERROR(SSL,
1574 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1575 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001576 }
1577 } else {
1578 encrypted_premaster_secret = client_key_exchange;
1579 }
1580
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001581 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1582 * timing-sensitive code below. */
1583 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001584 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001585 CBS_data(&encrypted_premaster_secret),
1586 CBS_len(&encrypted_premaster_secret));
1587 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001588 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001589 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001590 decrypt_result =
1591 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001593
1594 switch (decrypt_result) {
1595 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001596 break;
1597 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001598 goto err;
1599 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001600 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001601 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001602 goto err;
1603 }
1604
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001605 if (decrypt_len != rsa_size) {
1606 al = SSL_AD_DECRYPT_ERROR;
1607 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1608 goto f_err;
1609 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001610
David Benjamin3f5b43d2015-12-01 19:31:24 -05001611 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1612 * section 7.4.7.1. */
1613 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1614 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001616 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 goto err;
1618 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001619 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001620 goto err;
1621 }
1622
David Benjamin3f5b43d2015-12-01 19:31:24 -05001623 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1624 * publicly invalid. */
1625 if (decrypt_len < 11 + premaster_secret_len) {
1626 al = SSL_AD_DECRYPT_ERROR;
1627 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1628 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 }
David Benjamin35c02302014-07-13 04:14:59 -04001630
David Benjamin3f5b43d2015-12-01 19:31:24 -05001631 /* Check the padding. See RFC 3447, section 7.2.2. */
1632 size_t padding_len = decrypt_len - premaster_secret_len;
1633 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1634 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001635 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001636 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1637 }
1638 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1639
1640 /* The premaster secret must begin with |client_version|. This too must be
1641 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1642 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001643 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001644 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001645 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001646
1647 /* Select, in constant time, either the decrypted premaster or the random
1648 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001649 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001650 premaster_secret[i] = constant_time_select_8(
1651 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1652 }
1653
1654 OPENSSL_free(decrypt_buf);
1655 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001656 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001657 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001658 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001659 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001660 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001662 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 goto f_err;
1664 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001665
David Benjamin4298d772015-12-19 00:18:25 -05001666 /* Compute the premaster. */
1667 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001668 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001669 &premaster_secret_len, &alert, CBS_data(&peer_key),
1670 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001671 al = alert;
1672 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 }
Adam Langley95c29f32014-06-20 12:00:00 -07001674
David Benjamin4298d772015-12-19 00:18:25 -05001675 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001676 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 } else if (alg_k & SSL_kPSK) {
1678 /* For plain PSK, other_secret is a block of 0s with the same length as the
1679 * pre-shared key. */
1680 premaster_secret_len = psk_len;
1681 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1682 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001683 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 goto err;
1685 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001686 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 } else {
1688 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001689 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 goto f_err;
1691 }
Adam Langley95c29f32014-06-20 12:00:00 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1694 * pre-shared key. */
1695 if (alg_a & SSL_aPSK) {
1696 CBB new_premaster, child;
1697 uint8_t *new_data;
1698 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001699
David Benjamina8653202015-06-28 01:26:10 -04001700 CBB_zero(&new_premaster);
1701 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1702 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1704 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1705 !CBB_add_bytes(&child, psk, psk_len) ||
1706 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001707 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 CBB_cleanup(&new_premaster);
1709 goto err;
1710 }
David Benjamin14c83e72014-07-13 04:54:57 -04001711
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1713 OPENSSL_free(premaster_secret);
1714 premaster_secret = new_data;
1715 premaster_secret_len = new_len;
1716 }
David Benjamin14c83e72014-07-13 04:54:57 -04001717
Adam Langleyfcf25832014-12-18 17:42:32 -08001718 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001719 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1720 ssl, ssl->s3->new_session->master_key, premaster_secret,
1721 premaster_secret_len);
1722 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 goto err;
1724 }
Steven Valdez87eab492016-06-27 16:34:59 -04001725 ssl->s3->new_session->extended_master_secret =
1726 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001727
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1729 OPENSSL_free(premaster_secret);
1730 return 1;
1731
Adam Langley95c29f32014-06-20 12:00:00 -07001732f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001733 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001734err:
David Benjamin4298d772015-12-19 00:18:25 -05001735 if (premaster_secret != NULL) {
1736 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 OPENSSL_free(premaster_secret);
1738 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001739 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 return -1;
1742}
Adam Langley95c29f32014-06-20 12:00:00 -07001743
David Benjaminc3c88822016-11-14 10:32:04 +09001744static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1745 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001746 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 /* Only RSA and ECDSA client certificates are supported, so a
1750 * CertificateVerify is required if and only if there's a client certificate.
1751 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001752 if (hs->peer_pubkey == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001753 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 return 1;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
David Benjamin09eb6552016-07-08 14:32:11 -07001757 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1758 ssl_dont_hash_message);
1759 if (msg_ret <= 0) {
1760 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 }
David Benjaminef865502014-08-24 02:48:34 -04001762
David Benjamin09eb6552016-07-08 14:32:11 -07001763 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001766 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001767 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001768 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001769 al = SSL_AD_DECODE_ERROR;
1770 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1771 goto f_err;
1772 }
David Benjamin887c3002016-07-08 16:15:32 -07001773 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001774 goto f_err;
1775 }
David Benjaminf1050fd2016-12-13 20:05:36 -05001776 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001777 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001778 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001779 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001780 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001781 } else {
1782 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1783 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1784 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 }
David Benjamin854dd652014-08-26 00:32:30 -04001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 /* Parse and verify the signature. */
1788 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1789 CBS_len(&certificate_verify) != 0) {
1790 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001791 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 goto f_err;
1793 }
Adam Langley95c29f32014-06-20 12:00:00 -07001794
Steven Valdez2b8415e2016-06-30 13:27:23 -04001795 int sig_ok;
1796 /* The SSL3 construction for CertificateVerify does not decompose into a
1797 * single final digest and signature, and must be special-cased. */
1798 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001799 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001800 uint8_t digest[EVP_MAX_MD_SIZE];
1801 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001802 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001803 signature_algorithm)) {
1804 goto err;
1805 }
1806
Adam Langleyd5157222016-12-12 11:37:43 -08001807 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001808 sig_ok = pctx != NULL &&
1809 EVP_PKEY_verify_init(pctx) &&
1810 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1811 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1812 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001813 EVP_PKEY_CTX_free(pctx);
1814 } else {
1815 sig_ok = ssl_public_key_verify(
1816 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001817 hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001818 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001820
David Benjaminbf82aed2016-03-01 22:57:40 -05001821#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1822 sig_ok = 1;
1823 ERR_clear_error();
1824#endif
1825 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001827 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 goto f_err;
1829 }
1830
Steven Valdez2b8415e2016-06-30 13:27:23 -04001831 /* The handshake buffer is no longer necessary, and we may hash the current
1832 * message.*/
1833 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001834 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001835 goto err;
1836 }
1837
Adam Langleyd5157222016-12-12 11:37:43 -08001838 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001839
Adam Langleyd5157222016-12-12 11:37:43 -08001840f_err:
1841 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001842err:
Adam Langleyd5157222016-12-12 11:37:43 -08001843 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001844}
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langley95c29f32014-06-20 12:00:00 -07001846/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1847 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001848static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1849 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001850 int ret =
1851 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1852 if (ret <= 0) {
1853 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 }
Adam Langley95c29f32014-06-20 12:00:00 -07001855
David Benjamin09eb6552016-07-08 14:32:11 -07001856 CBS next_protocol, selected_protocol, padding;
1857 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1859 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001860 CBS_len(&next_protocol) != 0) {
1861 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1862 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1863 return 0;
1864 }
1865
1866 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001867 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 return 0;
1869 }
Adam Langley95c29f32014-06-20 12:00:00 -07001870
Adam Langleyfcf25832014-12-18 17:42:32 -08001871 return 1;
1872}
Adam Langley95c29f32014-06-20 12:00:00 -07001873
Adam Langley1258b6a2014-06-20 12:00:00 -07001874/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001875static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1876 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001877 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1878 ssl_dont_hash_message);
1879 if (msg_ret <= 0) {
1880 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001882
Nick Harper60a85cb2016-09-23 16:25:11 -07001883 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001884 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 return -1;
1886 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001887 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001888}
David Benjamin9f1dc822016-06-07 17:03:46 -04001889
David Benjaminc3c88822016-11-14 10:32:04 +09001890static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1891 SSL *const ssl = hs->ssl;
David Benjamin123db572016-11-03 16:59:25 -04001892 const SSL_SESSION *session;
1893 SSL_SESSION *session_copy = NULL;
1894 if (ssl->session == NULL) {
1895 /* Fix the timeout to measure from the ticket issuance time. */
1896 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1897 session = ssl->s3->new_session;
1898 } else {
1899 /* We are renewing an existing session. Duplicate the session to adjust the
1900 * timeout. */
1901 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1902 if (session_copy == NULL) {
1903 return -1;
1904 }
1905
1906 ssl_session_refresh_time(ssl, session_copy);
1907 session = session_copy;
1908 }
1909
David Benjamin75836432016-06-17 18:48:29 -04001910 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001911 int ok =
1912 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1913 CBB_add_u32(&body, session->timeout) &&
1914 CBB_add_u16_length_prefixed(&body, &ticket) &&
1915 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001916 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001917
1918 SSL_SESSION_free(session_copy);
1919 CBB_cleanup(&cbb);
1920
1921 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001922 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001923 }
1924
David Benjamin16315f72017-01-12 20:02:05 -05001925 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001926}