blob: d57735a9c4eb6db23fa5711cee0fb164cdc5a0bc [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
Kenny Rootb8494592015-09-25 02:29:14 +0000149#include <openssl/ssl.h>
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <string.h>
153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
156#include <openssl/bytestring.h>
157#include <openssl/cipher.h>
158#include <openssl/dh.h>
159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700161#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400166#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168#include <openssl/x509.h>
169
Adam Langleye9ada862015-05-11 17:20:37 -0700170#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172
173
David Benjamin6e899c72016-06-09 18:02:18 -0400174static int ssl3_get_client_hello(SSL *ssl);
175static int ssl3_send_server_hello(SSL *ssl);
176static int ssl3_send_server_certificate(SSL *ssl);
177static int ssl3_send_certificate_status(SSL *ssl);
178static int ssl3_send_server_key_exchange(SSL *ssl);
179static int ssl3_send_certificate_request(SSL *ssl);
180static int ssl3_send_server_hello_done(SSL *ssl);
181static int ssl3_get_client_certificate(SSL *ssl);
182static int ssl3_get_client_key_exchange(SSL *ssl);
183static int ssl3_get_cert_verify(SSL *ssl);
184static int ssl3_get_next_proto(SSL *ssl);
185static int ssl3_get_channel_id(SSL *ssl);
186static int ssl3_send_new_session_ticket(SSL *ssl);
187
Adam Langley4139edb2016-01-13 15:00:54 -0800188int ssl3_accept(SSL *ssl) {
Adam Langleye9ada862015-05-11 17:20:37 -0700189 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400191 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192
Adam Langley4139edb2016-01-13 15:00:54 -0800193 assert(ssl->handshake_func == ssl3_accept);
194 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195
196 for (;;) {
Adam Langley4139edb2016-01-13 15:00:54 -0800197 state = ssl->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198
Adam Langley4139edb2016-01-13 15:00:54 -0800199 switch (ssl->state) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400200 case SSL_ST_INIT:
201 ssl->state = SSL_ST_ACCEPT;
202 skip = 1;
203 break;
204
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
David Benjaminc895d6b2016-08-11 13:26:41 -0400208 ssl->s3->hs = ssl_handshake_new(tls13_server_handshake);
209 if (ssl->s3->hs == NULL) {
210 ret = -1;
211 goto end;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800213
Adam Langleye9ada862015-05-11 17:20:37 -0700214 /* Enable a write buffer. This groups handshake messages within a flight
215 * into a single write. */
David Benjamind316cba2016-06-02 16:17:39 -0400216 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700217 ret = -1;
218 goto end;
219 }
220
Adam Langley4139edb2016-01-13 15:00:54 -0800221 if (!ssl3_init_handshake_buffer(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000222 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223 ret = -1;
224 goto end;
225 }
226
Adam Langley4139edb2016-01-13 15:00:54 -0800227 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 break;
229
230 case SSL3_ST_SR_CLNT_HELLO_A:
231 case SSL3_ST_SR_CLNT_HELLO_B:
232 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjaminc895d6b2016-08-11 13:26:41 -0400233 case SSL3_ST_SR_CLNT_HELLO_D:
Adam Langley4139edb2016-01-13 15:00:54 -0800234 ret = ssl3_get_client_hello(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400235 if (ssl->state == SSL_ST_TLS13) {
236 break;
237 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 if (ret <= 0) {
239 goto end;
240 }
David Benjamin6e899c72016-06-09 18:02:18 -0400241 ssl->method->received_flight(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800242 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 break;
244
245 case SSL3_ST_SW_SRVR_HELLO_A:
246 case SSL3_ST_SW_SRVR_HELLO_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800247 ret = ssl3_send_server_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 if (ret <= 0) {
249 goto end;
250 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400251 if (ssl->session != NULL) {
252 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800254 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 break;
257
258 case SSL3_ST_SW_CERT_A:
259 case SSL3_ST_SW_CERT_B:
David Benjamind316cba2016-06-02 16:17:39 -0400260 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800261 ret = ssl3_send_server_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 if (ret <= 0) {
263 goto end;
264 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265 } else {
266 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400268 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269 break;
270
Kenny Rootb8494592015-09-25 02:29:14 +0000271 case SSL3_ST_SW_CERT_STATUS_A:
272 case SSL3_ST_SW_CERT_STATUS_B:
David Benjaminc895d6b2016-08-11 13:26:41 -0400273 if (ssl->s3->tmp.certificate_status_expected) {
274 ret = ssl3_send_certificate_status(ssl);
275 if (ret <= 0) {
276 goto end;
277 }
278 } else {
279 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000280 }
Adam Langley4139edb2016-01-13 15:00:54 -0800281 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000282 break;
283
Adam Langleyd9e397b2015-01-22 14:27:53 -0800284 case SSL3_ST_SW_KEY_EXCH_A:
285 case SSL3_ST_SW_KEY_EXCH_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000286 case SSL3_ST_SW_KEY_EXCH_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800287 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800288
David Benjamin6e899c72016-06-09 18:02:18 -0400289 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
Adam Langley4139edb2016-01-13 15:00:54 -0800290 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
291 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
292 ret = ssl3_send_server_key_exchange(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -0700293 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800294 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700295 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 } else {
297 skip = 1;
298 }
299
Adam Langley4139edb2016-01-13 15:00:54 -0800300 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301 break;
302
303 case SSL3_ST_SW_CERT_REQ_A:
304 case SSL3_ST_SW_CERT_REQ_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800305 if (ssl->s3->tmp.cert_request) {
306 ret = ssl3_send_certificate_request(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800307 if (ret <= 0) {
308 goto end;
309 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700310 } else {
311 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800312 }
Adam Langley4139edb2016-01-13 15:00:54 -0800313 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800314 break;
315
316 case SSL3_ST_SW_SRVR_DONE_A:
317 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin6e899c72016-06-09 18:02:18 -0400318 ret = ssl3_send_server_hello_done(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 if (ret <= 0) {
320 goto end;
321 }
Adam Langley4139edb2016-01-13 15:00:54 -0800322 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
323 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800324 break;
325
Adam Langleyd9e397b2015-01-22 14:27:53 -0800326 case SSL3_ST_SR_CERT_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800327 if (ssl->s3->tmp.cert_request) {
328 ret = ssl3_get_client_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 if (ret <= 0) {
330 goto end;
331 }
332 }
Adam Langley4139edb2016-01-13 15:00:54 -0800333 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 break;
335
336 case SSL3_ST_SR_KEY_EXCH_A:
337 case SSL3_ST_SR_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800338 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339 if (ret <= 0) {
340 goto end;
341 }
Adam Langley4139edb2016-01-13 15:00:54 -0800342 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343 break;
344
345 case SSL3_ST_SR_CERT_VRFY_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800346 ret = ssl3_get_cert_verify(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800347 if (ret <= 0) {
348 goto end;
349 }
350
Adam Langley4139edb2016-01-13 15:00:54 -0800351 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 break;
353
Adam Langley4139edb2016-01-13 15:00:54 -0800354 case SSL3_ST_SR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400355 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800356 if (ret <= 0) {
357 goto end;
358 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359
David Benjamin4969cc92016-04-22 15:02:23 -0400360 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361 ret = -1;
362 goto end;
363 }
Adam Langley4139edb2016-01-13 15:00:54 -0800364
David Benjaminc895d6b2016-08-11 13:26:41 -0400365 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800366 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367
368 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400369 if (ssl->s3->next_proto_neg_seen) {
370 ret = ssl3_get_next_proto(ssl);
371 if (ret <= 0) {
372 goto end;
373 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400375 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400377 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800378 break;
379
380 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400381 if (ssl->s3->tlsext_channel_id_valid) {
382 ret = ssl3_get_channel_id(ssl);
383 if (ret <= 0) {
384 goto end;
385 }
386 } else {
387 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800388 }
Adam Langley4139edb2016-01-13 15:00:54 -0800389 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390 break;
391
392 case SSL3_ST_SR_FINISHED_A:
David Benjamind316cba2016-06-02 16:17:39 -0400393 ret = ssl3_get_finished(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394 if (ret <= 0) {
395 goto end;
396 }
397
David Benjamin6e899c72016-06-09 18:02:18 -0400398 ssl->method->received_flight(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400399 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800400 ssl->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800401 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400402 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 }
David Benjamin6e899c72016-06-09 18:02:18 -0400404
David Benjaminc895d6b2016-08-11 13:26:41 -0400405 /* If this is a full handshake with ChannelID then record the handshake
406 * hashes in |ssl->s3->new_session| in case we need them to verify a
407 * ChannelID signature on a resumption of this session in the future. */
408 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Adam Langley4139edb2016-01-13 15:00:54 -0800409 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410 if (ret <= 0) {
411 goto end;
412 }
413 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800414 break;
415
416 case SSL3_ST_SW_SESSION_TICKET_A:
417 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjaminc895d6b2016-08-11 13:26:41 -0400418 if (ssl->tlsext_ticket_expected) {
419 ret = ssl3_send_new_session_ticket(ssl);
420 if (ret <= 0) {
421 goto end;
422 }
423 } else {
424 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400426 ssl->state = SSL3_ST_SW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800427 break;
428
David Benjaminc895d6b2016-08-11 13:26:41 -0400429 case SSL3_ST_SW_CHANGE:
430 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800431 if (ret <= 0) {
432 goto end;
433 }
Adam Langley4139edb2016-01-13 15:00:54 -0800434 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800435
David Benjamin4969cc92016-04-22 15:02:23 -0400436 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800437 ret = -1;
438 goto end;
439 }
440 break;
441
442 case SSL3_ST_SW_FINISHED_A:
443 case SSL3_ST_SW_FINISHED_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800444 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjamin4969cc92016-04-22 15:02:23 -0400445 SSL3_ST_SW_FINISHED_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800446 if (ret <= 0) {
447 goto end;
448 }
Adam Langley4139edb2016-01-13 15:00:54 -0800449 ssl->state = SSL3_ST_SW_FLUSH;
David Benjaminc895d6b2016-08-11 13:26:41 -0400450 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800451 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800452 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800453 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800454 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800455 break;
456
David Benjamin6e899c72016-06-09 18:02:18 -0400457 case SSL3_ST_SW_FLUSH:
458 if (BIO_flush(ssl->wbio) <= 0) {
459 ssl->rwstate = SSL_WRITING;
460 ret = -1;
461 goto end;
462 }
463
464 ssl->state = ssl->s3->tmp.next_state;
465 if (ssl->state != SSL_ST_OK) {
466 ssl->method->expect_flight(ssl);
467 }
468 break;
469
David Benjaminc895d6b2016-08-11 13:26:41 -0400470 case SSL_ST_TLS13:
471 ret = tls13_handshake(ssl);
472 if (ret <= 0) {
473 goto end;
474 }
475 ssl->state = SSL_ST_OK;
476 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477
David Benjaminc895d6b2016-08-11 13:26:41 -0400478 case SSL_ST_OK:
479 /* Clean a few things up. */
480 ssl3_cleanup_key_block(ssl);
481 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
482
483 /* If we aren't retaining peer certificates then we can discard it
484 * now. */
485 if (ssl->s3->new_session != NULL &&
486 ssl->ctx->retain_only_sha256_of_client_certs) {
487 X509_free(ssl->s3->new_session->peer);
488 ssl->s3->new_session->peer = NULL;
489 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
490 ssl->s3->new_session->cert_chain = NULL;
491 }
492
493 SSL_SESSION_free(ssl->s3->established_session);
494 if (ssl->session != NULL) {
495 SSL_SESSION_up_ref(ssl->session);
496 ssl->s3->established_session = ssl->session;
497 } else {
498 ssl->s3->established_session = ssl->s3->new_session;
499 ssl->s3->established_session->not_resumable = 0;
500 ssl->s3->new_session = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400501 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502
503 /* remove buffering on output */
Adam Langley4139edb2016-01-13 15:00:54 -0800504 ssl_free_wbio_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800505
David Benjaminc895d6b2016-08-11 13:26:41 -0400506 ssl_handshake_free(ssl->s3->hs);
507 ssl->s3->hs = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400508
Adam Langley4139edb2016-01-13 15:00:54 -0800509 ssl->s3->initial_handshake_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800510
Adam Langley4139edb2016-01-13 15:00:54 -0800511 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800512
David Benjamin6e899c72016-06-09 18:02:18 -0400513 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800514
515 ret = 1;
516 goto end;
517
518 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000519 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800520 ret = -1;
521 goto end;
522 }
523
David Benjamin6e899c72016-06-09 18:02:18 -0400524 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
525 int new_state = ssl->state;
Adam Langley4139edb2016-01-13 15:00:54 -0800526 ssl->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400527 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langley4139edb2016-01-13 15:00:54 -0800528 ssl->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800529 }
530 skip = 0;
531 }
532
533end:
David Benjamin6e899c72016-06-09 18:02:18 -0400534 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800535 return ret;
536}
537
David Benjaminc895d6b2016-08-11 13:26:41 -0400538int ssl_client_cipher_list_contains_cipher(
539 const struct ssl_early_callback_ctx *client_hello, uint16_t id) {
540 CBS cipher_suites;
541 CBS_init(&cipher_suites, client_hello->cipher_suites,
542 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800543
David Benjaminc895d6b2016-08-11 13:26:41 -0400544 while (CBS_len(&cipher_suites) > 0) {
545 uint16_t got_id;
546 if (!CBS_get_u16(&cipher_suites, &got_id)) {
547 return 0;
548 }
549
550 if (got_id == id) {
551 return 1;
552 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800553 }
554
David Benjaminc895d6b2016-08-11 13:26:41 -0400555 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800556}
557
David Benjaminc895d6b2016-08-11 13:26:41 -0400558static int negotiate_version(
559 SSL *ssl, int *out_alert,
560 const struct ssl_early_callback_ctx *client_hello) {
561 uint16_t min_version, max_version;
562 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
563 *out_alert = SSL_AD_PROTOCOL_VERSION;
564 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800565 }
566
David Benjamin7c0d06c2016-08-11 13:26:41 -0400567 /* For TLS versions which use ClientHello.version, convert it to a version we
568 * are aware of. */
569 uint16_t version = 0;
570 if (SSL_is_dtls(ssl)) {
571 if (client_hello->version <= DTLS1_2_VERSION) {
572 version = TLS1_2_VERSION;
573 } else if (client_hello->version <= DTLS1_VERSION) {
574 version = TLS1_1_VERSION;
575 }
576 } else {
577 if (client_hello->version >= TLS1_3_VERSION) {
578 version = TLS1_3_VERSION;
579 } else if (client_hello->version >= TLS1_2_VERSION) {
580 version = TLS1_2_VERSION;
581 } else if (client_hello->version >= TLS1_1_VERSION) {
582 version = TLS1_1_VERSION;
583 } else if (client_hello->version >= TLS1_VERSION) {
584 version = TLS1_VERSION;
585 } else if (client_hello->version >= SSL3_VERSION) {
586 version = SSL3_VERSION;
587 }
588 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800589
David Benjamin7c0d06c2016-08-11 13:26:41 -0400590 /* Apply our minimum and maximum version. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400591 if (version > max_version) {
592 version = max_version;
Adam Langleye9ada862015-05-11 17:20:37 -0700593 }
David Benjamin6e899c72016-06-09 18:02:18 -0400594
David Benjaminc895d6b2016-08-11 13:26:41 -0400595 if (version < min_version) {
596 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
597 *out_alert = SSL_AD_PROTOCOL_VERSION;
598 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800599 }
600
David Benjaminc895d6b2016-08-11 13:26:41 -0400601 /* Handle FALLBACK_SCSV. */
602 if (ssl_client_cipher_list_contains_cipher(client_hello,
603 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
604 version < max_version) {
605 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
606 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
607 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800608 }
609
David Benjamin7c0d06c2016-08-11 13:26:41 -0400610 ssl->client_version = client_hello->version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400611 ssl->version = ssl->method->version_to_wire(version);
612 ssl->s3->enc_method = ssl3_get_enc_method(version);
613 assert(ssl->s3->enc_method != NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800614
David Benjaminc895d6b2016-08-11 13:26:41 -0400615 /* At this point, the connection's version is known and |ssl->version| is
616 * fixed. Begin enforcing the record-layer version. */
617 ssl->s3->have_version = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800618
619 return 1;
620}
621
David Benjamin6e899c72016-06-09 18:02:18 -0400622static int ssl3_get_client_hello(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400623 int al = SSL_AD_INTERNAL_ERROR, ret = -1;
Kenny Rootb8494592015-09-25 02:29:14 +0000624 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800625
David Benjaminc895d6b2016-08-11 13:26:41 -0400626 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) {
627 /* The first time around, read the ClientHello. */
628 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
629 ssl_hash_message);
630 if (msg_ret <= 0) {
631 return msg_ret;
632 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800633
David Benjaminc895d6b2016-08-11 13:26:41 -0400634 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
635 }
636
637 struct ssl_early_callback_ctx client_hello;
638 if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg,
639 ssl->init_num)) {
640 al = SSL_AD_DECODE_ERROR;
641 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
642 goto f_err;
643 }
644
645 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) {
646 /* Unlike other callbacks, the early callback is not run a second time if
647 * paused. */
648 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
649
650 /* Run the early callback. */
651 if (ssl->ctx->select_certificate_cb != NULL) {
652 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
653 case 0:
654 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
655 goto err;
656
657 case -1:
658 /* Connection rejected. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400659 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjaminc895d6b2016-08-11 13:26:41 -0400660 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
661 goto f_err;
662
663 default:
664 /* fallthrough */;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800665 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400666 }
667 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800668
David Benjaminc895d6b2016-08-11 13:26:41 -0400669 /* Negotiate the protocol version if we have not done so yet. */
670 if (!ssl->s3->have_version) {
671 if (!negotiate_version(ssl, &al, &client_hello)) {
672 goto f_err;
673 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674
David Benjaminc895d6b2016-08-11 13:26:41 -0400675 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
676 ssl->state = SSL_ST_TLS13;
677 return 1;
678 }
679 }
680
681 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) {
682 /* Load the client random. */
683 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
684 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
685 return -1;
686 }
687 memcpy(ssl->s3->client_random, client_hello.random,
688 client_hello.random_len);
689
690 /* Determine whether we are doing session resumption. */
691 int send_new_ticket = 0;
692 switch (
693 ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) {
694 case ssl_session_success:
695 break;
696 case ssl_session_error:
697 goto err;
698 case ssl_session_retry:
699 ssl->rwstate = SSL_PENDING_SESSION;
700 goto err;
701 }
702 ssl->tlsext_ticket_expected = send_new_ticket;
703
704 /* The EMS state is needed when making the resumption decision, but
705 * extensions are not normally parsed until later. This detects the EMS
706 * extension for the resumption decision and it's checked against the result
707 * of the normal parse later in this function. */
708 CBS ems;
709 int have_extended_master_secret =
710 ssl->version != SSL3_VERSION &&
711 ssl_early_callback_get_extension(&client_hello, &ems,
712 TLSEXT_TYPE_extended_master_secret) &&
713 CBS_len(&ems) == 0;
714
715 int has_session = 0;
716 if (session != NULL) {
717 if (session->extended_master_secret &&
718 !have_extended_master_secret) {
719 /* A ClientHello without EMS that attempts to resume a session with EMS
720 * is fatal to the connection. */
721 al = SSL_AD_HANDSHAKE_FAILURE;
722 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800723 goto f_err;
724 }
725
David Benjaminc895d6b2016-08-11 13:26:41 -0400726 has_session =
727 /* Only resume if the session's version matches the negotiated
728 * version: most clients do not accept a mismatch. */
729 ssl->version == session->ssl_version &&
730 /* If the client offers the EMS extension, but the previous session
731 * didn't use it, then negotiate a new session. */
732 have_extended_master_secret == session->extended_master_secret;
733 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800734
David Benjaminc895d6b2016-08-11 13:26:41 -0400735 if (has_session) {
736 /* Use the old session. */
737 ssl->session = session;
738 session = NULL;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400739 ssl->s3->session_reused = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400740 } else {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400741 ssl_set_session(ssl, NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400742 if (!ssl_get_new_session(ssl, 1 /* server */)) {
743 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800744 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800745
David Benjaminc895d6b2016-08-11 13:26:41 -0400746 /* Clear the session ID if we want the session to be single-use. */
747 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
748 ssl->s3->new_session->session_id_length = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800749 }
750 }
751
David Benjaminc895d6b2016-08-11 13:26:41 -0400752 if (ssl->ctx->dos_protection_cb != NULL &&
753 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
754 /* Connection rejected for DOS reasons. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400755 al = SSL_AD_INTERNAL_ERROR;
David Benjaminc895d6b2016-08-11 13:26:41 -0400756 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
757 goto f_err;
758 }
759
760 /* Only null compression is supported. */
761 if (memchr(client_hello.compression_methods, 0,
762 client_hello.compression_methods_len) == NULL) {
763 al = SSL_AD_ILLEGAL_PARAMETER;
764 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
765 goto f_err;
766 }
767
768 /* TLS extensions. */
769 if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
770 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
771 goto err;
772 }
773
774 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
775 al = SSL_AD_INTERNAL_ERROR;
776 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
777 goto f_err;
778 }
779
780 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
781 }
782
783 /* Determine the remaining connection parameters. This is a separate state so
784 * |cert_cb| does not cause earlier logic to run multiple times. */
785 assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D);
786
787 if (ssl->session != NULL) {
788 /* Check that the cipher is in the list. */
789 if (!ssl_client_cipher_list_contains_cipher(
790 &client_hello, (uint16_t)ssl->session->cipher->id)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800791 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000792 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800793 goto f_err;
794 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800795
David Benjaminc895d6b2016-08-11 13:26:41 -0400796 ssl->s3->tmp.new_cipher = ssl->session->cipher;
797 ssl->s3->tmp.cert_request = 0;
798 } else {
799 /* Call |cert_cb| to update server certificates if required. */
800 if (ssl->cert->cert_cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800801 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800802 if (rv == 0) {
803 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000804 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800805 goto f_err;
806 }
807 if (rv < 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800808 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800809 goto err;
810 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800811 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800812
David Benjaminc895d6b2016-08-11 13:26:41 -0400813 const SSL_CIPHER *c =
814 ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800815 if (c == NULL) {
816 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000817 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800818 goto f_err;
819 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400820
821 ssl->s3->new_session->cipher = c;
Adam Langley4139edb2016-01-13 15:00:54 -0800822 ssl->s3->tmp.new_cipher = c;
Adam Langleyf4e42722015-06-04 17:45:09 -0700823
824 /* Determine whether to request a client certificate. */
Adam Langley4139edb2016-01-13 15:00:54 -0800825 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -0700826 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -0800827 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
828 ssl->s3->tlsext_channel_id_valid) {
829 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -0700830 }
David Benjamind316cba2016-06-02 16:17:39 -0400831 /* CertificateRequest may only be sent in certificate-based ciphers. */
832 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800833 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -0700834 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400835
836 if (!ssl->s3->tmp.cert_request) {
837 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
838 * classed by them as a bug, but it's assumed by at least NGINX. */
839 ssl->s3->new_session->verify_result = X509_V_OK;
840 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800841 }
842
Kenny Rootb8494592015-09-25 02:29:14 +0000843 /* Now that the cipher is known, initialize the handshake hash. */
Adam Langley4139edb2016-01-13 15:00:54 -0800844 if (!ssl3_init_handshake_hash(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000845 goto f_err;
846 }
847
David Benjaminc895d6b2016-08-11 13:26:41 -0400848 /* Release the handshake buffer if client authentication isn't required. */
849 if (!ssl->s3->tmp.cert_request) {
Adam Langley4139edb2016-01-13 15:00:54 -0800850 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800851 }
852
Adam Langley4139edb2016-01-13 15:00:54 -0800853 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800854
855 if (0) {
856 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -0800857 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800858 }
859
860err:
Kenny Rootb8494592015-09-25 02:29:14 +0000861 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800862 return ret;
863}
864
David Benjamin6e899c72016-06-09 18:02:18 -0400865static int ssl3_send_server_hello(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -0800866 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400867 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800868 }
869
Kenny Roote99801b2015-11-06 15:31:15 -0800870 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
871
872 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
873 * known attack while we fix ChannelID itself. */
874 if (ssl->s3->tlsext_channel_id_valid &&
875 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
876 ssl->s3->tlsext_channel_id_valid = 0;
877 }
878
879 /* If this is a resumption and the original handshake didn't support
880 * ChannelID then we didn't record the original handshake hashes in the
881 * session and so cannot resume with ChannelIDs. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400882 if (ssl->session != NULL &&
883 ssl->session->original_handshake_hash_len == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -0800884 ssl->s3->tlsext_channel_id_valid = 0;
885 }
886
David Benjaminc895d6b2016-08-11 13:26:41 -0400887 struct timeval now;
888 ssl_get_current_time(ssl, &now);
889 ssl->s3->server_random[0] = now.tv_sec >> 24;
890 ssl->s3->server_random[1] = now.tv_sec >> 16;
891 ssl->s3->server_random[2] = now.tv_sec >> 8;
892 ssl->s3->server_random[3] = now.tv_sec;
893 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800894 return -1;
895 }
896
David Benjamin7c0d06c2016-08-11 13:26:41 -0400897 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
898 * 1.3 is finalized and we are not implementing a draft version. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400899
900 const SSL_SESSION *session = ssl->s3->new_session;
901 if (ssl->session != NULL) {
902 session = ssl->session;
903 }
904
905 CBB cbb, body, session_id;
906 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
907 !CBB_add_u16(&body, ssl->version) ||
908 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
909 !CBB_add_u8_length_prefixed(&body, &session_id) ||
910 !CBB_add_bytes(&session_id, session->session_id,
911 session->session_id_length) ||
912 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
913 !CBB_add_u8(&body, 0 /* no compression */) ||
914 !ssl_add_serverhello_tlsext(ssl, &body) ||
915 !ssl->method->finish_message(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800916 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
917 CBB_cleanup(&cbb);
918 return -1;
919 }
920
921 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjaminc895d6b2016-08-11 13:26:41 -0400922 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800923}
924
David Benjamin6e899c72016-06-09 18:02:18 -0400925static int ssl3_send_server_certificate(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400926 if (ssl->state == SSL3_ST_SW_CERT_B) {
927 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -0400928 }
929
David Benjaminc895d6b2016-08-11 13:26:41 -0400930 if (!ssl_has_certificate(ssl)) {
931 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
932 return 0;
933 }
934
935 if (!ssl3_output_cert_chain(ssl)) {
936 return 0;
937 }
938 ssl->state = SSL3_ST_SW_CERT_B;
939 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -0400940}
941
942static int ssl3_send_certificate_status(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400943 if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
944 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000945 }
946
David Benjaminc895d6b2016-08-11 13:26:41 -0400947 CBB cbb, body, ocsp_response;
948 if (!ssl->method->init_message(ssl, &cbb, &body,
949 SSL3_MT_CERTIFICATE_STATUS) ||
950 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
951 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
952 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
953 ssl->ctx->ocsp_response_length) ||
954 !ssl->method->finish_message(ssl, &cbb)) {
955 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
956 CBB_cleanup(&cbb);
957 return -1;
958 }
959
960 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
961 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000962}
963
David Benjamin6e899c72016-06-09 18:02:18 -0400964static int ssl3_send_server_key_exchange(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -0800965 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400966 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000967 }
968
Adam Langley4139edb2016-01-13 15:00:54 -0800969 CBB cbb, child;
David Benjaminc895d6b2016-08-11 13:26:41 -0400970 CBB_zero(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +0000971
David Benjaminc895d6b2016-08-11 13:26:41 -0400972 /* Put together the parameters. */
Adam Langley4139edb2016-01-13 15:00:54 -0800973 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -0800974 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
975 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800976
David Benjaminc895d6b2016-08-11 13:26:41 -0400977 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
978 if (!CBB_init(&cbb, 128)) {
979 goto err;
980 }
981
Adam Langley4139edb2016-01-13 15:00:54 -0800982 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800983 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -0800984 size_t len =
985 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
986 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
987 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
988 len)) {
989 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800990 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800991 }
992
Adam Langleye9ada862015-05-11 17:20:37 -0700993 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -0800994 /* Determine the group to use. */
995 DH *params = ssl->cert->dh_tmp;
996 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
997 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800998 }
Adam Langley4139edb2016-01-13 15:00:54 -0800999 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001000 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001001 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001002 goto err;
1003 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001004 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
Adam Langley4139edb2016-01-13 15:00:54 -08001005
1006 /* Set up DH, generate a key, and emit the public half. */
1007 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001008 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -07001009 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001010 }
1011
Adam Langley4139edb2016-01-13 15:00:54 -08001012 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1013 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1014 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1015 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1016 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1017 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001018 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001019 goto err;
1020 }
Adam Langleye9ada862015-05-11 17:20:37 -07001021 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001022 /* Determine the group to use. */
1023 uint16_t group_id;
1024 if (!tls1_get_shared_group(ssl, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001025 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001026 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001027 goto err;
1028 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001029 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001030
1031 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamind316cba2016-06-02 16:17:39 -04001032 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001033 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001034 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001035 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001036 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1037 goto err;
1038 }
1039 } else if (alg_k & SSL_kCECPQ1) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001040 SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
1041 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001042 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001043 goto err;
1044 }
Adam Langley4139edb2016-01-13 15:00:54 -08001045 } else {
1046 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001047 }
1048
David Benjaminc895d6b2016-08-11 13:26:41 -04001049 size_t len;
1050 if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) ||
1051 len > 0xffffffffu) {
1052 OPENSSL_free(ssl->s3->tmp.server_params);
1053 ssl->s3->tmp.server_params = NULL;
1054 goto err;
1055 }
1056 ssl->s3->tmp.server_params_len = (uint32_t)len;
1057 }
1058
1059 /* Assemble the message. */
1060 CBB body;
1061 if (!ssl->method->init_message(ssl, &cbb, &body,
1062 SSL3_MT_SERVER_KEY_EXCHANGE) ||
1063 !CBB_add_bytes(&body, ssl->s3->tmp.server_params,
1064 ssl->s3->tmp.server_params_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001065 goto err;
1066 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001067
Adam Langley4139edb2016-01-13 15:00:54 -08001068 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001069 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001070 if (!ssl_has_private_key(ssl)) {
1071 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001072 goto err;
1073 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001074
David Benjaminc895d6b2016-08-11 13:26:41 -04001075 /* Determine the signature algorithm. */
1076 uint16_t signature_algorithm;
1077 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1078 goto err;
1079 }
1080 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1081 if (!CBB_add_u16(&body, signature_algorithm)) {
1082 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1083 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1084 goto err;
1085 }
1086 }
1087
1088 /* Add space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001089 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001090 uint8_t *ptr;
1091 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1092 !CBB_reserve(&child, &ptr, max_sig_len)) {
1093 goto err;
1094 }
1095
Adam Langley4139edb2016-01-13 15:00:54 -08001096 size_t sig_len;
1097 enum ssl_private_key_result_t sign_result;
1098 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001099 CBB transcript;
1100 uint8_t *transcript_data;
1101 size_t transcript_len;
1102 if (!CBB_init(&transcript,
1103 2*SSL3_RANDOM_SIZE + ssl->s3->tmp.server_params_len) ||
1104 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1105 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1106 !CBB_add_bytes(&transcript, ssl->s3->tmp.server_params,
1107 ssl->s3->tmp.server_params_len) ||
1108 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1109 CBB_cleanup(&transcript);
1110 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1111 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001112 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001113 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001114
David Benjaminc895d6b2016-08-11 13:26:41 -04001115 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
1116 signature_algorithm, transcript_data,
1117 transcript_len);
1118 OPENSSL_free(transcript_data);
Kenny Rootb8494592015-09-25 02:29:14 +00001119 } else {
Adam Langley4139edb2016-01-13 15:00:54 -08001120 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001121 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001122 }
Adam Langley4139edb2016-01-13 15:00:54 -08001123
1124 switch (sign_result) {
1125 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001126 if (!CBB_did_write(&child, sig_len)) {
1127 goto err;
1128 }
1129 break;
1130 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001131 goto err;
1132 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001133 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1134 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1135 goto err;
1136 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001137 }
1138
David Benjaminc895d6b2016-08-11 13:26:41 -04001139 if (!ssl->method->finish_message(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001140 goto err;
1141 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001142
1143 OPENSSL_free(ssl->s3->tmp.server_params);
1144 ssl->s3->tmp.server_params = NULL;
1145 ssl->s3->tmp.server_params_len = 0;
1146
Adam Langley4139edb2016-01-13 15:00:54 -08001147 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjaminc895d6b2016-08-11 13:26:41 -04001148 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001149
Adam Langleyd9e397b2015-01-22 14:27:53 -08001150err:
Adam Langley4139edb2016-01-13 15:00:54 -08001151 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001152 return -1;
1153}
1154
David Benjaminc895d6b2016-08-11 13:26:41 -04001155static int add_cert_types(SSL *ssl, CBB *cbb) {
1156 /* Get configured signature algorithms. */
1157 int have_rsa_sign = 0;
1158 int have_ecdsa_sign = 0;
1159 const uint16_t *sig_algs;
1160 size_t sig_algs_len = tls12_get_psigalgs(ssl, &sig_algs);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001161 for (size_t i = 0; i < sig_algs_len; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001162 switch (sig_algs[i]) {
1163 case SSL_SIGN_RSA_PKCS1_SHA512:
1164 case SSL_SIGN_RSA_PKCS1_SHA384:
1165 case SSL_SIGN_RSA_PKCS1_SHA256:
1166 case SSL_SIGN_RSA_PKCS1_SHA1:
1167 have_rsa_sign = 1;
1168 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001169
David Benjaminc895d6b2016-08-11 13:26:41 -04001170 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1171 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1172 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1173 case SSL_SIGN_ECDSA_SHA1:
1174 have_ecdsa_sign = 1;
1175 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001176 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001177 }
1178
David Benjaminc895d6b2016-08-11 13:26:41 -04001179 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1180 return 0;
1181 }
1182
1183 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1184 * check for SSL_kECDH or SSL_kECDHE. */
1185 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1186 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1187 return 0;
1188 }
1189
1190 return 1;
1191}
1192
1193static int ssl3_send_certificate_request(SSL *ssl) {
1194 if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
1195 return ssl->method->write_message(ssl);
1196 }
1197
1198 CBB cbb, body, cert_types, sigalgs_cbb;
1199 if (!ssl->method->init_message(ssl, &cbb, &body,
1200 SSL3_MT_CERTIFICATE_REQUEST) ||
1201 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1202 !add_cert_types(ssl, &cert_types)) {
1203 goto err;
1204 }
1205
1206 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1207 const uint16_t *sigalgs;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001208 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001209 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
1210 goto err;
1211 }
1212
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001213 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001214 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1215 goto err;
1216 }
1217 }
1218 }
1219
1220 if (!ssl_add_client_CA_list(ssl, &body) ||
1221 !ssl->method->finish_message(ssl, &cbb)) {
1222 goto err;
1223 }
1224
1225 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1226 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001227
1228err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001229 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1230 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001231 return -1;
1232}
1233
David Benjamin6e899c72016-06-09 18:02:18 -04001234static int ssl3_send_server_hello_done(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001235 if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
1236 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04001237 }
1238
David Benjaminc895d6b2016-08-11 13:26:41 -04001239 CBB cbb, body;
1240 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
1241 !ssl->method->finish_message(ssl, &cbb)) {
1242 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1243 CBB_cleanup(&cbb);
1244 return -1;
1245 }
1246
1247 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1248 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04001249}
1250
1251static int ssl3_get_client_certificate(SSL *ssl) {
David Benjamin6e899c72016-06-09 18:02:18 -04001252 assert(ssl->s3->tmp.cert_request);
David Benjamin6e899c72016-06-09 18:02:18 -04001253
David Benjaminc895d6b2016-08-11 13:26:41 -04001254 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1255 if (msg_ret <= 0) {
1256 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001257 }
1258
1259 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1260 if (ssl->version == SSL3_VERSION &&
1261 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001262 /* In SSL 3.0, the Certificate message is omitted to signal no
1263 * certificate. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001264 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001265 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001266 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1267 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001268 }
1269
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001270 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1271 * classed by them as a bug, but it's assumed by at least NGINX. */
1272 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001273 ssl->s3->tmp.reuse_message = 1;
1274 return 1;
1275 }
1276
David Benjamin6e899c72016-06-09 18:02:18 -04001277 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001278 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1279 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001280 }
1281
David Benjaminc895d6b2016-08-11 13:26:41 -04001282 CBS certificate_msg;
1283 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
1284 uint8_t alert;
1285 STACK_OF(X509) *chain = ssl_parse_cert_chain(
1286 ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs
1287 ? ssl->s3->new_session->peer_sha256
1288 : NULL,
1289 &certificate_msg);
1290 if (chain == NULL) {
1291 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin6e899c72016-06-09 18:02:18 -04001292 goto err;
1293 }
1294
David Benjaminc895d6b2016-08-11 13:26:41 -04001295 if (CBS_len(&certificate_msg) != 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001296 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001297 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1298 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001299 }
1300
David Benjaminc895d6b2016-08-11 13:26:41 -04001301 if (sk_X509_num(chain) == 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001302 /* No client certificate so the handshake buffer may be discarded. */
1303 ssl3_free_handshake_buffer(ssl);
1304
David Benjaminc895d6b2016-08-11 13:26:41 -04001305 /* In SSL 3.0, sending no certificate is signaled by omitting the
1306 * Certificate message. */
David Benjamin6e899c72016-06-09 18:02:18 -04001307 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001308 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjaminc895d6b2016-08-11 13:26:41 -04001309 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1310 goto err;
1311 }
1312
1313 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001314 /* Fail for TLS only if we required a certificate */
1315 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001316 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1317 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001318 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001319
1320 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1321 * classed by them as a bug, but it's assumed by at least NGINX. */
1322 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001323 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -04001324 /* The hash would have been filled in. */
1325 if (ssl->ctx->retain_only_sha256_of_client_certs) {
1326 ssl->s3->new_session->peer_sha256_valid = 1;
1327 }
1328
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001329 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1330 chain)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001331 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001332 }
1333 }
1334
David Benjaminc895d6b2016-08-11 13:26:41 -04001335 X509_free(ssl->s3->new_session->peer);
1336 ssl->s3->new_session->peer = sk_X509_shift(chain);
David Benjamin6e899c72016-06-09 18:02:18 -04001337
David Benjaminc895d6b2016-08-11 13:26:41 -04001338 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
1339 ssl->s3->new_session->cert_chain = chain;
David Benjamin6e899c72016-06-09 18:02:18 -04001340 /* Inconsistency alert: cert_chain does *not* include the peer's own
1341 * certificate, while we do include it in s3_clnt.c */
1342
David Benjaminc895d6b2016-08-11 13:26:41 -04001343 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001344
1345err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001346 sk_X509_pop_free(chain, X509_free);
1347 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001348}
1349
1350static int ssl3_get_client_key_exchange(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001351 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001352 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001353 uint32_t alg_k;
1354 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001355 uint8_t *premaster_secret = NULL;
1356 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001357 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001358
Adam Langley4139edb2016-01-13 15:00:54 -08001359 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001360 uint8_t psk[PSK_MAX_PSK_LEN];
1361
David Benjamind316cba2016-06-02 16:17:39 -04001362 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001363 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1364 ssl_hash_message);
1365 if (ret <= 0) {
1366 return ret;
Kenny Roote99801b2015-11-06 15:31:15 -08001367 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001368 }
1369
Adam Langley4139edb2016-01-13 15:00:54 -08001370 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1371 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1372 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001373
1374 /* If using a PSK key exchange, prepare the pre-shared key. */
1375 if (alg_a & SSL_aPSK) {
1376 CBS psk_identity;
1377
1378 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1379 * then this is the only field in the message. */
1380 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1381 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001382 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001383 al = SSL_AD_DECODE_ERROR;
1384 goto f_err;
1385 }
1386
Adam Langley4139edb2016-01-13 15:00:54 -08001387 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001388 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001389 al = SSL_AD_INTERNAL_ERROR;
1390 goto f_err;
1391 }
1392
1393 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1394 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001395 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001396 al = SSL_AD_ILLEGAL_PARAMETER;
1397 goto f_err;
1398 }
1399
David Benjaminc895d6b2016-08-11 13:26:41 -04001400 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001401 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001402 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001403 goto f_err;
1404 }
1405
1406 /* Look up the key for the identity. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001407 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1408 psk, sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001409 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001410 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001411 al = SSL_AD_INTERNAL_ERROR;
1412 goto f_err;
1413 } else if (psk_len == 0) {
1414 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001415 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001416 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1417 goto f_err;
1418 }
1419 }
1420
1421 /* Depending on the key exchange method, compute |premaster_secret| and
1422 * |premaster_secret_len|. */
1423 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001424 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001425 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001426 decrypt_buf = OPENSSL_malloc(rsa_size);
1427 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001428 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001429 goto err;
1430 }
1431
Kenny Roote99801b2015-11-06 15:31:15 -08001432 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001433 size_t decrypt_len;
David Benjamind316cba2016-06-02 16:17:39 -04001434 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001435 if (!ssl_has_private_key(ssl) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001436 ssl_private_key_type(ssl) != NID_rsaEncryption) {
Kenny Roote99801b2015-11-06 15:31:15 -08001437 al = SSL_AD_HANDSHAKE_FAILURE;
1438 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1439 goto f_err;
1440 }
Adam Langley4139edb2016-01-13 15:00:54 -08001441 CBS encrypted_premaster_secret;
1442 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001443 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1444 &encrypted_premaster_secret) ||
1445 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001446 al = SSL_AD_DECODE_ERROR;
1447 OPENSSL_PUT_ERROR(SSL,
1448 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1449 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001450 }
1451 } else {
1452 encrypted_premaster_secret = client_key_exchange;
1453 }
1454
Kenny Roote99801b2015-11-06 15:31:15 -08001455 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1456 * timing-sensitive code below. */
1457 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001458 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001459 CBS_data(&encrypted_premaster_secret),
1460 CBS_len(&encrypted_premaster_secret));
1461 } else {
David Benjamind316cba2016-06-02 16:17:39 -04001462 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001463 /* Complete async decrypt. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001464 decrypt_result =
1465 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001466 }
Kenny Roote99801b2015-11-06 15:31:15 -08001467
1468 switch (decrypt_result) {
1469 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001470 break;
1471 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001472 goto err;
1473 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001474 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind316cba2016-06-02 16:17:39 -04001475 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001476 goto err;
1477 }
1478
David Benjamin4969cc92016-04-22 15:02:23 -04001479 if (decrypt_len != rsa_size) {
1480 al = SSL_AD_DECRYPT_ERROR;
1481 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1482 goto f_err;
1483 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001484
Adam Langley4139edb2016-01-13 15:00:54 -08001485 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1486 * section 7.4.7.1. */
1487 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1488 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001489 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001490 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001491 goto err;
1492 }
Adam Langley4139edb2016-01-13 15:00:54 -08001493 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1494 goto err;
1495 }
1496
1497 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1498 * publicly invalid. */
1499 if (decrypt_len < 11 + premaster_secret_len) {
1500 al = SSL_AD_DECRYPT_ERROR;
1501 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1502 goto f_err;
1503 }
1504
1505 /* Check the padding. See RFC 3447, section 7.2.2. */
1506 size_t padding_len = decrypt_len - premaster_secret_len;
1507 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1508 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001509 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001510 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1511 }
1512 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1513
1514 /* The premaster secret must begin with |client_version|. This too must be
1515 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1516 good &= constant_time_eq_8(decrypt_buf[padding_len],
1517 (unsigned)(ssl->client_version >> 8));
1518 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
1519 (unsigned)(ssl->client_version & 0xff));
1520
1521 /* Select, in constant time, either the decrypted premaster or the random
1522 * premaster based on |good|. */
David Benjamin7c0d06c2016-08-11 13:26:41 -04001523 for (size_t i = 0; i < premaster_secret_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001524 premaster_secret[i] = constant_time_select_8(
1525 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1526 }
1527
Adam Langleyd9e397b2015-01-22 14:27:53 -08001528 OPENSSL_free(decrypt_buf);
1529 decrypt_buf = NULL;
David Benjamind316cba2016-06-02 16:17:39 -04001530 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1531 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001532 CBS peer_key;
David Benjamind316cba2016-06-02 16:17:39 -04001533 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1534 &peer_key) ||
1535 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001536 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001537 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001538 goto f_err;
1539 }
1540
Adam Langley4139edb2016-01-13 15:00:54 -08001541 /* Compute the premaster. */
1542 uint8_t alert;
David Benjamind316cba2016-06-02 16:17:39 -04001543 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1544 &premaster_secret_len, &alert, CBS_data(&peer_key),
1545 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001546 al = alert;
1547 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001548 }
1549
Adam Langley4139edb2016-01-13 15:00:54 -08001550 /* The key exchange state may now be discarded. */
1551 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001552 } else if (alg_k & SSL_kPSK) {
1553 /* For plain PSK, other_secret is a block of 0s with the same length as the
1554 * pre-shared key. */
1555 premaster_secret_len = psk_len;
1556 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1557 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001558 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001559 goto err;
1560 }
1561 memset(premaster_secret, 0, premaster_secret_len);
1562 } else {
1563 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001564 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001565 goto f_err;
1566 }
1567
1568 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1569 * pre-shared key. */
1570 if (alg_a & SSL_aPSK) {
1571 CBB new_premaster, child;
1572 uint8_t *new_data;
1573 size_t new_len;
1574
Kenny Rootb8494592015-09-25 02:29:14 +00001575 CBB_zero(&new_premaster);
1576 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1577 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001578 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1579 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1580 !CBB_add_bytes(&child, psk, psk_len) ||
1581 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001582 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001583 CBB_cleanup(&new_premaster);
1584 goto err;
1585 }
1586
1587 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1588 OPENSSL_free(premaster_secret);
1589 premaster_secret = new_data;
1590 premaster_secret_len = new_len;
1591 }
1592
1593 /* Compute the master secret */
David Benjaminc895d6b2016-08-11 13:26:41 -04001594 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1595 ssl, ssl->s3->new_session->master_key, premaster_secret,
1596 premaster_secret_len);
1597 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001598 goto err;
1599 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001600 ssl->s3->new_session->extended_master_secret =
1601 ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001602
1603 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1604 OPENSSL_free(premaster_secret);
1605 return 1;
1606
1607f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001608 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001609err:
Adam Langley4139edb2016-01-13 15:00:54 -08001610 if (premaster_secret != NULL) {
1611 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001612 OPENSSL_free(premaster_secret);
1613 }
Adam Langleye9ada862015-05-11 17:20:37 -07001614 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001615
1616 return -1;
1617}
1618
David Benjamin6e899c72016-06-09 18:02:18 -04001619static int ssl3_get_cert_verify(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001620 int al, ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001621 CBS certificate_verify, signature;
David Benjaminc895d6b2016-08-11 13:26:41 -04001622 X509 *peer = ssl->s3->new_session->peer;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001623 EVP_PKEY *pkey = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001624
1625 /* Only RSA and ECDSA client certificates are supported, so a
1626 * CertificateVerify is required if and only if there's a client certificate.
1627 * */
1628 if (peer == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001629 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001630 return 1;
1631 }
1632
David Benjaminc895d6b2016-08-11 13:26:41 -04001633 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1634 ssl_dont_hash_message);
1635 if (msg_ret <= 0) {
1636 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001637 }
1638
1639 /* Filter out unsupported certificate types. */
1640 pkey = X509_get_pubkey(peer);
Adam Langleye9ada862015-05-11 17:20:37 -07001641 if (pkey == NULL) {
1642 goto err;
1643 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001644
David Benjaminc895d6b2016-08-11 13:26:41 -04001645 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001646
1647 /* Determine the digest type if needbe. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001648 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001649 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001650 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001651 al = SSL_AD_DECODE_ERROR;
1652 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1653 goto f_err;
1654 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001655 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001656 goto f_err;
1657 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001658 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
1659 } else if (pkey->type == EVP_PKEY_RSA) {
1660 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1661 } else if (pkey->type == EVP_PKEY_EC) {
1662 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
1663 } else {
1664 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1665 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1666 goto f_err;
Adam Langleye9ada862015-05-11 17:20:37 -07001667 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001668
1669 /* Parse and verify the signature. */
1670 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1671 CBS_len(&certificate_verify) != 0) {
1672 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001673 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001674 goto f_err;
1675 }
1676
David Benjaminc895d6b2016-08-11 13:26:41 -04001677 int sig_ok;
1678 /* The SSL3 construction for CertificateVerify does not decompose into a
1679 * single final digest and signature, and must be special-cased. */
1680 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1681 const EVP_MD *md;
1682 uint8_t digest[EVP_MAX_MD_SIZE];
1683 size_t digest_len;
1684 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
1685 signature_algorithm)) {
1686 goto err;
1687 }
1688
1689 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1690 sig_ok = pctx != NULL &&
1691 EVP_PKEY_verify_init(pctx) &&
1692 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1693 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1694 digest, digest_len);
1695 EVP_PKEY_CTX_free(pctx);
1696 } else {
1697 sig_ok = ssl_public_key_verify(
1698 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1699 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1700 ssl->s3->handshake_buffer->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001701 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001702
David Benjamin4969cc92016-04-22 15:02:23 -04001703#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1704 sig_ok = 1;
1705 ERR_clear_error();
1706#endif
1707 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001708 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001709 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001710 goto f_err;
1711 }
1712
David Benjaminc895d6b2016-08-11 13:26:41 -04001713 /* The handshake buffer is no longer necessary, and we may hash the current
1714 * message.*/
1715 ssl3_free_handshake_buffer(ssl);
1716 if (!ssl->method->hash_current_message(ssl)) {
1717 goto err;
1718 }
1719
Adam Langleyd9e397b2015-01-22 14:27:53 -08001720 ret = 1;
1721
1722 if (0) {
1723 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001724 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001725 }
1726
1727err:
Adam Langleyd9e397b2015-01-22 14:27:53 -08001728 EVP_PKEY_free(pkey);
1729
1730 return ret;
1731}
1732
David Benjamin6e899c72016-06-09 18:02:18 -04001733/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1734 * sets the next_proto member in s if found */
1735static int ssl3_get_next_proto(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001736 int ret =
1737 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1738 if (ret <= 0) {
1739 return ret;
1740 }
1741
David Benjamin6e899c72016-06-09 18:02:18 -04001742 CBS next_protocol, selected_protocol, padding;
David Benjaminc895d6b2016-08-11 13:26:41 -04001743 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001744 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1745 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001746 CBS_len(&next_protocol) != 0) {
1747 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1748 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1749 return 0;
1750 }
1751
1752 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin6e899c72016-06-09 18:02:18 -04001753 &ssl->s3->next_proto_negotiated_len)) {
1754 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001755 }
1756
David Benjamin6e899c72016-06-09 18:02:18 -04001757 return 1;
1758}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001759
David Benjamin6e899c72016-06-09 18:02:18 -04001760/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
1761static int ssl3_get_channel_id(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001762 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001763 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
1764 size_t channel_id_hash_len;
1765 const uint8_t *p;
1766 uint16_t extension_type;
1767 EC_GROUP *p256 = NULL;
1768 EC_KEY *key = NULL;
1769 EC_POINT *point = NULL;
1770 ECDSA_SIG sig;
1771 BIGNUM x, y;
1772 CBS encrypted_extensions, extension;
1773
David Benjaminc895d6b2016-08-11 13:26:41 -04001774 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1775 ssl_dont_hash_message);
1776 if (msg_ret <= 0) {
1777 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001778 }
1779
1780 /* Before incorporating the EncryptedExtensions message to the handshake
1781 * hash, compute the hash that should have been signed. */
1782 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
1783 return -1;
1784 }
1785 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
1786
David Benjaminc895d6b2016-08-11 13:26:41 -04001787 if (!ssl->method->hash_current_message(ssl)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001788 return -1;
1789 }
1790
David Benjaminc895d6b2016-08-11 13:26:41 -04001791 CBS_init(&encrypted_extensions, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001792
1793 /* EncryptedExtensions could include multiple extensions, but the only
David Benjaminc895d6b2016-08-11 13:26:41 -04001794 * extension that could be negotiated is Channel ID, so there can only be one
1795 * entry. */
David Benjamin6e899c72016-06-09 18:02:18 -04001796 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
1797 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
1798 CBS_len(&encrypted_extensions) != 0 ||
1799 extension_type != TLSEXT_TYPE_channel_id ||
1800 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04001801 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1802 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin6e899c72016-06-09 18:02:18 -04001803 return -1;
1804 }
1805
1806 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
1807 if (!p256) {
1808 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
1809 return -1;
1810 }
1811
1812 BN_init(&x);
1813 BN_init(&y);
1814 sig.r = BN_new();
1815 sig.s = BN_new();
1816 if (sig.r == NULL || sig.s == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001817 goto err;
1818 }
1819
David Benjamin6e899c72016-06-09 18:02:18 -04001820 p = CBS_data(&extension);
1821 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
1822 BN_bin2bn(p + 32, 32, &y) == NULL ||
1823 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
1824 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
1825 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001826 }
1827
David Benjamin6e899c72016-06-09 18:02:18 -04001828 point = EC_POINT_new(p256);
1829 if (!point ||
1830 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
1831 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001832 }
1833
David Benjamin6e899c72016-06-09 18:02:18 -04001834 key = EC_KEY_new();
1835 if (!key || !EC_KEY_set_group(key, p256) ||
1836 !EC_KEY_set_public_key(key, point)) {
1837 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001838 }
1839
David Benjamin6e899c72016-06-09 18:02:18 -04001840 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
1841 * were called. */
1842 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
1843 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
1844 ssl->s3->tlsext_channel_id_valid = 0;
1845 goto err;
1846 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001847
David Benjamin6e899c72016-06-09 18:02:18 -04001848 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001849 ret = 1;
1850
Adam Langleyd9e397b2015-01-22 14:27:53 -08001851err:
David Benjamin6e899c72016-06-09 18:02:18 -04001852 BN_free(&x);
1853 BN_free(&y);
1854 BN_free(sig.r);
1855 BN_free(sig.s);
1856 EC_KEY_free(key);
1857 EC_POINT_free(point);
1858 EC_GROUP_free(p256);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001859 return ret;
1860}
1861
David Benjamin6e899c72016-06-09 18:02:18 -04001862static int ssl3_send_new_session_ticket(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001863 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
1864 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001865 }
1866
David Benjaminc895d6b2016-08-11 13:26:41 -04001867 CBB cbb, body, ticket;
1868 if (!ssl->method->init_message(ssl, &cbb, &body,
1869 SSL3_MT_NEW_SESSION_TICKET) ||
1870 /* Ticket lifetime hint (advisory only): We leave this unspecified for
1871 * resumed session (for simplicity), and guess that tickets for new
1872 * sessions will live as long as their sessions. */
1873 !CBB_add_u32(&body,
1874 ssl->session != NULL ? 0 : ssl->s3->new_session->timeout) ||
1875 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1876 !ssl_encrypt_ticket(ssl, &ticket, ssl->session != NULL
1877 ? ssl->session
1878 : ssl->s3->new_session) ||
1879 !ssl->method->finish_message(ssl, &cbb)) {
1880 return 0;
1881 }
Adam Langleye9ada862015-05-11 17:20:37 -07001882
David Benjaminc895d6b2016-08-11 13:26:41 -04001883 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
1884 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001885}