blob: f04112962114f7938655f4b4e7ca4ac8b5f84b02 [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) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200 case SSL_ST_ACCEPT:
David Benjamin6e899c72016-06-09 18:02:18 -0400201 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202
David Benjaminc895d6b2016-08-11 13:26:41 -0400203 ssl->s3->hs = ssl_handshake_new(tls13_server_handshake);
204 if (ssl->s3->hs == NULL) {
205 ret = -1;
206 goto end;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208
Adam Langleye9ada862015-05-11 17:20:37 -0700209 /* Enable a write buffer. This groups handshake messages within a flight
210 * into a single write. */
David Benjamind316cba2016-06-02 16:17:39 -0400211 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700212 ret = -1;
213 goto end;
214 }
215
Adam Langley4139edb2016-01-13 15:00:54 -0800216 if (!ssl3_init_handshake_buffer(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218 ret = -1;
219 goto end;
220 }
221
Adam Langley4139edb2016-01-13 15:00:54 -0800222 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223 break;
224
225 case SSL3_ST_SR_CLNT_HELLO_A:
226 case SSL3_ST_SR_CLNT_HELLO_B:
227 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjaminc895d6b2016-08-11 13:26:41 -0400228 case SSL3_ST_SR_CLNT_HELLO_D:
Adam Langley4139edb2016-01-13 15:00:54 -0800229 ret = ssl3_get_client_hello(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400230 if (ssl->state == SSL_ST_TLS13) {
231 break;
232 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 if (ret <= 0) {
234 goto end;
235 }
David Benjamin6e899c72016-06-09 18:02:18 -0400236 ssl->method->received_flight(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800237 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 break;
239
240 case SSL3_ST_SW_SRVR_HELLO_A:
241 case SSL3_ST_SW_SRVR_HELLO_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800242 ret = ssl3_send_server_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 if (ret <= 0) {
244 goto end;
245 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400246 if (ssl->session != NULL) {
247 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800249 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 break;
252
253 case SSL3_ST_SW_CERT_A:
254 case SSL3_ST_SW_CERT_B:
David Benjamind316cba2016-06-02 16:17:39 -0400255 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800256 ret = ssl3_send_server_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257 if (ret <= 0) {
258 goto end;
259 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260 } else {
261 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400263 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 break;
265
Kenny Rootb8494592015-09-25 02:29:14 +0000266 case SSL3_ST_SW_CERT_STATUS_A:
267 case SSL3_ST_SW_CERT_STATUS_B:
David Benjaminc895d6b2016-08-11 13:26:41 -0400268 if (ssl->s3->tmp.certificate_status_expected) {
269 ret = ssl3_send_certificate_status(ssl);
270 if (ret <= 0) {
271 goto end;
272 }
273 } else {
274 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000275 }
Adam Langley4139edb2016-01-13 15:00:54 -0800276 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000277 break;
278
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279 case SSL3_ST_SW_KEY_EXCH_A:
280 case SSL3_ST_SW_KEY_EXCH_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000281 case SSL3_ST_SW_KEY_EXCH_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800282 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283
David Benjamin6e899c72016-06-09 18:02:18 -0400284 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
Adam Langley4139edb2016-01-13 15:00:54 -0800285 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
286 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
287 ret = ssl3_send_server_key_exchange(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -0700288 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700290 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291 } else {
292 skip = 1;
293 }
294
Adam Langley4139edb2016-01-13 15:00:54 -0800295 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 break;
297
298 case SSL3_ST_SW_CERT_REQ_A:
299 case SSL3_ST_SW_CERT_REQ_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800300 if (ssl->s3->tmp.cert_request) {
301 ret = ssl3_send_certificate_request(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800302 if (ret <= 0) {
303 goto end;
304 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700305 } else {
306 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800307 }
Adam Langley4139edb2016-01-13 15:00:54 -0800308 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309 break;
310
311 case SSL3_ST_SW_SRVR_DONE_A:
312 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin6e899c72016-06-09 18:02:18 -0400313 ret = ssl3_send_server_hello_done(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800314 if (ret <= 0) {
315 goto end;
316 }
Adam Langley4139edb2016-01-13 15:00:54 -0800317 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
318 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 break;
320
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321 case SSL3_ST_SR_CERT_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800322 if (ssl->s3->tmp.cert_request) {
323 ret = ssl3_get_client_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800324 if (ret <= 0) {
325 goto end;
326 }
327 }
Adam Langley4139edb2016-01-13 15:00:54 -0800328 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 break;
330
331 case SSL3_ST_SR_KEY_EXCH_A:
332 case SSL3_ST_SR_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800333 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 if (ret <= 0) {
335 goto end;
336 }
Adam Langley4139edb2016-01-13 15:00:54 -0800337 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800338 break;
339
340 case SSL3_ST_SR_CERT_VRFY_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800341 ret = ssl3_get_cert_verify(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 if (ret <= 0) {
343 goto end;
344 }
345
Adam Langley4139edb2016-01-13 15:00:54 -0800346 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800347 break;
348
Adam Langley4139edb2016-01-13 15:00:54 -0800349 case SSL3_ST_SR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400350 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800351 if (ret <= 0) {
352 goto end;
353 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800354
David Benjamin4969cc92016-04-22 15:02:23 -0400355 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800356 ret = -1;
357 goto end;
358 }
Adam Langley4139edb2016-01-13 15:00:54 -0800359
David Benjaminc895d6b2016-08-11 13:26:41 -0400360 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362
363 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400364 if (ssl->s3->next_proto_neg_seen) {
365 ret = ssl3_get_next_proto(ssl);
366 if (ret <= 0) {
367 goto end;
368 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400370 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800371 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400372 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800373 break;
374
375 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400376 if (ssl->s3->tlsext_channel_id_valid) {
377 ret = ssl3_get_channel_id(ssl);
378 if (ret <= 0) {
379 goto end;
380 }
381 } else {
382 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800383 }
Adam Langley4139edb2016-01-13 15:00:54 -0800384 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800385 break;
386
387 case SSL3_ST_SR_FINISHED_A:
David Benjamind316cba2016-06-02 16:17:39 -0400388 ret = ssl3_get_finished(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800389 if (ret <= 0) {
390 goto end;
391 }
392
David Benjamin6e899c72016-06-09 18:02:18 -0400393 ssl->method->received_flight(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400394 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800395 ssl->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800396 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400397 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398 }
David Benjamin6e899c72016-06-09 18:02:18 -0400399
David Benjaminc895d6b2016-08-11 13:26:41 -0400400 /* If this is a full handshake with ChannelID then record the handshake
401 * hashes in |ssl->s3->new_session| in case we need them to verify a
402 * ChannelID signature on a resumption of this session in the future. */
403 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Adam Langley4139edb2016-01-13 15:00:54 -0800404 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800405 if (ret <= 0) {
406 goto end;
407 }
408 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 break;
410
411 case SSL3_ST_SW_SESSION_TICKET_A:
412 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjaminc895d6b2016-08-11 13:26:41 -0400413 if (ssl->tlsext_ticket_expected) {
414 ret = ssl3_send_new_session_ticket(ssl);
415 if (ret <= 0) {
416 goto end;
417 }
418 } else {
419 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800420 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400421 ssl->state = SSL3_ST_SW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800422 break;
423
David Benjaminc895d6b2016-08-11 13:26:41 -0400424 case SSL3_ST_SW_CHANGE:
425 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800426 if (ret <= 0) {
427 goto end;
428 }
Adam Langley4139edb2016-01-13 15:00:54 -0800429 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800430
David Benjamin4969cc92016-04-22 15:02:23 -0400431 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800432 ret = -1;
433 goto end;
434 }
435 break;
436
437 case SSL3_ST_SW_FINISHED_A:
438 case SSL3_ST_SW_FINISHED_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800439 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjamin4969cc92016-04-22 15:02:23 -0400440 SSL3_ST_SW_FINISHED_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 if (ret <= 0) {
442 goto end;
443 }
Adam Langley4139edb2016-01-13 15:00:54 -0800444 ssl->state = SSL3_ST_SW_FLUSH;
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800446 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800447 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800448 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800449 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800450 break;
451
David Benjamin6e899c72016-06-09 18:02:18 -0400452 case SSL3_ST_SW_FLUSH:
453 if (BIO_flush(ssl->wbio) <= 0) {
454 ssl->rwstate = SSL_WRITING;
455 ret = -1;
456 goto end;
457 }
458
459 ssl->state = ssl->s3->tmp.next_state;
460 if (ssl->state != SSL_ST_OK) {
461 ssl->method->expect_flight(ssl);
462 }
463 break;
464
David Benjaminc895d6b2016-08-11 13:26:41 -0400465 case SSL_ST_TLS13:
466 ret = tls13_handshake(ssl);
467 if (ret <= 0) {
468 goto end;
469 }
470 ssl->state = SSL_ST_OK;
471 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800472
David Benjaminc895d6b2016-08-11 13:26:41 -0400473 case SSL_ST_OK:
474 /* Clean a few things up. */
475 ssl3_cleanup_key_block(ssl);
476 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
477
478 /* If we aren't retaining peer certificates then we can discard it
479 * now. */
480 if (ssl->s3->new_session != NULL &&
481 ssl->ctx->retain_only_sha256_of_client_certs) {
482 X509_free(ssl->s3->new_session->peer);
483 ssl->s3->new_session->peer = NULL;
484 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
485 ssl->s3->new_session->cert_chain = NULL;
486 }
487
488 SSL_SESSION_free(ssl->s3->established_session);
489 if (ssl->session != NULL) {
490 SSL_SESSION_up_ref(ssl->session);
491 ssl->s3->established_session = ssl->session;
492 } else {
493 ssl->s3->established_session = ssl->s3->new_session;
494 ssl->s3->established_session->not_resumable = 0;
495 ssl->s3->new_session = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400496 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800497
498 /* remove buffering on output */
Adam Langley4139edb2016-01-13 15:00:54 -0800499 ssl_free_wbio_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500
David Benjaminc895d6b2016-08-11 13:26:41 -0400501 ssl_handshake_free(ssl->s3->hs);
502 ssl->s3->hs = NULL;
David Benjamin6e899c72016-06-09 18:02:18 -0400503
Adam Langley4139edb2016-01-13 15:00:54 -0800504 ssl->s3->initial_handshake_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800505
Adam Langley4139edb2016-01-13 15:00:54 -0800506 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800507
David Benjamin6e899c72016-06-09 18:02:18 -0400508 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800509
510 ret = 1;
511 goto end;
512
513 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000514 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800515 ret = -1;
516 goto end;
517 }
518
David Benjamin6e899c72016-06-09 18:02:18 -0400519 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
520 int new_state = ssl->state;
Adam Langley4139edb2016-01-13 15:00:54 -0800521 ssl->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400522 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langley4139edb2016-01-13 15:00:54 -0800523 ssl->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800524 }
525 skip = 0;
526 }
527
528end:
David Benjamin6e899c72016-06-09 18:02:18 -0400529 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800530 return ret;
531}
532
David Benjaminc895d6b2016-08-11 13:26:41 -0400533int ssl_client_cipher_list_contains_cipher(
534 const struct ssl_early_callback_ctx *client_hello, uint16_t id) {
535 CBS cipher_suites;
536 CBS_init(&cipher_suites, client_hello->cipher_suites,
537 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800538
David Benjaminc895d6b2016-08-11 13:26:41 -0400539 while (CBS_len(&cipher_suites) > 0) {
540 uint16_t got_id;
541 if (!CBS_get_u16(&cipher_suites, &got_id)) {
542 return 0;
543 }
544
545 if (got_id == id) {
546 return 1;
547 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800548 }
549
David Benjaminc895d6b2016-08-11 13:26:41 -0400550 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800551}
552
David Benjaminc895d6b2016-08-11 13:26:41 -0400553static int negotiate_version(
554 SSL *ssl, int *out_alert,
555 const struct ssl_early_callback_ctx *client_hello) {
556 uint16_t min_version, max_version;
557 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
558 *out_alert = SSL_AD_PROTOCOL_VERSION;
559 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800560 }
561
David Benjaminc895d6b2016-08-11 13:26:41 -0400562 uint16_t client_version =
563 ssl->method->version_from_wire(client_hello->version);
564 ssl->client_version = client_hello->version;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800565
David Benjaminc895d6b2016-08-11 13:26:41 -0400566 /* Select the version to use. */
567 uint16_t version = client_version;
568 if (version > max_version) {
569 version = max_version;
Adam Langleye9ada862015-05-11 17:20:37 -0700570 }
David Benjamin6e899c72016-06-09 18:02:18 -0400571
David Benjaminc895d6b2016-08-11 13:26:41 -0400572 if (version < min_version) {
573 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
574 *out_alert = SSL_AD_PROTOCOL_VERSION;
575 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800576 }
577
David Benjaminc895d6b2016-08-11 13:26:41 -0400578 /* Handle FALLBACK_SCSV. */
579 if (ssl_client_cipher_list_contains_cipher(client_hello,
580 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
581 version < max_version) {
582 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
583 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
584 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800585 }
586
David Benjaminc895d6b2016-08-11 13:26:41 -0400587 ssl->version = ssl->method->version_to_wire(version);
588 ssl->s3->enc_method = ssl3_get_enc_method(version);
589 assert(ssl->s3->enc_method != NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800590
David Benjaminc895d6b2016-08-11 13:26:41 -0400591 /* At this point, the connection's version is known and |ssl->version| is
592 * fixed. Begin enforcing the record-layer version. */
593 ssl->s3->have_version = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800594
595 return 1;
596}
597
David Benjamin6e899c72016-06-09 18:02:18 -0400598static int ssl3_get_client_hello(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400599 int al = SSL_AD_INTERNAL_ERROR, ret = -1;
Kenny Rootb8494592015-09-25 02:29:14 +0000600 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800601
David Benjaminc895d6b2016-08-11 13:26:41 -0400602 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) {
603 /* The first time around, read the ClientHello. */
604 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
605 ssl_hash_message);
606 if (msg_ret <= 0) {
607 return msg_ret;
608 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800609
David Benjaminc895d6b2016-08-11 13:26:41 -0400610 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
611 }
612
613 struct ssl_early_callback_ctx client_hello;
614 if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg,
615 ssl->init_num)) {
616 al = SSL_AD_DECODE_ERROR;
617 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
618 goto f_err;
619 }
620
621 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) {
622 /* Unlike other callbacks, the early callback is not run a second time if
623 * paused. */
624 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
625
626 /* Run the early callback. */
627 if (ssl->ctx->select_certificate_cb != NULL) {
628 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
629 case 0:
630 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
631 goto err;
632
633 case -1:
634 /* Connection rejected. */
635 al = SSL_AD_ACCESS_DENIED;
636 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
637 goto f_err;
638
639 default:
640 /* fallthrough */;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800641 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400642 }
643 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800644
David Benjaminc895d6b2016-08-11 13:26:41 -0400645 /* Negotiate the protocol version if we have not done so yet. */
646 if (!ssl->s3->have_version) {
647 if (!negotiate_version(ssl, &al, &client_hello)) {
648 goto f_err;
649 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800650
David Benjaminc895d6b2016-08-11 13:26:41 -0400651 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
652 ssl->state = SSL_ST_TLS13;
653 return 1;
654 }
655 }
656
657 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) {
658 /* Load the client random. */
659 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
660 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
661 return -1;
662 }
663 memcpy(ssl->s3->client_random, client_hello.random,
664 client_hello.random_len);
665
666 /* Determine whether we are doing session resumption. */
667 int send_new_ticket = 0;
668 switch (
669 ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) {
670 case ssl_session_success:
671 break;
672 case ssl_session_error:
673 goto err;
674 case ssl_session_retry:
675 ssl->rwstate = SSL_PENDING_SESSION;
676 goto err;
677 }
678 ssl->tlsext_ticket_expected = send_new_ticket;
679
680 /* The EMS state is needed when making the resumption decision, but
681 * extensions are not normally parsed until later. This detects the EMS
682 * extension for the resumption decision and it's checked against the result
683 * of the normal parse later in this function. */
684 CBS ems;
685 int have_extended_master_secret =
686 ssl->version != SSL3_VERSION &&
687 ssl_early_callback_get_extension(&client_hello, &ems,
688 TLSEXT_TYPE_extended_master_secret) &&
689 CBS_len(&ems) == 0;
690
691 int has_session = 0;
692 if (session != NULL) {
693 if (session->extended_master_secret &&
694 !have_extended_master_secret) {
695 /* A ClientHello without EMS that attempts to resume a session with EMS
696 * is fatal to the connection. */
697 al = SSL_AD_HANDSHAKE_FAILURE;
698 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800699 goto f_err;
700 }
701
David Benjaminc895d6b2016-08-11 13:26:41 -0400702 has_session =
703 /* Only resume if the session's version matches the negotiated
704 * version: most clients do not accept a mismatch. */
705 ssl->version == session->ssl_version &&
706 /* If the client offers the EMS extension, but the previous session
707 * didn't use it, then negotiate a new session. */
708 have_extended_master_secret == session->extended_master_secret;
709 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800710
David Benjaminc895d6b2016-08-11 13:26:41 -0400711 if (has_session) {
712 /* Use the old session. */
713 ssl->session = session;
714 session = NULL;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400715 ssl->s3->session_reused = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400716 } else {
717 SSL_set_session(ssl, NULL);
718 if (!ssl_get_new_session(ssl, 1 /* server */)) {
719 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800720 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800721
David Benjaminc895d6b2016-08-11 13:26:41 -0400722 /* Clear the session ID if we want the session to be single-use. */
723 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
724 ssl->s3->new_session->session_id_length = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800725 }
726 }
727
David Benjaminc895d6b2016-08-11 13:26:41 -0400728 if (ssl->ctx->dos_protection_cb != NULL &&
729 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
730 /* Connection rejected for DOS reasons. */
731 al = SSL_AD_ACCESS_DENIED;
732 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
733 goto f_err;
734 }
735
736 /* Only null compression is supported. */
737 if (memchr(client_hello.compression_methods, 0,
738 client_hello.compression_methods_len) == NULL) {
739 al = SSL_AD_ILLEGAL_PARAMETER;
740 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
741 goto f_err;
742 }
743
744 /* TLS extensions. */
745 if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
746 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
747 goto err;
748 }
749
750 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
751 al = SSL_AD_INTERNAL_ERROR;
752 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
753 goto f_err;
754 }
755
756 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
757 }
758
759 /* Determine the remaining connection parameters. This is a separate state so
760 * |cert_cb| does not cause earlier logic to run multiple times. */
761 assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D);
762
763 if (ssl->session != NULL) {
764 /* Check that the cipher is in the list. */
765 if (!ssl_client_cipher_list_contains_cipher(
766 &client_hello, (uint16_t)ssl->session->cipher->id)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800767 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000768 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800769 goto f_err;
770 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800771
David Benjaminc895d6b2016-08-11 13:26:41 -0400772 ssl->s3->tmp.new_cipher = ssl->session->cipher;
773 ssl->s3->tmp.cert_request = 0;
774 } else {
775 /* Call |cert_cb| to update server certificates if required. */
776 if (ssl->cert->cert_cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800777 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800778 if (rv == 0) {
779 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000780 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800781 goto f_err;
782 }
783 if (rv < 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800784 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800785 goto err;
786 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800787 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800788
David Benjaminc895d6b2016-08-11 13:26:41 -0400789 const SSL_CIPHER *c =
790 ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800791 if (c == NULL) {
792 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000793 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800794 goto f_err;
795 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400796
797 ssl->s3->new_session->cipher = c;
Adam Langley4139edb2016-01-13 15:00:54 -0800798 ssl->s3->tmp.new_cipher = c;
Adam Langleyf4e42722015-06-04 17:45:09 -0700799
800 /* Determine whether to request a client certificate. */
Adam Langley4139edb2016-01-13 15:00:54 -0800801 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -0700802 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -0800803 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
804 ssl->s3->tlsext_channel_id_valid) {
805 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -0700806 }
David Benjamind316cba2016-06-02 16:17:39 -0400807 /* CertificateRequest may only be sent in certificate-based ciphers. */
808 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800809 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -0700810 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400811
812 if (!ssl->s3->tmp.cert_request) {
813 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
814 * classed by them as a bug, but it's assumed by at least NGINX. */
815 ssl->s3->new_session->verify_result = X509_V_OK;
816 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800817 }
818
Kenny Rootb8494592015-09-25 02:29:14 +0000819 /* Now that the cipher is known, initialize the handshake hash. */
Adam Langley4139edb2016-01-13 15:00:54 -0800820 if (!ssl3_init_handshake_hash(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000821 goto f_err;
822 }
823
David Benjaminc895d6b2016-08-11 13:26:41 -0400824 /* Release the handshake buffer if client authentication isn't required. */
825 if (!ssl->s3->tmp.cert_request) {
Adam Langley4139edb2016-01-13 15:00:54 -0800826 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800827 }
828
Adam Langley4139edb2016-01-13 15:00:54 -0800829 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800830
831 if (0) {
832 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -0800833 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800834 }
835
836err:
Kenny Rootb8494592015-09-25 02:29:14 +0000837 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800838 return ret;
839}
840
David Benjamin6e899c72016-06-09 18:02:18 -0400841static int ssl3_send_server_hello(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -0800842 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400843 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800844 }
845
Kenny Roote99801b2015-11-06 15:31:15 -0800846 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
847
848 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
849 * known attack while we fix ChannelID itself. */
850 if (ssl->s3->tlsext_channel_id_valid &&
851 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
852 ssl->s3->tlsext_channel_id_valid = 0;
853 }
854
855 /* If this is a resumption and the original handshake didn't support
856 * ChannelID then we didn't record the original handshake hashes in the
857 * session and so cannot resume with ChannelIDs. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400858 if (ssl->session != NULL &&
859 ssl->session->original_handshake_hash_len == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -0800860 ssl->s3->tlsext_channel_id_valid = 0;
861 }
862
David Benjaminc895d6b2016-08-11 13:26:41 -0400863 struct timeval now;
864 ssl_get_current_time(ssl, &now);
865 ssl->s3->server_random[0] = now.tv_sec >> 24;
866 ssl->s3->server_random[1] = now.tv_sec >> 16;
867 ssl->s3->server_random[2] = now.tv_sec >> 8;
868 ssl->s3->server_random[3] = now.tv_sec;
869 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800870 return -1;
871 }
872
David Benjaminc895d6b2016-08-11 13:26:41 -0400873 /* Fill in the TLS 1.2 downgrade signal. See draft-ietf-tls-tls13-14.
874 *
875 * TODO(davidben): Also implement the TLS 1.1 sentinel when things have
876 * settled down. */
877 uint16_t min_version, max_version;
878 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
879 return -1;
880 }
881 if (max_version >= TLS1_3_VERSION &&
882 ssl3_protocol_version(ssl) <= TLS1_2_VERSION) {
883 static const uint8_t kDowngradeTLS12[8] = {0x44, 0x4f, 0x57, 0x4e,
884 0x47, 0x52, 0x44, 0x01};
885 memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE - 8, kDowngradeTLS12, 8);
886 }
887
888 const SSL_SESSION *session = ssl->s3->new_session;
889 if (ssl->session != NULL) {
890 session = ssl->session;
891 }
892
893 CBB cbb, body, session_id;
894 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
895 !CBB_add_u16(&body, ssl->version) ||
896 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
897 !CBB_add_u8_length_prefixed(&body, &session_id) ||
898 !CBB_add_bytes(&session_id, session->session_id,
899 session->session_id_length) ||
900 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
901 !CBB_add_u8(&body, 0 /* no compression */) ||
902 !ssl_add_serverhello_tlsext(ssl, &body) ||
903 !ssl->method->finish_message(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800904 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
905 CBB_cleanup(&cbb);
906 return -1;
907 }
908
909 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjaminc895d6b2016-08-11 13:26:41 -0400910 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800911}
912
David Benjamin6e899c72016-06-09 18:02:18 -0400913static int ssl3_send_server_certificate(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400914 if (ssl->state == SSL3_ST_SW_CERT_B) {
915 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -0400916 }
917
David Benjaminc895d6b2016-08-11 13:26:41 -0400918 if (!ssl_has_certificate(ssl)) {
919 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
920 return 0;
921 }
922
923 if (!ssl3_output_cert_chain(ssl)) {
924 return 0;
925 }
926 ssl->state = SSL3_ST_SW_CERT_B;
927 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -0400928}
929
930static int ssl3_send_certificate_status(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400931 if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
932 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000933 }
934
David Benjaminc895d6b2016-08-11 13:26:41 -0400935 CBB cbb, body, ocsp_response;
936 if (!ssl->method->init_message(ssl, &cbb, &body,
937 SSL3_MT_CERTIFICATE_STATUS) ||
938 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
939 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
940 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
941 ssl->ctx->ocsp_response_length) ||
942 !ssl->method->finish_message(ssl, &cbb)) {
943 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
944 CBB_cleanup(&cbb);
945 return -1;
946 }
947
948 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
949 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000950}
951
David Benjamin6e899c72016-06-09 18:02:18 -0400952static int ssl3_send_server_key_exchange(SSL *ssl) {
Adam Langley4139edb2016-01-13 15:00:54 -0800953 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400954 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000955 }
956
Adam Langley4139edb2016-01-13 15:00:54 -0800957 CBB cbb, child;
David Benjaminc895d6b2016-08-11 13:26:41 -0400958 CBB_zero(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +0000959
David Benjaminc895d6b2016-08-11 13:26:41 -0400960 /* Put together the parameters. */
Adam Langley4139edb2016-01-13 15:00:54 -0800961 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -0800962 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
963 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800964
David Benjaminc895d6b2016-08-11 13:26:41 -0400965 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
966 if (!CBB_init(&cbb, 128)) {
967 goto err;
968 }
969
Adam Langley4139edb2016-01-13 15:00:54 -0800970 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800971 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -0800972 size_t len =
973 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
974 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
975 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
976 len)) {
977 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800978 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800979 }
980
Adam Langleye9ada862015-05-11 17:20:37 -0700981 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -0800982 /* Determine the group to use. */
983 DH *params = ssl->cert->dh_tmp;
984 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
985 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800986 }
Adam Langley4139edb2016-01-13 15:00:54 -0800987 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000988 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -0800989 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800990 goto err;
991 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400992 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
Adam Langley4139edb2016-01-13 15:00:54 -0800993
994 /* Set up DH, generate a key, and emit the public half. */
995 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800996 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -0700997 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800998 }
999
Adam Langley4139edb2016-01-13 15:00:54 -08001000 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1001 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1002 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1003 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1004 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1005 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001006 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001007 goto err;
1008 }
Adam Langleye9ada862015-05-11 17:20:37 -07001009 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001010 /* Determine the group to use. */
1011 uint16_t group_id;
1012 if (!tls1_get_shared_group(ssl, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001013 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001014 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001015 goto err;
1016 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001017 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001018
1019 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamind316cba2016-06-02 16:17:39 -04001020 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001021 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001022 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001023 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001024 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1025 goto err;
1026 }
1027 } else if (alg_k & SSL_kCECPQ1) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001028 SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
1029 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001030 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001031 goto err;
1032 }
Adam Langley4139edb2016-01-13 15:00:54 -08001033 } else {
1034 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001035 }
1036
David Benjaminc895d6b2016-08-11 13:26:41 -04001037 size_t len;
1038 if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) ||
1039 len > 0xffffffffu) {
1040 OPENSSL_free(ssl->s3->tmp.server_params);
1041 ssl->s3->tmp.server_params = NULL;
1042 goto err;
1043 }
1044 ssl->s3->tmp.server_params_len = (uint32_t)len;
1045 }
1046
1047 /* Assemble the message. */
1048 CBB body;
1049 if (!ssl->method->init_message(ssl, &cbb, &body,
1050 SSL3_MT_SERVER_KEY_EXCHANGE) ||
1051 !CBB_add_bytes(&body, ssl->s3->tmp.server_params,
1052 ssl->s3->tmp.server_params_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001053 goto err;
1054 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001055
Adam Langley4139edb2016-01-13 15:00:54 -08001056 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001057 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001058 if (!ssl_has_private_key(ssl)) {
1059 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001060 goto err;
1061 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001062
David Benjaminc895d6b2016-08-11 13:26:41 -04001063 /* Determine the signature algorithm. */
1064 uint16_t signature_algorithm;
1065 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1066 goto err;
1067 }
1068 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1069 if (!CBB_add_u16(&body, signature_algorithm)) {
1070 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1071 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1072 goto err;
1073 }
1074 }
1075
1076 /* Add space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001077 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001078 uint8_t *ptr;
1079 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1080 !CBB_reserve(&child, &ptr, max_sig_len)) {
1081 goto err;
1082 }
1083
Adam Langley4139edb2016-01-13 15:00:54 -08001084 size_t sig_len;
1085 enum ssl_private_key_result_t sign_result;
1086 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001087 CBB transcript;
1088 uint8_t *transcript_data;
1089 size_t transcript_len;
1090 if (!CBB_init(&transcript,
1091 2*SSL3_RANDOM_SIZE + ssl->s3->tmp.server_params_len) ||
1092 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1093 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1094 !CBB_add_bytes(&transcript, ssl->s3->tmp.server_params,
1095 ssl->s3->tmp.server_params_len) ||
1096 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1097 CBB_cleanup(&transcript);
1098 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1099 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001100 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001101 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001102
David Benjaminc895d6b2016-08-11 13:26:41 -04001103 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
1104 signature_algorithm, transcript_data,
1105 transcript_len);
1106 OPENSSL_free(transcript_data);
Kenny Rootb8494592015-09-25 02:29:14 +00001107 } else {
Adam Langley4139edb2016-01-13 15:00:54 -08001108 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001109 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001110 }
Adam Langley4139edb2016-01-13 15:00:54 -08001111
1112 switch (sign_result) {
1113 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001114 if (!CBB_did_write(&child, sig_len)) {
1115 goto err;
1116 }
1117 break;
1118 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001119 goto err;
1120 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001121 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1122 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1123 goto err;
1124 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001125 }
1126
David Benjaminc895d6b2016-08-11 13:26:41 -04001127 if (!ssl->method->finish_message(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001128 goto err;
1129 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001130
1131 OPENSSL_free(ssl->s3->tmp.server_params);
1132 ssl->s3->tmp.server_params = NULL;
1133 ssl->s3->tmp.server_params_len = 0;
1134
Adam Langley4139edb2016-01-13 15:00:54 -08001135 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjaminc895d6b2016-08-11 13:26:41 -04001136 return ssl->method->write_message(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001137
Adam Langleyd9e397b2015-01-22 14:27:53 -08001138err:
Adam Langley4139edb2016-01-13 15:00:54 -08001139 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001140 return -1;
1141}
1142
David Benjaminc895d6b2016-08-11 13:26:41 -04001143static int add_cert_types(SSL *ssl, CBB *cbb) {
1144 /* Get configured signature algorithms. */
1145 int have_rsa_sign = 0;
1146 int have_ecdsa_sign = 0;
1147 const uint16_t *sig_algs;
1148 size_t sig_algs_len = tls12_get_psigalgs(ssl, &sig_algs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001149 size_t i;
David Benjaminc895d6b2016-08-11 13:26:41 -04001150 for (i = 0; i < sig_algs_len; i++) {
1151 switch (sig_algs[i]) {
1152 case SSL_SIGN_RSA_PKCS1_SHA512:
1153 case SSL_SIGN_RSA_PKCS1_SHA384:
1154 case SSL_SIGN_RSA_PKCS1_SHA256:
1155 case SSL_SIGN_RSA_PKCS1_SHA1:
1156 have_rsa_sign = 1;
1157 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001158
David Benjaminc895d6b2016-08-11 13:26:41 -04001159 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1160 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1161 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1162 case SSL_SIGN_ECDSA_SHA1:
1163 have_ecdsa_sign = 1;
1164 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001165 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001166 }
1167
David Benjaminc895d6b2016-08-11 13:26:41 -04001168 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1169 return 0;
1170 }
1171
1172 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1173 * check for SSL_kECDH or SSL_kECDHE. */
1174 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1175 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1176 return 0;
1177 }
1178
1179 return 1;
1180}
1181
1182static int ssl3_send_certificate_request(SSL *ssl) {
1183 if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
1184 return ssl->method->write_message(ssl);
1185 }
1186
1187 CBB cbb, body, cert_types, sigalgs_cbb;
1188 if (!ssl->method->init_message(ssl, &cbb, &body,
1189 SSL3_MT_CERTIFICATE_REQUEST) ||
1190 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1191 !add_cert_types(ssl, &cert_types)) {
1192 goto err;
1193 }
1194
1195 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1196 const uint16_t *sigalgs;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001197 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001198 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
1199 goto err;
1200 }
1201
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001202 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001203 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1204 goto err;
1205 }
1206 }
1207 }
1208
1209 if (!ssl_add_client_CA_list(ssl, &body) ||
1210 !ssl->method->finish_message(ssl, &cbb)) {
1211 goto err;
1212 }
1213
1214 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1215 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001216
1217err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001218 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1219 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001220 return -1;
1221}
1222
David Benjamin6e899c72016-06-09 18:02:18 -04001223static int ssl3_send_server_hello_done(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001224 if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
1225 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04001226 }
1227
David Benjaminc895d6b2016-08-11 13:26:41 -04001228 CBB cbb, body;
1229 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
1230 !ssl->method->finish_message(ssl, &cbb)) {
1231 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1232 CBB_cleanup(&cbb);
1233 return -1;
1234 }
1235
1236 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1237 return ssl->method->write_message(ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04001238}
1239
1240static int ssl3_get_client_certificate(SSL *ssl) {
David Benjamin6e899c72016-06-09 18:02:18 -04001241 assert(ssl->s3->tmp.cert_request);
David Benjamin6e899c72016-06-09 18:02:18 -04001242
David Benjaminc895d6b2016-08-11 13:26:41 -04001243 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1244 if (msg_ret <= 0) {
1245 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001246 }
1247
1248 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1249 if (ssl->version == SSL3_VERSION &&
1250 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001251 /* In SSL 3.0, the Certificate message is omitted to signal no
1252 * certificate. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001253 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001254 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001255 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1256 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001257 }
1258
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001259 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1260 * classed by them as a bug, but it's assumed by at least NGINX. */
1261 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001262 ssl->s3->tmp.reuse_message = 1;
1263 return 1;
1264 }
1265
David Benjamin6e899c72016-06-09 18:02:18 -04001266 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001267 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1268 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001269 }
1270
David Benjaminc895d6b2016-08-11 13:26:41 -04001271 CBS certificate_msg;
1272 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
1273 uint8_t alert;
1274 STACK_OF(X509) *chain = ssl_parse_cert_chain(
1275 ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs
1276 ? ssl->s3->new_session->peer_sha256
1277 : NULL,
1278 &certificate_msg);
1279 if (chain == NULL) {
1280 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin6e899c72016-06-09 18:02:18 -04001281 goto err;
1282 }
1283
David Benjaminc895d6b2016-08-11 13:26:41 -04001284 if (CBS_len(&certificate_msg) != 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001285 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001286 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1287 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001288 }
1289
David Benjaminc895d6b2016-08-11 13:26:41 -04001290 if (sk_X509_num(chain) == 0) {
David Benjamin6e899c72016-06-09 18:02:18 -04001291 /* No client certificate so the handshake buffer may be discarded. */
1292 ssl3_free_handshake_buffer(ssl);
1293
David Benjaminc895d6b2016-08-11 13:26:41 -04001294 /* In SSL 3.0, sending no certificate is signaled by omitting the
1295 * Certificate message. */
David Benjamin6e899c72016-06-09 18:02:18 -04001296 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001297 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjaminc895d6b2016-08-11 13:26:41 -04001298 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1299 goto err;
1300 }
1301
1302 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -04001303 /* Fail for TLS only if we required a certificate */
1304 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001305 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1306 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001307 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001308
1309 /* OpenSSL returns X509_V_OK when no certificates are received. This is
1310 * classed by them as a bug, but it's assumed by at least NGINX. */
1311 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin6e899c72016-06-09 18:02:18 -04001312 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -04001313 /* The hash would have been filled in. */
1314 if (ssl->ctx->retain_only_sha256_of_client_certs) {
1315 ssl->s3->new_session->peer_sha256_valid = 1;
1316 }
1317
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001318 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1319 chain)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001320 goto err;
David Benjamin6e899c72016-06-09 18:02:18 -04001321 }
1322 }
1323
David Benjaminc895d6b2016-08-11 13:26:41 -04001324 X509_free(ssl->s3->new_session->peer);
1325 ssl->s3->new_session->peer = sk_X509_shift(chain);
David Benjamin6e899c72016-06-09 18:02:18 -04001326
David Benjaminc895d6b2016-08-11 13:26:41 -04001327 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
1328 ssl->s3->new_session->cert_chain = chain;
David Benjamin6e899c72016-06-09 18:02:18 -04001329 /* Inconsistency alert: cert_chain does *not* include the peer's own
1330 * certificate, while we do include it in s3_clnt.c */
1331
David Benjaminc895d6b2016-08-11 13:26:41 -04001332 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001333
1334err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001335 sk_X509_pop_free(chain, X509_free);
1336 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001337}
1338
1339static int ssl3_get_client_key_exchange(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001340 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001341 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001342 uint32_t alg_k;
1343 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001344 uint8_t *premaster_secret = NULL;
1345 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001346 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001347
Adam Langley4139edb2016-01-13 15:00:54 -08001348 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001349 uint8_t psk[PSK_MAX_PSK_LEN];
1350
David Benjamind316cba2016-06-02 16:17:39 -04001351 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001352 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1353 ssl_hash_message);
1354 if (ret <= 0) {
1355 return ret;
Kenny Roote99801b2015-11-06 15:31:15 -08001356 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001357 }
1358
Adam Langley4139edb2016-01-13 15:00:54 -08001359 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1360 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1361 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001362
1363 /* If using a PSK key exchange, prepare the pre-shared key. */
1364 if (alg_a & SSL_aPSK) {
1365 CBS psk_identity;
1366
1367 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1368 * then this is the only field in the message. */
1369 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1370 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001371 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001372 al = SSL_AD_DECODE_ERROR;
1373 goto f_err;
1374 }
1375
Adam Langley4139edb2016-01-13 15:00:54 -08001376 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001377 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001378 al = SSL_AD_INTERNAL_ERROR;
1379 goto f_err;
1380 }
1381
1382 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1383 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001384 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001385 al = SSL_AD_ILLEGAL_PARAMETER;
1386 goto f_err;
1387 }
1388
David Benjaminc895d6b2016-08-11 13:26:41 -04001389 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001390 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001391 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001392 goto f_err;
1393 }
1394
1395 /* Look up the key for the identity. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001396 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1397 psk, sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001398 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001399 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001400 al = SSL_AD_INTERNAL_ERROR;
1401 goto f_err;
1402 } else if (psk_len == 0) {
1403 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001404 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001405 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1406 goto f_err;
1407 }
1408 }
1409
1410 /* Depending on the key exchange method, compute |premaster_secret| and
1411 * |premaster_secret_len|. */
1412 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001413 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001414 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001415 decrypt_buf = OPENSSL_malloc(rsa_size);
1416 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001417 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001418 goto err;
1419 }
1420
Kenny Roote99801b2015-11-06 15:31:15 -08001421 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001422 size_t decrypt_len;
David Benjamind316cba2016-06-02 16:17:39 -04001423 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001424 if (!ssl_has_private_key(ssl) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001425 ssl_private_key_type(ssl) != NID_rsaEncryption) {
Kenny Roote99801b2015-11-06 15:31:15 -08001426 al = SSL_AD_HANDSHAKE_FAILURE;
1427 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1428 goto f_err;
1429 }
Adam Langley4139edb2016-01-13 15:00:54 -08001430 CBS encrypted_premaster_secret;
1431 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001432 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1433 &encrypted_premaster_secret) ||
1434 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001435 al = SSL_AD_DECODE_ERROR;
1436 OPENSSL_PUT_ERROR(SSL,
1437 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1438 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001439 }
1440 } else {
1441 encrypted_premaster_secret = client_key_exchange;
1442 }
1443
Kenny Roote99801b2015-11-06 15:31:15 -08001444 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1445 * timing-sensitive code below. */
1446 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001447 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001448 CBS_data(&encrypted_premaster_secret),
1449 CBS_len(&encrypted_premaster_secret));
1450 } else {
David Benjamind316cba2016-06-02 16:17:39 -04001451 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001452 /* Complete async decrypt. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001453 decrypt_result =
1454 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001455 }
Kenny Roote99801b2015-11-06 15:31:15 -08001456
1457 switch (decrypt_result) {
1458 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001459 break;
1460 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001461 goto err;
1462 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001463 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind316cba2016-06-02 16:17:39 -04001464 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001465 goto err;
1466 }
1467
David Benjamin4969cc92016-04-22 15:02:23 -04001468 if (decrypt_len != rsa_size) {
1469 al = SSL_AD_DECRYPT_ERROR;
1470 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1471 goto f_err;
1472 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001473
Adam Langley4139edb2016-01-13 15:00:54 -08001474 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1475 * section 7.4.7.1. */
1476 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1477 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001478 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001479 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001480 goto err;
1481 }
Adam Langley4139edb2016-01-13 15:00:54 -08001482 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1483 goto err;
1484 }
1485
1486 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1487 * publicly invalid. */
1488 if (decrypt_len < 11 + premaster_secret_len) {
1489 al = SSL_AD_DECRYPT_ERROR;
1490 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1491 goto f_err;
1492 }
1493
1494 /* Check the padding. See RFC 3447, section 7.2.2. */
1495 size_t padding_len = decrypt_len - premaster_secret_len;
1496 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1497 constant_time_eq_int_8(decrypt_buf[1], 2);
1498 size_t i;
1499 for (i = 2; i < padding_len - 1; i++) {
1500 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1501 }
1502 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1503
1504 /* The premaster secret must begin with |client_version|. This too must be
1505 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1506 good &= constant_time_eq_8(decrypt_buf[padding_len],
1507 (unsigned)(ssl->client_version >> 8));
1508 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
1509 (unsigned)(ssl->client_version & 0xff));
1510
1511 /* Select, in constant time, either the decrypted premaster or the random
1512 * premaster based on |good|. */
1513 for (i = 0; i < premaster_secret_len; i++) {
1514 premaster_secret[i] = constant_time_select_8(
1515 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1516 }
1517
Adam Langleyd9e397b2015-01-22 14:27:53 -08001518 OPENSSL_free(decrypt_buf);
1519 decrypt_buf = NULL;
David Benjamind316cba2016-06-02 16:17:39 -04001520 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1521 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001522 CBS peer_key;
David Benjamind316cba2016-06-02 16:17:39 -04001523 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1524 &peer_key) ||
1525 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001526 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001527 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001528 goto f_err;
1529 }
1530
Adam Langley4139edb2016-01-13 15:00:54 -08001531 /* Compute the premaster. */
1532 uint8_t alert;
David Benjamind316cba2016-06-02 16:17:39 -04001533 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1534 &premaster_secret_len, &alert, CBS_data(&peer_key),
1535 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001536 al = alert;
1537 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001538 }
1539
Adam Langley4139edb2016-01-13 15:00:54 -08001540 /* The key exchange state may now be discarded. */
1541 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001542 } else if (alg_k & SSL_kPSK) {
1543 /* For plain PSK, other_secret is a block of 0s with the same length as the
1544 * pre-shared key. */
1545 premaster_secret_len = psk_len;
1546 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1547 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001548 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001549 goto err;
1550 }
1551 memset(premaster_secret, 0, premaster_secret_len);
1552 } else {
1553 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001554 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001555 goto f_err;
1556 }
1557
1558 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1559 * pre-shared key. */
1560 if (alg_a & SSL_aPSK) {
1561 CBB new_premaster, child;
1562 uint8_t *new_data;
1563 size_t new_len;
1564
Kenny Rootb8494592015-09-25 02:29:14 +00001565 CBB_zero(&new_premaster);
1566 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1567 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001568 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1569 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1570 !CBB_add_bytes(&child, psk, psk_len) ||
1571 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001572 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001573 CBB_cleanup(&new_premaster);
1574 goto err;
1575 }
1576
1577 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1578 OPENSSL_free(premaster_secret);
1579 premaster_secret = new_data;
1580 premaster_secret_len = new_len;
1581 }
1582
1583 /* Compute the master secret */
David Benjaminc895d6b2016-08-11 13:26:41 -04001584 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1585 ssl, ssl->s3->new_session->master_key, premaster_secret,
1586 premaster_secret_len);
1587 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001588 goto err;
1589 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001590 ssl->s3->new_session->extended_master_secret =
1591 ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001592
1593 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1594 OPENSSL_free(premaster_secret);
1595 return 1;
1596
1597f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001598 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001599err:
Adam Langley4139edb2016-01-13 15:00:54 -08001600 if (premaster_secret != NULL) {
1601 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001602 OPENSSL_free(premaster_secret);
1603 }
Adam Langleye9ada862015-05-11 17:20:37 -07001604 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001605
1606 return -1;
1607}
1608
David Benjamin6e899c72016-06-09 18:02:18 -04001609static int ssl3_get_cert_verify(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001610 int al, ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001611 CBS certificate_verify, signature;
David Benjaminc895d6b2016-08-11 13:26:41 -04001612 X509 *peer = ssl->s3->new_session->peer;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001613 EVP_PKEY *pkey = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001614
1615 /* Only RSA and ECDSA client certificates are supported, so a
1616 * CertificateVerify is required if and only if there's a client certificate.
1617 * */
1618 if (peer == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001619 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001620 return 1;
1621 }
1622
David Benjaminc895d6b2016-08-11 13:26:41 -04001623 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1624 ssl_dont_hash_message);
1625 if (msg_ret <= 0) {
1626 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001627 }
1628
1629 /* Filter out unsupported certificate types. */
1630 pkey = X509_get_pubkey(peer);
Adam Langleye9ada862015-05-11 17:20:37 -07001631 if (pkey == NULL) {
1632 goto err;
1633 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001634
David Benjaminc895d6b2016-08-11 13:26:41 -04001635 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001636
1637 /* Determine the digest type if needbe. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001638 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001639 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001640 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001641 al = SSL_AD_DECODE_ERROR;
1642 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1643 goto f_err;
1644 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001645 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001646 goto f_err;
1647 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001648 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
1649 } else if (pkey->type == EVP_PKEY_RSA) {
1650 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1651 } else if (pkey->type == EVP_PKEY_EC) {
1652 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
1653 } else {
1654 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1655 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1656 goto f_err;
Adam Langleye9ada862015-05-11 17:20:37 -07001657 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001658
1659 /* Parse and verify the signature. */
1660 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1661 CBS_len(&certificate_verify) != 0) {
1662 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001663 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001664 goto f_err;
1665 }
1666
David Benjaminc895d6b2016-08-11 13:26:41 -04001667 int sig_ok;
1668 /* The SSL3 construction for CertificateVerify does not decompose into a
1669 * single final digest and signature, and must be special-cased. */
1670 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1671 const EVP_MD *md;
1672 uint8_t digest[EVP_MAX_MD_SIZE];
1673 size_t digest_len;
1674 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
1675 signature_algorithm)) {
1676 goto err;
1677 }
1678
1679 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1680 sig_ok = pctx != NULL &&
1681 EVP_PKEY_verify_init(pctx) &&
1682 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1683 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1684 digest, digest_len);
1685 EVP_PKEY_CTX_free(pctx);
1686 } else {
1687 sig_ok = ssl_public_key_verify(
1688 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1689 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1690 ssl->s3->handshake_buffer->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001691 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001692
David Benjamin4969cc92016-04-22 15:02:23 -04001693#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1694 sig_ok = 1;
1695 ERR_clear_error();
1696#endif
1697 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001698 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001699 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001700 goto f_err;
1701 }
1702
David Benjaminc895d6b2016-08-11 13:26:41 -04001703 /* The handshake buffer is no longer necessary, and we may hash the current
1704 * message.*/
1705 ssl3_free_handshake_buffer(ssl);
1706 if (!ssl->method->hash_current_message(ssl)) {
1707 goto err;
1708 }
1709
Adam Langleyd9e397b2015-01-22 14:27:53 -08001710 ret = 1;
1711
1712 if (0) {
1713 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001714 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001715 }
1716
1717err:
Adam Langleyd9e397b2015-01-22 14:27:53 -08001718 EVP_PKEY_free(pkey);
1719
1720 return ret;
1721}
1722
David Benjamin6e899c72016-06-09 18:02:18 -04001723/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1724 * sets the next_proto member in s if found */
1725static int ssl3_get_next_proto(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001726 int ret =
1727 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1728 if (ret <= 0) {
1729 return ret;
1730 }
1731
David Benjamin6e899c72016-06-09 18:02:18 -04001732 CBS next_protocol, selected_protocol, padding;
David Benjaminc895d6b2016-08-11 13:26:41 -04001733 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001734 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1735 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
1736 CBS_len(&next_protocol) != 0 ||
1737 !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
1738 &ssl->s3->next_proto_negotiated_len)) {
1739 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001740 }
1741
David Benjamin6e899c72016-06-09 18:02:18 -04001742 return 1;
1743}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001744
David Benjamin6e899c72016-06-09 18:02:18 -04001745/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
1746static int ssl3_get_channel_id(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001747 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001748 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
1749 size_t channel_id_hash_len;
1750 const uint8_t *p;
1751 uint16_t extension_type;
1752 EC_GROUP *p256 = NULL;
1753 EC_KEY *key = NULL;
1754 EC_POINT *point = NULL;
1755 ECDSA_SIG sig;
1756 BIGNUM x, y;
1757 CBS encrypted_extensions, extension;
1758
David Benjaminc895d6b2016-08-11 13:26:41 -04001759 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1760 ssl_dont_hash_message);
1761 if (msg_ret <= 0) {
1762 return msg_ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001763 }
1764
1765 /* Before incorporating the EncryptedExtensions message to the handshake
1766 * hash, compute the hash that should have been signed. */
1767 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
1768 return -1;
1769 }
1770 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
1771
David Benjaminc895d6b2016-08-11 13:26:41 -04001772 if (!ssl->method->hash_current_message(ssl)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001773 return -1;
1774 }
1775
David Benjaminc895d6b2016-08-11 13:26:41 -04001776 CBS_init(&encrypted_extensions, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001777
1778 /* EncryptedExtensions could include multiple extensions, but the only
David Benjaminc895d6b2016-08-11 13:26:41 -04001779 * extension that could be negotiated is Channel ID, so there can only be one
1780 * entry. */
David Benjamin6e899c72016-06-09 18:02:18 -04001781 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
1782 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
1783 CBS_len(&encrypted_extensions) != 0 ||
1784 extension_type != TLSEXT_TYPE_channel_id ||
1785 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
1786 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
1787 return -1;
1788 }
1789
1790 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
1791 if (!p256) {
1792 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
1793 return -1;
1794 }
1795
1796 BN_init(&x);
1797 BN_init(&y);
1798 sig.r = BN_new();
1799 sig.s = BN_new();
1800 if (sig.r == NULL || sig.s == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001801 goto err;
1802 }
1803
David Benjamin6e899c72016-06-09 18:02:18 -04001804 p = CBS_data(&extension);
1805 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
1806 BN_bin2bn(p + 32, 32, &y) == NULL ||
1807 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
1808 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
1809 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001810 }
1811
David Benjamin6e899c72016-06-09 18:02:18 -04001812 point = EC_POINT_new(p256);
1813 if (!point ||
1814 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
1815 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001816 }
1817
David Benjamin6e899c72016-06-09 18:02:18 -04001818 key = EC_KEY_new();
1819 if (!key || !EC_KEY_set_group(key, p256) ||
1820 !EC_KEY_set_public_key(key, point)) {
1821 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001822 }
1823
David Benjamin6e899c72016-06-09 18:02:18 -04001824 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
1825 * were called. */
1826 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
1827 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
1828 ssl->s3->tlsext_channel_id_valid = 0;
1829 goto err;
1830 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001831
David Benjamin6e899c72016-06-09 18:02:18 -04001832 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001833 ret = 1;
1834
Adam Langleyd9e397b2015-01-22 14:27:53 -08001835err:
David Benjamin6e899c72016-06-09 18:02:18 -04001836 BN_free(&x);
1837 BN_free(&y);
1838 BN_free(sig.r);
1839 BN_free(sig.s);
1840 EC_KEY_free(key);
1841 EC_POINT_free(point);
1842 EC_GROUP_free(p256);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001843 return ret;
1844}
1845
David Benjamin6e899c72016-06-09 18:02:18 -04001846static int ssl3_send_new_session_ticket(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001847 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
1848 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001849 }
1850
David Benjaminc895d6b2016-08-11 13:26:41 -04001851 CBB cbb, body, ticket;
1852 if (!ssl->method->init_message(ssl, &cbb, &body,
1853 SSL3_MT_NEW_SESSION_TICKET) ||
1854 /* Ticket lifetime hint (advisory only): We leave this unspecified for
1855 * resumed session (for simplicity), and guess that tickets for new
1856 * sessions will live as long as their sessions. */
1857 !CBB_add_u32(&body,
1858 ssl->session != NULL ? 0 : ssl->s3->new_session->timeout) ||
1859 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1860 !ssl_encrypt_ticket(ssl, &ticket, ssl->session != NULL
1861 ? ssl->session
1862 : ssl->s3->new_session) ||
1863 !ssl->method->finish_message(ssl, &cbb)) {
1864 return 0;
1865 }
Adam Langleye9ada862015-05-11 17:20:37 -07001866
David Benjaminc895d6b2016-08-11 13:26:41 -04001867 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
1868 return ssl->method->write_message(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001869}