blob: b5c50b493729a3eed87dda511da3ac6a32de893e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
Adam Langley95c29f32014-06-20 12:00:00 -0700149#define NETSCAPE_HANG_BUG
150
151#include <stdio.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
158#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
165#include <openssl/obj.h>
166#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700167#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168#include <openssl/x509.h>
169
170#include "ssl_locl.h"
171#include "../crypto/dh/internal.h"
172
173static const SSL_METHOD *ssl3_get_server_method(int ver);
174
175static const SSL_METHOD *ssl3_get_server_method(int ver)
176 {
177 if (ver == SSL3_VERSION)
178 return(SSLv3_server_method());
179 else
180 return(NULL);
181 }
182
183IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
184 ssl3_accept,
185 ssl_undefined_function,
186 ssl3_get_server_method)
187
188int ssl3_accept(SSL *s)
189 {
190 BUF_MEM *buf;
Adam Langleyc26c8022014-06-20 12:00:00 -0700191 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700192 void (*cb)(const SSL *ssl,int type,int val)=NULL;
193 int ret= -1;
194 int new_state,state,skip=0;
195
196 ERR_clear_error();
197 ERR_clear_system_error();
198
199 if (s->info_callback != NULL)
200 cb=s->info_callback;
201 else if (s->ctx->info_callback != NULL)
202 cb=s->ctx->info_callback;
203
204 /* init things to blank */
205 s->in_handshake++;
206 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
207
208 if (s->cert == NULL)
209 {
210 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
211 return(-1);
212 }
213
Adam Langley95c29f32014-06-20 12:00:00 -0700214 for (;;)
215 {
216 state=s->state;
217
218 switch (s->state)
219 {
220 case SSL_ST_RENEGOTIATE:
221 s->renegotiate=1;
222 /* s->state=SSL_ST_ACCEPT; */
223
224 case SSL_ST_BEFORE:
225 case SSL_ST_ACCEPT:
226 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
227 case SSL_ST_OK|SSL_ST_ACCEPT:
228
229 s->server=1;
230 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
231
232 if ((s->version>>8) != 3)
233 {
234 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
235 return -1;
236 }
237 s->type=SSL_ST_ACCEPT;
238
239 if (s->init_buf == NULL)
240 {
241 if ((buf=BUF_MEM_new()) == NULL)
242 {
243 ret= -1;
244 goto end;
245 }
246 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
247 {
248 ret= -1;
249 goto end;
250 }
251 s->init_buf=buf;
252 }
253
254 if (!ssl3_setup_buffers(s))
255 {
256 ret= -1;
257 goto end;
258 }
259
260 s->init_num=0;
Adam Langley95c29f32014-06-20 12:00:00 -0700261
262 if (s->state != SSL_ST_RENEGOTIATE)
263 {
264 /* Ok, we now need to push on a buffering BIO so that
265 * the output is sent in a way that TCP likes :-)
266 */
267 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
268
269 ssl3_init_finished_mac(s);
270 s->state=SSL3_ST_SR_CLNT_HELLO_A;
271 s->ctx->stats.sess_accept++;
272 }
273 else if (!s->s3->send_connection_binding &&
274 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
275 {
276 /* Server attempting to renegotiate with
277 * client that doesn't support secure
278 * renegotiation.
279 */
280 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
281 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
282 ret = -1;
283 goto end;
284 }
285 else
286 {
287 /* s->state == SSL_ST_RENEGOTIATE,
288 * we will just send a HelloRequest */
289 s->ctx->stats.sess_accept_renegotiate++;
290 s->state=SSL3_ST_SW_HELLO_REQ_A;
291 }
292 break;
293
294 case SSL3_ST_SW_HELLO_REQ_A:
295 case SSL3_ST_SW_HELLO_REQ_B:
296
297 s->shutdown=0;
298 ret=ssl3_send_hello_request(s);
299 if (ret <= 0) goto end;
300 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
301 s->state=SSL3_ST_SW_FLUSH;
302 s->init_num=0;
303
304 ssl3_init_finished_mac(s);
305 break;
306
307 case SSL3_ST_SW_HELLO_REQ_C:
308 s->state=SSL_ST_OK;
309 break;
310
311 case SSL3_ST_SR_CLNT_HELLO_A:
312 case SSL3_ST_SR_CLNT_HELLO_B:
313 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langleyb2ce0582014-06-20 12:00:00 -0700314 case SSL3_ST_SR_CLNT_HELLO_D:
Adam Langley95c29f32014-06-20 12:00:00 -0700315 s->shutdown=0;
316 ret=ssl3_get_client_hello(s);
Adam Langleyb2ce0582014-06-20 12:00:00 -0700317 if (ret == PENDING_SESSION) {
Adam Langleyb2ce0582014-06-20 12:00:00 -0700318 s->rwstate = SSL_PENDING_SESSION;
319 goto end;
320 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700321 if (ret == CERTIFICATE_SELECTION_PENDING)
322 {
323 s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
324 goto end;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326 if (ret <= 0) goto end;
327 s->renegotiate = 2;
328 s->state=SSL3_ST_SW_SRVR_HELLO_A;
329 s->init_num=0;
330 break;
331
332 case SSL3_ST_SW_SRVR_HELLO_A:
333 case SSL3_ST_SW_SRVR_HELLO_B:
334 ret=ssl3_send_server_hello(s);
335 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700336 if (s->hit)
337 {
338 if (s->tlsext_ticket_expected)
339 s->state=SSL3_ST_SW_SESSION_TICKET_A;
340 else
341 s->state=SSL3_ST_SW_CHANGE_A;
342 }
Adam Langley95c29f32014-06-20 12:00:00 -0700343 else
David Benjamin2b0aeec2014-07-01 00:39:02 -0400344 s->state = SSL3_ST_SW_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700345 s->init_num = 0;
346 break;
347
Adam Langley95c29f32014-06-20 12:00:00 -0700348 case SSL3_ST_SW_CERT_A:
349 case SSL3_ST_SW_CERT_B:
David Benjamine8f3d662014-07-12 01:10:19 -0400350 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700351 {
352 ret=ssl3_send_server_certificate(s);
353 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700354 if (s->tlsext_status_expected)
355 s->state=SSL3_ST_SW_CERT_STATUS_A;
356 else
357 s->state=SSL3_ST_SW_KEY_EXCH_A;
358 }
359 else
360 {
361 skip = 1;
362 s->state=SSL3_ST_SW_KEY_EXCH_A;
363 }
Adam Langley95c29f32014-06-20 12:00:00 -0700364 s->init_num=0;
365 break;
366
367 case SSL3_ST_SW_KEY_EXCH_A:
368 case SSL3_ST_SW_KEY_EXCH_B:
Adam Langleyc26c8022014-06-20 12:00:00 -0700369 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700370
David Benjaminb9cc33a2014-07-15 00:09:48 -0400371 /* Send a ServerKeyExchange message if:
372 * - The key exchange is ephemeral or anonymous
373 * Diffie-Hellman.
374 * - There is a PSK identity hint.
Adam Langley95c29f32014-06-20 12:00:00 -0700375 *
David Benjaminb9cc33a2014-07-15 00:09:48 -0400376 * TODO(davidben): This logic is currently duplicated
377 * in d1_srvr.c. Fix this. In the meantime, keep them
378 * in sync.
Adam Langley95c29f32014-06-20 12:00:00 -0700379 */
David Benjaminb9cc33a2014-07-15 00:09:48 -0400380 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
David Benjamin77a942b2014-07-15 01:22:50 -0400381 ((alg_a & SSL_aPSK) && s->session->psk_identity_hint))
Adam Langley95c29f32014-06-20 12:00:00 -0700382 {
383 ret=ssl3_send_server_key_exchange(s);
384 if (ret <= 0) goto end;
385 }
386 else
387 skip=1;
388
389 s->state=SSL3_ST_SW_CERT_REQ_A;
390 s->init_num=0;
391 break;
392
393 case SSL3_ST_SW_CERT_REQ_A:
394 case SSL3_ST_SW_CERT_REQ_B:
395 if (/* don't request cert unless asked for it: */
396 !(s->verify_mode & SSL_VERIFY_PEER) ||
Adam Langley509e5ed2014-06-20 12:00:00 -0700397 /* Don't request a certificate if an obc was presented */
398 ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
399 s->s3->tlsext_channel_id_valid) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700400 /* if SSL_VERIFY_CLIENT_ONCE is set,
401 * don't request cert during re-negotiation: */
402 ((s->session->peer != NULL) &&
403 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
404 /* never request cert in anonymous ciphersuites
405 * (see section "Certificate request" in SSL 3 drafts
406 * and in RFC 2246): */
407 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
408 /* ... except when the application insists on verification
409 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
410 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700411 /* With normal PSK Certificates and
412 * Certificate Requests are omitted */
David Benjamind26aea62014-07-12 00:13:56 -0400413 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -0700414 {
415 /* no cert request */
416 skip=1;
417 s->s3->tmp.cert_request=0;
418 s->state=SSL3_ST_SW_SRVR_DONE_A;
419 if (s->s3->handshake_buffer)
420 if (!ssl3_digest_cached_records(s))
421 return -1;
422 }
423 else
424 {
425 s->s3->tmp.cert_request=1;
426 ret=ssl3_send_certificate_request(s);
427 if (ret <= 0) goto end;
428#ifndef NETSCAPE_HANG_BUG
429 s->state=SSL3_ST_SW_SRVR_DONE_A;
430#else
431 s->state=SSL3_ST_SW_FLUSH;
432 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
433#endif
434 s->init_num=0;
435 }
436 break;
437
438 case SSL3_ST_SW_SRVR_DONE_A:
439 case SSL3_ST_SW_SRVR_DONE_B:
440 ret=ssl3_send_server_done(s);
441 if (ret <= 0) goto end;
442 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
443 s->state=SSL3_ST_SW_FLUSH;
444 s->init_num=0;
445 break;
446
447 case SSL3_ST_SW_FLUSH:
448
449 /* This code originally checked to see if
450 * any data was pending using BIO_CTRL_INFO
451 * and then flushed. This caused problems
452 * as documented in PR#1939. The proposed
453 * fix doesn't completely resolve this issue
454 * as buggy implementations of BIO_CTRL_PENDING
455 * still exist. So instead we just flush
456 * unconditionally.
457 */
458
459 s->rwstate=SSL_WRITING;
460 if (BIO_flush(s->wbio) <= 0)
461 {
462 ret= -1;
463 goto end;
464 }
465 s->rwstate=SSL_NOTHING;
466
467 s->state=s->s3->tmp.next_state;
468 break;
469
470 case SSL3_ST_SR_CERT_A:
471 case SSL3_ST_SR_CERT_B:
David Benjamin92909a62014-08-20 11:40:03 -0400472 if (s->s3->tmp.cert_request)
473 {
474 ret=ssl3_get_client_certificate(s);
475 if (ret <= 0) goto end;
476 }
477 s->init_num=0;
478 s->state=SSL3_ST_SR_KEY_EXCH_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700479 break;
480
481 case SSL3_ST_SR_KEY_EXCH_A:
482 case SSL3_ST_SR_KEY_EXCH_B:
483 ret=ssl3_get_client_key_exchange(s);
484 if (ret <= 0)
485 goto end;
David Benjaminb52e3dd2014-08-20 11:12:48 -0400486 s->state=SSL3_ST_SR_CERT_VRFY_A;
487 s->init_num=0;
488
489 /* TODO(davidben): These two blocks are different
490 * between SSL and DTLS. Resolve the difference and code
491 * duplication. */
David Benjaminbd30f8e2014-08-19 16:02:38 -0400492 if (SSL_USE_SIGALGS(s))
Adam Langley95c29f32014-06-20 12:00:00 -0700493 {
Adam Langley95c29f32014-06-20 12:00:00 -0700494 if (!s->session->peer)
495 break;
496 /* For sigalgs freeze the handshake buffer
497 * at this point and digest cached records.
498 */
499 if (!s->s3->handshake_buffer)
500 {
501 OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
502 return -1;
503 }
504 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
505 if (!ssl3_digest_cached_records(s))
506 return -1;
507 }
508 else
509 {
510 int offset=0;
511 int dgst_num;
512
Adam Langley95c29f32014-06-20 12:00:00 -0700513 /* We need to get hashes here so if there is
514 * a client cert, it can be verified
515 * FIXME - digest processing for CertificateVerify
516 * should be generalized. But it is next step
517 */
518 if (s->s3->handshake_buffer)
519 if (!ssl3_digest_cached_records(s))
520 return -1;
521 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
522 if (s->s3->handshake_dgst[dgst_num])
523 {
524 int dgst_size;
525
526 s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
527 dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
528 if (dgst_size < 0)
529 {
530 ret = -1;
531 goto end;
532 }
533 offset+=dgst_size;
534 }
535 }
536 break;
537
538 case SSL3_ST_SR_CERT_VRFY_A:
539 case SSL3_ST_SR_CERT_VRFY_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700540 ret=ssl3_get_cert_verify(s);
541 if (ret <= 0) goto end;
542
David Benjamin84ec49e2014-07-22 14:25:28 -0400543 s->state = SSL3_ST_SR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700544 s->init_num=0;
545 break;
546
David Benjamin84ec49e2014-07-22 14:25:28 -0400547 case SSL3_ST_SR_CHANGE: {
Adam Langley1258b6a2014-06-20 12:00:00 -0700548 char next_proto_neg = 0;
549 char channel_id = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -0700550# if !defined(OPENSSL_NO_NEXTPROTONEG)
551 next_proto_neg = s->s3->next_proto_neg_seen;
552# endif
553 channel_id = s->s3->tlsext_channel_id_valid;
Adam Langley1258b6a2014-06-20 12:00:00 -0700554
David Benjamin84ec49e2014-07-22 14:25:28 -0400555 /* At this point, the next message must be entirely
556 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400557 if (!ssl3_expect_change_cipher_spec(s))
558 {
559 ret = -1;
560 goto end;
561 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700562 if (next_proto_neg)
David Benjamin84ec49e2014-07-22 14:25:28 -0400563 s->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700564 else if (channel_id)
David Benjamin84ec49e2014-07-22 14:25:28 -0400565 s->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700566 else
David Benjamin84ec49e2014-07-22 14:25:28 -0400567 s->state = SSL3_ST_SR_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700568 break;
569 }
570
David Benjamin6dbd73d2014-07-03 15:59:49 -0400571#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700572 case SSL3_ST_SR_NEXT_PROTO_A:
573 case SSL3_ST_SR_NEXT_PROTO_B:
574 ret=ssl3_get_next_proto(s);
575 if (ret <= 0) goto end;
576 s->init_num = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -0700577 if (s->s3->tlsext_channel_id_valid)
578 s->state=SSL3_ST_SR_CHANNEL_ID_A;
579 else
580 s->state=SSL3_ST_SR_FINISHED_A;
581 break;
582#endif
583
Adam Langley1258b6a2014-06-20 12:00:00 -0700584 case SSL3_ST_SR_CHANNEL_ID_A:
585 case SSL3_ST_SR_CHANNEL_ID_B:
586 ret=ssl3_get_channel_id(s);
587 if (ret <= 0) goto end;
588 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700589 s->state=SSL3_ST_SR_FINISHED_A;
590 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700591
592 case SSL3_ST_SR_FINISHED_A:
593 case SSL3_ST_SR_FINISHED_B:
594 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
595 SSL3_ST_SR_FINISHED_B);
596 if (ret <= 0) goto end;
597 if (s->hit)
598 s->state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700599 else if (s->tlsext_ticket_expected)
600 s->state=SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700601 else
602 s->state=SSL3_ST_SW_CHANGE_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700603 /* If this is a full handshake with ChannelID then
604 * record the hashshake hashes in |s->session| in case
605 * we need them to verify a ChannelID signature on a
606 * resumption of this session in the future. */
607 if (!s->hit && s->s3->tlsext_channel_id_new)
608 {
609 ret = tls1_record_handshake_hashes_for_channel_id(s);
610 if (ret <= 0) goto end;
611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612 s->init_num=0;
613 break;
614
Adam Langley95c29f32014-06-20 12:00:00 -0700615 case SSL3_ST_SW_SESSION_TICKET_A:
616 case SSL3_ST_SW_SESSION_TICKET_B:
617 ret=ssl3_send_newsession_ticket(s);
618 if (ret <= 0) goto end;
619 s->state=SSL3_ST_SW_CHANGE_A;
620 s->init_num=0;
621 break;
622
623 case SSL3_ST_SW_CERT_STATUS_A:
624 case SSL3_ST_SW_CERT_STATUS_B:
625 ret=ssl3_send_cert_status(s);
626 if (ret <= 0) goto end;
627 s->state=SSL3_ST_SW_KEY_EXCH_A;
628 s->init_num=0;
629 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700630
631 case SSL3_ST_SW_CHANGE_A:
632 case SSL3_ST_SW_CHANGE_B:
633
634 s->session->cipher=s->s3->tmp.new_cipher;
635 if (!s->method->ssl3_enc->setup_key_block(s))
636 { ret= -1; goto end; }
637
638 ret=ssl3_send_change_cipher_spec(s,
639 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
640
641 if (ret <= 0) goto end;
642 s->state=SSL3_ST_SW_FINISHED_A;
643 s->init_num=0;
644
645 if (!s->method->ssl3_enc->change_cipher_state(s,
646 SSL3_CHANGE_CIPHER_SERVER_WRITE))
647 {
648 ret= -1;
649 goto end;
650 }
651
652 break;
653
654 case SSL3_ST_SW_FINISHED_A:
655 case SSL3_ST_SW_FINISHED_B:
656 ret=ssl3_send_finished(s,
657 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
658 s->method->ssl3_enc->server_finished_label,
659 s->method->ssl3_enc->server_finished_label_len);
660 if (ret <= 0) goto end;
David Benjamin84ec49e2014-07-22 14:25:28 -0400661 s->state = SSL3_ST_SW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700662 if (s->hit)
David Benjamin84ec49e2014-07-22 14:25:28 -0400663 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700664 else
David Benjamin84ec49e2014-07-22 14:25:28 -0400665 s->s3->tmp.next_state = SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700666 s->init_num=0;
667 break;
668
669 case SSL_ST_OK:
670 /* clean a few things up */
671 ssl3_cleanup_key_block(s);
672
673 BUF_MEM_free(s->init_buf);
674 s->init_buf=NULL;
675
676 /* remove buffering on output */
677 ssl_free_wbio_buffer(s);
678
679 s->init_num=0;
680
Adam Langley75872532014-06-20 12:00:00 -0700681 /* If we aren't retaining peer certificates then we can
682 * discard it now. */
683 if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs)
684 {
685 X509_free(s->session->peer);
686 s->session->peer = NULL;
687 }
688
Adam Langley95c29f32014-06-20 12:00:00 -0700689 if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
690 {
691 s->renegotiate=0;
692 s->new_session=0;
693
694 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
695
696 s->ctx->stats.sess_accept_good++;
697 /* s->server=1; */
698 s->handshake_func=ssl3_accept;
699
700 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
701 }
702
703 ret = 1;
704 goto end;
705 /* break; */
706
707 default:
708 OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
709 ret= -1;
710 goto end;
711 /* break; */
712 }
713
714 if (!s->s3->tmp.reuse_message && !skip)
715 {
716 if (s->debug)
717 {
718 if ((ret=BIO_flush(s->wbio)) <= 0)
719 goto end;
720 }
721
722
723 if ((cb != NULL) && (s->state != state))
724 {
725 new_state=s->state;
726 s->state=state;
727 cb(s,SSL_CB_ACCEPT_LOOP,1);
728 s->state=new_state;
729 }
730 }
731 skip=0;
732 }
733end:
734 /* BIO_flush(s->wbio); */
735
736 s->in_handshake--;
737 if (cb != NULL)
738 cb(s,SSL_CB_ACCEPT_EXIT,ret);
739 return(ret);
740 }
741
742int ssl3_send_hello_request(SSL *s)
743 {
744
745 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
746 {
747 ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
748 s->state=SSL3_ST_SW_HELLO_REQ_B;
749 }
750
751 /* SSL3_ST_SW_HELLO_REQ_B */
752 return ssl_do_write(s);
753 }
754
Adam Langley95c29f32014-06-20 12:00:00 -0700755int ssl3_get_client_hello(SSL *s)
756 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400757 int i,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700758 long n;
David Benjamin6f260012014-08-15 13:49:12 -0400759 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700760 STACK_OF(SSL_CIPHER) *ciphers=NULL;
Adam Langleydc9b1412014-06-20 12:00:00 -0700761 struct ssl_early_callback_ctx early_ctx;
David Benjamin22f9bcc2014-07-13 12:29:21 -0400762 CBS client_hello;
763 uint16_t client_version;
764 CBS client_random, session_id, cipher_suites, compression_methods;
Adam Langley95c29f32014-06-20 12:00:00 -0700765
766 /* We do this so that we will respond with our native type.
767 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
768 * This down switching should be handled by a different method.
769 * If we are SSLv3, we will respond with SSLv3, even if prompted with
770 * TLSv1.
771 */
Adam Langleydc9b1412014-06-20 12:00:00 -0700772 switch (s->state) {
773 case SSL3_ST_SR_CLNT_HELLO_A:
Adam Langleydc9b1412014-06-20 12:00:00 -0700774 case SSL3_ST_SR_CLNT_HELLO_B:
Adam Langleyb2ce0582014-06-20 12:00:00 -0700775 s->first_packet=1;
776 n=s->method->ssl_get_message(s,
David Benjamin95fcaa42014-08-05 13:10:14 -0400777 SSL3_ST_SR_CLNT_HELLO_A,
Adam Langleyb2ce0582014-06-20 12:00:00 -0700778 SSL3_ST_SR_CLNT_HELLO_B,
Adam Langleyb2ce0582014-06-20 12:00:00 -0700779 SSL3_MT_CLIENT_HELLO,
780 SSL3_RT_MAX_PLAIN_LENGTH,
781 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyb2ce0582014-06-20 12:00:00 -0700783 if (!ok) return((int)n);
784 s->first_packet=0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700785
786 /* If we require cookies and this ClientHello doesn't
787 * contain one, just return since we do not want to
788 * allocate any memory yet. So check cookie length...
789 */
David Benjamin22f9bcc2014-07-13 12:29:21 -0400790 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
Adam Langleydc9b1412014-06-20 12:00:00 -0700791 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400792 CBS session_id;
793 uint8_t cookie_length;
Adam Langleydc9b1412014-06-20 12:00:00 -0700794
David Benjamin22f9bcc2014-07-13 12:29:21 -0400795 CBS_init(&client_hello, s->init_msg, n);
796 if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) ||
797 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
798 !CBS_get_u8(&client_hello, &cookie_length))
799 {
800 al = SSL_AD_DECODE_ERROR;
801 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
802 goto f_err;
803 }
804
Adam Langleydc9b1412014-06-20 12:00:00 -0700805 if (cookie_length == 0)
806 return 1;
807 }
808 s->state = SSL3_ST_SR_CLNT_HELLO_C;
809 /* fallthrough */
810 case SSL3_ST_SR_CLNT_HELLO_C:
811 case SSL3_ST_SR_CLNT_HELLO_D:
812 /* We have previously parsed the ClientHello message,
813 * and can't call ssl_get_message again without hashing
814 * the message into the Finished digest again. */
Adam Langleyb2ce0582014-06-20 12:00:00 -0700815 n = s->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700816
817 memset(&early_ctx, 0, sizeof(early_ctx));
818 early_ctx.ssl = s;
819 early_ctx.client_hello = s->init_msg;
820 early_ctx.client_hello_len = n;
821 if (!ssl_early_callback_init(&early_ctx))
822 {
823 al = SSL_AD_DECODE_ERROR;
824 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED);
825 goto f_err;
826 }
827
828 if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
829 s->ctx->select_certificate_cb != NULL)
830 {
831 int ret;
832
833 s->state = SSL3_ST_SR_CLNT_HELLO_D;
834 ret = s->ctx->select_certificate_cb(&early_ctx);
835 if (ret == 0)
836 return CERTIFICATE_SELECTION_PENDING;
837 else if (ret == -1)
838 {
839 /* Connection rejected. */
840 al = SSL_AD_ACCESS_DENIED;
841 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
842 goto f_err;
843 }
844 }
845 s->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4c852c52014-07-02 15:24:48 -0400846 break;
Adam Langleydc9b1412014-06-20 12:00:00 -0700847 default:
David Benjamin4c852c52014-07-02 15:24:48 -0400848 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
Adam Langleydc9b1412014-06-20 12:00:00 -0700849 return -1;
850 }
Adam Langleyb2ce0582014-06-20 12:00:00 -0700851
David Benjamin22f9bcc2014-07-13 12:29:21 -0400852 CBS_init(&client_hello, s->init_msg, n);
853 if (!CBS_get_u16(&client_hello, &client_version) ||
854 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
855 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
856 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH)
857 {
858 al = SSL_AD_DECODE_ERROR;
859 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
860 goto f_err;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
863 /* use version from inside client hello, not from record header
864 * (may differ: see RFC 2246, Appendix E, second paragraph) */
David Benjamin22f9bcc2014-07-13 12:29:21 -0400865 s->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700866
867 if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
868 s->method->version != DTLS_ANY_VERSION)
869 : (s->client_version < s->version))
870 {
871 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
872 if ((s->client_version>>8) == SSL3_VERSION_MAJOR &&
873 !s->enc_write_ctx && !s->write_hash)
874 {
875 /* similar to ssl3_get_record, send alert using remote version number */
876 s->version = s->client_version;
877 }
878 al = SSL_AD_PROTOCOL_VERSION;
879 goto f_err;
880 }
881
David Benjamin22f9bcc2014-07-13 12:29:21 -0400882 /* Load the client random. */
883 memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700884
885 s->hit=0;
886 /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
887 * 0.9.7 and later allow this by default, but optionally ignore resumption requests
888 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
889 * than a change to default behavior so that applications relying on this for security
890 * won't even compile against older library versions).
891 *
892 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
893 * renegotiation but not a new session (s->new_session remains unset): for servers,
894 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
895 * setting will be ignored.
896 */
897 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
898 {
899 if (!ssl_get_new_session(s,1))
900 goto err;
901 }
902 else
903 {
Adam Langleydc9b1412014-06-20 12:00:00 -0700904 i=ssl_get_prev_session(s, &early_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700905 if (i == 1)
906 { /* previous session */
907 s->hit=1;
908 }
909 else if (i == -1)
910 goto err;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700911 else if (i == PENDING_SESSION)
912 {
913 ret = PENDING_SESSION;
914 goto err;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916 else /* i == 0 */
917 {
918 if (!ssl_get_new_session(s,1))
919 goto err;
920 }
921 }
922
Adam Langley95c29f32014-06-20 12:00:00 -0700923 if (SSL_IS_DTLS(s))
924 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400925 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -0700926
David Benjamin22f9bcc2014-07-13 12:29:21 -0400927 /* TODO(davidben): The length check here is off. Per
928 * spec, the maximum cookie length is 32. However, the
929 * DTLS1_COOKIE_LENGTH check is checking against 256,
930 * not 32 (so it's actually redundant).
931 * 07a9d1a2c2b735cbc327065000b545deb5e136cf from
932 * OpenSSL switched this from 32 to 256. */
933 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
934 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH)
Adam Langley95c29f32014-06-20 12:00:00 -0700935 {
Adam Langley95c29f32014-06-20 12:00:00 -0700936 al = SSL_AD_DECODE_ERROR;
David Benjamin22f9bcc2014-07-13 12:29:21 -0400937 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700938 goto f_err;
939 }
940
David Benjamin22f9bcc2014-07-13 12:29:21 -0400941 /* Verify the cookie if appropriate option is set. */
Adam Langley95c29f32014-06-20 12:00:00 -0700942 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400943 CBS_len(&cookie) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700944 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400945 if (s->ctx->app_verify_cookie_cb != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700946 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400947 if (s->ctx->app_verify_cookie_cb(s,
David Benjaminfb4ea282014-08-15 13:38:15 -0400948 CBS_data(&cookie), CBS_len(&cookie)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700949 {
950 al=SSL_AD_HANDSHAKE_FAILURE;
951 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
952 goto f_err;
953 }
954 /* else cookie verification succeeded */
955 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400956 else if (!CBS_mem_equal(&cookie, s->d1->cookie, s->d1->cookie_len))
Adam Langley95c29f32014-06-20 12:00:00 -0700957 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400958 /* default verification */
959 al=SSL_AD_HANDSHAKE_FAILURE;
960 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
961 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700962 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400963 /* Set to -2 so if successful we return 2 and
964 * don't send HelloVerifyRequest. */
Adam Langley95c29f32014-06-20 12:00:00 -0700965 ret = -2;
966 }
967
Adam Langley95c29f32014-06-20 12:00:00 -0700968 if (s->method->version == DTLS_ANY_VERSION)
969 {
970 /* Select version to use */
971 if (s->client_version <= DTLS1_2_VERSION &&
972 !(s->options & SSL_OP_NO_DTLSv1_2))
973 {
974 s->version = DTLS1_2_VERSION;
975 s->method = DTLSv1_2_server_method();
976 }
Adam Langley95c29f32014-06-20 12:00:00 -0700977 else if (s->client_version <= DTLS1_VERSION &&
978 !(s->options & SSL_OP_NO_DTLSv1))
979 {
980 s->version = DTLS1_VERSION;
981 s->method = DTLSv1_server_method();
982 }
983 else
984 {
985 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
986 s->version = s->client_version;
987 al = SSL_AD_PROTOCOL_VERSION;
988 goto f_err;
989 }
990 s->session->ssl_version = s->version;
991 }
992 }
993
David Benjamin22f9bcc2014-07-13 12:29:21 -0400994 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
995 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
996 CBS_len(&compression_methods) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700997 {
David Benjamin22f9bcc2014-07-13 12:29:21 -0400998 al = SSL_AD_DECODE_ERROR;
999 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
1000 goto f_err;
1001 }
1002
1003 /* TODO(davidben): Per spec, cipher_suites can never be empty
1004 * (specified at the ClientHello structure level). This logic
1005 * allows it to be empty if resuming a session. Can we always
1006 * require non-empty? If a client sends empty cipher_suites
1007 * because it's resuming a session, it could always fail to
1008 * resume a session, so it's unlikely to actually work. */
1009 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0)
1010 {
1011 /* We need a cipher if we are not resuming a session. */
1012 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001013 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
1014 goto f_err;
1015 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001016
David Benjamin39482a12014-07-20 13:30:15 -04001017 if (ssl_bytes_to_cipher_list(s, &cipher_suites, &ciphers) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001018 {
1019 goto err;
1020 }
Adam Langley95c29f32014-06-20 12:00:00 -07001021
1022 /* If it is a hit, check that the cipher is in the list */
David Benjamin22f9bcc2014-07-13 12:29:21 -04001023 if (s->hit && CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001024 {
David Benjamin22f9bcc2014-07-13 12:29:21 -04001025 int found_cipher = 0;
1026 unsigned long id = s->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langley95c29f32014-06-20 12:00:00 -07001028 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
1029 {
1030 c=sk_SSL_CIPHER_value(ciphers,i);
Adam Langley95c29f32014-06-20 12:00:00 -07001031 if (c->id == id)
1032 {
David Benjamin22f9bcc2014-07-13 12:29:21 -04001033 found_cipher = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001034 break;
1035 }
1036 }
David Benjamin22f9bcc2014-07-13 12:29:21 -04001037 if (!found_cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07001038 {
1039 /* we need to have the cipher in the cipher
1040 * list if we are asked to reuse it */
1041 al=SSL_AD_ILLEGAL_PARAMETER;
1042 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING);
1043 goto f_err;
1044 }
1045 }
1046
David Benjamin22f9bcc2014-07-13 12:29:21 -04001047 /* Only null compression is supported. */
1048 if (memchr(CBS_data(&compression_methods), 0,
1049 CBS_len(&compression_methods)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001050 {
David Benjamin22f9bcc2014-07-13 12:29:21 -04001051 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001052 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED);
1053 goto f_err;
1054 }
1055
Adam Langley95c29f32014-06-20 12:00:00 -07001056 /* TLS extensions*/
1057 if (s->version >= SSL3_VERSION)
1058 {
David Benjamin22f9bcc2014-07-13 12:29:21 -04001059 if (!ssl_parse_clienthello_tlsext(s, &client_hello))
Adam Langley95c29f32014-06-20 12:00:00 -07001060 {
1061 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
1062 goto err;
1063 }
1064 }
1065
David Benjamindc72ff72014-06-25 12:36:10 -04001066 /* There should be nothing left over in the record. */
David Benjamin22f9bcc2014-07-13 12:29:21 -04001067 if (CBS_len(&client_hello) != 0)
David Benjamindc72ff72014-06-25 12:36:10 -04001068 {
1069 /* wrong packet length */
1070 al=SSL_AD_DECODE_ERROR;
1071 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
1072 goto f_err;
1073 }
1074
Adam Langley95c29f32014-06-20 12:00:00 -07001075 /* Check if we want to use external pre-shared secret for this
1076 * handshake for not reused session only. We need to generate
1077 * server_random before calling tls_session_secret_cb in order to allow
1078 * SessionTicket processing to use it in key derivation. */
1079 {
1080 unsigned char *pos;
1081 pos=s->s3->server_random;
1082 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0)
1083 {
1084 goto f_err;
1085 }
1086 }
1087
1088 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1089 {
David Benjamin6f260012014-08-15 13:49:12 -04001090 const SSL_CIPHER *pref_cipher=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001091
1092 s->session->master_key_length=sizeof(s->session->master_key);
1093 if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
1094 ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
1095 {
1096 s->hit=1;
1097 s->session->ciphers=ciphers;
1098 s->session->verify_result=X509_V_OK;
1099
1100 ciphers=NULL;
1101
1102 /* check if some cipher was preferred by call back */
Adam Langley858a88d2014-06-20 12:00:00 -07001103 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, ssl_get_cipher_preferences(s));
Adam Langley95c29f32014-06-20 12:00:00 -07001104 if (pref_cipher == NULL)
1105 {
1106 al=SSL_AD_HANDSHAKE_FAILURE;
1107 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1108 goto f_err;
1109 }
1110
1111 s->session->cipher=pref_cipher;
1112
1113 if (s->cipher_list)
Adam Langley858a88d2014-06-20 12:00:00 -07001114 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07001115
1116 if (s->cipher_list_by_id)
1117 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1118
Adam Langley858a88d2014-06-20 12:00:00 -07001119 s->cipher_list = ssl_cipher_preference_list_from_ciphers(s->session->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001120 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1121 }
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langley95c29f32014-06-20 12:00:00 -07001124 /* Given s->session->ciphers and SSL_get_ciphers, we must
1125 * pick a cipher */
1126
1127 if (!s->hit)
1128 {
Adam Langley95c29f32014-06-20 12:00:00 -07001129 if (s->session->ciphers != NULL)
1130 sk_SSL_CIPHER_free(s->session->ciphers);
1131 s->session->ciphers=ciphers;
1132 if (ciphers == NULL)
1133 {
1134 al=SSL_AD_ILLEGAL_PARAMETER;
1135 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
1136 goto f_err;
1137 }
1138 ciphers=NULL;
1139 /* Let cert callback update server certificates if required */
Adam Langley95c29f32014-06-20 12:00:00 -07001140 if (s->cert->cert_cb)
1141 {
1142 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1143 if (rv == 0)
1144 {
1145 al=SSL_AD_INTERNAL_ERROR;
1146 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
1147 goto f_err;
1148 }
1149 if (rv < 0)
1150 {
1151 s->rwstate=SSL_X509_LOOKUP;
1152 return -1;
1153 }
1154 s->rwstate = SSL_NOTHING;
1155 }
1156 c=ssl3_choose_cipher(s,s->session->ciphers,
Adam Langley858a88d2014-06-20 12:00:00 -07001157 ssl_get_cipher_preferences(s));
Adam Langley95c29f32014-06-20 12:00:00 -07001158
1159 if (c == NULL)
1160 {
1161 al=SSL_AD_HANDSHAKE_FAILURE;
1162 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
1163 goto f_err;
1164 }
1165 s->s3->tmp.new_cipher=c;
1166 }
1167 else
1168 {
1169 /* Session-id reuse */
Adam Langley95c29f32014-06-20 12:00:00 -07001170 s->s3->tmp.new_cipher=s->session->cipher;
1171 }
1172
1173 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER))
1174 {
1175 if (!ssl3_digest_cached_records(s))
1176 goto f_err;
1177 }
1178
1179 /* we now have the following setup.
1180 * client_random
1181 * cipher_list - our prefered list of ciphers
1182 * ciphers - the clients prefered list of ciphers
1183 * compression - basically ignored right now
1184 * ssl version is set - sslv3
1185 * s->session - The ssl session has been setup.
1186 * s->hit - session reuse flag
1187 * s->tmp.new_cipher - the new cipher to use.
1188 */
1189
1190 /* Handles TLS extensions that we couldn't check earlier */
1191 if (s->version >= SSL3_VERSION)
1192 {
1193 if (ssl_check_clienthello_tlsext_late(s) <= 0)
1194 {
1195 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
1196 goto err;
1197 }
1198 }
1199
1200 if (ret < 0) ret=-ret;
1201 if (0)
1202 {
1203f_err:
1204 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1205 }
1206err:
1207 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
Adam Langleyb2ce0582014-06-20 12:00:00 -07001208 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07001209 }
1210
1211int ssl3_send_server_hello(SSL *s)
1212 {
1213 unsigned char *buf;
1214 unsigned char *p,*d;
David Benjamin39482a12014-07-20 13:30:15 -04001215 int sl;
Adam Langley95c29f32014-06-20 12:00:00 -07001216 unsigned long l;
1217
1218 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1219 {
Adam Langley1258b6a2014-06-20 12:00:00 -07001220 /* We only accept ChannelIDs on connections with ECDHE in order
1221 * to avoid a known attack while we fix ChannelID itself. */
1222 if (s->s3 &&
1223 s->s3->tlsext_channel_id_valid &&
1224 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0)
1225 s->s3->tlsext_channel_id_valid = 0;
1226
1227 /* If this is a resumption and the original handshake didn't
1228 * support ChannelID then we didn't record the original
1229 * handshake hashes in the session and so cannot resume with
1230 * ChannelIDs. */
1231 if (s->hit &&
1232 s->s3->tlsext_channel_id_new &&
1233 s->session->original_handshake_hash_len == 0)
1234 s->s3->tlsext_channel_id_valid = 0;
1235
Adam Langley1b9d4772014-06-20 12:00:00 -07001236 if (s->mode & SSL_MODE_RELEASE_BUFFERS)
1237 {
1238 /* Free s->session->ciphers in order to release memory. This
1239 * breaks SSL_get_shared_ciphers(), but many servers will
David Benjamin5ffeb7c2014-07-15 00:34:01 -04001240 * prefer the memory savings. */
Adam Langley1b9d4772014-06-20 12:00:00 -07001241 sk_SSL_CIPHER_free(s->session->ciphers);
1242 s->session->ciphers = NULL;
1243 }
1244
Adam Langley95c29f32014-06-20 12:00:00 -07001245 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07001246 /* Do the message type and length last */
1247 d=p= ssl_handshake_start(s);
1248
1249 *(p++)=s->version>>8;
1250 *(p++)=s->version&0xff;
1251
1252 /* Random stuff */
1253 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1254 p+=SSL3_RANDOM_SIZE;
1255
1256 /* There are several cases for the session ID to send
1257 * back in the server hello:
1258 * - For session reuse from the session cache,
1259 * we send back the old session ID.
1260 * - If stateless session reuse (using a session ticket)
1261 * is successful, we send back the client's "session ID"
1262 * (which doesn't actually identify the session).
1263 * - If it is a new session, we send back the new
1264 * session ID.
1265 * - However, if we want the new session to be single-use,
1266 * we send back a 0-length session ID.
1267 * s->hit is non-zero in either case of session reuse,
1268 * so the following won't overwrite an ID that we're supposed
1269 * to send back.
1270 */
1271 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1272 && !s->hit)
1273 s->session->session_id_length=0;
1274
1275 sl=s->session->session_id_length;
1276 if (sl > (int)sizeof(s->session->session_id))
1277 {
1278 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1279 return -1;
1280 }
1281 *(p++)=sl;
1282 memcpy(p,s->session->session_id,sl);
1283 p+=sl;
1284
1285 /* put the cipher */
David Benjamin39482a12014-07-20 13:30:15 -04001286 s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001287
1288 /* put the compression method */
1289 *(p++)=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001290 if (ssl_prepare_serverhello_tlsext(s) <= 0)
1291 {
1292 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
1293 return -1;
1294 }
1295 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
1296 {
1297 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
1298 return -1;
1299 }
Adam Langley95c29f32014-06-20 12:00:00 -07001300 /* do the header */
1301 l=(p-d);
1302 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1303 s->state=SSL3_ST_SW_SRVR_HELLO_B;
1304 }
1305
1306 /* SSL3_ST_SW_SRVR_HELLO_B */
1307 return ssl_do_write(s);
1308 }
1309
1310int ssl3_send_server_done(SSL *s)
1311 {
1312
1313 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1314 {
1315 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1316 s->state = SSL3_ST_SW_SRVR_DONE_B;
1317 }
1318
1319 /* SSL3_ST_SW_SRVR_DONE_B */
1320 return ssl_do_write(s);
1321 }
1322
1323int ssl3_send_server_key_exchange(SSL *s)
1324 {
Adam Langley95c29f32014-06-20 12:00:00 -07001325 unsigned char *q;
1326 int j,num;
Adam Langley95c29f32014-06-20 12:00:00 -07001327 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1328 unsigned int u;
Adam Langley95c29f32014-06-20 12:00:00 -07001329#ifndef OPENSSL_NO_DH
1330 DH *dh=NULL,*dhp;
1331#endif
1332#ifndef OPENSSL_NO_ECDH
1333 EC_KEY *ecdh=NULL, *ecdhp;
1334 unsigned char *encodedPoint = NULL;
1335 int encodedlen = 0;
1336 int curve_id = 0;
1337 BN_CTX *bn_ctx = NULL;
1338#endif
David Benjamin77a942b2014-07-15 01:22:50 -04001339 const char* psk_identity_hint = NULL;
1340 size_t psk_identity_hint_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001341 EVP_PKEY *pkey;
1342 const EVP_MD *md = NULL;
1343 unsigned char *p,*d;
1344 int al,i;
Adam Langleyc26c8022014-06-20 12:00:00 -07001345 unsigned long alg_k;
1346 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001347 int n;
1348 CERT *cert;
1349 BIGNUM *r[4];
1350 int nr[4],kn;
1351 BUF_MEM *buf;
1352 EVP_MD_CTX md_ctx;
1353
1354 EVP_MD_CTX_init(&md_ctx);
1355 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1356 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001357 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1358 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001359 cert=s->cert;
1360
1361 buf=s->init_buf;
1362
1363 r[0]=r[1]=r[2]=r[3]=NULL;
1364 n=0;
Adam Langleyc26c8022014-06-20 12:00:00 -07001365 if (alg_a & SSL_aPSK)
1366 {
1367 /* size for PSK identity hint */
Adam Langley0289c732014-06-20 12:00:00 -07001368 psk_identity_hint = s->session->psk_identity_hint;
1369 if (psk_identity_hint)
1370 psk_identity_hint_len = strlen(psk_identity_hint);
1371 else
1372 psk_identity_hint_len = 0;
1373 n+=2+psk_identity_hint_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001374 }
Adam Langley95c29f32014-06-20 12:00:00 -07001375#ifndef OPENSSL_NO_DH
David Benjamin77a942b2014-07-15 01:22:50 -04001376 if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001377 {
1378 dhp=cert->dh_tmp;
1379 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
David Benjamin060d9d22014-07-15 00:54:26 -04001380 dhp=s->cert->dh_tmp_cb(s, 0, 1024);
Adam Langley95c29f32014-06-20 12:00:00 -07001381 if (dhp == NULL)
1382 {
1383 al=SSL_AD_HANDSHAKE_FAILURE;
1384 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
1385 goto f_err;
1386 }
1387
1388 if (s->s3->tmp.dh != NULL)
1389 {
1390 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
1391 goto err;
1392 }
1393
1394 if ((dh=DHparams_dup(dhp)) == NULL)
1395 {
1396 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1397 goto err;
1398 }
1399
1400 s->s3->tmp.dh=dh;
1401 if ((dhp->pub_key == NULL ||
1402 dhp->priv_key == NULL ||
1403 (s->options & SSL_OP_SINGLE_DH_USE)))
1404 {
1405 if(!DH_generate_key(dh))
1406 {
1407 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1408 goto err;
1409 }
1410 }
1411 else
1412 {
1413 dh->pub_key=BN_dup(dhp->pub_key);
1414 dh->priv_key=BN_dup(dhp->priv_key);
1415 if ((dh->pub_key == NULL) ||
1416 (dh->priv_key == NULL))
1417 {
1418 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
1419 goto err;
1420 }
1421 }
1422 r[0]=dh->p;
1423 r[1]=dh->g;
1424 r[2]=dh->pub_key;
1425 }
David Benjamin77a942b2014-07-15 01:22:50 -04001426 else
Adam Langley95c29f32014-06-20 12:00:00 -07001427#endif
1428#ifndef OPENSSL_NO_ECDH
David Benjamin77a942b2014-07-15 01:22:50 -04001429 if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001430 {
1431 const EC_GROUP *group;
1432
1433 ecdhp=cert->ecdh_tmp;
1434 if (s->cert->ecdh_tmp_auto)
1435 {
1436 /* Get NID of appropriate shared curve */
David Benjamin072334d2014-07-13 16:24:27 -04001437 int nid = tls1_get_shared_curve(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001438 if (nid != NID_undef)
1439 ecdhp = EC_KEY_new_by_curve_name(nid);
1440 }
1441 else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb)
1442 {
David Benjamin060d9d22014-07-15 00:54:26 -04001443 ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024);
Adam Langley95c29f32014-06-20 12:00:00 -07001444 }
1445 if (ecdhp == NULL)
1446 {
1447 al=SSL_AD_HANDSHAKE_FAILURE;
1448 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY);
1449 goto f_err;
1450 }
1451
1452 if (s->s3->tmp.ecdh != NULL)
1453 {
1454 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
1455 goto err;
1456 }
1457
1458 /* Duplicate the ECDH structure. */
1459 if (ecdhp == NULL)
1460 {
1461 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1462 goto err;
1463 }
1464 if (s->cert->ecdh_tmp_auto)
1465 ecdh = ecdhp;
1466 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
1467 {
1468 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1469 goto err;
1470 }
1471
1472 s->s3->tmp.ecdh=ecdh;
1473 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1474 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1475 (s->options & SSL_OP_SINGLE_ECDH_USE))
1476 {
1477 if(!EC_KEY_generate_key(ecdh))
1478 {
1479 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1480 goto err;
1481 }
1482 }
1483
1484 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1485 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1486 (EC_KEY_get0_private_key(ecdh) == NULL))
1487 {
1488 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1489 goto err;
1490 }
1491
Adam Langley95c29f32014-06-20 12:00:00 -07001492 /* XXX: For now, we only support ephemeral ECDH
1493 * keys over named (not generic) curves. For
1494 * supported named curves, curve_id is non-zero.
1495 */
1496 if ((curve_id =
1497 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1498 == 0)
1499 {
1500 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1501 goto err;
1502 }
1503
1504 /* Encode the public key.
1505 * First check the size of encoding and
1506 * allocate memory accordingly.
1507 */
1508 encodedlen = EC_POINT_point2oct(group,
1509 EC_KEY_get0_public_key(ecdh),
1510 POINT_CONVERSION_UNCOMPRESSED,
1511 NULL, 0, NULL);
1512
1513 encodedPoint = (unsigned char *)
1514 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1515 bn_ctx = BN_CTX_new();
1516 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1517 {
1518 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE);
1519 goto err;
1520 }
1521
1522
1523 encodedlen = EC_POINT_point2oct(group,
1524 EC_KEY_get0_public_key(ecdh),
1525 POINT_CONVERSION_UNCOMPRESSED,
1526 encodedPoint, encodedlen, bn_ctx);
1527
1528 if (encodedlen == 0)
1529 {
1530 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
1531 goto err;
1532 }
1533
1534 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1535
1536 /* XXX: For now, we only support named (not
1537 * generic) curves in ECDH ephemeral key exchanges.
1538 * In this situation, we need four additional bytes
1539 * to encode the entire ServerECDHParams
1540 * structure.
1541 */
Adam Langleyc26c8022014-06-20 12:00:00 -07001542 n += 4 + encodedlen;
Adam Langley95c29f32014-06-20 12:00:00 -07001543
1544 /* We'll generate the serverKeyExchange message
1545 * explicitly so we can set these to NULLs
1546 */
1547 r[0]=NULL;
1548 r[1]=NULL;
1549 r[2]=NULL;
1550 r[3]=NULL;
1551 }
David Benjamin77a942b2014-07-15 01:22:50 -04001552 else
Adam Langley95c29f32014-06-20 12:00:00 -07001553#endif /* !OPENSSL_NO_ECDH */
David Benjamin77a942b2014-07-15 01:22:50 -04001554 if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001555 {
1556 al=SSL_AD_HANDSHAKE_FAILURE;
1557 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1558 goto f_err;
1559 }
1560 for (i=0; i < 4 && r[i] != NULL; i++)
1561 {
1562 nr[i]=BN_num_bytes(r[i]);
1563 n+=2+nr[i];
1564 }
1565
David Benjamine8f3d662014-07-12 01:10:19 -04001566 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001567 {
1568 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md))
1569 == NULL)
1570 {
1571 al=SSL_AD_DECODE_ERROR;
1572 goto f_err;
1573 }
1574 kn=EVP_PKEY_size(pkey);
1575 }
1576 else
1577 {
1578 pkey=NULL;
1579 kn=0;
1580 }
1581
1582 if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn))
1583 {
1584 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
1585 goto err;
1586 }
1587 d = p = ssl_handshake_start(s);
1588
1589 for (i=0; i < 4 && r[i] != NULL; i++)
1590 {
1591 s2n(nr[i],p);
1592 BN_bn2bin(r[i],p);
1593 p+=nr[i];
1594 }
1595
Adam Langleyc26c8022014-06-20 12:00:00 -07001596/* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK.
1597 * When one of them is used, the server key exchange record needs to have both
1598 * the psk_identity_hint and the ServerECDHParams. */
Adam Langleyc26c8022014-06-20 12:00:00 -07001599 if (alg_a & SSL_aPSK)
1600 {
Adam Langley0289c732014-06-20 12:00:00 -07001601 /* copy PSK identity hint (if provided) */
1602 s2n(psk_identity_hint_len, p);
1603 if (psk_identity_hint_len > 0)
Adam Langleyc26c8022014-06-20 12:00:00 -07001604 {
Adam Langley0289c732014-06-20 12:00:00 -07001605 memcpy(p, psk_identity_hint, psk_identity_hint_len);
1606 p+=psk_identity_hint_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001607 }
1608 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001609
Adam Langley95c29f32014-06-20 12:00:00 -07001610#ifndef OPENSSL_NO_ECDH
Adam Langleyc26c8022014-06-20 12:00:00 -07001611 if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {
1613 /* XXX: For now, we only support named (not generic) curves.
1614 * In this situation, the serverKeyExchange message has:
1615 * [1 byte CurveType], [2 byte CurveName]
1616 * [1 byte length of encoded point], followed by
1617 * the actual encoded point itself
1618 */
1619 *p = NAMED_CURVE_TYPE;
1620 p += 1;
1621 *p = 0;
1622 p += 1;
1623 *p = curve_id;
1624 p += 1;
1625 *p = encodedlen;
1626 p += 1;
1627 memcpy((unsigned char*)p,
1628 (unsigned char *)encodedPoint,
1629 encodedlen);
1630 OPENSSL_free(encodedPoint);
1631 encodedPoint = NULL;
1632 p += encodedlen;
1633 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001634#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07001635
1636 /* not anonymous */
1637 if (pkey != NULL)
1638 {
1639 /* n is the length of the params, they start at &(d[4])
1640 * and p points to the space at the end. */
Adam Langley95c29f32014-06-20 12:00:00 -07001641 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1642 {
1643 q=md_buf;
1644 j=0;
1645 for (num=2; num > 0; num--)
1646 {
David Benjamine7127782014-08-18 23:15:29 -04001647 EVP_DigestInit_ex(&md_ctx,
1648 (num == 2) ? EVP_md5() : EVP_sha1(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001649 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1650 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1651 EVP_DigestUpdate(&md_ctx,d,n);
1652 EVP_DigestFinal_ex(&md_ctx,q,
1653 (unsigned int *)&i);
1654 q+=i;
1655 j+=i;
1656 }
1657 if (RSA_sign(NID_md5_sha1, md_buf, j,
1658 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1659 {
1660 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA);
1661 goto err;
1662 }
1663 s2n(u,p);
1664 n+=u+2;
1665 }
1666 else
Adam Langley95c29f32014-06-20 12:00:00 -07001667 if (md)
1668 {
1669 /* send signature algorithm */
1670 if (SSL_USE_SIGALGS(s))
1671 {
1672 if (!tls12_get_sigandhash(p, pkey, md))
1673 {
1674 /* Should never happen */
1675 al=SSL_AD_INTERNAL_ERROR;
1676 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
1677 goto f_err;
1678 }
1679 p+=2;
1680 }
1681#ifdef SSL_DEBUG
1682 fprintf(stderr, "Using hash %s\n",
1683 EVP_MD_name(md));
1684#endif
1685 EVP_SignInit_ex(&md_ctx, md, NULL);
1686 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1687 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1688 EVP_SignUpdate(&md_ctx,d,n);
1689 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1690 (unsigned int *)&i,pkey))
1691 {
1692 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
1693 goto err;
1694 }
1695 s2n(i,p);
1696 n+=i+2;
1697 if (SSL_USE_SIGALGS(s))
1698 n+= 2;
1699 }
1700 else
1701 {
1702 /* Is this error check actually needed? */
1703 al=SSL_AD_HANDSHAKE_FAILURE;
1704 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE);
1705 goto f_err;
1706 }
1707 }
1708
1709 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
1710 }
1711
1712 s->state = SSL3_ST_SW_KEY_EXCH_B;
1713 EVP_MD_CTX_cleanup(&md_ctx);
1714 return ssl_do_write(s);
1715f_err:
1716 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1717err:
1718#ifndef OPENSSL_NO_ECDH
1719 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1720 BN_CTX_free(bn_ctx);
1721#endif
1722 EVP_MD_CTX_cleanup(&md_ctx);
1723 return(-1);
1724 }
1725
1726int ssl3_send_certificate_request(SSL *s)
1727 {
1728 unsigned char *p,*d;
1729 int i,j,nl,off,n;
1730 STACK_OF(X509_NAME) *sk=NULL;
1731 X509_NAME *name;
1732 BUF_MEM *buf;
1733
1734 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1735 {
1736 buf=s->init_buf;
1737
1738 d=p=ssl_handshake_start(s);
1739
1740 /* get the list of acceptable cert types */
1741 p++;
1742 n=ssl3_get_req_cert_type(s,p);
1743 d[0]=n;
1744 p+=n;
1745 n++;
1746
1747 if (SSL_USE_SIGALGS(s))
1748 {
1749 const unsigned char *psigs;
1750 nl = tls12_get_psigalgs(s, &psigs);
1751 s2n(nl, p);
1752 memcpy(p, psigs, nl);
1753 p += nl;
1754 n += nl + 2;
1755 }
1756
1757 off=n;
1758 p+=2;
1759 n+=2;
1760
1761 sk=SSL_get_client_CA_list(s);
1762 nl=0;
1763 if (sk != NULL)
1764 {
1765 for (i=0; i<sk_X509_NAME_num(sk); i++)
1766 {
1767 name=sk_X509_NAME_value(sk,i);
1768 j=i2d_X509_NAME(name,NULL);
1769 if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2))
1770 {
1771 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1772 goto err;
1773 }
1774 p = ssl_handshake_start(s) + n;
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001775 s2n(j,p);
1776 i2d_X509_NAME(name,&p);
1777 n+=2+j;
1778 nl+=2+j;
Adam Langley95c29f32014-06-20 12:00:00 -07001779 }
1780 }
1781 /* else no CA names */
1782 p = ssl_handshake_start(s) + off;
1783 s2n(nl,p);
1784
1785 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
1786
1787#ifdef NETSCAPE_HANG_BUG
1788 if (!SSL_IS_DTLS(s))
1789 {
Adam Langleyce1cfe12014-06-20 12:00:00 -07001790 if (!BUF_MEM_grow_clean(buf, s->init_num + 4))
1791 {
1792 OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
1793 goto err;
1794 }
Adam Langley95c29f32014-06-20 12:00:00 -07001795 p=(unsigned char *)s->init_buf->data + s->init_num;
1796 /* do the header */
1797 *(p++)=SSL3_MT_SERVER_DONE;
1798 *(p++)=0;
1799 *(p++)=0;
1800 *(p++)=0;
1801 s->init_num += 4;
1802 }
1803#endif
1804
1805 s->state = SSL3_ST_SW_CERT_REQ_B;
1806 }
1807
1808 /* SSL3_ST_SW_CERT_REQ_B */
1809 return ssl_do_write(s);
1810err:
1811 return(-1);
1812 }
1813
1814int ssl3_get_client_key_exchange(SSL *s)
1815 {
David Benjamin35c02302014-07-13 04:14:59 -04001816 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001817 long n;
David Benjamin35c02302014-07-13 04:14:59 -04001818 CBS client_key_exchange;
Adam Langley95c29f32014-06-20 12:00:00 -07001819 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001820 unsigned long alg_a;
David Benjamin35c02302014-07-13 04:14:59 -04001821 uint8_t *premaster_secret = NULL;
David Benjamin14c83e72014-07-13 04:54:57 -04001822 size_t premaster_secret_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001823 RSA *rsa=NULL;
David Benjamin0aa07672014-07-24 15:34:14 -04001824 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001825 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001826#ifndef OPENSSL_NO_DH
1827 BIGNUM *pub=NULL;
David Benjaminbd30f8e2014-08-19 16:02:38 -04001828 DH *dh_srvr;
Adam Langley95c29f32014-06-20 12:00:00 -07001829#endif
1830
1831#ifndef OPENSSL_NO_ECDH
1832 EC_KEY *srvr_ecdh = NULL;
1833 EVP_PKEY *clnt_pub_pkey = NULL;
1834 EC_POINT *clnt_ecpoint = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001835 BN_CTX *bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001836 unsigned int psk_len = 0;
1837 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001838#endif
1839
1840 n=s->method->ssl_get_message(s,
1841 SSL3_ST_SR_KEY_EXCH_A,
1842 SSL3_ST_SR_KEY_EXCH_B,
1843 SSL3_MT_CLIENT_KEY_EXCHANGE,
1844 2048, /* ??? */
1845 &ok);
1846
1847 if (!ok) return((int)n);
David Benjamin35c02302014-07-13 04:14:59 -04001848 CBS_init(&client_key_exchange, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001849
1850 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07001851 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001852
David Benjamin14c83e72014-07-13 04:54:57 -04001853 /* If using a PSK key exchange, prepare the pre-shared key. */
Adam Langleyc26c8022014-06-20 12:00:00 -07001854 if (alg_a & SSL_aPSK)
1855 {
David Benjamin35c02302014-07-13 04:14:59 -04001856 CBS psk_identity;
Adam Langleyc26c8022014-06-20 12:00:00 -07001857
David Benjamin35c02302014-07-13 04:14:59 -04001858 /* If using PSK, the ClientKeyExchange contains a
1859 * psk_identity. If PSK, then this is the only field
1860 * in the message. */
1861 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1862 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0))
Adam Langleyc26c8022014-06-20 12:00:00 -07001863 {
David Benjamin35c02302014-07-13 04:14:59 -04001864 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
1865 al = SSL_AD_DECODE_ERROR;
David Benjamin14c83e72014-07-13 04:54:57 -04001866 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001867 }
David Benjamin35c02302014-07-13 04:14:59 -04001868
Adam Langleyc26c8022014-06-20 12:00:00 -07001869 if (s->psk_server_callback == NULL)
1870 {
1871 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_NO_SERVER_CB);
David Benjamin35c02302014-07-13 04:14:59 -04001872 al = SSL_AD_INTERNAL_ERROR;
David Benjamin14c83e72014-07-13 04:54:57 -04001873 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001874 }
1875
David Benjamin35c02302014-07-13 04:14:59 -04001876 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1877 CBS_contains_zero_byte(&psk_identity))
1878 {
1879 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
1880 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin14c83e72014-07-13 04:54:57 -04001881 goto f_err;
David Benjamin35c02302014-07-13 04:14:59 -04001882 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001883
David Benjamin35c02302014-07-13 04:14:59 -04001884 if (!CBS_strdup(&psk_identity, &s->session->psk_identity))
1885 {
1886 al = SSL_AD_INTERNAL_ERROR;
1887 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamin14c83e72014-07-13 04:54:57 -04001888 goto f_err;
David Benjamin35c02302014-07-13 04:14:59 -04001889 }
1890
1891 /* Look up the key for the identity. */
1892 psk_len = s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07001893 if (psk_len > PSK_MAX_PSK_LEN)
1894 {
1895 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
David Benjamin35c02302014-07-13 04:14:59 -04001896 al = SSL_AD_INTERNAL_ERROR;
David Benjamin14c83e72014-07-13 04:54:57 -04001897 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001898 }
1899 else if (psk_len == 0)
1900 {
1901 /* PSK related to the given identity not found */
1902 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamin35c02302014-07-13 04:14:59 -04001903 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
Adam Langleyc26c8022014-06-20 12:00:00 -07001904 goto f_err;
David Benjamin14c83e72014-07-13 04:54:57 -04001905 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001906 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001907
David Benjamin14c83e72014-07-13 04:54:57 -04001908 /* Depending on the key exchange method, compute |premaster_secret| and
David Benjaminbd30f8e2014-08-19 16:02:38 -04001909 * |premaster_secret_len|. */
David Benjamin35c02302014-07-13 04:14:59 -04001910 if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001911 {
David Benjamin35c02302014-07-13 04:14:59 -04001912 CBS encrypted_premaster_secret;
David Benjamin0aa07672014-07-24 15:34:14 -04001913 uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1914 int decrypt_good_mask;
1915 uint8_t version_good;
1916 size_t rsa_size, decrypt_len, premaster_index, j;
Adam Langleyacff73f2014-06-20 12:00:00 -07001917
David Benjaminb9cc33a2014-07-15 00:09:48 -04001918 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1919 if ( (pkey == NULL) ||
1920 (pkey->type != EVP_PKEY_RSA) ||
1921 (pkey->pkey.rsa == NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07001922 {
David Benjaminb9cc33a2014-07-15 00:09:48 -04001923 al=SSL_AD_HANDSHAKE_FAILURE;
1924 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE);
1925 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001926 }
David Benjaminb9cc33a2014-07-15 00:09:48 -04001927 rsa=pkey->pkey.rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07001928
David Benjamincc23df52014-08-03 13:37:47 -04001929 /* TLS and [incidentally] DTLS{0xFEFF} */
David Benjamin35c02302014-07-13 04:14:59 -04001930 if (s->version > SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07001931 {
David Benjamin35c02302014-07-13 04:14:59 -04001932 CBS copy = client_key_exchange;
1933 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1934 &encrypted_premaster_secret) ||
1935 CBS_len(&client_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001936 {
1937 if (!(s->options & SSL_OP_TLS_D5_BUG))
1938 {
Adam Langleyacff73f2014-06-20 12:00:00 -07001939 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001940 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
Adam Langleyacff73f2014-06-20 12:00:00 -07001941 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001942 }
1943 else
David Benjamin35c02302014-07-13 04:14:59 -04001944 encrypted_premaster_secret = copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946 }
David Benjamin35c02302014-07-13 04:14:59 -04001947 else
1948 encrypted_premaster_secret = client_key_exchange;
Adam Langley95c29f32014-06-20 12:00:00 -07001949
David Benjamin0aa07672014-07-24 15:34:14 -04001950 /* Reject overly short RSA keys because we want to be sure that
1951 * the buffer size makes it safe to iterate over the entire size
1952 * of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The actual
1953 * expected size is larger due to RSA padding, but the bound is
1954 * sufficient to be safe. */
1955 rsa_size = RSA_size(rsa);
1956 if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH)
Adam Langley95c29f32014-06-20 12:00:00 -07001957 {
Adam Langleyacff73f2014-06-20 12:00:00 -07001958 al = SSL_AD_DECRYPT_ERROR;
1959 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
1960 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001961 }
1962
Adam Langleyacff73f2014-06-20 12:00:00 -07001963 /* We must not leak whether a decryption failure occurs because
1964 * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
1965 * RFC 2246, section 7.4.7.1). The code follows that advice of
1966 * the TLS RFC and generates a random premaster secret for the
1967 * case that the decrypt fails. See
1968 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
1969 if (RAND_pseudo_bytes(rand_premaster_secret,
1970 sizeof(rand_premaster_secret)) <= 0)
1971 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001972
David Benjamin35c02302014-07-13 04:14:59 -04001973 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0aa07672014-07-24 15:34:14 -04001974 decrypt_buf = OPENSSL_malloc(rsa_size);
1975 if (decrypt_buf == NULL)
David Benjamin35c02302014-07-13 04:14:59 -04001976 {
1977 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
1978 goto err;
1979 }
1980
David Benjamin0aa07672014-07-24 15:34:14 -04001981 /* Decrypt with no padding. PKCS#1 padding will be removed as
1982 * part of the timing-sensitive code below. */
1983 if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
1984 CBS_data(&encrypted_premaster_secret),
1985 CBS_len(&encrypted_premaster_secret),
1986 RSA_NO_PADDING))
1987 {
1988 goto err;
1989 }
1990 if (decrypt_len != rsa_size)
1991 {
1992 /* This should never happen, but do a check so we do not
1993 * read uninitialized memory. */
1994 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
1995 goto err;
1996 }
David Benjamin35c02302014-07-13 04:14:59 -04001997
David Benjamin0aa07672014-07-24 15:34:14 -04001998 /* Remove the PKCS#1 padding and adjust decrypt_len as
1999 * appropriate. decrypt_good_mask will be zero if the premaster
2000 * if good and non-zero otherwise. */
2001 decrypt_good_mask = RSA_message_index_PKCS1_type_2(
2002 decrypt_buf, decrypt_len, &premaster_index);
2003 decrypt_good_mask--;
2004 decrypt_len = decrypt_len - premaster_index;
Adam Langleyacff73f2014-06-20 12:00:00 -07002005
David Benjamin0aa07672014-07-24 15:34:14 -04002006 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
2007 decrypt_good_mask |= decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH;
2008
2009 /* Copy over the unpadded premaster. Whatever the value of
2010 * |decrypt_good_mask|, copy as if the premaster were the right
2011 * length. It is important the memory access pattern be
2012 * constant. */
2013 premaster_secret = BUF_memdup(
2014 decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
2015 SSL_MAX_MASTER_KEY_LENGTH);
2016 if (premaster_secret == NULL)
2017 {
2018 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2019 goto err;
2020 }
2021 OPENSSL_free(decrypt_buf);
2022 decrypt_buf = NULL;
Adam Langleyacff73f2014-06-20 12:00:00 -07002023
2024 /* If the version in the decrypted pre-master secret is correct
2025 * then version_good will be zero. The Klima-Pokorny-Rosa
2026 * extension of Bleichenbacher's attack
2027 * (http://eprint.iacr.org/2003/052/) exploits the version
2028 * number check as a "bad version oracle". Thus version checks
2029 * are done in constant time and are treated like any other
2030 * decryption error. */
David Benjamin35c02302014-07-13 04:14:59 -04002031 version_good = premaster_secret[0] ^ (s->client_version>>8);
2032 version_good |= premaster_secret[1] ^ (s->client_version&0xff);
Adam Langleyacff73f2014-06-20 12:00:00 -07002033
Adam Langleyacff73f2014-06-20 12:00:00 -07002034 /* If any bits in version_good are set then they'll poision
2035 * decrypt_good_mask and cause rand_premaster_secret to be
2036 * used. */
2037 decrypt_good_mask |= version_good;
2038
2039 /* decrypt_good_mask will be zero iff decrypt_len ==
2040 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We
2041 * fold the bottom 32 bits of it with an OR so that the LSB
2042 * will be zero iff everything is good. This assumes that we'll
2043 * never decrypt a value > 2**31 bytes, which seems safe. */
2044 decrypt_good_mask |= decrypt_good_mask >> 16;
2045 decrypt_good_mask |= decrypt_good_mask >> 8;
2046 decrypt_good_mask |= decrypt_good_mask >> 4;
2047 decrypt_good_mask |= decrypt_good_mask >> 2;
2048 decrypt_good_mask |= decrypt_good_mask >> 1;
2049 /* Now select only the LSB and subtract one. If decrypt_len ==
2050 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then
2051 * decrypt_good_mask will be all ones. Otherwise it'll be all
2052 * zeros. */
2053 decrypt_good_mask &= 1;
2054 decrypt_good_mask--;
2055
David Benjamin35c02302014-07-13 04:14:59 -04002056 /* Now copy rand_premaster_secret over premaster_secret using
Adam Langleyacff73f2014-06-20 12:00:00 -07002057 * decrypt_good_mask. */
2058 for (j = 0; j < sizeof(rand_premaster_secret); j++)
Adam Langley95c29f32014-06-20 12:00:00 -07002059 {
David Benjamin35c02302014-07-13 04:14:59 -04002060 premaster_secret[j] = (premaster_secret[j] & decrypt_good_mask) |
Adam Langleyacff73f2014-06-20 12:00:00 -07002061 (rand_premaster_secret[j] & ~decrypt_good_mask);
Adam Langley95c29f32014-06-20 12:00:00 -07002062 }
Adam Langleyacff73f2014-06-20 12:00:00 -07002063
David Benjamin14c83e72014-07-13 04:54:57 -04002064 premaster_secret_len = sizeof(rand_premaster_secret);
Adam Langley95c29f32014-06-20 12:00:00 -07002065 }
Adam Langley95c29f32014-06-20 12:00:00 -07002066#ifndef OPENSSL_NO_DH
David Benjamin0da0e182014-08-19 16:20:28 -04002067 else if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002068 {
David Benjamin35c02302014-07-13 04:14:59 -04002069 CBS dh_Yc;
David Benjamin35c02302014-07-13 04:14:59 -04002070 int dh_len;
David Benjamin35c02302014-07-13 04:14:59 -04002071
2072 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
David Benjaminbd30f8e2014-08-19 16:02:38 -04002073 CBS_len(&dh_Yc) == 0 ||
David Benjamin35c02302014-07-13 04:14:59 -04002074 CBS_len(&client_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002075 {
David Benjamin35c02302014-07-13 04:14:59 -04002076 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2077 al = SSL_R_DECODE_ERROR;
2078 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07002079 }
David Benjamin35c02302014-07-13 04:14:59 -04002080
David Benjamin0da0e182014-08-19 16:20:28 -04002081 if (s->s3->tmp.dh == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002082 {
2083 al=SSL_AD_HANDSHAKE_FAILURE;
2084 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
2085 goto f_err;
2086 }
David Benjamin0da0e182014-08-19 16:20:28 -04002087 dh_srvr=s->s3->tmp.dh;
Adam Langley95c29f32014-06-20 12:00:00 -07002088
David Benjaminbd30f8e2014-08-19 16:02:38 -04002089 pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002090 if (pub == NULL)
2091 {
2092 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
2093 goto err;
2094 }
2095
David Benjamin35c02302014-07-13 04:14:59 -04002096 /* Allocate a buffer for the premaster secret. */
2097 premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
2098 if (premaster_secret == NULL)
2099 {
2100 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2101 goto err;
2102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
David Benjamin35c02302014-07-13 04:14:59 -04002104 dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
2105 if (dh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002106 {
2107 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
2108 BN_clear_free(pub);
2109 goto err;
2110 }
2111
2112 DH_free(s->s3->tmp.dh);
2113 s->s3->tmp.dh=NULL;
David Benjaminbd30f8e2014-08-19 16:02:38 -04002114 BN_clear_free(pub);
Adam Langley95c29f32014-06-20 12:00:00 -07002115 pub=NULL;
David Benjamin14c83e72014-07-13 04:54:57 -04002116
2117 premaster_secret_len = dh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002118 }
Adam Langley95c29f32014-06-20 12:00:00 -07002119#endif
2120
2121#ifndef OPENSSL_NO_ECDH
David Benjamin0da0e182014-08-19 16:20:28 -04002122 else if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002123 {
David Benjamin35c02302014-07-13 04:14:59 -04002124 int field_size = 0, ecdh_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002125 const EC_KEY *tkey;
2126 const EC_GROUP *group;
2127 const BIGNUM *priv_key;
David Benjaminbd30f8e2014-08-19 16:02:38 -04002128 CBS ecdh_Yc;
Adam Langley95c29f32014-06-20 12:00:00 -07002129
2130 /* initialize structures for server's ECDH key pair */
2131 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2132 {
2133 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2134 goto err;
2135 }
2136
David Benjamin0da0e182014-08-19 16:20:28 -04002137 /* Use the ephermeral values we saved when generating the
2138 * ServerKeyExchange msg. */
2139 tkey = s->s3->tmp.ecdh;
Adam Langley95c29f32014-06-20 12:00:00 -07002140
2141 group = EC_KEY_get0_group(tkey);
2142 priv_key = EC_KEY_get0_private_key(tkey);
2143
2144 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2145 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2146 {
2147 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2148 goto err;
2149 }
2150
2151 /* Let's get client's public key */
2152 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2153 {
2154 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2155 goto err;
2156 }
2157
David Benjaminbd30f8e2014-08-19 16:02:38 -04002158 /* Get client's public key from encoded point
2159 * in the ClientKeyExchange message.
2160 */
2161 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
2162 CBS_len(&client_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002163 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002164 al = SSL_AD_DECODE_ERROR;
2165 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
2166 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07002167 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002168
2169 if ((bn_ctx = BN_CTX_new()) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002170 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002171 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2172 goto err;
2173 }
David Benjamin35c02302014-07-13 04:14:59 -04002174
David Benjaminbd30f8e2014-08-19 16:02:38 -04002175 if (!EC_POINT_oct2point(group, clnt_ecpoint,
2176 CBS_data(&ecdh_Yc), CBS_len(&ecdh_Yc), bn_ctx))
2177 {
2178 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
2179 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002180 }
2181
David Benjamin35c02302014-07-13 04:14:59 -04002182 /* Allocate a buffer for both the secret and the PSK. */
Adam Langley95c29f32014-06-20 12:00:00 -07002183 field_size = EC_GROUP_get_degree(group);
2184 if (field_size <= 0)
2185 {
2186 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2187 goto err;
2188 }
David Benjamin35c02302014-07-13 04:14:59 -04002189
2190 ecdh_len = (field_size + 7) / 8;
2191 premaster_secret = OPENSSL_malloc(ecdh_len);
2192 if (premaster_secret == NULL)
2193 {
2194 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2195 goto err;
2196 }
2197
2198 /* Compute the shared pre-master secret */
2199 ecdh_len = ECDH_compute_key(premaster_secret,
2200 ecdh_len, clnt_ecpoint, srvr_ecdh, NULL);
2201 if (ecdh_len <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002202 {
2203 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
2204 goto err;
2205 }
2206
2207 EVP_PKEY_free(clnt_pub_pkey);
2208 EC_POINT_free(clnt_ecpoint);
2209 EC_KEY_free(srvr_ecdh);
2210 BN_CTX_free(bn_ctx);
2211 EC_KEY_free(s->s3->tmp.ecdh);
Adam Langleyc26c8022014-06-20 12:00:00 -07002212 s->s3->tmp.ecdh = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002213
David Benjamin14c83e72014-07-13 04:54:57 -04002214 premaster_secret_len = ecdh_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002215 }
Adam Langley95c29f32014-06-20 12:00:00 -07002216#endif
David Benjamin14c83e72014-07-13 04:54:57 -04002217 else if (alg_k & SSL_kPSK)
2218 {
2219 /* For plain PSK, other_secret is a block of 0s with the same
2220 * length as the pre-shared key. */
2221 premaster_secret_len = psk_len;
2222 premaster_secret = OPENSSL_malloc(premaster_secret_len);
2223 if (premaster_secret == NULL)
2224 {
2225 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2226 goto err;
2227 }
2228 memset(premaster_secret, 0, premaster_secret_len);
2229 }
David Benjamin820c74a2014-07-13 04:22:14 -04002230 else
Adam Langley95c29f32014-06-20 12:00:00 -07002231 {
2232 al=SSL_AD_HANDSHAKE_FAILURE;
2233 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE);
2234 goto f_err;
2235 }
2236
David Benjamin14c83e72014-07-13 04:54:57 -04002237 /* For a PSK cipher suite, the actual pre-master secret is combined with
2238 * the pre-shared key. */
2239 if (alg_a & SSL_aPSK)
2240 {
2241 CBB new_premaster, child;
2242 uint8_t *new_data;
2243 size_t new_len;
2244
2245 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len))
2246 {
2247 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
2248 goto err;
2249 }
2250 if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2251 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
2252 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
2253 !CBB_add_bytes(&child, psk, psk_len) ||
2254 !CBB_finish(&new_premaster, &new_data, &new_len))
2255 {
2256 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
2257 CBB_cleanup(&new_premaster);
2258 goto err;
2259 }
2260
2261 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2262 OPENSSL_free(premaster_secret);
2263 premaster_secret = new_data;
2264 premaster_secret_len = new_len;
2265 }
David Benjamin14c83e72014-07-13 04:54:57 -04002266
2267 /* Compute the master secret */
2268 s->session->master_key_length = s->method->ssl3_enc
2269 ->generate_master_secret(s,
2270 s->session->master_key, premaster_secret, premaster_secret_len);
2271
2272 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
2273 OPENSSL_free(premaster_secret);
David Benjaminbd30f8e2014-08-19 16:02:38 -04002274 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002275f_err:
2276 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002277err:
David Benjamin35c02302014-07-13 04:14:59 -04002278 if (premaster_secret)
David Benjamin14c83e72014-07-13 04:54:57 -04002279 {
2280 if (premaster_secret_len)
2281 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
David Benjamin35c02302014-07-13 04:14:59 -04002282 OPENSSL_free(premaster_secret);
David Benjamin14c83e72014-07-13 04:54:57 -04002283 }
David Benjamin0aa07672014-07-24 15:34:14 -04002284 if (decrypt_buf)
2285 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07002286#ifndef OPENSSL_NO_ECDH
2287 EVP_PKEY_free(clnt_pub_pkey);
2288 EC_POINT_free(clnt_ecpoint);
2289 if (srvr_ecdh != NULL)
2290 EC_KEY_free(srvr_ecdh);
2291 BN_CTX_free(bn_ctx);
2292#endif
2293 return(-1);
2294 }
2295
2296int ssl3_get_cert_verify(SSL *s)
2297 {
2298 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002299 int al,ok,ret=0;
2300 long n;
David Benjamin6897dbe2014-07-12 20:18:28 -04002301 CBS certificate_verify, signature;
2302 int type = 0;
David Benjamin6553b372014-07-22 14:11:30 -04002303 X509 *peer = s->session->peer;
Adam Langley95c29f32014-06-20 12:00:00 -07002304 const EVP_MD *md = NULL;
2305 EVP_MD_CTX mctx;
David Benjamin6897dbe2014-07-12 20:18:28 -04002306
Adam Langley95c29f32014-06-20 12:00:00 -07002307 EVP_MD_CTX_init(&mctx);
2308
David Benjamin6553b372014-07-22 14:11:30 -04002309 /* Determine if a CertificateVerify message is expected at all. It is
2310 * important that this be determined before ssl_get_message is called,
2311 * so as not to process the ChangeCipherSpec message early. */
2312 if (peer != NULL)
2313 {
2314 pkey = X509_get_pubkey(peer);
2315 type = X509_certificate_type(peer,pkey);
2316 }
2317 if (!(type & EVP_PKT_SIGN))
2318 {
2319 ret = 1;
David Benjamin734fca02014-08-05 13:06:20 -04002320 goto done_with_buffer;
David Benjamin6553b372014-07-22 14:11:30 -04002321 }
2322
Adam Langley95c29f32014-06-20 12:00:00 -07002323 n=s->method->ssl_get_message(s,
2324 SSL3_ST_SR_CERT_VRFY_A,
2325 SSL3_ST_SR_CERT_VRFY_B,
David Benjamin6553b372014-07-22 14:11:30 -04002326 SSL3_MT_CERTIFICATE_VERIFY,
Adam Langley887b1c32014-07-24 18:00:06 -07002327 SSL3_RT_MAX_PLAIN_LENGTH,
Adam Langley95c29f32014-06-20 12:00:00 -07002328 &ok);
2329
David Benjamin734fca02014-08-05 13:06:20 -04002330 if (!ok)
2331 {
2332 ret = (int)n;
2333 goto done;
2334 }
Adam Langley95c29f32014-06-20 12:00:00 -07002335
David Benjamin6897dbe2014-07-12 20:18:28 -04002336 CBS_init(&certificate_verify, s->init_msg, n);
2337
2338 /* We now have a signature that we need to verify. */
2339 /* TODO(davidben): This should share code with
David Benjamin8f8040d2014-07-14 19:14:46 -04002340 * ssl3_get_server_key_exchange. */
David Benjamin05da6e12014-07-12 20:42:55 -04002341
Adam Langley3213bed2014-06-23 12:26:07 -07002342 if (SSL_USE_SIGALGS(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002343 {
David Benjamin05da6e12014-07-12 20:42:55 -04002344 if (!tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey))
David Benjamin6897dbe2014-07-12 20:18:28 -04002345 goto f_err;
Adam Langley3213bed2014-06-23 12:26:07 -07002346 }
David Benjamin6897dbe2014-07-12 20:18:28 -04002347
2348 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
2349 CBS_len(&certificate_verify) != 0)
Adam Langley3213bed2014-06-23 12:26:07 -07002350 {
David Benjamin6897dbe2014-07-12 20:18:28 -04002351 al = SSL_AD_DECODE_ERROR;
2352 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002353 goto f_err;
2354 }
2355
2356 if (SSL_USE_SIGALGS(s))
2357 {
Adam Langley37a623c2014-07-18 09:28:40 -07002358 size_t hdatalen;
2359 const uint8_t *hdata;
2360 if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen))
Adam Langley95c29f32014-06-20 12:00:00 -07002361 {
2362 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR);
2363 al=SSL_AD_INTERNAL_ERROR;
2364 goto f_err;
2365 }
Adam Langley95c29f32014-06-20 12:00:00 -07002366 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
2367 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen))
2368 {
2369 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_EVP_LIB);
2370 al=SSL_AD_INTERNAL_ERROR;
2371 goto f_err;
2372 }
2373
David Benjamin6897dbe2014-07-12 20:18:28 -04002374 if (EVP_VerifyFinal(&mctx,
2375 CBS_data(&signature), CBS_len(&signature),
2376 pkey) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002377 {
2378 al=SSL_AD_DECRYPT_ERROR;
2379 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
2380 goto f_err;
2381 }
2382 }
2383 else
Adam Langley95c29f32014-06-20 12:00:00 -07002384 if (pkey->type == EVP_PKEY_RSA)
2385 {
David Benjamin6897dbe2014-07-12 20:18:28 -04002386 if (!RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2387 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2388 CBS_data(&signature), CBS_len(&signature),
2389 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07002390 {
David Benjamin6897dbe2014-07-12 20:18:28 -04002391 al = SSL_AD_DECRYPT_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002392 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_RSA_SIGNATURE);
2393 goto f_err;
2394 }
2395 }
2396 else
Adam Langley95c29f32014-06-20 12:00:00 -07002397#ifndef OPENSSL_NO_ECDSA
2398 if (pkey->type == EVP_PKEY_EC)
2399 {
David Benjamin6897dbe2014-07-12 20:18:28 -04002400 if (!ECDSA_verify(pkey->save_type,
2401 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2402 SHA_DIGEST_LENGTH,
2403 CBS_data(&signature), CBS_len(&signature),
2404 pkey->pkey.ec))
Adam Langley95c29f32014-06-20 12:00:00 -07002405 {
2406 /* bad signature */
David Benjamin6897dbe2014-07-12 20:18:28 -04002407 al = SSL_AD_DECRYPT_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002408 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_ECDSA_SIGNATURE);
2409 goto f_err;
2410 }
2411 }
2412 else
2413#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002414 {
2415 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR);
2416 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2417 goto f_err;
2418 }
2419
2420
2421 ret=1;
2422 if (0)
2423 {
2424f_err:
2425 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2426 }
David Benjamin734fca02014-08-05 13:06:20 -04002427done_with_buffer:
2428 /* There is no more need for the handshake buffer. */
Adam Langley95c29f32014-06-20 12:00:00 -07002429 if (s->s3->handshake_buffer)
2430 {
2431 BIO_free(s->s3->handshake_buffer);
2432 s->s3->handshake_buffer = NULL;
2433 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
2434 }
David Benjamin734fca02014-08-05 13:06:20 -04002435done:
Adam Langley95c29f32014-06-20 12:00:00 -07002436 EVP_MD_CTX_cleanup(&mctx);
2437 EVP_PKEY_free(pkey);
2438 return(ret);
2439 }
2440
2441int ssl3_get_client_certificate(SSL *s)
2442 {
2443 int i,ok,al,ret= -1;
2444 X509 *x=NULL;
David Benjamind5b1f842014-07-11 23:43:48 -04002445 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07002446 STACK_OF(X509) *sk=NULL;
Adam Langley75872532014-06-20 12:00:00 -07002447 SHA256_CTX sha256;
David Benjamind5b1f842014-07-11 23:43:48 -04002448 CBS certificate_msg, certificate_list;
2449 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002450
2451 n=s->method->ssl_get_message(s,
2452 SSL3_ST_SR_CERT_A,
2453 SSL3_ST_SR_CERT_B,
2454 -1,
2455 s->max_cert_list,
2456 &ok);
2457
2458 if (!ok) return((int)n);
2459
2460 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
2461 {
2462 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
2463 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2464 {
2465 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2466 al=SSL_AD_HANDSHAKE_FAILURE;
2467 goto f_err;
2468 }
2469 /* If tls asked for a client cert, the client must return a 0 list */
2470 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
2471 {
2472 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2473 al=SSL_AD_UNEXPECTED_MESSAGE;
2474 goto f_err;
2475 }
2476 s->s3->tmp.reuse_message=1;
2477 return(1);
2478 }
2479
2480 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
2481 {
2482 al=SSL_AD_UNEXPECTED_MESSAGE;
2483 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE);
2484 goto f_err;
2485 }
David Benjamind5b1f842014-07-11 23:43:48 -04002486
David Benjamin51b1f742014-07-12 16:31:12 -04002487 CBS_init(&certificate_msg, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002488
2489 if ((sk=sk_X509_new_null()) == NULL)
2490 {
2491 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2492 goto err;
2493 }
2494
David Benjamind5b1f842014-07-11 23:43:48 -04002495 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
2496 CBS_len(&certificate_msg) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002497 {
David Benjamind5b1f842014-07-11 23:43:48 -04002498 al = SSL_AD_DECODE_ERROR;
2499 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002500 goto f_err;
2501 }
David Benjamind5b1f842014-07-11 23:43:48 -04002502
2503 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002504 {
David Benjamind5b1f842014-07-11 23:43:48 -04002505 CBS certificate;
2506 const uint8_t *data;
2507
2508 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07002509 {
David Benjamind5b1f842014-07-11 23:43:48 -04002510 al = SSL_AD_DECODE_ERROR;
2511 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002512 goto f_err;
2513 }
David Benjamind5b1f842014-07-11 23:43:48 -04002514 if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs)
Adam Langley75872532014-06-20 12:00:00 -07002515 {
2516 /* If this is the first certificate, and we don't want
2517 * to keep peer certificates in memory, then we hash it
2518 * right away. */
2519 SHA256_Init(&sha256);
David Benjamind5b1f842014-07-11 23:43:48 -04002520 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
Adam Langley75872532014-06-20 12:00:00 -07002521 SHA256_Final(s->session->peer_sha256, &sha256);
2522 s->session->peer_sha256_valid = 1;
2523 }
David Benjamind5b1f842014-07-11 23:43:48 -04002524 is_first_certificate = 0;
2525 data = CBS_data(&certificate);
2526 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07002527 if (x == NULL)
2528 {
David Benjamind5b1f842014-07-11 23:43:48 -04002529 al = SSL_AD_BAD_CERTIFICATE;
Adam Langley95c29f32014-06-20 12:00:00 -07002530 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
David Benjamind5b1f842014-07-11 23:43:48 -04002531 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07002532 }
David Benjamind5b1f842014-07-11 23:43:48 -04002533 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07002534 {
David Benjamind5b1f842014-07-11 23:43:48 -04002535 al = SSL_AD_INTERNAL_ERROR;
2536 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_INTERNAL_ERROR);
2537 goto f_err;
2538 }
2539 if (CBS_len(&certificate) != 0)
2540 {
2541 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002542 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH);
2543 goto f_err;
2544 }
2545 if (!sk_X509_push(sk,x))
2546 {
2547 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2548 goto err;
2549 }
David Benjamind5b1f842014-07-11 23:43:48 -04002550 x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002551 }
2552
2553 if (sk_X509_num(sk) <= 0)
2554 {
2555 /* TLS does not mind 0 certs returned */
2556 if (s->version == SSL3_VERSION)
2557 {
2558 al=SSL_AD_HANDSHAKE_FAILURE;
2559 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED);
2560 goto f_err;
2561 }
2562 /* Fail for TLS only if we required a certificate */
2563 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2564 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2565 {
2566 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2567 al=SSL_AD_HANDSHAKE_FAILURE;
2568 goto f_err;
2569 }
2570 /* No client certificate so digest cached records */
2571 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
2572 {
2573 al=SSL_AD_INTERNAL_ERROR;
2574 goto f_err;
2575 }
2576 }
2577 else
2578 {
2579 i=ssl_verify_cert_chain(s,sk);
2580 if (i <= 0)
2581 {
2582 al=ssl_verify_alarm_type(s->verify_result);
2583 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
2584 goto f_err;
2585 }
2586 }
2587
2588 if (s->session->peer != NULL) /* This should not be needed */
2589 X509_free(s->session->peer);
2590 s->session->peer=sk_X509_shift(sk);
2591 s->session->verify_result = s->verify_result;
2592
2593 /* With the current implementation, sess_cert will always be NULL
2594 * when we arrive here. */
2595 if (s->session->sess_cert == NULL)
2596 {
2597 s->session->sess_cert = ssl_sess_cert_new();
2598 if (s->session->sess_cert == NULL)
2599 {
2600 OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
2601 goto err;
2602 }
2603 }
2604 if (s->session->sess_cert->cert_chain != NULL)
2605 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2606 s->session->sess_cert->cert_chain=sk;
2607 /* Inconsistency alert: cert_chain does *not* include the
2608 * peer's own certificate, while we do include it in s3_clnt.c */
2609
2610 sk=NULL;
2611
2612 ret=1;
2613 if (0)
2614 {
2615f_err:
2616 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2617 }
2618err:
2619 if (x != NULL) X509_free(x);
2620 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2621 return(ret);
2622 }
2623
2624int ssl3_send_server_certificate(SSL *s)
2625 {
2626 CERT_PKEY *cpk;
2627
2628 if (s->state == SSL3_ST_SW_CERT_A)
2629 {
2630 cpk=ssl_get_server_send_pkey(s);
2631 if (cpk == NULL)
2632 {
David Benjamind26aea62014-07-12 00:13:56 -04002633 OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR);
2634 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -07002635 }
2636
2637 ssl3_output_cert_chain(s,cpk);
2638 s->state=SSL3_ST_SW_CERT_B;
2639 }
2640
2641 /* SSL3_ST_SW_CERT_B */
2642 return ssl_do_write(s);
2643 }
2644
Adam Langley95c29f32014-06-20 12:00:00 -07002645/* send a new session ticket (not necessarily for a new session) */
2646int ssl3_send_newsession_ticket(SSL *s)
2647 {
2648 if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
2649 {
2650 unsigned char *p, *senc, *macstart;
2651 const unsigned char *const_p;
2652 int len, slen_full, slen;
2653 SSL_SESSION *sess;
2654 unsigned int hlen;
2655 EVP_CIPHER_CTX ctx;
2656 HMAC_CTX hctx;
2657 SSL_CTX *tctx = s->initial_ctx;
2658 unsigned char iv[EVP_MAX_IV_LENGTH];
2659 unsigned char key_name[16];
2660
2661 /* get session encoding length */
2662 slen_full = i2d_SSL_SESSION(s->session, NULL);
2663 /* Some length values are 16 bits, so forget it if session is
2664 * too long
2665 */
2666 if (slen_full > 0xFF00)
2667 return -1;
2668 senc = OPENSSL_malloc(slen_full);
2669 if (!senc)
2670 return -1;
2671 p = senc;
2672 i2d_SSL_SESSION(s->session, &p);
2673
2674 /* create a fresh copy (not shared with other threads) to clean up */
2675 const_p = senc;
2676 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
2677 if (sess == NULL)
2678 {
2679 OPENSSL_free(senc);
2680 return -1;
2681 }
2682 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
2683
2684 slen = i2d_SSL_SESSION(sess, NULL);
2685 if (slen > slen_full) /* shouldn't ever happen */
2686 {
2687 OPENSSL_free(senc);
2688 return -1;
2689 }
2690 p = senc;
2691 i2d_SSL_SESSION(sess, &p);
2692 SSL_SESSION_free(sess);
2693
2694 /* Grow buffer if need be: the length calculation is as
2695 * follows handshake_header_length +
2696 * 4 (ticket lifetime hint) + 2 (ticket length) +
2697 * 16 (key name) + max_iv_len (iv length) +
2698 * session_length + max_enc_block_size (max encrypted session
2699 * length) + max_md_size (HMAC).
2700 */
2701 if (!BUF_MEM_grow(s->init_buf,
2702 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
2703 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
2704 return -1;
2705 p = ssl_handshake_start(s);
2706 EVP_CIPHER_CTX_init(&ctx);
2707 HMAC_CTX_init(&hctx);
2708 /* Initialize HMAC and cipher contexts. If callback present
2709 * it does all the work otherwise use generated values
2710 * from parent ctx.
2711 */
2712 if (tctx->tlsext_ticket_key_cb)
2713 {
2714 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
2715 &hctx, 1) < 0)
2716 {
2717 OPENSSL_free(senc);
2718 return -1;
2719 }
2720 }
2721 else
2722 {
2723 RAND_pseudo_bytes(iv, 16);
2724 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2725 tctx->tlsext_tick_aes_key, iv);
2726 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2727 tlsext_tick_md(), NULL);
2728 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2729 }
2730
2731 /* Ticket lifetime hint (advisory only):
2732 * We leave this unspecified for resumed session (for simplicity),
2733 * and guess that tickets for new sessions will live as long
2734 * as their sessions. */
2735 l2n(s->hit ? 0 : s->session->timeout, p);
2736
2737 /* Skip ticket length for now */
2738 p += 2;
2739 /* Output key name */
2740 macstart = p;
2741 memcpy(p, key_name, 16);
2742 p += 16;
2743 /* output IV */
2744 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2745 p += EVP_CIPHER_CTX_iv_length(&ctx);
2746 /* Encrypt session data */
2747 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2748 p += len;
2749 EVP_EncryptFinal_ex(&ctx, p, &len);
2750 p += len;
2751 EVP_CIPHER_CTX_cleanup(&ctx);
2752
2753 HMAC_Update(&hctx, macstart, p - macstart);
2754 HMAC_Final(&hctx, p, &hlen);
2755 HMAC_CTX_cleanup(&hctx);
2756
2757 p += hlen;
2758 /* Now write out lengths: p points to end of data written */
2759 /* Total length */
2760 len = p - ssl_handshake_start(s);
2761 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
2762 /* Skip ticket lifetime hint */
2763 p = ssl_handshake_start(s) + 4;
2764 s2n(len - 6, p);
2765 s->state=SSL3_ST_SW_SESSION_TICKET_B;
2766 OPENSSL_free(senc);
2767 }
2768
2769 /* SSL3_ST_SW_SESSION_TICKET_B */
2770 return ssl_do_write(s);
2771 }
2772
2773int ssl3_send_cert_status(SSL *s)
2774 {
2775 if (s->state == SSL3_ST_SW_CERT_STATUS_A)
2776 {
2777 unsigned char *p;
2778 /* Grow buffer if need be: the length calculation is as
2779 * follows 1 (message type) + 3 (message length) +
2780 * 1 (ocsp response type) + 3 (ocsp response length)
2781 * + (ocsp response)
2782 */
2783 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
2784 return -1;
2785
2786 p=(unsigned char *)s->init_buf->data;
2787
2788 /* do the header */
2789 *(p++)=SSL3_MT_CERTIFICATE_STATUS;
2790 /* message length */
2791 l2n3(s->tlsext_ocsp_resplen + 4, p);
2792 /* status type */
2793 *(p++)= s->tlsext_status_type;
2794 /* length of OCSP response */
2795 l2n3(s->tlsext_ocsp_resplen, p);
2796 /* actual response */
2797 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2798 /* number of bytes to write */
2799 s->init_num = 8 + s->tlsext_ocsp_resplen;
2800 s->state=SSL3_ST_SW_CERT_STATUS_B;
2801 s->init_off = 0;
2802 }
2803
2804 /* SSL3_ST_SW_CERT_STATUS_B */
2805 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2806 }
2807
2808# ifndef OPENSSL_NO_NEXTPROTONEG
2809/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2810 * sets the next_proto member in s if found */
2811int ssl3_get_next_proto(SSL *s)
2812 {
2813 int ok;
Adam Langley95c29f32014-06-20 12:00:00 -07002814 long n;
David Benjaminced551f2014-07-11 23:16:16 -04002815 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002816
2817 /* Clients cannot send a NextProtocol message if we didn't see the
2818 * extension in their ClientHello */
2819 if (!s->s3->next_proto_neg_seen)
2820 {
2821 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2822 return -1;
2823 }
2824
2825 n=s->method->ssl_get_message(s,
2826 SSL3_ST_SR_NEXT_PROTO_A,
2827 SSL3_ST_SR_NEXT_PROTO_B,
2828 SSL3_MT_NEXT_PROTO,
2829 514, /* See the payload format below */
2830 &ok);
2831
2832 if (!ok)
2833 return((int)n);
2834
2835 /* s->state doesn't reflect whether ChangeCipherSpec has been received
2836 * in this handshake, but s->s3->change_cipher_spec does (will be reset
David Benjamin86271ee2014-07-21 16:14:03 -04002837 * by ssl3_get_finished).
2838 * TODO(davidben): Is this check now redundant with
2839 * SSL3_FLAGS_EXPECT_CCS? */
Adam Langley95c29f32014-06-20 12:00:00 -07002840 if (!s->s3->change_cipher_spec)
2841 {
2842 OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2843 return -1;
2844 }
2845
David Benjamin51b1f742014-07-12 16:31:12 -04002846 CBS_init(&next_protocol, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002847
2848 /* The payload looks like:
2849 * uint8 proto_len;
2850 * uint8 proto[proto_len];
2851 * uint8 padding_len;
2852 * uint8 padding[padding_len];
2853 */
David Benjaminced551f2014-07-11 23:16:16 -04002854 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2855 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2856 CBS_len(&next_protocol) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002857 return 0;
2858
David Benjaminced551f2014-07-11 23:16:16 -04002859 if (!CBS_stow(&selected_protocol,
2860 &s->next_proto_negotiated,
2861 &s->next_proto_negotiated_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002862 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002863
2864 return 1;
2865 }
2866# endif
2867
Adam Langley1258b6a2014-06-20 12:00:00 -07002868/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
2869int ssl3_get_channel_id(SSL *s)
2870 {
2871 int ret = -1, ok;
2872 long n;
David Benjamin9a6232f2014-07-11 23:07:47 -04002873 const uint8_t *p;
2874 uint16_t extension_type, expected_extension_type;
Adam Langley1258b6a2014-06-20 12:00:00 -07002875 EC_GROUP* p256 = NULL;
2876 EC_KEY* key = NULL;
2877 EC_POINT* point = NULL;
2878 ECDSA_SIG sig;
2879 BIGNUM x, y;
David Benjamin9a6232f2014-07-11 23:07:47 -04002880 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002881
2882 if (s->state == SSL3_ST_SR_CHANNEL_ID_A && s->init_num == 0)
2883 {
2884 /* The first time that we're called we take the current
2885 * handshake hash and store it. */
2886 EVP_MD_CTX md_ctx;
2887 unsigned int len;
2888
2889 EVP_MD_CTX_init(&md_ctx);
2890 EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL);
2891 if (!tls1_channel_id_hash(&md_ctx, s))
2892 return -1;
2893 len = sizeof(s->s3->tlsext_channel_id);
2894 EVP_DigestFinal(&md_ctx, s->s3->tlsext_channel_id, &len);
2895 EVP_MD_CTX_cleanup(&md_ctx);
2896 }
2897
2898 n = s->method->ssl_get_message(s,
2899 SSL3_ST_SR_CHANNEL_ID_A,
2900 SSL3_ST_SR_CHANNEL_ID_B,
2901 SSL3_MT_ENCRYPTED_EXTENSIONS,
2902 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
2903 &ok);
2904
2905 if (!ok)
2906 return((int)n);
2907
2908 ssl3_finish_mac(s, (unsigned char*)s->init_buf->data, s->init_num + 4);
2909
2910 /* s->state doesn't reflect whether ChangeCipherSpec has been received
2911 * in this handshake, but s->s3->change_cipher_spec does (will be reset
David Benjamin86271ee2014-07-21 16:14:03 -04002912 * by ssl3_get_finished).
2913 * TODO(davidben): Is this check now redundant with
2914 * SSL3_FLAGS_EXPECT_CCS? */
Adam Langley1258b6a2014-06-20 12:00:00 -07002915 if (!s->s3->change_cipher_spec)
2916 {
2917 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
2918 return -1;
2919 }
2920
David Benjamin51b1f742014-07-12 16:31:12 -04002921 CBS_init(&encrypted_extensions, s->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002922
David Benjamin9a6232f2014-07-11 23:07:47 -04002923 /* EncryptedExtensions could include multiple extensions, but
2924 * the only extension that could be negotiated is ChannelID,
2925 * so there can only be one entry.
2926 *
2927 * The payload looks like:
Adam Langley1258b6a2014-06-20 12:00:00 -07002928 * uint16 extension_type
2929 * uint16 extension_len;
2930 * uint8 x[32];
2931 * uint8 y[32];
2932 * uint8 r[32];
2933 * uint8 s[32];
2934 */
Adam Langley1258b6a2014-06-20 12:00:00 -07002935 expected_extension_type = TLSEXT_TYPE_channel_id;
2936 if (s->s3->tlsext_channel_id_new)
2937 expected_extension_type = TLSEXT_TYPE_channel_id_new;
2938
David Benjamin9a6232f2014-07-11 23:07:47 -04002939 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2940 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2941 CBS_len(&encrypted_extensions) != 0 ||
2942 extension_type != expected_extension_type ||
2943 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE)
Adam Langley1258b6a2014-06-20 12:00:00 -07002944 {
2945 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
2946 return -1;
2947 }
2948
2949 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2950 if (!p256)
2951 {
2952 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
2953 return -1;
2954 }
2955
2956 BN_init(&x);
2957 BN_init(&y);
2958 sig.r = BN_new();
2959 sig.s = BN_new();
2960
David Benjamin9a6232f2014-07-11 23:07:47 -04002961 p = CBS_data(&extension);
Adam Langley1258b6a2014-06-20 12:00:00 -07002962 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2963 BN_bin2bn(p + 32, 32, &y) == NULL ||
2964 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2965 BN_bin2bn(p + 96, 32, sig.s) == NULL)
2966 goto err;
2967
2968 point = EC_POINT_new(p256);
2969 if (!point ||
2970 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL))
2971 goto err;
2972
2973 key = EC_KEY_new();
2974 if (!key ||
2975 !EC_KEY_set_group(key, p256) ||
2976 !EC_KEY_set_public_key(key, point))
2977 goto err;
2978
2979 /* We stored the handshake hash in |tlsext_channel_id| the first time
2980 * that we were called. */
David Benjamin3a66e282014-08-24 02:22:29 -04002981 if (!ECDSA_do_verify(s->s3->tlsext_channel_id, SHA256_DIGEST_LENGTH, &sig, key))
2982 {
Adam Langley1258b6a2014-06-20 12:00:00 -07002983 OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2984 s->s3->tlsext_channel_id_valid = 0;
2985 goto err;
David Benjamin3a66e282014-08-24 02:22:29 -04002986 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002987
2988 memcpy(s->s3->tlsext_channel_id, p, 64);
2989 ret = 1;
2990
2991err:
2992 BN_free(&x);
2993 BN_free(&y);
2994 BN_free(sig.r);
2995 BN_free(sig.s);
2996 if (key)
2997 EC_KEY_free(key);
2998 if (point)
2999 EC_POINT_free(point);
3000 if (p256)
3001 EC_GROUP_free(p256);
3002 return ret;
3003 }
3004