blob: c352dd9596c1f92de696a514908de69ed410dd5c [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000149#include <openssl/ssl.h>
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <string.h>
153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
156#include <openssl/bytestring.h>
157#include <openssl/cipher.h>
158#include <openssl/dh.h>
159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700161#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400166#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168#include <openssl/x509.h>
169
Adam Langleye9ada862015-05-11 17:20:37 -0700170#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172
173
Robert Sloan5d625782017-02-13 09:55:39 -0800174static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
176static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
David Benjamin1b249672016-12-06 18:25:50 -0500177static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
182static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
183static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
184static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
185static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
186static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
187static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
188static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamin6e899c72016-06-09 18:02:18 -0400189
Robert Sloan4d1ac502017-02-06 08:36:14 -0800190static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
191 CRYPTO_STATIC_MUTEX_INIT;
192static uint64_t g_v2clienthello_count = 0;
193
194uint64_t SSL_get_v2clienthello_count(void) {
195 CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
196 uint64_t ret = g_v2clienthello_count;
197 CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
198 return ret;
199}
200
David Benjamin1b249672016-12-06 18:25:50 -0500201int ssl3_accept(SSL_HANDSHAKE *hs) {
202 SSL *const ssl = hs->ssl;
Adam Langleye9ada862015-05-11 17:20:37 -0700203 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400205 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800206
Adam Langley4139edb2016-01-13 15:00:54 -0800207 assert(ssl->handshake_func == ssl3_accept);
208 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209
210 for (;;) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500211 state = hs->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212
Steven Valdeze7531f02016-12-14 13:29:57 -0500213 switch (hs->state) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400214 case SSL_ST_INIT:
Steven Valdeze7531f02016-12-14 13:29:57 -0500215 hs->state = SSL_ST_ACCEPT;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400216 skip = 1;
217 break;
218
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 case SSL_ST_ACCEPT:
David Benjamin6e899c72016-06-09 18:02:18 -0400220 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Steven Valdeze7531f02016-12-14 13:29:57 -0500221 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222 break;
223
224 case SSL3_ST_SR_CLNT_HELLO_A:
Robert Sloan5d625782017-02-13 09:55:39 -0800225 ret = ssl->method->ssl_get_message(ssl);
226 if (ret <= 0) {
227 goto end;
David Benjaminc895d6b2016-08-11 13:26:41 -0400228 }
Robert Sloan5d625782017-02-13 09:55:39 -0800229 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
230 break;
231
232 case SSL3_ST_SR_CLNT_HELLO_B:
233 ret = ssl3_process_client_hello(hs);
234 if (ret <= 0) {
235 goto end;
236 }
237 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
238 break;
239
240 case SSL3_ST_SR_CLNT_HELLO_C:
241 ret = ssl3_select_certificate(hs);
242 if (ret <= 0) {
243 goto end;
244 }
245 if (hs->state != SSL_ST_TLS13) {
246 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
247 }
248 break;
249
250 case SSL3_ST_SR_CLNT_HELLO_D:
251 ret = ssl3_select_parameters(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252 if (ret <= 0) {
253 goto end;
254 }
David Benjamin6e899c72016-06-09 18:02:18 -0400255 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500256 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257 break;
258
259 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500260 ret = ssl3_send_server_hello(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800261 if (ret <= 0) {
262 goto end;
263 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400264 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500265 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500267 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800268 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269 break;
270
271 case SSL3_ST_SW_CERT_A:
David Benjamind316cba2016-06-02 16:17:39 -0400272 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -0500273 ret = ssl3_send_server_certificate(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 if (ret <= 0) {
275 goto end;
276 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277 } else {
278 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500280 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800281 break;
282
Kenny Rootb8494592015-09-25 02:29:14 +0000283 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamin1b249672016-12-06 18:25:50 -0500284 if (hs->certificate_status_expected) {
285 ret = ssl3_send_certificate_status(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400286 if (ret <= 0) {
287 goto end;
288 }
289 } else {
290 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000291 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500292 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000293 break;
294
Adam Langleyd9e397b2015-01-22 14:27:53 -0800295 case SSL3_ST_SW_KEY_EXCH_A:
296 case SSL3_ST_SW_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800297 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800298
David Benjamin6e899c72016-06-09 18:02:18 -0400299 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
Adam Langley4139edb2016-01-13 15:00:54 -0800300 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
301 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjamin1b249672016-12-06 18:25:50 -0500302 ret = ssl3_send_server_key_exchange(hs);
Adam Langleye9ada862015-05-11 17:20:37 -0700303 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800304 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700305 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306 } else {
307 skip = 1;
308 }
309
Steven Valdeze7531f02016-12-14 13:29:57 -0500310 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800311 break;
312
313 case SSL3_ST_SW_CERT_REQ_A:
David Benjamin1b249672016-12-06 18:25:50 -0500314 if (hs->cert_request) {
315 ret = ssl3_send_certificate_request(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800316 if (ret <= 0) {
317 goto end;
318 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700319 } else {
320 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500322 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323 break;
324
325 case SSL3_ST_SW_SRVR_DONE_A:
David Benjamin1b249672016-12-06 18:25:50 -0500326 ret = ssl3_send_server_hello_done(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327 if (ret <= 0) {
328 goto end;
329 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500330 hs->next_state = SSL3_ST_SR_CERT_A;
331 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 break;
333
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 case SSL3_ST_SR_CERT_A:
David Benjamin1b249672016-12-06 18:25:50 -0500335 if (hs->cert_request) {
336 ret = ssl3_get_client_certificate(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800337 if (ret <= 0) {
338 goto end;
339 }
340 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500341 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 break;
343
344 case SSL3_ST_SR_KEY_EXCH_A:
345 case SSL3_ST_SR_KEY_EXCH_B:
David Benjamin1b249672016-12-06 18:25:50 -0500346 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800347 if (ret <= 0) {
348 goto end;
349 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500350 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800351 break;
352
353 case SSL3_ST_SR_CERT_VRFY_A:
David Benjamin1b249672016-12-06 18:25:50 -0500354 ret = ssl3_get_cert_verify(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355 if (ret <= 0) {
356 goto end;
357 }
358
Steven Valdeze7531f02016-12-14 13:29:57 -0500359 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800360 break;
361
Adam Langley4139edb2016-01-13 15:00:54 -0800362 case SSL3_ST_SR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400363 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800364 if (ret <= 0) {
365 goto end;
366 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367
David Benjamin1b249672016-12-06 18:25:50 -0500368 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 ret = -1;
370 goto end;
371 }
Adam Langley4139edb2016-01-13 15:00:54 -0800372
Steven Valdeze7531f02016-12-14 13:29:57 -0500373 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800375
376 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500377 if (hs->next_proto_neg_seen) {
378 ret = ssl3_get_next_proto(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400379 if (ret <= 0) {
380 goto end;
381 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400383 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800384 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500385 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800386 break;
387
388 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400389 if (ssl->s3->tlsext_channel_id_valid) {
David Benjamin1b249672016-12-06 18:25:50 -0500390 ret = ssl3_get_channel_id(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400391 if (ret <= 0) {
392 goto end;
393 }
394 } else {
395 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800396 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500397 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398 break;
399
400 case SSL3_ST_SR_FINISHED_A:
David Benjamin1b249672016-12-06 18:25:50 -0500401 ret = ssl3_get_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402 if (ret <= 0) {
403 goto end;
404 }
405
David Benjamin6e899c72016-06-09 18:02:18 -0400406 ssl->method->received_flight(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400407 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500408 hs->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500410 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800411 }
David Benjamin6e899c72016-06-09 18:02:18 -0400412
David Benjaminc895d6b2016-08-11 13:26:41 -0400413 /* If this is a full handshake with ChannelID then record the handshake
414 * hashes in |ssl->s3->new_session| in case we need them to verify a
415 * ChannelID signature on a resumption of this session in the future. */
416 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Robert Sloan5d625782017-02-13 09:55:39 -0800417 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800418 if (ret <= 0) {
419 goto end;
420 }
421 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800422 break;
423
424 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamin1b249672016-12-06 18:25:50 -0500425 if (hs->ticket_expected) {
426 ret = ssl3_send_new_session_ticket(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400427 if (ret <= 0) {
428 goto end;
429 }
430 } else {
431 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800432 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500433 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800434 break;
435
David Benjaminc895d6b2016-08-11 13:26:41 -0400436 case SSL3_ST_SW_CHANGE:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800437 if (!ssl->method->add_change_cipher_spec(ssl) ||
438 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800439 ret = -1;
440 goto end;
441 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800442
443 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800444 break;
445
446 case SSL3_ST_SW_FINISHED_A:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800447 ret = ssl3_send_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800448 if (ret <= 0) {
449 goto end;
450 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500451 hs->state = SSL3_ST_SW_FLUSH;
David Benjaminc895d6b2016-08-11 13:26:41 -0400452 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500453 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800454 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500455 hs->next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800456 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800457 break;
458
David Benjamin6e899c72016-06-09 18:02:18 -0400459 case SSL3_ST_SW_FLUSH:
David Benjaminf31229b2017-01-25 14:08:15 -0500460 ret = ssl->method->flush_flight(ssl);
461 if (ret <= 0) {
David Benjamin6e899c72016-06-09 18:02:18 -0400462 goto end;
463 }
464
Steven Valdeze7531f02016-12-14 13:29:57 -0500465 hs->state = hs->next_state;
466 if (hs->state != SSL_ST_OK) {
David Benjamin6e899c72016-06-09 18:02:18 -0400467 ssl->method->expect_flight(ssl);
468 }
469 break;
470
David Benjaminc895d6b2016-08-11 13:26:41 -0400471 case SSL_ST_TLS13:
David Benjamin1b249672016-12-06 18:25:50 -0500472 ret = tls13_handshake(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400473 if (ret <= 0) {
474 goto end;
475 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500476 hs->state = SSL_ST_OK;
David Benjaminc895d6b2016-08-11 13:26:41 -0400477 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800478
David Benjaminc895d6b2016-08-11 13:26:41 -0400479 case SSL_ST_OK:
David Benjaminc895d6b2016-08-11 13:26:41 -0400480 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
481
482 /* If we aren't retaining peer certificates then we can discard it
483 * now. */
484 if (ssl->s3->new_session != NULL &&
Steven Valdez909b19f2016-11-21 15:35:44 -0500485 ssl->retain_only_sha256_of_client_certs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800486 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs,
487 CRYPTO_BUFFER_free);
488 ssl->s3->new_session->certs = NULL;
489 ssl->ctx->x509_method->session_clear(ssl->s3->new_session);
David Benjaminc895d6b2016-08-11 13:26:41 -0400490 }
491
492 SSL_SESSION_free(ssl->s3->established_session);
493 if (ssl->session != NULL) {
494 SSL_SESSION_up_ref(ssl->session);
495 ssl->s3->established_session = ssl->session;
496 } else {
497 ssl->s3->established_session = ssl->s3->new_session;
498 ssl->s3->established_session->not_resumable = 0;
499 ssl->s3->new_session = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400500 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800501
Robert Sloan4d1ac502017-02-06 08:36:14 -0800502 if (hs->v2_clienthello) {
503 CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
504 g_v2clienthello_count++;
505 CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
506 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800507
Steven Valdez909b19f2016-11-21 15:35:44 -0500508 ssl->s3->initial_handshake_complete = 1;
David Benjamin1b249672016-12-06 18:25:50 -0500509 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin6e899c72016-06-09 18:02:18 -0400510
David Benjamin6e899c72016-06-09 18:02:18 -0400511 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800512 ret = 1;
513 goto end;
514
515 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000516 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517 ret = -1;
518 goto end;
519 }
520
Steven Valdeze7531f02016-12-14 13:29:57 -0500521 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
522 int new_state = hs->state;
523 hs->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400524 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Steven Valdeze7531f02016-12-14 13:29:57 -0500525 hs->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800526 }
527 skip = 0;
528 }
529
530end:
David Benjamin6e899c72016-06-09 18:02:18 -0400531 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800532 return ret;
533}
534
David Benjamin1b249672016-12-06 18:25:50 -0500535int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
536 uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400537 CBS cipher_suites;
538 CBS_init(&cipher_suites, client_hello->cipher_suites,
539 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800540
David Benjaminc895d6b2016-08-11 13:26:41 -0400541 while (CBS_len(&cipher_suites) > 0) {
542 uint16_t got_id;
543 if (!CBS_get_u16(&cipher_suites, &got_id)) {
544 return 0;
545 }
546
547 if (got_id == id) {
548 return 1;
549 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800550 }
551
David Benjaminc895d6b2016-08-11 13:26:41 -0400552 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800553}
554
David Benjamin1b249672016-12-06 18:25:50 -0500555static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
556 const SSL_CLIENT_HELLO *client_hello) {
557 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800558 assert(!ssl->s3->have_version);
David Benjaminc895d6b2016-08-11 13:26:41 -0400559 uint16_t min_version, max_version;
560 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
561 *out_alert = SSL_AD_PROTOCOL_VERSION;
562 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800563 }
564
David Benjamin7c0d06c2016-08-11 13:26:41 -0400565 uint16_t version = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400566 /* Check supported_versions extension if it is present. */
567 CBS supported_versions;
David Benjamin1b249672016-12-06 18:25:50 -0500568 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
569 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400570 CBS versions;
571 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
572 CBS_len(&supported_versions) != 0 ||
573 CBS_len(&versions) == 0) {
574 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
575 *out_alert = SSL_AD_DECODE_ERROR;
576 return 0;
577 }
578
Steven Valdez909b19f2016-11-21 15:35:44 -0500579 /* Choose the newest commonly-supported version advertised by the client.
580 * The client orders the versions according to its preferences, but we're
581 * not required to honor the client's preferences. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400582 int found_version = 0;
583 while (CBS_len(&versions) != 0) {
584 uint16_t ext_version;
585 if (!CBS_get_u16(&versions, &ext_version)) {
586 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
587 *out_alert = SSL_AD_DECODE_ERROR;
588 return 0;
589 }
590 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
591 continue;
592 }
593 if (min_version <= ext_version &&
Steven Valdez909b19f2016-11-21 15:35:44 -0500594 ext_version <= max_version &&
595 (!found_version || version < ext_version)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400596 version = ext_version;
597 found_version = 1;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400598 }
599 }
600
601 if (!found_version) {
602 goto unsupported_protocol;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400603 }
604 } else {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400605 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
606 * do not use this mechanism. */
607 if (SSL_is_dtls(ssl)) {
608 if (client_hello->version <= DTLS1_2_VERSION) {
609 version = TLS1_2_VERSION;
610 } else if (client_hello->version <= DTLS1_VERSION) {
611 version = TLS1_1_VERSION;
612 } else {
613 goto unsupported_protocol;
614 }
615 } else {
616 if (client_hello->version >= TLS1_2_VERSION) {
617 version = TLS1_2_VERSION;
618 } else if (client_hello->version >= TLS1_1_VERSION) {
619 version = TLS1_1_VERSION;
620 } else if (client_hello->version >= TLS1_VERSION) {
621 version = TLS1_VERSION;
622 } else if (client_hello->version >= SSL3_VERSION) {
623 version = SSL3_VERSION;
624 } else {
625 goto unsupported_protocol;
626 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400627 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800628
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400629 /* Apply our minimum and maximum version. */
630 if (version > max_version) {
631 version = max_version;
632 }
David Benjamin6e899c72016-06-09 18:02:18 -0400633
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400634 if (version < min_version) {
635 goto unsupported_protocol;
636 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800637 }
638
David Benjaminc895d6b2016-08-11 13:26:41 -0400639 /* Handle FALLBACK_SCSV. */
640 if (ssl_client_cipher_list_contains_cipher(client_hello,
641 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
642 version < max_version) {
643 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
644 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
645 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800646 }
647
David Benjamin1b249672016-12-06 18:25:50 -0500648 hs->client_version = client_hello->version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400649 ssl->version = ssl->method->version_to_wire(version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800650
David Benjaminc895d6b2016-08-11 13:26:41 -0400651 /* At this point, the connection's version is known and |ssl->version| is
652 * fixed. Begin enforcing the record-layer version. */
653 ssl->s3->have_version = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800654
655 return 1;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400656
657unsupported_protocol:
658 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
659 *out_alert = SSL_AD_PROTOCOL_VERSION;
660 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800661}
662
David Benjamin1b249672016-12-06 18:25:50 -0500663static STACK_OF(SSL_CIPHER) *
664 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
665 CBS cipher_suites;
666 CBS_init(&cipher_suites, client_hello->cipher_suites,
667 client_hello->cipher_suites_len);
668
669 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
670 if (sk == NULL) {
671 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
672 goto err;
673 }
674
675 while (CBS_len(&cipher_suites) > 0) {
676 uint16_t cipher_suite;
677
678 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
679 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
680 goto err;
681 }
682
683 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
684 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
685 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
686 goto err;
687 }
688 }
689
690 return sk;
691
692err:
693 sk_SSL_CIPHER_free(sk);
694 return NULL;
695}
696
697/* ssl_get_compatible_server_ciphers determines the key exchange and
698 * authentication cipher suite masks compatible with the server configuration
699 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
700 * exchange mask and |*out_mask_a| to the authentication mask. */
701static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
702 uint32_t *out_mask_k,
703 uint32_t *out_mask_a) {
704 SSL *const ssl = hs->ssl;
705 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
706 *out_mask_k = SSL_kGENERIC;
707 *out_mask_a = SSL_aGENERIC;
708 return;
709 }
710
711 uint32_t mask_k = 0;
712 uint32_t mask_a = 0;
713
Robert Sloan4d1ac502017-02-06 08:36:14 -0800714 if (ssl_has_certificate(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -0500715 int type = ssl_private_key_type(ssl);
716 if (type == NID_rsaEncryption) {
717 mask_k |= SSL_kRSA;
718 mask_a |= SSL_aRSA;
719 } else if (ssl_is_ecdsa_key_type(type)) {
720 mask_a |= SSL_aECDSA;
721 }
722 }
723
724 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
725 mask_k |= SSL_kDHE;
726 }
727
728 /* Check for a shared group to consider ECDHE ciphers. */
729 uint16_t unused;
730 if (tls1_get_shared_group(hs, &unused)) {
731 mask_k |= SSL_kECDHE;
732 }
733
David Benjamin1b249672016-12-06 18:25:50 -0500734 /* PSK requires a server callback. */
735 if (ssl->psk_server_callback != NULL) {
736 mask_k |= SSL_kPSK;
737 mask_a |= SSL_aPSK;
738 }
739
740 *out_mask_k = mask_k;
741 *out_mask_a = mask_a;
742}
743
744static const SSL_CIPHER *ssl3_choose_cipher(
745 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
746 const struct ssl_cipher_preference_list_st *server_pref) {
747 SSL *const ssl = hs->ssl;
748 const SSL_CIPHER *c, *ret = NULL;
749 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
750 int ok;
751 size_t cipher_index;
752 uint32_t alg_k, alg_a, mask_k, mask_a;
753 /* in_group_flags will either be NULL, or will point to an array of bytes
754 * which indicate equal-preference groups in the |prio| stack. See the
755 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
756 * struct. */
757 const uint8_t *in_group_flags;
758 /* group_min contains the minimal index so far found in a group, or -1 if no
759 * such value exists yet. */
760 int group_min = -1;
761
762 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
763 if (clnt == NULL) {
764 return NULL;
765 }
766
767 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
768 prio = srvr;
769 in_group_flags = server_pref->in_group_flags;
770 allow = clnt;
771 } else {
772 prio = clnt;
773 in_group_flags = NULL;
774 allow = srvr;
775 }
776
777 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
778
779 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
780 c = sk_SSL_CIPHER_value(prio, i);
781
782 ok = 1;
783
784 /* Check the TLS version. */
785 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
786 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
787 ok = 0;
788 }
789
790 alg_k = c->algorithm_mkey;
791 alg_a = c->algorithm_auth;
792
793 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
794
795 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
796 if (in_group_flags != NULL && in_group_flags[i] == 1) {
797 /* This element of |prio| is in a group. Update the minimum index found
798 * so far and continue looking. */
799 if (group_min == -1 || (size_t)group_min > cipher_index) {
800 group_min = cipher_index;
801 }
802 } else {
803 if (group_min != -1 && (size_t)group_min < cipher_index) {
804 cipher_index = group_min;
805 }
806 ret = sk_SSL_CIPHER_value(allow, cipher_index);
807 break;
808 }
809 }
810
811 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
812 /* We are about to leave a group, but we found a match in it, so that's
813 * our answer. */
814 ret = sk_SSL_CIPHER_value(allow, group_min);
815 break;
816 }
817 }
818
819 sk_SSL_CIPHER_free(clnt);
820 return ret;
821}
822
Robert Sloan5d625782017-02-13 09:55:39 -0800823static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500824 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -0800825 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
826 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400827 }
828
David Benjamin1b249672016-12-06 18:25:50 -0500829 SSL_CLIENT_HELLO client_hello;
830 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
831 ssl->init_num)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400832 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan5d625782017-02-13 09:55:39 -0800833 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
834 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400835 }
836
Robert Sloan5d625782017-02-13 09:55:39 -0800837 /* Run the early callback. */
838 if (ssl->ctx->select_certificate_cb != NULL) {
839 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
840 case 0:
841 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
842 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400843
Robert Sloan5d625782017-02-13 09:55:39 -0800844 case -1:
845 /* Connection rejected. */
846 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
847 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
848 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400849
Robert Sloan5d625782017-02-13 09:55:39 -0800850 default:
851 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400852 }
Robert Sloan5d625782017-02-13 09:55:39 -0800853 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800854
Robert Sloan5d625782017-02-13 09:55:39 -0800855 uint8_t alert = SSL_AD_DECODE_ERROR;
856 if (!negotiate_version(hs, &alert, &client_hello)) {
857 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
858 return -1;
859 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800860
Robert Sloan5d625782017-02-13 09:55:39 -0800861 /* Load the client random. */
862 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
863 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
864 return -1;
865 }
866 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
867 client_hello.random_len);
868
869 /* Only null compression is supported. TLS 1.3 further requires the peer
870 * advertise no other compression. */
871 if (OPENSSL_memchr(client_hello.compression_methods, 0,
872 client_hello.compression_methods_len) == NULL ||
873 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
874 client_hello.compression_methods_len != 1)) {
875 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
876 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
877 return -1;
878 }
879
880 /* TLS extensions. */
881 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
882 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
883 return -1;
884 }
885
886 return 1;
887}
888
889static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
890 SSL *const ssl = hs->ssl;
891 /* Call |cert_cb| to update server certificates if required. */
892 if (ssl->cert->cert_cb != NULL) {
893 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
894 if (rv == 0) {
895 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
896 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400897 return -1;
898 }
Robert Sloan5d625782017-02-13 09:55:39 -0800899 if (rv < 0) {
900 ssl->rwstate = SSL_X509_LOOKUP;
901 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400902 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400903 }
904
Robert Sloan5d625782017-02-13 09:55:39 -0800905 if (!ssl_auto_chain_if_needed(ssl)) {
906 return -1;
Steven Valdez909b19f2016-11-21 15:35:44 -0500907 }
908
Robert Sloan5d625782017-02-13 09:55:39 -0800909 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
910 /* Jump to the TLS 1.3 state machine. */
911 hs->state = SSL_ST_TLS13;
912 hs->do_tls13_handshake = tls13_server_handshake;
913 return 1;
914 }
915
916 SSL_CLIENT_HELLO client_hello;
917 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
918 ssl->init_num)) {
919 return -1;
920 }
921
922 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
923 * certificate is finalized. */
924 ssl->s3->tmp.new_cipher =
925 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
926 if (ssl->s3->tmp.new_cipher == NULL) {
927 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
928 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
929 return -1;
930 }
931
932 return 1;
933}
934
935static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
936 SSL *const ssl = hs->ssl;
937 uint8_t al = SSL_AD_INTERNAL_ERROR;
938 int ret = -1;
939 SSL_SESSION *session = NULL;
940
941 SSL_CLIENT_HELLO client_hello;
942 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
943 ssl->init_num)) {
944 return -1;
945 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500946
947 /* Determine whether we are doing session resumption. */
948 int tickets_supported = 0, renew_ticket = 0;
949 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
950 &client_hello)) {
951 case ssl_session_success:
952 break;
953 case ssl_session_error:
954 goto err;
955 case ssl_session_retry:
956 ssl->rwstate = SSL_PENDING_SESSION;
957 goto err;
958 }
959
960 if (session != NULL) {
961 if (session->extended_master_secret &&
962 !ssl->s3->tmp.extended_master_secret) {
963 /* A ClientHello without EMS that attempts to resume a session with EMS
964 * is fatal to the connection. */
965 al = SSL_AD_HANDSHAKE_FAILURE;
966 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
967 goto f_err;
968 }
969
970 if (!ssl_session_is_resumable(ssl, session) ||
971 /* If the client offers the EMS extension, but the previous session
972 * didn't use it, then negotiate a new session. */
973 ssl->s3->tmp.extended_master_secret !=
974 session->extended_master_secret) {
975 SSL_SESSION_free(session);
976 session = NULL;
977 }
978 }
979
980 if (session != NULL) {
981 /* Use the old session. */
David Benjamin1b249672016-12-06 18:25:50 -0500982 hs->ticket_expected = renew_ticket;
Steven Valdez909b19f2016-11-21 15:35:44 -0500983 ssl->session = session;
984 session = NULL;
985 ssl->s3->session_reused = 1;
986 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500987 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500988 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500989 if (!ssl_get_new_session(hs, 1 /* server */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500990 goto err;
991 }
992
993 /* Clear the session ID if we want the session to be single-use. */
994 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
995 ssl->s3->new_session->session_id_length = 0;
996 }
997 }
998
999 if (ssl->ctx->dos_protection_cb != NULL &&
1000 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
1001 /* Connection rejected for DOS reasons. */
1002 al = SSL_AD_INTERNAL_ERROR;
1003 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
1004 goto f_err;
1005 }
1006
1007 if (ssl->session == NULL) {
1008 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
1009
1010 /* On new sessions, stash the SNI value in the session. */
David Benjamin1b249672016-12-06 18:25:50 -05001011 if (hs->hostname != NULL) {
Robert Sloan47f43ed2017-02-06 14:55:15 -08001012 OPENSSL_free(ssl->s3->new_session->tlsext_hostname);
David Benjamin1b249672016-12-06 18:25:50 -05001013 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
Steven Valdez909b19f2016-11-21 15:35:44 -05001014 if (ssl->s3->new_session->tlsext_hostname == NULL) {
1015 al = SSL_AD_INTERNAL_ERROR;
1016 goto f_err;
1017 }
1018 }
Adam Langleyf4e42722015-06-04 17:45:09 -07001019
1020 /* Determine whether to request a client certificate. */
David Benjamin1b249672016-12-06 18:25:50 -05001021 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -07001022 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -08001023 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1024 ssl->s3->tlsext_channel_id_valid) {
David Benjamin1b249672016-12-06 18:25:50 -05001025 hs->cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001026 }
David Benjamind316cba2016-06-02 16:17:39 -04001027 /* CertificateRequest may only be sent in certificate-based ciphers. */
1028 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -05001029 hs->cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001030 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001031
David Benjamin1b249672016-12-06 18:25:50 -05001032 if (!hs->cert_request) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001033 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
1034 * classed by them as a bug, but it's assumed by at least NGINX. */
1035 ssl->s3->new_session->verify_result = X509_V_OK;
1036 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001037 }
1038
Steven Valdez909b19f2016-11-21 15:35:44 -05001039 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1040 * deferred. Complete it now. */
David Benjamin1b249672016-12-06 18:25:50 -05001041 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001042 goto f_err;
1043 }
1044
Robert Sloan4d1ac502017-02-06 08:36:14 -08001045 /* Now that all parameters are known, initialize the handshake hash and hash
1046 * the ClientHello. */
Robert Sloan5d625782017-02-13 09:55:39 -08001047 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
1048 ssl->s3->tmp.new_cipher->algorithm_prf) ||
1049 !ssl_hash_current_message(hs)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001050 goto f_err;
1051 }
1052
David Benjaminc895d6b2016-08-11 13:26:41 -04001053 /* Release the handshake buffer if client authentication isn't required. */
David Benjamin1b249672016-12-06 18:25:50 -05001054 if (!hs->cert_request) {
Robert Sloan5d625782017-02-13 09:55:39 -08001055 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001056 }
1057
Adam Langley4139edb2016-01-13 15:00:54 -08001058 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001059
1060 if (0) {
1061 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001062 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001063 }
1064
1065err:
Kenny Rootb8494592015-09-25 02:29:14 +00001066 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001067 return ret;
1068}
1069
David Benjamin1b249672016-12-06 18:25:50 -05001070static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1071 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08001072
1073 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1074 * known attack while we fix ChannelID itself. */
1075 if (ssl->s3->tlsext_channel_id_valid &&
1076 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1077 ssl->s3->tlsext_channel_id_valid = 0;
1078 }
1079
1080 /* If this is a resumption and the original handshake didn't support
1081 * ChannelID then we didn't record the original handshake hashes in the
1082 * session and so cannot resume with ChannelIDs. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001083 if (ssl->session != NULL &&
1084 ssl->session->original_handshake_hash_len == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08001085 ssl->s3->tlsext_channel_id_valid = 0;
1086 }
1087
David Benjaminc895d6b2016-08-11 13:26:41 -04001088 struct timeval now;
1089 ssl_get_current_time(ssl, &now);
1090 ssl->s3->server_random[0] = now.tv_sec >> 24;
1091 ssl->s3->server_random[1] = now.tv_sec >> 16;
1092 ssl->s3->server_random[2] = now.tv_sec >> 8;
1093 ssl->s3->server_random[3] = now.tv_sec;
1094 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001095 return -1;
1096 }
1097
David Benjamin7c0d06c2016-08-11 13:26:41 -04001098 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1099 * 1.3 is finalized and we are not implementing a draft version. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001100
1101 const SSL_SESSION *session = ssl->s3->new_session;
1102 if (ssl->session != NULL) {
1103 session = ssl->session;
1104 }
1105
1106 CBB cbb, body, session_id;
1107 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1108 !CBB_add_u16(&body, ssl->version) ||
1109 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1110 !CBB_add_u8_length_prefixed(&body, &session_id) ||
1111 !CBB_add_bytes(&session_id, session->session_id,
1112 session->session_id_length) ||
1113 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1114 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -05001115 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001116 !ssl_add_message_cbb(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001117 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1118 CBB_cleanup(&cbb);
1119 return -1;
1120 }
1121
Robert Sloan4d1ac502017-02-06 08:36:14 -08001122 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001123}
1124
David Benjamin1b249672016-12-06 18:25:50 -05001125static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1126 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001127 if (!ssl_has_certificate(ssl)) {
1128 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloan4d1ac502017-02-06 08:36:14 -08001129 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001130 }
1131
1132 if (!ssl3_output_cert_chain(ssl)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001133 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001134 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001135 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001136}
1137
David Benjamin1b249672016-12-06 18:25:50 -05001138static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1139 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001140 CBB cbb, body, ocsp_response;
1141 if (!ssl->method->init_message(ssl, &cbb, &body,
1142 SSL3_MT_CERTIFICATE_STATUS) ||
1143 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1144 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Steven Valdeze7531f02016-12-14 13:29:57 -05001145 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1146 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001147 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001148 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1149 CBB_cleanup(&cbb);
1150 return -1;
1151 }
1152
Robert Sloan4d1ac502017-02-06 08:36:14 -08001153 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001154}
1155
David Benjamin1b249672016-12-06 18:25:50 -05001156static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1157 SSL *const ssl = hs->ssl;
Adam Langley4139edb2016-01-13 15:00:54 -08001158 CBB cbb, child;
David Benjaminc895d6b2016-08-11 13:26:41 -04001159 CBB_zero(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +00001160
David Benjaminc895d6b2016-08-11 13:26:41 -04001161 /* Put together the parameters. */
Steven Valdeze7531f02016-12-14 13:29:57 -05001162 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001163 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1164 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001165
David Benjaminc895d6b2016-08-11 13:26:41 -04001166 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1167 if (!CBB_init(&cbb, 128)) {
1168 goto err;
1169 }
1170
Adam Langley4139edb2016-01-13 15:00:54 -08001171 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001172 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -08001173 size_t len =
1174 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1175 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1176 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1177 len)) {
1178 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001179 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001180 }
1181
Adam Langleye9ada862015-05-11 17:20:37 -07001182 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001183 /* Determine the group to use. */
1184 DH *params = ssl->cert->dh_tmp;
1185 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1186 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001187 }
Adam Langley4139edb2016-01-13 15:00:54 -08001188 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001189 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001190 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001191 goto err;
1192 }
Adam Langley4139edb2016-01-13 15:00:54 -08001193
1194 /* Set up DH, generate a key, and emit the public half. */
1195 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001196 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -07001197 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001198 }
1199
David Benjamin1b249672016-12-06 18:25:50 -05001200 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley4139edb2016-01-13 15:00:54 -08001201 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1202 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1203 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1204 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1205 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin1b249672016-12-06 18:25:50 -05001206 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001207 goto err;
1208 }
Adam Langleye9ada862015-05-11 17:20:37 -07001209 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001210 /* Determine the group to use. */
1211 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -05001212 if (!tls1_get_shared_group(hs, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001213 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001214 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001215 goto err;
1216 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001217 ssl->s3->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001218
1219 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamin1b249672016-12-06 18:25:50 -05001220 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001221 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001222 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001223 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamin1b249672016-12-06 18:25:50 -05001224 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamind316cba2016-06-02 16:17:39 -04001225 goto err;
1226 }
Adam Langley4139edb2016-01-13 15:00:54 -08001227 } else {
1228 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001229 }
1230
David Benjamin1b249672016-12-06 18:25:50 -05001231 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001232 goto err;
1233 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001234 }
1235
1236 /* Assemble the message. */
1237 CBB body;
1238 if (!ssl->method->init_message(ssl, &cbb, &body,
1239 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin1b249672016-12-06 18:25:50 -05001240 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001241 goto err;
1242 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001243
Adam Langley4139edb2016-01-13 15:00:54 -08001244 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001245 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001246 if (!ssl_has_private_key(ssl)) {
1247 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001248 goto err;
1249 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001250
David Benjaminc895d6b2016-08-11 13:26:41 -04001251 /* Determine the signature algorithm. */
1252 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -05001253 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001254 goto err;
1255 }
1256 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1257 if (!CBB_add_u16(&body, signature_algorithm)) {
1258 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1259 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1260 goto err;
1261 }
1262 }
1263
1264 /* Add space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001265 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001266 uint8_t *ptr;
1267 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1268 !CBB_reserve(&child, &ptr, max_sig_len)) {
1269 goto err;
1270 }
1271
Adam Langley4139edb2016-01-13 15:00:54 -08001272 size_t sig_len;
1273 enum ssl_private_key_result_t sign_result;
Steven Valdeze7531f02016-12-14 13:29:57 -05001274 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001275 CBB transcript;
1276 uint8_t *transcript_data;
1277 size_t transcript_len;
1278 if (!CBB_init(&transcript,
David Benjamin1b249672016-12-06 18:25:50 -05001279 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1280 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1281 SSL3_RANDOM_SIZE) ||
1282 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1283 SSL3_RANDOM_SIZE) ||
1284 !CBB_add_bytes(&transcript, hs->server_params,
1285 hs->server_params_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001286 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1287 CBB_cleanup(&transcript);
1288 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1289 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001290 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001291 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001292
David Benjaminc895d6b2016-08-11 13:26:41 -04001293 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
1294 signature_algorithm, transcript_data,
1295 transcript_len);
1296 OPENSSL_free(transcript_data);
Kenny Rootb8494592015-09-25 02:29:14 +00001297 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -05001298 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001299 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001300 }
Adam Langley4139edb2016-01-13 15:00:54 -08001301
1302 switch (sign_result) {
1303 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001304 if (!CBB_did_write(&child, sig_len)) {
1305 goto err;
1306 }
1307 break;
1308 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001309 goto err;
1310 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001311 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
Steven Valdeze7531f02016-12-14 13:29:57 -05001312 hs->state = SSL3_ST_SW_KEY_EXCH_B;
Adam Langley4139edb2016-01-13 15:00:54 -08001313 goto err;
1314 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001315 }
1316
Robert Sloan4d1ac502017-02-06 08:36:14 -08001317 if (!ssl_add_message_cbb(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001318 goto err;
1319 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001320
David Benjamin1b249672016-12-06 18:25:50 -05001321 OPENSSL_free(hs->server_params);
1322 hs->server_params = NULL;
1323 hs->server_params_len = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001324
Robert Sloan4d1ac502017-02-06 08:36:14 -08001325 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001326
Adam Langleyd9e397b2015-01-22 14:27:53 -08001327err:
Adam Langley4139edb2016-01-13 15:00:54 -08001328 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001329 return -1;
1330}
1331
David Benjaminc895d6b2016-08-11 13:26:41 -04001332static int add_cert_types(SSL *ssl, CBB *cbb) {
1333 /* Get configured signature algorithms. */
1334 int have_rsa_sign = 0;
1335 int have_ecdsa_sign = 0;
1336 const uint16_t *sig_algs;
David Benjamin95add822016-10-19 01:09:12 -04001337 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1338 for (size_t i = 0; i < num_sig_algs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001339 switch (sig_algs[i]) {
1340 case SSL_SIGN_RSA_PKCS1_SHA512:
1341 case SSL_SIGN_RSA_PKCS1_SHA384:
1342 case SSL_SIGN_RSA_PKCS1_SHA256:
1343 case SSL_SIGN_RSA_PKCS1_SHA1:
1344 have_rsa_sign = 1;
1345 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001346
David Benjaminc895d6b2016-08-11 13:26:41 -04001347 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1348 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1349 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1350 case SSL_SIGN_ECDSA_SHA1:
1351 have_ecdsa_sign = 1;
1352 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001353 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001354 }
1355
David Benjaminc895d6b2016-08-11 13:26:41 -04001356 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1357 return 0;
1358 }
1359
1360 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1361 * check for SSL_kECDH or SSL_kECDHE. */
1362 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1363 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1364 return 0;
1365 }
1366
1367 return 1;
1368}
1369
David Benjamin1b249672016-12-06 18:25:50 -05001370static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1371 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001372 CBB cbb, body, cert_types, sigalgs_cbb;
1373 if (!ssl->method->init_message(ssl, &cbb, &body,
1374 SSL3_MT_CERTIFICATE_REQUEST) ||
1375 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1376 !add_cert_types(ssl, &cert_types)) {
1377 goto err;
1378 }
1379
1380 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1381 const uint16_t *sigalgs;
David Benjamin95add822016-10-19 01:09:12 -04001382 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001383 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
1384 goto err;
1385 }
1386
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001387 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001388 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1389 goto err;
1390 }
1391 }
1392 }
1393
1394 if (!ssl_add_client_CA_list(ssl, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001395 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001396 goto err;
1397 }
1398
Robert Sloan4d1ac502017-02-06 08:36:14 -08001399 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001400
1401err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001402 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1403 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001404 return -1;
1405}
1406
David Benjamin1b249672016-12-06 18:25:50 -05001407static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1408 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001409 CBB cbb, body;
1410 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001411 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001412 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1413 CBB_cleanup(&cbb);
1414 return -1;
1415 }
1416
Robert Sloan4d1ac502017-02-06 08:36:14 -08001417 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001418}
1419
David Benjamin1b249672016-12-06 18:25:50 -05001420static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1421 SSL *const ssl = hs->ssl;
1422 assert(hs->cert_request);
David Benjamin6e899c72016-06-09 18:02:18 -04001423
Robert Sloan4d1ac502017-02-06 08:36:14 -08001424 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001425 if (msg_ret <= 0) {
1426 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001427 }
1428
1429 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1430 if (ssl->version == SSL3_VERSION &&
1431 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001432 /* In SSL 3.0, the Certificate message is omitted to signal no
1433 * certificate. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001434 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001435 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001436 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1437 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001438 }
1439
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001440 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1441 * classed by them as a bug, but it's assumed by at least NGINX. */
1442 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001443 ssl->s3->tmp.reuse_message = 1;
1444 return 1;
1445 }
1446
David Benjamin6e899c72016-06-09 18:02:18 -04001447 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001448 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1449 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001450 }
1451
Robert Sloan5d625782017-02-13 09:55:39 -08001452 if (!ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001453 return -1;
1454 }
1455
David Benjaminc895d6b2016-08-11 13:26:41 -04001456 CBS certificate_msg;
1457 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Steven Valdeze7531f02016-12-14 13:29:57 -05001458
1459 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
1460 EVP_PKEY_free(hs->peer_pubkey);
1461 hs->peer_pubkey = NULL;
Robert Sloan5d625782017-02-13 09:55:39 -08001462 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdeze7531f02016-12-14 13:29:57 -05001463 ssl->s3->new_session->certs =
1464 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1465 ssl->retain_only_sha256_of_client_certs
1466 ? ssl->s3->new_session->peer_sha256
1467 : NULL,
1468 &certificate_msg, ssl->ctx->pool);
1469 if (ssl->s3->new_session->certs == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001470 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdeze7531f02016-12-14 13:29:57 -05001471 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001472 }
1473
Steven Valdeze7531f02016-12-14 13:29:57 -05001474 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloan5d625782017-02-13 09:55:39 -08001475 !ssl->ctx->x509_method->session_cache_objects(ssl->s3->new_session)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001476 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001477 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdeze7531f02016-12-14 13:29:57 -05001478 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001479 }
1480
Steven Valdeze7531f02016-12-14 13:29:57 -05001481 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001482 /* No client certificate so the handshake buffer may be discarded. */
Robert Sloan5d625782017-02-13 09:55:39 -08001483 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin6e899c72016-06-09 18:02:18 -04001484
David Benjaminc895d6b2016-08-11 13:26:41 -04001485 /* In SSL 3.0, sending no certificate is signaled by omitting the
1486 * Certificate message. */
David Benjamin6e899c72016-06-09 18:02:18 -04001487 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001488 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjaminc895d6b2016-08-11 13:26:41 -04001489 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdeze7531f02016-12-14 13:29:57 -05001490 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001491 }
1492
1493 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001494 /* Fail for TLS only if we required a certificate */
1495 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001496 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdeze7531f02016-12-14 13:29:57 -05001497 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001498 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001499
1500 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1501 * classed by them as a bug, but it's assumed by at least NGINX. */
1502 ssl->s3->new_session->verify_result = X509_V_OK;
Steven Valdeze7531f02016-12-14 13:29:57 -05001503 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001504 }
1505
Steven Valdeze7531f02016-12-14 13:29:57 -05001506 /* The hash will have been filled in. */
1507 if (ssl->retain_only_sha256_of_client_certs) {
1508 ssl->s3->new_session->peer_sha256_valid = 1;
1509 }
David Benjamin6e899c72016-06-09 18:02:18 -04001510
Steven Valdeze7531f02016-12-14 13:29:57 -05001511 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1512 ssl->s3->new_session->x509_chain)) {
1513 return -1;
1514 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001515 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001516}
1517
David Benjamin1b249672016-12-06 18:25:50 -05001518static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1519 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08001520 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001521 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001522 uint32_t alg_k;
1523 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001524 uint8_t *premaster_secret = NULL;
1525 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001526 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001527
Adam Langley4139edb2016-01-13 15:00:54 -08001528 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001529 uint8_t psk[PSK_MAX_PSK_LEN];
1530
Steven Valdeze7531f02016-12-14 13:29:57 -05001531 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001532 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001533 if (ret <= 0) {
1534 return ret;
Kenny Roote99801b2015-11-06 15:31:15 -08001535 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001536
1537 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001538 !ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001539 return -1;
1540 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001541 }
1542
Adam Langley4139edb2016-01-13 15:00:54 -08001543 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1544 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1545 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001546
1547 /* If using a PSK key exchange, prepare the pre-shared key. */
1548 if (alg_a & SSL_aPSK) {
1549 CBS psk_identity;
1550
1551 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1552 * then this is the only field in the message. */
1553 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1554 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001555 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001556 al = SSL_AD_DECODE_ERROR;
1557 goto f_err;
1558 }
1559
Adam Langley4139edb2016-01-13 15:00:54 -08001560 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001561 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001562 al = SSL_AD_INTERNAL_ERROR;
1563 goto f_err;
1564 }
1565
1566 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1567 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001568 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001569 al = SSL_AD_ILLEGAL_PARAMETER;
1570 goto f_err;
1571 }
1572
David Benjaminc895d6b2016-08-11 13:26:41 -04001573 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001574 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001575 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001576 goto f_err;
1577 }
1578
1579 /* Look up the key for the identity. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001580 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1581 psk, sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001582 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001583 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001584 al = SSL_AD_INTERNAL_ERROR;
1585 goto f_err;
1586 } else if (psk_len == 0) {
1587 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001588 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001589 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1590 goto f_err;
1591 }
1592 }
1593
1594 /* Depending on the key exchange method, compute |premaster_secret| and
1595 * |premaster_secret_len|. */
1596 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001597 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001598 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001599 decrypt_buf = OPENSSL_malloc(rsa_size);
1600 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001601 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001602 goto err;
1603 }
1604
Kenny Roote99801b2015-11-06 15:31:15 -08001605 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001606 size_t decrypt_len;
Steven Valdeze7531f02016-12-14 13:29:57 -05001607 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001608 if (!ssl_has_private_key(ssl) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001609 ssl_private_key_type(ssl) != NID_rsaEncryption) {
Kenny Roote99801b2015-11-06 15:31:15 -08001610 al = SSL_AD_HANDSHAKE_FAILURE;
1611 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1612 goto f_err;
1613 }
Adam Langley4139edb2016-01-13 15:00:54 -08001614 CBS encrypted_premaster_secret;
1615 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001616 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1617 &encrypted_premaster_secret) ||
1618 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001619 al = SSL_AD_DECODE_ERROR;
1620 OPENSSL_PUT_ERROR(SSL,
1621 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1622 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001623 }
1624 } else {
1625 encrypted_premaster_secret = client_key_exchange;
1626 }
1627
Kenny Roote99801b2015-11-06 15:31:15 -08001628 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1629 * timing-sensitive code below. */
1630 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001631 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001632 CBS_data(&encrypted_premaster_secret),
1633 CBS_len(&encrypted_premaster_secret));
1634 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -05001635 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001636 /* Complete async decrypt. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001637 decrypt_result =
1638 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001639 }
Kenny Roote99801b2015-11-06 15:31:15 -08001640
1641 switch (decrypt_result) {
1642 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001643 break;
1644 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001645 goto err;
1646 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001647 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
Steven Valdeze7531f02016-12-14 13:29:57 -05001648 hs->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001649 goto err;
1650 }
1651
David Benjamin4969cc92016-04-22 15:02:23 -04001652 if (decrypt_len != rsa_size) {
1653 al = SSL_AD_DECRYPT_ERROR;
1654 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1655 goto f_err;
1656 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001657
Adam Langley4139edb2016-01-13 15:00:54 -08001658 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1659 * section 7.4.7.1. */
1660 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1661 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001662 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001663 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001664 goto err;
1665 }
Adam Langley4139edb2016-01-13 15:00:54 -08001666 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1667 goto err;
1668 }
1669
1670 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1671 * publicly invalid. */
1672 if (decrypt_len < 11 + premaster_secret_len) {
1673 al = SSL_AD_DECRYPT_ERROR;
1674 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1675 goto f_err;
1676 }
1677
1678 /* Check the padding. See RFC 3447, section 7.2.2. */
1679 size_t padding_len = decrypt_len - premaster_secret_len;
1680 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1681 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001682 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001683 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1684 }
1685 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1686
1687 /* The premaster secret must begin with |client_version|. This too must be
1688 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1689 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001690 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001691 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001692 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001693
1694 /* Select, in constant time, either the decrypted premaster or the random
1695 * premaster based on |good|. */
David Benjamin7c0d06c2016-08-11 13:26:41 -04001696 for (size_t i = 0; i < premaster_secret_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001697 premaster_secret[i] = constant_time_select_8(
1698 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1699 }
1700
Adam Langleyd9e397b2015-01-22 14:27:53 -08001701 OPENSSL_free(decrypt_buf);
1702 decrypt_buf = NULL;
Steven Valdeze7531f02016-12-14 13:29:57 -05001703 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
David Benjamind316cba2016-06-02 16:17:39 -04001704 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001705 CBS peer_key;
David Benjamin1b249672016-12-06 18:25:50 -05001706 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001707 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001708 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001709 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001710 goto f_err;
1711 }
1712
Adam Langley4139edb2016-01-13 15:00:54 -08001713 /* Compute the premaster. */
Robert Sloan5d625782017-02-13 09:55:39 -08001714 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05001715 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
David Benjamind316cba2016-06-02 16:17:39 -04001716 &premaster_secret_len, &alert, CBS_data(&peer_key),
1717 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001718 al = alert;
1719 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001720 }
1721
Adam Langley4139edb2016-01-13 15:00:54 -08001722 /* The key exchange state may now be discarded. */
David Benjamin1b249672016-12-06 18:25:50 -05001723 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001724 } else if (alg_k & SSL_kPSK) {
1725 /* For plain PSK, other_secret is a block of 0s with the same length as the
1726 * pre-shared key. */
1727 premaster_secret_len = psk_len;
1728 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1729 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001730 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001731 goto err;
1732 }
Robert Sloan69939df2017-01-09 10:53:07 -08001733 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001734 } else {
1735 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001736 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001737 goto f_err;
1738 }
1739
1740 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1741 * pre-shared key. */
1742 if (alg_a & SSL_aPSK) {
1743 CBB new_premaster, child;
1744 uint8_t *new_data;
1745 size_t new_len;
1746
Kenny Rootb8494592015-09-25 02:29:14 +00001747 CBB_zero(&new_premaster);
1748 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1749 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001750 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1751 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1752 !CBB_add_bytes(&child, psk, psk_len) ||
1753 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001754 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001755 CBB_cleanup(&new_premaster);
1756 goto err;
1757 }
1758
1759 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1760 OPENSSL_free(premaster_secret);
1761 premaster_secret = new_data;
1762 premaster_secret_len = new_len;
1763 }
1764
1765 /* Compute the master secret */
Robert Sloan5d625782017-02-13 09:55:39 -08001766 ssl->s3->new_session->master_key_length =
1767 tls1_generate_master_secret(hs, ssl->s3->new_session->master_key,
1768 premaster_secret, premaster_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001769 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001770 goto err;
1771 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001772 ssl->s3->new_session->extended_master_secret =
1773 ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001774
1775 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1776 OPENSSL_free(premaster_secret);
1777 return 1;
1778
1779f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001780 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001781err:
Adam Langley4139edb2016-01-13 15:00:54 -08001782 if (premaster_secret != NULL) {
1783 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001784 OPENSSL_free(premaster_secret);
1785 }
Adam Langleye9ada862015-05-11 17:20:37 -07001786 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001787
1788 return -1;
1789}
1790
David Benjamin1b249672016-12-06 18:25:50 -05001791static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1792 SSL *const ssl = hs->ssl;
Steven Valdeze7531f02016-12-14 13:29:57 -05001793 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001794 CBS certificate_verify, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001795
1796 /* Only RSA and ECDSA client certificates are supported, so a
1797 * CertificateVerify is required if and only if there's a client certificate.
1798 * */
Steven Valdeze7531f02016-12-14 13:29:57 -05001799 if (hs->peer_pubkey == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -08001800 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001801 return 1;
1802 }
1803
Robert Sloan4d1ac502017-02-06 08:36:14 -08001804 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001805 if (msg_ret <= 0) {
1806 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001807 }
1808
Robert Sloan4d1ac502017-02-06 08:36:14 -08001809 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1810 return -1;
1811 }
1812
David Benjaminc895d6b2016-08-11 13:26:41 -04001813 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001814
1815 /* Determine the digest type if needbe. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001816 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001817 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001818 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001819 al = SSL_AD_DECODE_ERROR;
1820 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1821 goto f_err;
1822 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001823 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001824 goto f_err;
1825 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001826 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
1827 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001828 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001829 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001830 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
1831 } else {
1832 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1833 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1834 goto f_err;
Adam Langleye9ada862015-05-11 17:20:37 -07001835 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001836
1837 /* Parse and verify the signature. */
1838 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1839 CBS_len(&certificate_verify) != 0) {
1840 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001841 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001842 goto f_err;
1843 }
1844
David Benjaminc895d6b2016-08-11 13:26:41 -04001845 int sig_ok;
1846 /* The SSL3 construction for CertificateVerify does not decompose into a
1847 * single final digest and signature, and must be special-cased. */
1848 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001849 uint8_t digest[EVP_MAX_MD_SIZE];
1850 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08001851 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1852 &digest_len, ssl->s3->new_session,
1853 signature_algorithm)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001854 goto err;
1855 }
1856
Steven Valdeze7531f02016-12-14 13:29:57 -05001857 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -04001858 sig_ok = pctx != NULL &&
1859 EVP_PKEY_verify_init(pctx) &&
David Benjaminc895d6b2016-08-11 13:26:41 -04001860 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1861 digest, digest_len);
1862 EVP_PKEY_CTX_free(pctx);
1863 } else {
1864 sig_ok = ssl_public_key_verify(
1865 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Robert Sloan5d625782017-02-13 09:55:39 -08001866 hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
1867 hs->transcript.buffer->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001868 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001869
David Benjamin4969cc92016-04-22 15:02:23 -04001870#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1871 sig_ok = 1;
1872 ERR_clear_error();
1873#endif
1874 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001875 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001876 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001877 goto f_err;
1878 }
1879
David Benjaminc895d6b2016-08-11 13:26:41 -04001880 /* The handshake buffer is no longer necessary, and we may hash the current
1881 * message.*/
Robert Sloan5d625782017-02-13 09:55:39 -08001882 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
1883 if (!ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001884 goto err;
1885 }
1886
Steven Valdeze7531f02016-12-14 13:29:57 -05001887 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001888
Steven Valdeze7531f02016-12-14 13:29:57 -05001889f_err:
1890 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001891err:
Steven Valdeze7531f02016-12-14 13:29:57 -05001892 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001893}
1894
David Benjamin6e899c72016-06-09 18:02:18 -04001895/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1896 * sets the next_proto member in s if found */
David Benjamin1b249672016-12-06 18:25:50 -05001897static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1898 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001899 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001900 if (ret <= 0) {
1901 return ret;
1902 }
1903
Robert Sloan4d1ac502017-02-06 08:36:14 -08001904 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001905 !ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001906 return -1;
1907 }
1908
David Benjamin6e899c72016-06-09 18:02:18 -04001909 CBS next_protocol, selected_protocol, padding;
David Benjaminc895d6b2016-08-11 13:26:41 -04001910 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001911 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1912 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001913 CBS_len(&next_protocol) != 0) {
1914 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1915 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1916 return 0;
1917 }
1918
1919 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin6e899c72016-06-09 18:02:18 -04001920 &ssl->s3->next_proto_negotiated_len)) {
1921 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001922 }
1923
David Benjamin6e899c72016-06-09 18:02:18 -04001924 return 1;
1925}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001926
David Benjamin6e899c72016-06-09 18:02:18 -04001927/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjamin1b249672016-12-06 18:25:50 -05001928static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1929 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001930 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001931 if (msg_ret <= 0) {
1932 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001933 }
1934
Robert Sloan4d1ac502017-02-06 08:36:14 -08001935 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001936 !tls1_verify_channel_id(hs) ||
1937 !ssl_hash_current_message(hs)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001938 return -1;
1939 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001940 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001941}
1942
David Benjamin1b249672016-12-06 18:25:50 -05001943static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1944 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001945 const SSL_SESSION *session;
1946 SSL_SESSION *session_copy = NULL;
1947 if (ssl->session == NULL) {
1948 /* Fix the timeout to measure from the ticket issuance time. */
Robert Sloan4d1ac502017-02-06 08:36:14 -08001949 ssl_session_rebase_time(ssl, ssl->s3->new_session);
Steven Valdez909b19f2016-11-21 15:35:44 -05001950 session = ssl->s3->new_session;
1951 } else {
1952 /* We are renewing an existing session. Duplicate the session to adjust the
1953 * timeout. */
1954 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1955 if (session_copy == NULL) {
1956 return -1;
1957 }
1958
Robert Sloan4d1ac502017-02-06 08:36:14 -08001959 ssl_session_rebase_time(ssl, session_copy);
Steven Valdez909b19f2016-11-21 15:35:44 -05001960 session = session_copy;
1961 }
1962
David Benjaminc895d6b2016-08-11 13:26:41 -04001963 CBB cbb, body, ticket;
Steven Valdez909b19f2016-11-21 15:35:44 -05001964 int ok =
1965 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1966 CBB_add_u32(&body, session->timeout) &&
1967 CBB_add_u16_length_prefixed(&body, &ticket) &&
1968 ssl_encrypt_ticket(ssl, &ticket, session) &&
Robert Sloan4d1ac502017-02-06 08:36:14 -08001969 ssl_add_message_cbb(ssl, &cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05001970
1971 SSL_SESSION_free(session_copy);
1972 CBB_cleanup(&cbb);
1973
1974 if (!ok) {
1975 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001976 }
Adam Langleye9ada862015-05-11 17:20:37 -07001977
Robert Sloan4d1ac502017-02-06 08:36:14 -08001978 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001979}