blob: 50007eb401ce1b4608739f76657264ff1dd09119 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
Kenny Rootb8494592015-09-25 02:29:14 +0000149#include <openssl/ssl.h>
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <string.h>
153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
156#include <openssl/bytestring.h>
157#include <openssl/cipher.h>
158#include <openssl/dh.h>
159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700161#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400166#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/rand.h>
168#include <openssl/sha.h>
169#include <openssl/x509.h>
170
Adam Langleye9ada862015-05-11 17:20:37 -0700171#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172#include "../crypto/internal.h"
173#include "../crypto/dh/internal.h"
174
175
Adam Langley4139edb2016-01-13 15:00:54 -0800176int ssl3_accept(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177 BUF_MEM *buf = NULL;
Adam Langleye9ada862015-05-11 17:20:37 -0700178 uint32_t alg_a;
Kenny Roote99801b2015-11-06 15:31:15 -0800179 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 int ret = -1;
181 int new_state, state, skip = 0;
182
Adam Langley4139edb2016-01-13 15:00:54 -0800183 assert(ssl->handshake_func == ssl3_accept);
184 assert(ssl->server);
185 assert(!SSL_IS_DTLS(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187 ERR_clear_system_error();
188
Adam Langley4139edb2016-01-13 15:00:54 -0800189 if (ssl->info_callback != NULL) {
190 cb = ssl->info_callback;
191 } else if (ssl->ctx->info_callback != NULL) {
192 cb = ssl->ctx->info_callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193 }
194
Adam Langley4139edb2016-01-13 15:00:54 -0800195 if (ssl->cert == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000196 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197 return -1;
198 }
199
200 for (;;) {
Adam Langley4139edb2016-01-13 15:00:54 -0800201 state = ssl->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202
Adam Langley4139edb2016-01-13 15:00:54 -0800203 switch (ssl->state) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204 case SSL_ST_ACCEPT:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205 if (cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800206 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207 }
208
Adam Langley4139edb2016-01-13 15:00:54 -0800209 if (ssl->init_buf == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 buf = BUF_MEM_new();
David Benjamind316cba2016-06-02 16:17:39 -0400211 if (!buf || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 ret = -1;
213 goto end;
214 }
Adam Langley4139edb2016-01-13 15:00:54 -0800215 ssl->init_buf = buf;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216 buf = NULL;
217 }
Adam Langley4139edb2016-01-13 15:00:54 -0800218 ssl->init_num = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219
Adam Langleye9ada862015-05-11 17:20:37 -0700220 /* Enable a write buffer. This groups handshake messages within a flight
221 * into a single write. */
David Benjamind316cba2016-06-02 16:17:39 -0400222 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700223 ret = -1;
224 goto end;
225 }
226
Adam Langley4139edb2016-01-13 15:00:54 -0800227 if (!ssl3_init_handshake_buffer(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000228 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 ret = -1;
230 goto end;
231 }
232
Adam Langley4139edb2016-01-13 15:00:54 -0800233 if (!ssl->s3->have_version) {
234 ssl->state = SSL3_ST_SR_INITIAL_BYTES;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800236 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 break;
239
240 case SSL3_ST_SR_INITIAL_BYTES:
Adam Langley4139edb2016-01-13 15:00:54 -0800241 ret = ssl3_get_initial_bytes(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 if (ret <= 0) {
243 goto end;
244 }
Adam Langley4139edb2016-01-13 15:00:54 -0800245 /* ssl3_get_initial_bytes sets ssl->state to one of
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
247 break;
248
249 case SSL3_ST_SR_V2_CLIENT_HELLO:
Adam Langley4139edb2016-01-13 15:00:54 -0800250 ret = ssl3_get_v2_client_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 if (ret <= 0) {
252 goto end;
253 }
Adam Langley4139edb2016-01-13 15:00:54 -0800254 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 break;
256
257 case SSL3_ST_SR_CLNT_HELLO_A:
258 case SSL3_ST_SR_CLNT_HELLO_B:
259 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800260 ret = ssl3_get_client_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800261 if (ret <= 0) {
262 goto end;
263 }
Adam Langley4139edb2016-01-13 15:00:54 -0800264 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265 break;
266
267 case SSL3_ST_SW_SRVR_HELLO_A:
268 case SSL3_ST_SW_SRVR_HELLO_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800269 ret = ssl3_send_server_hello(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 if (ret <= 0) {
271 goto end;
272 }
Adam Langley4139edb2016-01-13 15:00:54 -0800273 if (ssl->hit) {
274 if (ssl->tlsext_ticket_expected) {
275 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800276 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800277 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278 }
279 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800280 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800281 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282 break;
283
284 case SSL3_ST_SW_CERT_A:
285 case SSL3_ST_SW_CERT_B:
David Benjamind316cba2016-06-02 16:17:39 -0400286 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800287 ret = ssl3_send_server_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800288 if (ret <= 0) {
289 goto end;
290 }
Adam Langley4139edb2016-01-13 15:00:54 -0800291 if (ssl->s3->tmp.certificate_status_expected) {
292 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800293 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800294 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800295 }
296 } else {
297 skip = 1;
Adam Langley4139edb2016-01-13 15:00:54 -0800298 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800299 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800300 break;
301
Kenny Rootb8494592015-09-25 02:29:14 +0000302 case SSL3_ST_SW_CERT_STATUS_A:
303 case SSL3_ST_SW_CERT_STATUS_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800304 ret = ssl3_send_certificate_status(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000305 if (ret <= 0) {
306 goto end;
307 }
Adam Langley4139edb2016-01-13 15:00:54 -0800308 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000309 break;
310
Adam Langleyd9e397b2015-01-22 14:27:53 -0800311 case SSL3_ST_SW_KEY_EXCH_A:
312 case SSL3_ST_SW_KEY_EXCH_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000313 case SSL3_ST_SW_KEY_EXCH_C:
Adam Langley4139edb2016-01-13 15:00:54 -0800314 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800315
316 /* Send a ServerKeyExchange message if:
317 * - The key exchange is ephemeral or anonymous
318 * Diffie-Hellman.
319 * - There is a PSK identity hint.
320 *
321 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
322 * this. In the meantime, keep them in sync. */
Adam Langley4139edb2016-01-13 15:00:54 -0800323 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
324 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
325 ret = ssl3_send_server_key_exchange(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -0700326 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700328 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 } else {
330 skip = 1;
331 }
332
Adam Langley4139edb2016-01-13 15:00:54 -0800333 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 break;
335
336 case SSL3_ST_SW_CERT_REQ_A:
337 case SSL3_ST_SW_CERT_REQ_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800338 if (ssl->s3->tmp.cert_request) {
339 ret = ssl3_send_certificate_request(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800340 if (ret <= 0) {
341 goto end;
342 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700343 } else {
344 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345 }
Adam Langley4139edb2016-01-13 15:00:54 -0800346 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800347 break;
348
349 case SSL3_ST_SW_SRVR_DONE_A:
350 case SSL3_ST_SW_SRVR_DONE_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800351 ret = ssl3_send_server_done(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 if (ret <= 0) {
353 goto end;
354 }
Adam Langley4139edb2016-01-13 15:00:54 -0800355 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
356 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800357 break;
358
359 case SSL3_ST_SW_FLUSH:
360 /* This code originally checked to see if any data was pending using
361 * BIO_CTRL_INFO and then flushed. This caused problems as documented
362 * in PR#1939. The proposed fix doesn't completely resolve this issue
363 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
364 * we just flush unconditionally. */
Adam Langley4139edb2016-01-13 15:00:54 -0800365 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400366 ssl->rwstate = SSL_WRITING;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 ret = -1;
368 goto end;
369 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370
Adam Langley4139edb2016-01-13 15:00:54 -0800371 ssl->state = ssl->s3->tmp.next_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 break;
373
374 case SSL3_ST_SR_CERT_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800375 if (ssl->s3->tmp.cert_request) {
376 ret = ssl3_get_client_certificate(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800377 if (ret <= 0) {
378 goto end;
379 }
380 }
Adam Langley4139edb2016-01-13 15:00:54 -0800381 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382 break;
383
384 case SSL3_ST_SR_KEY_EXCH_A:
385 case SSL3_ST_SR_KEY_EXCH_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800386 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800387 if (ret <= 0) {
388 goto end;
389 }
Adam Langley4139edb2016-01-13 15:00:54 -0800390 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800391 break;
392
393 case SSL3_ST_SR_CERT_VRFY_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800394 ret = ssl3_get_cert_verify(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800395 if (ret <= 0) {
396 goto end;
397 }
398
Adam Langley4139edb2016-01-13 15:00:54 -0800399 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800400 break;
401
Adam Langley4139edb2016-01-13 15:00:54 -0800402 case SSL3_ST_SR_CHANGE:
403 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
404 if (ret <= 0) {
405 goto end;
406 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800407
David Benjamin4969cc92016-04-22 15:02:23 -0400408 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 ret = -1;
410 goto end;
411 }
Adam Langley4139edb2016-01-13 15:00:54 -0800412
413 if (ssl->s3->next_proto_neg_seen) {
414 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
415 } else if (ssl->s3->tlsext_channel_id_valid) {
416 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800418 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800419 }
420 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800421
422 case SSL3_ST_SR_NEXT_PROTO_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800423 ret = ssl3_get_next_proto(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800424 if (ret <= 0) {
425 goto end;
426 }
Adam Langley4139edb2016-01-13 15:00:54 -0800427 if (ssl->s3->tlsext_channel_id_valid) {
428 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800430 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800431 }
432 break;
433
434 case SSL3_ST_SR_CHANNEL_ID_A:
Adam Langley4139edb2016-01-13 15:00:54 -0800435 ret = ssl3_get_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 if (ret <= 0) {
437 goto end;
438 }
Adam Langley4139edb2016-01-13 15:00:54 -0800439 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440 break;
441
442 case SSL3_ST_SR_FINISHED_A:
David Benjamind316cba2016-06-02 16:17:39 -0400443 ret = ssl3_get_finished(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800444 if (ret <= 0) {
445 goto end;
446 }
447
Adam Langley4139edb2016-01-13 15:00:54 -0800448 if (ssl->hit) {
449 ssl->state = SSL_ST_OK;
450 } else if (ssl->tlsext_ticket_expected) {
451 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800452 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800453 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800454 }
455 /* If this is a full handshake with ChannelID then record the hashshake
Adam Langley4139edb2016-01-13 15:00:54 -0800456 * hashes in |ssl->session| in case we need them to verify a ChannelID
Adam Langleyd9e397b2015-01-22 14:27:53 -0800457 * signature on a resumption of this session in the future. */
Adam Langley4139edb2016-01-13 15:00:54 -0800458 if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
459 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800460 if (ret <= 0) {
461 goto end;
462 }
463 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800464 break;
465
466 case SSL3_ST_SW_SESSION_TICKET_A:
467 case SSL3_ST_SW_SESSION_TICKET_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800468 ret = ssl3_send_new_session_ticket(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800469 if (ret <= 0) {
470 goto end;
471 }
Adam Langley4139edb2016-01-13 15:00:54 -0800472 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800473 break;
474
475 case SSL3_ST_SW_CHANGE_A:
476 case SSL3_ST_SW_CHANGE_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800477 ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800478 SSL3_ST_SW_CHANGE_B);
479 if (ret <= 0) {
480 goto end;
481 }
Adam Langley4139edb2016-01-13 15:00:54 -0800482 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800483
David Benjamin4969cc92016-04-22 15:02:23 -0400484 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800485 ret = -1;
486 goto end;
487 }
488 break;
489
490 case SSL3_ST_SW_FINISHED_A:
491 case SSL3_ST_SW_FINISHED_B:
Adam Langley4139edb2016-01-13 15:00:54 -0800492 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjamin4969cc92016-04-22 15:02:23 -0400493 SSL3_ST_SW_FINISHED_B);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800494 if (ret <= 0) {
495 goto end;
496 }
Adam Langley4139edb2016-01-13 15:00:54 -0800497 ssl->state = SSL3_ST_SW_FLUSH;
498 if (ssl->hit) {
499 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800501 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800503 break;
504
505 case SSL_ST_OK:
506 /* clean a few things up */
Adam Langley4139edb2016-01-13 15:00:54 -0800507 ssl3_cleanup_key_block(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800508
Adam Langley4139edb2016-01-13 15:00:54 -0800509 BUF_MEM_free(ssl->init_buf);
510 ssl->init_buf = NULL;
David Benjamind316cba2016-06-02 16:17:39 -0400511 ssl->init_num = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800512
513 /* remove buffering on output */
Adam Langley4139edb2016-01-13 15:00:54 -0800514 ssl_free_wbio_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800515
Adam Langleyd9e397b2015-01-22 14:27:53 -0800516
517 /* If we aren't retaining peer certificates then we can discard it
518 * now. */
Adam Langley4139edb2016-01-13 15:00:54 -0800519 if (ssl->ctx->retain_only_sha256_of_client_certs) {
520 X509_free(ssl->session->peer);
521 ssl->session->peer = NULL;
522 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
523 ssl->session->cert_chain = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800524 }
525
Adam Langley4139edb2016-01-13 15:00:54 -0800526 ssl->s3->initial_handshake_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800527
Adam Langley4139edb2016-01-13 15:00:54 -0800528 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800529
Adam Langleyf4e42722015-06-04 17:45:09 -0700530 if (cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800531 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800532 }
533
534 ret = 1;
535 goto end;
536
537 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000538 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800539 ret = -1;
540 goto end;
541 }
542
Adam Langley4139edb2016-01-13 15:00:54 -0800543 if (!ssl->s3->tmp.reuse_message && !skip && cb != NULL &&
544 ssl->state != state) {
545 new_state = ssl->state;
546 ssl->state = state;
547 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
548 ssl->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800549 }
550 skip = 0;
551 }
552
553end:
Adam Langleye9ada862015-05-11 17:20:37 -0700554 BUF_MEM_free(buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800555 if (cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800556 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800557 }
558 return ret;
559}
560
Adam Langley4139edb2016-01-13 15:00:54 -0800561int ssl3_get_initial_bytes(SSL *ssl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000562 /* Read the first 5 bytes, the size of the TLS record header. This is
563 * sufficient to detect a V2ClientHello and ensures that we never read beyond
564 * the first record. */
Adam Langley4139edb2016-01-13 15:00:54 -0800565 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800566 if (ret <= 0) {
567 return ret;
568 }
Adam Langley4139edb2016-01-13 15:00:54 -0800569 assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
570 const uint8_t *p = ssl_read_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800571
572 /* Some dedicated error codes for protocol mixups should the application wish
573 * to interpret them differently. (These do not overlap with ClientHello or
574 * V2ClientHello.) */
575 if (strncmp("GET ", (const char *)p, 4) == 0 ||
576 strncmp("POST ", (const char *)p, 5) == 0 ||
577 strncmp("HEAD ", (const char *)p, 5) == 0 ||
578 strncmp("PUT ", (const char *)p, 4) == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000579 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800580 return -1;
581 }
Kenny Rootb8494592015-09-25 02:29:14 +0000582 if (strncmp("CONNE", (const char *)p, 5) == 0) {
583 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800584 return -1;
585 }
586
Kenny Rootb8494592015-09-25 02:29:14 +0000587 /* Determine if this is a V2ClientHello. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800588 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
589 p[3] >= SSL3_VERSION_MAJOR) {
590 /* This is a V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800591 ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800592 return 1;
593 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800594
Kenny Rootb8494592015-09-25 02:29:14 +0000595 /* Fall through to the standard logic. */
Adam Langley4139edb2016-01-13 15:00:54 -0800596 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000597 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800598}
599
Adam Langley4139edb2016-01-13 15:00:54 -0800600int ssl3_get_v2_client_hello(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800601 const uint8_t *p;
602 int ret;
603 CBS v2_client_hello, cipher_specs, session_id, challenge;
David Benjamind316cba2016-06-02 16:17:39 -0400604 size_t msg_length, rand_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800605 uint8_t msg_type;
606 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
607 CBB client_hello, hello_body, cipher_suites;
608 uint8_t random[SSL3_RANDOM_SIZE];
609
Kenny Rootb8494592015-09-25 02:29:14 +0000610 /* Determine the length of the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800611 assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
612 p = ssl_read_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613 msg_length = ((p[0] & 0x7f) << 8) | p[1];
614 if (msg_length > (1024 * 4)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000615 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800616 return -1;
617 }
Kenny Rootb8494592015-09-25 02:29:14 +0000618 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
619 /* Reject lengths that are too short early. We have already read
620 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
621 * (invalid) V2ClientHello which would be shorter than that. */
622 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800623 return -1;
624 }
625
Kenny Rootb8494592015-09-25 02:29:14 +0000626 /* Read the remainder of the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800627 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800628 if (ret <= 0) {
629 return ret;
630 }
Adam Langley4139edb2016-01-13 15:00:54 -0800631 assert(ssl_read_buffer_len(ssl) == msg_length + 2);
632 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800633
Kenny Rootb8494592015-09-25 02:29:14 +0000634 /* The V2ClientHello without the length is incorporated into the handshake
Adam Langleyd9e397b2015-01-22 14:27:53 -0800635 * hash. */
Adam Langley4139edb2016-01-13 15:00:54 -0800636 if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
Kenny Rootb8494592015-09-25 02:29:14 +0000637 CBS_len(&v2_client_hello))) {
Adam Langleye9ada862015-05-11 17:20:37 -0700638 return -1;
639 }
Adam Langley4139edb2016-01-13 15:00:54 -0800640 if (ssl->msg_callback) {
641 ssl->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
642 CBS_len(&v2_client_hello), ssl, ssl->msg_callback_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800643 }
644
645 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
646 !CBS_get_u16(&v2_client_hello, &version) ||
647 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
648 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
649 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
650 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
651 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
652 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
653 CBS_len(&v2_client_hello) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000654 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800655 return -1;
656 }
657
658 /* msg_type has already been checked. */
659 assert(msg_type == SSL2_MT_CLIENT_HELLO);
660
661 /* The client_random is the V2ClientHello challenge. Truncate or
662 * left-pad with zeros as needed. */
663 memset(random, 0, SSL3_RANDOM_SIZE);
664 rand_len = CBS_len(&challenge);
665 if (rand_len > SSL3_RANDOM_SIZE) {
666 rand_len = SSL3_RANDOM_SIZE;
667 }
668 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
669 rand_len);
670
671 /* Write out an equivalent SSLv3 ClientHello. */
Kenny Rootb8494592015-09-25 02:29:14 +0000672 CBB_zero(&client_hello);
Adam Langley4139edb2016-01-13 15:00:54 -0800673 if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
674 ssl->init_buf->max) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000675 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800676 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
677 !CBB_add_u16(&hello_body, version) ||
678 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
679 /* No session id. */
680 !CBB_add_u8(&hello_body, 0) ||
681 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
682 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000683 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800684 return -1;
685 }
686
687 /* Copy the cipher suites. */
688 while (CBS_len(&cipher_specs) > 0) {
689 uint32_t cipher_spec;
690 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
691 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000692 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800693 return -1;
694 }
695
696 /* Skip SSLv2 ciphers. */
697 if ((cipher_spec & 0xff0000) != 0) {
698 continue;
699 }
700 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
701 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000702 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800703 return -1;
704 }
705 }
706
707 /* Add the null compression scheme and finish. */
708 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
David Benjamind316cba2016-06-02 16:17:39 -0400709 !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800710 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000711 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800712 return -1;
713 }
714
715 /* Mark the message for "re"-use by the version-specific method. */
Adam Langley4139edb2016-01-13 15:00:54 -0800716 ssl->s3->tmp.reuse_message = 1;
717 ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
David Benjamind316cba2016-06-02 16:17:39 -0400718 ssl->s3->tmp.message_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800719
Kenny Rootb8494592015-09-25 02:29:14 +0000720 /* Consume and discard the V2ClientHello. */
Adam Langley4139edb2016-01-13 15:00:54 -0800721 ssl_read_buffer_consume(ssl, 2 + msg_length);
722 ssl_read_buffer_discard(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800723
724 return 1;
725}
726
Adam Langley4139edb2016-01-13 15:00:54 -0800727int ssl3_get_client_hello(SSL *ssl) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700728 int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800729 long n;
730 const SSL_CIPHER *c;
731 STACK_OF(SSL_CIPHER) *ciphers = NULL;
732 struct ssl_early_callback_ctx early_ctx;
733 CBS client_hello;
734 uint16_t client_version;
735 CBS client_random, session_id, cipher_suites, compression_methods;
Kenny Rootb8494592015-09-25 02:29:14 +0000736 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800737
738 /* We do this so that we will respond with our native type. If we are TLSv1
739 * and we get SSLv3, we will respond with TLSv1, This down switching should
740 * be handled by a different method. If we are SSLv3, we will respond with
741 * SSLv3, even if prompted with TLSv1. */
Adam Langley4139edb2016-01-13 15:00:54 -0800742 switch (ssl->state) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800743 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamind316cba2016-06-02 16:17:39 -0400744 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
745 ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800746
747 if (!ok) {
748 return n;
749 }
750
David Benjamind316cba2016-06-02 16:17:39 -0400751 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800752 /* fallthrough */
David Benjamind316cba2016-06-02 16:17:39 -0400753 case SSL3_ST_SR_CLNT_HELLO_B:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800754 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800755 /* We have previously parsed the ClientHello message, and can't call
756 * ssl_get_message again without hashing the message into the Finished
757 * digest again. */
Adam Langley4139edb2016-01-13 15:00:54 -0800758 n = ssl->init_num;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800759
760 memset(&early_ctx, 0, sizeof(early_ctx));
Adam Langley4139edb2016-01-13 15:00:54 -0800761 early_ctx.ssl = ssl;
762 early_ctx.client_hello = ssl->init_msg;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800763 early_ctx.client_hello_len = n;
764 if (!ssl_early_callback_init(&early_ctx)) {
765 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000766 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800767 goto f_err;
768 }
769
David Benjamind316cba2016-06-02 16:17:39 -0400770 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B &&
Adam Langley4139edb2016-01-13 15:00:54 -0800771 ssl->ctx->select_certificate_cb != NULL) {
David Benjamind316cba2016-06-02 16:17:39 -0400772 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
Adam Langley4139edb2016-01-13 15:00:54 -0800773 switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800774 case 0:
Adam Langley4139edb2016-01-13 15:00:54 -0800775 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
Adam Langleye9ada862015-05-11 17:20:37 -0700776 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800777
778 case -1:
779 /* Connection rejected. */
780 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000781 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800782 goto f_err;
783
784 default:
785 /* fallthrough */;
786 }
787 }
David Benjamind316cba2016-06-02 16:17:39 -0400788 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800789 break;
790
791 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000792 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800793 return -1;
794 }
795
Adam Langley4139edb2016-01-13 15:00:54 -0800796 CBS_init(&client_hello, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800797 if (!CBS_get_u16(&client_hello, &client_version) ||
798 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
799 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
800 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
801 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000802 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800803 goto f_err;
804 }
805
806 /* use version from inside client hello, not from record header (may differ:
807 * see RFC 2246, Appendix E, second paragraph) */
Adam Langley4139edb2016-01-13 15:00:54 -0800808 ssl->client_version = client_version;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800809
810 /* Load the client random. */
Adam Langley4139edb2016-01-13 15:00:54 -0800811 memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800812
Adam Langley4139edb2016-01-13 15:00:54 -0800813 if (SSL_IS_DTLS(ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800814 CBS cookie;
815
816 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
817 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
818 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000819 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800820 goto f_err;
821 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800822 }
823
Adam Langleyf4e42722015-06-04 17:45:09 -0700824 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
825 * asynchronously.
826 *
827 * TODO(davidben): Clean up the order of events around ClientHello
828 * processing. */
Adam Langley4139edb2016-01-13 15:00:54 -0800829 if (!ssl->s3->have_version) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800830 /* Select version to use */
Adam Langley4139edb2016-01-13 15:00:54 -0800831 uint16_t version = ssl3_get_mutual_version(ssl, client_version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800832 if (version == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000833 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
Adam Langley4139edb2016-01-13 15:00:54 -0800834 ssl->version = ssl->client_version;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800835 al = SSL_AD_PROTOCOL_VERSION;
836 goto f_err;
837 }
Adam Langley4139edb2016-01-13 15:00:54 -0800838 ssl->version = version;
David Benjamin4969cc92016-04-22 15:02:23 -0400839 ssl->s3->enc_method = ssl3_get_enc_method(version);
840 assert(ssl->s3->enc_method != NULL);
Adam Langley4139edb2016-01-13 15:00:54 -0800841 /* At this point, the connection's version is known and |ssl->version| is
Adam Langleyd9e397b2015-01-22 14:27:53 -0800842 * fixed. Begin enforcing the record-layer version. */
Adam Langley4139edb2016-01-13 15:00:54 -0800843 ssl->s3->have_version = 1;
844 } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
845 : (ssl->client_version < ssl->version)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000846 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800847 al = SSL_AD_PROTOCOL_VERSION;
848 goto f_err;
849 }
850
Adam Langley4139edb2016-01-13 15:00:54 -0800851 ssl->hit = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000852 int send_new_ticket = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800853 switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000854 case ssl_session_success:
855 break;
856 case ssl_session_error:
857 goto err;
858 case ssl_session_retry:
Adam Langley4139edb2016-01-13 15:00:54 -0800859 ssl->rwstate = SSL_PENDING_SESSION;
Kenny Rootb8494592015-09-25 02:29:14 +0000860 goto err;
Adam Langleyf4e42722015-06-04 17:45:09 -0700861 }
Adam Langley4139edb2016-01-13 15:00:54 -0800862 ssl->tlsext_ticket_expected = send_new_ticket;
Adam Langleyf4e42722015-06-04 17:45:09 -0700863
864 /* The EMS state is needed when making the resumption decision, but
865 * extensions are not normally parsed until later. This detects the EMS
866 * extension for the resumption decision and it's checked against the result
867 * of the normal parse later in this function. */
868 const uint8_t *ems_data;
869 size_t ems_len;
870 int have_extended_master_secret =
Adam Langley4139edb2016-01-13 15:00:54 -0800871 ssl->version != SSL3_VERSION &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700872 SSL_early_callback_ctx_extension_get(&early_ctx,
873 TLSEXT_TYPE_extended_master_secret,
874 &ems_data, &ems_len) &&
875 ems_len == 0;
876
Kenny Rootb8494592015-09-25 02:29:14 +0000877 if (session != NULL) {
878 if (session->extended_master_secret &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700879 !have_extended_master_secret) {
880 /* A ClientHello without EMS that attempts to resume a session with EMS
881 * is fatal to the connection. */
882 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000883 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -0700884 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800885 }
886
Adam Langley4139edb2016-01-13 15:00:54 -0800887 ssl->hit =
Adam Langleyf4e42722015-06-04 17:45:09 -0700888 /* Only resume if the session's version matches the negotiated version:
889 * most clients do not accept a mismatch. */
Adam Langley4139edb2016-01-13 15:00:54 -0800890 ssl->version == session->ssl_version &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700891 /* If the client offers the EMS extension, but the previous session
892 * didn't use it, then negotiate a new session. */
Kenny Rootb8494592015-09-25 02:29:14 +0000893 have_extended_master_secret == session->extended_master_secret;
Adam Langleyf4e42722015-06-04 17:45:09 -0700894 }
895
Adam Langley4139edb2016-01-13 15:00:54 -0800896 if (ssl->hit) {
Kenny Rootb8494592015-09-25 02:29:14 +0000897 /* Use the new session. */
Adam Langley4139edb2016-01-13 15:00:54 -0800898 SSL_SESSION_free(ssl->session);
899 ssl->session = session;
Kenny Rootb8494592015-09-25 02:29:14 +0000900 session = NULL;
901
Adam Langley4139edb2016-01-13 15:00:54 -0800902 ssl->verify_result = ssl->session->verify_result;
Kenny Roote99801b2015-11-06 15:31:15 -0800903 } else {
Adam Langley4139edb2016-01-13 15:00:54 -0800904 if (!ssl_get_new_session(ssl, 1 /* server */)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800905 goto err;
906 }
907
908 /* Clear the session ID if we want the session to be single-use. */
Adam Langley4139edb2016-01-13 15:00:54 -0800909 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
910 ssl->session->session_id_length = 0;
Kenny Roote99801b2015-11-06 15:31:15 -0800911 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800912 }
913
Adam Langley4139edb2016-01-13 15:00:54 -0800914 if (ssl->ctx->dos_protection_cb != NULL &&
915 ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700916 /* Connection rejected for DOS reasons. */
917 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000918 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleye9ada862015-05-11 17:20:37 -0700919 goto f_err;
920 }
921
Adam Langleyd9e397b2015-01-22 14:27:53 -0800922 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
Adam Langleye9ada862015-05-11 17:20:37 -0700923 CBS_len(&cipher_suites) == 0 ||
924 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800925 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
926 CBS_len(&compression_methods) == 0) {
927 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000928 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800929 goto f_err;
930 }
931
Adam Langley4139edb2016-01-13 15:00:54 -0800932 ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800933 if (ciphers == NULL) {
934 goto err;
935 }
936
937 /* If it is a hit, check that the cipher is in the list. */
Adam Langley4139edb2016-01-13 15:00:54 -0800938 if (ssl->hit) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800939 size_t j;
940 int found_cipher = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800941 uint32_t id = ssl->session->cipher->id;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800942
943 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
944 c = sk_SSL_CIPHER_value(ciphers, j);
945 if (c->id == id) {
946 found_cipher = 1;
947 break;
948 }
949 }
950
951 if (!found_cipher) {
952 /* we need to have the cipher in the cipher list if we are asked to reuse
953 * it */
954 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000955 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800956 goto f_err;
957 }
958 }
959
960 /* Only null compression is supported. */
961 if (memchr(CBS_data(&compression_methods), 0,
962 CBS_len(&compression_methods)) == NULL) {
963 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000964 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800965 goto f_err;
966 }
967
968 /* TLS extensions. */
Adam Langley4139edb2016-01-13 15:00:54 -0800969 if (ssl->version >= SSL3_VERSION &&
970 !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000971 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800972 goto err;
973 }
974
975 /* There should be nothing left over in the record. */
976 if (CBS_len(&client_hello) != 0) {
977 /* wrong packet length */
978 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000979 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800980 goto f_err;
981 }
982
Adam Langley4139edb2016-01-13 15:00:54 -0800983 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700984 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000985 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
Adam Langleyf4e42722015-06-04 17:45:09 -0700986 goto f_err;
987 }
988
Adam Langleyd9e397b2015-01-22 14:27:53 -0800989 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
Adam Langley4139edb2016-01-13 15:00:54 -0800990 if (!ssl->hit) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800991 if (ciphers == NULL) {
992 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000993 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800994 goto f_err;
995 }
996
997 /* Let cert callback update server certificates if required */
Adam Langley4139edb2016-01-13 15:00:54 -0800998 if (ssl->cert->cert_cb) {
999 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001000 if (rv == 0) {
1001 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001002 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001003 goto f_err;
1004 }
1005 if (rv < 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001006 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001007 goto err;
1008 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001009 }
Adam Langley4139edb2016-01-13 15:00:54 -08001010 c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001011
1012 if (c == NULL) {
1013 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001014 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001015 goto f_err;
1016 }
David Benjamin4969cc92016-04-22 15:02:23 -04001017 ssl->session->cipher = c;
Adam Langley4139edb2016-01-13 15:00:54 -08001018 ssl->s3->tmp.new_cipher = c;
Adam Langleyf4e42722015-06-04 17:45:09 -07001019
1020 /* Determine whether to request a client certificate. */
Adam Langley4139edb2016-01-13 15:00:54 -08001021 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Adam Langleyf4e42722015-06-04 17:45:09 -07001022 /* Only request a certificate if Channel ID isn't negotiated. */
Adam Langley4139edb2016-01-13 15:00:54 -08001023 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1024 ssl->s3->tlsext_channel_id_valid) {
1025 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001026 }
David Benjamind316cba2016-06-02 16:17:39 -04001027 /* CertificateRequest may only be sent in certificate-based ciphers. */
1028 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001029 ssl->s3->tmp.cert_request = 0;
Adam Langleyf4e42722015-06-04 17:45:09 -07001030 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001031 } else {
1032 /* Session-id reuse */
Adam Langley4139edb2016-01-13 15:00:54 -08001033 ssl->s3->tmp.new_cipher = ssl->session->cipher;
1034 ssl->s3->tmp.cert_request = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001035 }
1036
Kenny Rootb8494592015-09-25 02:29:14 +00001037 /* Now that the cipher is known, initialize the handshake hash. */
Adam Langley4139edb2016-01-13 15:00:54 -08001038 if (!ssl3_init_handshake_hash(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001039 goto f_err;
1040 }
1041
Adam Langleyf4e42722015-06-04 17:45:09 -07001042 /* In TLS 1.2, client authentication requires hashing the handshake transcript
1043 * under a different hash. Otherwise, release the handshake buffer. */
David Benjamin4969cc92016-04-22 15:02:23 -04001044 if (!ssl->s3->tmp.cert_request ||
1045 ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langley4139edb2016-01-13 15:00:54 -08001046 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001047 }
1048
1049 /* we now have the following setup;
1050 * client_random
1051 * cipher_list - our prefered list of ciphers
1052 * ciphers - the clients prefered list of ciphers
1053 * compression - basically ignored right now
1054 * ssl version is set - sslv3
Adam Langley4139edb2016-01-13 15:00:54 -08001055 * ssl->session - The ssl session has been setup.
1056 * ssl->hit - session reuse flag
1057 * ssl->tmp.new_cipher - the new cipher to use. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001058
Adam Langley4139edb2016-01-13 15:00:54 -08001059 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001060
1061 if (0) {
1062 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001063 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001064 }
1065
1066err:
Adam Langleye9ada862015-05-11 17:20:37 -07001067 sk_SSL_CIPHER_free(ciphers);
Kenny Rootb8494592015-09-25 02:29:14 +00001068 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001069 return ret;
1070}
1071
Kenny Roote99801b2015-11-06 15:31:15 -08001072int ssl3_send_server_hello(SSL *ssl) {
1073 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1074 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001075 }
1076
Kenny Roote99801b2015-11-06 15:31:15 -08001077 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1078
1079 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1080 * known attack while we fix ChannelID itself. */
1081 if (ssl->s3->tlsext_channel_id_valid &&
1082 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1083 ssl->s3->tlsext_channel_id_valid = 0;
1084 }
1085
1086 /* If this is a resumption and the original handshake didn't support
1087 * ChannelID then we didn't record the original handshake hashes in the
1088 * session and so cannot resume with ChannelIDs. */
1089 if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1090 ssl->s3->tlsext_channel_id_valid = 0;
1091 }
1092
1093 if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1094 1 /* server */)) {
1095 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1096 return -1;
1097 }
1098
1099 CBB cbb, session_id;
1100 size_t length;
1101 CBB_zero(&cbb);
1102 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1103 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1104 !CBB_add_u16(&cbb, ssl->version) ||
1105 !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1106 !CBB_add_u8_length_prefixed(&cbb, &session_id) ||
1107 !CBB_add_bytes(&session_id, ssl->session->session_id,
1108 ssl->session->session_id_length) ||
1109 !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1110 !CBB_add_u8(&cbb, 0 /* no compression */) ||
1111 !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1112 !CBB_finish(&cbb, NULL, &length) ||
1113 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1114 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1115 CBB_cleanup(&cbb);
1116 return -1;
1117 }
1118
1119 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1120 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001121}
1122
Kenny Rootb8494592015-09-25 02:29:14 +00001123int ssl3_send_certificate_status(SSL *ssl) {
1124 if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1125 CBB out, ocsp_response;
1126 size_t length;
1127
1128 CBB_zero(&out);
1129 if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1130 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1131 !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
1132 !CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
1133 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1134 ssl->ctx->ocsp_response_length) ||
1135 !CBB_finish(&out, NULL, &length) ||
1136 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1137 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1138 CBB_cleanup(&out);
1139 return -1;
1140 }
1141
1142 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1143 }
1144
1145 /* SSL3_ST_SW_CERT_STATUS_B */
1146 return ssl_do_write(ssl);
1147}
1148
Adam Langley4139edb2016-01-13 15:00:54 -08001149int ssl3_send_server_done(SSL *ssl) {
1150 if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
1151 if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_DONE, 0)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001152 return -1;
1153 }
Adam Langley4139edb2016-01-13 15:00:54 -08001154 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001155 }
1156
1157 /* SSL3_ST_SW_SRVR_DONE_B */
Adam Langley4139edb2016-01-13 15:00:54 -08001158 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001159}
1160
Adam Langley4139edb2016-01-13 15:00:54 -08001161int ssl3_send_server_key_exchange(SSL *ssl) {
1162 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
1163 return ssl_do_write(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001164 }
1165
Adam Langley4139edb2016-01-13 15:00:54 -08001166 CBB cbb, child;
1167 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1168 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1169 goto err;
Kenny Rootb8494592015-09-25 02:29:14 +00001170 }
1171
Adam Langley4139edb2016-01-13 15:00:54 -08001172 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1173 /* This is the first iteration, so write parameters. */
1174 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1175 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001176
Adam Langley4139edb2016-01-13 15:00:54 -08001177 /* PSK ciphers begin with an identity hint. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001178 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -08001179 size_t len =
1180 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1181 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1182 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1183 len)) {
1184 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001185 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001186 }
1187
Adam Langleye9ada862015-05-11 17:20:37 -07001188 if (alg_k & SSL_kDHE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001189 /* Determine the group to use. */
1190 DH *params = ssl->cert->dh_tmp;
1191 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1192 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001193 }
Adam Langley4139edb2016-01-13 15:00:54 -08001194 if (params == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001195 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001196 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001197 goto err;
1198 }
Adam Langley4139edb2016-01-13 15:00:54 -08001199 ssl->session->key_exchange_info = DH_num_bits(params);
1200
1201 /* Set up DH, generate a key, and emit the public half. */
1202 DH *dh = DHparams_dup(params);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001203 if (dh == NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -07001204 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001205 }
1206
Adam Langley4139edb2016-01-13 15:00:54 -08001207 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1208 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1209 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1210 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1211 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1212 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001213 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001214 goto err;
1215 }
Adam Langleye9ada862015-05-11 17:20:37 -07001216 } else if (alg_k & SSL_kECDHE) {
David Benjamind316cba2016-06-02 16:17:39 -04001217 /* Determine the group to use. */
1218 uint16_t group_id;
1219 if (!tls1_get_shared_group(ssl, &group_id)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001220 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001221 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001222 goto err;
1223 }
David Benjamind316cba2016-06-02 16:17:39 -04001224 ssl->session->key_exchange_info = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001225
1226 /* Set up ECDH, generate a key, and emit the public half. */
David Benjamind316cba2016-06-02 16:17:39 -04001227 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001228 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
David Benjamind316cba2016-06-02 16:17:39 -04001229 !CBB_add_u16(&cbb, group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001230 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjamind316cba2016-06-02 16:17:39 -04001231 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1232 goto err;
1233 }
1234 } else if (alg_k & SSL_kCECPQ1) {
1235 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1) ||
1236 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1237 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001238 goto err;
1239 }
Adam Langley4139edb2016-01-13 15:00:54 -08001240 } else {
1241 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001242 }
1243
Adam Langley4139edb2016-01-13 15:00:54 -08001244 /* Otherwise, restore |cbb| from the previous iteration.
1245 * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler
1246 * pattern. Probably keep the |CBB| around in the handshake state. */
1247 } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) {
1248 goto err;
1249 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001250
Adam Langley4139edb2016-01-13 15:00:54 -08001251 /* Add a signature. */
David Benjamind316cba2016-06-02 16:17:39 -04001252 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001253 if (!ssl_has_private_key(ssl)) {
1254 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001255 goto err;
1256 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001257
Adam Langley4139edb2016-01-13 15:00:54 -08001258 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1259 size_t sig_len;
1260 enum ssl_private_key_result_t sign_result;
1261 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1262 /* This is the first iteration, so set up the signature. Sample the
1263 * parameter length before adding a signature algorithm. */
1264 if (!CBB_flush(&cbb)) {
1265 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001266 }
Adam Langley4139edb2016-01-13 15:00:54 -08001267 size_t params_len = CBB_len(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001268
1269 /* Determine signature algorithm. */
Adam Langley4139edb2016-01-13 15:00:54 -08001270 const EVP_MD *md;
David Benjamin4969cc92016-04-22 15:02:23 -04001271 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley4139edb2016-01-13 15:00:54 -08001272 md = tls1_choose_signing_digest(ssl);
1273 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001274 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001275 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1276 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001277 }
Adam Langley4139edb2016-01-13 15:00:54 -08001278 } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001279 md = EVP_md5_sha1();
1280 } else {
1281 md = EVP_sha1();
1282 }
1283
Adam Langley4139edb2016-01-13 15:00:54 -08001284 /* Compute the digest and sign it. */
1285 uint8_t digest[EVP_MAX_MD_SIZE];
1286 unsigned digest_len = 0;
1287 EVP_MD_CTX md_ctx;
1288 EVP_MD_CTX_init(&md_ctx);
1289 int digest_ret =
1290 EVP_DigestInit_ex(&md_ctx, md, NULL) &&
1291 EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
1292 EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
1293 EVP_DigestUpdate(&md_ctx, CBB_data(&cbb), params_len) &&
1294 EVP_DigestFinal_ex(&md_ctx, digest, &digest_len);
1295 EVP_MD_CTX_cleanup(&md_ctx);
1296 uint8_t *ptr;
1297 if (!digest_ret ||
1298 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1299 !CBB_reserve(&child, &ptr, max_sig_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001300 goto err;
1301 }
Adam Langley4139edb2016-01-13 15:00:54 -08001302 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1303 digest, digest_len);
Kenny Rootb8494592015-09-25 02:29:14 +00001304 } else {
Adam Langley4139edb2016-01-13 15:00:54 -08001305 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
1306
1307 /* Retry the signature. */
1308 uint8_t *ptr;
1309 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1310 !CBB_reserve(&child, &ptr, max_sig_len)) {
1311 goto err;
1312 }
1313 sign_result =
1314 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001315 }
Adam Langley4139edb2016-01-13 15:00:54 -08001316
1317 switch (sign_result) {
1318 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001319 if (!CBB_did_write(&child, sig_len)) {
1320 goto err;
1321 }
1322 break;
1323 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001324 goto err;
1325 case ssl_private_key_retry:
1326 /* Discard the unfinished signature and save the state of |cbb| for the
1327 * next iteration. */
1328 CBB_discard_child(&cbb);
1329 ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb);
1330 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1331 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1332 goto err;
1333 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001334 }
1335
Adam Langley4139edb2016-01-13 15:00:54 -08001336 size_t length;
1337 if (!CBB_finish(&cbb, NULL, &length) ||
1338 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001339 goto err;
1340 }
Adam Langley4139edb2016-01-13 15:00:54 -08001341 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
1342 return ssl_do_write(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001343
Adam Langleyd9e397b2015-01-22 14:27:53 -08001344err:
Adam Langley4139edb2016-01-13 15:00:54 -08001345 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001346 return -1;
1347}
1348
Adam Langley4139edb2016-01-13 15:00:54 -08001349int ssl3_send_certificate_request(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001350 uint8_t *p, *d;
1351 size_t i;
1352 int j, nl, off, n;
1353 STACK_OF(X509_NAME) *sk = NULL;
1354 X509_NAME *name;
1355 BUF_MEM *buf;
1356
Adam Langley4139edb2016-01-13 15:00:54 -08001357 if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
1358 buf = ssl->init_buf;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001359
Adam Langley4139edb2016-01-13 15:00:54 -08001360 d = p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001361
1362 /* get the list of acceptable cert types */
1363 p++;
Adam Langley4139edb2016-01-13 15:00:54 -08001364 n = ssl3_get_req_cert_type(ssl, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001365 d[0] = n;
1366 p += n;
1367 n++;
1368
David Benjamin4969cc92016-04-22 15:02:23 -04001369 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001370 const uint8_t *psigs;
Adam Langley4139edb2016-01-13 15:00:54 -08001371 nl = tls12_get_psigalgs(ssl, &psigs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001372 s2n(nl, p);
1373 memcpy(p, psigs, nl);
1374 p += nl;
1375 n += nl + 2;
1376 }
1377
1378 off = n;
1379 p += 2;
1380 n += 2;
1381
Adam Langley4139edb2016-01-13 15:00:54 -08001382 sk = SSL_get_client_CA_list(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001383 nl = 0;
1384 if (sk != NULL) {
1385 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1386 name = sk_X509_NAME_value(sk, i);
1387 j = i2d_X509_NAME(name, NULL);
Adam Langley4139edb2016-01-13 15:00:54 -08001388 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001389 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001390 goto err;
1391 }
Adam Langley4139edb2016-01-13 15:00:54 -08001392 p = ssl_handshake_start(ssl) + n;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001393 s2n(j, p);
1394 i2d_X509_NAME(name, &p);
1395 n += 2 + j;
1396 nl += 2 + j;
1397 }
1398 }
1399
1400 /* else no CA names */
Adam Langley4139edb2016-01-13 15:00:54 -08001401 p = ssl_handshake_start(ssl) + off;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001402 s2n(nl, p);
1403
Adam Langley4139edb2016-01-13 15:00:54 -08001404 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001405 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001406 }
Adam Langley4139edb2016-01-13 15:00:54 -08001407 ssl->state = SSL3_ST_SW_CERT_REQ_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001408 }
1409
1410 /* SSL3_ST_SW_CERT_REQ_B */
Adam Langley4139edb2016-01-13 15:00:54 -08001411 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001412
1413err:
1414 return -1;
1415}
1416
Adam Langley4139edb2016-01-13 15:00:54 -08001417int ssl3_get_client_key_exchange(SSL *ssl) {
Kenny Roote99801b2015-11-06 15:31:15 -08001418 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001419 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001420 uint32_t alg_k;
1421 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001422 uint8_t *premaster_secret = NULL;
1423 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001424 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001425
Adam Langley4139edb2016-01-13 15:00:54 -08001426 unsigned psk_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001427 uint8_t psk[PSK_MAX_PSK_LEN];
1428
David Benjamind316cba2016-06-02 16:17:39 -04001429 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Kenny Roote99801b2015-11-06 15:31:15 -08001430 int ok;
Adam Langley4139edb2016-01-13 15:00:54 -08001431 const long n = ssl->method->ssl_get_message(
David Benjamind316cba2016-06-02 16:17:39 -04001432 ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok);
Kenny Roote99801b2015-11-06 15:31:15 -08001433 if (!ok) {
1434 return n;
1435 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001436 }
1437
Adam Langley4139edb2016-01-13 15:00:54 -08001438 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1439 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1440 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001441
1442 /* If using a PSK key exchange, prepare the pre-shared key. */
1443 if (alg_a & SSL_aPSK) {
1444 CBS psk_identity;
1445
1446 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1447 * then this is the only field in the message. */
1448 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1449 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001450 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001451 al = SSL_AD_DECODE_ERROR;
1452 goto f_err;
1453 }
1454
Adam Langley4139edb2016-01-13 15:00:54 -08001455 if (ssl->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001456 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001457 al = SSL_AD_INTERNAL_ERROR;
1458 goto f_err;
1459 }
1460
1461 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1462 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001463 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001464 al = SSL_AD_ILLEGAL_PARAMETER;
1465 goto f_err;
1466 }
1467
Adam Langley4139edb2016-01-13 15:00:54 -08001468 if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001469 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001470 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001471 goto f_err;
1472 }
1473
1474 /* Look up the key for the identity. */
Adam Langley4139edb2016-01-13 15:00:54 -08001475 psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
1476 sizeof(psk));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001477 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001478 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001479 al = SSL_AD_INTERNAL_ERROR;
1480 goto f_err;
1481 } else if (psk_len == 0) {
1482 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001483 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001484 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1485 goto f_err;
1486 }
1487 }
1488
1489 /* Depending on the key exchange method, compute |premaster_secret| and
1490 * |premaster_secret_len|. */
1491 if (alg_k & SSL_kRSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001492 /* Allocate a buffer large enough for an RSA decryption. */
Adam Langley4139edb2016-01-13 15:00:54 -08001493 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001494 decrypt_buf = OPENSSL_malloc(rsa_size);
1495 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001496 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001497 goto err;
1498 }
1499
Kenny Roote99801b2015-11-06 15:31:15 -08001500 enum ssl_private_key_result_t decrypt_result;
Adam Langley4139edb2016-01-13 15:00:54 -08001501 size_t decrypt_len;
David Benjamind316cba2016-06-02 16:17:39 -04001502 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
Adam Langley4139edb2016-01-13 15:00:54 -08001503 if (!ssl_has_private_key(ssl) ||
1504 ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
Kenny Roote99801b2015-11-06 15:31:15 -08001505 al = SSL_AD_HANDSHAKE_FAILURE;
1506 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1507 goto f_err;
1508 }
Adam Langley4139edb2016-01-13 15:00:54 -08001509 CBS encrypted_premaster_secret;
1510 if (ssl->version > SSL3_VERSION) {
Kenny Roote99801b2015-11-06 15:31:15 -08001511 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1512 &encrypted_premaster_secret) ||
1513 CBS_len(&client_key_exchange) != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001514 al = SSL_AD_DECODE_ERROR;
1515 OPENSSL_PUT_ERROR(SSL,
1516 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1517 goto f_err;
Kenny Roote99801b2015-11-06 15:31:15 -08001518 }
1519 } else {
1520 encrypted_premaster_secret = client_key_exchange;
1521 }
1522
Kenny Roote99801b2015-11-06 15:31:15 -08001523 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1524 * timing-sensitive code below. */
1525 decrypt_result = ssl_private_key_decrypt(
Adam Langley4139edb2016-01-13 15:00:54 -08001526 ssl, decrypt_buf, &decrypt_len, rsa_size,
Kenny Roote99801b2015-11-06 15:31:15 -08001527 CBS_data(&encrypted_premaster_secret),
1528 CBS_len(&encrypted_premaster_secret));
1529 } else {
David Benjamind316cba2016-06-02 16:17:39 -04001530 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
Kenny Roote99801b2015-11-06 15:31:15 -08001531 /* Complete async decrypt. */
1532 decrypt_result = ssl_private_key_decrypt_complete(
Adam Langley4139edb2016-01-13 15:00:54 -08001533 ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001534 }
Kenny Roote99801b2015-11-06 15:31:15 -08001535
1536 switch (decrypt_result) {
1537 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001538 break;
1539 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001540 goto err;
1541 case ssl_private_key_retry:
Adam Langley4139edb2016-01-13 15:00:54 -08001542 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind316cba2016-06-02 16:17:39 -04001543 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
Kenny Roote99801b2015-11-06 15:31:15 -08001544 goto err;
1545 }
1546
David Benjamin4969cc92016-04-22 15:02:23 -04001547 if (decrypt_len != rsa_size) {
1548 al = SSL_AD_DECRYPT_ERROR;
1549 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1550 goto f_err;
1551 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001552
Adam Langley4139edb2016-01-13 15:00:54 -08001553 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1554 * section 7.4.7.1. */
1555 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1556 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001557 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 }
Adam Langley4139edb2016-01-13 15:00:54 -08001561 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1562 goto err;
1563 }
1564
1565 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1566 * publicly invalid. */
1567 if (decrypt_len < 11 + premaster_secret_len) {
1568 al = SSL_AD_DECRYPT_ERROR;
1569 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1570 goto f_err;
1571 }
1572
1573 /* Check the padding. See RFC 3447, section 7.2.2. */
1574 size_t padding_len = decrypt_len - premaster_secret_len;
1575 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1576 constant_time_eq_int_8(decrypt_buf[1], 2);
1577 size_t i;
1578 for (i = 2; i < padding_len - 1; i++) {
1579 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1580 }
1581 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1582
1583 /* The premaster secret must begin with |client_version|. This too must be
1584 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1585 good &= constant_time_eq_8(decrypt_buf[padding_len],
1586 (unsigned)(ssl->client_version >> 8));
1587 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
1588 (unsigned)(ssl->client_version & 0xff));
1589
1590 /* Select, in constant time, either the decrypted premaster or the random
1591 * premaster based on |good|. */
1592 for (i = 0; i < premaster_secret_len; i++) {
1593 premaster_secret[i] = constant_time_select_8(
1594 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1595 }
1596
Adam Langleyd9e397b2015-01-22 14:27:53 -08001597 OPENSSL_free(decrypt_buf);
1598 decrypt_buf = NULL;
David Benjamind316cba2016-06-02 16:17:39 -04001599 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1600 /* Parse the ClientKeyExchange. */
Adam Langley4139edb2016-01-13 15:00:54 -08001601 CBS peer_key;
David Benjamind316cba2016-06-02 16:17:39 -04001602 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1603 &peer_key) ||
1604 CBS_len(&client_key_exchange) != 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001605 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001606 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001607 goto f_err;
1608 }
1609
Adam Langley4139edb2016-01-13 15:00:54 -08001610 /* Compute the premaster. */
1611 uint8_t alert;
David Benjamind316cba2016-06-02 16:17:39 -04001612 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1613 &premaster_secret_len, &alert, CBS_data(&peer_key),
1614 CBS_len(&peer_key))) {
Adam Langley4139edb2016-01-13 15:00:54 -08001615 al = alert;
1616 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001617 }
1618
Adam Langley4139edb2016-01-13 15:00:54 -08001619 /* The key exchange state may now be discarded. */
1620 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001621 } else if (alg_k & SSL_kPSK) {
1622 /* For plain PSK, other_secret is a block of 0s with the same length as the
1623 * pre-shared key. */
1624 premaster_secret_len = psk_len;
1625 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1626 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001627 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001628 goto err;
1629 }
1630 memset(premaster_secret, 0, premaster_secret_len);
1631 } else {
1632 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001633 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001634 goto f_err;
1635 }
1636
1637 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1638 * pre-shared key. */
1639 if (alg_a & SSL_aPSK) {
1640 CBB new_premaster, child;
1641 uint8_t *new_data;
1642 size_t new_len;
1643
Kenny Rootb8494592015-09-25 02:29:14 +00001644 CBB_zero(&new_premaster);
1645 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1646 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001647 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1648 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1649 !CBB_add_bytes(&child, psk, psk_len) ||
1650 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001651 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001652 CBB_cleanup(&new_premaster);
1653 goto err;
1654 }
1655
1656 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1657 OPENSSL_free(premaster_secret);
1658 premaster_secret = new_data;
1659 premaster_secret_len = new_len;
1660 }
1661
1662 /* Compute the master secret */
David Benjamin4969cc92016-04-22 15:02:23 -04001663 ssl->session->master_key_length = tls1_generate_master_secret(
Adam Langley4139edb2016-01-13 15:00:54 -08001664 ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
1665 if (ssl->session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001666 goto err;
1667 }
Adam Langley4139edb2016-01-13 15:00:54 -08001668 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001669
1670 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1671 OPENSSL_free(premaster_secret);
1672 return 1;
1673
1674f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001675 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001676err:
Adam Langley4139edb2016-01-13 15:00:54 -08001677 if (premaster_secret != NULL) {
1678 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001679 OPENSSL_free(premaster_secret);
1680 }
Adam Langleye9ada862015-05-11 17:20:37 -07001681 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001682
1683 return -1;
1684}
1685
Adam Langley4139edb2016-01-13 15:00:54 -08001686int ssl3_get_cert_verify(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001687 int al, ok, ret = 0;
1688 long n;
1689 CBS certificate_verify, signature;
Adam Langley4139edb2016-01-13 15:00:54 -08001690 X509 *peer = ssl->session->peer;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001691 EVP_PKEY *pkey = NULL;
1692 const EVP_MD *md = NULL;
1693 uint8_t digest[EVP_MAX_MD_SIZE];
1694 size_t digest_length;
1695 EVP_PKEY_CTX *pctx = NULL;
1696
1697 /* Only RSA and ECDSA client certificates are supported, so a
1698 * CertificateVerify is required if and only if there's a client certificate.
1699 * */
1700 if (peer == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001701 ssl3_free_handshake_buffer(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001702 return 1;
1703 }
1704
David Benjamind316cba2016-06-02 16:17:39 -04001705 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1706 ssl_dont_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001707
1708 if (!ok) {
1709 return n;
1710 }
1711
1712 /* Filter out unsupported certificate types. */
1713 pkey = X509_get_pubkey(peer);
Adam Langleye9ada862015-05-11 17:20:37 -07001714 if (pkey == NULL) {
1715 goto err;
1716 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001717 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
1718 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
1719 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00001720 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001721 goto f_err;
1722 }
1723
Adam Langley4139edb2016-01-13 15:00:54 -08001724 CBS_init(&certificate_verify, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001725
1726 /* Determine the digest type if needbe. */
David Benjamin4969cc92016-04-22 15:02:23 -04001727 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyfad63272015-11-12 12:15:39 -08001728 uint8_t hash, signature_type;
1729 if (!CBS_get_u8(&certificate_verify, &hash) ||
1730 !CBS_get_u8(&certificate_verify, &signature_type)) {
1731 al = SSL_AD_DECODE_ERROR;
1732 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1733 goto f_err;
1734 }
Adam Langley4139edb2016-01-13 15:00:54 -08001735 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001736 goto f_err;
1737 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001738 }
1739
1740 /* Compute the digest. */
Adam Langley4139edb2016-01-13 15:00:54 -08001741 if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001742 goto err;
1743 }
1744
1745 /* The handshake buffer is no longer necessary, and we may hash the current
1746 * message.*/
Adam Langley4139edb2016-01-13 15:00:54 -08001747 ssl3_free_handshake_buffer(ssl);
1748 if (!ssl3_hash_current_message(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001749 goto err;
1750 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001751
1752 /* Parse and verify the signature. */
1753 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1754 CBS_len(&certificate_verify) != 0) {
1755 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001756 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001757 goto f_err;
1758 }
1759
1760 pctx = EVP_PKEY_CTX_new(pkey, NULL);
1761 if (pctx == NULL) {
1762 goto err;
1763 }
David Benjamin4969cc92016-04-22 15:02:23 -04001764 int sig_ok = EVP_PKEY_verify_init(pctx) &&
1765 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1766 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1767 digest, digest_length);
1768#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1769 sig_ok = 1;
1770 ERR_clear_error();
1771#endif
1772 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001773 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001774 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001775 goto f_err;
1776 }
1777
1778 ret = 1;
1779
1780 if (0) {
1781 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001782 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001783 }
1784
1785err:
1786 EVP_PKEY_CTX_free(pctx);
1787 EVP_PKEY_free(pkey);
1788
1789 return ret;
1790}
1791
Adam Langley4139edb2016-01-13 15:00:54 -08001792int ssl3_get_client_certificate(SSL *ssl) {
David Benjamin4969cc92016-04-22 15:02:23 -04001793 int ok, al, ret = -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001794 X509 *x = NULL;
1795 unsigned long n;
1796 STACK_OF(X509) *sk = NULL;
1797 SHA256_CTX sha256;
1798 CBS certificate_msg, certificate_list;
1799 int is_first_certificate = 1;
1800
David Benjamin4969cc92016-04-22 15:02:23 -04001801 assert(ssl->s3->tmp.cert_request);
David Benjamind316cba2016-06-02 16:17:39 -04001802 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001803
1804 if (!ok) {
1805 return n;
1806 }
1807
Adam Langley4139edb2016-01-13 15:00:54 -08001808 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
David Benjamin4969cc92016-04-22 15:02:23 -04001809 if (ssl->version == SSL3_VERSION &&
1810 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1811 /* In SSL 3.0, the Certificate message is omitted to signal no certificate. */
1812 if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1813 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1815 al = SSL_AD_HANDSHAKE_FAILURE;
1816 goto f_err;
1817 }
1818
1819 ssl->s3->tmp.reuse_message = 1;
1820 return 1;
1821 }
1822
Adam Langleyd9e397b2015-01-22 14:27:53 -08001823 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin4969cc92016-04-22 15:02:23 -04001824 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001825 goto f_err;
1826 }
1827
Adam Langley4139edb2016-01-13 15:00:54 -08001828 CBS_init(&certificate_msg, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001829
1830 sk = sk_X509_new_null();
1831 if (sk == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001832 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001833 goto err;
1834 }
1835
1836 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
1837 CBS_len(&certificate_msg) != 0) {
1838 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001839 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001840 goto f_err;
1841 }
1842
1843 while (CBS_len(&certificate_list) > 0) {
1844 CBS certificate;
1845 const uint8_t *data;
1846
1847 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1848 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001849 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001850 goto f_err;
1851 }
1852
Adam Langley4139edb2016-01-13 15:00:54 -08001853 if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001854 /* If this is the first certificate, and we don't want to keep peer
1855 * certificates in memory, then we hash it right away. */
1856 SHA256_Init(&sha256);
1857 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
Adam Langley4139edb2016-01-13 15:00:54 -08001858 SHA256_Final(ssl->session->peer_sha256, &sha256);
1859 ssl->session->peer_sha256_valid = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001860 }
1861 is_first_certificate = 0;
1862
Adam Langley4139edb2016-01-13 15:00:54 -08001863 /* A u24 length cannot overflow a long. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001864 data = CBS_data(&certificate);
Adam Langley4139edb2016-01-13 15:00:54 -08001865 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001866 if (x == NULL) {
1867 al = SSL_AD_BAD_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00001868 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001869 goto f_err;
1870 }
1871 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1872 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001873 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001874 goto f_err;
1875 }
1876 if (!sk_X509_push(sk, x)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001877 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001878 goto err;
1879 }
1880 x = NULL;
1881 }
1882
1883 if (sk_X509_num(sk) <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001884 /* No client certificate so the handshake buffer may be discarded. */
Adam Langley4139edb2016-01-13 15:00:54 -08001885 ssl3_free_handshake_buffer(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +00001886
Adam Langleyd9e397b2015-01-22 14:27:53 -08001887 /* TLS does not mind 0 certs returned */
Adam Langley4139edb2016-01-13 15:00:54 -08001888 if (ssl->version == SSL3_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001889 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001890 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001891 goto f_err;
Adam Langley4139edb2016-01-13 15:00:54 -08001892 } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
David Benjamin4969cc92016-04-22 15:02:23 -04001893 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001894 /* Fail for TLS only if we required a certificate */
1895 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001896 al = SSL_AD_HANDSHAKE_FAILURE;
1897 goto f_err;
1898 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001899 } else {
David Benjamin4969cc92016-04-22 15:02:23 -04001900 if (ssl_verify_cert_chain(ssl, sk) <= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001901 al = ssl_verify_alarm_type(ssl->verify_result);
Kenny Rootb8494592015-09-25 02:29:14 +00001902 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001903 goto f_err;
1904 }
1905 }
1906
Adam Langley4139edb2016-01-13 15:00:54 -08001907 X509_free(ssl->session->peer);
1908 ssl->session->peer = sk_X509_shift(sk);
1909 ssl->session->verify_result = ssl->verify_result;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001910
Adam Langley4139edb2016-01-13 15:00:54 -08001911 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1912 ssl->session->cert_chain = sk;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001913 /* Inconsistency alert: cert_chain does *not* include the peer's own
1914 * certificate, while we do include it in s3_clnt.c */
1915
1916 sk = NULL;
1917
1918 ret = 1;
1919
1920 if (0) {
1921 f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001922 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001923 }
1924
1925err:
Adam Langleye9ada862015-05-11 17:20:37 -07001926 X509_free(x);
1927 sk_X509_pop_free(sk, X509_free);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001928 return ret;
1929}
1930
Adam Langley4139edb2016-01-13 15:00:54 -08001931int ssl3_send_server_certificate(SSL *ssl) {
1932 if (ssl->state == SSL3_ST_SW_CERT_A) {
1933 if (!ssl3_output_cert_chain(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001934 return 0;
1935 }
Adam Langley4139edb2016-01-13 15:00:54 -08001936 ssl->state = SSL3_ST_SW_CERT_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001937 }
1938
1939 /* SSL3_ST_SW_CERT_B */
Adam Langley4139edb2016-01-13 15:00:54 -08001940 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001941}
1942
1943/* send a new session ticket (not necessarily for a new session) */
Adam Langley4139edb2016-01-13 15:00:54 -08001944int ssl3_send_new_session_ticket(SSL *ssl) {
Adam Langleye9ada862015-05-11 17:20:37 -07001945 int ret = -1;
1946 uint8_t *session = NULL;
1947 size_t session_len;
1948 EVP_CIPHER_CTX ctx;
1949 HMAC_CTX hctx;
1950
1951 EVP_CIPHER_CTX_init(&ctx);
1952 HMAC_CTX_init(&hctx);
1953
Adam Langley4139edb2016-01-13 15:00:54 -08001954 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001955 uint8_t *p, *macstart;
1956 int len;
1957 unsigned int hlen;
Adam Langley4139edb2016-01-13 15:00:54 -08001958 SSL_CTX *tctx = ssl->initial_ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001959 uint8_t iv[EVP_MAX_IV_LENGTH];
1960 uint8_t key_name[16];
1961 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
1962 * one block of encryption overhead + HMAC. */
1963 const size_t max_ticket_overhead =
1964 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
1965
1966 /* Serialize the SSL_SESSION to be encoded into the ticket. */
Adam Langley4139edb2016-01-13 15:00:54 -08001967 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
1968 &session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001969 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001970 }
1971
1972 /* If the session is too long, emit a dummy value rather than abort the
1973 * connection. */
1974 if (session_len > 0xFFFF - max_ticket_overhead) {
1975 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
1976 const size_t placeholder_len = strlen(kTicketPlaceholder);
1977
1978 OPENSSL_free(session);
Adam Langleye9ada862015-05-11 17:20:37 -07001979 session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001980
Adam Langley4139edb2016-01-13 15:00:54 -08001981 p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001982 /* Emit ticket_lifetime_hint. */
1983 l2n(0, p);
1984 /* Emit ticket. */
1985 s2n(placeholder_len, p);
1986 memcpy(p, kTicketPlaceholder, placeholder_len);
1987 p += placeholder_len;
1988
Adam Langley4139edb2016-01-13 15:00:54 -08001989 len = p - ssl_handshake_start(ssl);
David Benjamind316cba2016-06-02 16:17:39 -04001990 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07001991 goto err;
1992 }
Adam Langley4139edb2016-01-13 15:00:54 -08001993 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
1994 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001995 }
1996
1997 /* Grow buffer if need be: the length calculation is as follows:
1998 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
1999 * max_ticket_overhead + * session_length */
Adam Langley4139edb2016-01-13 15:00:54 -08002000 if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
Adam Langleyd9e397b2015-01-22 14:27:53 -08002001 max_ticket_overhead + session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002002 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002003 }
Adam Langley4139edb2016-01-13 15:00:54 -08002004 p = ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002005 /* Initialize HMAC and cipher contexts. If callback present it does all the
2006 * work otherwise use generated values from parent ctx. */
2007 if (tctx->tlsext_ticket_key_cb) {
Adam Langley4139edb2016-01-13 15:00:54 -08002008 if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
Adam Langleye9ada862015-05-11 17:20:37 -07002009 1 /* encrypt */) < 0) {
2010 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002011 }
2012 } else {
2013 if (!RAND_bytes(iv, 16) ||
2014 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2015 tctx->tlsext_tick_aes_key, iv) ||
2016 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2017 NULL)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002018 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002019 }
2020 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2021 }
2022
2023 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2024 * resumed session (for simplicity), and guess that tickets for new
2025 * sessions will live as long as their sessions. */
Adam Langley4139edb2016-01-13 15:00:54 -08002026 l2n(ssl->hit ? 0 : ssl->session->timeout, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002027
2028 /* Skip ticket length for now */
2029 p += 2;
2030 /* Output key name */
2031 macstart = p;
2032 memcpy(p, key_name, 16);
2033 p += 16;
2034 /* output IV */
2035 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2036 p += EVP_CIPHER_CTX_iv_length(&ctx);
2037 /* Encrypt session data */
Adam Langleye9ada862015-05-11 17:20:37 -07002038 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2039 goto err;
2040 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002041 p += len;
Adam Langleye9ada862015-05-11 17:20:37 -07002042 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2043 goto err;
2044 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002045 p += len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002046
Adam Langleye9ada862015-05-11 17:20:37 -07002047 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2048 !HMAC_Final(&hctx, p, &hlen)) {
2049 goto err;
2050 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002051
2052 p += hlen;
2053 /* Now write out lengths: p points to end of data written */
2054 /* Total length */
Adam Langley4139edb2016-01-13 15:00:54 -08002055 len = p - ssl_handshake_start(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002056 /* Skip ticket lifetime hint */
Adam Langley4139edb2016-01-13 15:00:54 -08002057 p = ssl_handshake_start(ssl) + 4;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002058 s2n(len - 6, p);
David Benjamind316cba2016-06-02 16:17:39 -04002059 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002060 goto err;
2061 }
Adam Langley4139edb2016-01-13 15:00:54 -08002062 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002063 }
2064
2065 /* SSL3_ST_SW_SESSION_TICKET_B */
Adam Langley4139edb2016-01-13 15:00:54 -08002066 ret = ssl_do_write(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -07002067
2068err:
2069 OPENSSL_free(session);
2070 EVP_CIPHER_CTX_cleanup(&ctx);
2071 HMAC_CTX_cleanup(&hctx);
2072 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002073}
2074
2075/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2076 * sets the next_proto member in s if found */
Adam Langley4139edb2016-01-13 15:00:54 -08002077int ssl3_get_next_proto(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002078 int ok;
2079 long n;
2080 CBS next_protocol, selected_protocol, padding;
2081
2082 /* Clients cannot send a NextProtocol message if we didn't see the extension
2083 * in their ClientHello */
Adam Langley4139edb2016-01-13 15:00:54 -08002084 if (!ssl->s3->next_proto_neg_seen) {
Kenny Rootb8494592015-09-25 02:29:14 +00002085 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002086 return -1;
2087 }
2088
David Benjamind316cba2016-06-02 16:17:39 -04002089 n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message,
2090 &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002091
2092 if (!ok) {
2093 return n;
2094 }
2095
Adam Langley4139edb2016-01-13 15:00:54 -08002096 CBS_init(&next_protocol, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002097
2098 /* The payload looks like:
2099 * uint8 proto_len;
2100 * uint8 proto[proto_len];
2101 * uint8 padding_len;
2102 * uint8 padding[padding_len]; */
2103 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2104 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2105 CBS_len(&next_protocol) != 0 ||
David Benjamin4969cc92016-04-22 15:02:23 -04002106 !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
2107 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002108 return 0;
2109 }
2110
2111 return 1;
2112}
2113
2114/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langley4139edb2016-01-13 15:00:54 -08002115int ssl3_get_channel_id(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002116 int ret = -1, ok;
2117 long n;
Kenny Rootb8494592015-09-25 02:29:14 +00002118 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
2119 size_t channel_id_hash_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002120 const uint8_t *p;
Kenny Rootb8494592015-09-25 02:29:14 +00002121 uint16_t extension_type;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002122 EC_GROUP *p256 = NULL;
2123 EC_KEY *key = NULL;
2124 EC_POINT *point = NULL;
2125 ECDSA_SIG sig;
2126 BIGNUM x, y;
2127 CBS encrypted_extensions, extension;
2128
David Benjamind316cba2016-06-02 16:17:39 -04002129 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS,
2130 ssl_dont_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002131
2132 if (!ok) {
2133 return n;
2134 }
2135
2136 /* Before incorporating the EncryptedExtensions message to the handshake
2137 * hash, compute the hash that should have been signed. */
Adam Langley4139edb2016-01-13 15:00:54 -08002138 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002139 return -1;
2140 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002141 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
2142
Adam Langley4139edb2016-01-13 15:00:54 -08002143 if (!ssl3_hash_current_message(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002144 return -1;
2145 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002146
Adam Langley4139edb2016-01-13 15:00:54 -08002147 CBS_init(&encrypted_extensions, ssl->init_msg, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002148
2149 /* EncryptedExtensions could include multiple extensions, but the only
2150 * extension that could be negotiated is ChannelID, so there can only be one
2151 * entry.
2152 *
2153 * The payload looks like:
2154 * uint16 extension_type
2155 * uint16 extension_len;
2156 * uint8 x[32];
2157 * uint8 y[32];
2158 * uint8 r[32];
2159 * uint8 s[32]; */
Adam Langleyd9e397b2015-01-22 14:27:53 -08002160
2161 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2162 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2163 CBS_len(&encrypted_extensions) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00002164 extension_type != TLSEXT_TYPE_channel_id ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08002165 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
Kenny Rootb8494592015-09-25 02:29:14 +00002166 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002167 return -1;
2168 }
2169
2170 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2171 if (!p256) {
Kenny Rootb8494592015-09-25 02:29:14 +00002172 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002173 return -1;
2174 }
2175
2176 BN_init(&x);
2177 BN_init(&y);
2178 sig.r = BN_new();
2179 sig.s = BN_new();
Adam Langleye9ada862015-05-11 17:20:37 -07002180 if (sig.r == NULL || sig.s == NULL) {
2181 goto err;
2182 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002183
2184 p = CBS_data(&extension);
2185 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2186 BN_bin2bn(p + 32, 32, &y) == NULL ||
2187 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2188 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2189 goto err;
2190 }
2191
2192 point = EC_POINT_new(p256);
Adam Langley4139edb2016-01-13 15:00:54 -08002193 if (!point ||
2194 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002195 goto err;
2196 }
2197
2198 key = EC_KEY_new();
2199 if (!key || !EC_KEY_set_group(key, p256) ||
2200 !EC_KEY_set_public_key(key, point)) {
2201 goto err;
2202 }
2203
2204 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2205 * were called. */
2206 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002207 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
Adam Langley4139edb2016-01-13 15:00:54 -08002208 ssl->s3->tlsext_channel_id_valid = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002209 goto err;
2210 }
2211
Adam Langley4139edb2016-01-13 15:00:54 -08002212 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002213 ret = 1;
2214
2215err:
2216 BN_free(&x);
2217 BN_free(&y);
2218 BN_free(sig.r);
2219 BN_free(sig.s);
Adam Langleye9ada862015-05-11 17:20:37 -07002220 EC_KEY_free(key);
2221 EC_POINT_free(point);
2222 EC_GROUP_free(p256);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002223 return ret;
2224}