blob: 0a2668969eb384bca703546cff9f01c1eda73eb3 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin880b14e2014-08-25 22:35:07 -0400149#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <stdio.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400151#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152
153#include <openssl/bn.h>
154#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/cipher.h>
157#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400160#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
165#include <openssl/obj.h>
166#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700167#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172#include "../crypto/dh/internal.h"
173
Adam Langleyfcf25832014-12-18 17:42:32 -0800174
David Benjamin4b755cb2014-12-12 03:58:07 -0500175/* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff
176 * buffer. */
177#define INITIAL_SNIFF_BUFFER_SIZE 8
178
Adam Langleyfcf25832014-12-18 17:42:32 -0800179int ssl3_accept(SSL *s) {
180 BUF_MEM *buf = NULL;
David Benjamin3fa27772015-04-17 22:32:19 -0400181 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 void (*cb)(const SSL *ssl, int type, int val) = NULL;
183 int ret = -1;
184 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 assert(s->handshake_func == ssl3_accept);
187 assert(s->server);
188 assert(!SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 ERR_clear_error();
191 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 if (s->info_callback != NULL) {
194 cb = s->info_callback;
195 } else if (s->ctx->info_callback != NULL) {
196 cb = s->ctx->info_callback;
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 if (s->cert == NULL) {
202 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
203 return -1;
204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 for (;;) {
207 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 switch (s->state) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 case SSL_ST_ACCEPT:
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 if (cb != NULL) {
212 cb(s, SSL_CB_HANDSHAKE_START, 1);
213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 if (s->init_buf == NULL) {
216 buf = BUF_MEM_new();
217 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
218 ret = -1;
219 goto end;
220 }
221 s->init_buf = buf;
222 buf = NULL;
223 }
224 s->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500225
David Benjamin6a08da22015-05-08 22:58:12 -0400226 /* Enable a write buffer. This groups handshake messages within a flight
227 * into a single write. */
228 if (!ssl_init_wbio_buffer(s, 1)) {
229 ret = -1;
230 goto end;
231 }
232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 if (!ssl3_init_finished_mac(s)) {
234 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
235 ret = -1;
236 goto end;
237 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 if (!s->s3->have_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 s->state = SSL3_ST_SR_INITIAL_BYTES;
241 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 s->state = SSL3_ST_SR_CLNT_HELLO_A;
243 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 case SSL3_ST_SR_INITIAL_BYTES:
247 ret = ssl3_get_initial_bytes(s);
248 if (ret <= 0) {
249 goto end;
250 }
251 /* ssl3_get_initial_bytes sets s->state to one of
252 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
253 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 case SSL3_ST_SR_V2_CLIENT_HELLO:
256 ret = ssl3_get_v2_client_hello(s);
257 if (ret <= 0) {
258 goto end;
259 }
260 s->state = SSL3_ST_SR_CLNT_HELLO_A;
261 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 case SSL3_ST_SR_CLNT_HELLO_A:
264 case SSL3_ST_SR_CLNT_HELLO_B:
265 case SSL3_ST_SR_CLNT_HELLO_C:
266 case SSL3_ST_SR_CLNT_HELLO_D:
267 s->shutdown = 0;
268 ret = ssl3_get_client_hello(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 if (ret <= 0) {
270 goto end;
271 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 s->state = SSL3_ST_SW_SRVR_HELLO_A;
273 s->init_num = 0;
274 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500275
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 case SSL3_ST_SW_SRVR_HELLO_A:
277 case SSL3_ST_SW_SRVR_HELLO_B:
278 ret = ssl3_send_server_hello(s);
279 if (ret <= 0) {
280 goto end;
281 }
282 if (s->hit) {
283 if (s->tlsext_ticket_expected) {
284 s->state = SSL3_ST_SW_SESSION_TICKET_A;
285 } else {
286 s->state = SSL3_ST_SW_CHANGE_A;
287 }
288 } else {
289 s->state = SSL3_ST_SW_CERT_A;
290 }
291 s->init_num = 0;
292 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 case SSL3_ST_SW_CERT_A:
295 case SSL3_ST_SW_CERT_B:
296 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
297 ret = ssl3_send_server_certificate(s);
298 if (ret <= 0) {
299 goto end;
300 }
301 if (s->s3->tmp.certificate_status_expected) {
302 s->state = SSL3_ST_SW_CERT_STATUS_A;
303 } else {
304 s->state = SSL3_ST_SW_KEY_EXCH_A;
305 }
306 } else {
307 skip = 1;
308 s->state = SSL3_ST_SW_KEY_EXCH_A;
309 }
310 s->init_num = 0;
311 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 case SSL3_ST_SW_KEY_EXCH_A:
314 case SSL3_ST_SW_KEY_EXCH_B:
315 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 /* Send a ServerKeyExchange message if:
318 * - The key exchange is ephemeral or anonymous
319 * Diffie-Hellman.
320 * - There is a PSK identity hint.
321 *
322 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
323 * this. In the meantime, keep them in sync. */
324 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
325 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
326 ret = ssl3_send_server_key_exchange(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500327 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500329 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 } else {
331 skip = 1;
332 }
Adam Langley95c29f32014-06-20 12:00:00 -0700333
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 s->state = SSL3_ST_SW_CERT_REQ_A;
335 s->init_num = 0;
336 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 case SSL3_ST_SW_CERT_REQ_A:
339 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin5c1ce292015-05-26 16:59:43 -0400340 if (s->s3->tmp.cert_request) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 ret = ssl3_send_certificate_request(s);
342 if (ret <= 0) {
343 goto end;
344 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400345 } else {
346 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400348 s->state = SSL3_ST_SW_SRVR_DONE_A;
349 s->init_num = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 case SSL3_ST_SW_SRVR_DONE_A:
353 case SSL3_ST_SW_SRVR_DONE_B:
354 ret = ssl3_send_server_done(s);
355 if (ret <= 0) {
356 goto end;
357 }
358 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
359 s->state = SSL3_ST_SW_FLUSH;
360 s->init_num = 0;
361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 case SSL3_ST_SW_FLUSH:
364 /* This code originally checked to see if any data was pending using
365 * BIO_CTRL_INFO and then flushed. This caused problems as documented
366 * in PR#1939. The proposed fix doesn't completely resolve this issue
367 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
368 * we just flush unconditionally. */
369 s->rwstate = SSL_WRITING;
370 if (BIO_flush(s->wbio) <= 0) {
371 ret = -1;
372 goto end;
373 }
374 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 s->state = s->s3->tmp.next_state;
377 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 case SSL3_ST_SR_CERT_A:
380 case SSL3_ST_SR_CERT_B:
381 if (s->s3->tmp.cert_request) {
382 ret = ssl3_get_client_certificate(s);
383 if (ret <= 0) {
384 goto end;
385 }
386 }
387 s->init_num = 0;
388 s->state = SSL3_ST_SR_KEY_EXCH_A;
389 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 case SSL3_ST_SR_KEY_EXCH_A:
392 case SSL3_ST_SR_KEY_EXCH_B:
393 ret = ssl3_get_client_key_exchange(s);
394 if (ret <= 0) {
395 goto end;
396 }
397 s->state = SSL3_ST_SR_CERT_VRFY_A;
398 s->init_num = 0;
399 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 case SSL3_ST_SR_CERT_VRFY_A:
402 case SSL3_ST_SR_CERT_VRFY_B:
403 ret = ssl3_get_cert_verify(s);
404 if (ret <= 0) {
405 goto end;
406 }
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 s->state = SSL3_ST_SR_CHANGE;
409 s->init_num = 0;
410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 case SSL3_ST_SR_CHANGE: {
413 char next_proto_neg = 0;
414 char channel_id = 0;
415 next_proto_neg = s->s3->next_proto_neg_seen;
416 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 /* At this point, the next message must be entirely behind a
419 * ChangeCipherSpec. */
420 if (!ssl3_expect_change_cipher_spec(s)) {
421 ret = -1;
422 goto end;
423 }
424 if (next_proto_neg) {
425 s->state = SSL3_ST_SR_NEXT_PROTO_A;
426 } else if (channel_id) {
427 s->state = SSL3_ST_SR_CHANNEL_ID_A;
428 } else {
429 s->state = SSL3_ST_SR_FINISHED_A;
430 }
431 break;
432 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700433
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 case SSL3_ST_SR_NEXT_PROTO_A:
435 case SSL3_ST_SR_NEXT_PROTO_B:
436 ret = ssl3_get_next_proto(s);
437 if (ret <= 0) {
438 goto end;
439 }
440 s->init_num = 0;
441 if (s->s3->tlsext_channel_id_valid) {
442 s->state = SSL3_ST_SR_CHANNEL_ID_A;
443 } else {
444 s->state = SSL3_ST_SR_FINISHED_A;
445 }
446 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 case SSL3_ST_SR_CHANNEL_ID_A:
449 case SSL3_ST_SR_CHANNEL_ID_B:
450 ret = ssl3_get_channel_id(s);
451 if (ret <= 0) {
452 goto end;
453 }
454 s->init_num = 0;
455 s->state = SSL3_ST_SR_FINISHED_A;
456 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 case SSL3_ST_SR_FINISHED_A:
459 case SSL3_ST_SR_FINISHED_B:
460 ret =
461 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
462 if (ret <= 0) {
463 goto end;
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (s->hit) {
467 s->state = SSL_ST_OK;
468 } else if (s->tlsext_ticket_expected) {
469 s->state = SSL3_ST_SW_SESSION_TICKET_A;
470 } else {
471 s->state = SSL3_ST_SW_CHANGE_A;
472 }
473 /* If this is a full handshake with ChannelID then record the hashshake
474 * hashes in |s->session| in case we need them to verify a ChannelID
475 * signature on a resumption of this session in the future. */
476 if (!s->hit && s->s3->tlsext_channel_id_new) {
477 ret = tls1_record_handshake_hashes_for_channel_id(s);
478 if (ret <= 0) {
479 goto end;
480 }
481 }
482 s->init_num = 0;
483 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 case SSL3_ST_SW_SESSION_TICKET_A:
486 case SSL3_ST_SW_SESSION_TICKET_B:
487 ret = ssl3_send_new_session_ticket(s);
488 if (ret <= 0) {
489 goto end;
490 }
491 s->state = SSL3_ST_SW_CHANGE_A;
492 s->init_num = 0;
493 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 case SSL3_ST_SW_CHANGE_A:
496 case SSL3_ST_SW_CHANGE_B:
497 s->session->cipher = s->s3->tmp.new_cipher;
498 if (!s->enc_method->setup_key_block(s)) {
499 ret = -1;
500 goto end;
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
504 SSL3_ST_SW_CHANGE_B);
505 if (ret <= 0) {
506 goto end;
507 }
508 s->state = SSL3_ST_SW_FINISHED_A;
509 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 if (!s->enc_method->change_cipher_state(
512 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
513 ret = -1;
514 goto end;
515 }
516 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 case SSL3_ST_SW_FINISHED_A:
519 case SSL3_ST_SW_FINISHED_B:
520 ret =
521 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
522 s->enc_method->server_finished_label,
523 s->enc_method->server_finished_label_len);
524 if (ret <= 0) {
525 goto end;
526 }
527 s->state = SSL3_ST_SW_FLUSH;
528 if (s->hit) {
529 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
530 } else {
531 s->s3->tmp.next_state = SSL_ST_OK;
532 }
533 s->init_num = 0;
534 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 case SSL_ST_OK:
537 /* clean a few things up */
538 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 BUF_MEM_free(s->init_buf);
541 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 /* remove buffering on output */
544 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 /* If we aren't retaining peer certificates then we can discard it
549 * now. */
David Benjamin2755a3e2015-04-22 16:17:58 -0400550 if (s->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 X509_free(s->session->peer);
552 s->session->peer = NULL;
553 }
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin44d3eed2015-05-21 01:29:55 -0400555 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamin44d3eed2015-05-21 01:29:55 -0400557 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin44d3eed2015-05-21 01:29:55 -0400559 if (cb != NULL) {
560 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 }
Adam Langley75872532014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 ret = 1;
564 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 default:
567 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
568 ret = -1;
569 goto end;
570 }
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
573 new_state = s->state;
574 s->state = state;
575 cb(s, SSL_CB_ACCEPT_LOOP, 1);
576 s->state = new_state;
577 }
578 skip = 0;
579 }
580
Adam Langley95c29f32014-06-20 12:00:00 -0700581end:
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400583 BUF_MEM_free(buf);
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 if (cb != NULL) {
585 cb(s, SSL_CB_ACCEPT_EXIT, ret);
586 }
587 return ret;
588}
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590static int ssl3_read_sniff_buffer(SSL *s, size_t n) {
591 if (s->s3->sniff_buffer == NULL) {
592 s->s3->sniff_buffer = BUF_MEM_new();
593 }
594 if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) {
595 return -1;
596 }
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 while (s->s3->sniff_buffer_len < n) {
599 int ret;
David Benjamin4b755cb2014-12-12 03:58:07 -0500600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 s->rwstate = SSL_READING;
602 ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len,
603 n - s->s3->sniff_buffer_len);
604 if (ret <= 0) {
605 return ret;
606 }
607 s->rwstate = SSL_NOTHING;
608 s->s3->sniff_buffer_len += ret;
609 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 return 1;
612}
David Benjamin4b755cb2014-12-12 03:58:07 -0500613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614int ssl3_get_initial_bytes(SSL *s) {
615 int ret;
616 const uint8_t *p;
David Benjamin4b755cb2014-12-12 03:58:07 -0500617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only
619 * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */
620 ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE);
621 if (ret <= 0) {
622 return ret;
623 }
624 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
625 p = (const uint8_t *)s->s3->sniff_buffer->data;
David Benjamin4b755cb2014-12-12 03:58:07 -0500626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 /* Some dedicated error codes for protocol mixups should the application wish
628 * to interpret them differently. (These do not overlap with ClientHello or
629 * V2ClientHello.) */
630 if (strncmp("GET ", (const char *)p, 4) == 0 ||
631 strncmp("POST ", (const char *)p, 5) == 0 ||
632 strncmp("HEAD ", (const char *)p, 5) == 0 ||
633 strncmp("PUT ", (const char *)p, 4) == 0) {
634 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST);
635 return -1;
636 }
637 if (strncmp("CONNECT ", (const char *)p, 8) == 0) {
638 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST);
639 return -1;
640 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 /* Determine if this is a ClientHello or V2ClientHello. */
643 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
644 p[3] >= SSL3_VERSION_MAJOR) {
645 /* This is a V2ClientHello. */
646 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
647 return 1;
648 }
649 if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR &&
650 p[5] == SSL3_MT_CLIENT_HELLO) {
651 /* This is a ClientHello. Initialize the record layer with the already
652 * consumed data and continue the handshake. */
David Benjamin6a08da22015-05-08 22:58:12 -0400653 if (!ssl3_setup_read_buffer(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 return -1;
655 }
656 assert(s->rstate == SSL_ST_READ_HEADER);
David Benjamin6a08da22015-05-08 22:58:12 -0400657 /* There cannot have already been data in the record layer. */
658 assert(s->s3->rbuf.left == 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len);
660 s->s3->rbuf.offset = 0;
661 s->s3->rbuf.left = s->s3->sniff_buffer_len;
662 s->packet_length = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 BUF_MEM_free(s->s3->sniff_buffer);
665 s->s3->sniff_buffer = NULL;
666 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 s->state = SSL3_ST_SR_CLNT_HELLO_A;
669 return 1;
670 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL);
673 return -1;
674}
David Benjamin4b755cb2014-12-12 03:58:07 -0500675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676int ssl3_get_v2_client_hello(SSL *s) {
677 const uint8_t *p;
678 int ret;
679 CBS v2_client_hello, cipher_specs, session_id, challenge;
680 size_t msg_length, rand_len, len;
681 uint8_t msg_type;
682 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
683 CBB client_hello, hello_body, cipher_suites;
684 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686 /* Read the remainder of the V2ClientHello. We have previously read 8 bytes
687 * in ssl3_get_initial_bytes. */
688 assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE);
689 p = (const uint8_t *)s->s3->sniff_buffer->data;
690 msg_length = ((p[0] & 0x7f) << 8) | p[1];
691 if (msg_length > (1024 * 4)) {
692 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
693 return -1;
694 }
695 if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) {
696 /* Reject lengths that are too short early. We have already read 8 bytes,
697 * so we should not attempt to process an (invalid) V2ClientHello which
698 * would be shorter than that. */
699 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello,
700 SSL_R_RECORD_LENGTH_MISMATCH);
701 return -1;
702 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500703
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 ret = ssl3_read_sniff_buffer(s, msg_length + 2);
705 if (ret <= 0) {
706 return ret;
707 }
708 assert(s->s3->sniff_buffer_len == msg_length + 2);
709 CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2,
710 msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500711
Adam Langleyfcf25832014-12-18 17:42:32 -0800712 /* The V2ClientHello without the length is incorporated into the Finished
713 * hash. */
David Benjaminfbdfefb2015-02-16 19:33:53 -0500714 if (!ssl3_finish_mac(s, CBS_data(&v2_client_hello),
715 CBS_len(&v2_client_hello))) {
716 return -1;
717 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 if (s->msg_callback) {
719 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
720 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
721 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
724 !CBS_get_u16(&v2_client_hello, &version) ||
725 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
726 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
727 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
728 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
729 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
730 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
731 CBS_len(&v2_client_hello) != 0) {
732 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
733 return -1;
734 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500735
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 /* msg_type has already been checked. */
737 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 /* The client_random is the V2ClientHello challenge. Truncate or
740 * left-pad with zeros as needed. */
741 memset(random, 0, SSL3_RANDOM_SIZE);
742 rand_len = CBS_len(&challenge);
743 if (rand_len > SSL3_RANDOM_SIZE) {
744 rand_len = SSL3_RANDOM_SIZE;
745 }
746 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
747 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 /* Write out an equivalent SSLv3 ClientHello. */
750 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
751 s->init_buf->max)) {
752 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
753 return -1;
754 }
755 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
756 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
757 !CBB_add_u16(&hello_body, version) ||
758 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
759 /* No session id. */
760 !CBB_add_u8(&hello_body, 0) ||
761 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
762 CBB_cleanup(&client_hello);
763 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
764 return -1;
765 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 /* Copy the cipher suites. */
768 while (CBS_len(&cipher_specs) > 0) {
769 uint32_t cipher_spec;
770 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
771 CBB_cleanup(&client_hello);
772 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR);
773 return -1;
774 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500775
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 /* Skip SSLv2 ciphers. */
777 if ((cipher_spec & 0xff0000) != 0) {
778 continue;
779 }
780 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
781 CBB_cleanup(&client_hello);
782 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
783 return -1;
784 }
785 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 /* Add the null compression scheme and finish. */
788 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
789 !CBB_finish(&client_hello, NULL, &len)) {
790 CBB_cleanup(&client_hello);
791 OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
792 return -1;
793 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 /* Mark the message for "re"-use by the version-specific method. */
796 s->s3->tmp.reuse_message = 1;
797 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
798 /* The handshake message header is 4 bytes. */
799 s->s3->tmp.message_size = len - 4;
David Benjamin4b755cb2014-12-12 03:58:07 -0500800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 /* Drop the sniff buffer. */
802 BUF_MEM_free(s->s3->sniff_buffer);
803 s->s3->sniff_buffer = NULL;
804 s->s3->sniff_buffer_len = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 return 1;
807}
David Benjamin4b755cb2014-12-12 03:58:07 -0500808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809int ssl3_get_client_hello(SSL *s) {
David Benjamin4b30b282015-05-26 16:37:58 -0400810 int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 long n;
812 const SSL_CIPHER *c;
813 STACK_OF(SSL_CIPHER) *ciphers = NULL;
814 struct ssl_early_callback_ctx early_ctx;
815 CBS client_hello;
816 uint16_t client_version;
817 CBS client_random, session_id, cipher_suites, compression_methods;
David Benjamine3aa1d92015-06-16 15:34:50 -0400818 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 /* We do this so that we will respond with our native type. If we are TLSv1
821 * and we get SSLv3, we will respond with TLSv1, This down switching should
822 * be handled by a different method. If we are SSLv3, we will respond with
823 * SSLv3, even if prompted with TLSv1. */
824 switch (s->state) {
825 case SSL3_ST_SR_CLNT_HELLO_A:
826 case SSL3_ST_SR_CLNT_HELLO_B:
827 n = s->method->ssl_get_message(
828 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
829 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500830 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 if (!ok) {
833 return n;
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 s->state = SSL3_ST_SR_CLNT_HELLO_C;
837 /* fallthrough */
838 case SSL3_ST_SR_CLNT_HELLO_C:
839 case SSL3_ST_SR_CLNT_HELLO_D:
840 /* We have previously parsed the ClientHello message, and can't call
841 * ssl_get_message again without hashing the message into the Finished
842 * digest again. */
843 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 memset(&early_ctx, 0, sizeof(early_ctx));
846 early_ctx.ssl = s;
847 early_ctx.client_hello = s->init_msg;
848 early_ctx.client_hello_len = n;
849 if (!ssl_early_callback_init(&early_ctx)) {
850 al = SSL_AD_DECODE_ERROR;
851 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
852 SSL_R_CLIENTHELLO_PARSE_FAILED);
853 goto f_err;
854 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
857 s->ctx->select_certificate_cb != NULL) {
858 s->state = SSL3_ST_SR_CLNT_HELLO_D;
859 switch (s->ctx->select_certificate_cb(&early_ctx)) {
860 case 0:
David Benjamin2fff5bf2015-02-09 04:32:28 -0500861 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
862 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 case -1:
865 /* Connection rejected. */
866 al = SSL_AD_ACCESS_DENIED;
867 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
868 SSL_R_CONNECTION_REJECTED);
869 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 default:
872 /* fallthrough */;
873 }
874 }
875 s->state = SSL3_ST_SR_CLNT_HELLO_D;
876 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 default:
879 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
880 return -1;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 CBS_init(&client_hello, s->init_msg, n);
884 if (!CBS_get_u16(&client_hello, &client_version) ||
885 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
886 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
887 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
888 al = SSL_AD_DECODE_ERROR;
889 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
890 goto f_err;
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 /* use version from inside client hello, not from record header (may differ:
894 * see RFC 2246, Appendix E, second paragraph) */
895 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* Load the client random. */
898 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 if (SSL_IS_DTLS(s)) {
901 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
904 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
905 al = SSL_AD_DECODE_ERROR;
906 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
907 goto f_err;
908 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 }
David Benjamin8b8c0062014-11-23 02:47:52 -0500910
David Benjamine6df0542015-05-12 22:02:08 -0400911 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
912 * asynchronously.
913 *
914 * TODO(davidben): Clean up the order of events around ClientHello
915 * processing. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (!s->s3->have_version) {
917 /* Select version to use */
918 uint16_t version = ssl3_get_mutual_version(s, client_version);
919 if (version == 0) {
920 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
921 s->version = s->client_version;
922 al = SSL_AD_PROTOCOL_VERSION;
923 goto f_err;
924 }
925 s->version = version;
926 s->enc_method = ssl3_get_enc_method(version);
927 assert(s->enc_method != NULL);
928 /* At this point, the connection's version is known and |s->version| is
929 * fixed. Begin enforcing the record-layer version. */
930 s->s3->have_version = 1;
931 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
932 : (s->client_version < s->version)) {
933 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
934 al = SSL_AD_PROTOCOL_VERSION;
935 goto f_err;
936 }
David Benjamin8b8c0062014-11-23 02:47:52 -0500937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 s->hit = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -0400939 int send_new_ticket = 0;
940 switch (ssl_get_prev_session(s, &session, &send_new_ticket, &early_ctx)) {
941 case ssl_session_success:
942 break;
943 case ssl_session_error:
944 goto err;
945 case ssl_session_retry:
946 s->rwstate = SSL_PENDING_SESSION;
947 goto err;
David Benjamin4b30b282015-05-26 16:37:58 -0400948 }
David Benjamine3aa1d92015-06-16 15:34:50 -0400949 s->tlsext_ticket_expected = send_new_ticket;
David Benjamin4b30b282015-05-26 16:37:58 -0400950
Adam Langleyba5934b2015-06-02 10:50:35 -0700951 /* The EMS state is needed when making the resumption decision, but
952 * extensions are not normally parsed until later. This detects the EMS
953 * extension for the resumption decision and it's checked against the result
954 * of the normal parse later in this function. */
955 const uint8_t *ems_data;
956 size_t ems_len;
957 int have_extended_master_secret =
958 s->version != SSL3_VERSION &&
959 SSL_early_callback_ctx_extension_get(&early_ctx,
960 TLSEXT_TYPE_extended_master_secret,
961 &ems_data, &ems_len) &&
962 ems_len == 0;
963
David Benjamine3aa1d92015-06-16 15:34:50 -0400964 if (session != NULL) {
965 if (session->extended_master_secret &&
Adam Langleyba5934b2015-06-02 10:50:35 -0700966 !have_extended_master_secret) {
967 /* A ClientHello without EMS that attempts to resume a session with EMS
968 * is fatal to the connection. */
969 al = SSL_AD_HANDSHAKE_FAILURE;
970 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
971 SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
972 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 }
Adam Langleyba5934b2015-06-02 10:50:35 -0700974
975 s->hit =
976 /* Only resume if the session's version matches the negotiated version:
977 * most clients do not accept a mismatch. */
David Benjamine3aa1d92015-06-16 15:34:50 -0400978 s->version == session->ssl_version &&
Adam Langleyba5934b2015-06-02 10:50:35 -0700979 /* If the client offers the EMS extension, but the previous session
980 * didn't use it, then negotiate a new session. */
David Benjamine3aa1d92015-06-16 15:34:50 -0400981 have_extended_master_secret == session->extended_master_secret;
Adam Langleyba5934b2015-06-02 10:50:35 -0700982 }
983
David Benjamine3aa1d92015-06-16 15:34:50 -0400984 if (s->hit) {
985 /* Use the new session. */
986 SSL_SESSION_free(s->session);
987 s->session = session;
988 session = NULL;
989
990 s->verify_result = s->session->verify_result;
991 } else if (!ssl_get_new_session(s, 1)) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700992 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400994
Adam Langley524e7172015-02-20 16:04:00 -0800995 if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) {
996 /* Connection rejected for DOS reasons. */
997 al = SSL_AD_ACCESS_DENIED;
998 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
999 goto f_err;
1000 }
1001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamin93de5e52015-04-17 22:33:25 -04001003 CBS_len(&cipher_suites) == 0 ||
1004 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
1006 CBS_len(&compression_methods) == 0) {
1007 al = SSL_AD_DECODE_ERROR;
1008 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1009 goto f_err;
1010 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
1013 if (ciphers == NULL) {
1014 goto err;
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 /* If it is a hit, check that the cipher is in the list. */
David Benjamin93de5e52015-04-17 22:33:25 -04001018 if (s->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 size_t j;
1020 int found_cipher = 0;
David Benjamin3fa27772015-04-17 22:32:19 -04001021 uint32_t id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
1024 c = sk_SSL_CIPHER_value(ciphers, j);
1025 if (c->id == id) {
1026 found_cipher = 1;
1027 break;
1028 }
1029 }
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 if (!found_cipher) {
1032 /* we need to have the cipher in the cipher list if we are asked to reuse
1033 * it */
1034 al = SSL_AD_ILLEGAL_PARAMETER;
1035 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1036 SSL_R_REQUIRED_CIPHER_MISSING);
1037 goto f_err;
1038 }
1039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 /* Only null compression is supported. */
1042 if (memchr(CBS_data(&compression_methods), 0,
1043 CBS_len(&compression_methods)) == NULL) {
1044 al = SSL_AD_ILLEGAL_PARAMETER;
1045 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello,
1046 SSL_R_NO_COMPRESSION_SPECIFIED);
1047 goto f_err;
1048 }
David Benjamindc72ff72014-06-25 12:36:10 -04001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 /* TLS extensions. */
1051 if (s->version >= SSL3_VERSION &&
1052 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
1053 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1054 goto err;
1055 }
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 /* There should be nothing left over in the record. */
1058 if (CBS_len(&client_hello) != 0) {
1059 /* wrong packet length */
1060 al = SSL_AD_DECODE_ERROR;
1061 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1062 goto f_err;
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyba5934b2015-06-02 10:50:35 -07001065 if (have_extended_master_secret != s->s3->tmp.extended_master_secret) {
1066 al = SSL_AD_INTERNAL_ERROR;
1067 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_EMS_STATE_INCONSISTENT);
1068 goto f_err;
1069 }
1070
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1072 if (!s->hit) {
1073 if (ciphers == NULL) {
1074 al = SSL_AD_ILLEGAL_PARAMETER;
1075 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1076 goto f_err;
1077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 /* Let cert callback update server certificates if required */
1080 if (s->cert->cert_cb) {
1081 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1082 if (rv == 0) {
1083 al = SSL_AD_INTERNAL_ERROR;
1084 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1085 goto f_err;
1086 }
1087 if (rv < 0) {
1088 s->rwstate = SSL_X509_LOOKUP;
1089 goto err;
1090 }
1091 s->rwstate = SSL_NOTHING;
1092 }
1093 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1094
1095 if (c == NULL) {
1096 al = SSL_AD_HANDSHAKE_FAILURE;
1097 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1098 goto f_err;
1099 }
1100 s->s3->tmp.new_cipher = c;
David Benjamin5c1ce292015-05-26 16:59:43 -04001101
1102 /* Determine whether to request a client certificate. */
1103 s->s3->tmp.cert_request = !!(s->verify_mode & SSL_VERIFY_PEER);
1104 /* Only request a certificate if Channel ID isn't negotiated. */
1105 if ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1106 s->s3->tlsext_channel_id_valid) {
1107 s->s3->tmp.cert_request = 0;
1108 }
1109 /* Plain PSK forbids Certificate and CertificateRequest. */
1110 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) {
1111 s->s3->tmp.cert_request = 0;
1112 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 } else {
1114 /* Session-id reuse */
1115 s->s3->tmp.new_cipher = s->session->cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -04001116 s->s3->tmp.cert_request = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 }
1118
David Benjamin5c1ce292015-05-26 16:59:43 -04001119 /* In TLS 1.2, client authentication requires hashing the handshake transcript
1120 * under a different hash. Otherwise, release the handshake buffer. */
1121 if ((!SSL_USE_SIGALGS(s) || !s->s3->tmp.cert_request) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08001122 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1123 goto f_err;
1124 }
1125
1126 /* we now have the following setup;
1127 * client_random
1128 * cipher_list - our prefered list of ciphers
1129 * ciphers - the clients prefered list of ciphers
1130 * compression - basically ignored right now
1131 * ssl version is set - sslv3
1132 * s->session - The ssl session has been setup.
1133 * s->hit - session reuse flag
1134 * s->tmp.new_cipher - the new cipher to use. */
1135
1136 if (ret < 0) {
1137 ret = -ret;
1138 }
1139
1140 if (0) {
1141 f_err:
1142 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1143 }
1144
Adam Langley95c29f32014-06-20 12:00:00 -07001145err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001146 sk_SSL_CIPHER_free(ciphers);
David Benjamine3aa1d92015-06-16 15:34:50 -04001147 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 return ret;
1149}
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151int ssl3_send_server_hello(SSL *s) {
1152 uint8_t *buf;
1153 uint8_t *p, *d;
1154 int sl;
1155 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1158 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1159 * known attack while we fix ChannelID itself. */
1160 if (s->s3->tlsext_channel_id_valid &&
David Benjamin7061e282015-03-19 11:10:48 -04001161 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 s->s3->tlsext_channel_id_valid = 0;
1163 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 /* If this is a resumption and the original handshake didn't support
1166 * ChannelID then we didn't record the original handshake hashes in the
1167 * session and so cannot resume with ChannelIDs. */
1168 if (s->hit && s->s3->tlsext_channel_id_new &&
1169 s->session->original_handshake_hash_len == 0) {
1170 s->s3->tlsext_channel_id_valid = 0;
1171 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 buf = (uint8_t *)s->init_buf->data;
1174 /* Do the message type and length last */
1175 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 *(p++) = s->version >> 8;
1178 *(p++) = s->version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 /* Random stuff */
David Benjamin74d8bc22015-05-21 02:16:53 -04001181 if (!ssl_fill_hello_random(s->s3->server_random, SSL3_RANDOM_SIZE,
1182 1 /* server */)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1184 return -1;
1185 }
1186 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1187 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 /* There are several cases for the session ID to send
1190 * back in the server hello:
1191 * - For session reuse from the session cache, we send back the old session
1192 * ID.
1193 * - If stateless session reuse (using a session ticket) is successful, we
1194 * send back the client's "session ID" (which doesn't actually identify
1195 * the session).
1196 * - If it is a new session, we send back the new session ID.
1197 * - However, if we want the new session to be single-use, we send back a
1198 * 0-length session ID.
1199 * s->hit is non-zero in either case of session reuse, so the following
1200 * won't overwrite an ID that we're supposed to send back. */
1201 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) {
1202 s->session->session_id_length = 0;
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 sl = s->session->session_id_length;
1206 if (sl > (int)sizeof(s->session->session_id)) {
1207 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1208 return -1;
1209 }
1210 *(p++) = sl;
1211 memcpy(p, s->session->session_id, sl);
1212 p += sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 /* put the cipher */
David Benjamina1c90a52015-05-30 17:03:14 -04001215 s2n(ssl_cipher_get_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001216
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 /* put the compression method */
1218 *(p++) = 0;
1219 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1220 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1221 return -1;
1222 }
1223 p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH);
1224 if (p == NULL) {
1225 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1226 return -1;
1227 }
Adam Langley95c29f32014-06-20 12:00:00 -07001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 /* do the header */
1230 l = (p - d);
David Benjaminfbdfefb2015-02-16 19:33:53 -05001231 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1232 return -1;
1233 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1235 }
Adam Langley95c29f32014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 /* SSL3_ST_SW_SRVR_HELLO_B */
1238 return ssl_do_write(s);
1239}
Adam Langley95c29f32014-06-20 12:00:00 -07001240
Adam Langleyfcf25832014-12-18 17:42:32 -08001241int ssl3_send_server_done(SSL *s) {
1242 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001243 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1244 return -1;
1245 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 s->state = SSL3_ST_SW_SRVR_DONE_B;
1247 }
Adam Langley95c29f32014-06-20 12:00:00 -07001248
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 /* SSL3_ST_SW_SRVR_DONE_B */
1250 return ssl_do_write(s);
1251}
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253int ssl3_send_server_key_exchange(SSL *s) {
1254 DH *dh = NULL, *dhp;
David Benjaminc0f763b2015-03-27 02:05:39 -04001255 EC_KEY *ecdh = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 uint8_t *encodedPoint = NULL;
1257 int encodedlen = 0;
David Benjamin70bd80a2014-12-27 03:06:46 -05001258 uint16_t curve_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 BN_CTX *bn_ctx = NULL;
1260 const char *psk_identity_hint = NULL;
1261 size_t psk_identity_hint_len = 0;
1262 EVP_PKEY *pkey;
1263 uint8_t *p, *d;
1264 int al, i;
David Benjamin3fa27772015-04-17 22:32:19 -04001265 uint32_t alg_k;
1266 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 int n;
1268 CERT *cert;
1269 BIGNUM *r[4];
1270 int nr[4], kn;
1271 BUF_MEM *buf;
1272 EVP_MD_CTX md_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274 EVP_MD_CTX_init(&md_ctx);
1275 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1276 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1277 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1278 cert = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07001279
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 r[0] = r[1] = r[2] = r[3] = NULL;
1283 n = 0;
1284 if (alg_a & SSL_aPSK) {
1285 /* size for PSK identity hint */
1286 psk_identity_hint = s->psk_identity_hint;
1287 if (psk_identity_hint) {
1288 psk_identity_hint_len = strlen(psk_identity_hint);
1289 } else {
1290 psk_identity_hint_len = 0;
1291 }
1292 n += 2 + psk_identity_hint_len;
1293 }
Adam Langley95c29f32014-06-20 12:00:00 -07001294
David Benjamin7061e282015-03-19 11:10:48 -04001295 if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 dhp = cert->dh_tmp;
1297 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1298 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1299 }
1300 if (dhp == NULL) {
1301 al = SSL_AD_HANDSHAKE_FAILURE;
1302 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1303 SSL_R_MISSING_TMP_DH_KEY);
1304 goto f_err;
1305 }
Adam Langley95c29f32014-06-20 12:00:00 -07001306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 if (s->s3->tmp.dh != NULL) {
1308 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1309 ERR_R_INTERNAL_ERROR);
1310 goto err;
1311 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 dh = DHparams_dup(dhp);
1313 if (dh == NULL) {
1314 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1315 goto err;
1316 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 s->s3->tmp.dh = dh;
David Benjamin9f226a52015-04-26 22:12:32 -04001318
1319 if (!DH_generate_key(dh)) {
1320 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1321 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 }
Adam Langley95c29f32014-06-20 12:00:00 -07001323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 r[0] = dh->p;
1325 r[1] = dh->g;
1326 r[2] = dh->pub_key;
David Benjamin7061e282015-03-19 11:10:48 -04001327 } else if (alg_k & SSL_kECDHE) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001328 /* Determine the curve to use. */
1329 int nid = NID_undef;
David Benjamindd978782015-04-24 15:20:13 -04001330 if (cert->ecdh_nid != NID_undef) {
David Benjaminc0f763b2015-03-27 02:05:39 -04001331 nid = cert->ecdh_nid;
1332 } else if (cert->ecdh_tmp_cb != NULL) {
1333 /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result
1334 * to the caller. */
1335 EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024);
1336 if (template != NULL && EC_KEY_get0_group(template) != NULL) {
1337 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template));
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 }
David Benjamindd978782015-04-24 15:20:13 -04001339 } else {
1340 nid = tls1_get_shared_curve(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001342 if (nid == NID_undef) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 al = SSL_AD_HANDSHAKE_FAILURE;
1344 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1345 SSL_R_MISSING_TMP_ECDH_KEY);
1346 goto f_err;
1347 }
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 if (s->s3->tmp.ecdh != NULL) {
1350 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1351 ERR_R_INTERNAL_ERROR);
1352 goto err;
1353 }
David Benjaminc0f763b2015-03-27 02:05:39 -04001354 ecdh = EC_KEY_new_by_curve_name(nid);
1355 if (ecdh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 goto err;
1357 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 s->s3->tmp.ecdh = ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
David Benjaminc0f763b2015-03-27 02:05:39 -04001360 if (!EC_KEY_generate_key(ecdh)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1362 goto err;
1363 }
Adam Langley95c29f32014-06-20 12:00:00 -07001364
David Benjamin70bd80a2014-12-27 03:06:46 -05001365 /* We only support ephemeral ECDH keys over named (not generic) curves. */
David Benjaminc0f763b2015-03-27 02:05:39 -04001366 const EC_GROUP *group = EC_KEY_get0_group(ecdh);
David Benjamin70bd80a2014-12-27 03:06:46 -05001367 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1369 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1370 goto err;
1371 }
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 /* Encode the public key. First check the size of encoding and allocate
1374 * memory accordingly. */
1375 encodedlen =
1376 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1377 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1380 bn_ctx = BN_CTX_new();
1381 if (encodedPoint == NULL || bn_ctx == NULL) {
1382 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1383 ERR_R_MALLOC_FAILURE);
1384 goto err;
1385 }
Adam Langley95c29f32014-06-20 12:00:00 -07001386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1388 POINT_CONVERSION_UNCOMPRESSED,
1389 encodedPoint, encodedlen, bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 if (encodedlen == 0) {
1392 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1393 goto err;
1394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 BN_CTX_free(bn_ctx);
1397 bn_ctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 /* We only support named (not generic) curves in ECDH ephemeral key
1400 * exchanges. In this situation, we need four additional bytes to encode
1401 * the entire ServerECDHParams structure. */
1402 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 /* We'll generate the serverKeyExchange message explicitly so we can set
1405 * these to NULLs */
1406 r[0] = NULL;
1407 r[1] = NULL;
1408 r[2] = NULL;
1409 r[3] = NULL;
1410 } else if (!(alg_k & SSL_kPSK)) {
1411 al = SSL_AD_HANDSHAKE_FAILURE;
1412 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1413 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1414 goto f_err;
1415 }
Adam Langley95c29f32014-06-20 12:00:00 -07001416
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 for (i = 0; i < 4 && r[i] != NULL; i++) {
1418 nr[i] = BN_num_bytes(r[i]);
1419 n += 2 + nr[i];
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1423 pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher);
1424 if (pkey == NULL) {
1425 al = SSL_AD_DECODE_ERROR;
1426 goto f_err;
1427 }
1428 kn = EVP_PKEY_size(pkey);
1429 } else {
1430 pkey = NULL;
1431 kn = 0;
1432 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1435 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1436 goto err;
1437 }
1438 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001439
Adam Langleyfcf25832014-12-18 17:42:32 -08001440 for (i = 0; i < 4 && r[i] != NULL; i++) {
1441 s2n(nr[i], p);
1442 BN_bn2bin(r[i], p);
1443 p += nr[i];
1444 }
David Benjamin00505ec2014-11-26 16:38:00 -05001445
David Benjamin7061e282015-03-19 11:10:48 -04001446 /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 * them is used, the server key exchange record needs to have both the
1448 * psk_identity_hint and the ServerECDHParams. */
1449 if (alg_a & SSL_aPSK) {
1450 /* copy PSK identity hint (if provided) */
1451 s2n(psk_identity_hint_len, p);
1452 if (psk_identity_hint_len > 0) {
1453 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1454 p += psk_identity_hint_len;
1455 }
1456 }
David Benjamin00505ec2014-11-26 16:38:00 -05001457
David Benjamin7061e282015-03-19 11:10:48 -04001458 if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 /* We only support named (not generic) curves. In this situation, the
1460 * serverKeyExchange message has:
1461 * [1 byte CurveType], [2 byte CurveName]
1462 * [1 byte length of encoded point], followed by
1463 * the actual encoded point itself. */
David Benjamin70bd80a2014-12-27 03:06:46 -05001464 *(p++) = NAMED_CURVE_TYPE;
1465 *(p++) = (uint8_t)(curve_id >> 8);
1466 *(p++) = (uint8_t)(curve_id & 0xff);
1467 *(p++) = encodedlen;
1468 memcpy(p, encodedPoint, encodedlen);
1469 p += encodedlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 OPENSSL_free(encodedPoint);
1471 encodedPoint = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 }
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 /* not anonymous */
1475 if (pkey != NULL) {
1476 /* n is the length of the params, they start at &(d[4]) and p points to
1477 * the space at the end. */
1478 const EVP_MD *md;
1479 size_t sig_len = EVP_PKEY_size(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001480
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 /* Determine signature algorithm. */
1482 if (SSL_USE_SIGALGS(s)) {
1483 md = tls1_choose_signing_digest(s, pkey);
David Benjaminb4d65fd2015-05-29 17:11:21 -04001484 if (!tls12_get_sigandhash(s, p, pkey, md)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 /* Should never happen */
1486 al = SSL_AD_INTERNAL_ERROR;
1487 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange,
1488 ERR_R_INTERNAL_ERROR);
1489 goto f_err;
1490 }
1491 p += 2;
1492 } else if (pkey->type == EVP_PKEY_RSA) {
1493 md = EVP_md5_sha1();
1494 } else {
1495 md = EVP_sha1();
1496 }
1497
1498 if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) ||
1499 !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random,
1500 SSL3_RANDOM_SIZE) ||
1501 !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random,
1502 SSL3_RANDOM_SIZE) ||
1503 !EVP_DigestSignUpdate(&md_ctx, d, n) ||
1504 !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) {
1505 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1506 goto err;
1507 }
1508
1509 s2n(sig_len, p);
1510 n += sig_len + 2;
1511 if (SSL_USE_SIGALGS(s)) {
1512 n += 2;
1513 }
1514 }
1515
David Benjaminfbdfefb2015-02-16 19:33:53 -05001516 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1517 goto err;
1518 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 }
1520
1521 s->state = SSL3_ST_SW_KEY_EXCH_B;
1522 EVP_MD_CTX_cleanup(&md_ctx);
1523 return ssl_do_write(s);
1524
Adam Langley95c29f32014-06-20 12:00:00 -07001525f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001527err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001528 OPENSSL_free(encodedPoint);
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 BN_CTX_free(bn_ctx);
1530 EVP_MD_CTX_cleanup(&md_ctx);
1531 return -1;
1532}
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534int ssl3_send_certificate_request(SSL *s) {
1535 uint8_t *p, *d;
1536 size_t i;
1537 int j, nl, off, n;
1538 STACK_OF(X509_NAME) *sk = NULL;
1539 X509_NAME *name;
1540 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1543 buf = s->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 /* get the list of acceptable cert types */
1548 p++;
1549 n = ssl3_get_req_cert_type(s, p);
1550 d[0] = n;
1551 p += n;
1552 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001553
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 if (SSL_USE_SIGALGS(s)) {
1555 const uint8_t *psigs;
1556 nl = tls12_get_psigalgs(s, &psigs);
1557 s2n(nl, p);
1558 memcpy(p, psigs, nl);
1559 p += nl;
1560 n += nl + 2;
1561 }
Adam Langley95c29f32014-06-20 12:00:00 -07001562
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 off = n;
1564 p += 2;
1565 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001566
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 sk = SSL_get_client_CA_list(s);
1568 nl = 0;
1569 if (sk != NULL) {
1570 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1571 name = sk_X509_NAME_value(sk, i);
1572 j = i2d_X509_NAME(name, NULL);
1573 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1574 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1575 goto err;
1576 }
1577 p = ssl_handshake_start(s) + n;
1578 s2n(j, p);
1579 i2d_X509_NAME(name, &p);
1580 n += 2 + j;
1581 nl += 2 + j;
1582 }
1583 }
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 /* else no CA names */
1586 p = ssl_handshake_start(s) + off;
1587 s2n(nl, p);
1588
David Benjaminfbdfefb2015-02-16 19:33:53 -05001589 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1590 goto err;
1591 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 s->state = SSL3_ST_SW_CERT_REQ_B;
1593 }
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 /* SSL3_ST_SW_CERT_REQ_B */
1596 return ssl_do_write(s);
1597
Adam Langley95c29f32014-06-20 12:00:00 -07001598err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 return -1;
1600}
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602int ssl3_get_client_key_exchange(SSL *s) {
1603 int al, ok;
1604 long n;
1605 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001606 uint32_t alg_k;
1607 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 uint8_t *premaster_secret = NULL;
1609 size_t premaster_secret_len = 0;
1610 RSA *rsa = NULL;
1611 uint8_t *decrypt_buf = NULL;
1612 EVP_PKEY *pkey = NULL;
1613 BIGNUM *pub = NULL;
1614 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 EC_KEY *srvr_ecdh = NULL;
1617 EVP_PKEY *clnt_pub_pkey = NULL;
1618 EC_POINT *clnt_ecpoint = NULL;
1619 BN_CTX *bn_ctx = NULL;
1620 unsigned int psk_len = 0;
1621 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1624 SSL3_ST_SR_KEY_EXCH_B,
1625 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001626 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001627
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 if (!ok) {
1629 return n;
1630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1635 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 /* If using a PSK key exchange, prepare the pre-shared key. */
1638 if (alg_a & SSL_aPSK) {
1639 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1642 * then this is the only field in the message. */
1643 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1644 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1645 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1646 al = SSL_AD_DECODE_ERROR;
1647 goto f_err;
1648 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 if (s->psk_server_callback == NULL) {
1651 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1652 SSL_R_PSK_NO_SERVER_CB);
1653 al = SSL_AD_INTERNAL_ERROR;
1654 goto f_err;
1655 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1658 CBS_contains_zero_byte(&psk_identity)) {
1659 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1660 SSL_R_DATA_LENGTH_TOO_LONG);
1661 al = SSL_AD_ILLEGAL_PARAMETER;
1662 goto f_err;
1663 }
David Benjamin35c02302014-07-13 04:14:59 -04001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1666 al = SSL_AD_INTERNAL_ERROR;
1667 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1668 ERR_R_MALLOC_FAILURE);
1669 goto f_err;
1670 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 /* Look up the key for the identity. */
1673 psk_len =
1674 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1675 if (psk_len > PSK_MAX_PSK_LEN) {
1676 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1677 ERR_R_INTERNAL_ERROR);
1678 al = SSL_AD_INTERNAL_ERROR;
1679 goto f_err;
1680 } else if (psk_len == 0) {
1681 /* PSK related to the given identity not found */
1682 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1683 SSL_R_PSK_IDENTITY_NOT_FOUND);
1684 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1685 goto f_err;
1686 }
1687 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001688
Adam Langleyfcf25832014-12-18 17:42:32 -08001689 /* Depending on the key exchange method, compute |premaster_secret| and
1690 * |premaster_secret_len|. */
1691 if (alg_k & SSL_kRSA) {
1692 CBS encrypted_premaster_secret;
1693 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1694 uint8_t good;
1695 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langley95c29f32014-06-20 12:00:00 -07001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1698 if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) {
1699 al = SSL_AD_HANDSHAKE_FAILURE;
1700 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1701 SSL_R_MISSING_RSA_CERTIFICATE);
1702 goto f_err;
1703 }
1704 rsa = pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 /* TLS and [incidentally] DTLS{0xFEFF} */
1707 if (s->version > SSL3_VERSION) {
1708 CBS copy = client_key_exchange;
1709 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1710 &encrypted_premaster_secret) ||
1711 CBS_len(&client_key_exchange) != 0) {
1712 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1713 al = SSL_AD_DECODE_ERROR;
1714 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1715 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1716 goto f_err;
1717 } else {
1718 encrypted_premaster_secret = copy;
1719 }
1720 }
1721 } else {
1722 encrypted_premaster_secret = client_key_exchange;
1723 }
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 /* Reject overly short RSA keys because we want to be sure that the buffer
1726 * size makes it safe to iterate over the entire size of a premaster secret
1727 * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to
1728 * RSA padding, but the bound is sufficient to be safe. */
1729 rsa_size = RSA_size(rsa);
1730 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1731 al = SSL_AD_DECRYPT_ERROR;
1732 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1733 SSL_R_DECRYPTION_FAILED);
1734 goto f_err;
1735 }
Adam Langley95c29f32014-06-20 12:00:00 -07001736
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 /* We must not leak whether a decryption failure occurs because of
1738 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1739 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1740 * generates a random premaster secret for the case that the decrypt fails.
1741 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1742 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1743 goto err;
1744 }
David Benjamin35c02302014-07-13 04:14:59 -04001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* Allocate a buffer large enough for an RSA decryption. */
1747 decrypt_buf = OPENSSL_malloc(rsa_size);
1748 if (decrypt_buf == NULL) {
1749 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1750 ERR_R_MALLOC_FAILURE);
1751 goto err;
1752 }
David Benjamin35c02302014-07-13 04:14:59 -04001753
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1755 * timing-sensitive code below. */
1756 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1757 CBS_data(&encrypted_premaster_secret),
1758 CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) {
1759 goto err;
1760 }
1761 if (decrypt_len != rsa_size) {
1762 /* This should never happen, but do a check so we do not read
1763 * uninitialized memory. */
1764 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1765 ERR_R_INTERNAL_ERROR);
1766 goto err;
1767 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1770 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1771 * */
1772 good =
1773 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1774 decrypt_buf, decrypt_len, &premaster_index),
1775 1);
1776 decrypt_len = decrypt_len - premaster_index;
David Benjamin0aa07672014-07-24 15:34:14 -04001777
Adam Langleyfcf25832014-12-18 17:42:32 -08001778 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1779 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
Adam Langleyacff73f2014-06-20 12:00:00 -07001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 /* Copy over the unpadded premaster. Whatever the value of
1782 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1783 * is important the memory access pattern be constant. */
1784 premaster_secret =
1785 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1786 SSL_MAX_MASTER_KEY_LENGTH);
1787 if (premaster_secret == NULL) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1789 ERR_R_MALLOC_FAILURE);
1790 goto err;
1791 }
1792 OPENSSL_free(decrypt_buf);
1793 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07001794
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 /* If the version in the decrypted pre-master secret is correct then
1796 * version_good will be 0xff, otherwise it'll be zero. The
1797 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1798 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1799 * a "bad version oracle". Thus version checks are done in constant time
1800 * and are treated like any other decryption error. */
1801 good &= constant_time_eq_8(premaster_secret[0],
1802 (unsigned)(s->client_version >> 8));
1803 good &= constant_time_eq_8(premaster_secret[1],
1804 (unsigned)(s->client_version & 0xff));
Adam Langleyacff73f2014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 /* Now copy rand_premaster_secret over premaster_secret using
1807 * decrypt_good_mask. */
1808 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1809 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1810 rand_premaster_secret[j]);
1811 }
David Benjamin35c02302014-07-13 04:14:59 -04001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813 premaster_secret_len = sizeof(rand_premaster_secret);
David Benjamin7061e282015-03-19 11:10:48 -04001814 } else if (alg_k & SSL_kDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 CBS dh_Yc;
1816 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1819 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
1820 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1821 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1822 al = SSL_R_DECODE_ERROR;
1823 goto f_err;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 if (s->s3->tmp.dh == NULL) {
1827 al = SSL_AD_HANDSHAKE_FAILURE;
1828 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1829 SSL_R_MISSING_TMP_DH_KEY);
1830 goto f_err;
1831 }
1832 dh_srvr = s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1835 if (pub == NULL) {
1836 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
1837 goto err;
1838 }
Adam Langley95c29f32014-06-20 12:00:00 -07001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 /* Allocate a buffer for the premaster secret. */
1841 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1842 if (premaster_secret == NULL) {
1843 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1844 ERR_R_MALLOC_FAILURE);
David Benjaminc35fb012015-02-12 00:48:45 -05001845 BN_clear_free(pub);
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 goto err;
1847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1850 if (dh_len <= 0) {
1851 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
1852 BN_clear_free(pub);
1853 goto err;
1854 }
David Benjamin14c83e72014-07-13 04:54:57 -04001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 DH_free(s->s3->tmp.dh);
1857 s->s3->tmp.dh = NULL;
1858 BN_clear_free(pub);
1859 pub = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 premaster_secret_len = dh_len;
David Benjamin7061e282015-03-19 11:10:48 -04001862 } else if (alg_k & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 int field_size = 0, ecdh_len;
1864 const EC_KEY *tkey;
1865 const EC_GROUP *group;
1866 const BIGNUM *priv_key;
1867 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 /* initialize structures for server's ECDH key pair */
1870 srvr_ecdh = EC_KEY_new();
1871 if (srvr_ecdh == NULL) {
1872 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1873 ERR_R_MALLOC_FAILURE);
1874 goto err;
1875 }
Adam Langley95c29f32014-06-20 12:00:00 -07001876
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1878 * msg. */
1879 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 group = EC_KEY_get0_group(tkey);
1882 priv_key = EC_KEY_get0_private_key(tkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1885 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1887 goto err;
1888 }
Adam Langley95c29f32014-06-20 12:00:00 -07001889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 /* Let's get client's public key */
1891 clnt_ecpoint = EC_POINT_new(group);
1892 if (clnt_ecpoint == NULL) {
1893 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1894 ERR_R_MALLOC_FAILURE);
1895 goto err;
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 /* Get client's public key from encoded point in the ClientKeyExchange
1899 * message. */
1900 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1901 CBS_len(&client_key_exchange) != 0) {
1902 al = SSL_AD_DECODE_ERROR;
1903 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1904 goto f_err;
1905 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 bn_ctx = BN_CTX_new();
1908 if (bn_ctx == NULL) {
1909 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1910 ERR_R_MALLOC_FAILURE);
1911 goto err;
1912 }
David Benjamin35c02302014-07-13 04:14:59 -04001913
Adam Langleyfcf25832014-12-18 17:42:32 -08001914 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
1915 CBS_len(&ecdh_Yc), bn_ctx)) {
1916 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
1917 goto err;
1918 }
Adam Langley95c29f32014-06-20 12:00:00 -07001919
Adam Langleyfcf25832014-12-18 17:42:32 -08001920 /* Allocate a buffer for both the secret and the PSK. */
1921 field_size = EC_GROUP_get_degree(group);
1922 if (field_size <= 0) {
1923 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
1924 goto err;
1925 }
David Benjamin35c02302014-07-13 04:14:59 -04001926
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 ecdh_len = (field_size + 7) / 8;
1928 premaster_secret = OPENSSL_malloc(ecdh_len);
1929 if (premaster_secret == NULL) {
1930 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1931 ERR_R_MALLOC_FAILURE);
1932 goto err;
1933 }
David Benjamin35c02302014-07-13 04:14:59 -04001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 /* Compute the shared pre-master secret */
1936 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
1937 srvr_ecdh, NULL);
1938 if (ecdh_len <= 0) {
1939 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
1940 goto err;
1941 }
Adam Langley95c29f32014-06-20 12:00:00 -07001942
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 EVP_PKEY_free(clnt_pub_pkey);
1944 clnt_pub_pkey = NULL;
1945 EC_POINT_free(clnt_ecpoint);
1946 clnt_ecpoint = NULL;
1947 EC_KEY_free(srvr_ecdh);
1948 srvr_ecdh = NULL;
1949 BN_CTX_free(bn_ctx);
1950 bn_ctx = NULL;
1951 EC_KEY_free(s->s3->tmp.ecdh);
1952 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 premaster_secret_len = ecdh_len;
1955 } else if (alg_k & SSL_kPSK) {
1956 /* For plain PSK, other_secret is a block of 0s with the same length as the
1957 * pre-shared key. */
1958 premaster_secret_len = psk_len;
1959 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1960 if (premaster_secret == NULL) {
1961 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1962 ERR_R_MALLOC_FAILURE);
1963 goto err;
1964 }
1965 memset(premaster_secret, 0, premaster_secret_len);
1966 } else {
1967 al = SSL_AD_HANDSHAKE_FAILURE;
1968 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1969 SSL_R_UNKNOWN_CIPHER_TYPE);
1970 goto f_err;
1971 }
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1974 * pre-shared key. */
1975 if (alg_a & SSL_aPSK) {
1976 CBB new_premaster, child;
1977 uint8_t *new_data;
1978 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) {
1981 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1982 ERR_R_MALLOC_FAILURE);
1983 goto err;
1984 }
1985 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1986 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1987 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1988 !CBB_add_bytes(&child, psk, psk_len) ||
1989 !CBB_finish(&new_premaster, &new_data, &new_len)) {
1990 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange,
1991 ERR_R_INTERNAL_ERROR);
1992 CBB_cleanup(&new_premaster);
1993 goto err;
1994 }
David Benjamin14c83e72014-07-13 04:54:57 -04001995
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1997 OPENSSL_free(premaster_secret);
1998 premaster_secret = new_data;
1999 premaster_secret_len = new_len;
2000 }
David Benjamin14c83e72014-07-13 04:54:57 -04002001
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 /* Compute the master secret */
2003 s->session->master_key_length = s->enc_method->generate_master_secret(
2004 s, s->session->master_key, premaster_secret, premaster_secret_len);
2005 if (s->session->master_key_length == 0) {
2006 goto err;
2007 }
2008 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2011 OPENSSL_free(premaster_secret);
2012 return 1;
2013
Adam Langley95c29f32014-06-20 12:00:00 -07002014f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002015 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07002016err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 if (premaster_secret) {
2018 if (premaster_secret_len) {
2019 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2020 }
2021 OPENSSL_free(premaster_secret);
2022 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002023 OPENSSL_free(decrypt_buf);
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 EVP_PKEY_free(clnt_pub_pkey);
2025 EC_POINT_free(clnt_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04002026 EC_KEY_free(srvr_ecdh);
Adam Langleyfcf25832014-12-18 17:42:32 -08002027 BN_CTX_free(bn_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 return -1;
2030}
Adam Langley95c29f32014-06-20 12:00:00 -07002031
Adam Langleyfcf25832014-12-18 17:42:32 -08002032int ssl3_get_cert_verify(SSL *s) {
2033 int al, ok, ret = 0;
2034 long n;
2035 CBS certificate_verify, signature;
2036 X509 *peer = s->session->peer;
2037 EVP_PKEY *pkey = NULL;
2038 const EVP_MD *md = NULL;
2039 uint8_t digest[EVP_MAX_MD_SIZE];
2040 size_t digest_length;
2041 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04002042
Adam Langleyfcf25832014-12-18 17:42:32 -08002043 /* Only RSA and ECDSA client certificates are supported, so a
2044 * CertificateVerify is required if and only if there's a client certificate.
2045 * */
2046 if (peer == NULL) {
2047 if (s->s3->handshake_buffer &&
2048 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2049 return -1;
2050 }
2051 return 1;
2052 }
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langleyfcf25832014-12-18 17:42:32 -08002054 n = s->method->ssl_get_message(
2055 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2056 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002057 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 if (!ok) {
2060 return n;
2061 }
David Benjaminef865502014-08-24 02:48:34 -04002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 /* Filter out unsupported certificate types. */
2064 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05002065 if (pkey == NULL) {
2066 goto err;
2067 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2069 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2070 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2071 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify,
2072 SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
2073 goto f_err;
2074 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 CBS_init(&certificate_verify, s->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 /* Determine the digest type if needbe. */
2079 if (SSL_USE_SIGALGS(s) &&
2080 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2081 goto f_err;
2082 }
David Benjamin854dd652014-08-26 00:32:30 -04002083
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 /* Compute the digest. */
2085 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2086 goto err;
2087 }
David Benjamin854dd652014-08-26 00:32:30 -04002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 /* The handshake buffer is no longer necessary, and we may hash the current
2090 * message.*/
2091 if (s->s3->handshake_buffer &&
2092 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2093 goto err;
2094 }
David Benjaminfbdfefb2015-02-16 19:33:53 -05002095 if (!ssl3_hash_current_message(s)) {
2096 goto err;
2097 }
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 /* Parse and verify the signature. */
2100 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2101 CBS_len(&certificate_verify) != 0) {
2102 al = SSL_AD_DECODE_ERROR;
2103 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
2104 goto f_err;
2105 }
Adam Langley95c29f32014-06-20 12:00:00 -07002106
Adam Langleyfcf25832014-12-18 17:42:32 -08002107 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2108 if (pctx == NULL) {
2109 goto err;
2110 }
2111 if (!EVP_PKEY_verify_init(pctx) ||
2112 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2113 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2114 digest_length)) {
2115 al = SSL_AD_DECRYPT_ERROR;
2116 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2117 goto f_err;
2118 }
2119
2120 ret = 1;
2121
2122 if (0) {
2123 f_err:
2124 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2125 }
2126
David Benjamin854dd652014-08-26 00:32:30 -04002127err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002128 EVP_PKEY_CTX_free(pctx);
2129 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 return ret;
2132}
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134int ssl3_get_client_certificate(SSL *s) {
2135 int i, ok, al, ret = -1;
2136 X509 *x = NULL;
2137 unsigned long n;
2138 STACK_OF(X509) *sk = NULL;
2139 SHA256_CTX sha256;
2140 CBS certificate_msg, certificate_list;
2141 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
David Benjamin1d0a1942015-04-26 15:35:35 -04002144 (long)s->max_cert_list, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002145
Adam Langleyfcf25832014-12-18 17:42:32 -08002146 if (!ok) {
2147 return n;
2148 }
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2151 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2152 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2153 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2154 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2155 al = SSL_AD_HANDSHAKE_FAILURE;
2156 goto f_err;
2157 }
David Benjamind5b1f842014-07-11 23:43:48 -04002158
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 /* If tls asked for a client cert, the client must return a 0 list */
2160 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
2161 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2162 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2163 al = SSL_AD_UNEXPECTED_MESSAGE;
2164 goto f_err;
2165 }
2166 s->s3->tmp.reuse_message = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002167
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 return 1;
2169 }
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2172 al = SSL_AD_UNEXPECTED_MESSAGE;
2173 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2174 SSL_R_WRONG_MESSAGE_TYPE);
2175 goto f_err;
2176 }
David Benjamind5b1f842014-07-11 23:43:48 -04002177
Adam Langleyfcf25832014-12-18 17:42:32 -08002178 CBS_init(&certificate_msg, s->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04002179
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 sk = sk_X509_new_null();
2181 if (sk == NULL) {
2182 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2183 goto err;
2184 }
Adam Langley95c29f32014-06-20 12:00:00 -07002185
Adam Langleyfcf25832014-12-18 17:42:32 -08002186 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2187 CBS_len(&certificate_msg) != 0) {
2188 al = SSL_AD_DECODE_ERROR;
2189 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2190 goto f_err;
2191 }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 while (CBS_len(&certificate_list) > 0) {
2194 CBS certificate;
2195 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07002196
Adam Langleyfcf25832014-12-18 17:42:32 -08002197 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2198 al = SSL_AD_DECODE_ERROR;
2199 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
2200 goto f_err;
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langleyfcf25832014-12-18 17:42:32 -08002203 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2204 /* If this is the first certificate, and we don't want to keep peer
2205 * certificates in memory, then we hash it right away. */
2206 SHA256_Init(&sha256);
2207 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2208 SHA256_Final(s->session->peer_sha256, &sha256);
2209 s->session->peer_sha256_valid = 1;
2210 }
2211 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 data = CBS_data(&certificate);
2214 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2215 if (x == NULL) {
2216 al = SSL_AD_BAD_CERTIFICATE;
2217 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
2218 goto f_err;
2219 }
2220 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2221 al = SSL_AD_DECODE_ERROR;
2222 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2223 SSL_R_CERT_LENGTH_MISMATCH);
2224 goto f_err;
2225 }
2226 if (!sk_X509_push(sk, x)) {
2227 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2228 goto err;
2229 }
2230 x = NULL;
2231 }
2232
2233 if (sk_X509_num(sk) <= 0) {
2234 /* TLS does not mind 0 certs returned */
2235 if (s->version == SSL3_VERSION) {
2236 al = SSL_AD_HANDSHAKE_FAILURE;
2237 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2238 SSL_R_NO_CERTIFICATES_RETURNED);
2239 goto f_err;
2240 }
2241 /* Fail for TLS only if we required a certificate */
2242 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2243 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2244 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2245 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2246 al = SSL_AD_HANDSHAKE_FAILURE;
2247 goto f_err;
2248 }
2249 /* No client certificate so digest cached records */
2250 if (s->s3->handshake_buffer &&
2251 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2252 al = SSL_AD_INTERNAL_ERROR;
2253 goto f_err;
2254 }
2255 } else {
2256 i = ssl_verify_cert_chain(s, sk);
2257 if (i <= 0) {
2258 al = ssl_verify_alarm_type(s->verify_result);
2259 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate,
2260 SSL_R_CERTIFICATE_VERIFY_FAILED);
2261 goto f_err;
2262 }
2263 }
2264
David Benjamin2755a3e2015-04-22 16:17:58 -04002265 X509_free(s->session->peer);
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 s->session->peer = sk_X509_shift(sk);
2267 s->session->verify_result = s->verify_result;
2268
2269 /* With the current implementation, sess_cert will always be NULL when we
2270 * arrive here. */
2271 if (s->session->sess_cert == NULL) {
2272 s->session->sess_cert = ssl_sess_cert_new();
2273 if (s->session->sess_cert == NULL) {
2274 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2275 goto err;
2276 }
2277 }
David Benjamin2755a3e2015-04-22 16:17:58 -04002278 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002279 s->session->sess_cert->cert_chain = sk;
2280 /* Inconsistency alert: cert_chain does *not* include the peer's own
2281 * certificate, while we do include it in s3_clnt.c */
2282
2283 sk = NULL;
2284
2285 ret = 1;
2286
2287 if (0) {
2288 f_err:
2289 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2290 }
2291
Adam Langley95c29f32014-06-20 12:00:00 -07002292err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002293 X509_free(x);
2294 sk_X509_pop_free(sk, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 return ret;
2296}
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298int ssl3_send_server_certificate(SSL *s) {
2299 CERT_PKEY *cpk;
Adam Langley95c29f32014-06-20 12:00:00 -07002300
Adam Langleyfcf25832014-12-18 17:42:32 -08002301 if (s->state == SSL3_ST_SW_CERT_A) {
2302 cpk = ssl_get_server_send_pkey(s);
2303 if (cpk == NULL) {
2304 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate,
2305 ERR_R_INTERNAL_ERROR);
2306 return 0;
2307 }
Adam Langley95c29f32014-06-20 12:00:00 -07002308
David Benjamin9d0847a2015-02-16 03:57:55 -05002309 if (!ssl3_output_cert_chain(s, cpk)) {
2310 return 0;
2311 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 s->state = SSL3_ST_SW_CERT_B;
2313 }
Adam Langley95c29f32014-06-20 12:00:00 -07002314
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 /* SSL3_ST_SW_CERT_B */
2316 return ssl_do_write(s);
2317}
Adam Langley95c29f32014-06-20 12:00:00 -07002318
Adam Langley95c29f32014-06-20 12:00:00 -07002319/* send a new session ticket (not necessarily for a new session) */
Adam Langleyfcf25832014-12-18 17:42:32 -08002320int ssl3_send_new_session_ticket(SSL *s) {
David Benjaminbcd37452015-03-09 14:48:28 -04002321 int ret = -1;
2322 uint8_t *session = NULL;
2323 size_t session_len;
2324 EVP_CIPHER_CTX ctx;
2325 HMAC_CTX hctx;
2326
2327 EVP_CIPHER_CTX_init(&ctx);
2328 HMAC_CTX_init(&hctx);
2329
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 uint8_t *p, *macstart;
2332 int len;
2333 unsigned int hlen;
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 SSL_CTX *tctx = s->initial_ctx;
2335 uint8_t iv[EVP_MAX_IV_LENGTH];
2336 uint8_t key_name[16];
2337 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2338 * one block of encryption overhead + HMAC. */
2339 const size_t max_ticket_overhead =
2340 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07002341
Adam Langleyfcf25832014-12-18 17:42:32 -08002342 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2343 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002344 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 }
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 /* If the session is too long, emit a dummy value rather than abort the
2348 * connection. */
2349 if (session_len > 0xFFFF - max_ticket_overhead) {
2350 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2351 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002354 session = NULL;
David Benjamin3cac4502014-10-21 01:46:30 -04002355
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 p = ssl_handshake_start(s);
2357 /* Emit ticket_lifetime_hint. */
2358 l2n(0, p);
2359 /* Emit ticket. */
2360 s2n(placeholder_len, p);
2361 memcpy(p, kTicketPlaceholder, placeholder_len);
2362 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04002363
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 len = p - ssl_handshake_start(s);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002365 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002366 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002367 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2369 return ssl_do_write(s);
2370 }
Adam Langley95c29f32014-06-20 12:00:00 -07002371
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 /* Grow buffer if need be: the length calculation is as follows:
2373 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2374 * max_ticket_overhead + * session_length */
2375 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2376 max_ticket_overhead + session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002377 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002378 }
2379 p = ssl_handshake_start(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 /* Initialize HMAC and cipher contexts. If callback present it does all the
2381 * work otherwise use generated values from parent ctx. */
2382 if (tctx->tlsext_ticket_key_cb) {
David Benjaminadcc3952015-04-26 13:07:57 -04002383 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx,
2384 1 /* encrypt */) < 0) {
David Benjaminbcd37452015-03-09 14:48:28 -04002385 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 }
2387 } else {
2388 if (!RAND_bytes(iv, 16) ||
2389 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2390 tctx->tlsext_tick_aes_key, iv) ||
2391 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2392 NULL)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002393 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 }
2395 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2396 }
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2399 * resumed session (for simplicity), and guess that tickets for new
2400 * sessions will live as long as their sessions. */
2401 l2n(s->hit ? 0 : s->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002402
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 /* Skip ticket length for now */
2404 p += 2;
2405 /* Output key name */
2406 macstart = p;
2407 memcpy(p, key_name, 16);
2408 p += 16;
2409 /* output IV */
2410 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2411 p += EVP_CIPHER_CTX_iv_length(&ctx);
2412 /* Encrypt session data */
David Benjaminbcd37452015-03-09 14:48:28 -04002413 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2414 goto err;
2415 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 p += len;
David Benjaminbcd37452015-03-09 14:48:28 -04002417 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2418 goto err;
2419 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002420 p += len;
Adam Langley95c29f32014-06-20 12:00:00 -07002421
David Benjaminbcd37452015-03-09 14:48:28 -04002422 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2423 !HMAC_Final(&hctx, p, &hlen)) {
2424 goto err;
2425 }
Adam Langley95c29f32014-06-20 12:00:00 -07002426
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 p += hlen;
2428 /* Now write out lengths: p points to end of data written */
2429 /* Total length */
2430 len = p - ssl_handshake_start(s);
2431 /* Skip ticket lifetime hint */
2432 p = ssl_handshake_start(s) + 4;
2433 s2n(len - 6, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -05002434 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002435 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002436 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002437 s->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08002438 }
Adam Langley95c29f32014-06-20 12:00:00 -07002439
Adam Langleyfcf25832014-12-18 17:42:32 -08002440 /* SSL3_ST_SW_SESSION_TICKET_B */
David Benjaminbcd37452015-03-09 14:48:28 -04002441 ret = ssl_do_write(s);
2442
2443err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002444 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002445 EVP_CIPHER_CTX_cleanup(&ctx);
2446 HMAC_CTX_cleanup(&hctx);
2447 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002448}
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langley95c29f32014-06-20 12:00:00 -07002450/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2451 * sets the next_proto member in s if found */
Adam Langleyfcf25832014-12-18 17:42:32 -08002452int ssl3_get_next_proto(SSL *s) {
2453 int ok;
2454 long n;
2455 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002456
Adam Langleyfcf25832014-12-18 17:42:32 -08002457 /* Clients cannot send a NextProtocol message if we didn't see the extension
2458 * in their ClientHello */
2459 if (!s->s3->next_proto_neg_seen) {
2460 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2461 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2462 return -1;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2466 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2467 514, /* See the payload format below */
David Benjamin5ca39fb2015-03-01 23:57:54 -05002468 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langleyfcf25832014-12-18 17:42:32 -08002470 if (!ok) {
2471 return n;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2475 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2476 * ssl3_get_finished).
2477 *
2478 * TODO(davidben): Is this check now redundant with
2479 * SSL3_FLAGS_EXPECT_CCS? */
2480 if (!s->s3->change_cipher_spec) {
2481 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto,
2482 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2483 return -1;
2484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 /* The payload looks like:
2489 * uint8 proto_len;
2490 * uint8 proto[proto_len];
2491 * uint8 padding_len;
2492 * uint8 padding[padding_len]; */
2493 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2494 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2495 CBS_len(&next_protocol) != 0 ||
2496 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2497 &s->next_proto_negotiated_len)) {
2498 return 0;
2499 }
Adam Langley95c29f32014-06-20 12:00:00 -07002500
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 return 1;
2502}
Adam Langley95c29f32014-06-20 12:00:00 -07002503
Adam Langley1258b6a2014-06-20 12:00:00 -07002504/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002505int ssl3_get_channel_id(SSL *s) {
2506 int ret = -1, ok;
2507 long n;
2508 EVP_MD_CTX md_ctx;
2509 uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
2510 unsigned int channel_id_hash_len;
2511 const uint8_t *p;
2512 uint16_t extension_type, expected_extension_type;
2513 EC_GROUP *p256 = NULL;
2514 EC_KEY *key = NULL;
2515 EC_POINT *point = NULL;
2516 ECDSA_SIG sig;
2517 BIGNUM x, y;
2518 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 n = s->method->ssl_get_message(
2521 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2522 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
David Benjamin5ca39fb2015-03-01 23:57:54 -05002523 ssl_dont_hash_message, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002524
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 if (!ok) {
2526 return n;
2527 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002528
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 /* Before incorporating the EncryptedExtensions message to the handshake
2530 * hash, compute the hash that should have been signed. */
2531 channel_id_hash_len = sizeof(channel_id_hash);
2532 EVP_MD_CTX_init(&md_ctx);
2533 if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
2534 !tls1_channel_id_hash(&md_ctx, s) ||
2535 !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) {
2536 EVP_MD_CTX_cleanup(&md_ctx);
2537 return -1;
2538 }
2539 EVP_MD_CTX_cleanup(&md_ctx);
2540 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002541
David Benjaminfbdfefb2015-02-16 19:33:53 -05002542 if (!ssl3_hash_current_message(s)) {
2543 return -1;
2544 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2547 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2548 * ssl3_get_finished).
2549 *
2550 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2551 if (!s->s3->change_cipher_spec) {
2552 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2553 SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2554 return -1;
2555 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002556
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002558
Adam Langleyfcf25832014-12-18 17:42:32 -08002559 /* EncryptedExtensions could include multiple extensions, but the only
2560 * extension that could be negotiated is ChannelID, so there can only be one
2561 * entry.
2562 *
2563 * The payload looks like:
2564 * uint16 extension_type
2565 * uint16 extension_len;
2566 * uint8 x[32];
2567 * uint8 y[32];
2568 * uint8 r[32];
2569 * uint8 s[32]; */
2570 expected_extension_type = TLSEXT_TYPE_channel_id;
2571 if (s->s3->tlsext_channel_id_new) {
2572 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2573 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2576 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2577 CBS_len(&encrypted_extensions) != 0 ||
2578 extension_type != expected_extension_type ||
2579 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2580 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2581 return -1;
2582 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2585 if (!p256) {
2586 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2587 return -1;
2588 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 BN_init(&x);
2591 BN_init(&y);
2592 sig.r = BN_new();
2593 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05002594 if (sig.r == NULL || sig.s == NULL) {
2595 goto err;
2596 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 p = CBS_data(&extension);
2599 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2600 BN_bin2bn(p + 32, 32, &y) == NULL ||
2601 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2602 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2603 goto err;
2604 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 point = EC_POINT_new(p256);
2607 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2608 goto err;
2609 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611 key = EC_KEY_new();
2612 if (!key || !EC_KEY_set_group(key, p256) ||
2613 !EC_KEY_set_public_key(key, point)) {
2614 goto err;
2615 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2618 * were called. */
2619 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2620 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id,
2621 SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2622 s->s3->tlsext_channel_id_valid = 0;
2623 goto err;
2624 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 memcpy(s->s3->tlsext_channel_id, p, 64);
2627 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002628
2629err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002630 BN_free(&x);
2631 BN_free(&y);
2632 BN_free(sig.r);
2633 BN_free(sig.s);
David Benjamin2755a3e2015-04-22 16:17:58 -04002634 EC_KEY_free(key);
2635 EC_POINT_free(point);
2636 EC_GROUP_free(p256);
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 return ret;
2638}