blob: fad2d0a9e5e86df4a829bcb54591db5e04502152 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
Kenny Rootb8494592015-09-25 02:29:14 +0000149#include <openssl/ssl.h>
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151#include <assert.h>
152#include <stdio.h>
153#include <string.h>
154
155#include <openssl/bn.h>
156#include <openssl/buf.h>
157#include <openssl/bytestring.h>
158#include <openssl/cipher.h>
159#include <openssl/dh.h>
160#include <openssl/ec.h>
161#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700162#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163#include <openssl/evp.h>
164#include <openssl/hmac.h>
165#include <openssl/md5.h>
166#include <openssl/mem.h>
167#include <openssl/obj.h>
168#include <openssl/rand.h>
169#include <openssl/sha.h>
170#include <openssl/x509.h>
171
Adam Langleye9ada862015-05-11 17:20:37 -0700172#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800173#include "../crypto/internal.h"
174#include "../crypto/dh/internal.h"
175
176
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177int ssl3_accept(SSL *s) {
178 BUF_MEM *buf = NULL;
Adam Langleye9ada862015-05-11 17:20:37 -0700179 uint32_t alg_a;
Kenny Roote99801b2015-11-06 15:31:15 -0800180 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800181 int ret = -1;
182 int new_state, state, skip = 0;
183
184 assert(s->handshake_func == ssl3_accept);
185 assert(s->server);
186 assert(!SSL_IS_DTLS(s));
187
188 ERR_clear_error();
189 ERR_clear_system_error();
190
191 if (s->info_callback != NULL) {
192 cb = s->info_callback;
193 } else if (s->ctx->info_callback != NULL) {
194 cb = s->ctx->info_callback;
195 }
196
197 s->in_handshake++;
198
199 if (s->cert == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000200 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201 return -1;
202 }
203
204 for (;;) {
205 state = s->state;
206
207 switch (s->state) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208 case SSL_ST_ACCEPT:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209 if (cb != NULL) {
210 cb(s, SSL_CB_HANDSHAKE_START, 1);
211 }
212
213 if (s->init_buf == NULL) {
214 buf = BUF_MEM_new();
215 if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
216 ret = -1;
217 goto end;
218 }
219 s->init_buf = buf;
220 buf = NULL;
221 }
222 s->init_num = 0;
223
Adam Langleye9ada862015-05-11 17:20:37 -0700224 /* Enable a write buffer. This groups handshake messages within a flight
225 * into a single write. */
226 if (!ssl_init_wbio_buffer(s, 1)) {
227 ret = -1;
228 goto end;
229 }
230
Kenny Rootb8494592015-09-25 02:29:14 +0000231 if (!ssl3_init_handshake_buffer(s)) {
232 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 ret = -1;
234 goto end;
235 }
236
237 if (!s->s3->have_version) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 s->state = SSL3_ST_SR_INITIAL_BYTES;
239 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 s->state = SSL3_ST_SR_CLNT_HELLO_A;
241 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 break;
243
244 case SSL3_ST_SR_INITIAL_BYTES:
245 ret = ssl3_get_initial_bytes(s);
246 if (ret <= 0) {
247 goto end;
248 }
249 /* ssl3_get_initial_bytes sets s->state to one of
250 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
251 break;
252
253 case SSL3_ST_SR_V2_CLIENT_HELLO:
254 ret = ssl3_get_v2_client_hello(s);
255 if (ret <= 0) {
256 goto end;
257 }
258 s->state = SSL3_ST_SR_CLNT_HELLO_A;
259 break;
260
261 case SSL3_ST_SR_CLNT_HELLO_A:
262 case SSL3_ST_SR_CLNT_HELLO_B:
263 case SSL3_ST_SR_CLNT_HELLO_C:
264 case SSL3_ST_SR_CLNT_HELLO_D:
265 s->shutdown = 0;
266 ret = ssl3_get_client_hello(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 if (ret <= 0) {
268 goto end;
269 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 s->state = SSL3_ST_SW_SRVR_HELLO_A;
271 s->init_num = 0;
272 break;
273
274 case SSL3_ST_SW_SRVR_HELLO_A:
275 case SSL3_ST_SW_SRVR_HELLO_B:
276 ret = ssl3_send_server_hello(s);
277 if (ret <= 0) {
278 goto end;
279 }
280 if (s->hit) {
281 if (s->tlsext_ticket_expected) {
282 s->state = SSL3_ST_SW_SESSION_TICKET_A;
283 } else {
284 s->state = SSL3_ST_SW_CHANGE_A;
285 }
286 } else {
287 s->state = SSL3_ST_SW_CERT_A;
288 }
289 s->init_num = 0;
290 break;
291
292 case SSL3_ST_SW_CERT_A:
293 case SSL3_ST_SW_CERT_B:
294 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
295 ret = ssl3_send_server_certificate(s);
296 if (ret <= 0) {
297 goto end;
298 }
299 if (s->s3->tmp.certificate_status_expected) {
300 s->state = SSL3_ST_SW_CERT_STATUS_A;
301 } else {
302 s->state = SSL3_ST_SW_KEY_EXCH_A;
303 }
304 } else {
305 skip = 1;
306 s->state = SSL3_ST_SW_KEY_EXCH_A;
307 }
308 s->init_num = 0;
309 break;
310
Kenny Rootb8494592015-09-25 02:29:14 +0000311 case SSL3_ST_SW_CERT_STATUS_A:
312 case SSL3_ST_SW_CERT_STATUS_B:
313 ret = ssl3_send_certificate_status(s);
314 if (ret <= 0) {
315 goto end;
316 }
317 s->state = SSL3_ST_SW_KEY_EXCH_A;
318 s->init_num = 0;
319 break;
320
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321 case SSL3_ST_SW_KEY_EXCH_A:
322 case SSL3_ST_SW_KEY_EXCH_B:
Kenny Rootb8494592015-09-25 02:29:14 +0000323 case SSL3_ST_SW_KEY_EXCH_C:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800324 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
325
326 /* Send a ServerKeyExchange message if:
327 * - The key exchange is ephemeral or anonymous
328 * Diffie-Hellman.
329 * - There is a PSK identity hint.
330 *
331 * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
332 * this. In the meantime, keep them in sync. */
333 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
334 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
335 ret = ssl3_send_server_key_exchange(s);
Adam Langleye9ada862015-05-11 17:20:37 -0700336 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800337 goto end;
Adam Langleye9ada862015-05-11 17:20:37 -0700338 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339 } else {
340 skip = 1;
341 }
342
343 s->state = SSL3_ST_SW_CERT_REQ_A;
344 s->init_num = 0;
345 break;
346
347 case SSL3_ST_SW_CERT_REQ_A:
348 case SSL3_ST_SW_CERT_REQ_B:
Adam Langleyf4e42722015-06-04 17:45:09 -0700349 if (s->s3->tmp.cert_request) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800350 ret = ssl3_send_certificate_request(s);
351 if (ret <= 0) {
352 goto end;
353 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700354 } else {
355 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800356 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700357 s->state = SSL3_ST_SW_SRVR_DONE_A;
358 s->init_num = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359 break;
360
361 case SSL3_ST_SW_SRVR_DONE_A:
362 case SSL3_ST_SW_SRVR_DONE_B:
363 ret = ssl3_send_server_done(s);
364 if (ret <= 0) {
365 goto end;
366 }
367 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
368 s->state = SSL3_ST_SW_FLUSH;
369 s->init_num = 0;
370 break;
371
372 case SSL3_ST_SW_FLUSH:
373 /* This code originally checked to see if any data was pending using
374 * BIO_CTRL_INFO and then flushed. This caused problems as documented
375 * in PR#1939. The proposed fix doesn't completely resolve this issue
376 * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
377 * we just flush unconditionally. */
378 s->rwstate = SSL_WRITING;
379 if (BIO_flush(s->wbio) <= 0) {
380 ret = -1;
381 goto end;
382 }
383 s->rwstate = SSL_NOTHING;
384
385 s->state = s->s3->tmp.next_state;
386 break;
387
388 case SSL3_ST_SR_CERT_A:
389 case SSL3_ST_SR_CERT_B:
390 if (s->s3->tmp.cert_request) {
391 ret = ssl3_get_client_certificate(s);
392 if (ret <= 0) {
393 goto end;
394 }
395 }
396 s->init_num = 0;
397 s->state = SSL3_ST_SR_KEY_EXCH_A;
398 break;
399
400 case SSL3_ST_SR_KEY_EXCH_A:
401 case SSL3_ST_SR_KEY_EXCH_B:
Kenny Roote99801b2015-11-06 15:31:15 -0800402 case SSL3_ST_SR_KEY_EXCH_C:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 ret = ssl3_get_client_key_exchange(s);
404 if (ret <= 0) {
405 goto end;
406 }
407 s->state = SSL3_ST_SR_CERT_VRFY_A;
408 s->init_num = 0;
409 break;
410
411 case SSL3_ST_SR_CERT_VRFY_A:
412 case SSL3_ST_SR_CERT_VRFY_B:
413 ret = ssl3_get_cert_verify(s);
414 if (ret <= 0) {
415 goto end;
416 }
417
418 s->state = SSL3_ST_SR_CHANGE;
419 s->init_num = 0;
420 break;
421
422 case SSL3_ST_SR_CHANGE: {
423 char next_proto_neg = 0;
424 char channel_id = 0;
425 next_proto_neg = s->s3->next_proto_neg_seen;
426 channel_id = s->s3->tlsext_channel_id_valid;
427
428 /* At this point, the next message must be entirely behind a
429 * ChangeCipherSpec. */
430 if (!ssl3_expect_change_cipher_spec(s)) {
431 ret = -1;
432 goto end;
433 }
434 if (next_proto_neg) {
435 s->state = SSL3_ST_SR_NEXT_PROTO_A;
436 } else if (channel_id) {
437 s->state = SSL3_ST_SR_CHANNEL_ID_A;
438 } else {
439 s->state = SSL3_ST_SR_FINISHED_A;
440 }
441 break;
442 }
443
444 case SSL3_ST_SR_NEXT_PROTO_A:
445 case SSL3_ST_SR_NEXT_PROTO_B:
446 ret = ssl3_get_next_proto(s);
447 if (ret <= 0) {
448 goto end;
449 }
450 s->init_num = 0;
451 if (s->s3->tlsext_channel_id_valid) {
452 s->state = SSL3_ST_SR_CHANNEL_ID_A;
453 } else {
454 s->state = SSL3_ST_SR_FINISHED_A;
455 }
456 break;
457
458 case SSL3_ST_SR_CHANNEL_ID_A:
459 case SSL3_ST_SR_CHANNEL_ID_B:
460 ret = ssl3_get_channel_id(s);
461 if (ret <= 0) {
462 goto end;
463 }
464 s->init_num = 0;
465 s->state = SSL3_ST_SR_FINISHED_A;
466 break;
467
468 case SSL3_ST_SR_FINISHED_A:
469 case SSL3_ST_SR_FINISHED_B:
470 ret =
471 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
472 if (ret <= 0) {
473 goto end;
474 }
475
476 if (s->hit) {
477 s->state = SSL_ST_OK;
478 } else if (s->tlsext_ticket_expected) {
479 s->state = SSL3_ST_SW_SESSION_TICKET_A;
480 } else {
481 s->state = SSL3_ST_SW_CHANGE_A;
482 }
483 /* If this is a full handshake with ChannelID then record the hashshake
484 * hashes in |s->session| in case we need them to verify a ChannelID
485 * signature on a resumption of this session in the future. */
Kenny Roote99801b2015-11-06 15:31:15 -0800486 if (!s->hit && s->s3->tlsext_channel_id_valid) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800487 ret = tls1_record_handshake_hashes_for_channel_id(s);
488 if (ret <= 0) {
489 goto end;
490 }
491 }
492 s->init_num = 0;
493 break;
494
495 case SSL3_ST_SW_SESSION_TICKET_A:
496 case SSL3_ST_SW_SESSION_TICKET_B:
497 ret = ssl3_send_new_session_ticket(s);
498 if (ret <= 0) {
499 goto end;
500 }
501 s->state = SSL3_ST_SW_CHANGE_A;
502 s->init_num = 0;
503 break;
504
505 case SSL3_ST_SW_CHANGE_A:
506 case SSL3_ST_SW_CHANGE_B:
507 s->session->cipher = s->s3->tmp.new_cipher;
508 if (!s->enc_method->setup_key_block(s)) {
509 ret = -1;
510 goto end;
511 }
512
513 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
514 SSL3_ST_SW_CHANGE_B);
515 if (ret <= 0) {
516 goto end;
517 }
518 s->state = SSL3_ST_SW_FINISHED_A;
519 s->init_num = 0;
520
521 if (!s->enc_method->change_cipher_state(
522 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
523 ret = -1;
524 goto end;
525 }
526 break;
527
528 case SSL3_ST_SW_FINISHED_A:
529 case SSL3_ST_SW_FINISHED_B:
530 ret =
531 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
532 s->enc_method->server_finished_label,
533 s->enc_method->server_finished_label_len);
534 if (ret <= 0) {
535 goto end;
536 }
537 s->state = SSL3_ST_SW_FLUSH;
538 if (s->hit) {
539 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
540 } else {
541 s->s3->tmp.next_state = SSL_ST_OK;
542 }
543 s->init_num = 0;
544 break;
545
546 case SSL_ST_OK:
547 /* clean a few things up */
548 ssl3_cleanup_key_block(s);
549
550 BUF_MEM_free(s->init_buf);
551 s->init_buf = NULL;
552
553 /* remove buffering on output */
554 ssl_free_wbio_buffer(s);
555
556 s->init_num = 0;
557
558 /* If we aren't retaining peer certificates then we can discard it
559 * now. */
Adam Langleye9ada862015-05-11 17:20:37 -0700560 if (s->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800561 X509_free(s->session->peer);
562 s->session->peer = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +0000563 sk_X509_pop_free(s->session->cert_chain, X509_free);
564 s->session->cert_chain = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800565 }
566
Adam Langleyf4e42722015-06-04 17:45:09 -0700567 s->s3->initial_handshake_complete = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800568
Adam Langleyf4e42722015-06-04 17:45:09 -0700569 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800570
Adam Langleyf4e42722015-06-04 17:45:09 -0700571 if (cb != NULL) {
572 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800573 }
574
575 ret = 1;
576 goto end;
577
578 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000579 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800580 ret = -1;
581 goto end;
582 }
583
584 if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) {
585 new_state = s->state;
586 s->state = state;
587 cb(s, SSL_CB_ACCEPT_LOOP, 1);
588 s->state = new_state;
589 }
590 skip = 0;
591 }
592
593end:
594 s->in_handshake--;
Adam Langleye9ada862015-05-11 17:20:37 -0700595 BUF_MEM_free(buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800596 if (cb != NULL) {
597 cb(s, SSL_CB_ACCEPT_EXIT, ret);
598 }
599 return ret;
600}
601
Adam Langleyd9e397b2015-01-22 14:27:53 -0800602int ssl3_get_initial_bytes(SSL *s) {
Kenny Rootb8494592015-09-25 02:29:14 +0000603 /* Read the first 5 bytes, the size of the TLS record header. This is
604 * sufficient to detect a V2ClientHello and ensures that we never read beyond
605 * the first record. */
606 int ret = ssl_read_buffer_extend_to(s, SSL3_RT_HEADER_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800607 if (ret <= 0) {
608 return ret;
609 }
Kenny Rootb8494592015-09-25 02:29:14 +0000610 assert(ssl_read_buffer_len(s) == SSL3_RT_HEADER_LENGTH);
611 const uint8_t *p = ssl_read_buffer(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800612
613 /* Some dedicated error codes for protocol mixups should the application wish
614 * to interpret them differently. (These do not overlap with ClientHello or
615 * V2ClientHello.) */
616 if (strncmp("GET ", (const char *)p, 4) == 0 ||
617 strncmp("POST ", (const char *)p, 5) == 0 ||
618 strncmp("HEAD ", (const char *)p, 5) == 0 ||
619 strncmp("PUT ", (const char *)p, 4) == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000620 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800621 return -1;
622 }
Kenny Rootb8494592015-09-25 02:29:14 +0000623 if (strncmp("CONNE", (const char *)p, 5) == 0) {
624 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800625 return -1;
626 }
627
Kenny Rootb8494592015-09-25 02:29:14 +0000628 /* Determine if this is a V2ClientHello. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800629 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
630 p[3] >= SSL3_VERSION_MAJOR) {
631 /* This is a V2ClientHello. */
632 s->state = SSL3_ST_SR_V2_CLIENT_HELLO;
633 return 1;
634 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800635
Kenny Rootb8494592015-09-25 02:29:14 +0000636 /* Fall through to the standard logic. */
637 s->state = SSL3_ST_SR_CLNT_HELLO_A;
638 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800639}
640
641int ssl3_get_v2_client_hello(SSL *s) {
642 const uint8_t *p;
643 int ret;
644 CBS v2_client_hello, cipher_specs, session_id, challenge;
645 size_t msg_length, rand_len, len;
646 uint8_t msg_type;
647 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
648 CBB client_hello, hello_body, cipher_suites;
649 uint8_t random[SSL3_RANDOM_SIZE];
650
Kenny Rootb8494592015-09-25 02:29:14 +0000651 /* Determine the length of the V2ClientHello. */
652 assert(ssl_read_buffer_len(s) >= SSL3_RT_HEADER_LENGTH);
653 p = ssl_read_buffer(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800654 msg_length = ((p[0] & 0x7f) << 8) | p[1];
655 if (msg_length > (1024 * 4)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000656 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800657 return -1;
658 }
Kenny Rootb8494592015-09-25 02:29:14 +0000659 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
660 /* Reject lengths that are too short early. We have already read
661 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
662 * (invalid) V2ClientHello which would be shorter than that. */
663 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800664 return -1;
665 }
666
Kenny Rootb8494592015-09-25 02:29:14 +0000667 /* Read the remainder of the V2ClientHello. */
668 ret = ssl_read_buffer_extend_to(s, 2 + msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800669 if (ret <= 0) {
670 return ret;
671 }
Kenny Rootb8494592015-09-25 02:29:14 +0000672 assert(ssl_read_buffer_len(s) == msg_length + 2);
673 CBS_init(&v2_client_hello, ssl_read_buffer(s) + 2, msg_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674
Kenny Rootb8494592015-09-25 02:29:14 +0000675 /* The V2ClientHello without the length is incorporated into the handshake
Adam Langleyd9e397b2015-01-22 14:27:53 -0800676 * hash. */
Kenny Rootb8494592015-09-25 02:29:14 +0000677 if (!ssl3_update_handshake_hash(s, CBS_data(&v2_client_hello),
678 CBS_len(&v2_client_hello))) {
Adam Langleye9ada862015-05-11 17:20:37 -0700679 return -1;
680 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800681 if (s->msg_callback) {
682 s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
683 CBS_len(&v2_client_hello), s, s->msg_callback_arg);
684 }
685
686 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
687 !CBS_get_u16(&v2_client_hello, &version) ||
688 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
689 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
690 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
691 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
692 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
693 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
694 CBS_len(&v2_client_hello) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000695 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800696 return -1;
697 }
698
699 /* msg_type has already been checked. */
700 assert(msg_type == SSL2_MT_CLIENT_HELLO);
701
702 /* The client_random is the V2ClientHello challenge. Truncate or
703 * left-pad with zeros as needed. */
704 memset(random, 0, SSL3_RANDOM_SIZE);
705 rand_len = CBS_len(&challenge);
706 if (rand_len > SSL3_RANDOM_SIZE) {
707 rand_len = SSL3_RANDOM_SIZE;
708 }
709 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
710 rand_len);
711
712 /* Write out an equivalent SSLv3 ClientHello. */
Kenny Rootb8494592015-09-25 02:29:14 +0000713 CBB_zero(&client_hello);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800714 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data,
Kenny Rootb8494592015-09-25 02:29:14 +0000715 s->init_buf->max) ||
716 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800717 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
718 !CBB_add_u16(&hello_body, version) ||
719 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
720 /* No session id. */
721 !CBB_add_u8(&hello_body, 0) ||
722 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
723 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000724 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800725 return -1;
726 }
727
728 /* Copy the cipher suites. */
729 while (CBS_len(&cipher_specs) > 0) {
730 uint32_t cipher_spec;
731 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
732 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000733 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800734 return -1;
735 }
736
737 /* Skip SSLv2 ciphers. */
738 if ((cipher_spec & 0xff0000) != 0) {
739 continue;
740 }
741 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
742 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000743 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800744 return -1;
745 }
746 }
747
748 /* Add the null compression scheme and finish. */
749 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
750 !CBB_finish(&client_hello, NULL, &len)) {
751 CBB_cleanup(&client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +0000752 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800753 return -1;
754 }
755
756 /* Mark the message for "re"-use by the version-specific method. */
757 s->s3->tmp.reuse_message = 1;
758 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
759 /* The handshake message header is 4 bytes. */
760 s->s3->tmp.message_size = len - 4;
761
Kenny Rootb8494592015-09-25 02:29:14 +0000762 /* Consume and discard the V2ClientHello. */
763 ssl_read_buffer_consume(s, 2 + msg_length);
764 ssl_read_buffer_discard(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800765
766 return 1;
767}
768
Adam Langleyd9e397b2015-01-22 14:27:53 -0800769int ssl3_get_client_hello(SSL *s) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700770 int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800771 long n;
772 const SSL_CIPHER *c;
773 STACK_OF(SSL_CIPHER) *ciphers = NULL;
774 struct ssl_early_callback_ctx early_ctx;
775 CBS client_hello;
776 uint16_t client_version;
777 CBS client_random, session_id, cipher_suites, compression_methods;
Kenny Rootb8494592015-09-25 02:29:14 +0000778 SSL_SESSION *session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800779
780 /* We do this so that we will respond with our native type. If we are TLSv1
781 * and we get SSLv3, we will respond with TLSv1, This down switching should
782 * be handled by a different method. If we are SSLv3, we will respond with
783 * SSLv3, even if prompted with TLSv1. */
784 switch (s->state) {
785 case SSL3_ST_SR_CLNT_HELLO_A:
786 case SSL3_ST_SR_CLNT_HELLO_B:
787 n = s->method->ssl_get_message(
788 s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
789 SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
Adam Langleye9ada862015-05-11 17:20:37 -0700790 ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800791
792 if (!ok) {
793 return n;
794 }
795
Adam Langleyd9e397b2015-01-22 14:27:53 -0800796 s->state = SSL3_ST_SR_CLNT_HELLO_C;
797 /* fallthrough */
798 case SSL3_ST_SR_CLNT_HELLO_C:
799 case SSL3_ST_SR_CLNT_HELLO_D:
800 /* We have previously parsed the ClientHello message, and can't call
801 * ssl_get_message again without hashing the message into the Finished
802 * digest again. */
803 n = s->init_num;
804
805 memset(&early_ctx, 0, sizeof(early_ctx));
806 early_ctx.ssl = s;
807 early_ctx.client_hello = s->init_msg;
808 early_ctx.client_hello_len = n;
809 if (!ssl_early_callback_init(&early_ctx)) {
810 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000811 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800812 goto f_err;
813 }
814
815 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
816 s->ctx->select_certificate_cb != NULL) {
817 s->state = SSL3_ST_SR_CLNT_HELLO_D;
818 switch (s->ctx->select_certificate_cb(&early_ctx)) {
819 case 0:
Adam Langleye9ada862015-05-11 17:20:37 -0700820 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
821 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800822
823 case -1:
824 /* Connection rejected. */
825 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000826 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800827 goto f_err;
828
829 default:
830 /* fallthrough */;
831 }
832 }
833 s->state = SSL3_ST_SR_CLNT_HELLO_D;
834 break;
835
836 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000837 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800838 return -1;
839 }
840
841 CBS_init(&client_hello, s->init_msg, n);
842 if (!CBS_get_u16(&client_hello, &client_version) ||
843 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
844 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
845 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
846 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000847 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800848 goto f_err;
849 }
850
851 /* use version from inside client hello, not from record header (may differ:
852 * see RFC 2246, Appendix E, second paragraph) */
853 s->client_version = client_version;
854
855 /* Load the client random. */
856 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
857
858 if (SSL_IS_DTLS(s)) {
859 CBS cookie;
860
861 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
862 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
863 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000864 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800865 goto f_err;
866 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800867 }
868
Adam Langleyf4e42722015-06-04 17:45:09 -0700869 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
870 * asynchronously.
871 *
872 * TODO(davidben): Clean up the order of events around ClientHello
873 * processing. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800874 if (!s->s3->have_version) {
875 /* Select version to use */
876 uint16_t version = ssl3_get_mutual_version(s, client_version);
877 if (version == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000878 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800879 s->version = s->client_version;
880 al = SSL_AD_PROTOCOL_VERSION;
881 goto f_err;
882 }
883 s->version = version;
884 s->enc_method = ssl3_get_enc_method(version);
885 assert(s->enc_method != NULL);
886 /* At this point, the connection's version is known and |s->version| is
887 * fixed. Begin enforcing the record-layer version. */
888 s->s3->have_version = 1;
889 } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version)
890 : (s->client_version < s->version)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000891 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800892 al = SSL_AD_PROTOCOL_VERSION;
893 goto f_err;
894 }
895
896 s->hit = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000897 int send_new_ticket = 0;
898 switch (ssl_get_prev_session(s, &session, &send_new_ticket, &early_ctx)) {
899 case ssl_session_success:
900 break;
901 case ssl_session_error:
902 goto err;
903 case ssl_session_retry:
904 s->rwstate = SSL_PENDING_SESSION;
905 goto err;
Adam Langleyf4e42722015-06-04 17:45:09 -0700906 }
Kenny Rootb8494592015-09-25 02:29:14 +0000907 s->tlsext_ticket_expected = send_new_ticket;
Adam Langleyf4e42722015-06-04 17:45:09 -0700908
909 /* The EMS state is needed when making the resumption decision, but
910 * extensions are not normally parsed until later. This detects the EMS
911 * extension for the resumption decision and it's checked against the result
912 * of the normal parse later in this function. */
913 const uint8_t *ems_data;
914 size_t ems_len;
915 int have_extended_master_secret =
916 s->version != SSL3_VERSION &&
917 SSL_early_callback_ctx_extension_get(&early_ctx,
918 TLSEXT_TYPE_extended_master_secret,
919 &ems_data, &ems_len) &&
920 ems_len == 0;
921
Kenny Rootb8494592015-09-25 02:29:14 +0000922 if (session != NULL) {
923 if (session->extended_master_secret &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700924 !have_extended_master_secret) {
925 /* A ClientHello without EMS that attempts to resume a session with EMS
926 * is fatal to the connection. */
927 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000928 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -0700929 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800930 }
931
Adam Langleyf4e42722015-06-04 17:45:09 -0700932 s->hit =
933 /* Only resume if the session's version matches the negotiated version:
934 * most clients do not accept a mismatch. */
Kenny Rootb8494592015-09-25 02:29:14 +0000935 s->version == session->ssl_version &&
Adam Langleyf4e42722015-06-04 17:45:09 -0700936 /* If the client offers the EMS extension, but the previous session
937 * didn't use it, then negotiate a new session. */
Kenny Rootb8494592015-09-25 02:29:14 +0000938 have_extended_master_secret == session->extended_master_secret;
Adam Langleyf4e42722015-06-04 17:45:09 -0700939 }
940
Kenny Rootb8494592015-09-25 02:29:14 +0000941 if (s->hit) {
942 /* Use the new session. */
943 SSL_SESSION_free(s->session);
944 s->session = session;
945 session = NULL;
946
947 s->verify_result = s->session->verify_result;
Kenny Roote99801b2015-11-06 15:31:15 -0800948 } else {
949 if (!ssl_get_new_session(s, 1 /* server */)) {
950 goto err;
951 }
952
953 /* Clear the session ID if we want the session to be single-use. */
954 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
955 s->session->session_id_length = 0;
956 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800957 }
958
Adam Langleye9ada862015-05-11 17:20:37 -0700959 if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) {
960 /* Connection rejected for DOS reasons. */
961 al = SSL_AD_ACCESS_DENIED;
Kenny Rootb8494592015-09-25 02:29:14 +0000962 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleye9ada862015-05-11 17:20:37 -0700963 goto f_err;
964 }
965
Adam Langleyd9e397b2015-01-22 14:27:53 -0800966 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
Adam Langleye9ada862015-05-11 17:20:37 -0700967 CBS_len(&cipher_suites) == 0 ||
968 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800969 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
970 CBS_len(&compression_methods) == 0) {
971 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000972 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800973 goto f_err;
974 }
975
Adam Langleyd9e397b2015-01-22 14:27:53 -0800976 ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites);
977 if (ciphers == NULL) {
978 goto err;
979 }
980
981 /* If it is a hit, check that the cipher is in the list. */
Adam Langleye9ada862015-05-11 17:20:37 -0700982 if (s->hit) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800983 size_t j;
984 int found_cipher = 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700985 uint32_t id = s->session->cipher->id;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800986
987 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
988 c = sk_SSL_CIPHER_value(ciphers, j);
989 if (c->id == id) {
990 found_cipher = 1;
991 break;
992 }
993 }
994
995 if (!found_cipher) {
996 /* we need to have the cipher in the cipher list if we are asked to reuse
997 * it */
998 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000999 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001000 goto f_err;
1001 }
1002 }
1003
1004 /* Only null compression is supported. */
1005 if (memchr(CBS_data(&compression_methods), 0,
1006 CBS_len(&compression_methods)) == NULL) {
1007 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +00001008 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001009 goto f_err;
1010 }
1011
1012 /* TLS extensions. */
1013 if (s->version >= SSL3_VERSION &&
1014 !ssl_parse_clienthello_tlsext(s, &client_hello)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001015 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001016 goto err;
1017 }
1018
1019 /* There should be nothing left over in the record. */
1020 if (CBS_len(&client_hello) != 0) {
1021 /* wrong packet length */
1022 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001023 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001024 goto f_err;
1025 }
1026
Adam Langleyf4e42722015-06-04 17:45:09 -07001027 if (have_extended_master_secret != s->s3->tmp.extended_master_secret) {
1028 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001029 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
Adam Langleyf4e42722015-06-04 17:45:09 -07001030 goto f_err;
1031 }
1032
Adam Langleyd9e397b2015-01-22 14:27:53 -08001033 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1034 if (!s->hit) {
1035 if (ciphers == NULL) {
1036 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +00001037 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001038 goto f_err;
1039 }
1040
1041 /* Let cert callback update server certificates if required */
1042 if (s->cert->cert_cb) {
1043 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1044 if (rv == 0) {
1045 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001046 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001047 goto f_err;
1048 }
1049 if (rv < 0) {
1050 s->rwstate = SSL_X509_LOOKUP;
1051 goto err;
1052 }
1053 s->rwstate = SSL_NOTHING;
1054 }
1055 c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
1056
1057 if (c == NULL) {
1058 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001059 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001060 goto f_err;
1061 }
1062 s->s3->tmp.new_cipher = c;
Adam Langleyf4e42722015-06-04 17:45:09 -07001063
1064 /* Determine whether to request a client certificate. */
1065 s->s3->tmp.cert_request = !!(s->verify_mode & SSL_VERIFY_PEER);
1066 /* Only request a certificate if Channel ID isn't negotiated. */
1067 if ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1068 s->s3->tlsext_channel_id_valid) {
1069 s->s3->tmp.cert_request = 0;
1070 }
1071 /* Plain PSK forbids Certificate and CertificateRequest. */
1072 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) {
1073 s->s3->tmp.cert_request = 0;
1074 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001075 } else {
1076 /* Session-id reuse */
1077 s->s3->tmp.new_cipher = s->session->cipher;
Adam Langleyf4e42722015-06-04 17:45:09 -07001078 s->s3->tmp.cert_request = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001079 }
1080
Kenny Rootb8494592015-09-25 02:29:14 +00001081 /* Now that the cipher is known, initialize the handshake hash. */
1082 if (!ssl3_init_handshake_hash(s)) {
1083 goto f_err;
1084 }
1085
Adam Langleyf4e42722015-06-04 17:45:09 -07001086 /* In TLS 1.2, client authentication requires hashing the handshake transcript
1087 * under a different hash. Otherwise, release the handshake buffer. */
Kenny Rootb8494592015-09-25 02:29:14 +00001088 if (!SSL_USE_SIGALGS(s) || !s->s3->tmp.cert_request) {
1089 ssl3_free_handshake_buffer(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001090 }
1091
1092 /* we now have the following setup;
1093 * client_random
1094 * cipher_list - our prefered list of ciphers
1095 * ciphers - the clients prefered list of ciphers
1096 * compression - basically ignored right now
1097 * ssl version is set - sslv3
1098 * s->session - The ssl session has been setup.
1099 * s->hit - session reuse flag
1100 * s->tmp.new_cipher - the new cipher to use. */
1101
1102 if (ret < 0) {
1103 ret = -ret;
1104 }
1105
1106 if (0) {
1107 f_err:
1108 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1109 }
1110
1111err:
Adam Langleye9ada862015-05-11 17:20:37 -07001112 sk_SSL_CIPHER_free(ciphers);
Kenny Rootb8494592015-09-25 02:29:14 +00001113 SSL_SESSION_free(session);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001114 return ret;
1115}
1116
Kenny Roote99801b2015-11-06 15:31:15 -08001117int ssl3_send_server_hello(SSL *ssl) {
1118 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1119 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001120 }
1121
Kenny Roote99801b2015-11-06 15:31:15 -08001122 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1123
1124 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1125 * known attack while we fix ChannelID itself. */
1126 if (ssl->s3->tlsext_channel_id_valid &&
1127 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1128 ssl->s3->tlsext_channel_id_valid = 0;
1129 }
1130
1131 /* If this is a resumption and the original handshake didn't support
1132 * ChannelID then we didn't record the original handshake hashes in the
1133 * session and so cannot resume with ChannelIDs. */
1134 if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1135 ssl->s3->tlsext_channel_id_valid = 0;
1136 }
1137
1138 if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1139 1 /* server */)) {
1140 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1141 return -1;
1142 }
1143
1144 CBB cbb, session_id;
1145 size_t length;
1146 CBB_zero(&cbb);
1147 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1148 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1149 !CBB_add_u16(&cbb, ssl->version) ||
1150 !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1151 !CBB_add_u8_length_prefixed(&cbb, &session_id) ||
1152 !CBB_add_bytes(&session_id, ssl->session->session_id,
1153 ssl->session->session_id_length) ||
1154 !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1155 !CBB_add_u8(&cbb, 0 /* no compression */) ||
1156 !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1157 !CBB_finish(&cbb, NULL, &length) ||
1158 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1159 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1160 CBB_cleanup(&cbb);
1161 return -1;
1162 }
1163
1164 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1165 return ssl_do_write(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001166}
1167
Kenny Rootb8494592015-09-25 02:29:14 +00001168int ssl3_send_certificate_status(SSL *ssl) {
1169 if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1170 CBB out, ocsp_response;
1171 size_t length;
1172
1173 CBB_zero(&out);
1174 if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1175 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1176 !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
1177 !CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
1178 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1179 ssl->ctx->ocsp_response_length) ||
1180 !CBB_finish(&out, NULL, &length) ||
1181 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1182 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1183 CBB_cleanup(&out);
1184 return -1;
1185 }
1186
1187 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1188 }
1189
1190 /* SSL3_ST_SW_CERT_STATUS_B */
1191 return ssl_do_write(ssl);
1192}
1193
Adam Langleyd9e397b2015-01-22 14:27:53 -08001194int ssl3_send_server_done(SSL *s) {
1195 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
Adam Langleye9ada862015-05-11 17:20:37 -07001196 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1197 return -1;
1198 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001199 s->state = SSL3_ST_SW_SRVR_DONE_B;
1200 }
1201
1202 /* SSL3_ST_SW_SRVR_DONE_B */
1203 return ssl_do_write(s);
1204}
1205
1206int ssl3_send_server_key_exchange(SSL *s) {
1207 DH *dh = NULL, *dhp;
Adam Langleye9ada862015-05-11 17:20:37 -07001208 EC_KEY *ecdh = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001209 uint8_t *encodedPoint = NULL;
1210 int encodedlen = 0;
1211 uint16_t curve_id = 0;
1212 BN_CTX *bn_ctx = NULL;
1213 const char *psk_identity_hint = NULL;
1214 size_t psk_identity_hint_len = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001215 size_t sig_len;
1216 size_t max_sig_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001217 uint8_t *p, *d;
1218 int al, i;
Adam Langleye9ada862015-05-11 17:20:37 -07001219 uint32_t alg_k;
1220 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001221 int n;
1222 CERT *cert;
1223 BIGNUM *r[4];
Kenny Rootb8494592015-09-25 02:29:14 +00001224 int nr[4];
Adam Langleyd9e397b2015-01-22 14:27:53 -08001225 BUF_MEM *buf;
1226 EVP_MD_CTX md_ctx;
1227
Kenny Rootb8494592015-09-25 02:29:14 +00001228 if (s->state == SSL3_ST_SW_KEY_EXCH_C) {
1229 return ssl_do_write(s);
1230 }
1231
1232 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1233 if (!ssl_has_private_key(s)) {
1234 al = SSL_AD_INTERNAL_ERROR;
1235 goto f_err;
1236 }
1237 max_sig_len = ssl_private_key_max_signature_len(s);
1238 } else {
1239 max_sig_len = 0;
1240 }
1241
Adam Langleyd9e397b2015-01-22 14:27:53 -08001242 EVP_MD_CTX_init(&md_ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00001243 enum ssl_private_key_result_t sign_result;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001244 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1245 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1246 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1247 cert = s->cert;
1248
1249 buf = s->init_buf;
1250
1251 r[0] = r[1] = r[2] = r[3] = NULL;
1252 n = 0;
1253 if (alg_a & SSL_aPSK) {
1254 /* size for PSK identity hint */
1255 psk_identity_hint = s->psk_identity_hint;
1256 if (psk_identity_hint) {
1257 psk_identity_hint_len = strlen(psk_identity_hint);
1258 } else {
1259 psk_identity_hint_len = 0;
1260 }
1261 n += 2 + psk_identity_hint_len;
1262 }
1263
Adam Langleye9ada862015-05-11 17:20:37 -07001264 if (alg_k & SSL_kDHE) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001265 dhp = cert->dh_tmp;
1266 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) {
1267 dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1268 }
1269 if (dhp == NULL) {
1270 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001271 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001272 goto f_err;
1273 }
1274
1275 if (s->s3->tmp.dh != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001276 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001277 goto err;
1278 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001279 dh = DHparams_dup(dhp);
1280 if (dh == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001281 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001282 goto err;
1283 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001284 s->s3->tmp.dh = dh;
Adam Langleye9ada862015-05-11 17:20:37 -07001285
1286 if (!DH_generate_key(dh)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001287 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langleye9ada862015-05-11 17:20:37 -07001288 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001289 }
1290
1291 r[0] = dh->p;
1292 r[1] = dh->g;
1293 r[2] = dh->pub_key;
Adam Langleye9ada862015-05-11 17:20:37 -07001294 } else if (alg_k & SSL_kECDHE) {
1295 /* Determine the curve to use. */
1296 int nid = NID_undef;
1297 if (cert->ecdh_nid != NID_undef) {
1298 nid = cert->ecdh_nid;
1299 } else if (cert->ecdh_tmp_cb != NULL) {
1300 /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result
1301 * to the caller. */
1302 EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024);
1303 if (template != NULL && EC_KEY_get0_group(template) != NULL) {
1304 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template));
Adam Langleyd9e397b2015-01-22 14:27:53 -08001305 }
Adam Langleye9ada862015-05-11 17:20:37 -07001306 } else {
1307 nid = tls1_get_shared_curve(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001308 }
Adam Langleye9ada862015-05-11 17:20:37 -07001309 if (nid == NID_undef) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001310 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001311 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001312 goto f_err;
1313 }
1314
1315 if (s->s3->tmp.ecdh != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001316 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001317 goto err;
1318 }
Adam Langleye9ada862015-05-11 17:20:37 -07001319 ecdh = EC_KEY_new_by_curve_name(nid);
1320 if (ecdh == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001321 goto err;
1322 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001323 s->s3->tmp.ecdh = ecdh;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001324
Adam Langleye9ada862015-05-11 17:20:37 -07001325 if (!EC_KEY_generate_key(ecdh)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001326 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001327 goto err;
1328 }
1329
1330 /* We only support ephemeral ECDH keys over named (not generic) curves. */
Adam Langleye9ada862015-05-11 17:20:37 -07001331 const EC_GROUP *group = EC_KEY_get0_group(ecdh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001332 if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001333 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001334 goto err;
1335 }
1336
1337 /* Encode the public key. First check the size of encoding and allocate
1338 * memory accordingly. */
1339 encodedlen =
1340 EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1341 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1342
1343 encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t));
1344 bn_ctx = BN_CTX_new();
1345 if (encodedPoint == NULL || bn_ctx == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001346 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001347 goto err;
1348 }
1349
1350 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1351 POINT_CONVERSION_UNCOMPRESSED,
1352 encodedPoint, encodedlen, bn_ctx);
1353
1354 if (encodedlen == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001355 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001356 goto err;
1357 }
1358
1359 BN_CTX_free(bn_ctx);
1360 bn_ctx = NULL;
1361
1362 /* We only support named (not generic) curves in ECDH ephemeral key
1363 * exchanges. In this situation, we need four additional bytes to encode
1364 * the entire ServerECDHParams structure. */
1365 n += 4 + encodedlen;
1366
1367 /* We'll generate the serverKeyExchange message explicitly so we can set
1368 * these to NULLs */
1369 r[0] = NULL;
1370 r[1] = NULL;
1371 r[2] = NULL;
1372 r[3] = NULL;
1373 } else if (!(alg_k & SSL_kPSK)) {
1374 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001375 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001376 goto f_err;
1377 }
1378
1379 for (i = 0; i < 4 && r[i] != NULL; i++) {
1380 nr[i] = BN_num_bytes(r[i]);
1381 n += 2 + nr[i];
1382 }
1383
Kenny Rootb8494592015-09-25 02:29:14 +00001384 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + max_sig_len)) {
1385 OPENSSL_PUT_ERROR(SSL, ERR_LIB_BUF);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001386 goto err;
1387 }
1388 d = p = ssl_handshake_start(s);
1389
1390 for (i = 0; i < 4 && r[i] != NULL; i++) {
1391 s2n(nr[i], p);
1392 BN_bn2bin(r[i], p);
1393 p += nr[i];
1394 }
1395
Adam Langleye9ada862015-05-11 17:20:37 -07001396 /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of
Adam Langleyd9e397b2015-01-22 14:27:53 -08001397 * them is used, the server key exchange record needs to have both the
1398 * psk_identity_hint and the ServerECDHParams. */
1399 if (alg_a & SSL_aPSK) {
1400 /* copy PSK identity hint (if provided) */
1401 s2n(psk_identity_hint_len, p);
1402 if (psk_identity_hint_len > 0) {
1403 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1404 p += psk_identity_hint_len;
1405 }
1406 }
1407
Adam Langleye9ada862015-05-11 17:20:37 -07001408 if (alg_k & SSL_kECDHE) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001409 /* We only support named (not generic) curves. In this situation, the
1410 * serverKeyExchange message has:
1411 * [1 byte CurveType], [2 byte CurveName]
1412 * [1 byte length of encoded point], followed by
1413 * the actual encoded point itself. */
1414 *(p++) = NAMED_CURVE_TYPE;
1415 *(p++) = (uint8_t)(curve_id >> 8);
1416 *(p++) = (uint8_t)(curve_id & 0xff);
1417 *(p++) = encodedlen;
1418 memcpy(p, encodedPoint, encodedlen);
1419 p += encodedlen;
1420 OPENSSL_free(encodedPoint);
1421 encodedPoint = NULL;
1422 }
1423
Kenny Rootb8494592015-09-25 02:29:14 +00001424 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1425 /* n is the length of the params, they start at d and p points to
Adam Langleyd9e397b2015-01-22 14:27:53 -08001426 * the space at the end. */
1427 const EVP_MD *md;
Kenny Rootb8494592015-09-25 02:29:14 +00001428 uint8_t digest[EVP_MAX_MD_SIZE];
1429 unsigned int digest_length;
1430
1431 const int pkey_type = ssl_private_key_type(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001432
1433 /* Determine signature algorithm. */
1434 if (SSL_USE_SIGALGS(s)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001435 md = tls1_choose_signing_digest(s);
1436 if (!tls12_get_sigandhash(s, p, md)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001437 /* Should never happen */
1438 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001439 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001440 goto f_err;
1441 }
1442 p += 2;
Kenny Rootb8494592015-09-25 02:29:14 +00001443 } else if (pkey_type == EVP_PKEY_RSA) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001444 md = EVP_md5_sha1();
1445 } else {
1446 md = EVP_sha1();
1447 }
1448
Kenny Rootb8494592015-09-25 02:29:14 +00001449 if (!EVP_DigestInit_ex(&md_ctx, md, NULL) ||
1450 !EVP_DigestUpdate(&md_ctx, s->s3->client_random, SSL3_RANDOM_SIZE) ||
1451 !EVP_DigestUpdate(&md_ctx, s->s3->server_random, SSL3_RANDOM_SIZE) ||
1452 !EVP_DigestUpdate(&md_ctx, d, n) ||
1453 !EVP_DigestFinal_ex(&md_ctx, digest, &digest_length)) {
1454 OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001455 goto err;
1456 }
1457
Kenny Rootb8494592015-09-25 02:29:14 +00001458 sign_result = ssl_private_key_sign(s, &p[2], &sig_len, max_sig_len,
1459 EVP_MD_CTX_md(&md_ctx), digest,
1460 digest_length);
1461 } else {
1462 /* This key exchange doesn't involve a signature. */
1463 sign_result = ssl_private_key_success;
1464 sig_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001465 }
Kenny Rootb8494592015-09-25 02:29:14 +00001466 } else {
1467 assert(s->state == SSL3_ST_SW_KEY_EXCH_B);
1468 /* Restore |p|. */
1469 p = ssl_handshake_start(s) + s->init_num - SSL_HM_HEADER_LENGTH(s);
1470 sign_result = ssl_private_key_sign_complete(s, &p[2], &sig_len,
1471 max_sig_len);
Adam Langley1e4884f2015-09-24 10:57:52 -07001472 }
1473
Kenny Rootb8494592015-09-25 02:29:14 +00001474 switch (sign_result) {
1475 case ssl_private_key_success:
1476 s->rwstate = SSL_NOTHING;
1477 break;
1478 case ssl_private_key_failure:
1479 s->rwstate = SSL_NOTHING;
1480 goto err;
1481 case ssl_private_key_retry:
1482 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
1483 /* Stash away |p|. */
1484 s->init_num = p - ssl_handshake_start(s) + SSL_HM_HEADER_LENGTH(s);
1485 s->state = SSL3_ST_SW_KEY_EXCH_B;
1486 goto err;
1487 }
1488
1489 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1490 s2n(sig_len, p);
1491 p += sig_len;
1492 }
1493 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE,
1494 p - ssl_handshake_start(s))) {
1495 goto err;
1496 }
1497 s->state = SSL3_ST_SW_KEY_EXCH_C;
1498
Adam Langleyd9e397b2015-01-22 14:27:53 -08001499 EVP_MD_CTX_cleanup(&md_ctx);
1500 return ssl_do_write(s);
1501
1502f_err:
1503 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1504err:
Adam Langleye9ada862015-05-11 17:20:37 -07001505 OPENSSL_free(encodedPoint);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001506 BN_CTX_free(bn_ctx);
1507 EVP_MD_CTX_cleanup(&md_ctx);
1508 return -1;
1509}
1510
1511int ssl3_send_certificate_request(SSL *s) {
1512 uint8_t *p, *d;
1513 size_t i;
1514 int j, nl, off, n;
1515 STACK_OF(X509_NAME) *sk = NULL;
1516 X509_NAME *name;
1517 BUF_MEM *buf;
1518
1519 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1520 buf = s->init_buf;
1521
1522 d = p = ssl_handshake_start(s);
1523
1524 /* get the list of acceptable cert types */
1525 p++;
1526 n = ssl3_get_req_cert_type(s, p);
1527 d[0] = n;
1528 p += n;
1529 n++;
1530
1531 if (SSL_USE_SIGALGS(s)) {
1532 const uint8_t *psigs;
1533 nl = tls12_get_psigalgs(s, &psigs);
1534 s2n(nl, p);
1535 memcpy(p, psigs, nl);
1536 p += nl;
1537 n += nl + 2;
1538 }
1539
1540 off = n;
1541 p += 2;
1542 n += 2;
1543
1544 sk = SSL_get_client_CA_list(s);
1545 nl = 0;
1546 if (sk != NULL) {
1547 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1548 name = sk_X509_NAME_value(sk, i);
1549 j = i2d_X509_NAME(name, NULL);
1550 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001551 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001552 goto err;
1553 }
1554 p = ssl_handshake_start(s) + n;
1555 s2n(j, p);
1556 i2d_X509_NAME(name, &p);
1557 n += 2 + j;
1558 nl += 2 + j;
1559 }
1560 }
1561
1562 /* else no CA names */
1563 p = ssl_handshake_start(s) + off;
1564 s2n(nl, p);
1565
Adam Langleye9ada862015-05-11 17:20:37 -07001566 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1567 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001568 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001569 s->state = SSL3_ST_SW_CERT_REQ_B;
1570 }
1571
1572 /* SSL3_ST_SW_CERT_REQ_B */
1573 return ssl_do_write(s);
1574
1575err:
1576 return -1;
1577}
1578
Kenny Roote99801b2015-11-06 15:31:15 -08001579static struct CRYPTO_STATIC_MUTEX g_d5_bug_lock = CRYPTO_STATIC_MUTEX_INIT;
1580static uint64_t g_d5_bug_use_count = 0;
1581
1582uint64_t OPENSSL_get_d5_bug_use_count(void) {
1583 CRYPTO_STATIC_MUTEX_lock_read(&g_d5_bug_lock);
1584 uint64_t ret = g_d5_bug_use_count;
1585 CRYPTO_STATIC_MUTEX_unlock(&g_d5_bug_lock);
1586 return ret;
1587}
1588
Adam Langleyd9e397b2015-01-22 14:27:53 -08001589int ssl3_get_client_key_exchange(SSL *s) {
Kenny Roote99801b2015-11-06 15:31:15 -08001590 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001591 CBS client_key_exchange;
Adam Langleye9ada862015-05-11 17:20:37 -07001592 uint32_t alg_k;
1593 uint32_t alg_a;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001594 uint8_t *premaster_secret = NULL;
1595 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001596 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001597 BIGNUM *pub = NULL;
1598 DH *dh_srvr;
1599
1600 EC_KEY *srvr_ecdh = NULL;
1601 EVP_PKEY *clnt_pub_pkey = NULL;
1602 EC_POINT *clnt_ecpoint = NULL;
1603 BN_CTX *bn_ctx = NULL;
1604 unsigned int psk_len = 0;
1605 uint8_t psk[PSK_MAX_PSK_LEN];
1606
Kenny Roote99801b2015-11-06 15:31:15 -08001607 if (s->state == SSL3_ST_SR_KEY_EXCH_A ||
1608 s->state == SSL3_ST_SR_KEY_EXCH_B) {
1609 int ok;
1610 const long n = s->method->ssl_get_message(
1611 s, SSL3_ST_SR_KEY_EXCH_A, SSL3_ST_SR_KEY_EXCH_B,
1612 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048 /* ??? */, ssl_hash_message, &ok);
1613 if (!ok) {
1614 return n;
1615 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001616 }
1617
Kenny Roote99801b2015-11-06 15:31:15 -08001618 CBS_init(&client_key_exchange, s->init_msg, s->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001619 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1620 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1621
1622 /* If using a PSK key exchange, prepare the pre-shared key. */
1623 if (alg_a & SSL_aPSK) {
1624 CBS psk_identity;
1625
1626 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1627 * then this is the only field in the message. */
1628 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1629 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001630 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001631 al = SSL_AD_DECODE_ERROR;
1632 goto f_err;
1633 }
1634
1635 if (s->psk_server_callback == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001636 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001637 al = SSL_AD_INTERNAL_ERROR;
1638 goto f_err;
1639 }
1640
1641 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1642 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001643 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001644 al = SSL_AD_ILLEGAL_PARAMETER;
1645 goto f_err;
1646 }
1647
1648 if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) {
1649 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001650 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001651 goto f_err;
1652 }
1653
1654 /* Look up the key for the identity. */
1655 psk_len =
1656 s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
1657 if (psk_len > PSK_MAX_PSK_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +00001658 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001659 al = SSL_AD_INTERNAL_ERROR;
1660 goto f_err;
1661 } else if (psk_len == 0) {
1662 /* PSK related to the given identity not found */
Kenny Rootb8494592015-09-25 02:29:14 +00001663 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001664 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1665 goto f_err;
1666 }
1667 }
1668
1669 /* Depending on the key exchange method, compute |premaster_secret| and
1670 * |premaster_secret_len|. */
1671 if (alg_k & SSL_kRSA) {
1672 CBS encrypted_premaster_secret;
1673 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1674 uint8_t good;
Kenny Roote99801b2015-11-06 15:31:15 -08001675 size_t decrypt_len, premaster_index, j;
1676 const size_t rsa_size = ssl_private_key_max_signature_len(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001677
1678 /* Allocate a buffer large enough for an RSA decryption. */
1679 decrypt_buf = OPENSSL_malloc(rsa_size);
1680 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001681 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001682 goto err;
1683 }
1684
Kenny Roote99801b2015-11-06 15:31:15 -08001685 enum ssl_private_key_result_t decrypt_result;
1686 if (s->state == SSL3_ST_SR_KEY_EXCH_B) {
1687 if (!ssl_has_private_key(s) || ssl_private_key_type(s) != EVP_PKEY_RSA) {
1688 al = SSL_AD_HANDSHAKE_FAILURE;
1689 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1690 goto f_err;
1691 }
1692 /* TLS and [incidentally] DTLS{0xFEFF} */
1693 if (s->version > SSL3_VERSION) {
1694 CBS copy = client_key_exchange;
1695 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1696 &encrypted_premaster_secret) ||
1697 CBS_len(&client_key_exchange) != 0) {
1698 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1699 al = SSL_AD_DECODE_ERROR;
1700 OPENSSL_PUT_ERROR(SSL,
1701 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1702 goto f_err;
1703 } else {
1704 CRYPTO_STATIC_MUTEX_lock_write(&g_d5_bug_lock);
1705 g_d5_bug_use_count++;
1706 CRYPTO_STATIC_MUTEX_unlock(&g_d5_bug_lock);
1707
1708 encrypted_premaster_secret = copy;
1709 }
1710 }
1711 } else {
1712 encrypted_premaster_secret = client_key_exchange;
1713 }
1714
1715 /* Reject overly short RSA keys because we want to be sure that the buffer
1716 * size makes it safe to iterate over the entire size of a premaster
1717 * secret (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger
1718 * due to RSA padding, but the bound is sufficient to be safe. */
1719 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) {
1720 al = SSL_AD_DECRYPT_ERROR;
1721 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1722 goto f_err;
1723 }
1724
1725 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1726 * timing-sensitive code below. */
1727 decrypt_result = ssl_private_key_decrypt(
1728 s, decrypt_buf, &decrypt_len, rsa_size,
1729 CBS_data(&encrypted_premaster_secret),
1730 CBS_len(&encrypted_premaster_secret));
1731 } else {
1732 assert(s->state == SSL3_ST_SR_KEY_EXCH_C);
1733 /* Complete async decrypt. */
1734 decrypt_result = ssl_private_key_decrypt_complete(
1735 s, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001736 }
Kenny Roote99801b2015-11-06 15:31:15 -08001737
1738 switch (decrypt_result) {
1739 case ssl_private_key_success:
1740 s->rwstate = SSL_NOTHING;
1741 break;
1742 case ssl_private_key_failure:
1743 s->rwstate = SSL_NOTHING;
1744 goto err;
1745 case ssl_private_key_retry:
1746 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
1747 s->state = SSL3_ST_SR_KEY_EXCH_C;
1748 goto err;
1749 }
1750
Adam Langleyd9e397b2015-01-22 14:27:53 -08001751 if (decrypt_len != rsa_size) {
1752 /* This should never happen, but do a check so we do not read
1753 * uninitialized memory. */
Kenny Rootb8494592015-09-25 02:29:14 +00001754 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001755 goto err;
1756 }
1757
1758 /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate.
1759 * |good| will be 0xff if the premaster is acceptable and zero otherwise.
1760 * */
1761 good =
1762 constant_time_eq_int_8(RSA_message_index_PKCS1_type_2(
1763 decrypt_buf, decrypt_len, &premaster_index),
1764 1);
1765 decrypt_len = decrypt_len - premaster_index;
1766
1767 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
1768 good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
1769
1770 /* Copy over the unpadded premaster. Whatever the value of
1771 * |decrypt_good_mask|, copy as if the premaster were the right length. It
1772 * is important the memory access pattern be constant. */
1773 premaster_secret =
1774 BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
1775 SSL_MAX_MASTER_KEY_LENGTH);
1776 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001777 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001778 goto err;
1779 }
1780 OPENSSL_free(decrypt_buf);
1781 decrypt_buf = NULL;
1782
1783 /* If the version in the decrypted pre-master secret is correct then
1784 * version_good will be 0xff, otherwise it'll be zero. The
1785 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1786 * (http://eprint.iacr.org/2003/052/) exploits the version number check as
1787 * a "bad version oracle". Thus version checks are done in constant time
1788 * and are treated like any other decryption error. */
1789 good &= constant_time_eq_8(premaster_secret[0],
1790 (unsigned)(s->client_version >> 8));
1791 good &= constant_time_eq_8(premaster_secret[1],
1792 (unsigned)(s->client_version & 0xff));
1793
Kenny Roote99801b2015-11-06 15:31:15 -08001794 /* We must not leak whether a decryption failure occurs because of
1795 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1796 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1797 * generates a random premaster secret for the case that the decrypt
1798 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1799 if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) {
1800 goto err;
1801 }
1802
Adam Langleyd9e397b2015-01-22 14:27:53 -08001803 /* Now copy rand_premaster_secret over premaster_secret using
1804 * decrypt_good_mask. */
1805 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
1806 premaster_secret[j] = constant_time_select_8(good, premaster_secret[j],
1807 rand_premaster_secret[j]);
1808 }
1809
1810 premaster_secret_len = sizeof(rand_premaster_secret);
Adam Langleye9ada862015-05-11 17:20:37 -07001811 } else if (alg_k & SSL_kDHE) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001812 CBS dh_Yc;
1813 int dh_len;
1814
1815 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
1816 CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001817 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001818 al = SSL_R_DECODE_ERROR;
1819 goto f_err;
1820 }
1821
1822 if (s->s3->tmp.dh == NULL) {
1823 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001824 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001825 goto f_err;
1826 }
1827 dh_srvr = s->s3->tmp.dh;
1828
1829 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
1830 if (pub == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001831 OPENSSL_PUT_ERROR(SSL, SSL_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001832 goto err;
1833 }
1834
1835 /* Allocate a buffer for the premaster secret. */
1836 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
1837 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001838 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleye9ada862015-05-11 17:20:37 -07001839 BN_clear_free(pub);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001840 goto err;
1841 }
1842
1843 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
1844 if (dh_len <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001845 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001846 BN_clear_free(pub);
1847 goto err;
1848 }
1849
1850 DH_free(s->s3->tmp.dh);
1851 s->s3->tmp.dh = NULL;
1852 BN_clear_free(pub);
1853 pub = NULL;
1854
1855 premaster_secret_len = dh_len;
Adam Langleye9ada862015-05-11 17:20:37 -07001856 } else if (alg_k & SSL_kECDHE) {
Kenny Roote99801b2015-11-06 15:31:15 -08001857 int ecdh_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001858 const EC_KEY *tkey;
1859 const EC_GROUP *group;
1860 const BIGNUM *priv_key;
1861 CBS ecdh_Yc;
1862
1863 /* initialize structures for server's ECDH key pair */
1864 srvr_ecdh = EC_KEY_new();
1865 if (srvr_ecdh == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001866 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001867 goto err;
1868 }
1869
1870 /* Use the ephermeral values we saved when generating the ServerKeyExchange
1871 * msg. */
1872 tkey = s->s3->tmp.ecdh;
1873
1874 group = EC_KEY_get0_group(tkey);
1875 priv_key = EC_KEY_get0_private_key(tkey);
1876
1877 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1878 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001879 OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001880 goto err;
1881 }
1882
1883 /* Let's get client's public key */
1884 clnt_ecpoint = EC_POINT_new(group);
1885 if (clnt_ecpoint == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001886 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001887 goto err;
1888 }
1889
1890 /* Get client's public key from encoded point in the ClientKeyExchange
1891 * message. */
1892 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
1893 CBS_len(&client_key_exchange) != 0) {
1894 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001895 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001896 goto f_err;
1897 }
1898
1899 bn_ctx = BN_CTX_new();
1900 if (bn_ctx == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001901 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001902 goto err;
1903 }
1904
1905 if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc),
1906 CBS_len(&ecdh_Yc), bn_ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001907 OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001908 goto err;
1909 }
1910
1911 /* Allocate a buffer for both the secret and the PSK. */
Kenny Roote99801b2015-11-06 15:31:15 -08001912 unsigned field_size = EC_GROUP_get_degree(group);
1913 if (field_size == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001914 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001915 goto err;
1916 }
1917
1918 ecdh_len = (field_size + 7) / 8;
1919 premaster_secret = OPENSSL_malloc(ecdh_len);
1920 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001921 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001922 goto err;
1923 }
1924
1925 /* Compute the shared pre-master secret */
1926 ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint,
1927 srvr_ecdh, NULL);
1928 if (ecdh_len <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001929 OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001930 goto err;
1931 }
1932
1933 EVP_PKEY_free(clnt_pub_pkey);
1934 clnt_pub_pkey = NULL;
1935 EC_POINT_free(clnt_ecpoint);
1936 clnt_ecpoint = NULL;
1937 EC_KEY_free(srvr_ecdh);
1938 srvr_ecdh = NULL;
1939 BN_CTX_free(bn_ctx);
1940 bn_ctx = NULL;
1941 EC_KEY_free(s->s3->tmp.ecdh);
1942 s->s3->tmp.ecdh = NULL;
1943
1944 premaster_secret_len = ecdh_len;
1945 } else if (alg_k & SSL_kPSK) {
1946 /* For plain PSK, other_secret is a block of 0s with the same length as the
1947 * pre-shared key. */
1948 premaster_secret_len = psk_len;
1949 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1950 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001951 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001952 goto err;
1953 }
1954 memset(premaster_secret, 0, premaster_secret_len);
1955 } else {
1956 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001957 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001958 goto f_err;
1959 }
1960
1961 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1962 * pre-shared key. */
1963 if (alg_a & SSL_aPSK) {
1964 CBB new_premaster, child;
1965 uint8_t *new_data;
1966 size_t new_len;
1967
Kenny Rootb8494592015-09-25 02:29:14 +00001968 CBB_zero(&new_premaster);
1969 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1970 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001971 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1972 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1973 !CBB_add_bytes(&child, psk, psk_len) ||
1974 !CBB_finish(&new_premaster, &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001975 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001976 CBB_cleanup(&new_premaster);
1977 goto err;
1978 }
1979
1980 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1981 OPENSSL_free(premaster_secret);
1982 premaster_secret = new_data;
1983 premaster_secret_len = new_len;
1984 }
1985
1986 /* Compute the master secret */
1987 s->session->master_key_length = s->enc_method->generate_master_secret(
1988 s, s->session->master_key, premaster_secret, premaster_secret_len);
1989 if (s->session->master_key_length == 0) {
1990 goto err;
1991 }
1992 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1993
1994 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1995 OPENSSL_free(premaster_secret);
1996 return 1;
1997
1998f_err:
1999 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2000err:
2001 if (premaster_secret) {
2002 if (premaster_secret_len) {
2003 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2004 }
2005 OPENSSL_free(premaster_secret);
2006 }
Adam Langleye9ada862015-05-11 17:20:37 -07002007 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002008 EVP_PKEY_free(clnt_pub_pkey);
2009 EC_POINT_free(clnt_ecpoint);
Adam Langleye9ada862015-05-11 17:20:37 -07002010 EC_KEY_free(srvr_ecdh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002011 BN_CTX_free(bn_ctx);
2012
2013 return -1;
2014}
2015
2016int ssl3_get_cert_verify(SSL *s) {
2017 int al, ok, ret = 0;
2018 long n;
2019 CBS certificate_verify, signature;
2020 X509 *peer = s->session->peer;
2021 EVP_PKEY *pkey = NULL;
2022 const EVP_MD *md = NULL;
2023 uint8_t digest[EVP_MAX_MD_SIZE];
2024 size_t digest_length;
2025 EVP_PKEY_CTX *pctx = NULL;
2026
2027 /* Only RSA and ECDSA client certificates are supported, so a
2028 * CertificateVerify is required if and only if there's a client certificate.
2029 * */
2030 if (peer == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00002031 ssl3_free_handshake_buffer(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002032 return 1;
2033 }
2034
2035 n = s->method->ssl_get_message(
2036 s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
2037 SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
Adam Langleye9ada862015-05-11 17:20:37 -07002038 ssl_dont_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002039
2040 if (!ok) {
2041 return n;
2042 }
2043
2044 /* Filter out unsupported certificate types. */
2045 pkey = X509_get_pubkey(peer);
Adam Langleye9ada862015-05-11 17:20:37 -07002046 if (pkey == NULL) {
2047 goto err;
2048 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002049 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
2050 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
2051 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00002052 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002053 goto f_err;
2054 }
2055
2056 CBS_init(&certificate_verify, s->init_msg, n);
2057
2058 /* Determine the digest type if needbe. */
2059 if (SSL_USE_SIGALGS(s) &&
2060 !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) {
2061 goto f_err;
2062 }
2063
2064 /* Compute the digest. */
Kenny Rootb8494592015-09-25 02:29:14 +00002065 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey->type)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002066 goto err;
2067 }
2068
2069 /* The handshake buffer is no longer necessary, and we may hash the current
2070 * message.*/
Kenny Rootb8494592015-09-25 02:29:14 +00002071 ssl3_free_handshake_buffer(s);
Adam Langleye9ada862015-05-11 17:20:37 -07002072 if (!ssl3_hash_current_message(s)) {
2073 goto err;
2074 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002075
2076 /* Parse and verify the signature. */
2077 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2078 CBS_len(&certificate_verify) != 0) {
2079 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00002080 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002081 goto f_err;
2082 }
2083
2084 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2085 if (pctx == NULL) {
2086 goto err;
2087 }
2088 if (!EVP_PKEY_verify_init(pctx) ||
2089 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2090 !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
2091 digest_length)) {
2092 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00002093 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002094 goto f_err;
2095 }
2096
2097 ret = 1;
2098
2099 if (0) {
2100 f_err:
2101 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2102 }
2103
2104err:
2105 EVP_PKEY_CTX_free(pctx);
2106 EVP_PKEY_free(pkey);
2107
2108 return ret;
2109}
2110
2111int ssl3_get_client_certificate(SSL *s) {
2112 int i, ok, al, ret = -1;
2113 X509 *x = NULL;
2114 unsigned long n;
2115 STACK_OF(X509) *sk = NULL;
2116 SHA256_CTX sha256;
2117 CBS certificate_msg, certificate_list;
2118 int is_first_certificate = 1;
2119
2120 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1,
Adam Langleye9ada862015-05-11 17:20:37 -07002121 (long)s->max_cert_list, ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002122
2123 if (!ok) {
2124 return n;
2125 }
2126
2127 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2128 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2129 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002130 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002131 al = SSL_AD_HANDSHAKE_FAILURE;
2132 goto f_err;
2133 }
2134
2135 /* If tls asked for a client cert, the client must return a 0 list */
2136 if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) {
Kenny Rootb8494592015-09-25 02:29:14 +00002137 OPENSSL_PUT_ERROR(SSL,
Adam Langleyd9e397b2015-01-22 14:27:53 -08002138 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2139 al = SSL_AD_UNEXPECTED_MESSAGE;
2140 goto f_err;
2141 }
2142 s->s3->tmp.reuse_message = 1;
2143
2144 return 1;
2145 }
2146
2147 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2148 al = SSL_AD_UNEXPECTED_MESSAGE;
Kenny Rootb8494592015-09-25 02:29:14 +00002149 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002150 goto f_err;
2151 }
2152
2153 CBS_init(&certificate_msg, s->init_msg, n);
2154
2155 sk = sk_X509_new_null();
2156 if (sk == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00002157 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002158 goto err;
2159 }
2160
2161 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2162 CBS_len(&certificate_msg) != 0) {
2163 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00002164 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002165 goto f_err;
2166 }
2167
2168 while (CBS_len(&certificate_list) > 0) {
2169 CBS certificate;
2170 const uint8_t *data;
2171
2172 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
2173 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00002174 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002175 goto f_err;
2176 }
2177
2178 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) {
2179 /* If this is the first certificate, and we don't want to keep peer
2180 * certificates in memory, then we hash it right away. */
2181 SHA256_Init(&sha256);
2182 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
2183 SHA256_Final(s->session->peer_sha256, &sha256);
2184 s->session->peer_sha256_valid = 1;
2185 }
2186 is_first_certificate = 0;
2187
2188 data = CBS_data(&certificate);
2189 x = d2i_X509(NULL, &data, CBS_len(&certificate));
2190 if (x == NULL) {
2191 al = SSL_AD_BAD_CERTIFICATE;
Kenny Rootb8494592015-09-25 02:29:14 +00002192 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002193 goto f_err;
2194 }
2195 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
2196 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00002197 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002198 goto f_err;
2199 }
2200 if (!sk_X509_push(sk, x)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002201 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002202 goto err;
2203 }
2204 x = NULL;
2205 }
2206
2207 if (sk_X509_num(sk) <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00002208 /* No client certificate so the handshake buffer may be discarded. */
2209 ssl3_free_handshake_buffer(s);
2210
Adam Langleyd9e397b2015-01-22 14:27:53 -08002211 /* TLS does not mind 0 certs returned */
2212 if (s->version == SSL3_VERSION) {
2213 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00002214 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002215 goto f_err;
Kenny Rootb8494592015-09-25 02:29:14 +00002216 } else if ((s->verify_mode & SSL_VERIFY_PEER) &&
Adam Langleyd9e397b2015-01-22 14:27:53 -08002217 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002218 /* Fail for TLS only if we required a certificate */
2219 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002220 al = SSL_AD_HANDSHAKE_FAILURE;
2221 goto f_err;
2222 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002223 } else {
2224 i = ssl_verify_cert_chain(s, sk);
2225 if (i <= 0) {
2226 al = ssl_verify_alarm_type(s->verify_result);
Kenny Rootb8494592015-09-25 02:29:14 +00002227 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002228 goto f_err;
2229 }
2230 }
2231
Adam Langleye9ada862015-05-11 17:20:37 -07002232 X509_free(s->session->peer);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002233 s->session->peer = sk_X509_shift(sk);
2234 s->session->verify_result = s->verify_result;
2235
Kenny Rootb8494592015-09-25 02:29:14 +00002236 sk_X509_pop_free(s->session->cert_chain, X509_free);
2237 s->session->cert_chain = sk;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002238 /* Inconsistency alert: cert_chain does *not* include the peer's own
2239 * certificate, while we do include it in s3_clnt.c */
2240
2241 sk = NULL;
2242
2243 ret = 1;
2244
2245 if (0) {
2246 f_err:
2247 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2248 }
2249
2250err:
Adam Langleye9ada862015-05-11 17:20:37 -07002251 X509_free(x);
2252 sk_X509_pop_free(sk, X509_free);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002253 return ret;
2254}
2255
2256int ssl3_send_server_certificate(SSL *s) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002257 if (s->state == SSL3_ST_SW_CERT_A) {
Kenny Rootb8494592015-09-25 02:29:14 +00002258 if (!ssl3_output_cert_chain(s)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002259 return 0;
2260 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002261 s->state = SSL3_ST_SW_CERT_B;
2262 }
2263
2264 /* SSL3_ST_SW_CERT_B */
2265 return ssl_do_write(s);
2266}
2267
2268/* send a new session ticket (not necessarily for a new session) */
2269int ssl3_send_new_session_ticket(SSL *s) {
Adam Langleye9ada862015-05-11 17:20:37 -07002270 int ret = -1;
2271 uint8_t *session = NULL;
2272 size_t session_len;
2273 EVP_CIPHER_CTX ctx;
2274 HMAC_CTX hctx;
2275
2276 EVP_CIPHER_CTX_init(&ctx);
2277 HMAC_CTX_init(&hctx);
2278
Adam Langleyd9e397b2015-01-22 14:27:53 -08002279 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002280 uint8_t *p, *macstart;
2281 int len;
2282 unsigned int hlen;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002283 SSL_CTX *tctx = s->initial_ctx;
2284 uint8_t iv[EVP_MAX_IV_LENGTH];
2285 uint8_t key_name[16];
2286 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2287 * one block of encryption overhead + HMAC. */
2288 const size_t max_ticket_overhead =
2289 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
2290
2291 /* Serialize the SSL_SESSION to be encoded into the ticket. */
2292 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002293 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002294 }
2295
2296 /* If the session is too long, emit a dummy value rather than abort the
2297 * connection. */
2298 if (session_len > 0xFFFF - max_ticket_overhead) {
2299 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2300 const size_t placeholder_len = strlen(kTicketPlaceholder);
2301
2302 OPENSSL_free(session);
Adam Langleye9ada862015-05-11 17:20:37 -07002303 session = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002304
2305 p = ssl_handshake_start(s);
2306 /* Emit ticket_lifetime_hint. */
2307 l2n(0, p);
2308 /* Emit ticket. */
2309 s2n(placeholder_len, p);
2310 memcpy(p, kTicketPlaceholder, placeholder_len);
2311 p += placeholder_len;
2312
2313 len = p - ssl_handshake_start(s);
Adam Langleye9ada862015-05-11 17:20:37 -07002314 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
2315 goto err;
2316 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002317 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2318 return ssl_do_write(s);
2319 }
2320
2321 /* Grow buffer if need be: the length calculation is as follows:
2322 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2323 * max_ticket_overhead + * session_length */
2324 if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 +
2325 max_ticket_overhead + session_len)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002326 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002327 }
2328 p = ssl_handshake_start(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002329 /* Initialize HMAC and cipher contexts. If callback present it does all the
2330 * work otherwise use generated values from parent ctx. */
2331 if (tctx->tlsext_ticket_key_cb) {
Adam Langleye9ada862015-05-11 17:20:37 -07002332 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx,
2333 1 /* encrypt */) < 0) {
2334 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002335 }
2336 } else {
2337 if (!RAND_bytes(iv, 16) ||
2338 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2339 tctx->tlsext_tick_aes_key, iv) ||
2340 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2341 NULL)) {
Adam Langleye9ada862015-05-11 17:20:37 -07002342 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002343 }
2344 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2345 }
2346
2347 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2348 * resumed session (for simplicity), and guess that tickets for new
2349 * sessions will live as long as their sessions. */
2350 l2n(s->hit ? 0 : s->session->timeout, p);
2351
2352 /* Skip ticket length for now */
2353 p += 2;
2354 /* Output key name */
2355 macstart = p;
2356 memcpy(p, key_name, 16);
2357 p += 16;
2358 /* output IV */
2359 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2360 p += EVP_CIPHER_CTX_iv_length(&ctx);
2361 /* Encrypt session data */
Adam Langleye9ada862015-05-11 17:20:37 -07002362 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2363 goto err;
2364 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002365 p += len;
Adam Langleye9ada862015-05-11 17:20:37 -07002366 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2367 goto err;
2368 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002369 p += len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002370
Adam Langleye9ada862015-05-11 17:20:37 -07002371 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2372 !HMAC_Final(&hctx, p, &hlen)) {
2373 goto err;
2374 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002375
2376 p += hlen;
2377 /* Now write out lengths: p points to end of data written */
2378 /* Total length */
2379 len = p - ssl_handshake_start(s);
2380 /* Skip ticket lifetime hint */
2381 p = ssl_handshake_start(s) + 4;
2382 s2n(len - 6, p);
Adam Langleye9ada862015-05-11 17:20:37 -07002383 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) {
2384 goto err;
2385 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002386 s->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002387 }
2388
2389 /* SSL3_ST_SW_SESSION_TICKET_B */
Adam Langleye9ada862015-05-11 17:20:37 -07002390 ret = ssl_do_write(s);
2391
2392err:
2393 OPENSSL_free(session);
2394 EVP_CIPHER_CTX_cleanup(&ctx);
2395 HMAC_CTX_cleanup(&hctx);
2396 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002397}
2398
2399/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2400 * sets the next_proto member in s if found */
2401int ssl3_get_next_proto(SSL *s) {
2402 int ok;
2403 long n;
2404 CBS next_protocol, selected_protocol, padding;
2405
2406 /* Clients cannot send a NextProtocol message if we didn't see the extension
2407 * in their ClientHello */
2408 if (!s->s3->next_proto_neg_seen) {
Kenny Rootb8494592015-09-25 02:29:14 +00002409 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002410 return -1;
2411 }
2412
2413 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2414 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2415 514, /* See the payload format below */
Adam Langleye9ada862015-05-11 17:20:37 -07002416 ssl_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002417
2418 if (!ok) {
2419 return n;
2420 }
2421
2422 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2423 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2424 * ssl3_get_finished).
2425 *
2426 * TODO(davidben): Is this check now redundant with
2427 * SSL3_FLAGS_EXPECT_CCS? */
2428 if (!s->s3->change_cipher_spec) {
Kenny Rootb8494592015-09-25 02:29:14 +00002429 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002430 return -1;
2431 }
2432
2433 CBS_init(&next_protocol, s->init_msg, n);
2434
2435 /* The payload looks like:
2436 * uint8 proto_len;
2437 * uint8 proto[proto_len];
2438 * uint8 padding_len;
2439 * uint8 padding[padding_len]; */
2440 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2441 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2442 CBS_len(&next_protocol) != 0 ||
2443 !CBS_stow(&selected_protocol, &s->next_proto_negotiated,
2444 &s->next_proto_negotiated_len)) {
2445 return 0;
2446 }
2447
2448 return 1;
2449}
2450
2451/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
2452int ssl3_get_channel_id(SSL *s) {
2453 int ret = -1, ok;
2454 long n;
Kenny Rootb8494592015-09-25 02:29:14 +00002455 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
2456 size_t channel_id_hash_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002457 const uint8_t *p;
Kenny Rootb8494592015-09-25 02:29:14 +00002458 uint16_t extension_type;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002459 EC_GROUP *p256 = NULL;
2460 EC_KEY *key = NULL;
2461 EC_POINT *point = NULL;
2462 ECDSA_SIG sig;
2463 BIGNUM x, y;
2464 CBS encrypted_extensions, extension;
2465
2466 n = s->method->ssl_get_message(
2467 s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2468 SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
Adam Langleye9ada862015-05-11 17:20:37 -07002469 ssl_dont_hash_message, &ok);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002470
2471 if (!ok) {
2472 return n;
2473 }
2474
2475 /* Before incorporating the EncryptedExtensions message to the handshake
2476 * hash, compute the hash that should have been signed. */
Kenny Rootb8494592015-09-25 02:29:14 +00002477 if (!tls1_channel_id_hash(s, channel_id_hash, &channel_id_hash_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002478 return -1;
2479 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002480 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
2481
Adam Langleye9ada862015-05-11 17:20:37 -07002482 if (!ssl3_hash_current_message(s)) {
2483 return -1;
2484 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002485
2486 /* s->state doesn't reflect whether ChangeCipherSpec has been received in
2487 * this handshake, but s->s3->change_cipher_spec does (will be reset by
2488 * ssl3_get_finished).
2489 *
2490 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
2491 if (!s->s3->change_cipher_spec) {
Kenny Rootb8494592015-09-25 02:29:14 +00002492 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002493 return -1;
2494 }
2495
2496 CBS_init(&encrypted_extensions, s->init_msg, n);
2497
2498 /* EncryptedExtensions could include multiple extensions, but the only
2499 * extension that could be negotiated is ChannelID, so there can only be one
2500 * entry.
2501 *
2502 * The payload looks like:
2503 * uint16 extension_type
2504 * uint16 extension_len;
2505 * uint8 x[32];
2506 * uint8 y[32];
2507 * uint8 r[32];
2508 * uint8 s[32]; */
Adam Langleyd9e397b2015-01-22 14:27:53 -08002509
2510 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2511 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2512 CBS_len(&encrypted_extensions) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00002513 extension_type != TLSEXT_TYPE_channel_id ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08002514 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
Kenny Rootb8494592015-09-25 02:29:14 +00002515 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002516 return -1;
2517 }
2518
2519 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2520 if (!p256) {
Kenny Rootb8494592015-09-25 02:29:14 +00002521 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002522 return -1;
2523 }
2524
2525 BN_init(&x);
2526 BN_init(&y);
2527 sig.r = BN_new();
2528 sig.s = BN_new();
Adam Langleye9ada862015-05-11 17:20:37 -07002529 if (sig.r == NULL || sig.s == NULL) {
2530 goto err;
2531 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002532
2533 p = CBS_data(&extension);
2534 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2535 BN_bin2bn(p + 32, 32, &y) == NULL ||
2536 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2537 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2538 goto err;
2539 }
2540
2541 point = EC_POINT_new(p256);
2542 if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2543 goto err;
2544 }
2545
2546 key = EC_KEY_new();
2547 if (!key || !EC_KEY_set_group(key, p256) ||
2548 !EC_KEY_set_public_key(key, point)) {
2549 goto err;
2550 }
2551
2552 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2553 * were called. */
2554 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002555 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002556 s->s3->tlsext_channel_id_valid = 0;
2557 goto err;
2558 }
2559
2560 memcpy(s->s3->tlsext_channel_id, p, 64);
2561 ret = 1;
2562
2563err:
2564 BN_free(&x);
2565 BN_free(&y);
2566 BN_free(sig.r);
2567 BN_free(sig.s);
Adam Langleye9ada862015-05-11 17:20:37 -07002568 EC_KEY_free(key);
2569 EC_POINT_free(point);
2570 EC_GROUP_free(p256);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002571 return ret;
2572}