blob: 68a013eba5d1e42798e54b5f66d28efc23238ebf [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>
168#include <openssl/sha.h>
169#include <openssl/x509.h>
170
Adam Langleye9ada862015-05-11 17:20:37 -0700171#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172#include "../crypto/internal.h"
173#include "../crypto/dh/internal.h"
174
175
David Benjamin6e899c72016-06-09 18:02:18 -0400176static int ssl3_get_initial_bytes(SSL *ssl);
177static int ssl3_get_v2_client_hello(SSL *ssl);
178static int ssl3_get_client_hello(SSL *ssl);
179static int ssl3_send_server_hello(SSL *ssl);
180static int ssl3_send_server_certificate(SSL *ssl);
181static int ssl3_send_certificate_status(SSL *ssl);
182static int ssl3_send_server_key_exchange(SSL *ssl);
183static int ssl3_send_certificate_request(SSL *ssl);
184static int ssl3_send_server_hello_done(SSL *ssl);
185static int ssl3_get_client_certificate(SSL *ssl);
186static int ssl3_get_client_key_exchange(SSL *ssl);
187static int ssl3_get_cert_verify(SSL *ssl);
188static int ssl3_get_next_proto(SSL *ssl);
189static int ssl3_get_channel_id(SSL *ssl);
190static int ssl3_send_new_session_ticket(SSL *ssl);
191
Adam Langley4139edb2016-01-13 15:00:54 -0800192int ssl3_accept(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193 BUF_MEM *buf = NULL;
Adam Langleye9ada862015-05-11 17:20:37 -0700194 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400196 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197
Adam Langley4139edb2016-01-13 15:00:54 -0800198 assert(ssl->handshake_func == ssl3_accept);
199 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200
201 for (;;) {
Adam Langley4139edb2016-01-13 15:00:54 -0800202 state = ssl->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203
Adam Langley4139edb2016-01-13 15:00:54 -0800204 switch (ssl->state) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205 case SSL_ST_ACCEPT:
David Benjamin6e899c72016-06-09 18:02:18 -0400206 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207
Adam Langley4139edb2016-01-13 15:00:54 -0800208 if (ssl->init_buf == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209 buf = BUF_MEM_new();
David Benjamind316cba2016-06-02 16:17:39 -0400210 if (!buf || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 ret = -1;
212 goto end;
213 }
Adam Langley4139edb2016-01-13 15:00:54 -0800214 ssl->init_buf = buf;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 buf = NULL;
216 }
Adam Langley4139edb2016-01-13 15:00:54 -0800217 ssl->init_num = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218
Adam Langleye9ada862015-05-11 17:20:37 -0700219 /* Enable a write buffer. This groups handshake messages within a flight
220 * into a single write. */
David Benjamind316cba2016-06-02 16:17:39 -0400221 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700222 ret = -1;
223 goto end;
224 }
225
Adam Langley4139edb2016-01-13 15:00:54 -0800226 if (!ssl3_init_handshake_buffer(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000227 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 ret = -1;
229 goto end;
230 }
231
David Benjamin6e899c72016-06-09 18:02:18 -0400232 if (!ssl->s3->have_version && !SSL_IS_DTLS(ssl)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800233 ssl->state = SSL3_ST_SR_INITIAL_BYTES;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800234 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800235 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 break;
238
239 case SSL3_ST_SR_INITIAL_BYTES:
David Benjamin6e899c72016-06-09 18:02:18 -0400240 assert(!SSL_IS_DTLS(ssl));
Adam Langley4139edb2016-01-13 15:00:54 -0800241 ret = ssl3_get_initial_bytes(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 if (ret <= 0) {
243 goto end;
244 }
Adam Langley4139edb2016-01-13 15:00:54 -0800245 /* ssl3_get_initial_bytes sets ssl->state to one of
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
247 break;
248
249 case SSL3_ST_SR_V2_CLIENT_HELLO:
David Benjamin6e899c72016-06-09 18:02:18 -0400250 assert(!SSL_IS_DTLS(ssl));
Adam Langley4139edb2016-01-13 15:00:54 -0800251 ret = ssl3_get_v2_client_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252 if (ret <= 0) {
253 goto end;
254 }
Adam Langley4139edb2016-01-13 15:00:54 -0800255 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 break;
257
258 case SSL3_ST_SR_CLNT_HELLO_A:
259 case SSL3_ST_SR_CLNT_HELLO_B:
260 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800261 ret = ssl3_get_client_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 if (ret <= 0) {
263 goto end;
264 }
David Benjamin6e899c72016-06-09 18:02:18 -0400265 ssl->method->received_flight(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800266 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 break;
268
269 case SSL3_ST_SW_SRVR_HELLO_A:
270 case SSL3_ST_SW_SRVR_HELLO_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800271 ret = ssl3_send_server_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800272 if (ret <= 0) {
273 goto end;
274 }
Adam Langley4139edb2016-01-13 15:00:54 -0800275 if (ssl->hit) {
276 if (ssl->tlsext_ticket_expected) {
277 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800279 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280 }
281 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800282 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800284 break;
285
286 case SSL3_ST_SW_CERT_A:
287 case SSL3_ST_SW_CERT_B:
David Benjamind316cba2016-06-02 16:17:39 -0400288 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800289 ret = ssl3_send_server_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800290 if (ret <= 0) {
291 goto end;
292 }
Adam Langley4139edb2016-01-13 15:00:54 -0800293 if (ssl->s3->tmp.certificate_status_expected) {
294 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800295 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800296 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800297 }
298 } else {
299 skip = 1;
Adam Langley4139edb2016-01-13 15:00:54 -0800300 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800302 break;
303
Kenny Rootb8494592015-09-25 02:29:14 +0000304 case SSL3_ST_SW_CERT_STATUS_A:
305 case SSL3_ST_SW_CERT_STATUS_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800306 ret = ssl3_send_certificate_status(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000307 if (ret <= 0) {
308 goto end;
309 }
Adam Langley4139edb2016-01-13 15:00:54 -0800310 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000311 break;
312
Adam Langleyd9e397b2015-01-22 14:27:53 -0800313 case SSL3_ST_SW_KEY_EXCH_A:
314 case SSL3_ST_SW_KEY_EXCH_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000315 case SSL3_ST_SW_KEY_EXCH_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800316 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317
David Benjamin6e899c72016-06-09 18:02:18 -0400318 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
Adam Langley4139edb2016-01-13 15:00:54 -0800319 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
320 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
321 ret = ssl3_send_server_key_exchange(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -0700322 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700324 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800325 } else {
326 skip = 1;
327 }
328
Adam Langley4139edb2016-01-13 15:00:54 -0800329 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 break;
331
332 case SSL3_ST_SW_CERT_REQ_A:
333 case SSL3_ST_SW_CERT_REQ_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800334 if (ssl->s3->tmp.cert_request) {
335 ret = ssl3_send_certificate_request(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 if (ret <= 0) {
337 goto end;
338 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700339 } else {
340 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 }
Adam Langley4139edb2016-01-13 15:00:54 -0800342 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343 break;
344
345 case SSL3_ST_SW_SRVR_DONE_A:
346 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin6e899c72016-06-09 18:02:18 -0400347 ret = ssl3_send_server_hello_done(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800348 if (ret <= 0) {
349 goto end;
350 }
Adam Langley4139edb2016-01-13 15:00:54 -0800351 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
352 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800353 break;
354
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355 case SSL3_ST_SR_CERT_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800356 if (ssl->s3->tmp.cert_request) {
357 ret = ssl3_get_client_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800358 if (ret <= 0) {
359 goto end;
360 }
361 }
Adam Langley4139edb2016-01-13 15:00:54 -0800362 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800363 break;
364
365 case SSL3_ST_SR_KEY_EXCH_A:
366 case SSL3_ST_SR_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800367 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800368 if (ret <= 0) {
369 goto end;
370 }
Adam Langley4139edb2016-01-13 15:00:54 -0800371 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 break;
373
374 case SSL3_ST_SR_CERT_VRFY_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800375 ret = ssl3_get_cert_verify(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376 if (ret <= 0) {
377 goto end;
378 }
379
Adam Langley4139edb2016-01-13 15:00:54 -0800380 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800381 break;
382
Adam Langley4139edb2016-01-13 15:00:54 -0800383 case SSL3_ST_SR_CHANGE:
384 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
385 if (ret <= 0) {
386 goto end;
387 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800388
David Benjamin4969cc92016-04-22 15:02:23 -0400389 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390 ret = -1;
391 goto end;
392 }
Adam Langley4139edb2016-01-13 15:00:54 -0800393
394 if (ssl->s3->next_proto_neg_seen) {
395 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
396 } else if (ssl->s3->tlsext_channel_id_valid) {
397 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800399 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800400 }
401 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402
403 case SSL3_ST_SR_NEXT_PROTO_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800404 ret = ssl3_get_next_proto(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800405 if (ret <= 0) {
406 goto end;
407 }
Adam Langley4139edb2016-01-13 15:00:54 -0800408 if (ssl->s3->tlsext_channel_id_valid) {
409 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800411 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 }
413 break;
414
415 case SSL3_ST_SR_CHANNEL_ID_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800416 ret = ssl3_get_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 if (ret <= 0) {
418 goto end;
419 }
Adam Langley4139edb2016-01-13 15:00:54 -0800420 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800421 break;
422
423 case SSL3_ST_SR_FINISHED_A:
David Benjamind316cba2016-06-02 16:17:39 -0400424 ret = ssl3_get_finished(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425 if (ret <= 0) {
426 goto end;
427 }
428
David Benjamin6e899c72016-06-09 18:02:18 -0400429 ssl->method->received_flight(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800430 if (ssl->hit) {
431 ssl->state = SSL_ST_OK;
432 } else if (ssl->tlsext_ticket_expected) {
433 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800434 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800435 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 }
David Benjamin6e899c72016-06-09 18:02:18 -0400437
Adam Langleyd9e397b2015-01-22 14:27:53 -0800438 /* If this is a full handshake with ChannelID then record the hashshake
Adam Langley4139edb2016-01-13 15:00:54 -0800439 * hashes in |ssl->session| in case we need them to verify a ChannelID
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440 * signature on a resumption of this session in the future. */
Adam Langley4139edb2016-01-13 15:00:54 -0800441 if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
442 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443 if (ret <= 0) {
444 goto end;
445 }
446 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800447 break;
448
449 case SSL3_ST_SW_SESSION_TICKET_A:
450 case SSL3_ST_SW_SESSION_TICKET_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800451 ret = ssl3_send_new_session_ticket(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800452 if (ret <= 0) {
453 goto end;
454 }
Adam Langley4139edb2016-01-13 15:00:54 -0800455 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800456 break;
457
458 case SSL3_ST_SW_CHANGE_A:
459 case SSL3_ST_SW_CHANGE_B:
David Benjamin6e899c72016-06-09 18:02:18 -0400460 ret = ssl->method->send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
461 SSL3_ST_SW_CHANGE_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462 if (ret <= 0) {
463 goto end;
464 }
Adam Langley4139edb2016-01-13 15:00:54 -0800465 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800466
David Benjamin4969cc92016-04-22 15:02:23 -0400467 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800468 ret = -1;
469 goto end;
470 }
471 break;
472
473 case SSL3_ST_SW_FINISHED_A:
474 case SSL3_ST_SW_FINISHED_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800475 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjamin4969cc92016-04-22 15:02:23 -0400476 SSL3_ST_SW_FINISHED_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477 if (ret <= 0) {
478 goto end;
479 }
Adam Langley4139edb2016-01-13 15:00:54 -0800480 ssl->state = SSL3_ST_SW_FLUSH;
481 if (ssl->hit) {
482 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800483 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800484 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800485 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800486 break;
487
David Benjamin6e899c72016-06-09 18:02:18 -0400488 case SSL3_ST_SW_FLUSH:
489 if (BIO_flush(ssl->wbio) <= 0) {
490 ssl->rwstate = SSL_WRITING;
491 ret = -1;
492 goto end;
493 }
494
495 ssl->state = ssl->s3->tmp.next_state;
496 if (ssl->state != SSL_ST_OK) {
497 ssl->method->expect_flight(ssl);
498 }
499 break;
500
Adam Langleyd9e397b2015-01-22 14:27:53 -0800501 case SSL_ST_OK:
502 /* clean a few things up */
Adam Langley4139edb2016-01-13 15:00:54 -0800503 ssl3_cleanup_key_block(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800504
David Benjamin6e899c72016-06-09 18:02:18 -0400505 /* In DTLS, |init_buf| cannot be released because post-handshake
506 * retransmit relies on that buffer being available as scratch space.
507 *
508 * TODO(davidben): Fix this. */
509 if (!SSL_IS_DTLS(ssl)) {
510 BUF_MEM_free(ssl->init_buf);
511 ssl->init_buf = NULL;
512 ssl->init_num = 0;
513 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800514
515 /* remove buffering on output */
Adam Langley4139edb2016-01-13 15:00:54 -0800516 ssl_free_wbio_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517
Adam Langleyd9e397b2015-01-22 14:27:53 -0800518 /* If we aren't retaining peer certificates then we can discard it
519 * now. */
Adam Langley4139edb2016-01-13 15:00:54 -0800520 if (ssl->ctx->retain_only_sha256_of_client_certs) {
521 X509_free(ssl->session->peer);
522 ssl->session->peer = NULL;
523 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
524 ssl->session->cert_chain = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800525 }
526
David Benjamin6e899c72016-06-09 18:02:18 -0400527 if (SSL_IS_DTLS(ssl)) {
528 ssl->d1->handshake_read_seq = 0;
529 ssl->d1->handshake_write_seq = 0;
530 ssl->d1->next_handshake_write_seq = 0;
531 }
532
Adam Langley4139edb2016-01-13 15:00:54 -0800533 ssl->s3->initial_handshake_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800534
Adam Langley4139edb2016-01-13 15:00:54 -0800535 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800536
David Benjamin6e899c72016-06-09 18:02:18 -0400537 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800538
539 ret = 1;
540 goto end;
541
542 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000543 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800544 ret = -1;
545 goto end;
546 }
547
David Benjamin6e899c72016-06-09 18:02:18 -0400548 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
549 int new_state = ssl->state;
Adam Langley4139edb2016-01-13 15:00:54 -0800550 ssl->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400551 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langley4139edb2016-01-13 15:00:54 -0800552 ssl->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800553 }
554 skip = 0;
555 }
556
557end:
Adam Langleye9ada862015-05-11 17:20:37 -0700558 BUF_MEM_free(buf);
David Benjamin6e899c72016-06-09 18:02:18 -0400559 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800560 return ret;
561}
562
David Benjamin6e899c72016-06-09 18:02:18 -0400563static int ssl3_get_initial_bytes(SSL *ssl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000564 /* Read the first 5 bytes, the size of the TLS record header. This is
565 * sufficient to detect a V2ClientHello and ensures that we never read beyond
566 * the first record. */
Adam Langley4139edb2016-01-13 15:00:54 -0800567 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800568 if (ret <= 0) {
569 return ret;
570 }
Adam Langley4139edb2016-01-13 15:00:54 -0800571 assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
572 const uint8_t *p = ssl_read_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800573
574 /* Some dedicated error codes for protocol mixups should the application wish
575 * to interpret them differently. (These do not overlap with ClientHello or
576 * V2ClientHello.) */
577 if (strncmp("GET ", (const char *)p, 4) == 0 ||
578 strncmp("POST ", (const char *)p, 5) == 0 ||
579 strncmp("HEAD ", (const char *)p, 5) == 0 ||
580 strncmp("PUT ", (const char *)p, 4) == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000581 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800582 return -1;
583 }
Kenny Rootb8494592015-09-25 02:29:14 +0000584 if (strncmp("CONNE", (const char *)p, 5) == 0) {
585 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800586 return -1;
587 }
588
Kenny Rootb8494592015-09-25 02:29:14 +0000589 /* Determine if this is a V2ClientHello. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800590 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
591 p[3] >= SSL3_VERSION_MAJOR) {
592 /* This is a V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800593 ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800594 return 1;
595 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800596
Kenny Rootb8494592015-09-25 02:29:14 +0000597 /* Fall through to the standard logic. */
Adam Langley4139edb2016-01-13 15:00:54 -0800598 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000599 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800600}
601
David Benjamin6e899c72016-06-09 18:02:18 -0400602static int ssl3_get_v2_client_hello(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800603 const uint8_t *p;
604 int ret;
605 CBS v2_client_hello, cipher_specs, session_id, challenge;
David Benjamind316cba2016-06-02 16:17:39 -0400606 size_t msg_length, rand_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800607 uint8_t msg_type;
608 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
609 CBB client_hello, hello_body, cipher_suites;
610 uint8_t random[SSL3_RANDOM_SIZE];
611
Kenny Rootb8494592015-09-25 02:29:14 +0000612 /* Determine the length of the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800613 assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
614 p = ssl_read_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800615 msg_length = ((p[0] & 0x7f) << 8) | p[1];
616 if (msg_length > (1024 * 4)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000617 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800618 return -1;
619 }
Kenny Rootb8494592015-09-25 02:29:14 +0000620 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
621 /* Reject lengths that are too short early. We have already read
622 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
623 * (invalid) V2ClientHello which would be shorter than that. */
624 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800625 return -1;
626 }
627
Kenny Rootb8494592015-09-25 02:29:14 +0000628 /* Read the remainder of the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800629 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800630 if (ret <= 0) {
631 return ret;
632 }
Adam Langley4139edb2016-01-13 15:00:54 -0800633 assert(ssl_read_buffer_len(ssl) == msg_length + 2);
634 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800635
Kenny Rootb8494592015-09-25 02:29:14 +0000636 /* The V2ClientHello without the length is incorporated into the handshake
Adam Langleyd9e397b2015-01-22 14:27:53 -0800637 * hash. */
Adam Langley4139edb2016-01-13 15:00:54 -0800638 if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
Kenny Rootb8494592015-09-25 02:29:14 +0000639 CBS_len(&v2_client_hello))) {
Adam Langleye9ada862015-05-11 17:20:37 -0700640 return -1;
641 }
David Benjamin6e899c72016-06-09 18:02:18 -0400642
643 ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
644 CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800645
646 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
647 !CBS_get_u16(&v2_client_hello, &version) ||
648 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
649 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
650 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
651 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
652 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
653 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
654 CBS_len(&v2_client_hello) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000655 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800656 return -1;
657 }
658
659 /* msg_type has already been checked. */
660 assert(msg_type == SSL2_MT_CLIENT_HELLO);
661
662 /* The client_random is the V2ClientHello challenge. Truncate or
663 * left-pad with zeros as needed. */
664 memset(random, 0, SSL3_RANDOM_SIZE);
665 rand_len = CBS_len(&challenge);
666 if (rand_len > SSL3_RANDOM_SIZE) {
667 rand_len = SSL3_RANDOM_SIZE;
668 }
669 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
670 rand_len);
671
672 /* Write out an equivalent SSLv3 ClientHello. */
Kenny Rootb8494592015-09-25 02:29:14 +0000673 CBB_zero(&client_hello);
Adam Langley4139edb2016-01-13 15:00:54 -0800674 if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
675 ssl->init_buf->max) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000676 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800677 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
678 !CBB_add_u16(&hello_body, version) ||
679 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
680 /* No session id. */
681 !CBB_add_u8(&hello_body, 0) ||
682 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
683 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800685 return -1;
686 }
687
688 /* Copy the cipher suites. */
689 while (CBS_len(&cipher_specs) > 0) {
690 uint32_t cipher_spec;
691 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
692 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000693 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800694 return -1;
695 }
696
697 /* Skip SSLv2 ciphers. */
698 if ((cipher_spec & 0xff0000) != 0) {
699 continue;
700 }
701 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
702 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000703 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800704 return -1;
705 }
706 }
707
708 /* Add the null compression scheme and finish. */
709 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
David Benjamind316cba2016-06-02 16:17:39 -0400710 !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800711 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000712 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800713 return -1;
714 }
715
716 /* Mark the message for "re"-use by the version-specific method. */
Adam Langley4139edb2016-01-13 15:00:54 -0800717 ssl->s3->tmp.reuse_message = 1;
718 ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
David Benjamind316cba2016-06-02 16:17:39 -0400719 ssl->s3->tmp.message_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800720
Kenny Rootb8494592015-09-25 02:29:14 +0000721 /* Consume and discard the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800722 ssl_read_buffer_consume(ssl, 2 + msg_length);
723 ssl_read_buffer_discard(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800724
725 return 1;
726}
727
David Benjamin6e899c72016-06-09 18:02:18 -0400728static int ssl3_get_client_hello(SSL *ssl) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700729 int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800730 long n;
731 const SSL_CIPHER *c;
732 STACK_OF(SSL_CIPHER) *ciphers = NULL;
733 struct ssl_early_callback_ctx early_ctx;
734 CBS client_hello;
735 uint16_t client_version;
736 CBS client_random, session_id, cipher_suites, compression_methods;
Kenny Rootb8494592015-09-25 02:29:14 +0000737 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800738
739 /* We do this so that we will respond with our native type. If we are TLSv1
740 * and we get SSLv3, we will respond with TLSv1, This down switching should
741 * be handled by a different method. If we are SSLv3, we will respond with
742 * SSLv3, even if prompted with TLSv1. */
Adam Langley4139edb2016-01-13 15:00:54 -0800743 switch (ssl->state) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800744 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamind316cba2016-06-02 16:17:39 -0400745 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
746 ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800747
748 if (!ok) {
749 return n;
750 }
751
David Benjamind316cba2016-06-02 16:17:39 -0400752 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800753 /* fallthrough */
David Benjamind316cba2016-06-02 16:17:39 -0400754 case SSL3_ST_SR_CLNT_HELLO_B:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800755 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800756 /* We have previously parsed the ClientHello message, and can't call
757 * ssl_get_message again without hashing the message into the Finished
758 * digest again. */
Adam Langley4139edb2016-01-13 15:00:54 -0800759 n = ssl->init_num;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800760
761 memset(&early_ctx, 0, sizeof(early_ctx));
Adam Langley4139edb2016-01-13 15:00:54 -0800762 early_ctx.ssl = ssl;
763 early_ctx.client_hello = ssl->init_msg;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800764 early_ctx.client_hello_len = n;
765 if (!ssl_early_callback_init(&early_ctx)) {
766 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000767 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800768 goto f_err;
769 }
770
David Benjamind316cba2016-06-02 16:17:39 -0400771 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B &&
Adam Langley4139edb2016-01-13 15:00:54 -0800772 ssl->ctx->select_certificate_cb != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400773 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
Adam Langley4139edb2016-01-13 15:00:54 -0800774 switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800775 case 0:
Adam Langley4139edb2016-01-13 15:00:54 -0800776 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
Adam Langleye9ada862015-05-11 17:20:37 -0700777 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800778
779 case -1:
780 /* Connection rejected. */
781 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000782 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800783 goto f_err;
784
785 default:
786 /* fallthrough */;
787 }
788 }
David Benjamind316cba2016-06-02 16:17:39 -0400789 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800790 break;
791
792 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000793 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800794 return -1;
795 }
796
Adam Langley4139edb2016-01-13 15:00:54 -0800797 CBS_init(&client_hello, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800798 if (!CBS_get_u16(&client_hello, &client_version) ||
799 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
800 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
801 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
802 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000803 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800804 goto f_err;
805 }
806
807 /* use version from inside client hello, not from record header (may differ:
808 * see RFC 2246, Appendix E, second paragraph) */
Adam Langley4139edb2016-01-13 15:00:54 -0800809 ssl->client_version = client_version;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800810
811 /* Load the client random. */
Adam Langley4139edb2016-01-13 15:00:54 -0800812 memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800813
Adam Langley4139edb2016-01-13 15:00:54 -0800814 if (SSL_IS_DTLS(ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800815 CBS cookie;
816
817 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
818 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
819 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000820 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800821 goto f_err;
822 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800823 }
824
Adam Langleyf4e42722015-06-04 17:45:09 -0700825 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
826 * asynchronously.
827 *
828 * TODO(davidben): Clean up the order of events around ClientHello
829 * processing. */
Adam Langley4139edb2016-01-13 15:00:54 -0800830 if (!ssl->s3->have_version) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800831 /* Select version to use */
Adam Langley4139edb2016-01-13 15:00:54 -0800832 uint16_t version = ssl3_get_mutual_version(ssl, client_version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800833 if (version == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000834 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
Adam Langley4139edb2016-01-13 15:00:54 -0800835 ssl->version = ssl->client_version;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800836 al = SSL_AD_PROTOCOL_VERSION;
837 goto f_err;
838 }
Adam Langley4139edb2016-01-13 15:00:54 -0800839 ssl->version = version;
David Benjamin4969cc92016-04-22 15:02:23 -0400840 ssl->s3->enc_method = ssl3_get_enc_method(version);
841 assert(ssl->s3->enc_method != NULL);
Adam Langley4139edb2016-01-13 15:00:54 -0800842 /* At this point, the connection's version is known and |ssl->version| is
Adam Langleyd9e397b2015-01-22 14:27:53 -0800843 * fixed. Begin enforcing the record-layer version. */
Adam Langley4139edb2016-01-13 15:00:54 -0800844 ssl->s3->have_version = 1;
845 } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
846 : (ssl->client_version < ssl->version)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000847 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800848 al = SSL_AD_PROTOCOL_VERSION;
849 goto f_err;
850 }
851
Adam Langley4139edb2016-01-13 15:00:54 -0800852 ssl->hit = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000853 int send_new_ticket = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800854 switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000855 case ssl_session_success:
856 break;
857 case ssl_session_error:
858 goto err;
859 case ssl_session_retry:
Adam Langley4139edb2016-01-13 15:00:54 -0800860 ssl->rwstate = SSL_PENDING_SESSION;
Kenny Rootb8494592015-09-25 02:29:14 +0000861 goto err;
Adam Langleyf4e42722015-06-04 17:45:09 -0700862 }
Adam Langley4139edb2016-01-13 15:00:54 -0800863 ssl->tlsext_ticket_expected = send_new_ticket;
Adam Langleyf4e42722015-06-04 17:45:09 -0700864
865 /* The EMS state is needed when making the resumption decision, but
866 * extensions are not normally parsed until later. This detects the EMS
867 * extension for the resumption decision and it's checked against the result
868 * of the normal parse later in this function. */
869 const uint8_t *ems_data;
870 size_t ems_len;
871 int have_extended_master_secret =
Adam Langley4139edb2016-01-13 15:00:54 -0800872 ssl->version != SSL3_VERSION &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700873 SSL_early_callback_ctx_extension_get(&early_ctx,
874 TLSEXT_TYPE_extended_master_secret,
875 &ems_data, &ems_len) &&
876 ems_len == 0;
877
Kenny Rootb8494592015-09-25 02:29:14 +0000878 if (session != NULL) {
879 if (session->extended_master_secret &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700880 !have_extended_master_secret) {
881 /* A ClientHello without EMS that attempts to resume a session with EMS
882 * is fatal to the connection. */
883 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000884 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -0700885 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800886 }
887
Adam Langley4139edb2016-01-13 15:00:54 -0800888 ssl->hit =
Adam Langleyf4e42722015-06-04 17:45:09 -0700889 /* Only resume if the session's version matches the negotiated version:
890 * most clients do not accept a mismatch. */
Adam Langley4139edb2016-01-13 15:00:54 -0800891 ssl->version == session->ssl_version &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700892 /* If the client offers the EMS extension, but the previous session
893 * didn't use it, then negotiate a new session. */
Kenny Rootb8494592015-09-25 02:29:14 +0000894 have_extended_master_secret == session->extended_master_secret;
Adam Langleyf4e42722015-06-04 17:45:09 -0700895 }
896
Adam Langley4139edb2016-01-13 15:00:54 -0800897 if (ssl->hit) {
Kenny Rootb8494592015-09-25 02:29:14 +0000898 /* Use the new session. */
Adam Langley4139edb2016-01-13 15:00:54 -0800899 SSL_SESSION_free(ssl->session);
900 ssl->session = session;
Kenny Rootb8494592015-09-25 02:29:14 +0000901 session = NULL;
902
Adam Langley4139edb2016-01-13 15:00:54 -0800903 ssl->verify_result = ssl->session->verify_result;
Kenny Roote99801b2015-11-06 15:31:15 -0800904 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800905 if (!ssl_get_new_session(ssl, 1 /* server */)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800906 goto err;
907 }
908
909 /* Clear the session ID if we want the session to be single-use. */
Adam Langley4139edb2016-01-13 15:00:54 -0800910 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
911 ssl->session->session_id_length = 0;
Kenny Roote99801b2015-11-06 15:31:15 -0800912 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800913 }
914
Adam Langley4139edb2016-01-13 15:00:54 -0800915 if (ssl->ctx->dos_protection_cb != NULL &&
916 ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700917 /* Connection rejected for DOS reasons. */
918 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000919 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleye9ada862015-05-11 17:20:37 -0700920 goto f_err;
921 }
922
Adam Langleyd9e397b2015-01-22 14:27:53 -0800923 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
Adam Langleye9ada862015-05-11 17:20:37 -0700924 CBS_len(&cipher_suites) == 0 ||
925 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800926 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
927 CBS_len(&compression_methods) == 0) {
928 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000929 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800930 goto f_err;
931 }
932
Adam Langley4139edb2016-01-13 15:00:54 -0800933 ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800934 if (ciphers == NULL) {
935 goto err;
936 }
937
938 /* If it is a hit, check that the cipher is in the list. */
Adam Langley4139edb2016-01-13 15:00:54 -0800939 if (ssl->hit) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800940 size_t j;
941 int found_cipher = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800942 uint32_t id = ssl->session->cipher->id;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800943
944 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
945 c = sk_SSL_CIPHER_value(ciphers, j);
946 if (c->id == id) {
947 found_cipher = 1;
948 break;
949 }
950 }
951
952 if (!found_cipher) {
953 /* we need to have the cipher in the cipher list if we are asked to reuse
954 * it */
955 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000956 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800957 goto f_err;
958 }
959 }
960
961 /* Only null compression is supported. */
962 if (memchr(CBS_data(&compression_methods), 0,
963 CBS_len(&compression_methods)) == NULL) {
964 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000965 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800966 goto f_err;
967 }
968
969 /* TLS extensions. */
Adam Langley4139edb2016-01-13 15:00:54 -0800970 if (ssl->version >= SSL3_VERSION &&
971 !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000972 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800973 goto err;
974 }
975
976 /* There should be nothing left over in the record. */
977 if (CBS_len(&client_hello) != 0) {
978 /* wrong packet length */
979 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000980 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800981 goto f_err;
982 }
983
Adam Langley4139edb2016-01-13 15:00:54 -0800984 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700985 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000986 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
Adam Langleyf4e42722015-06-04 17:45:09 -0700987 goto f_err;
988 }
989
Adam Langleyd9e397b2015-01-22 14:27:53 -0800990 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
Adam Langley4139edb2016-01-13 15:00:54 -0800991 if (!ssl->hit) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800992 if (ciphers == NULL) {
993 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000994 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800995 goto f_err;
996 }
997
998 /* Let cert callback update server certificates if required */
Adam Langley4139edb2016-01-13 15:00:54 -0800999 if (ssl->cert->cert_cb) {
1000 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001001 if (rv == 0) {
1002 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001003 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001004 goto f_err;
1005 }
1006 if (rv < 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001007 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001008 goto err;
1009 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001010 }
Adam Langley4139edb2016-01-13 15:00:54 -08001011 c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001012
1013 if (c == NULL) {
1014 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001015 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001016 goto f_err;
1017 }
David Benjamin4969cc92016-04-22 15:02:23 -04001018 ssl->session->cipher = c;
Adam Langley4139edb2016-01-13 15:00:54 -08001019 ssl->s3->tmp.new_cipher = c;
Adam Langleyf4e42722015-06-04 17:45:09 -07001020
1021 /* Determine whether to request a client certificate. */
Adam Langley4139edb2016-01-13 15:00:54 -08001022 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -07001023 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -08001024 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1025 ssl->s3->tlsext_channel_id_valid) {
1026 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001027 }
David Benjamind316cba2016-06-02 16:17:39 -04001028 /* CertificateRequest may only be sent in certificate-based ciphers. */
1029 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001030 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001031 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001032 } else {
1033 /* Session-id reuse */
Adam Langley4139edb2016-01-13 15:00:54 -08001034 ssl->s3->tmp.new_cipher = ssl->session->cipher;
1035 ssl->s3->tmp.cert_request = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001036 }
1037
Kenny Rootb8494592015-09-25 02:29:14 +00001038 /* Now that the cipher is known, initialize the handshake hash. */
Adam Langley4139edb2016-01-13 15:00:54 -08001039 if (!ssl3_init_handshake_hash(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001040 goto f_err;
1041 }
1042
Adam Langleyf4e42722015-06-04 17:45:09 -07001043 /* In TLS 1.2, client authentication requires hashing the handshake transcript
1044 * under a different hash. Otherwise, release the handshake buffer. */
David Benjamin4969cc92016-04-22 15:02:23 -04001045 if (!ssl->s3->tmp.cert_request ||
1046 ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langley4139edb2016-01-13 15:00:54 -08001047 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001048 }
1049
1050 /* we now have the following setup;
1051 * client_random
1052 * cipher_list - our prefered list of ciphers
1053 * ciphers - the clients prefered list of ciphers
1054 * compression - basically ignored right now
1055 * ssl version is set - sslv3
Adam Langley4139edb2016-01-13 15:00:54 -08001056 * ssl->session - The ssl session has been setup.
1057 * ssl->hit - session reuse flag
1058 * ssl->tmp.new_cipher - the new cipher to use. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001059
Adam Langley4139edb2016-01-13 15:00:54 -08001060 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001061
1062 if (0) {
1063 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001064 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001065 }
1066
1067err:
Adam Langleye9ada862015-05-11 17:20:37 -07001068 sk_SSL_CIPHER_free(ciphers);
Kenny Rootb8494592015-09-25 02:29:14 +00001069 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001070 return ret;
1071}
1072
David Benjamin6e899c72016-06-09 18:02:18 -04001073static int ssl3_send_server_hello(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001074 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1075 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001076 }
1077
Kenny Roote99801b2015-11-06 15:31:15 -08001078 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1079
1080 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1081 * known attack while we fix ChannelID itself. */
1082 if (ssl->s3->tlsext_channel_id_valid &&
1083 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1084 ssl->s3->tlsext_channel_id_valid = 0;
1085 }
1086
1087 /* If this is a resumption and the original handshake didn't support
1088 * ChannelID then we didn't record the original handshake hashes in the
1089 * session and so cannot resume with ChannelIDs. */
1090 if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1091 ssl->s3->tlsext_channel_id_valid = 0;
1092 }
1093
1094 if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1095 1 /* server */)) {
1096 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1097 return -1;
1098 }
1099
1100 CBB cbb, session_id;
1101 size_t length;
1102 CBB_zero(&cbb);
1103 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1104 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1105 !CBB_add_u16(&cbb, ssl->version) ||
1106 !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1107 !CBB_add_u8_length_prefixed(&cbb, &session_id) ||
1108 !CBB_add_bytes(&session_id, ssl->session->session_id,
1109 ssl->session->session_id_length) ||
1110 !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1111 !CBB_add_u8(&cbb, 0 /* no compression */) ||
1112 !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1113 !CBB_finish(&cbb, NULL, &length) ||
1114 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1115 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1116 CBB_cleanup(&cbb);
1117 return -1;
1118 }
1119
1120 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1121 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001122}
1123
David Benjamin6e899c72016-06-09 18:02:18 -04001124static int ssl3_send_server_certificate(SSL *ssl) {
1125 if (ssl->state == SSL3_ST_SW_CERT_A) {
1126 if (!ssl3_output_cert_chain(ssl)) {
1127 return 0;
1128 }
1129 ssl->state = SSL3_ST_SW_CERT_B;
1130 }
1131
1132 /* SSL3_ST_SW_CERT_B */
1133 return ssl_do_write(ssl);
1134}
1135
1136static int ssl3_send_certificate_status(SSL *ssl) {
Kenny Rootb8494592015-09-25 02:29:14 +00001137 if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1138 CBB out, ocsp_response;
1139 size_t length;
1140
1141 CBB_zero(&out);
1142 if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1143 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1144 !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
1145 !CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
1146 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1147 ssl->ctx->ocsp_response_length) ||
1148 !CBB_finish(&out, NULL, &length) ||
1149 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1150 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1151 CBB_cleanup(&out);
1152 return -1;
1153 }
1154
1155 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1156 }
1157
1158 /* SSL3_ST_SW_CERT_STATUS_B */
1159 return ssl_do_write(ssl);
1160}
1161
David Benjamin6e899c72016-06-09 18:02:18 -04001162static int ssl3_send_server_key_exchange(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -08001163 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
1164 return ssl_do_write(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001165 }
1166
Adam Langley4139edb2016-01-13 15:00:54 -08001167 CBB cbb, child;
1168 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1169 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1170 goto err;
Kenny Rootb8494592015-09-25 02:29:14 +00001171 }
1172
Adam Langley4139edb2016-01-13 15:00:54 -08001173 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1174 /* This is the first iteration, so write parameters. */
1175 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1176 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001177
Adam Langley4139edb2016-01-13 15:00:54 -08001178 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001179 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -08001180 size_t len =
1181 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1182 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1183 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1184 len)) {
1185 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001186 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001187 }
1188
Adam Langleye9ada862015-05-11 17:20:37 -07001189 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001190 /* Determine the group to use. */
1191 DH *params = ssl->cert->dh_tmp;
1192 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1193 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001194 }
Adam Langley4139edb2016-01-13 15:00:54 -08001195 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001196 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001197 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001198 goto err;
1199 }
Adam Langley4139edb2016-01-13 15:00:54 -08001200 ssl->session->key_exchange_info = DH_num_bits(params);
1201
1202 /* Set up DH, generate a key, and emit the public half. */
1203 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001204 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -07001205 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001206 }
1207
Adam Langley4139edb2016-01-13 15:00:54 -08001208 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1209 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1210 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1211 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1212 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1213 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001214 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001215 goto err;
1216 }
Adam Langleye9ada862015-05-11 17:20:37 -07001217 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001218 /* Determine the group to use. */
1219 uint16_t group_id;
1220 if (!tls1_get_shared_group(ssl, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001221 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001222 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001223 goto err;
1224 }
David Benjamind316cba2016-06-02 16:17:39 -04001225 ssl->session->key_exchange_info = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001226
1227 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamind316cba2016-06-02 16:17:39 -04001228 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001229 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001230 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001231 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001232 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1233 goto err;
1234 }
1235 } else if (alg_k & SSL_kCECPQ1) {
1236 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1) ||
1237 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1238 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001239 goto err;
1240 }
Adam Langley4139edb2016-01-13 15:00:54 -08001241 } else {
1242 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001243 }
1244
Adam Langley4139edb2016-01-13 15:00:54 -08001245 /* Otherwise, restore |cbb| from the previous iteration.
1246 * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler
1247 * pattern. Probably keep the |CBB| around in the handshake state. */
1248 } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) {
1249 goto err;
1250 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001251
Adam Langley4139edb2016-01-13 15:00:54 -08001252 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001253 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001254 if (!ssl_has_private_key(ssl)) {
1255 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001256 goto err;
1257 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001258
Adam Langley4139edb2016-01-13 15:00:54 -08001259 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1260 size_t sig_len;
1261 enum ssl_private_key_result_t sign_result;
1262 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1263 /* This is the first iteration, so set up the signature. Sample the
1264 * parameter length before adding a signature algorithm. */
1265 if (!CBB_flush(&cbb)) {
1266 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001267 }
Adam Langley4139edb2016-01-13 15:00:54 -08001268 size_t params_len = CBB_len(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001269
1270 /* Determine signature algorithm. */
Adam Langley4139edb2016-01-13 15:00:54 -08001271 const EVP_MD *md;
David Benjamin4969cc92016-04-22 15:02:23 -04001272 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley4139edb2016-01-13 15:00:54 -08001273 md = tls1_choose_signing_digest(ssl);
1274 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001275 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001276 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1277 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001278 }
Adam Langley4139edb2016-01-13 15:00:54 -08001279 } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001280 md = EVP_md5_sha1();
1281 } else {
1282 md = EVP_sha1();
1283 }
1284
Adam Langley4139edb2016-01-13 15:00:54 -08001285 /* Compute the digest and sign it. */
1286 uint8_t digest[EVP_MAX_MD_SIZE];
1287 unsigned digest_len = 0;
1288 EVP_MD_CTX md_ctx;
1289 EVP_MD_CTX_init(&md_ctx);
1290 int digest_ret =
1291 EVP_DigestInit_ex(&md_ctx, md, NULL) &&
1292 EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
1293 EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
1294 EVP_DigestUpdate(&md_ctx, CBB_data(&cbb), params_len) &&
1295 EVP_DigestFinal_ex(&md_ctx, digest, &digest_len);
1296 EVP_MD_CTX_cleanup(&md_ctx);
1297 uint8_t *ptr;
1298 if (!digest_ret ||
1299 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1300 !CBB_reserve(&child, &ptr, max_sig_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001301 goto err;
1302 }
Adam Langley4139edb2016-01-13 15:00:54 -08001303 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1304 digest, digest_len);
Kenny Rootb8494592015-09-25 02:29:14 +00001305 } else {
Adam Langley4139edb2016-01-13 15:00:54 -08001306 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
1307
1308 /* Retry the signature. */
1309 uint8_t *ptr;
1310 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1311 !CBB_reserve(&child, &ptr, max_sig_len)) {
1312 goto err;
1313 }
1314 sign_result =
1315 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001316 }
Adam Langley4139edb2016-01-13 15:00:54 -08001317
1318 switch (sign_result) {
1319 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001320 if (!CBB_did_write(&child, sig_len)) {
1321 goto err;
1322 }
1323 break;
1324 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001325 goto err;
1326 case ssl_private_key_retry:
1327 /* Discard the unfinished signature and save the state of |cbb| for the
1328 * next iteration. */
1329 CBB_discard_child(&cbb);
1330 ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb);
1331 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1332 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1333 goto err;
1334 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001335 }
1336
Adam Langley4139edb2016-01-13 15:00:54 -08001337 size_t length;
1338 if (!CBB_finish(&cbb, NULL, &length) ||
1339 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001340 goto err;
1341 }
Adam Langley4139edb2016-01-13 15:00:54 -08001342 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
1343 return ssl_do_write(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001344
Adam Langleyd9e397b2015-01-22 14:27:53 -08001345err:
Adam Langley4139edb2016-01-13 15:00:54 -08001346 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001347 return -1;
1348}
1349
David Benjamin6e899c72016-06-09 18:02:18 -04001350static int ssl3_send_certificate_request(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001351 uint8_t *p, *d;
1352 size_t i;
1353 int j, nl, off, n;
1354 STACK_OF(X509_NAME) *sk = NULL;
1355 X509_NAME *name;
1356 BUF_MEM *buf;
1357
Adam Langley4139edb2016-01-13 15:00:54 -08001358 if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
1359 buf = ssl->init_buf;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001360
Adam Langley4139edb2016-01-13 15:00:54 -08001361 d = p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001362
1363 /* get the list of acceptable cert types */
1364 p++;
Adam Langley4139edb2016-01-13 15:00:54 -08001365 n = ssl3_get_req_cert_type(ssl, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001366 d[0] = n;
1367 p += n;
1368 n++;
1369
David Benjamin4969cc92016-04-22 15:02:23 -04001370 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001371 const uint8_t *psigs;
Adam Langley4139edb2016-01-13 15:00:54 -08001372 nl = tls12_get_psigalgs(ssl, &psigs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001373 s2n(nl, p);
1374 memcpy(p, psigs, nl);
1375 p += nl;
1376 n += nl + 2;
1377 }
1378
1379 off = n;
1380 p += 2;
1381 n += 2;
1382
Adam Langley4139edb2016-01-13 15:00:54 -08001383 sk = SSL_get_client_CA_list(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001384 nl = 0;
1385 if (sk != NULL) {
1386 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1387 name = sk_X509_NAME_value(sk, i);
1388 j = i2d_X509_NAME(name, NULL);
Adam Langley4139edb2016-01-13 15:00:54 -08001389 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001390 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001391 goto err;
1392 }
Adam Langley4139edb2016-01-13 15:00:54 -08001393 p = ssl_handshake_start(ssl) + n;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001394 s2n(j, p);
1395 i2d_X509_NAME(name, &p);
1396 n += 2 + j;
1397 nl += 2 + j;
1398 }
1399 }
1400
1401 /* else no CA names */
Adam Langley4139edb2016-01-13 15:00:54 -08001402 p = ssl_handshake_start(ssl) + off;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001403 s2n(nl, p);
1404
Adam Langley4139edb2016-01-13 15:00:54 -08001405 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001406 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001407 }
Adam Langley4139edb2016-01-13 15:00:54 -08001408 ssl->state = SSL3_ST_SW_CERT_REQ_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001409 }
1410
1411 /* SSL3_ST_SW_CERT_REQ_B */
Adam Langley4139edb2016-01-13 15:00:54 -08001412 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001413
1414err:
1415 return -1;
1416}
1417
David Benjamin6e899c72016-06-09 18:02:18 -04001418static int ssl3_send_server_hello_done(SSL *ssl) {
1419 if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
1420 if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO_DONE, 0)) {
1421 return -1;
1422 }
1423 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1424 }
1425
1426 /* SSL3_ST_SW_SRVR_DONE_B */
1427 return ssl_do_write(ssl);
1428}
1429
1430static int ssl3_get_client_certificate(SSL *ssl) {
1431 int ok, al, ret = -1;
1432 X509 *x = NULL;
1433 unsigned long n;
1434 STACK_OF(X509) *sk = NULL;
1435 SHA256_CTX sha256;
1436 CBS certificate_msg, certificate_list;
1437 int is_first_certificate = 1;
1438
1439 assert(ssl->s3->tmp.cert_request);
1440 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
1441
1442 if (!ok) {
1443 return n;
1444 }
1445
1446 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1447 if (ssl->version == SSL3_VERSION &&
1448 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1449 /* In SSL 3.0, the Certificate message is omitted to signal no certificate. */
1450 if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1451 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1452 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1453 al = SSL_AD_HANDSHAKE_FAILURE;
1454 goto f_err;
1455 }
1456
1457 ssl->s3->tmp.reuse_message = 1;
1458 return 1;
1459 }
1460
1461 al = SSL_AD_UNEXPECTED_MESSAGE;
1462 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1463 goto f_err;
1464 }
1465
1466 CBS_init(&certificate_msg, ssl->init_msg, n);
1467
1468 sk = sk_X509_new_null();
1469 if (sk == NULL) {
1470 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1471 goto err;
1472 }
1473
1474 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
1475 CBS_len(&certificate_msg) != 0) {
1476 al = SSL_AD_DECODE_ERROR;
1477 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1478 goto f_err;
1479 }
1480
1481 while (CBS_len(&certificate_list) > 0) {
1482 CBS certificate;
1483 const uint8_t *data;
1484
1485 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1486 al = SSL_AD_DECODE_ERROR;
1487 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1488 goto f_err;
1489 }
1490
1491 if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
1492 /* If this is the first certificate, and we don't want to keep peer
1493 * certificates in memory, then we hash it right away. */
1494 SHA256_Init(&sha256);
1495 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
1496 SHA256_Final(ssl->session->peer_sha256, &sha256);
1497 ssl->session->peer_sha256_valid = 1;
1498 }
1499 is_first_certificate = 0;
1500
1501 /* A u24 length cannot overflow a long. */
1502 data = CBS_data(&certificate);
1503 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
1504 if (x == NULL) {
1505 al = SSL_AD_BAD_CERTIFICATE;
1506 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1507 goto f_err;
1508 }
1509 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1510 al = SSL_AD_DECODE_ERROR;
1511 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1512 goto f_err;
1513 }
1514 if (!sk_X509_push(sk, x)) {
1515 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1516 goto err;
1517 }
1518 x = NULL;
1519 }
1520
1521 if (sk_X509_num(sk) <= 0) {
1522 /* No client certificate so the handshake buffer may be discarded. */
1523 ssl3_free_handshake_buffer(ssl);
1524
1525 /* TLS does not mind 0 certs returned */
1526 if (ssl->version == SSL3_VERSION) {
1527 al = SSL_AD_HANDSHAKE_FAILURE;
1528 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
1529 goto f_err;
1530 } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1531 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1532 /* Fail for TLS only if we required a certificate */
1533 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1534 al = SSL_AD_HANDSHAKE_FAILURE;
1535 goto f_err;
1536 }
1537 } else {
1538 if (ssl_verify_cert_chain(ssl, sk) <= 0) {
1539 al = ssl_verify_alarm_type(ssl->verify_result);
1540 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
1541 goto f_err;
1542 }
1543 }
1544
1545 X509_free(ssl->session->peer);
1546 ssl->session->peer = sk_X509_shift(sk);
1547 ssl->session->verify_result = ssl->verify_result;
1548
1549 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1550 ssl->session->cert_chain = sk;
1551 /* Inconsistency alert: cert_chain does *not* include the peer's own
1552 * certificate, while we do include it in s3_clnt.c */
1553
1554 sk = NULL;
1555
1556 ret = 1;
1557
1558 if (0) {
1559 f_err:
1560 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1561 }
1562
1563err:
1564 X509_free(x);
1565 sk_X509_pop_free(sk, X509_free);
1566 return ret;
1567}
1568
1569static int ssl3_get_client_key_exchange(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001570 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001571 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001572 uint32_t alg_k;
1573 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001574 uint8_t *premaster_secret = NULL;
1575 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001576 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001577
Adam Langley4139edb2016-01-13 15:00:54 -08001578 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001579 uint8_t psk[PSK_MAX_PSK_LEN];
1580
David Benjamind316cba2016-06-02 16:17:39 -04001581 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Kenny Roote99801b2015-11-06 15:31:15 -08001582 int ok;
Adam Langley4139edb2016-01-13 15:00:54 -08001583 const long n = ssl->method->ssl_get_message(
David Benjamind316cba2016-06-02 16:17:39 -04001584 ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok);
Kenny Roote99801b2015-11-06 15:31:15 -08001585 if (!ok) {
1586 return n;
1587 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001588 }
1589
Adam Langley4139edb2016-01-13 15:00:54 -08001590 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1591 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1592 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001593
1594 /* If using a PSK key exchange, prepare the pre-shared key. */
1595 if (alg_a & SSL_aPSK) {
1596 CBS psk_identity;
1597
1598 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1599 * then this is the only field in the message. */
1600 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1601 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001602 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001603 al = SSL_AD_DECODE_ERROR;
1604 goto f_err;
1605 }
1606
Adam Langley4139edb2016-01-13 15:00:54 -08001607 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001608 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001609 al = SSL_AD_INTERNAL_ERROR;
1610 goto f_err;
1611 }
1612
1613 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1614 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001615 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001616 al = SSL_AD_ILLEGAL_PARAMETER;
1617 goto f_err;
1618 }
1619
Adam Langley4139edb2016-01-13 15:00:54 -08001620 if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001621 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001622 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001623 goto f_err;
1624 }
1625
1626 /* Look up the key for the identity. */
Adam Langley4139edb2016-01-13 15:00:54 -08001627 psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
1628 sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001629 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001630 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001631 al = SSL_AD_INTERNAL_ERROR;
1632 goto f_err;
1633 } else if (psk_len == 0) {
1634 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001635 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001636 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1637 goto f_err;
1638 }
1639 }
1640
1641 /* Depending on the key exchange method, compute |premaster_secret| and
1642 * |premaster_secret_len|. */
1643 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001644 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001645 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001646 decrypt_buf = OPENSSL_malloc(rsa_size);
1647 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001648 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001649 goto err;
1650 }
1651
Kenny Roote99801b2015-11-06 15:31:15 -08001652 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001653 size_t decrypt_len;
David Benjamind316cba2016-06-02 16:17:39 -04001654 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001655 if (!ssl_has_private_key(ssl) ||
1656 ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
Kenny Roote99801b2015-11-06 15:31:15 -08001657 al = SSL_AD_HANDSHAKE_FAILURE;
1658 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1659 goto f_err;
1660 }
Adam Langley4139edb2016-01-13 15:00:54 -08001661 CBS encrypted_premaster_secret;
1662 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001663 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1664 &encrypted_premaster_secret) ||
1665 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001666 al = SSL_AD_DECODE_ERROR;
1667 OPENSSL_PUT_ERROR(SSL,
1668 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1669 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001670 }
1671 } else {
1672 encrypted_premaster_secret = client_key_exchange;
1673 }
1674
Kenny Roote99801b2015-11-06 15:31:15 -08001675 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1676 * timing-sensitive code below. */
1677 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001678 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001679 CBS_data(&encrypted_premaster_secret),
1680 CBS_len(&encrypted_premaster_secret));
1681 } else {
David Benjamind316cba2016-06-02 16:17:39 -04001682 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001683 /* Complete async decrypt. */
1684 decrypt_result = ssl_private_key_decrypt_complete(
Adam Langley4139edb2016-01-13 15:00:54 -08001685 ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001686 }
Kenny Roote99801b2015-11-06 15:31:15 -08001687
1688 switch (decrypt_result) {
1689 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001690 break;
1691 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001692 goto err;
1693 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001694 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind316cba2016-06-02 16:17:39 -04001695 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001696 goto err;
1697 }
1698
David Benjamin4969cc92016-04-22 15:02:23 -04001699 if (decrypt_len != rsa_size) {
1700 al = SSL_AD_DECRYPT_ERROR;
1701 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1702 goto f_err;
1703 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001704
Adam Langley4139edb2016-01-13 15:00:54 -08001705 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1706 * section 7.4.7.1. */
1707 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1708 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001709 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001710 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001711 goto err;
1712 }
Adam Langley4139edb2016-01-13 15:00:54 -08001713 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1714 goto err;
1715 }
1716
1717 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1718 * publicly invalid. */
1719 if (decrypt_len < 11 + premaster_secret_len) {
1720 al = SSL_AD_DECRYPT_ERROR;
1721 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1722 goto f_err;
1723 }
1724
1725 /* Check the padding. See RFC 3447, section 7.2.2. */
1726 size_t padding_len = decrypt_len - premaster_secret_len;
1727 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1728 constant_time_eq_int_8(decrypt_buf[1], 2);
1729 size_t i;
1730 for (i = 2; i < padding_len - 1; i++) {
1731 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1732 }
1733 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1734
1735 /* The premaster secret must begin with |client_version|. This too must be
1736 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1737 good &= constant_time_eq_8(decrypt_buf[padding_len],
1738 (unsigned)(ssl->client_version >> 8));
1739 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
1740 (unsigned)(ssl->client_version & 0xff));
1741
1742 /* Select, in constant time, either the decrypted premaster or the random
1743 * premaster based on |good|. */
1744 for (i = 0; i < premaster_secret_len; i++) {
1745 premaster_secret[i] = constant_time_select_8(
1746 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1747 }
1748
Adam Langleyd9e397b2015-01-22 14:27:53 -08001749 OPENSSL_free(decrypt_buf);
1750 decrypt_buf = NULL;
David Benjamind316cba2016-06-02 16:17:39 -04001751 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1752 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001753 CBS peer_key;
David Benjamind316cba2016-06-02 16:17:39 -04001754 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1755 &peer_key) ||
1756 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001757 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001758 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001759 goto f_err;
1760 }
1761
Adam Langley4139edb2016-01-13 15:00:54 -08001762 /* Compute the premaster. */
1763 uint8_t alert;
David Benjamind316cba2016-06-02 16:17:39 -04001764 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1765 &premaster_secret_len, &alert, CBS_data(&peer_key),
1766 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001767 al = alert;
1768 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001769 }
1770
Adam Langley4139edb2016-01-13 15:00:54 -08001771 /* The key exchange state may now be discarded. */
1772 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001773 } else if (alg_k & SSL_kPSK) {
1774 /* For plain PSK, other_secret is a block of 0s with the same length as the
1775 * pre-shared key. */
1776 premaster_secret_len = psk_len;
1777 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1778 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001779 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001780 goto err;
1781 }
1782 memset(premaster_secret, 0, premaster_secret_len);
1783 } else {
1784 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001785 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001786 goto f_err;
1787 }
1788
1789 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1790 * pre-shared key. */
1791 if (alg_a & SSL_aPSK) {
1792 CBB new_premaster, child;
1793 uint8_t *new_data;
1794 size_t new_len;
1795
Kenny Rootb8494592015-09-25 02:29:14 +00001796 CBB_zero(&new_premaster);
1797 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1798 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001799 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1800 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1801 !CBB_add_bytes(&child, psk, psk_len) ||
1802 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001803 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001804 CBB_cleanup(&new_premaster);
1805 goto err;
1806 }
1807
1808 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1809 OPENSSL_free(premaster_secret);
1810 premaster_secret = new_data;
1811 premaster_secret_len = new_len;
1812 }
1813
1814 /* Compute the master secret */
David Benjamin4969cc92016-04-22 15:02:23 -04001815 ssl->session->master_key_length = tls1_generate_master_secret(
Adam Langley4139edb2016-01-13 15:00:54 -08001816 ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
1817 if (ssl->session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001818 goto err;
1819 }
Adam Langley4139edb2016-01-13 15:00:54 -08001820 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001821
1822 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1823 OPENSSL_free(premaster_secret);
1824 return 1;
1825
1826f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001827 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001828err:
Adam Langley4139edb2016-01-13 15:00:54 -08001829 if (premaster_secret != NULL) {
1830 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001831 OPENSSL_free(premaster_secret);
1832 }
Adam Langleye9ada862015-05-11 17:20:37 -07001833 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001834
1835 return -1;
1836}
1837
David Benjamin6e899c72016-06-09 18:02:18 -04001838static int ssl3_get_cert_verify(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001839 int al, ok, ret = 0;
1840 long n;
1841 CBS certificate_verify, signature;
Adam Langley4139edb2016-01-13 15:00:54 -08001842 X509 *peer = ssl->session->peer;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001843 EVP_PKEY *pkey = NULL;
1844 const EVP_MD *md = NULL;
1845 uint8_t digest[EVP_MAX_MD_SIZE];
1846 size_t digest_length;
1847 EVP_PKEY_CTX *pctx = NULL;
1848
1849 /* Only RSA and ECDSA client certificates are supported, so a
1850 * CertificateVerify is required if and only if there's a client certificate.
1851 * */
1852 if (peer == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001853 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001854 return 1;
1855 }
1856
David Benjamind316cba2016-06-02 16:17:39 -04001857 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1858 ssl_dont_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001859
1860 if (!ok) {
1861 return n;
1862 }
1863
1864 /* Filter out unsupported certificate types. */
1865 pkey = X509_get_pubkey(peer);
Adam Langleye9ada862015-05-11 17:20:37 -07001866 if (pkey == NULL) {
1867 goto err;
1868 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001869 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
1870 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
1871 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00001872 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001873 goto f_err;
1874 }
1875
Adam Langley4139edb2016-01-13 15:00:54 -08001876 CBS_init(&certificate_verify, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001877
1878 /* Determine the digest type if needbe. */
David Benjamin4969cc92016-04-22 15:02:23 -04001879 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyfad63272015-11-12 12:15:39 -08001880 uint8_t hash, signature_type;
1881 if (!CBS_get_u8(&certificate_verify, &hash) ||
1882 !CBS_get_u8(&certificate_verify, &signature_type)) {
1883 al = SSL_AD_DECODE_ERROR;
1884 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1885 goto f_err;
1886 }
Adam Langley4139edb2016-01-13 15:00:54 -08001887 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001888 goto f_err;
1889 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001890 }
1891
1892 /* Compute the digest. */
Adam Langley4139edb2016-01-13 15:00:54 -08001893 if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001894 goto err;
1895 }
1896
1897 /* The handshake buffer is no longer necessary, and we may hash the current
1898 * message.*/
Adam Langley4139edb2016-01-13 15:00:54 -08001899 ssl3_free_handshake_buffer(ssl);
1900 if (!ssl3_hash_current_message(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001901 goto err;
1902 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001903
1904 /* Parse and verify the signature. */
1905 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1906 CBS_len(&certificate_verify) != 0) {
1907 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001908 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001909 goto f_err;
1910 }
1911
1912 pctx = EVP_PKEY_CTX_new(pkey, NULL);
1913 if (pctx == NULL) {
1914 goto err;
1915 }
David Benjamin4969cc92016-04-22 15:02:23 -04001916 int sig_ok = EVP_PKEY_verify_init(pctx) &&
1917 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1918 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1919 digest, digest_length);
1920#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1921 sig_ok = 1;
1922 ERR_clear_error();
1923#endif
1924 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001925 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001926 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001927 goto f_err;
1928 }
1929
1930 ret = 1;
1931
1932 if (0) {
1933 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001934 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001935 }
1936
1937err:
1938 EVP_PKEY_CTX_free(pctx);
1939 EVP_PKEY_free(pkey);
1940
1941 return ret;
1942}
1943
David Benjamin6e899c72016-06-09 18:02:18 -04001944/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1945 * sets the next_proto member in s if found */
1946static int ssl3_get_next_proto(SSL *ssl) {
1947 int ok;
1948 long n;
1949 CBS next_protocol, selected_protocol, padding;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001950
David Benjamin6e899c72016-06-09 18:02:18 -04001951 /* Clients cannot send a NextProtocol message if we didn't see the extension
1952 * in their ClientHello */
1953 if (!ssl->s3->next_proto_neg_seen) {
1954 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
1955 return -1;
1956 }
1957
1958 n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message,
1959 &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001960
1961 if (!ok) {
1962 return n;
1963 }
1964
David Benjamin6e899c72016-06-09 18:02:18 -04001965 CBS_init(&next_protocol, ssl->init_msg, n);
David Benjamin4969cc92016-04-22 15:02:23 -04001966
David Benjamin6e899c72016-06-09 18:02:18 -04001967 /* The payload looks like:
1968 * uint8 proto_len;
1969 * uint8 proto[proto_len];
1970 * uint8 padding_len;
1971 * uint8 padding[padding_len]; */
1972 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1973 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
1974 CBS_len(&next_protocol) != 0 ||
1975 !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
1976 &ssl->s3->next_proto_negotiated_len)) {
1977 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001978 }
1979
David Benjamin6e899c72016-06-09 18:02:18 -04001980 return 1;
1981}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001982
David Benjamin6e899c72016-06-09 18:02:18 -04001983/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
1984static int ssl3_get_channel_id(SSL *ssl) {
1985 int ret = -1, ok;
1986 long n;
1987 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
1988 size_t channel_id_hash_len;
1989 const uint8_t *p;
1990 uint16_t extension_type;
1991 EC_GROUP *p256 = NULL;
1992 EC_KEY *key = NULL;
1993 EC_POINT *point = NULL;
1994 ECDSA_SIG sig;
1995 BIGNUM x, y;
1996 CBS encrypted_extensions, extension;
1997
1998 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS,
1999 ssl_dont_hash_message, &ok);
2000
2001 if (!ok) {
2002 return n;
2003 }
2004
2005 /* Before incorporating the EncryptedExtensions message to the handshake
2006 * hash, compute the hash that should have been signed. */
2007 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
2008 return -1;
2009 }
2010 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
2011
2012 if (!ssl3_hash_current_message(ssl)) {
2013 return -1;
2014 }
2015
2016 CBS_init(&encrypted_extensions, ssl->init_msg, n);
2017
2018 /* EncryptedExtensions could include multiple extensions, but the only
2019 * extension that could be negotiated is ChannelID, so there can only be one
2020 * entry.
2021 *
2022 * The payload looks like:
2023 * uint16 extension_type
2024 * uint16 extension_len;
2025 * uint8 x[32];
2026 * uint8 y[32];
2027 * uint8 r[32];
2028 * uint8 s[32]; */
2029
2030 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2031 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2032 CBS_len(&encrypted_extensions) != 0 ||
2033 extension_type != TLSEXT_TYPE_channel_id ||
2034 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2035 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
2036 return -1;
2037 }
2038
2039 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2040 if (!p256) {
2041 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
2042 return -1;
2043 }
2044
2045 BN_init(&x);
2046 BN_init(&y);
2047 sig.r = BN_new();
2048 sig.s = BN_new();
2049 if (sig.r == NULL || sig.s == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002050 goto err;
2051 }
2052
David Benjamin6e899c72016-06-09 18:02:18 -04002053 p = CBS_data(&extension);
2054 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2055 BN_bin2bn(p + 32, 32, &y) == NULL ||
2056 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2057 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2058 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002059 }
2060
David Benjamin6e899c72016-06-09 18:02:18 -04002061 point = EC_POINT_new(p256);
2062 if (!point ||
2063 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2064 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002065 }
2066
David Benjamin6e899c72016-06-09 18:02:18 -04002067 key = EC_KEY_new();
2068 if (!key || !EC_KEY_set_group(key, p256) ||
2069 !EC_KEY_set_public_key(key, point)) {
2070 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002071 }
2072
David Benjamin6e899c72016-06-09 18:02:18 -04002073 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2074 * were called. */
2075 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2076 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2077 ssl->s3->tlsext_channel_id_valid = 0;
2078 goto err;
2079 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002080
David Benjamin6e899c72016-06-09 18:02:18 -04002081 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002082 ret = 1;
2083
Adam Langleyd9e397b2015-01-22 14:27:53 -08002084err:
David Benjamin6e899c72016-06-09 18:02:18 -04002085 BN_free(&x);
2086 BN_free(&y);
2087 BN_free(sig.r);
2088 BN_free(sig.s);
2089 EC_KEY_free(key);
2090 EC_POINT_free(point);
2091 EC_GROUP_free(p256);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002092 return ret;
2093}
2094
Adam Langleyd9e397b2015-01-22 14:27:53 -08002095/* send a new session ticket (not necessarily for a new session) */
David Benjamin6e899c72016-06-09 18:02:18 -04002096static int ssl3_send_new_session_ticket(SSL *ssl) {
Adam Langleye9ada862015-05-11 17:20:37 -07002097 int ret = -1;
2098 uint8_t *session = NULL;
2099 size_t session_len;
2100 EVP_CIPHER_CTX ctx;
2101 HMAC_CTX hctx;
2102
2103 EVP_CIPHER_CTX_init(&ctx);
2104 HMAC_CTX_init(&hctx);
2105
Adam Langley4139edb2016-01-13 15:00:54 -08002106 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002107 uint8_t *p, *macstart;
2108 int len;
2109 unsigned int hlen;
Adam Langley4139edb2016-01-13 15:00:54 -08002110 SSL_CTX *tctx = ssl->initial_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002111 uint8_t iv[EVP_MAX_IV_LENGTH];
2112 uint8_t key_name[16];
2113 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2114 * one block of encryption overhead + HMAC. */
2115 const size_t max_ticket_overhead =
2116 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
2117
2118 /* Serialize the SSL_SESSION to be encoded into the ticket. */
Adam Langley4139edb2016-01-13 15:00:54 -08002119 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
2120 &session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002121 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002122 }
2123
2124 /* If the session is too long, emit a dummy value rather than abort the
2125 * connection. */
2126 if (session_len > 0xFFFF - max_ticket_overhead) {
2127 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2128 const size_t placeholder_len = strlen(kTicketPlaceholder);
2129
2130 OPENSSL_free(session);
Adam Langleye9ada862015-05-11 17:20:37 -07002131 session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002132
Adam Langley4139edb2016-01-13 15:00:54 -08002133 p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002134 /* Emit ticket_lifetime_hint. */
2135 l2n(0, p);
2136 /* Emit ticket. */
2137 s2n(placeholder_len, p);
2138 memcpy(p, kTicketPlaceholder, placeholder_len);
2139 p += placeholder_len;
2140
Adam Langley4139edb2016-01-13 15:00:54 -08002141 len = p - ssl_handshake_start(ssl);
David Benjamind316cba2016-06-02 16:17:39 -04002142 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002143 goto err;
2144 }
Adam Langley4139edb2016-01-13 15:00:54 -08002145 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
2146 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002147 }
2148
2149 /* Grow buffer if need be: the length calculation is as follows:
2150 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2151 * max_ticket_overhead + * session_length */
Adam Langley4139edb2016-01-13 15:00:54 -08002152 if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
Adam Langleyd9e397b2015-01-22 14:27:53 -08002153 max_ticket_overhead + session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002154 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002155 }
Adam Langley4139edb2016-01-13 15:00:54 -08002156 p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002157 /* Initialize HMAC and cipher contexts. If callback present it does all the
2158 * work otherwise use generated values from parent ctx. */
2159 if (tctx->tlsext_ticket_key_cb) {
Adam Langley4139edb2016-01-13 15:00:54 -08002160 if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
Adam Langleye9ada862015-05-11 17:20:37 -07002161 1 /* encrypt */) < 0) {
2162 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002163 }
2164 } else {
2165 if (!RAND_bytes(iv, 16) ||
2166 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2167 tctx->tlsext_tick_aes_key, iv) ||
2168 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2169 NULL)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002170 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002171 }
2172 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2173 }
2174
2175 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2176 * resumed session (for simplicity), and guess that tickets for new
2177 * sessions will live as long as their sessions. */
Adam Langley4139edb2016-01-13 15:00:54 -08002178 l2n(ssl->hit ? 0 : ssl->session->timeout, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002179
2180 /* Skip ticket length for now */
2181 p += 2;
2182 /* Output key name */
2183 macstart = p;
2184 memcpy(p, key_name, 16);
2185 p += 16;
2186 /* output IV */
2187 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2188 p += EVP_CIPHER_CTX_iv_length(&ctx);
2189 /* Encrypt session data */
Adam Langleye9ada862015-05-11 17:20:37 -07002190 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2191 goto err;
2192 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002193 p += len;
Adam Langleye9ada862015-05-11 17:20:37 -07002194 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2195 goto err;
2196 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002197 p += len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002198
Adam Langleye9ada862015-05-11 17:20:37 -07002199 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2200 !HMAC_Final(&hctx, p, &hlen)) {
2201 goto err;
2202 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002203
2204 p += hlen;
2205 /* Now write out lengths: p points to end of data written */
2206 /* Total length */
Adam Langley4139edb2016-01-13 15:00:54 -08002207 len = p - ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002208 /* Skip ticket lifetime hint */
Adam Langley4139edb2016-01-13 15:00:54 -08002209 p = ssl_handshake_start(ssl) + 4;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002210 s2n(len - 6, p);
David Benjamind316cba2016-06-02 16:17:39 -04002211 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002212 goto err;
2213 }
Adam Langley4139edb2016-01-13 15:00:54 -08002214 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002215 }
2216
2217 /* SSL3_ST_SW_SESSION_TICKET_B */
Adam Langley4139edb2016-01-13 15:00:54 -08002218 ret = ssl_do_write(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -07002219
2220err:
2221 OPENSSL_free(session);
2222 EVP_CIPHER_CTX_cleanup(&ctx);
2223 HMAC_CTX_cleanup(&hctx);
2224 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002225}