blob: f1698b7f5ef3f4d8422a7465bef5a0c4b35aa72c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
134 *
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
138 *
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
143 *
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
149 */
150
David Benjamin81ea0bf2014-11-23 04:20:17 -0500151#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
155#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/rand.h>
158#include <openssl/obj.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400159#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/evp.h>
161#include <openssl/mem.h>
162#include <openssl/md5.h>
163#include <openssl/dh.h>
164#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/x509.h>
166
David Benjamin2ee94aa2015-04-07 22:38:30 -0400167#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700168#include "../crypto/dh/internal.h"
169
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langley1bea1732014-12-17 19:06:57 -0800171int ssl3_connect(SSL *s) {
172 BUF_MEM *buf = NULL;
173 void (*cb)(const SSL *ssl, int type, int val) = NULL;
174 int ret = -1;
175 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500176
Adam Langley1bea1732014-12-17 19:06:57 -0800177 assert(s->handshake_func == ssl3_connect);
178 assert(!s->server);
179 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langley1bea1732014-12-17 19:06:57 -0800181 ERR_clear_error();
182 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley1bea1732014-12-17 19:06:57 -0800184 if (s->info_callback != NULL) {
185 cb = s->info_callback;
186 } else if (s->ctx->info_callback != NULL) {
187 cb = s->ctx->info_callback;
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley1bea1732014-12-17 19:06:57 -0800190 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langley1bea1732014-12-17 19:06:57 -0800192 for (;;) {
193 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley1bea1732014-12-17 19:06:57 -0800195 switch (s->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800196 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500197 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800198 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langley1bea1732014-12-17 19:06:57 -0800201 if (s->init_buf == NULL) {
202 buf = BUF_MEM_new();
203 if (buf == NULL ||
204 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
205 ret = -1;
206 goto end;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley1bea1732014-12-17 19:06:57 -0800209 s->init_buf = buf;
210 buf = NULL;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin6a08da22015-05-08 22:58:12 -0400213 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800214 ret = -1;
215 goto end;
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley1bea1732014-12-17 19:06:57 -0800218 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langley1bea1732014-12-17 19:06:57 -0800220 if (!ssl3_init_finished_mac(s)) {
221 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
222 ret = -1;
223 goto end;
224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langley1bea1732014-12-17 19:06:57 -0800226 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800227 s->init_num = 0;
228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langley1bea1732014-12-17 19:06:57 -0800230 case SSL3_ST_CW_CLNT_HELLO_A:
231 case SSL3_ST_CW_CLNT_HELLO_B:
232 s->shutdown = 0;
233 ret = ssl3_send_client_hello(s);
234 if (ret <= 0) {
235 goto end;
236 }
237 s->state = SSL3_ST_CR_SRVR_HELLO_A;
238 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley1bea1732014-12-17 19:06:57 -0800240 /* turn on buffering for the next lot of output */
241 if (s->bbio != s->wbio) {
242 s->wbio = BIO_push(s->bbio, s->wbio);
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley1bea1732014-12-17 19:06:57 -0800245 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley1bea1732014-12-17 19:06:57 -0800247 case SSL3_ST_CR_SRVR_HELLO_A:
248 case SSL3_ST_CR_SRVR_HELLO_B:
249 ret = ssl3_get_server_hello(s);
250 if (ret <= 0) {
251 goto end;
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley1bea1732014-12-17 19:06:57 -0800254 if (s->hit) {
255 s->state = SSL3_ST_CR_CHANGE;
256 if (s->tlsext_ticket_expected) {
257 /* receive renewed session ticket */
258 s->state = SSL3_ST_CR_SESSION_TICKET_A;
259 }
260 } else {
261 s->state = SSL3_ST_CR_CERT_A;
262 }
263 s->init_num = 0;
264 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400265
Adam Langley1bea1732014-12-17 19:06:57 -0800266 case SSL3_ST_CR_CERT_A:
267 case SSL3_ST_CR_CERT_B:
268 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
269 ret = ssl3_get_server_certificate(s);
270 if (ret <= 0) {
271 goto end;
272 }
273 if (s->s3->tmp.certificate_status_expected) {
274 s->state = SSL3_ST_CR_CERT_STATUS_A;
275 } else {
276 s->state = SSL3_ST_CR_KEY_EXCH_A;
277 }
278 } else {
279 skip = 1;
280 s->state = SSL3_ST_CR_KEY_EXCH_A;
281 }
282 s->init_num = 0;
283 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langley1bea1732014-12-17 19:06:57 -0800285 case SSL3_ST_CR_KEY_EXCH_A:
286 case SSL3_ST_CR_KEY_EXCH_B:
287 ret = ssl3_get_server_key_exchange(s);
288 if (ret <= 0) {
289 goto end;
290 }
291 s->state = SSL3_ST_CR_CERT_REQ_A;
292 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langley1bea1732014-12-17 19:06:57 -0800294 /* at this point we check that we have the
295 * required stuff from the server */
296 if (!ssl3_check_cert_and_algorithm(s)) {
297 ret = -1;
298 goto end;
299 }
300 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langley1bea1732014-12-17 19:06:57 -0800302 case SSL3_ST_CR_CERT_REQ_A:
303 case SSL3_ST_CR_CERT_REQ_B:
304 ret = ssl3_get_certificate_request(s);
305 if (ret <= 0) {
306 goto end;
307 }
308 s->state = SSL3_ST_CR_SRVR_DONE_A;
309 s->init_num = 0;
310 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley1bea1732014-12-17 19:06:57 -0800312 case SSL3_ST_CR_SRVR_DONE_A:
313 case SSL3_ST_CR_SRVR_DONE_B:
314 ret = ssl3_get_server_done(s);
315 if (ret <= 0) {
316 goto end;
317 }
318 if (s->s3->tmp.cert_req) {
319 s->state = SSL3_ST_CW_CERT_A;
320 } else {
321 s->state = SSL3_ST_CW_KEY_EXCH_A;
322 }
323 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langley1bea1732014-12-17 19:06:57 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CW_CERT_A:
328 case SSL3_ST_CW_CERT_B:
329 case SSL3_ST_CW_CERT_C:
330 case SSL3_ST_CW_CERT_D:
331 ret = ssl3_send_client_certificate(s);
332 if (ret <= 0) {
333 goto end;
334 }
335 s->state = SSL3_ST_CW_KEY_EXCH_A;
336 s->init_num = 0;
337 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langley1bea1732014-12-17 19:06:57 -0800339 case SSL3_ST_CW_KEY_EXCH_A:
340 case SSL3_ST_CW_KEY_EXCH_B:
341 ret = ssl3_send_client_key_exchange(s);
342 if (ret <= 0) {
343 goto end;
344 }
345 /* For TLS, cert_req is set to 2, so a cert chain
346 * of nothing is sent, but no verify packet is sent */
347 if (s->s3->tmp.cert_req == 1) {
348 s->state = SSL3_ST_CW_CERT_VRFY_A;
349 } else {
350 s->state = SSL3_ST_CW_CHANGE_A;
351 s->s3->change_cipher_spec = 0;
352 }
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley1bea1732014-12-17 19:06:57 -0800354 s->init_num = 0;
355 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langley1bea1732014-12-17 19:06:57 -0800357 case SSL3_ST_CW_CERT_VRFY_A:
358 case SSL3_ST_CW_CERT_VRFY_B:
359 ret = ssl3_send_cert_verify(s);
360 if (ret <= 0) {
361 goto end;
362 }
363 s->state = SSL3_ST_CW_CHANGE_A;
364 s->init_num = 0;
365 s->s3->change_cipher_spec = 0;
366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley1bea1732014-12-17 19:06:57 -0800368 case SSL3_ST_CW_CHANGE_A:
369 case SSL3_ST_CW_CHANGE_B:
370 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
371 SSL3_ST_CW_CHANGE_B);
372 if (ret <= 0) {
373 goto end;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 s->state = SSL3_ST_CW_FINISHED_A;
377 if (s->s3->tlsext_channel_id_valid) {
378 s->state = SSL3_ST_CW_CHANNEL_ID_A;
379 }
380 if (s->s3->next_proto_neg_seen) {
381 s->state = SSL3_ST_CW_NEXT_PROTO_A;
382 }
383 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langley1bea1732014-12-17 19:06:57 -0800385 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400386 if (!s->enc_method->setup_key_block(s) ||
387 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800388 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
389 ret = -1;
390 goto end;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langley1bea1732014-12-17 19:06:57 -0800393 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley1bea1732014-12-17 19:06:57 -0800395 case SSL3_ST_CW_NEXT_PROTO_A:
396 case SSL3_ST_CW_NEXT_PROTO_B:
397 ret = ssl3_send_next_proto(s);
398 if (ret <= 0) {
399 goto end;
400 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700401
Adam Langley1bea1732014-12-17 19:06:57 -0800402 if (s->s3->tlsext_channel_id_valid) {
403 s->state = SSL3_ST_CW_CHANNEL_ID_A;
404 } else {
405 s->state = SSL3_ST_CW_FINISHED_A;
406 }
407 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley1bea1732014-12-17 19:06:57 -0800409 case SSL3_ST_CW_CHANNEL_ID_A:
410 case SSL3_ST_CW_CHANNEL_ID_B:
411 ret = ssl3_send_channel_id(s);
412 if (ret <= 0) {
413 goto end;
414 }
415 s->state = SSL3_ST_CW_FINISHED_A;
416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley1bea1732014-12-17 19:06:57 -0800418 case SSL3_ST_CW_FINISHED_A:
419 case SSL3_ST_CW_FINISHED_B:
420 ret =
421 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
422 s->enc_method->client_finished_label,
423 s->enc_method->client_finished_label_len);
424 if (ret <= 0) {
425 goto end;
426 }
427 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley1bea1732014-12-17 19:06:57 -0800429 if (s->hit) {
430 s->s3->tmp.next_state = SSL_ST_OK;
431 } else {
432 /* This is a non-resumption handshake. If it involves ChannelID, then
433 * record the handshake hashes at this point in the session so that
434 * any resumption of this session with ChannelID can sign those
435 * hashes. */
436 if (s->s3->tlsext_channel_id_new) {
437 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500438 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800439 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500440 }
Adam Langley1bea1732014-12-17 19:06:57 -0800441 }
David Benjamined7c4752015-02-16 19:16:46 -0500442 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
443 ssl3_can_false_start(s) &&
444 /* No False Start on renegotiation (would complicate the state
445 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400446 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500447 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800448 } else {
449 /* Allow NewSessionTicket if ticket expected */
450 if (s->tlsext_ticket_expected) {
451 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
452 } else {
453 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
454 }
455 }
456 }
457 s->init_num = 0;
458 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langley1bea1732014-12-17 19:06:57 -0800460 case SSL3_ST_CR_SESSION_TICKET_A:
461 case SSL3_ST_CR_SESSION_TICKET_B:
462 ret = ssl3_get_new_session_ticket(s);
463 if (ret <= 0) {
464 goto end;
465 }
466 s->state = SSL3_ST_CR_CHANGE;
467 s->init_num = 0;
468 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langley1bea1732014-12-17 19:06:57 -0800470 case SSL3_ST_CR_CERT_STATUS_A:
471 case SSL3_ST_CR_CERT_STATUS_B:
472 ret = ssl3_get_cert_status(s);
473 if (ret <= 0) {
474 goto end;
475 }
476 s->state = SSL3_ST_CR_KEY_EXCH_A;
477 s->init_num = 0;
478 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400479
Adam Langley1bea1732014-12-17 19:06:57 -0800480 case SSL3_ST_CR_CHANGE:
481 /* At this point, the next message must be entirely behind a
482 * ChangeCipherSpec. */
483 if (!ssl3_expect_change_cipher_spec(s)) {
484 ret = -1;
485 goto end;
486 }
487 s->state = SSL3_ST_CR_FINISHED_A;
488 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langley1bea1732014-12-17 19:06:57 -0800490 case SSL3_ST_CR_FINISHED_A:
491 case SSL3_ST_CR_FINISHED_B:
492 ret =
493 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
494 if (ret <= 0) {
495 goto end;
496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langley1bea1732014-12-17 19:06:57 -0800498 if (s->hit) {
499 s->state = SSL3_ST_CW_CHANGE_A;
500 } else {
501 s->state = SSL_ST_OK;
502 }
503 s->init_num = 0;
504 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langley1bea1732014-12-17 19:06:57 -0800506 case SSL3_ST_CW_FLUSH:
507 s->rwstate = SSL_WRITING;
508 if (BIO_flush(s->wbio) <= 0) {
509 ret = -1;
510 goto end;
511 }
512 s->rwstate = SSL_NOTHING;
513 s->state = s->s3->tmp.next_state;
514 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700515
David Benjamined7c4752015-02-16 19:16:46 -0500516 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800517 /* Allow NewSessionTicket if ticket expected */
518 if (s->tlsext_ticket_expected) {
519 s->state = SSL3_ST_CR_SESSION_TICKET_A;
520 } else {
521 s->state = SSL3_ST_CR_CHANGE;
522 }
David Benjamined7c4752015-02-16 19:16:46 -0500523 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700524
Adam Langley1bea1732014-12-17 19:06:57 -0800525 ssl_free_wbio_buffer(s);
526 ret = 1;
527 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley1bea1732014-12-17 19:06:57 -0800529 case SSL_ST_OK:
530 /* clean a few things up */
531 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin2755a3e2015-04-22 16:17:58 -0400533 BUF_MEM_free(s->init_buf);
534 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 /* Remove write buffering now. */
537 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langley1bea1732014-12-17 19:06:57 -0800539 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500540 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400541 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley1bea1732014-12-17 19:06:57 -0800543 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley1bea1732014-12-17 19:06:57 -0800545 ret = 1;
546 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langley1bea1732014-12-17 19:06:57 -0800548 if (cb != NULL) {
549 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langley1bea1732014-12-17 19:06:57 -0800552 goto end;
553
554 default:
555 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
556 ret = -1;
557 goto end;
558 }
559
560 if (!s->s3->tmp.reuse_message && !skip) {
561 if (cb != NULL && s->state != state) {
562 new_state = s->state;
563 s->state = state;
564 cb(s, SSL_CB_CONNECT_LOOP, 1);
565 s->state = new_state;
566 }
567 }
568 skip = 0;
569 }
570
Adam Langley95c29f32014-06-20 12:00:00 -0700571end:
Adam Langley1bea1732014-12-17 19:06:57 -0800572 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400573 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800574 if (cb != NULL) {
575 cb(s, SSL_CB_CONNECT_EXIT, ret);
576 }
577 return ret;
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langley1bea1732014-12-17 19:06:57 -0800580int ssl3_send_client_hello(SSL *s) {
581 uint8_t *buf, *p, *d;
582 int i;
583 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langley1bea1732014-12-17 19:06:57 -0800585 buf = (uint8_t *)s->init_buf->data;
586 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
587 if (!s->s3->have_version) {
588 uint16_t max_version = ssl3_get_max_client_version(s);
589 /* Disabling all versions is silly: return an error. */
590 if (max_version == 0) {
591 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
592 goto err;
593 }
594 s->version = max_version;
595 s->client_version = max_version;
596 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500597
Adam Langley1bea1732014-12-17 19:06:57 -0800598 /* If the configured session was created at a version higher than our
599 * maximum version, drop it. */
600 if (s->session &&
601 (s->session->session_id_length == 0 || s->session->not_resumable ||
602 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
603 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
604 SSL_set_session(s, NULL);
605 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500606
Adam Langley1bea1732014-12-17 19:06:57 -0800607 /* else use the pre-loaded session */
608 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley1bea1732014-12-17 19:06:57 -0800610 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
611 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400612 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400613 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
614 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400615 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langley1bea1732014-12-17 19:06:57 -0800618 /* Do the message type and length last. Note: the final argument to
619 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
620 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langley1bea1732014-12-17 19:06:57 -0800622 /* version indicates the negotiated version: for example from an SSLv2/v3
623 * compatible client hello). The client_version field is the maximum
624 * version we permit and it is also used in RSA encrypted premaster
625 * secrets. Some servers can choke if we initially report a higher version
626 * then renegotiate to a lower one in the premaster secret. This didn't
627 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
628 * or later if the server only supports 1.0.
629 *
630 * Possible scenario with previous logic:
631 * 1. Client hello indicates TLS 1.2
632 * 2. Server hello says TLS 1.0
633 * 3. RSA encrypted premaster secret uses 1.2.
634 * 4. Handhaked proceeds using TLS 1.0.
635 * 5. Server sends hello request to renegotiate.
636 * 6. Client hello indicates TLS v1.0 as we now
637 * know that is maximum server supports.
638 * 7. Server chokes on RSA encrypted premaster secret
639 * containing version 1.0.
640 *
641 * For interoperability it should be OK to always use the maximum version
642 * we support in client hello and then rely on the checking of version to
643 * ensure the servers isn't being inconsistent: for example initially
644 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
645 * using client_version in client hello and not resetting it to the
646 * negotiated version. */
647 *(p++) = s->client_version >> 8;
648 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langley1bea1732014-12-17 19:06:57 -0800650 /* Random stuff */
651 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
652 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langley1bea1732014-12-17 19:06:57 -0800654 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400655 if (s->s3->initial_handshake_complete || s->session == NULL) {
656 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800657 i = 0;
658 } else {
659 i = s->session->session_id_length;
660 }
661 *(p++) = i;
662 if (i != 0) {
663 if (i > (int)sizeof(s->session->session_id)) {
664 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
665 goto err;
666 }
667 memcpy(p, s->session->session_id, i);
668 p += i;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langley1bea1732014-12-17 19:06:57 -0800671 /* cookie stuff for DTLS */
672 if (SSL_IS_DTLS(s)) {
673 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
674 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
675 goto err;
676 }
677 *(p++) = s->d1->cookie_len;
678 memcpy(p, s->d1->cookie, s->d1->cookie_len);
679 p += s->d1->cookie_len;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langley1bea1732014-12-17 19:06:57 -0800682 /* Ciphers supported */
683 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
684 if (i == 0) {
685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
686 SSL_R_NO_CIPHERS_AVAILABLE);
687 goto err;
688 }
689 s2n(i, p);
690 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley1bea1732014-12-17 19:06:57 -0800692 /* COMPRESSION */
693 *(p++) = 1;
694 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langley1bea1732014-12-17 19:06:57 -0800696 /* TLS extensions*/
697 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
698 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
699 goto err;
700 }
701
702 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
703 p - buf);
704 if (p == NULL) {
705 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
706 goto err;
707 }
708
709 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500710 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
711 goto err;
712 }
Adam Langley1bea1732014-12-17 19:06:57 -0800713 s->state = SSL3_ST_CW_CLNT_HELLO_B;
714 }
715
716 /* SSL3_ST_CW_CLNT_HELLO_B */
717 return ssl_do_write(s);
718
Adam Langley95c29f32014-06-20 12:00:00 -0700719err:
Adam Langley1bea1732014-12-17 19:06:57 -0800720 return -1;
721}
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langley1bea1732014-12-17 19:06:57 -0800723int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400724 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800725 const SSL_CIPHER *c;
726 CERT *ct = s->cert;
727 int al = SSL_AD_INTERNAL_ERROR, ok;
728 long n;
729 CBS server_hello, server_random, session_id;
730 uint16_t server_version, cipher_suite;
731 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400732 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langley1bea1732014-12-17 19:06:57 -0800734 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
735 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
736 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500737 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langley1bea1732014-12-17 19:06:57 -0800739 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500740 uint32_t err = ERR_peek_error();
741 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
742 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
743 /* Add a dedicated error code to the queue for a handshake_failure alert
744 * in response to ClientHello. This matches NSS's client behavior and
745 * gives a better error on a (probable) failure to negotiate initial
746 * parameters. Note: this error code comes after the original one.
747 *
748 * See https://crbug.com/446505. */
749 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
750 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
751 }
Adam Langley1bea1732014-12-17 19:06:57 -0800752 return n;
753 }
Adam Langley95c29f32014-06-20 12:00:00 -0700754
Adam Langley1bea1732014-12-17 19:06:57 -0800755 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400756
Adam Langley1bea1732014-12-17 19:06:57 -0800757 if (!CBS_get_u16(&server_hello, &server_version) ||
758 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
759 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
760 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
761 !CBS_get_u16(&server_hello, &cipher_suite) ||
762 !CBS_get_u8(&server_hello, &compression_method)) {
763 al = SSL_AD_DECODE_ERROR;
764 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
765 goto f_err;
766 }
David Benjamina03d95d2014-07-12 19:49:07 -0400767
David Benjamine6df0542015-05-12 22:02:08 -0400768 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800769 if (!s->s3->have_version) {
770 if (!ssl3_is_version_enabled(s, server_version)) {
771 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
772 s->version = server_version;
773 /* Mark the version as fixed so the record-layer version is not clamped
774 * to TLS 1.0. */
775 s->s3->have_version = 1;
776 al = SSL_AD_PROTOCOL_VERSION;
777 goto f_err;
778 }
779 s->version = server_version;
780 s->enc_method = ssl3_get_enc_method(server_version);
781 assert(s->enc_method != NULL);
782 /* At this point, the connection's version is known and s->version is
783 * fixed. Begin enforcing the record-layer version. */
784 s->s3->have_version = 1;
785 } else if (server_version != s->version) {
786 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
787 al = SSL_AD_PROTOCOL_VERSION;
788 goto f_err;
789 }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langley1bea1732014-12-17 19:06:57 -0800791 /* Copy over the server random. */
792 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langley1bea1732014-12-17 19:06:57 -0800794 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400795 if (!s->s3->initial_handshake_complete && s->session != NULL &&
796 CBS_mem_equal(&session_id, s->session->session_id,
797 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800798 if (s->sid_ctx_length != s->session->sid_ctx_length ||
799 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
800 /* actually a client application bug */
801 al = SSL_AD_ILLEGAL_PARAMETER;
802 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
803 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
804 goto f_err;
805 }
806 s->hit = 1;
807 } else {
808 /* The session wasn't resumed. Create a fresh SSL_SESSION to
809 * fill out. */
810 s->hit = 0;
811 if (!ssl_get_new_session(s, 0)) {
812 goto f_err;
813 }
814 /* Note: session_id could be empty. */
815 s->session->session_id_length = CBS_len(&session_id);
816 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
817 }
David Benjamina03d95d2014-07-12 19:49:07 -0400818
Adam Langley1bea1732014-12-17 19:06:57 -0800819 c = ssl3_get_cipher_by_value(cipher_suite);
820 if (c == NULL) {
821 /* unknown cipher */
822 al = SSL_AD_ILLEGAL_PARAMETER;
823 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
824 SSL_R_UNKNOWN_CIPHER_RETURNED);
825 goto f_err;
826 }
827 /* ct->mask_ssl was computed from client capabilities. Now
828 * that the final version is known, compute a new mask_ssl. */
829 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
830 mask_ssl = SSL_TLSV1_2;
831 } else {
832 mask_ssl = 0;
833 }
834 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
835 * the server selected it, it's an error. */
836 if ((c->algorithm_ssl & mask_ssl) ||
837 (c->algorithm_mkey & ct->mask_k) ||
838 (c->algorithm_auth & ct->mask_a)) {
839 al = SSL_AD_ILLEGAL_PARAMETER;
840 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
841 goto f_err;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langley1bea1732014-12-17 19:06:57 -0800844 sk = ssl_get_ciphers_by_id(s);
845 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
846 /* we did not say we would use this cipher */
847 al = SSL_AD_ILLEGAL_PARAMETER;
848 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
849 goto f_err;
850 }
Adam Langley95c29f32014-06-20 12:00:00 -0700851
David Benjaminece3de92015-03-16 18:02:20 -0400852 if (s->hit) {
853 if (s->session->cipher != c) {
854 al = SSL_AD_ILLEGAL_PARAMETER;
855 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
856 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
857 goto f_err;
858 }
859 if (s->session->ssl_version != s->version) {
860 al = SSL_AD_ILLEGAL_PARAMETER;
861 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
862 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
863 goto f_err;
864 }
Adam Langley1bea1732014-12-17 19:06:57 -0800865 }
866 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500867
David Benjamin5f04b652015-05-26 17:30:10 -0400868 /* If doing a full handshake with TLS 1.2, the server may request a client
869 * certificate which requires hashing the handshake transcript under a
870 * different hash. Otherwise, release the handshake buffer. */
871 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800872 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
873 goto f_err;
874 }
David Benjamina03d95d2014-07-12 19:49:07 -0400875
Adam Langley1bea1732014-12-17 19:06:57 -0800876 /* Only the NULL compression algorithm is supported. */
877 if (compression_method != 0) {
878 al = SSL_AD_ILLEGAL_PARAMETER;
879 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
880 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
881 goto f_err;
882 }
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley1bea1732014-12-17 19:06:57 -0800884 /* TLS extensions */
885 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
886 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
887 goto err;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langley1bea1732014-12-17 19:06:57 -0800890 /* There should be nothing left over in the record. */
891 if (CBS_len(&server_hello) != 0) {
892 /* wrong packet length */
893 al = SSL_AD_DECODE_ERROR;
894 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
895 goto f_err;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langley1bea1732014-12-17 19:06:57 -0800898 return 1;
899
Adam Langley95c29f32014-06-20 12:00:00 -0700900f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800901 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700902err:
Adam Langley1bea1732014-12-17 19:06:57 -0800903 return -1;
904}
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langley1bea1732014-12-17 19:06:57 -0800906int ssl3_get_server_certificate(SSL *s) {
907 int al, i, ok, ret = -1;
908 unsigned long n;
909 X509 *x = NULL;
910 STACK_OF(X509) *sk = NULL;
911 SESS_CERT *sc;
912 EVP_PKEY *pkey = NULL;
913 CBS cbs, certificate_list;
914 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langley1bea1732014-12-17 19:06:57 -0800916 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400917 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500918 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langley1bea1732014-12-17 19:06:57 -0800920 if (!ok) {
921 return n;
922 }
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langley1bea1732014-12-17 19:06:57 -0800924 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langley1bea1732014-12-17 19:06:57 -0800926 sk = sk_X509_new_null();
927 if (sk == NULL) {
928 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
929 goto err;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langley1bea1732014-12-17 19:06:57 -0800932 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
933 CBS_len(&cbs) != 0) {
934 al = SSL_AD_DECODE_ERROR;
935 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
936 goto f_err;
937 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400938
Adam Langley1bea1732014-12-17 19:06:57 -0800939 while (CBS_len(&certificate_list) > 0) {
940 CBS certificate;
941 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
942 al = SSL_AD_DECODE_ERROR;
943 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
944 SSL_R_CERT_LENGTH_MISMATCH);
945 goto f_err;
946 }
947 data = CBS_data(&certificate);
948 x = d2i_X509(NULL, &data, CBS_len(&certificate));
949 if (x == NULL) {
950 al = SSL_AD_BAD_CERTIFICATE;
951 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
952 goto f_err;
953 }
954 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
955 al = SSL_AD_DECODE_ERROR;
956 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
957 SSL_R_CERT_LENGTH_MISMATCH);
958 goto f_err;
959 }
960 if (!sk_X509_push(sk, x)) {
961 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
962 goto err;
963 }
964 x = NULL;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langley1bea1732014-12-17 19:06:57 -0800967 i = ssl_verify_cert_chain(s, sk);
968 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
969 al = ssl_verify_alarm_type(s->verify_result);
970 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
971 SSL_R_CERTIFICATE_VERIFY_FAILED);
972 goto f_err;
973 }
974 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langley1bea1732014-12-17 19:06:57 -0800976 sc = ssl_sess_cert_new();
977 if (sc == NULL) {
978 goto err;
979 }
Adam Langley95c29f32014-06-20 12:00:00 -0700980
David Benjamin2755a3e2015-04-22 16:17:58 -0400981 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -0800982 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langley1bea1732014-12-17 19:06:57 -0800984 sc->cert_chain = sk;
985 /* Inconsistency alert: cert_chain does include the peer's certificate, which
986 * we don't include in s3_srvr.c */
987 x = sk_X509_value(sk, 0);
988 sk = NULL;
989 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langley1bea1732014-12-17 19:06:57 -0800991 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -0700992
Adam Langley1bea1732014-12-17 19:06:57 -0800993 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
994 x = NULL;
995 al = SSL3_AL_FATAL;
996 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
997 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
998 goto f_err;
999 }
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langley1bea1732014-12-17 19:06:57 -08001001 i = ssl_cert_type(pkey);
1002 if (i < 0) {
1003 x = NULL;
1004 al = SSL3_AL_FATAL;
1005 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1006 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1007 goto f_err;
1008 }
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langley1bea1732014-12-17 19:06:57 -08001010 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1011 if (exp_idx >= 0 && i != exp_idx) {
1012 x = NULL;
1013 al = SSL_AD_ILLEGAL_PARAMETER;
1014 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1015 SSL_R_WRONG_CERTIFICATE_TYPE);
1016 goto f_err;
1017 }
1018 sc->peer_cert_type = i;
David Benjamin2755a3e2015-04-22 16:17:58 -04001019 X509_free(sc->peer_pkeys[i].x509);
Adam Langley1bea1732014-12-17 19:06:57 -08001020 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1021 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001022
David Benjamin2755a3e2015-04-22 16:17:58 -04001023 X509_free(s->session->peer);
Adam Langley1bea1732014-12-17 19:06:57 -08001024 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001025
Adam Langley1bea1732014-12-17 19:06:57 -08001026 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langley1bea1732014-12-17 19:06:57 -08001028 x = NULL;
1029 ret = 1;
1030
1031 if (0) {
1032 f_err:
1033 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1034 }
1035
1036err:
1037 EVP_PKEY_free(pkey);
1038 X509_free(x);
1039 sk_X509_pop_free(sk, X509_free);
1040 return ret;
1041}
1042
1043int ssl3_get_server_key_exchange(SSL *s) {
1044 EVP_MD_CTX md_ctx;
1045 int al, ok;
1046 long n, alg_k, alg_a;
1047 EVP_PKEY *pkey = NULL;
1048 const EVP_MD *md = NULL;
1049 RSA *rsa = NULL;
1050 DH *dh = NULL;
1051 EC_KEY *ecdh = NULL;
1052 BN_CTX *bn_ctx = NULL;
1053 EC_POINT *srvr_ecpoint = NULL;
1054 CBS server_key_exchange, server_key_exchange_orig, parameter;
1055
1056 /* use same message size as in ssl3_get_certificate_request() as
1057 * ServerKeyExchange message may be skipped */
1058 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1059 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001060 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001061 if (!ok) {
1062 return n;
1063 }
1064
1065 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1066 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1067 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1068 SSL_R_UNEXPECTED_MESSAGE);
1069 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1070 return -1;
1071 }
1072
1073 /* In plain PSK ciphersuite, ServerKeyExchange can be
1074 omitted if no identity hint is sent. Set session->sess_cert anyway to
1075 avoid problems later.*/
1076 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1077 /* PSK ciphersuites that also send a Certificate would have already
1078 * initialized |sess_cert|. */
1079 if (s->session->sess_cert == NULL) {
1080 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001081 if (s->session->sess_cert == NULL) {
1082 return -1;
1083 }
Adam Langley1bea1732014-12-17 19:06:57 -08001084 }
1085
1086 /* TODO(davidben): This should be reset in one place with the rest of the
1087 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001088 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1089 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001090 }
1091 s->s3->tmp.reuse_message = 1;
1092 return 1;
1093 }
1094
1095 /* Retain a copy of the original CBS to compute the signature over. */
1096 CBS_init(&server_key_exchange, s->init_msg, n);
1097 server_key_exchange_orig = server_key_exchange;
1098
1099 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001100 DH_free(s->session->sess_cert->peer_dh_tmp);
1101 s->session->sess_cert->peer_dh_tmp = NULL;
1102 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1103 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001104 } else {
1105 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001106 if (s->session->sess_cert == NULL) {
1107 return -1;
1108 }
Adam Langley1bea1732014-12-17 19:06:57 -08001109 }
1110
1111 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1112 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1113 EVP_MD_CTX_init(&md_ctx);
1114
1115 if (alg_a & SSL_aPSK) {
1116 CBS psk_identity_hint;
1117
1118 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1119 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1120 &psk_identity_hint)) {
1121 al = SSL_AD_DECODE_ERROR;
1122 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1123 goto f_err;
1124 }
1125
1126 /* Store PSK identity hint for later use, hint is used in
1127 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1128 * identity hint can be as long as the maximum length of a PSK identity.
1129 * Also do not allow NULL characters; identities are saved as C strings.
1130 *
1131 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1132 * a specific identity. */
1133 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1134 CBS_contains_zero_byte(&psk_identity_hint)) {
1135 al = SSL_AD_HANDSHAKE_FAILURE;
1136 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1137 SSL_R_DATA_LENGTH_TOO_LONG);
1138 goto f_err;
1139 }
1140
1141 /* Save the identity hint as a C string. */
1142 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1143 al = SSL_AD_INTERNAL_ERROR;
1144 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1145 ERR_R_MALLOC_FAILURE);
1146 goto f_err;
1147 }
1148 }
1149
David Benjamin7061e282015-03-19 11:10:48 -04001150 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001151 CBS dh_p, dh_g, dh_Ys;
1152
1153 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1154 CBS_len(&dh_p) == 0 ||
1155 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1156 CBS_len(&dh_g) == 0 ||
1157 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1158 CBS_len(&dh_Ys) == 0) {
1159 al = SSL_AD_DECODE_ERROR;
1160 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1161 goto f_err;
1162 }
1163
1164 dh = DH_new();
1165 if (dh == NULL) {
1166 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1167 goto err;
1168 }
1169
1170 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1171 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1172 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1173 NULL) {
1174 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1175 goto err;
1176 }
1177
Adam Langleya7997f12015-05-14 17:38:50 -07001178 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1180 SSL_R_BAD_DH_P_LENGTH);
1181 goto err;
1182 }
1183
1184 if (alg_a & SSL_aRSA) {
1185 pkey = X509_get_pubkey(
1186 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1187 }
1188 /* else anonymous DH, so no certificate or pkey. */
1189
1190 s->session->sess_cert->peer_dh_tmp = dh;
1191 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001192 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001193 uint16_t curve_id;
1194 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001195 const EC_GROUP *group;
1196 CBS point;
1197
1198 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1199 * key. Check curve is one of our preferences, if not server has sent an
1200 * invalid curve. */
1201 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1202 al = SSL_AD_DECODE_ERROR;
1203 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1204 goto f_err;
1205 }
1206
1207 curve_nid = tls1_ec_curve_id2nid(curve_id);
1208 if (curve_nid == 0) {
1209 al = SSL_AD_INTERNAL_ERROR;
1210 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1211 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1212 goto f_err;
1213 }
1214
David Benjamin4f7783e2015-03-05 03:05:11 -05001215 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001216 if (ecdh == NULL) {
1217 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001218 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001219 goto err;
1220 }
1221
Adam Langley1bea1732014-12-17 19:06:57 -08001222 group = EC_KEY_get0_group(ecdh);
1223
1224 /* Next, get the encoded ECPoint */
1225 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1226 al = SSL_AD_DECODE_ERROR;
1227 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1228 goto f_err;
1229 }
1230
1231 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1232 ((bn_ctx = BN_CTX_new()) == NULL)) {
1233 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1234 ERR_R_MALLOC_FAILURE);
1235 goto err;
1236 }
1237
1238 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1239 CBS_len(&point), bn_ctx)) {
1240 al = SSL_AD_DECODE_ERROR;
1241 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1242 goto f_err;
1243 }
1244
1245 /* The ECC/TLS specification does not mention the use of DSA to sign
1246 * ECParameters in the server key exchange message. We do support RSA and
1247 * ECDSA. */
1248 if (alg_a & SSL_aRSA) {
1249 pkey = X509_get_pubkey(
1250 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1251 } else if (alg_a & SSL_aECDSA) {
1252 pkey =
1253 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1254 }
1255 /* else anonymous ECDH, so no certificate or pkey. */
1256 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1257 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1258 ecdh = NULL;
1259 BN_CTX_free(bn_ctx);
1260 bn_ctx = NULL;
1261 EC_POINT_free(srvr_ecpoint);
1262 srvr_ecpoint = NULL;
1263 } else if (!(alg_k & SSL_kPSK)) {
1264 al = SSL_AD_UNEXPECTED_MESSAGE;
1265 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1266 SSL_R_UNEXPECTED_MESSAGE);
1267 goto f_err;
1268 }
1269
1270 /* At this point, |server_key_exchange| contains the signature, if any, while
1271 * |server_key_exchange_orig| contains the entire message. From that, derive
1272 * a CBS containing just the parameter. */
1273 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1274 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1275
1276 /* if it was signed, check the signature */
1277 if (pkey != NULL) {
1278 CBS signature;
1279
1280 if (SSL_USE_SIGALGS(s)) {
1281 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1282 goto f_err;
1283 }
1284 } else if (pkey->type == EVP_PKEY_RSA) {
1285 md = EVP_md5_sha1();
1286 } else {
1287 md = EVP_sha1();
1288 }
1289
1290 /* The last field in |server_key_exchange| is the signature. */
1291 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1292 CBS_len(&server_key_exchange) != 0) {
1293 al = SSL_AD_DECODE_ERROR;
1294 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1295 goto f_err;
1296 }
1297
1298 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1299 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1300 SSL3_RANDOM_SIZE) ||
1301 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1302 SSL3_RANDOM_SIZE) ||
1303 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1304 CBS_len(&parameter)) ||
1305 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1306 CBS_len(&signature))) {
1307 /* bad signature */
1308 al = SSL_AD_DECRYPT_ERROR;
1309 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1310 goto f_err;
1311 }
1312 } else {
1313 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1314 /* Might be wrong key type, check it */
1315 if (ssl3_check_cert_and_algorithm(s)) {
1316 /* Otherwise this shouldn't happen */
1317 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1318 ERR_R_INTERNAL_ERROR);
1319 }
1320 goto err;
1321 }
1322 /* still data left over */
1323 if (CBS_len(&server_key_exchange) > 0) {
1324 al = SSL_AD_DECODE_ERROR;
1325 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1326 SSL_R_EXTRA_DATA_IN_MESSAGE);
1327 goto f_err;
1328 }
1329 }
1330 EVP_PKEY_free(pkey);
1331 EVP_MD_CTX_cleanup(&md_ctx);
1332 return 1;
1333
Adam Langley95c29f32014-06-20 12:00:00 -07001334f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001335 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001336err:
Adam Langley1bea1732014-12-17 19:06:57 -08001337 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001338 RSA_free(rsa);
1339 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001340 BN_CTX_free(bn_ctx);
1341 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001342 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001343 EVP_MD_CTX_cleanup(&md_ctx);
1344 return -1;
1345}
Adam Langley95c29f32014-06-20 12:00:00 -07001346
Adam Langley1bea1732014-12-17 19:06:57 -08001347static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1348 return X509_NAME_cmp(*a, *b);
1349}
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langley1bea1732014-12-17 19:06:57 -08001351int ssl3_get_certificate_request(SSL *s) {
1352 int ok, ret = 0;
1353 unsigned long n;
1354 X509_NAME *xn = NULL;
1355 STACK_OF(X509_NAME) *ca_sk = NULL;
1356 CBS cbs;
1357 CBS certificate_types;
1358 CBS certificate_authorities;
1359 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langley1bea1732014-12-17 19:06:57 -08001361 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1362 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001363 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001364
Adam Langley1bea1732014-12-17 19:06:57 -08001365 if (!ok) {
1366 return n;
1367 }
David Benjamin688d8df2014-11-02 23:06:42 -05001368
Adam Langley1bea1732014-12-17 19:06:57 -08001369 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langley1bea1732014-12-17 19:06:57 -08001371 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1372 s->s3->tmp.reuse_message = 1;
1373 /* If we get here we don't need any cached handshake records as we wont be
1374 * doing client auth. */
1375 if (s->s3->handshake_buffer &&
1376 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1377 goto err;
1378 }
1379 return 1;
1380 }
David Benjamined439582014-07-14 19:13:02 -04001381
Adam Langley1bea1732014-12-17 19:06:57 -08001382 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1383 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1384 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1385 SSL_R_WRONG_MESSAGE_TYPE);
1386 goto err;
1387 }
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langley1bea1732014-12-17 19:06:57 -08001389 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langley1bea1732014-12-17 19:06:57 -08001391 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1392 if (ca_sk == NULL) {
1393 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1394 goto err;
1395 }
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langley1bea1732014-12-17 19:06:57 -08001397 /* get the certificate types */
1398 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1399 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1400 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1401 goto err;
1402 }
David Benjamined439582014-07-14 19:13:02 -04001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1405 &s->s3->tmp.num_certificate_types)) {
1406 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1407 goto err;
1408 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001409
Adam Langley1bea1732014-12-17 19:06:57 -08001410 if (SSL_USE_SIGALGS(s)) {
1411 CBS supported_signature_algorithms;
1412 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1413 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1414 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1415 goto err;
1416 }
David Benjamined439582014-07-14 19:13:02 -04001417
Adam Langley1bea1732014-12-17 19:06:57 -08001418 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1420 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1421 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1422 goto err;
1423 }
1424 }
David Benjamined439582014-07-14 19:13:02 -04001425
Adam Langley1bea1732014-12-17 19:06:57 -08001426 /* get the CA RDNs */
1427 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1428 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1429 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1430 goto err;
1431 }
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langley1bea1732014-12-17 19:06:57 -08001433 while (CBS_len(&certificate_authorities) > 0) {
1434 CBS distinguished_name;
1435 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1436 &distinguished_name)) {
1437 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1438 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1439 SSL_R_CA_DN_TOO_LONG);
1440 goto err;
1441 }
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langley1bea1732014-12-17 19:06:57 -08001443 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001444
Adam Langley1bea1732014-12-17 19:06:57 -08001445 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1446 if (xn == NULL) {
1447 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1448 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1449 goto err;
1450 }
Adam Langley95c29f32014-06-20 12:00:00 -07001451
Adam Langley1bea1732014-12-17 19:06:57 -08001452 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001454 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001455 goto err;
1456 }
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley1bea1732014-12-17 19:06:57 -08001458 if (CBS_len(&distinguished_name) != 0) {
1459 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1460 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1461 SSL_R_CA_DN_LENGTH_MISMATCH);
1462 goto err;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langley1bea1732014-12-17 19:06:57 -08001465 if (!sk_X509_NAME_push(ca_sk, xn)) {
1466 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1467 ERR_R_MALLOC_FAILURE);
1468 goto err;
1469 }
1470 }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langley1bea1732014-12-17 19:06:57 -08001472 /* we should setup a certificate to return.... */
1473 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001474 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001475 s->s3->tmp.ca_names = ca_sk;
1476 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langley1bea1732014-12-17 19:06:57 -08001478 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001479
Adam Langley1bea1732014-12-17 19:06:57 -08001480err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001481 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001482 return ret;
1483}
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langley1bea1732014-12-17 19:06:57 -08001485int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001486 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001487 long n;
1488 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001489
Adam Langley1bea1732014-12-17 19:06:57 -08001490 n = s->method->ssl_get_message(
1491 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001492 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langley1bea1732014-12-17 19:06:57 -08001494 if (!ok) {
1495 return n;
1496 }
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langley1bea1732014-12-17 19:06:57 -08001498 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001499
Adam Langley1bea1732014-12-17 19:06:57 -08001500 if (!CBS_get_u32(&new_session_ticket,
1501 &s->session->tlsext_tick_lifetime_hint) ||
1502 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1503 CBS_len(&new_session_ticket) != 0) {
1504 al = SSL_AD_DECODE_ERROR;
1505 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1506 goto f_err;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
Adam Langley1bea1732014-12-17 19:06:57 -08001509 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1510 &s->session->tlsext_ticklen)) {
1511 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1512 goto err;
1513 }
Adam Langley95c29f32014-06-20 12:00:00 -07001514
David Benjamin68070622015-02-08 23:44:59 -05001515 /* Generate a session ID for this session based on the session ticket. We use
1516 * the session ID mechanism for detecting ticket resumption. This also fits in
1517 * with assumptions elsewhere in OpenSSL.*/
1518 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1519 &s->session->session_id_length, EVP_sha256(), NULL)) {
1520 goto err;
1521 }
1522
1523 return 1;
David Benjamined439582014-07-14 19:13:02 -04001524
Adam Langley95c29f32014-06-20 12:00:00 -07001525f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001526 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001527err:
Adam Langley1bea1732014-12-17 19:06:57 -08001528 return -1;
1529}
Adam Langley95c29f32014-06-20 12:00:00 -07001530
Adam Langley1bea1732014-12-17 19:06:57 -08001531int ssl3_get_cert_status(SSL *s) {
1532 int ok, al;
1533 long n;
1534 CBS certificate_status, ocsp_response;
1535 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langley1bea1732014-12-17 19:06:57 -08001537 n = s->method->ssl_get_message(
1538 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001539 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001540
Adam Langley1bea1732014-12-17 19:06:57 -08001541 if (!ok) {
1542 return n;
1543 }
Adam Langley95c29f32014-06-20 12:00:00 -07001544
David Benjamindcd979f2015-04-20 18:26:52 -04001545 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1546 /* A server may send status_request in ServerHello and then change
1547 * its mind about sending CertificateStatus. */
1548 s->s3->tmp.reuse_message = 1;
1549 return 1;
1550 }
1551
Adam Langley1bea1732014-12-17 19:06:57 -08001552 CBS_init(&certificate_status, s->init_msg, n);
1553 if (!CBS_get_u8(&certificate_status, &status_type) ||
1554 status_type != TLSEXT_STATUSTYPE_ocsp ||
1555 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1556 CBS_len(&ocsp_response) == 0 ||
1557 CBS_len(&certificate_status) != 0) {
1558 al = SSL_AD_DECODE_ERROR;
1559 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1560 goto f_err;
1561 }
Adam Langley95c29f32014-06-20 12:00:00 -07001562
Adam Langley1bea1732014-12-17 19:06:57 -08001563 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1564 &s->session->ocsp_response_length)) {
1565 al = SSL_AD_INTERNAL_ERROR;
1566 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1567 goto f_err;
1568 }
1569 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001570
Adam Langley95c29f32014-06-20 12:00:00 -07001571f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001572 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1573 return -1;
1574}
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langley1bea1732014-12-17 19:06:57 -08001576int ssl3_get_server_done(SSL *s) {
1577 int ok;
1578 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langley1bea1732014-12-17 19:06:57 -08001580 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1581 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1582 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001583 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585 if (!ok) {
1586 return n;
1587 }
David Benjamin46062682014-07-14 19:14:32 -04001588
Adam Langley1bea1732014-12-17 19:06:57 -08001589 if (n > 0) {
1590 /* should contain no data */
1591 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1592 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1593 return -1;
1594 }
David Benjamin46062682014-07-14 19:14:32 -04001595
Adam Langley1bea1732014-12-17 19:06:57 -08001596 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001597}
Adam Langley1258b6a2014-06-20 12:00:00 -07001598
Adam Langley1258b6a2014-06-20 12:00:00 -07001599
Adam Langley1bea1732014-12-17 19:06:57 -08001600int ssl3_send_client_key_exchange(SSL *s) {
1601 uint8_t *p;
1602 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001603 uint32_t alg_k;
1604 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001605 uint8_t *q;
1606 EVP_PKEY *pkey = NULL;
1607 EC_KEY *clnt_ecdh = NULL;
1608 const EC_POINT *srvr_ecpoint = NULL;
1609 EVP_PKEY *srvr_pub_pkey = NULL;
1610 uint8_t *encodedPoint = NULL;
1611 int encoded_pt_len = 0;
1612 BN_CTX *bn_ctx = NULL;
1613 unsigned int psk_len = 0;
1614 uint8_t psk[PSK_MAX_PSK_LEN];
1615 uint8_t *pms = NULL;
1616 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001617
Adam Langley1bea1732014-12-17 19:06:57 -08001618 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1619 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001620
Adam Langley1bea1732014-12-17 19:06:57 -08001621 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1622 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 /* If using a PSK key exchange, prepare the pre-shared key. */
1625 if (alg_a & SSL_aPSK) {
1626 char identity[PSK_MAX_IDENTITY_LEN + 1];
1627 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001628
Adam Langley1bea1732014-12-17 19:06:57 -08001629 if (s->psk_client_callback == NULL) {
1630 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1631 SSL_R_PSK_NO_CLIENT_CB);
1632 goto err;
1633 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001634
Adam Langley1bea1732014-12-17 19:06:57 -08001635 memset(identity, 0, sizeof(identity));
1636 psk_len =
1637 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1638 sizeof(identity), psk, sizeof(psk));
1639 if (psk_len > PSK_MAX_PSK_LEN) {
1640 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1641 ERR_R_INTERNAL_ERROR);
1642 goto err;
1643 } else if (psk_len == 0) {
1644 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1645 SSL_R_PSK_IDENTITY_NOT_FOUND);
1646 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1647 goto err;
1648 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001649
Adam Langley1bea1732014-12-17 19:06:57 -08001650 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1651 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1652 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1653 ERR_R_INTERNAL_ERROR);
1654 goto err;
1655 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001656
David Benjamin2755a3e2015-04-22 16:17:58 -04001657 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001658 s->session->psk_identity = BUF_strdup(identity);
1659 if (s->session->psk_identity == NULL) {
1660 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1661 ERR_R_MALLOC_FAILURE);
1662 goto err;
1663 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001664
Adam Langley1bea1732014-12-17 19:06:57 -08001665 /* Write out psk_identity. */
1666 s2n(identity_len, p);
1667 memcpy(p, identity, identity_len);
1668 p += identity_len;
1669 n = 2 + identity_len;
1670 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001671
Adam Langley1bea1732014-12-17 19:06:57 -08001672 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1673 if (alg_k & SSL_kRSA) {
1674 RSA *rsa;
1675 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001676
Adam Langley1bea1732014-12-17 19:06:57 -08001677 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1678 pms = OPENSSL_malloc(pms_len);
1679 if (pms == NULL) {
1680 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1681 ERR_R_MALLOC_FAILURE);
1682 goto err;
1683 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001684
Adam Langley1bea1732014-12-17 19:06:57 -08001685 if (s->session->sess_cert == NULL) {
1686 /* We should always have a server certificate with SSL_kRSA. */
1687 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1688 ERR_R_INTERNAL_ERROR);
1689 goto err;
1690 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001691
Adam Langley1bea1732014-12-17 19:06:57 -08001692 pkey = X509_get_pubkey(
1693 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1694 if (pkey == NULL ||
1695 pkey->type != EVP_PKEY_RSA ||
1696 pkey->pkey.rsa == NULL) {
1697 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1698 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001699 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001700 goto err;
1701 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001702
Adam Langley1bea1732014-12-17 19:06:57 -08001703 rsa = pkey->pkey.rsa;
1704 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001705
Adam Langley1bea1732014-12-17 19:06:57 -08001706 pms[0] = s->client_version >> 8;
1707 pms[1] = s->client_version & 0xff;
1708 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1709 goto err;
1710 }
1711
1712 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1713
1714 q = p;
1715 /* In TLS and beyond, reserve space for the length prefix. */
1716 if (s->version > SSL3_VERSION) {
1717 p += 2;
1718 n += 2;
1719 }
1720 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1721 RSA_PKCS1_PADDING)) {
1722 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1723 SSL_R_BAD_RSA_ENCRYPT);
1724 goto err;
1725 }
1726 n += enc_pms_len;
1727
1728 /* Log the premaster secret, if logging is enabled. */
1729 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1730 pms_len)) {
1731 goto err;
1732 }
1733
1734 /* Fill in the length prefix. */
1735 if (s->version > SSL3_VERSION) {
1736 s2n(enc_pms_len, q);
1737 }
David Benjamin7061e282015-03-19 11:10:48 -04001738 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001739 DH *dh_srvr, *dh_clnt;
1740 SESS_CERT *scert = s->session->sess_cert;
1741 int dh_len;
1742 size_t pub_len;
1743
1744 if (scert == NULL) {
1745 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1746 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1747 SSL_R_UNEXPECTED_MESSAGE);
1748 goto err;
1749 }
1750
1751 if (scert->peer_dh_tmp == NULL) {
1752 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1753 ERR_R_INTERNAL_ERROR);
1754 goto err;
1755 }
1756 dh_srvr = scert->peer_dh_tmp;
1757
1758 /* generate a new random key */
1759 dh_clnt = DHparams_dup(dh_srvr);
1760 if (dh_clnt == NULL) {
1761 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1762 goto err;
1763 }
1764 if (!DH_generate_key(dh_clnt)) {
1765 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1766 DH_free(dh_clnt);
1767 goto err;
1768 }
1769
1770 pms_len = DH_size(dh_clnt);
1771 pms = OPENSSL_malloc(pms_len);
1772 if (pms == NULL) {
1773 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1774 ERR_R_MALLOC_FAILURE);
1775 DH_free(dh_clnt);
1776 goto err;
1777 }
1778
1779 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1780 if (dh_len <= 0) {
1781 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1782 DH_free(dh_clnt);
1783 goto err;
1784 }
1785 pms_len = dh_len;
1786
1787 /* send off the data */
1788 pub_len = BN_num_bytes(dh_clnt->pub_key);
1789 s2n(pub_len, p);
1790 BN_bn2bin(dh_clnt->pub_key, p);
1791 n += 2 + pub_len;
1792
1793 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001794 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001795 const EC_GROUP *srvr_group = NULL;
1796 EC_KEY *tkey;
1797 int field_size = 0, ecdh_len;
1798
1799 if (s->session->sess_cert == NULL) {
1800 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1801 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1802 SSL_R_UNEXPECTED_MESSAGE);
1803 goto err;
1804 }
1805
1806 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1807 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1808 ERR_R_INTERNAL_ERROR);
1809 goto err;
1810 }
1811
1812 tkey = s->session->sess_cert->peer_ecdh_tmp;
1813
1814 srvr_group = EC_KEY_get0_group(tkey);
1815 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1816 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1818 ERR_R_INTERNAL_ERROR);
1819 goto err;
1820 }
1821
1822 clnt_ecdh = EC_KEY_new();
1823 if (clnt_ecdh == NULL) {
1824 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1825 ERR_R_MALLOC_FAILURE);
1826 goto err;
1827 }
1828
1829 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1830 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1831 goto err;
1832 }
1833
1834 /* Generate a new ECDH key pair */
1835 if (!EC_KEY_generate_key(clnt_ecdh)) {
1836 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1837 goto err;
1838 }
1839
1840 field_size = EC_GROUP_get_degree(srvr_group);
1841 if (field_size <= 0) {
1842 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1843 goto err;
1844 }
1845
1846 pms_len = (field_size + 7) / 8;
1847 pms = OPENSSL_malloc(pms_len);
1848 if (pms == NULL) {
1849 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1850 ERR_R_MALLOC_FAILURE);
1851 goto err;
1852 }
1853
1854 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1855 if (ecdh_len <= 0) {
1856 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1857 goto err;
1858 }
1859 pms_len = ecdh_len;
1860
1861 /* First check the size of encoding and allocate memory accordingly. */
1862 encoded_pt_len =
1863 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1864 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1865
1866 encodedPoint =
1867 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1868 bn_ctx = BN_CTX_new();
1869 if (encodedPoint == NULL || bn_ctx == NULL) {
1870 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1871 ERR_R_MALLOC_FAILURE);
1872 goto err;
1873 }
1874
1875 /* Encode the public key */
1876 encoded_pt_len = EC_POINT_point2oct(
1877 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1878 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1879
1880 *p = encoded_pt_len; /* length of encoded point */
1881 /* Encoded point will be copied here */
1882 p += 1;
1883 n += 1;
1884 /* copy the point */
1885 memcpy(p, encodedPoint, encoded_pt_len);
1886 /* increment n to account for length field */
1887 n += encoded_pt_len;
1888
1889 /* Free allocated memory */
1890 BN_CTX_free(bn_ctx);
1891 bn_ctx = NULL;
1892 OPENSSL_free(encodedPoint);
1893 encodedPoint = NULL;
1894 EC_KEY_free(clnt_ecdh);
1895 clnt_ecdh = NULL;
1896 EVP_PKEY_free(srvr_pub_pkey);
1897 srvr_pub_pkey = NULL;
1898 } else if (alg_k & SSL_kPSK) {
1899 /* For plain PSK, other_secret is a block of 0s with the same length as
1900 * the pre-shared key. */
1901 pms_len = psk_len;
1902 pms = OPENSSL_malloc(pms_len);
1903 if (pms == NULL) {
1904 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1905 ERR_R_MALLOC_FAILURE);
1906 goto err;
1907 }
1908 memset(pms, 0, pms_len);
1909 } else {
1910 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1911 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1912 ERR_R_INTERNAL_ERROR);
1913 goto err;
1914 }
1915
1916 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1917 * key. */
1918 if (alg_a & SSL_aPSK) {
1919 CBB cbb, child;
1920 uint8_t *new_pms;
1921 size_t new_pms_len;
1922
1923 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1924 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1925 ERR_R_MALLOC_FAILURE);
1926 goto err;
1927 }
1928 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1929 !CBB_add_bytes(&child, pms, pms_len) ||
1930 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1931 !CBB_add_bytes(&child, psk, psk_len) ||
1932 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1933 CBB_cleanup(&cbb);
1934 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1935 ERR_R_INTERNAL_ERROR);
1936 goto err;
1937 }
1938 OPENSSL_cleanse(pms, pms_len);
1939 OPENSSL_free(pms);
1940 pms = new_pms;
1941 pms_len = new_pms_len;
1942 }
1943
1944 /* The message must be added to the finished hash before calculating the
1945 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001946 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1947 goto err;
1948 }
Adam Langley1bea1732014-12-17 19:06:57 -08001949 s->state = SSL3_ST_CW_KEY_EXCH_B;
1950
1951 s->session->master_key_length = s->enc_method->generate_master_secret(
1952 s, s->session->master_key, pms, pms_len);
1953 if (s->session->master_key_length == 0) {
1954 goto err;
1955 }
1956 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1957 OPENSSL_cleanse(pms, pms_len);
1958 OPENSSL_free(pms);
1959 }
1960
1961 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001962 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001963
1964err:
Adam Langley1bea1732014-12-17 19:06:57 -08001965 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001966 OPENSSL_free(encodedPoint);
1967 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001968 EVP_PKEY_free(srvr_pub_pkey);
1969 if (pms) {
1970 OPENSSL_cleanse(pms, pms_len);
1971 OPENSSL_free(pms);
1972 }
1973 return -1;
1974}
Adam Langley1258b6a2014-06-20 12:00:00 -07001975
Adam Langley1bea1732014-12-17 19:06:57 -08001976int ssl3_send_cert_verify(SSL *s) {
1977 uint8_t *buf, *p;
1978 const EVP_MD *md = NULL;
1979 uint8_t digest[EVP_MAX_MD_SIZE];
1980 size_t digest_length;
1981 EVP_PKEY *pkey;
1982 EVP_PKEY_CTX *pctx = NULL;
1983 size_t signature_length = 0;
1984 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001985
Adam Langley1bea1732014-12-17 19:06:57 -08001986 buf = (uint8_t *)s->init_buf->data;
1987
1988 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1989 p = ssl_handshake_start(s);
1990 pkey = s->cert->key->privatekey;
1991
1992 /* Write out the digest type if needbe. */
1993 if (SSL_USE_SIGALGS(s)) {
1994 md = tls1_choose_signing_digest(s, pkey);
1995 if (!tls12_get_sigandhash(p, pkey, md)) {
1996 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
1997 goto err;
1998 }
1999 p += 2;
2000 n += 2;
2001 }
2002
2003 /* Compute the digest. */
2004 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2005 goto err;
2006 }
2007
2008 /* The handshake buffer is no longer necessary. */
2009 if (s->s3->handshake_buffer &&
2010 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2011 goto err;
2012 }
2013
2014 /* Sign the digest. */
2015 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2016 if (pctx == NULL) {
2017 goto err;
2018 }
2019
2020 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2021 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2022 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2023 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2024 goto err;
2025 }
2026
2027 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2028 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2029 goto err;
2030 }
2031
2032 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2033 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2034 goto err;
2035 }
2036
2037 s2n(signature_length, p);
2038 n += signature_length + 2;
2039
David Benjaminfbdfefb2015-02-16 19:33:53 -05002040 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2041 goto err;
2042 }
Adam Langley1bea1732014-12-17 19:06:57 -08002043 s->state = SSL3_ST_CW_CERT_VRFY_B;
2044 }
2045
2046 EVP_PKEY_CTX_free(pctx);
2047 return ssl_do_write(s);
2048
2049err:
2050 EVP_PKEY_CTX_free(pctx);
2051 return -1;
2052}
2053
2054/* ssl3_has_client_certificate returns true if a client certificate is
2055 * configured. */
2056static int ssl3_has_client_certificate(SSL *s) {
2057 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2058}
2059
2060int ssl3_send_client_certificate(SSL *s) {
2061 X509 *x509 = NULL;
2062 EVP_PKEY *pkey = NULL;
2063 int i;
2064
2065 if (s->state == SSL3_ST_CW_CERT_A) {
2066 /* Let cert callback update client certificates if required */
2067 if (s->cert->cert_cb) {
2068 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2069 if (i < 0) {
2070 s->rwstate = SSL_X509_LOOKUP;
2071 return -1;
2072 }
2073 if (i == 0) {
2074 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2075 return 0;
2076 }
2077 s->rwstate = SSL_NOTHING;
2078 }
2079
2080 if (ssl3_has_client_certificate(s)) {
2081 s->state = SSL3_ST_CW_CERT_C;
2082 } else {
2083 s->state = SSL3_ST_CW_CERT_B;
2084 }
2085 }
2086
2087 /* We need to get a client cert */
2088 if (s->state == SSL3_ST_CW_CERT_B) {
2089 /* If we get an error, we need to:
2090 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2091 * We then get retried later */
2092 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2093 if (i < 0) {
2094 s->rwstate = SSL_X509_LOOKUP;
2095 return -1;
2096 }
2097 s->rwstate = SSL_NOTHING;
2098 if (i == 1 && pkey != NULL && x509 != NULL) {
2099 s->state = SSL3_ST_CW_CERT_B;
2100 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2101 i = 0;
2102 }
2103 } else if (i == 1) {
2104 i = 0;
2105 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2106 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2107 }
2108
David Benjamin2755a3e2015-04-22 16:17:58 -04002109 X509_free(x509);
2110 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002111 if (i && !ssl3_has_client_certificate(s)) {
2112 i = 0;
2113 }
2114 if (i == 0) {
2115 if (s->version == SSL3_VERSION) {
2116 s->s3->tmp.cert_req = 0;
2117 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2118 return 1;
2119 } else {
2120 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002121 /* There is no client certificate, so the handshake buffer may be
2122 * released. */
2123 if (s->s3->handshake_buffer &&
2124 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2125 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2126 return -1;
2127 }
Adam Langley1bea1732014-12-17 19:06:57 -08002128 }
2129 }
2130
2131 /* Ok, we have a cert */
2132 s->state = SSL3_ST_CW_CERT_C;
2133 }
2134
2135 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002136 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2137 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2138 return -1;
2139 }
Adam Langley1bea1732014-12-17 19:06:57 -08002140 }
2141
2142 /* SSL3_ST_CW_CERT_D */
2143 return ssl_do_write(s);
2144}
2145
2146#define has_bits(i, m) (((i) & (m)) == (m))
2147
2148int ssl3_check_cert_and_algorithm(SSL *s) {
2149 int i, idx;
2150 long alg_k, alg_a;
2151 EVP_PKEY *pkey = NULL;
2152 SESS_CERT *sc;
2153 DH *dh;
2154
2155 /* we don't have a certificate */
2156 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2157 return 1;
2158 }
2159
2160 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2161 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2162
2163 sc = s->session->sess_cert;
2164 if (sc == NULL) {
2165 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2166 goto err;
2167 }
2168
2169 dh = s->session->sess_cert->peer_dh_tmp;
2170
2171 /* This is the passed certificate */
2172
2173 idx = sc->peer_cert_type;
2174 if (idx == SSL_PKEY_ECC) {
2175 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2176 /* check failed */
2177 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2178 goto f_err;
2179 } else {
2180 return 1;
2181 }
2182 } else if (alg_a & SSL_aECDSA) {
2183 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2184 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2185 goto f_err;
2186 }
2187 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2188 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2189 EVP_PKEY_free(pkey);
2190
2191 /* Check that we have a certificate if we require one */
2192 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2193 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2194 SSL_R_MISSING_RSA_SIGNING_CERT);
2195 goto f_err;
2196 }
2197
2198 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2199 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2200 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2201 goto f_err;
2202 }
2203
David Benjamin7061e282015-03-19 11:10:48 -04002204 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002205 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2206 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2207 goto f_err;
2208 }
2209
2210 return 1;
2211
2212f_err:
2213 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2214err:
2215 return 0;
2216}
2217
2218int ssl3_send_next_proto(SSL *s) {
2219 unsigned int len, padding_len;
2220 uint8_t *d, *p;
2221
2222 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2223 len = s->next_proto_negotiated_len;
2224 padding_len = 32 - ((len + 2) % 32);
2225
2226 d = p = ssl_handshake_start(s);
2227 *(p++) = len;
2228 memcpy(p, s->next_proto_negotiated, len);
2229 p += len;
2230 *(p++) = padding_len;
2231 memset(p, 0, padding_len);
2232 p += padding_len;
2233
David Benjaminfbdfefb2015-02-16 19:33:53 -05002234 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2235 return -1;
2236 }
Adam Langley1bea1732014-12-17 19:06:57 -08002237 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2238 }
2239
2240 return ssl_do_write(s);
2241}
2242
2243int ssl3_send_channel_id(SSL *s) {
2244 uint8_t *d;
2245 int ret = -1, public_key_len;
2246 EVP_MD_CTX md_ctx;
2247 size_t sig_len;
2248 ECDSA_SIG *sig = NULL;
2249 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2250
2251 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2252 return ssl_do_write(s);
2253 }
2254
2255 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2256 EVP_PKEY *key = NULL;
2257 s->ctx->channel_id_cb(s, &key);
2258 if (key != NULL) {
2259 s->tlsext_channel_id_private = key;
2260 }
2261 }
2262
2263 if (!s->tlsext_channel_id_private) {
2264 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2265 return -1;
2266 }
2267 s->rwstate = SSL_NOTHING;
2268
2269 d = ssl_handshake_start(s);
2270 if (s->s3->tlsext_channel_id_new) {
2271 s2n(TLSEXT_TYPE_channel_id_new, d);
2272 } else {
2273 s2n(TLSEXT_TYPE_channel_id, d);
2274 }
2275 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2276
2277 EVP_MD_CTX_init(&md_ctx);
2278
2279 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2280 if (public_key_len <= 0) {
2281 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2282 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2283 goto err;
2284 }
2285
2286 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2287 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2288 * field elements as 32-byte, big-endian numbers. */
2289 if (public_key_len != 65) {
2290 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2291 goto err;
2292 }
2293 public_key = OPENSSL_malloc(public_key_len);
2294 if (!public_key) {
2295 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2296 goto err;
2297 }
2298
2299 derp = public_key;
2300 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2301
2302 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2303 s->tlsext_channel_id_private) != 1) {
2304 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2305 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2306 goto err;
2307 }
2308
2309 if (!tls1_channel_id_hash(&md_ctx, s)) {
2310 goto err;
2311 }
2312
2313 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2314 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2315 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2316 goto err;
2317 }
2318
2319 der_sig = OPENSSL_malloc(sig_len);
2320 if (!der_sig) {
2321 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2322 goto err;
2323 }
2324
2325 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2327 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2328 goto err;
2329 }
2330
2331 derp = der_sig;
2332 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2333 if (sig == NULL) {
2334 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2335 goto err;
2336 }
2337
2338 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2339 memcpy(d, public_key + 1, 64);
2340 d += 64;
2341 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2342 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2343 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2344 goto err;
2345 }
2346
David Benjaminfbdfefb2015-02-16 19:33:53 -05002347 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2348 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2349 goto err;
2350 }
Adam Langley1bea1732014-12-17 19:06:57 -08002351 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2352
2353 ret = ssl_do_write(s);
2354
2355err:
2356 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002357 OPENSSL_free(public_key);
2358 OPENSSL_free(der_sig);
2359 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002360
2361 return ret;
2362}
2363
2364int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2365 int i = 0;
2366 if (s->ctx->client_cert_cb) {
2367 i = s->ctx->client_cert_cb(s, px509, ppkey);
2368 }
2369 return i;
2370}