blob: 7b66cf20f0ed34771091f427d9bb609080644e12 [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
David Benjamin1b249672016-12-06 18:25:50 -0500174static int ssl3_get_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamin6e899c72016-06-09 18:02:18 -0400187
Robert Sloan4d1ac502017-02-06 08:36:14 -0800188static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
189 CRYPTO_STATIC_MUTEX_INIT;
190static uint64_t g_v2clienthello_count = 0;
191
192uint64_t SSL_get_v2clienthello_count(void) {
193 CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
194 uint64_t ret = g_v2clienthello_count;
195 CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
196 return ret;
197}
198
David Benjamin1b249672016-12-06 18:25:50 -0500199int ssl3_accept(SSL_HANDSHAKE *hs) {
200 SSL *const ssl = hs->ssl;
Adam Langleye9ada862015-05-11 17:20:37 -0700201 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400203 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204
Adam Langley4139edb2016-01-13 15:00:54 -0800205 assert(ssl->handshake_func == ssl3_accept);
206 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207
208 for (;;) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500209 state = hs->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210
Steven Valdeze7531f02016-12-14 13:29:57 -0500211 switch (hs->state) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400212 case SSL_ST_INIT:
Steven Valdeze7531f02016-12-14 13:29:57 -0500213 hs->state = SSL_ST_ACCEPT;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400214 skip = 1;
215 break;
216
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217 case SSL_ST_ACCEPT:
David Benjamin6e899c72016-06-09 18:02:18 -0400218 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219
Adam Langley4139edb2016-01-13 15:00:54 -0800220 if (!ssl3_init_handshake_buffer(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000221 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222 ret = -1;
223 goto end;
224 }
225
Steven Valdeze7531f02016-12-14 13:29:57 -0500226 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227 break;
228
229 case SSL3_ST_SR_CLNT_HELLO_A:
230 case SSL3_ST_SR_CLNT_HELLO_B:
231 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjaminc895d6b2016-08-11 13:26:41 -0400232 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin1b249672016-12-06 18:25:50 -0500233 ret = ssl3_get_client_hello(hs);
Steven Valdeze7531f02016-12-14 13:29:57 -0500234 if (hs->state == SSL_ST_TLS13) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400235 break;
236 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 if (ret <= 0) {
238 goto end;
239 }
David Benjamin6e899c72016-06-09 18:02:18 -0400240 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500241 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 break;
243
244 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500245 ret = ssl3_send_server_hello(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 if (ret <= 0) {
247 goto end;
248 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400249 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500250 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500252 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254 break;
255
256 case SSL3_ST_SW_CERT_A:
David Benjamind316cba2016-06-02 16:17:39 -0400257 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -0500258 ret = ssl3_send_server_certificate(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800259 if (ret <= 0) {
260 goto end;
261 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 } else {
263 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500265 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 break;
267
Kenny Rootb8494592015-09-25 02:29:14 +0000268 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamin1b249672016-12-06 18:25:50 -0500269 if (hs->certificate_status_expected) {
270 ret = ssl3_send_certificate_status(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000276 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500277 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000278 break;
279
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280 case SSL3_ST_SW_KEY_EXCH_A:
281 case SSL3_ST_SW_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800282 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283
David Benjamin6e899c72016-06-09 18:02:18 -0400284 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
Adam Langley4139edb2016-01-13 15:00:54 -0800285 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
286 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjamin1b249672016-12-06 18:25:50 -0500287 ret = ssl3_send_server_key_exchange(hs);
Adam Langleye9ada862015-05-11 17:20:37 -0700288 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700290 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291 } else {
292 skip = 1;
293 }
294
Steven Valdeze7531f02016-12-14 13:29:57 -0500295 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 break;
297
298 case SSL3_ST_SW_CERT_REQ_A:
David Benjamin1b249672016-12-06 18:25:50 -0500299 if (hs->cert_request) {
300 ret = ssl3_send_certificate_request(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301 if (ret <= 0) {
302 goto end;
303 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700304 } else {
305 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500307 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800308 break;
309
310 case SSL3_ST_SW_SRVR_DONE_A:
David Benjamin1b249672016-12-06 18:25:50 -0500311 ret = ssl3_send_server_hello_done(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800312 if (ret <= 0) {
313 goto end;
314 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500315 hs->next_state = SSL3_ST_SR_CERT_A;
316 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317 break;
318
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 case SSL3_ST_SR_CERT_A:
David Benjamin1b249672016-12-06 18:25:50 -0500320 if (hs->cert_request) {
321 ret = ssl3_get_client_certificate(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800322 if (ret <= 0) {
323 goto end;
324 }
325 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500326 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327 break;
328
329 case SSL3_ST_SR_KEY_EXCH_A:
330 case SSL3_ST_SR_KEY_EXCH_B:
David Benjamin1b249672016-12-06 18:25:50 -0500331 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 if (ret <= 0) {
333 goto end;
334 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500335 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 break;
337
338 case SSL3_ST_SR_CERT_VRFY_A:
David Benjamin1b249672016-12-06 18:25:50 -0500339 ret = ssl3_get_cert_verify(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800340 if (ret <= 0) {
341 goto end;
342 }
343
Steven Valdeze7531f02016-12-14 13:29:57 -0500344 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345 break;
346
Adam Langley4139edb2016-01-13 15:00:54 -0800347 case SSL3_ST_SR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400348 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800349 if (ret <= 0) {
350 goto end;
351 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352
David Benjamin1b249672016-12-06 18:25:50 -0500353 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800354 ret = -1;
355 goto end;
356 }
Adam Langley4139edb2016-01-13 15:00:54 -0800357
Steven Valdeze7531f02016-12-14 13:29:57 -0500358 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800360
361 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500362 if (hs->next_proto_neg_seen) {
363 ret = ssl3_get_next_proto(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400364 if (ret <= 0) {
365 goto end;
366 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400368 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500370 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800371 break;
372
373 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400374 if (ssl->s3->tlsext_channel_id_valid) {
David Benjamin1b249672016-12-06 18:25:50 -0500375 ret = ssl3_get_channel_id(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400376 if (ret <= 0) {
377 goto end;
378 }
379 } else {
380 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800381 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500382 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800383 break;
384
385 case SSL3_ST_SR_FINISHED_A:
David Benjamin1b249672016-12-06 18:25:50 -0500386 ret = ssl3_get_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800387 if (ret <= 0) {
388 goto end;
389 }
390
David Benjamin6e899c72016-06-09 18:02:18 -0400391 ssl->method->received_flight(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400392 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500393 hs->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500395 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800396 }
David Benjamin6e899c72016-06-09 18:02:18 -0400397
David Benjaminc895d6b2016-08-11 13:26:41 -0400398 /* If this is a full handshake with ChannelID then record the handshake
399 * hashes in |ssl->s3->new_session| in case we need them to verify a
400 * ChannelID signature on a resumption of this session in the future. */
401 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Adam Langley4139edb2016-01-13 15:00:54 -0800402 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 if (ret <= 0) {
404 goto end;
405 }
406 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800407 break;
408
409 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamin1b249672016-12-06 18:25:50 -0500410 if (hs->ticket_expected) {
411 ret = ssl3_send_new_session_ticket(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400412 if (ret <= 0) {
413 goto end;
414 }
415 } else {
416 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500418 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800419 break;
420
David Benjaminc895d6b2016-08-11 13:26:41 -0400421 case SSL3_ST_SW_CHANGE:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800422 if (!ssl->method->add_change_cipher_spec(ssl) ||
423 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800424 ret = -1;
425 goto end;
426 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800427
428 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 break;
430
431 case SSL3_ST_SW_FINISHED_A:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800432 ret = ssl3_send_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800433 if (ret <= 0) {
434 goto end;
435 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500436 hs->state = SSL3_ST_SW_FLUSH;
David Benjaminc895d6b2016-08-11 13:26:41 -0400437 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500438 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800439 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500440 hs->next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800442 break;
443
David Benjamin6e899c72016-06-09 18:02:18 -0400444 case SSL3_ST_SW_FLUSH:
David Benjaminf31229b2017-01-25 14:08:15 -0500445 ret = ssl->method->flush_flight(ssl);
446 if (ret <= 0) {
David Benjamin6e899c72016-06-09 18:02:18 -0400447 goto end;
448 }
449
Steven Valdeze7531f02016-12-14 13:29:57 -0500450 hs->state = hs->next_state;
451 if (hs->state != SSL_ST_OK) {
David Benjamin6e899c72016-06-09 18:02:18 -0400452 ssl->method->expect_flight(ssl);
453 }
454 break;
455
David Benjaminc895d6b2016-08-11 13:26:41 -0400456 case SSL_ST_TLS13:
David Benjamin1b249672016-12-06 18:25:50 -0500457 ret = tls13_handshake(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400458 if (ret <= 0) {
459 goto end;
460 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500461 hs->state = SSL_ST_OK;
David Benjaminc895d6b2016-08-11 13:26:41 -0400462 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800463
David Benjaminc895d6b2016-08-11 13:26:41 -0400464 case SSL_ST_OK:
David Benjaminc895d6b2016-08-11 13:26:41 -0400465 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
466
467 /* If we aren't retaining peer certificates then we can discard it
468 * now. */
469 if (ssl->s3->new_session != NULL &&
Steven Valdez909b19f2016-11-21 15:35:44 -0500470 ssl->retain_only_sha256_of_client_certs) {
471 X509_free(ssl->s3->new_session->x509_peer);
472 ssl->s3->new_session->x509_peer = NULL;
473 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
474 ssl->s3->new_session->x509_chain = NULL;
David Benjaminc895d6b2016-08-11 13:26:41 -0400475 }
476
477 SSL_SESSION_free(ssl->s3->established_session);
478 if (ssl->session != NULL) {
479 SSL_SESSION_up_ref(ssl->session);
480 ssl->s3->established_session = ssl->session;
481 } else {
482 ssl->s3->established_session = ssl->s3->new_session;
483 ssl->s3->established_session->not_resumable = 0;
484 ssl->s3->new_session = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400485 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800486
Robert Sloan4d1ac502017-02-06 08:36:14 -0800487 if (hs->v2_clienthello) {
488 CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
489 g_v2clienthello_count++;
490 CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
491 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800492
Steven Valdez909b19f2016-11-21 15:35:44 -0500493 ssl->s3->initial_handshake_complete = 1;
David Benjamin1b249672016-12-06 18:25:50 -0500494 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin6e899c72016-06-09 18:02:18 -0400495
David Benjamin6e899c72016-06-09 18:02:18 -0400496 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800497 ret = 1;
498 goto end;
499
500 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000501 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502 ret = -1;
503 goto end;
504 }
505
Steven Valdeze7531f02016-12-14 13:29:57 -0500506 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
507 int new_state = hs->state;
508 hs->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400509 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Steven Valdeze7531f02016-12-14 13:29:57 -0500510 hs->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800511 }
512 skip = 0;
513 }
514
515end:
David Benjamin6e899c72016-06-09 18:02:18 -0400516 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517 return ret;
518}
519
David Benjamin1b249672016-12-06 18:25:50 -0500520int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
521 uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400522 CBS cipher_suites;
523 CBS_init(&cipher_suites, client_hello->cipher_suites,
524 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800525
David Benjaminc895d6b2016-08-11 13:26:41 -0400526 while (CBS_len(&cipher_suites) > 0) {
527 uint16_t got_id;
528 if (!CBS_get_u16(&cipher_suites, &got_id)) {
529 return 0;
530 }
531
532 if (got_id == id) {
533 return 1;
534 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800535 }
536
David Benjaminc895d6b2016-08-11 13:26:41 -0400537 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800538}
539
David Benjamin1b249672016-12-06 18:25:50 -0500540static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
541 const SSL_CLIENT_HELLO *client_hello) {
542 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800543 assert(!ssl->s3->have_version);
David Benjaminc895d6b2016-08-11 13:26:41 -0400544 uint16_t min_version, max_version;
545 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
546 *out_alert = SSL_AD_PROTOCOL_VERSION;
547 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800548 }
549
David Benjamin7c0d06c2016-08-11 13:26:41 -0400550 uint16_t version = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400551 /* Check supported_versions extension if it is present. */
552 CBS supported_versions;
David Benjamin1b249672016-12-06 18:25:50 -0500553 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
554 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400555 CBS versions;
556 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
557 CBS_len(&supported_versions) != 0 ||
558 CBS_len(&versions) == 0) {
559 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
560 *out_alert = SSL_AD_DECODE_ERROR;
561 return 0;
562 }
563
Steven Valdez909b19f2016-11-21 15:35:44 -0500564 /* Choose the newest commonly-supported version advertised by the client.
565 * The client orders the versions according to its preferences, but we're
566 * not required to honor the client's preferences. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400567 int found_version = 0;
568 while (CBS_len(&versions) != 0) {
569 uint16_t ext_version;
570 if (!CBS_get_u16(&versions, &ext_version)) {
571 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
572 *out_alert = SSL_AD_DECODE_ERROR;
573 return 0;
574 }
575 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
576 continue;
577 }
578 if (min_version <= ext_version &&
Steven Valdez909b19f2016-11-21 15:35:44 -0500579 ext_version <= max_version &&
580 (!found_version || version < ext_version)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400581 version = ext_version;
582 found_version = 1;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400583 }
584 }
585
586 if (!found_version) {
587 goto unsupported_protocol;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400588 }
589 } else {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400590 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
591 * do not use this mechanism. */
592 if (SSL_is_dtls(ssl)) {
593 if (client_hello->version <= DTLS1_2_VERSION) {
594 version = TLS1_2_VERSION;
595 } else if (client_hello->version <= DTLS1_VERSION) {
596 version = TLS1_1_VERSION;
597 } else {
598 goto unsupported_protocol;
599 }
600 } else {
601 if (client_hello->version >= TLS1_2_VERSION) {
602 version = TLS1_2_VERSION;
603 } else if (client_hello->version >= TLS1_1_VERSION) {
604 version = TLS1_1_VERSION;
605 } else if (client_hello->version >= TLS1_VERSION) {
606 version = TLS1_VERSION;
607 } else if (client_hello->version >= SSL3_VERSION) {
608 version = SSL3_VERSION;
609 } else {
610 goto unsupported_protocol;
611 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400612 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400614 /* Apply our minimum and maximum version. */
615 if (version > max_version) {
616 version = max_version;
617 }
David Benjamin6e899c72016-06-09 18:02:18 -0400618
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400619 if (version < min_version) {
620 goto unsupported_protocol;
621 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800622 }
623
David Benjaminc895d6b2016-08-11 13:26:41 -0400624 /* Handle FALLBACK_SCSV. */
625 if (ssl_client_cipher_list_contains_cipher(client_hello,
626 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
627 version < max_version) {
628 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
629 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
630 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800631 }
632
David Benjamin1b249672016-12-06 18:25:50 -0500633 hs->client_version = client_hello->version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400634 ssl->version = ssl->method->version_to_wire(version);
635 ssl->s3->enc_method = ssl3_get_enc_method(version);
636 assert(ssl->s3->enc_method != NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800637
David Benjaminc895d6b2016-08-11 13:26:41 -0400638 /* At this point, the connection's version is known and |ssl->version| is
639 * fixed. Begin enforcing the record-layer version. */
640 ssl->s3->have_version = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800641
642 return 1;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400643
644unsupported_protocol:
645 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
646 *out_alert = SSL_AD_PROTOCOL_VERSION;
647 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800648}
649
David Benjamin1b249672016-12-06 18:25:50 -0500650static STACK_OF(SSL_CIPHER) *
651 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
652 CBS cipher_suites;
653 CBS_init(&cipher_suites, client_hello->cipher_suites,
654 client_hello->cipher_suites_len);
655
656 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
657 if (sk == NULL) {
658 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
659 goto err;
660 }
661
662 while (CBS_len(&cipher_suites) > 0) {
663 uint16_t cipher_suite;
664
665 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
666 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
667 goto err;
668 }
669
670 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
671 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
672 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
673 goto err;
674 }
675 }
676
677 return sk;
678
679err:
680 sk_SSL_CIPHER_free(sk);
681 return NULL;
682}
683
684/* ssl_get_compatible_server_ciphers determines the key exchange and
685 * authentication cipher suite masks compatible with the server configuration
686 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
687 * exchange mask and |*out_mask_a| to the authentication mask. */
688static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
689 uint32_t *out_mask_k,
690 uint32_t *out_mask_a) {
691 SSL *const ssl = hs->ssl;
692 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
693 *out_mask_k = SSL_kGENERIC;
694 *out_mask_a = SSL_aGENERIC;
695 return;
696 }
697
698 uint32_t mask_k = 0;
699 uint32_t mask_a = 0;
700
Robert Sloan4d1ac502017-02-06 08:36:14 -0800701 if (ssl_has_certificate(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -0500702 int type = ssl_private_key_type(ssl);
703 if (type == NID_rsaEncryption) {
704 mask_k |= SSL_kRSA;
705 mask_a |= SSL_aRSA;
706 } else if (ssl_is_ecdsa_key_type(type)) {
707 mask_a |= SSL_aECDSA;
708 }
709 }
710
711 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
712 mask_k |= SSL_kDHE;
713 }
714
715 /* Check for a shared group to consider ECDHE ciphers. */
716 uint16_t unused;
717 if (tls1_get_shared_group(hs, &unused)) {
718 mask_k |= SSL_kECDHE;
719 }
720
David Benjamin1b249672016-12-06 18:25:50 -0500721 /* PSK requires a server callback. */
722 if (ssl->psk_server_callback != NULL) {
723 mask_k |= SSL_kPSK;
724 mask_a |= SSL_aPSK;
725 }
726
727 *out_mask_k = mask_k;
728 *out_mask_a = mask_a;
729}
730
731static const SSL_CIPHER *ssl3_choose_cipher(
732 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
733 const struct ssl_cipher_preference_list_st *server_pref) {
734 SSL *const ssl = hs->ssl;
735 const SSL_CIPHER *c, *ret = NULL;
736 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
737 int ok;
738 size_t cipher_index;
739 uint32_t alg_k, alg_a, mask_k, mask_a;
740 /* in_group_flags will either be NULL, or will point to an array of bytes
741 * which indicate equal-preference groups in the |prio| stack. See the
742 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
743 * struct. */
744 const uint8_t *in_group_flags;
745 /* group_min contains the minimal index so far found in a group, or -1 if no
746 * such value exists yet. */
747 int group_min = -1;
748
749 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
750 if (clnt == NULL) {
751 return NULL;
752 }
753
754 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
755 prio = srvr;
756 in_group_flags = server_pref->in_group_flags;
757 allow = clnt;
758 } else {
759 prio = clnt;
760 in_group_flags = NULL;
761 allow = srvr;
762 }
763
764 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
765
766 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
767 c = sk_SSL_CIPHER_value(prio, i);
768
769 ok = 1;
770
771 /* Check the TLS version. */
772 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
773 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
774 ok = 0;
775 }
776
777 alg_k = c->algorithm_mkey;
778 alg_a = c->algorithm_auth;
779
780 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
781
782 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
783 if (in_group_flags != NULL && in_group_flags[i] == 1) {
784 /* This element of |prio| is in a group. Update the minimum index found
785 * so far and continue looking. */
786 if (group_min == -1 || (size_t)group_min > cipher_index) {
787 group_min = cipher_index;
788 }
789 } else {
790 if (group_min != -1 && (size_t)group_min < cipher_index) {
791 cipher_index = group_min;
792 }
793 ret = sk_SSL_CIPHER_value(allow, cipher_index);
794 break;
795 }
796 }
797
798 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
799 /* We are about to leave a group, but we found a match in it, so that's
800 * our answer. */
801 ret = sk_SSL_CIPHER_value(allow, group_min);
802 break;
803 }
804 }
805
806 sk_SSL_CIPHER_free(clnt);
807 return ret;
808}
809
810static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
811 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -0500812 uint8_t al = SSL_AD_INTERNAL_ERROR;
813 int ret = -1;
Kenny Rootb8494592015-09-25 02:29:14 +0000814 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800815
Steven Valdeze7531f02016-12-14 13:29:57 -0500816 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400817 /* The first time around, read the ClientHello. */
Robert Sloan4d1ac502017-02-06 08:36:14 -0800818 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400819 if (msg_ret <= 0) {
820 return msg_ret;
821 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800822
Robert Sloan4d1ac502017-02-06 08:36:14 -0800823 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
824 return -1;
825 }
826
Steven Valdeze7531f02016-12-14 13:29:57 -0500827 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
David Benjaminc895d6b2016-08-11 13:26:41 -0400828 }
829
David Benjamin1b249672016-12-06 18:25:50 -0500830 SSL_CLIENT_HELLO client_hello;
831 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
832 ssl->init_num)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400833 al = SSL_AD_DECODE_ERROR;
834 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
835 goto f_err;
836 }
837
Steven Valdeze7531f02016-12-14 13:29:57 -0500838 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400839 /* Run the early callback. */
840 if (ssl->ctx->select_certificate_cb != NULL) {
841 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
842 case 0:
843 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
844 goto err;
845
846 case -1:
847 /* Connection rejected. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400848 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjaminc895d6b2016-08-11 13:26:41 -0400849 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
850 goto f_err;
851
852 default:
853 /* fallthrough */;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800854 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400855 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800856
David Benjamin1b249672016-12-06 18:25:50 -0500857 if (!negotiate_version(hs, &al, &client_hello)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400858 goto f_err;
859 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800860
David Benjaminc895d6b2016-08-11 13:26:41 -0400861 /* 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 }
Robert Sloan69939df2017-01-09 10:53:07 -0800866 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
867 client_hello.random_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400868
Robert Sloan4d1ac502017-02-06 08:36:14 -0800869 /* Only null compression is supported. TLS 1.3 further requires the peer
870 * advertise no other compression. */
Robert Sloan69939df2017-01-09 10:53:07 -0800871 if (OPENSSL_memchr(client_hello.compression_methods, 0,
Robert Sloan4d1ac502017-02-06 08:36:14 -0800872 client_hello.compression_methods_len) == NULL ||
873 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
874 client_hello.compression_methods_len != 1)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400875 al = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800876 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
David Benjaminc895d6b2016-08-11 13:26:41 -0400877 goto f_err;
878 }
879
880 /* TLS extensions. */
David Benjamin1b249672016-12-06 18:25:50 -0500881 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400882 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
883 goto err;
884 }
885
Robert Sloan4d1ac502017-02-06 08:36:14 -0800886 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjaminc895d6b2016-08-11 13:26:41 -0400887 }
888
Robert Sloan4d1ac502017-02-06 08:36:14 -0800889 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400890 /* Call |cert_cb| to update server certificates if required. */
891 if (ssl->cert->cert_cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800892 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800893 if (rv == 0) {
894 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000895 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800896 goto f_err;
897 }
898 if (rv < 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800899 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800900 goto err;
901 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800902 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800903
Robert Sloan69939df2017-01-09 10:53:07 -0800904 if (!ssl_auto_chain_if_needed(ssl)) {
905 goto err;
906 }
907
Robert Sloan4d1ac502017-02-06 08:36:14 -0800908 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
909 /* Jump to the TLS 1.3 state machine. */
910 hs->state = SSL_ST_TLS13;
911 hs->do_tls13_handshake = tls13_server_handshake;
912 return 1;
913 }
914
Steven Valdez909b19f2016-11-21 15:35:44 -0500915 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
916 * certificate is finalized. */
917 ssl->s3->tmp.new_cipher =
David Benjamin1b249672016-12-06 18:25:50 -0500918 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
Steven Valdez909b19f2016-11-21 15:35:44 -0500919 if (ssl->s3->tmp.new_cipher == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800920 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000921 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800922 goto f_err;
923 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400924
Robert Sloan4d1ac502017-02-06 08:36:14 -0800925 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
Steven Valdez909b19f2016-11-21 15:35:44 -0500926 }
927
Robert Sloan4d1ac502017-02-06 08:36:14 -0800928 assert(hs->state == SSL3_ST_SR_CLNT_HELLO_D);
Steven Valdez909b19f2016-11-21 15:35:44 -0500929
930 /* Determine whether we are doing session resumption. */
931 int tickets_supported = 0, renew_ticket = 0;
932 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
933 &client_hello)) {
934 case ssl_session_success:
935 break;
936 case ssl_session_error:
937 goto err;
938 case ssl_session_retry:
939 ssl->rwstate = SSL_PENDING_SESSION;
940 goto err;
941 }
942
943 if (session != NULL) {
944 if (session->extended_master_secret &&
945 !ssl->s3->tmp.extended_master_secret) {
946 /* A ClientHello without EMS that attempts to resume a session with EMS
947 * is fatal to the connection. */
948 al = SSL_AD_HANDSHAKE_FAILURE;
949 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
950 goto f_err;
951 }
952
953 if (!ssl_session_is_resumable(ssl, session) ||
954 /* If the client offers the EMS extension, but the previous session
955 * didn't use it, then negotiate a new session. */
956 ssl->s3->tmp.extended_master_secret !=
957 session->extended_master_secret) {
958 SSL_SESSION_free(session);
959 session = NULL;
960 }
961 }
962
963 if (session != NULL) {
964 /* Use the old session. */
David Benjamin1b249672016-12-06 18:25:50 -0500965 hs->ticket_expected = renew_ticket;
Steven Valdez909b19f2016-11-21 15:35:44 -0500966 ssl->session = session;
967 session = NULL;
968 ssl->s3->session_reused = 1;
969 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500970 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500971 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500972 if (!ssl_get_new_session(hs, 1 /* server */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500973 goto err;
974 }
975
976 /* Clear the session ID if we want the session to be single-use. */
977 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
978 ssl->s3->new_session->session_id_length = 0;
979 }
980 }
981
982 if (ssl->ctx->dos_protection_cb != NULL &&
983 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
984 /* Connection rejected for DOS reasons. */
985 al = SSL_AD_INTERNAL_ERROR;
986 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
987 goto f_err;
988 }
989
990 if (ssl->session == NULL) {
991 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
992
993 /* On new sessions, stash the SNI value in the session. */
David Benjamin1b249672016-12-06 18:25:50 -0500994 if (hs->hostname != NULL) {
995 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
Steven Valdez909b19f2016-11-21 15:35:44 -0500996 if (ssl->s3->new_session->tlsext_hostname == NULL) {
997 al = SSL_AD_INTERNAL_ERROR;
998 goto f_err;
999 }
1000 }
Adam Langleyf4e42722015-06-04 17:45:09 -07001001
1002 /* Determine whether to request a client certificate. */
David Benjamin1b249672016-12-06 18:25:50 -05001003 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -07001004 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -08001005 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1006 ssl->s3->tlsext_channel_id_valid) {
David Benjamin1b249672016-12-06 18:25:50 -05001007 hs->cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001008 }
David Benjamind316cba2016-06-02 16:17:39 -04001009 /* CertificateRequest may only be sent in certificate-based ciphers. */
1010 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -05001011 hs->cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001012 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001013
David Benjamin1b249672016-12-06 18:25:50 -05001014 if (!hs->cert_request) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001015 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
1016 * classed by them as a bug, but it's assumed by at least NGINX. */
1017 ssl->s3->new_session->verify_result = X509_V_OK;
1018 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001019 }
1020
Steven Valdez909b19f2016-11-21 15:35:44 -05001021 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1022 * deferred. Complete it now. */
David Benjamin1b249672016-12-06 18:25:50 -05001023 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001024 goto f_err;
1025 }
1026
Robert Sloan4d1ac502017-02-06 08:36:14 -08001027 /* Now that all parameters are known, initialize the handshake hash and hash
1028 * the ClientHello. */
1029 if (!ssl3_init_handshake_hash(ssl) ||
1030 !ssl_hash_current_message(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001031 goto f_err;
1032 }
1033
David Benjaminc895d6b2016-08-11 13:26:41 -04001034 /* Release the handshake buffer if client authentication isn't required. */
David Benjamin1b249672016-12-06 18:25:50 -05001035 if (!hs->cert_request) {
Adam Langley4139edb2016-01-13 15:00:54 -08001036 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001037 }
1038
Adam Langley4139edb2016-01-13 15:00:54 -08001039 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001040
1041 if (0) {
1042 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001043 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001044 }
1045
1046err:
Kenny Rootb8494592015-09-25 02:29:14 +00001047 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001048 return ret;
1049}
1050
David Benjamin1b249672016-12-06 18:25:50 -05001051static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1052 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08001053
1054 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1055 * known attack while we fix ChannelID itself. */
1056 if (ssl->s3->tlsext_channel_id_valid &&
1057 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1058 ssl->s3->tlsext_channel_id_valid = 0;
1059 }
1060
1061 /* If this is a resumption and the original handshake didn't support
1062 * ChannelID then we didn't record the original handshake hashes in the
1063 * session and so cannot resume with ChannelIDs. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001064 if (ssl->session != NULL &&
1065 ssl->session->original_handshake_hash_len == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08001066 ssl->s3->tlsext_channel_id_valid = 0;
1067 }
1068
David Benjaminc895d6b2016-08-11 13:26:41 -04001069 struct timeval now;
1070 ssl_get_current_time(ssl, &now);
1071 ssl->s3->server_random[0] = now.tv_sec >> 24;
1072 ssl->s3->server_random[1] = now.tv_sec >> 16;
1073 ssl->s3->server_random[2] = now.tv_sec >> 8;
1074 ssl->s3->server_random[3] = now.tv_sec;
1075 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001076 return -1;
1077 }
1078
David Benjamin7c0d06c2016-08-11 13:26:41 -04001079 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1080 * 1.3 is finalized and we are not implementing a draft version. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001081
1082 const SSL_SESSION *session = ssl->s3->new_session;
1083 if (ssl->session != NULL) {
1084 session = ssl->session;
1085 }
1086
1087 CBB cbb, body, session_id;
1088 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1089 !CBB_add_u16(&body, ssl->version) ||
1090 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1091 !CBB_add_u8_length_prefixed(&body, &session_id) ||
1092 !CBB_add_bytes(&session_id, session->session_id,
1093 session->session_id_length) ||
1094 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1095 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -05001096 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001097 !ssl_add_message_cbb(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001098 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1099 CBB_cleanup(&cbb);
1100 return -1;
1101 }
1102
Robert Sloan4d1ac502017-02-06 08:36:14 -08001103 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001104}
1105
David Benjamin1b249672016-12-06 18:25:50 -05001106static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1107 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001108 if (!ssl_has_certificate(ssl)) {
1109 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloan4d1ac502017-02-06 08:36:14 -08001110 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001111 }
1112
1113 if (!ssl3_output_cert_chain(ssl)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001114 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001115 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001116 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001117}
1118
David Benjamin1b249672016-12-06 18:25:50 -05001119static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1120 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001121 CBB cbb, body, ocsp_response;
1122 if (!ssl->method->init_message(ssl, &cbb, &body,
1123 SSL3_MT_CERTIFICATE_STATUS) ||
1124 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1125 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Steven Valdeze7531f02016-12-14 13:29:57 -05001126 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1127 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001128 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001129 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1130 CBB_cleanup(&cbb);
1131 return -1;
1132 }
1133
Robert Sloan4d1ac502017-02-06 08:36:14 -08001134 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001135}
1136
David Benjamin1b249672016-12-06 18:25:50 -05001137static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1138 SSL *const ssl = hs->ssl;
Adam Langley4139edb2016-01-13 15:00:54 -08001139 CBB cbb, child;
David Benjaminc895d6b2016-08-11 13:26:41 -04001140 CBB_zero(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +00001141
David Benjaminc895d6b2016-08-11 13:26:41 -04001142 /* Put together the parameters. */
Steven Valdeze7531f02016-12-14 13:29:57 -05001143 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001144 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1145 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001146
David Benjaminc895d6b2016-08-11 13:26:41 -04001147 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1148 if (!CBB_init(&cbb, 128)) {
1149 goto err;
1150 }
1151
Adam Langley4139edb2016-01-13 15:00:54 -08001152 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001153 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -08001154 size_t len =
1155 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1156 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1157 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1158 len)) {
1159 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001160 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001161 }
1162
Adam Langleye9ada862015-05-11 17:20:37 -07001163 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001164 /* Determine the group to use. */
1165 DH *params = ssl->cert->dh_tmp;
1166 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1167 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001168 }
Adam Langley4139edb2016-01-13 15:00:54 -08001169 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001170 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001171 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001172 goto err;
1173 }
Adam Langley4139edb2016-01-13 15:00:54 -08001174
1175 /* Set up DH, generate a key, and emit the public half. */
1176 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001177 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -07001178 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001179 }
1180
David Benjamin1b249672016-12-06 18:25:50 -05001181 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley4139edb2016-01-13 15:00:54 -08001182 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1183 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1184 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1185 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1186 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin1b249672016-12-06 18:25:50 -05001187 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001188 goto err;
1189 }
Adam Langleye9ada862015-05-11 17:20:37 -07001190 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001191 /* Determine the group to use. */
1192 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -05001193 if (!tls1_get_shared_group(hs, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001194 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001195 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001196 goto err;
1197 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001198 ssl->s3->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001199
1200 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamin1b249672016-12-06 18:25:50 -05001201 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001202 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001203 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001204 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamin1b249672016-12-06 18:25:50 -05001205 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamind316cba2016-06-02 16:17:39 -04001206 goto err;
1207 }
Adam Langley4139edb2016-01-13 15:00:54 -08001208 } else {
1209 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001210 }
1211
David Benjamin1b249672016-12-06 18:25:50 -05001212 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001213 goto err;
1214 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001215 }
1216
1217 /* Assemble the message. */
1218 CBB body;
1219 if (!ssl->method->init_message(ssl, &cbb, &body,
1220 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin1b249672016-12-06 18:25:50 -05001221 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001222 goto err;
1223 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001224
Adam Langley4139edb2016-01-13 15:00:54 -08001225 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001226 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001227 if (!ssl_has_private_key(ssl)) {
1228 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001229 goto err;
1230 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001231
David Benjaminc895d6b2016-08-11 13:26:41 -04001232 /* Determine the signature algorithm. */
1233 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -05001234 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001235 goto err;
1236 }
1237 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1238 if (!CBB_add_u16(&body, signature_algorithm)) {
1239 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1240 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1241 goto err;
1242 }
1243 }
1244
1245 /* Add space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001246 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001247 uint8_t *ptr;
1248 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1249 !CBB_reserve(&child, &ptr, max_sig_len)) {
1250 goto err;
1251 }
1252
Adam Langley4139edb2016-01-13 15:00:54 -08001253 size_t sig_len;
1254 enum ssl_private_key_result_t sign_result;
Steven Valdeze7531f02016-12-14 13:29:57 -05001255 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001256 CBB transcript;
1257 uint8_t *transcript_data;
1258 size_t transcript_len;
1259 if (!CBB_init(&transcript,
David Benjamin1b249672016-12-06 18:25:50 -05001260 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1261 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1262 SSL3_RANDOM_SIZE) ||
1263 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1264 SSL3_RANDOM_SIZE) ||
1265 !CBB_add_bytes(&transcript, hs->server_params,
1266 hs->server_params_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001267 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1268 CBB_cleanup(&transcript);
1269 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1270 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001271 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001272 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001273
David Benjaminc895d6b2016-08-11 13:26:41 -04001274 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
1275 signature_algorithm, transcript_data,
1276 transcript_len);
1277 OPENSSL_free(transcript_data);
Kenny Rootb8494592015-09-25 02:29:14 +00001278 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -05001279 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001280 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001281 }
Adam Langley4139edb2016-01-13 15:00:54 -08001282
1283 switch (sign_result) {
1284 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001285 if (!CBB_did_write(&child, sig_len)) {
1286 goto err;
1287 }
1288 break;
1289 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001290 goto err;
1291 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001292 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
Steven Valdeze7531f02016-12-14 13:29:57 -05001293 hs->state = SSL3_ST_SW_KEY_EXCH_B;
Adam Langley4139edb2016-01-13 15:00:54 -08001294 goto err;
1295 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001296 }
1297
Robert Sloan4d1ac502017-02-06 08:36:14 -08001298 if (!ssl_add_message_cbb(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001299 goto err;
1300 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001301
David Benjamin1b249672016-12-06 18:25:50 -05001302 OPENSSL_free(hs->server_params);
1303 hs->server_params = NULL;
1304 hs->server_params_len = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001305
Robert Sloan4d1ac502017-02-06 08:36:14 -08001306 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001307
Adam Langleyd9e397b2015-01-22 14:27:53 -08001308err:
Adam Langley4139edb2016-01-13 15:00:54 -08001309 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001310 return -1;
1311}
1312
David Benjaminc895d6b2016-08-11 13:26:41 -04001313static int add_cert_types(SSL *ssl, CBB *cbb) {
1314 /* Get configured signature algorithms. */
1315 int have_rsa_sign = 0;
1316 int have_ecdsa_sign = 0;
1317 const uint16_t *sig_algs;
David Benjamin95add822016-10-19 01:09:12 -04001318 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1319 for (size_t i = 0; i < num_sig_algs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001320 switch (sig_algs[i]) {
1321 case SSL_SIGN_RSA_PKCS1_SHA512:
1322 case SSL_SIGN_RSA_PKCS1_SHA384:
1323 case SSL_SIGN_RSA_PKCS1_SHA256:
1324 case SSL_SIGN_RSA_PKCS1_SHA1:
1325 have_rsa_sign = 1;
1326 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001327
David Benjaminc895d6b2016-08-11 13:26:41 -04001328 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1329 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1330 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1331 case SSL_SIGN_ECDSA_SHA1:
1332 have_ecdsa_sign = 1;
1333 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001334 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001335 }
1336
David Benjaminc895d6b2016-08-11 13:26:41 -04001337 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1338 return 0;
1339 }
1340
1341 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1342 * check for SSL_kECDH or SSL_kECDHE. */
1343 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1344 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1345 return 0;
1346 }
1347
1348 return 1;
1349}
1350
David Benjamin1b249672016-12-06 18:25:50 -05001351static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1352 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001353 CBB cbb, body, cert_types, sigalgs_cbb;
1354 if (!ssl->method->init_message(ssl, &cbb, &body,
1355 SSL3_MT_CERTIFICATE_REQUEST) ||
1356 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1357 !add_cert_types(ssl, &cert_types)) {
1358 goto err;
1359 }
1360
1361 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1362 const uint16_t *sigalgs;
David Benjamin95add822016-10-19 01:09:12 -04001363 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001364 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
1365 goto err;
1366 }
1367
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001368 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001369 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1370 goto err;
1371 }
1372 }
1373 }
1374
1375 if (!ssl_add_client_CA_list(ssl, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001376 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001377 goto err;
1378 }
1379
Robert Sloan4d1ac502017-02-06 08:36:14 -08001380 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001381
1382err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001383 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1384 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001385 return -1;
1386}
1387
David Benjamin1b249672016-12-06 18:25:50 -05001388static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1389 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001390 CBB cbb, body;
1391 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001392 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001393 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1394 CBB_cleanup(&cbb);
1395 return -1;
1396 }
1397
Robert Sloan4d1ac502017-02-06 08:36:14 -08001398 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001399}
1400
David Benjamin1b249672016-12-06 18:25:50 -05001401static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1402 SSL *const ssl = hs->ssl;
1403 assert(hs->cert_request);
David Benjamin6e899c72016-06-09 18:02:18 -04001404
Robert Sloan4d1ac502017-02-06 08:36:14 -08001405 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001406 if (msg_ret <= 0) {
1407 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001408 }
1409
1410 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1411 if (ssl->version == SSL3_VERSION &&
1412 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001413 /* In SSL 3.0, the Certificate message is omitted to signal no
1414 * certificate. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001415 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001416 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001417 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1418 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001419 }
1420
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001421 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1422 * classed by them as a bug, but it's assumed by at least NGINX. */
1423 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001424 ssl->s3->tmp.reuse_message = 1;
1425 return 1;
1426 }
1427
David Benjamin6e899c72016-06-09 18:02:18 -04001428 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001429 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1430 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001431 }
1432
Robert Sloan4d1ac502017-02-06 08:36:14 -08001433 if (!ssl_hash_current_message(ssl)) {
1434 return -1;
1435 }
1436
David Benjaminc895d6b2016-08-11 13:26:41 -04001437 CBS certificate_msg;
1438 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Steven Valdeze7531f02016-12-14 13:29:57 -05001439
1440 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
1441 EVP_PKEY_free(hs->peer_pubkey);
1442 hs->peer_pubkey = NULL;
David Benjaminc895d6b2016-08-11 13:26:41 -04001443 uint8_t alert;
Steven Valdeze7531f02016-12-14 13:29:57 -05001444 ssl->s3->new_session->certs =
1445 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1446 ssl->retain_only_sha256_of_client_certs
1447 ? ssl->s3->new_session->peer_sha256
1448 : NULL,
1449 &certificate_msg, ssl->ctx->pool);
1450 if (ssl->s3->new_session->certs == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001451 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdeze7531f02016-12-14 13:29:57 -05001452 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001453 }
1454
Steven Valdeze7531f02016-12-14 13:29:57 -05001455 if (CBS_len(&certificate_msg) != 0 ||
1456 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001457 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001458 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdeze7531f02016-12-14 13:29:57 -05001459 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001460 }
1461
Steven Valdeze7531f02016-12-14 13:29:57 -05001462 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001463 /* No client certificate so the handshake buffer may be discarded. */
1464 ssl3_free_handshake_buffer(ssl);
1465
David Benjaminc895d6b2016-08-11 13:26:41 -04001466 /* In SSL 3.0, sending no certificate is signaled by omitting the
1467 * Certificate message. */
David Benjamin6e899c72016-06-09 18:02:18 -04001468 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001469 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjaminc895d6b2016-08-11 13:26:41 -04001470 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdeze7531f02016-12-14 13:29:57 -05001471 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001472 }
1473
1474 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001475 /* Fail for TLS only if we required a certificate */
1476 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001477 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdeze7531f02016-12-14 13:29:57 -05001478 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001479 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001480
1481 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1482 * classed by them as a bug, but it's assumed by at least NGINX. */
1483 ssl->s3->new_session->verify_result = X509_V_OK;
Steven Valdeze7531f02016-12-14 13:29:57 -05001484 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001485 }
1486
Steven Valdeze7531f02016-12-14 13:29:57 -05001487 /* The hash will have been filled in. */
1488 if (ssl->retain_only_sha256_of_client_certs) {
1489 ssl->s3->new_session->peer_sha256_valid = 1;
1490 }
David Benjamin6e899c72016-06-09 18:02:18 -04001491
Steven Valdeze7531f02016-12-14 13:29:57 -05001492 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1493 ssl->s3->new_session->x509_chain)) {
1494 return -1;
1495 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001496 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001497}
1498
David Benjamin1b249672016-12-06 18:25:50 -05001499static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1500 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08001501 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001502 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001503 uint32_t alg_k;
1504 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001505 uint8_t *premaster_secret = NULL;
1506 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001507 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001508
Adam Langley4139edb2016-01-13 15:00:54 -08001509 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001510 uint8_t psk[PSK_MAX_PSK_LEN];
1511
Steven Valdeze7531f02016-12-14 13:29:57 -05001512 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001513 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001514 if (ret <= 0) {
1515 return ret;
Kenny Roote99801b2015-11-06 15:31:15 -08001516 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001517
1518 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
1519 !ssl_hash_current_message(ssl)) {
1520 return -1;
1521 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001522 }
1523
Adam Langley4139edb2016-01-13 15:00:54 -08001524 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1525 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1526 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001527
1528 /* If using a PSK key exchange, prepare the pre-shared key. */
1529 if (alg_a & SSL_aPSK) {
1530 CBS psk_identity;
1531
1532 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1533 * then this is the only field in the message. */
1534 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1535 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001536 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001537 al = SSL_AD_DECODE_ERROR;
1538 goto f_err;
1539 }
1540
Adam Langley4139edb2016-01-13 15:00:54 -08001541 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001542 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001543 al = SSL_AD_INTERNAL_ERROR;
1544 goto f_err;
1545 }
1546
1547 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1548 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001549 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001550 al = SSL_AD_ILLEGAL_PARAMETER;
1551 goto f_err;
1552 }
1553
David Benjaminc895d6b2016-08-11 13:26:41 -04001554 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001555 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001556 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001557 goto f_err;
1558 }
1559
1560 /* Look up the key for the identity. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001561 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1562 psk, sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001563 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001564 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001565 al = SSL_AD_INTERNAL_ERROR;
1566 goto f_err;
1567 } else if (psk_len == 0) {
1568 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001569 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001570 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1571 goto f_err;
1572 }
1573 }
1574
1575 /* Depending on the key exchange method, compute |premaster_secret| and
1576 * |premaster_secret_len|. */
1577 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001578 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001579 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001580 decrypt_buf = OPENSSL_malloc(rsa_size);
1581 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001582 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001583 goto err;
1584 }
1585
Kenny Roote99801b2015-11-06 15:31:15 -08001586 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001587 size_t decrypt_len;
Steven Valdeze7531f02016-12-14 13:29:57 -05001588 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001589 if (!ssl_has_private_key(ssl) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001590 ssl_private_key_type(ssl) != NID_rsaEncryption) {
Kenny Roote99801b2015-11-06 15:31:15 -08001591 al = SSL_AD_HANDSHAKE_FAILURE;
1592 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1593 goto f_err;
1594 }
Adam Langley4139edb2016-01-13 15:00:54 -08001595 CBS encrypted_premaster_secret;
1596 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001597 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1598 &encrypted_premaster_secret) ||
1599 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001600 al = SSL_AD_DECODE_ERROR;
1601 OPENSSL_PUT_ERROR(SSL,
1602 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1603 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001604 }
1605 } else {
1606 encrypted_premaster_secret = client_key_exchange;
1607 }
1608
Kenny Roote99801b2015-11-06 15:31:15 -08001609 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1610 * timing-sensitive code below. */
1611 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001612 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001613 CBS_data(&encrypted_premaster_secret),
1614 CBS_len(&encrypted_premaster_secret));
1615 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -05001616 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001617 /* Complete async decrypt. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001618 decrypt_result =
1619 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001620 }
Kenny Roote99801b2015-11-06 15:31:15 -08001621
1622 switch (decrypt_result) {
1623 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001624 break;
1625 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001626 goto err;
1627 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001628 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
Steven Valdeze7531f02016-12-14 13:29:57 -05001629 hs->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001630 goto err;
1631 }
1632
David Benjamin4969cc92016-04-22 15:02:23 -04001633 if (decrypt_len != rsa_size) {
1634 al = SSL_AD_DECRYPT_ERROR;
1635 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1636 goto f_err;
1637 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001638
Adam Langley4139edb2016-01-13 15:00:54 -08001639 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1640 * section 7.4.7.1. */
1641 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1642 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001643 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001644 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001645 goto err;
1646 }
Adam Langley4139edb2016-01-13 15:00:54 -08001647 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1648 goto err;
1649 }
1650
1651 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1652 * publicly invalid. */
1653 if (decrypt_len < 11 + premaster_secret_len) {
1654 al = SSL_AD_DECRYPT_ERROR;
1655 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1656 goto f_err;
1657 }
1658
1659 /* Check the padding. See RFC 3447, section 7.2.2. */
1660 size_t padding_len = decrypt_len - premaster_secret_len;
1661 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1662 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001663 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001664 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1665 }
1666 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1667
1668 /* The premaster secret must begin with |client_version|. This too must be
1669 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1670 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001671 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001672 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001673 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001674
1675 /* Select, in constant time, either the decrypted premaster or the random
1676 * premaster based on |good|. */
David Benjamin7c0d06c2016-08-11 13:26:41 -04001677 for (size_t i = 0; i < premaster_secret_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001678 premaster_secret[i] = constant_time_select_8(
1679 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1680 }
1681
Adam Langleyd9e397b2015-01-22 14:27:53 -08001682 OPENSSL_free(decrypt_buf);
1683 decrypt_buf = NULL;
Steven Valdeze7531f02016-12-14 13:29:57 -05001684 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
David Benjamind316cba2016-06-02 16:17:39 -04001685 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001686 CBS peer_key;
David Benjamin1b249672016-12-06 18:25:50 -05001687 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001688 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001689 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001690 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001691 goto f_err;
1692 }
1693
Adam Langley4139edb2016-01-13 15:00:54 -08001694 /* Compute the premaster. */
1695 uint8_t alert;
David Benjamin1b249672016-12-06 18:25:50 -05001696 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
David Benjamind316cba2016-06-02 16:17:39 -04001697 &premaster_secret_len, &alert, CBS_data(&peer_key),
1698 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001699 al = alert;
1700 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001701 }
1702
Adam Langley4139edb2016-01-13 15:00:54 -08001703 /* The key exchange state may now be discarded. */
David Benjamin1b249672016-12-06 18:25:50 -05001704 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001705 } else if (alg_k & SSL_kPSK) {
1706 /* For plain PSK, other_secret is a block of 0s with the same length as the
1707 * pre-shared key. */
1708 premaster_secret_len = psk_len;
1709 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1710 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001711 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001712 goto err;
1713 }
Robert Sloan69939df2017-01-09 10:53:07 -08001714 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001715 } else {
1716 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001717 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001718 goto f_err;
1719 }
1720
1721 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1722 * pre-shared key. */
1723 if (alg_a & SSL_aPSK) {
1724 CBB new_premaster, child;
1725 uint8_t *new_data;
1726 size_t new_len;
1727
Kenny Rootb8494592015-09-25 02:29:14 +00001728 CBB_zero(&new_premaster);
1729 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1730 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001731 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1732 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1733 !CBB_add_bytes(&child, psk, psk_len) ||
1734 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001735 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001736 CBB_cleanup(&new_premaster);
1737 goto err;
1738 }
1739
1740 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1741 OPENSSL_free(premaster_secret);
1742 premaster_secret = new_data;
1743 premaster_secret_len = new_len;
1744 }
1745
1746 /* Compute the master secret */
David Benjaminc895d6b2016-08-11 13:26:41 -04001747 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1748 ssl, ssl->s3->new_session->master_key, premaster_secret,
1749 premaster_secret_len);
1750 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001751 goto err;
1752 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001753 ssl->s3->new_session->extended_master_secret =
1754 ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001755
1756 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1757 OPENSSL_free(premaster_secret);
1758 return 1;
1759
1760f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001761 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001762err:
Adam Langley4139edb2016-01-13 15:00:54 -08001763 if (premaster_secret != NULL) {
1764 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001765 OPENSSL_free(premaster_secret);
1766 }
Adam Langleye9ada862015-05-11 17:20:37 -07001767 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001768
1769 return -1;
1770}
1771
David Benjamin1b249672016-12-06 18:25:50 -05001772static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1773 SSL *const ssl = hs->ssl;
Steven Valdeze7531f02016-12-14 13:29:57 -05001774 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001775 CBS certificate_verify, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001776
1777 /* Only RSA and ECDSA client certificates are supported, so a
1778 * CertificateVerify is required if and only if there's a client certificate.
1779 * */
Steven Valdeze7531f02016-12-14 13:29:57 -05001780 if (hs->peer_pubkey == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001781 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001782 return 1;
1783 }
1784
Robert Sloan4d1ac502017-02-06 08:36:14 -08001785 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001786 if (msg_ret <= 0) {
1787 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001788 }
1789
Robert Sloan4d1ac502017-02-06 08:36:14 -08001790 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1791 return -1;
1792 }
1793
David Benjaminc895d6b2016-08-11 13:26:41 -04001794 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001795
1796 /* Determine the digest type if needbe. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001797 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001798 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001799 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001800 al = SSL_AD_DECODE_ERROR;
1801 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1802 goto f_err;
1803 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001804 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001805 goto f_err;
1806 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001807 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
1808 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001809 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001810 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001811 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
1812 } else {
1813 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1815 goto f_err;
Adam Langleye9ada862015-05-11 17:20:37 -07001816 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001817
1818 /* Parse and verify the signature. */
1819 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1820 CBS_len(&certificate_verify) != 0) {
1821 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001822 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001823 goto f_err;
1824 }
1825
David Benjaminc895d6b2016-08-11 13:26:41 -04001826 int sig_ok;
1827 /* The SSL3 construction for CertificateVerify does not decompose into a
1828 * single final digest and signature, and must be special-cased. */
1829 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1830 const EVP_MD *md;
1831 uint8_t digest[EVP_MAX_MD_SIZE];
1832 size_t digest_len;
1833 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
1834 signature_algorithm)) {
1835 goto err;
1836 }
1837
Steven Valdeze7531f02016-12-14 13:29:57 -05001838 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -04001839 sig_ok = pctx != NULL &&
1840 EVP_PKEY_verify_init(pctx) &&
1841 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1842 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1843 digest, digest_len);
1844 EVP_PKEY_CTX_free(pctx);
1845 } else {
1846 sig_ok = ssl_public_key_verify(
1847 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdeze7531f02016-12-14 13:29:57 -05001848 hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
David Benjaminc895d6b2016-08-11 13:26:41 -04001849 ssl->s3->handshake_buffer->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001850 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001851
David Benjamin4969cc92016-04-22 15:02:23 -04001852#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1853 sig_ok = 1;
1854 ERR_clear_error();
1855#endif
1856 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001857 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001858 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001859 goto f_err;
1860 }
1861
David Benjaminc895d6b2016-08-11 13:26:41 -04001862 /* The handshake buffer is no longer necessary, and we may hash the current
1863 * message.*/
1864 ssl3_free_handshake_buffer(ssl);
Steven Valdez909b19f2016-11-21 15:35:44 -05001865 if (!ssl_hash_current_message(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001866 goto err;
1867 }
1868
Steven Valdeze7531f02016-12-14 13:29:57 -05001869 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001870
Steven Valdeze7531f02016-12-14 13:29:57 -05001871f_err:
1872 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001873err:
Steven Valdeze7531f02016-12-14 13:29:57 -05001874 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001875}
1876
David Benjamin6e899c72016-06-09 18:02:18 -04001877/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1878 * sets the next_proto member in s if found */
David Benjamin1b249672016-12-06 18:25:50 -05001879static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1880 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001881 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001882 if (ret <= 0) {
1883 return ret;
1884 }
1885
Robert Sloan4d1ac502017-02-06 08:36:14 -08001886 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
1887 !ssl_hash_current_message(ssl)) {
1888 return -1;
1889 }
1890
David Benjamin6e899c72016-06-09 18:02:18 -04001891 CBS next_protocol, selected_protocol, padding;
David Benjaminc895d6b2016-08-11 13:26:41 -04001892 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001893 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1894 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001895 CBS_len(&next_protocol) != 0) {
1896 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1897 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1898 return 0;
1899 }
1900
1901 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin6e899c72016-06-09 18:02:18 -04001902 &ssl->s3->next_proto_negotiated_len)) {
1903 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001904 }
1905
David Benjamin6e899c72016-06-09 18:02:18 -04001906 return 1;
1907}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001908
David Benjamin6e899c72016-06-09 18:02:18 -04001909/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjamin1b249672016-12-06 18:25:50 -05001910static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1911 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001912 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001913 if (msg_ret <= 0) {
1914 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001915 }
1916
Robert Sloan4d1ac502017-02-06 08:36:14 -08001917 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
1918 !tls1_verify_channel_id(ssl) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001919 !ssl_hash_current_message(ssl)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001920 return -1;
1921 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001922 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001923}
1924
David Benjamin1b249672016-12-06 18:25:50 -05001925static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1926 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001927 const SSL_SESSION *session;
1928 SSL_SESSION *session_copy = NULL;
1929 if (ssl->session == NULL) {
1930 /* Fix the timeout to measure from the ticket issuance time. */
Robert Sloan4d1ac502017-02-06 08:36:14 -08001931 ssl_session_rebase_time(ssl, ssl->s3->new_session);
Steven Valdez909b19f2016-11-21 15:35:44 -05001932 session = ssl->s3->new_session;
1933 } else {
1934 /* We are renewing an existing session. Duplicate the session to adjust the
1935 * timeout. */
1936 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1937 if (session_copy == NULL) {
1938 return -1;
1939 }
1940
Robert Sloan4d1ac502017-02-06 08:36:14 -08001941 ssl_session_rebase_time(ssl, session_copy);
Steven Valdez909b19f2016-11-21 15:35:44 -05001942 session = session_copy;
1943 }
1944
David Benjaminc895d6b2016-08-11 13:26:41 -04001945 CBB cbb, body, ticket;
Steven Valdez909b19f2016-11-21 15:35:44 -05001946 int ok =
1947 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1948 CBB_add_u32(&body, session->timeout) &&
1949 CBB_add_u16_length_prefixed(&body, &ticket) &&
1950 ssl_encrypt_ticket(ssl, &ticket, session) &&
Robert Sloan4d1ac502017-02-06 08:36:14 -08001951 ssl_add_message_cbb(ssl, &cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05001952
1953 SSL_SESSION_free(session_copy);
1954 CBB_cleanup(&cbb);
1955
1956 if (!ok) {
1957 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -04001958 }
Adam Langleye9ada862015-05-11 17:20:37 -07001959
Robert Sloan4d1ac502017-02-06 08:36:14 -08001960 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001961}