blob: 789abbdbf45ce44450936fbcab0a179e568a9835 [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
Adam Langley1bea1732014-12-17 19:06:57 -0800868 /* Don't digest cached records if no sigalgs: we may need them for client
869 * authentication. */
870 if (!SSL_USE_SIGALGS(s) &&
871 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
872 goto f_err;
873 }
David Benjamina03d95d2014-07-12 19:49:07 -0400874
Adam Langley1bea1732014-12-17 19:06:57 -0800875 /* Only the NULL compression algorithm is supported. */
876 if (compression_method != 0) {
877 al = SSL_AD_ILLEGAL_PARAMETER;
878 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
879 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
880 goto f_err;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langley1bea1732014-12-17 19:06:57 -0800883 /* TLS extensions */
884 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
885 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
886 goto err;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langley1bea1732014-12-17 19:06:57 -0800889 /* There should be nothing left over in the record. */
890 if (CBS_len(&server_hello) != 0) {
891 /* wrong packet length */
892 al = SSL_AD_DECODE_ERROR;
893 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
894 goto f_err;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langley1bea1732014-12-17 19:06:57 -0800897 return 1;
898
Adam Langley95c29f32014-06-20 12:00:00 -0700899f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800900 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700901err:
Adam Langley1bea1732014-12-17 19:06:57 -0800902 return -1;
903}
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langley1bea1732014-12-17 19:06:57 -0800905int ssl3_get_server_certificate(SSL *s) {
906 int al, i, ok, ret = -1;
907 unsigned long n;
908 X509 *x = NULL;
909 STACK_OF(X509) *sk = NULL;
910 SESS_CERT *sc;
911 EVP_PKEY *pkey = NULL;
912 CBS cbs, certificate_list;
913 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langley1bea1732014-12-17 19:06:57 -0800915 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400916 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500917 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langley1bea1732014-12-17 19:06:57 -0800919 if (!ok) {
920 return n;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langley1bea1732014-12-17 19:06:57 -0800923 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langley1bea1732014-12-17 19:06:57 -0800925 sk = sk_X509_new_null();
926 if (sk == NULL) {
927 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
928 goto err;
929 }
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langley1bea1732014-12-17 19:06:57 -0800931 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
932 CBS_len(&cbs) != 0) {
933 al = SSL_AD_DECODE_ERROR;
934 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
935 goto f_err;
936 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400937
Adam Langley1bea1732014-12-17 19:06:57 -0800938 while (CBS_len(&certificate_list) > 0) {
939 CBS certificate;
940 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
941 al = SSL_AD_DECODE_ERROR;
942 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
943 SSL_R_CERT_LENGTH_MISMATCH);
944 goto f_err;
945 }
946 data = CBS_data(&certificate);
947 x = d2i_X509(NULL, &data, CBS_len(&certificate));
948 if (x == NULL) {
949 al = SSL_AD_BAD_CERTIFICATE;
950 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
951 goto f_err;
952 }
953 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
954 al = SSL_AD_DECODE_ERROR;
955 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
956 SSL_R_CERT_LENGTH_MISMATCH);
957 goto f_err;
958 }
959 if (!sk_X509_push(sk, x)) {
960 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
961 goto err;
962 }
963 x = NULL;
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langley1bea1732014-12-17 19:06:57 -0800966 i = ssl_verify_cert_chain(s, sk);
967 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
968 al = ssl_verify_alarm_type(s->verify_result);
969 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
970 SSL_R_CERTIFICATE_VERIFY_FAILED);
971 goto f_err;
972 }
973 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langley1bea1732014-12-17 19:06:57 -0800975 sc = ssl_sess_cert_new();
976 if (sc == NULL) {
977 goto err;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
David Benjamin2755a3e2015-04-22 16:17:58 -0400980 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -0800981 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langley1bea1732014-12-17 19:06:57 -0800983 sc->cert_chain = sk;
984 /* Inconsistency alert: cert_chain does include the peer's certificate, which
985 * we don't include in s3_srvr.c */
986 x = sk_X509_value(sk, 0);
987 sk = NULL;
988 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langley1bea1732014-12-17 19:06:57 -0800990 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langley1bea1732014-12-17 19:06:57 -0800992 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
993 x = NULL;
994 al = SSL3_AL_FATAL;
995 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
996 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
997 goto f_err;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langley1bea1732014-12-17 19:06:57 -08001000 i = ssl_cert_type(pkey);
1001 if (i < 0) {
1002 x = NULL;
1003 al = SSL3_AL_FATAL;
1004 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1005 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1006 goto f_err;
1007 }
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langley1bea1732014-12-17 19:06:57 -08001009 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1010 if (exp_idx >= 0 && i != exp_idx) {
1011 x = NULL;
1012 al = SSL_AD_ILLEGAL_PARAMETER;
1013 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1014 SSL_R_WRONG_CERTIFICATE_TYPE);
1015 goto f_err;
1016 }
1017 sc->peer_cert_type = i;
David Benjamin2755a3e2015-04-22 16:17:58 -04001018 X509_free(sc->peer_pkeys[i].x509);
Adam Langley1bea1732014-12-17 19:06:57 -08001019 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1020 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001021
David Benjamin2755a3e2015-04-22 16:17:58 -04001022 X509_free(s->session->peer);
Adam Langley1bea1732014-12-17 19:06:57 -08001023 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001024
Adam Langley1bea1732014-12-17 19:06:57 -08001025 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langley1bea1732014-12-17 19:06:57 -08001027 x = NULL;
1028 ret = 1;
1029
1030 if (0) {
1031 f_err:
1032 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1033 }
1034
1035err:
1036 EVP_PKEY_free(pkey);
1037 X509_free(x);
1038 sk_X509_pop_free(sk, X509_free);
1039 return ret;
1040}
1041
1042int ssl3_get_server_key_exchange(SSL *s) {
1043 EVP_MD_CTX md_ctx;
1044 int al, ok;
1045 long n, alg_k, alg_a;
1046 EVP_PKEY *pkey = NULL;
1047 const EVP_MD *md = NULL;
1048 RSA *rsa = NULL;
1049 DH *dh = NULL;
1050 EC_KEY *ecdh = NULL;
1051 BN_CTX *bn_ctx = NULL;
1052 EC_POINT *srvr_ecpoint = NULL;
1053 CBS server_key_exchange, server_key_exchange_orig, parameter;
1054
1055 /* use same message size as in ssl3_get_certificate_request() as
1056 * ServerKeyExchange message may be skipped */
1057 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1058 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001059 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001060 if (!ok) {
1061 return n;
1062 }
1063
1064 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1065 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1066 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1067 SSL_R_UNEXPECTED_MESSAGE);
1068 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1069 return -1;
1070 }
1071
1072 /* In plain PSK ciphersuite, ServerKeyExchange can be
1073 omitted if no identity hint is sent. Set session->sess_cert anyway to
1074 avoid problems later.*/
1075 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1076 /* PSK ciphersuites that also send a Certificate would have already
1077 * initialized |sess_cert|. */
1078 if (s->session->sess_cert == NULL) {
1079 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001080 if (s->session->sess_cert == NULL) {
1081 return -1;
1082 }
Adam Langley1bea1732014-12-17 19:06:57 -08001083 }
1084
1085 /* TODO(davidben): This should be reset in one place with the rest of the
1086 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001087 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1088 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001089 }
1090 s->s3->tmp.reuse_message = 1;
1091 return 1;
1092 }
1093
1094 /* Retain a copy of the original CBS to compute the signature over. */
1095 CBS_init(&server_key_exchange, s->init_msg, n);
1096 server_key_exchange_orig = server_key_exchange;
1097
1098 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001099 DH_free(s->session->sess_cert->peer_dh_tmp);
1100 s->session->sess_cert->peer_dh_tmp = NULL;
1101 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1102 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001103 } else {
1104 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001105 if (s->session->sess_cert == NULL) {
1106 return -1;
1107 }
Adam Langley1bea1732014-12-17 19:06:57 -08001108 }
1109
1110 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1111 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1112 EVP_MD_CTX_init(&md_ctx);
1113
1114 if (alg_a & SSL_aPSK) {
1115 CBS psk_identity_hint;
1116
1117 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1118 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1119 &psk_identity_hint)) {
1120 al = SSL_AD_DECODE_ERROR;
1121 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1122 goto f_err;
1123 }
1124
1125 /* Store PSK identity hint for later use, hint is used in
1126 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1127 * identity hint can be as long as the maximum length of a PSK identity.
1128 * Also do not allow NULL characters; identities are saved as C strings.
1129 *
1130 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1131 * a specific identity. */
1132 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1133 CBS_contains_zero_byte(&psk_identity_hint)) {
1134 al = SSL_AD_HANDSHAKE_FAILURE;
1135 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1136 SSL_R_DATA_LENGTH_TOO_LONG);
1137 goto f_err;
1138 }
1139
1140 /* Save the identity hint as a C string. */
1141 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1142 al = SSL_AD_INTERNAL_ERROR;
1143 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1144 ERR_R_MALLOC_FAILURE);
1145 goto f_err;
1146 }
1147 }
1148
David Benjamin7061e282015-03-19 11:10:48 -04001149 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001150 CBS dh_p, dh_g, dh_Ys;
1151
1152 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1153 CBS_len(&dh_p) == 0 ||
1154 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1155 CBS_len(&dh_g) == 0 ||
1156 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1157 CBS_len(&dh_Ys) == 0) {
1158 al = SSL_AD_DECODE_ERROR;
1159 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1160 goto f_err;
1161 }
1162
1163 dh = DH_new();
1164 if (dh == NULL) {
1165 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1166 goto err;
1167 }
1168
1169 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1170 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1171 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1172 NULL) {
1173 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1174 goto err;
1175 }
1176
Adam Langleya7997f12015-05-14 17:38:50 -07001177 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001178 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1179 SSL_R_BAD_DH_P_LENGTH);
1180 goto err;
1181 }
1182
1183 if (alg_a & SSL_aRSA) {
1184 pkey = X509_get_pubkey(
1185 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1186 }
1187 /* else anonymous DH, so no certificate or pkey. */
1188
1189 s->session->sess_cert->peer_dh_tmp = dh;
1190 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001191 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001192 uint16_t curve_id;
1193 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001194 const EC_GROUP *group;
1195 CBS point;
1196
1197 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1198 * key. Check curve is one of our preferences, if not server has sent an
1199 * invalid curve. */
1200 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1201 al = SSL_AD_DECODE_ERROR;
1202 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1203 goto f_err;
1204 }
1205
1206 curve_nid = tls1_ec_curve_id2nid(curve_id);
1207 if (curve_nid == 0) {
1208 al = SSL_AD_INTERNAL_ERROR;
1209 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1210 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1211 goto f_err;
1212 }
1213
David Benjamin4f7783e2015-03-05 03:05:11 -05001214 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001215 if (ecdh == NULL) {
1216 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001217 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001218 goto err;
1219 }
1220
Adam Langley1bea1732014-12-17 19:06:57 -08001221 group = EC_KEY_get0_group(ecdh);
1222
1223 /* Next, get the encoded ECPoint */
1224 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1225 al = SSL_AD_DECODE_ERROR;
1226 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1227 goto f_err;
1228 }
1229
1230 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1231 ((bn_ctx = BN_CTX_new()) == NULL)) {
1232 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1233 ERR_R_MALLOC_FAILURE);
1234 goto err;
1235 }
1236
1237 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1238 CBS_len(&point), bn_ctx)) {
1239 al = SSL_AD_DECODE_ERROR;
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1241 goto f_err;
1242 }
1243
1244 /* The ECC/TLS specification does not mention the use of DSA to sign
1245 * ECParameters in the server key exchange message. We do support RSA and
1246 * ECDSA. */
1247 if (alg_a & SSL_aRSA) {
1248 pkey = X509_get_pubkey(
1249 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1250 } else if (alg_a & SSL_aECDSA) {
1251 pkey =
1252 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1253 }
1254 /* else anonymous ECDH, so no certificate or pkey. */
1255 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1256 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1257 ecdh = NULL;
1258 BN_CTX_free(bn_ctx);
1259 bn_ctx = NULL;
1260 EC_POINT_free(srvr_ecpoint);
1261 srvr_ecpoint = NULL;
1262 } else if (!(alg_k & SSL_kPSK)) {
1263 al = SSL_AD_UNEXPECTED_MESSAGE;
1264 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1265 SSL_R_UNEXPECTED_MESSAGE);
1266 goto f_err;
1267 }
1268
1269 /* At this point, |server_key_exchange| contains the signature, if any, while
1270 * |server_key_exchange_orig| contains the entire message. From that, derive
1271 * a CBS containing just the parameter. */
1272 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1273 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1274
1275 /* if it was signed, check the signature */
1276 if (pkey != NULL) {
1277 CBS signature;
1278
1279 if (SSL_USE_SIGALGS(s)) {
1280 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1281 goto f_err;
1282 }
1283 } else if (pkey->type == EVP_PKEY_RSA) {
1284 md = EVP_md5_sha1();
1285 } else {
1286 md = EVP_sha1();
1287 }
1288
1289 /* The last field in |server_key_exchange| is the signature. */
1290 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1291 CBS_len(&server_key_exchange) != 0) {
1292 al = SSL_AD_DECODE_ERROR;
1293 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1294 goto f_err;
1295 }
1296
1297 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1298 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1299 SSL3_RANDOM_SIZE) ||
1300 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1301 SSL3_RANDOM_SIZE) ||
1302 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1303 CBS_len(&parameter)) ||
1304 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1305 CBS_len(&signature))) {
1306 /* bad signature */
1307 al = SSL_AD_DECRYPT_ERROR;
1308 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1309 goto f_err;
1310 }
1311 } else {
1312 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1313 /* Might be wrong key type, check it */
1314 if (ssl3_check_cert_and_algorithm(s)) {
1315 /* Otherwise this shouldn't happen */
1316 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1317 ERR_R_INTERNAL_ERROR);
1318 }
1319 goto err;
1320 }
1321 /* still data left over */
1322 if (CBS_len(&server_key_exchange) > 0) {
1323 al = SSL_AD_DECODE_ERROR;
1324 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1325 SSL_R_EXTRA_DATA_IN_MESSAGE);
1326 goto f_err;
1327 }
1328 }
1329 EVP_PKEY_free(pkey);
1330 EVP_MD_CTX_cleanup(&md_ctx);
1331 return 1;
1332
Adam Langley95c29f32014-06-20 12:00:00 -07001333f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001334 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001335err:
Adam Langley1bea1732014-12-17 19:06:57 -08001336 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001337 RSA_free(rsa);
1338 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001339 BN_CTX_free(bn_ctx);
1340 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001341 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001342 EVP_MD_CTX_cleanup(&md_ctx);
1343 return -1;
1344}
Adam Langley95c29f32014-06-20 12:00:00 -07001345
Adam Langley1bea1732014-12-17 19:06:57 -08001346static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1347 return X509_NAME_cmp(*a, *b);
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langley1bea1732014-12-17 19:06:57 -08001350int ssl3_get_certificate_request(SSL *s) {
1351 int ok, ret = 0;
1352 unsigned long n;
1353 X509_NAME *xn = NULL;
1354 STACK_OF(X509_NAME) *ca_sk = NULL;
1355 CBS cbs;
1356 CBS certificate_types;
1357 CBS certificate_authorities;
1358 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langley1bea1732014-12-17 19:06:57 -08001360 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1361 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001362 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001363
Adam Langley1bea1732014-12-17 19:06:57 -08001364 if (!ok) {
1365 return n;
1366 }
David Benjamin688d8df2014-11-02 23:06:42 -05001367
Adam Langley1bea1732014-12-17 19:06:57 -08001368 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langley1bea1732014-12-17 19:06:57 -08001370 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1371 s->s3->tmp.reuse_message = 1;
1372 /* If we get here we don't need any cached handshake records as we wont be
1373 * doing client auth. */
1374 if (s->s3->handshake_buffer &&
1375 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1376 goto err;
1377 }
1378 return 1;
1379 }
David Benjamined439582014-07-14 19:13:02 -04001380
Adam Langley1bea1732014-12-17 19:06:57 -08001381 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1382 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1383 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1384 SSL_R_WRONG_MESSAGE_TYPE);
1385 goto err;
1386 }
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langley1bea1732014-12-17 19:06:57 -08001388 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001389
Adam Langley1bea1732014-12-17 19:06:57 -08001390 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1391 if (ca_sk == NULL) {
1392 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1393 goto err;
1394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langley1bea1732014-12-17 19:06:57 -08001396 /* get the certificate types */
1397 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1398 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1399 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1400 goto err;
1401 }
David Benjamined439582014-07-14 19:13:02 -04001402
Adam Langley1bea1732014-12-17 19:06:57 -08001403 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1404 &s->s3->tmp.num_certificate_types)) {
1405 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1406 goto err;
1407 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001408
Adam Langley1bea1732014-12-17 19:06:57 -08001409 if (SSL_USE_SIGALGS(s)) {
1410 CBS supported_signature_algorithms;
1411 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1412 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1413 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1414 goto err;
1415 }
David Benjamined439582014-07-14 19:13:02 -04001416
Adam Langley1bea1732014-12-17 19:06:57 -08001417 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1418 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1419 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1420 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1421 goto err;
1422 }
1423 }
David Benjamined439582014-07-14 19:13:02 -04001424
Adam Langley1bea1732014-12-17 19:06:57 -08001425 /* get the CA RDNs */
1426 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1427 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1428 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1429 goto err;
1430 }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
Adam Langley1bea1732014-12-17 19:06:57 -08001432 while (CBS_len(&certificate_authorities) > 0) {
1433 CBS distinguished_name;
1434 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1435 &distinguished_name)) {
1436 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1437 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1438 SSL_R_CA_DN_TOO_LONG);
1439 goto err;
1440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langley1bea1732014-12-17 19:06:57 -08001442 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001443
Adam Langley1bea1732014-12-17 19:06:57 -08001444 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1445 if (xn == NULL) {
1446 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1447 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1448 goto err;
1449 }
Adam Langley95c29f32014-06-20 12:00:00 -07001450
Adam Langley1bea1732014-12-17 19:06:57 -08001451 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1452 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001453 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001454 goto err;
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langley1bea1732014-12-17 19:06:57 -08001457 if (CBS_len(&distinguished_name) != 0) {
1458 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1459 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1460 SSL_R_CA_DN_LENGTH_MISMATCH);
1461 goto err;
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langley1bea1732014-12-17 19:06:57 -08001464 if (!sk_X509_NAME_push(ca_sk, xn)) {
1465 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1466 ERR_R_MALLOC_FAILURE);
1467 goto err;
1468 }
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
Adam Langley1bea1732014-12-17 19:06:57 -08001471 /* we should setup a certificate to return.... */
1472 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001473 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001474 s->s3->tmp.ca_names = ca_sk;
1475 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langley1bea1732014-12-17 19:06:57 -08001477 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001478
Adam Langley1bea1732014-12-17 19:06:57 -08001479err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001480 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001481 return ret;
1482}
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langley1bea1732014-12-17 19:06:57 -08001484int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001485 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001486 long n;
1487 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001488
Adam Langley1bea1732014-12-17 19:06:57 -08001489 n = s->method->ssl_get_message(
1490 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001491 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langley1bea1732014-12-17 19:06:57 -08001493 if (!ok) {
1494 return n;
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langley1bea1732014-12-17 19:06:57 -08001497 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001498
Adam Langley1bea1732014-12-17 19:06:57 -08001499 if (!CBS_get_u32(&new_session_ticket,
1500 &s->session->tlsext_tick_lifetime_hint) ||
1501 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1502 CBS_len(&new_session_ticket) != 0) {
1503 al = SSL_AD_DECODE_ERROR;
1504 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1505 goto f_err;
1506 }
Adam Langley95c29f32014-06-20 12:00:00 -07001507
Adam Langley1bea1732014-12-17 19:06:57 -08001508 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1509 &s->session->tlsext_ticklen)) {
1510 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1511 goto err;
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjamin68070622015-02-08 23:44:59 -05001514 /* Generate a session ID for this session based on the session ticket. We use
1515 * the session ID mechanism for detecting ticket resumption. This also fits in
1516 * with assumptions elsewhere in OpenSSL.*/
1517 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1518 &s->session->session_id_length, EVP_sha256(), NULL)) {
1519 goto err;
1520 }
1521
1522 return 1;
David Benjamined439582014-07-14 19:13:02 -04001523
Adam Langley95c29f32014-06-20 12:00:00 -07001524f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001525 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001526err:
Adam Langley1bea1732014-12-17 19:06:57 -08001527 return -1;
1528}
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langley1bea1732014-12-17 19:06:57 -08001530int ssl3_get_cert_status(SSL *s) {
1531 int ok, al;
1532 long n;
1533 CBS certificate_status, ocsp_response;
1534 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langley1bea1732014-12-17 19:06:57 -08001536 n = s->method->ssl_get_message(
1537 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001538 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001539
Adam Langley1bea1732014-12-17 19:06:57 -08001540 if (!ok) {
1541 return n;
1542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
David Benjamindcd979f2015-04-20 18:26:52 -04001544 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1545 /* A server may send status_request in ServerHello and then change
1546 * its mind about sending CertificateStatus. */
1547 s->s3->tmp.reuse_message = 1;
1548 return 1;
1549 }
1550
Adam Langley1bea1732014-12-17 19:06:57 -08001551 CBS_init(&certificate_status, s->init_msg, n);
1552 if (!CBS_get_u8(&certificate_status, &status_type) ||
1553 status_type != TLSEXT_STATUSTYPE_ocsp ||
1554 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1555 CBS_len(&ocsp_response) == 0 ||
1556 CBS_len(&certificate_status) != 0) {
1557 al = SSL_AD_DECODE_ERROR;
1558 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1559 goto f_err;
1560 }
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langley1bea1732014-12-17 19:06:57 -08001562 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1563 &s->session->ocsp_response_length)) {
1564 al = SSL_AD_INTERNAL_ERROR;
1565 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1566 goto f_err;
1567 }
1568 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001569
Adam Langley95c29f32014-06-20 12:00:00 -07001570f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001571 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1572 return -1;
1573}
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langley1bea1732014-12-17 19:06:57 -08001575int ssl3_get_server_done(SSL *s) {
1576 int ok;
1577 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langley1bea1732014-12-17 19:06:57 -08001579 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1580 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1581 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001582 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langley1bea1732014-12-17 19:06:57 -08001584 if (!ok) {
1585 return n;
1586 }
David Benjamin46062682014-07-14 19:14:32 -04001587
Adam Langley1bea1732014-12-17 19:06:57 -08001588 if (n > 0) {
1589 /* should contain no data */
1590 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1591 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1592 return -1;
1593 }
David Benjamin46062682014-07-14 19:14:32 -04001594
Adam Langley1bea1732014-12-17 19:06:57 -08001595 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001596}
Adam Langley1258b6a2014-06-20 12:00:00 -07001597
Adam Langley1258b6a2014-06-20 12:00:00 -07001598
Adam Langley1bea1732014-12-17 19:06:57 -08001599int ssl3_send_client_key_exchange(SSL *s) {
1600 uint8_t *p;
1601 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001602 uint32_t alg_k;
1603 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001604 uint8_t *q;
1605 EVP_PKEY *pkey = NULL;
1606 EC_KEY *clnt_ecdh = NULL;
1607 const EC_POINT *srvr_ecpoint = NULL;
1608 EVP_PKEY *srvr_pub_pkey = NULL;
1609 uint8_t *encodedPoint = NULL;
1610 int encoded_pt_len = 0;
1611 BN_CTX *bn_ctx = NULL;
1612 unsigned int psk_len = 0;
1613 uint8_t psk[PSK_MAX_PSK_LEN];
1614 uint8_t *pms = NULL;
1615 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001616
Adam Langley1bea1732014-12-17 19:06:57 -08001617 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1618 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001619
Adam Langley1bea1732014-12-17 19:06:57 -08001620 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1621 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001622
Adam Langley1bea1732014-12-17 19:06:57 -08001623 /* If using a PSK key exchange, prepare the pre-shared key. */
1624 if (alg_a & SSL_aPSK) {
1625 char identity[PSK_MAX_IDENTITY_LEN + 1];
1626 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001627
Adam Langley1bea1732014-12-17 19:06:57 -08001628 if (s->psk_client_callback == NULL) {
1629 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1630 SSL_R_PSK_NO_CLIENT_CB);
1631 goto err;
1632 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001633
Adam Langley1bea1732014-12-17 19:06:57 -08001634 memset(identity, 0, sizeof(identity));
1635 psk_len =
1636 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1637 sizeof(identity), psk, sizeof(psk));
1638 if (psk_len > PSK_MAX_PSK_LEN) {
1639 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1640 ERR_R_INTERNAL_ERROR);
1641 goto err;
1642 } else if (psk_len == 0) {
1643 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1644 SSL_R_PSK_IDENTITY_NOT_FOUND);
1645 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1646 goto err;
1647 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001648
Adam Langley1bea1732014-12-17 19:06:57 -08001649 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1650 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1651 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1652 ERR_R_INTERNAL_ERROR);
1653 goto err;
1654 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001655
David Benjamin2755a3e2015-04-22 16:17:58 -04001656 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001657 s->session->psk_identity = BUF_strdup(identity);
1658 if (s->session->psk_identity == NULL) {
1659 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1660 ERR_R_MALLOC_FAILURE);
1661 goto err;
1662 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001663
Adam Langley1bea1732014-12-17 19:06:57 -08001664 /* Write out psk_identity. */
1665 s2n(identity_len, p);
1666 memcpy(p, identity, identity_len);
1667 p += identity_len;
1668 n = 2 + identity_len;
1669 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langley1bea1732014-12-17 19:06:57 -08001671 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1672 if (alg_k & SSL_kRSA) {
1673 RSA *rsa;
1674 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001675
Adam Langley1bea1732014-12-17 19:06:57 -08001676 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1677 pms = OPENSSL_malloc(pms_len);
1678 if (pms == NULL) {
1679 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1680 ERR_R_MALLOC_FAILURE);
1681 goto err;
1682 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001683
Adam Langley1bea1732014-12-17 19:06:57 -08001684 if (s->session->sess_cert == NULL) {
1685 /* We should always have a server certificate with SSL_kRSA. */
1686 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1687 ERR_R_INTERNAL_ERROR);
1688 goto err;
1689 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001690
Adam Langley1bea1732014-12-17 19:06:57 -08001691 pkey = X509_get_pubkey(
1692 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1693 if (pkey == NULL ||
1694 pkey->type != EVP_PKEY_RSA ||
1695 pkey->pkey.rsa == NULL) {
1696 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1697 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001698 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001699 goto err;
1700 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001701
Adam Langley1bea1732014-12-17 19:06:57 -08001702 rsa = pkey->pkey.rsa;
1703 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001704
Adam Langley1bea1732014-12-17 19:06:57 -08001705 pms[0] = s->client_version >> 8;
1706 pms[1] = s->client_version & 0xff;
1707 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1708 goto err;
1709 }
1710
1711 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1712
1713 q = p;
1714 /* In TLS and beyond, reserve space for the length prefix. */
1715 if (s->version > SSL3_VERSION) {
1716 p += 2;
1717 n += 2;
1718 }
1719 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1720 RSA_PKCS1_PADDING)) {
1721 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1722 SSL_R_BAD_RSA_ENCRYPT);
1723 goto err;
1724 }
1725 n += enc_pms_len;
1726
1727 /* Log the premaster secret, if logging is enabled. */
1728 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1729 pms_len)) {
1730 goto err;
1731 }
1732
1733 /* Fill in the length prefix. */
1734 if (s->version > SSL3_VERSION) {
1735 s2n(enc_pms_len, q);
1736 }
David Benjamin7061e282015-03-19 11:10:48 -04001737 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001738 DH *dh_srvr, *dh_clnt;
1739 SESS_CERT *scert = s->session->sess_cert;
1740 int dh_len;
1741 size_t pub_len;
1742
1743 if (scert == NULL) {
1744 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1745 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1746 SSL_R_UNEXPECTED_MESSAGE);
1747 goto err;
1748 }
1749
1750 if (scert->peer_dh_tmp == NULL) {
1751 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1752 ERR_R_INTERNAL_ERROR);
1753 goto err;
1754 }
1755 dh_srvr = scert->peer_dh_tmp;
1756
1757 /* generate a new random key */
1758 dh_clnt = DHparams_dup(dh_srvr);
1759 if (dh_clnt == NULL) {
1760 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1761 goto err;
1762 }
1763 if (!DH_generate_key(dh_clnt)) {
1764 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1765 DH_free(dh_clnt);
1766 goto err;
1767 }
1768
1769 pms_len = DH_size(dh_clnt);
1770 pms = OPENSSL_malloc(pms_len);
1771 if (pms == NULL) {
1772 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1773 ERR_R_MALLOC_FAILURE);
1774 DH_free(dh_clnt);
1775 goto err;
1776 }
1777
1778 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1779 if (dh_len <= 0) {
1780 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1781 DH_free(dh_clnt);
1782 goto err;
1783 }
1784 pms_len = dh_len;
1785
1786 /* send off the data */
1787 pub_len = BN_num_bytes(dh_clnt->pub_key);
1788 s2n(pub_len, p);
1789 BN_bn2bin(dh_clnt->pub_key, p);
1790 n += 2 + pub_len;
1791
1792 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001793 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001794 const EC_GROUP *srvr_group = NULL;
1795 EC_KEY *tkey;
1796 int field_size = 0, ecdh_len;
1797
1798 if (s->session->sess_cert == NULL) {
1799 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1800 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1801 SSL_R_UNEXPECTED_MESSAGE);
1802 goto err;
1803 }
1804
1805 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1806 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1807 ERR_R_INTERNAL_ERROR);
1808 goto err;
1809 }
1810
1811 tkey = s->session->sess_cert->peer_ecdh_tmp;
1812
1813 srvr_group = EC_KEY_get0_group(tkey);
1814 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1815 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1816 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1817 ERR_R_INTERNAL_ERROR);
1818 goto err;
1819 }
1820
1821 clnt_ecdh = EC_KEY_new();
1822 if (clnt_ecdh == NULL) {
1823 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1824 ERR_R_MALLOC_FAILURE);
1825 goto err;
1826 }
1827
1828 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1829 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1830 goto err;
1831 }
1832
1833 /* Generate a new ECDH key pair */
1834 if (!EC_KEY_generate_key(clnt_ecdh)) {
1835 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1836 goto err;
1837 }
1838
1839 field_size = EC_GROUP_get_degree(srvr_group);
1840 if (field_size <= 0) {
1841 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1842 goto err;
1843 }
1844
1845 pms_len = (field_size + 7) / 8;
1846 pms = OPENSSL_malloc(pms_len);
1847 if (pms == NULL) {
1848 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1849 ERR_R_MALLOC_FAILURE);
1850 goto err;
1851 }
1852
1853 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1854 if (ecdh_len <= 0) {
1855 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1856 goto err;
1857 }
1858 pms_len = ecdh_len;
1859
1860 /* First check the size of encoding and allocate memory accordingly. */
1861 encoded_pt_len =
1862 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1863 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1864
1865 encodedPoint =
1866 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1867 bn_ctx = BN_CTX_new();
1868 if (encodedPoint == NULL || bn_ctx == NULL) {
1869 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1870 ERR_R_MALLOC_FAILURE);
1871 goto err;
1872 }
1873
1874 /* Encode the public key */
1875 encoded_pt_len = EC_POINT_point2oct(
1876 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1877 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1878
1879 *p = encoded_pt_len; /* length of encoded point */
1880 /* Encoded point will be copied here */
1881 p += 1;
1882 n += 1;
1883 /* copy the point */
1884 memcpy(p, encodedPoint, encoded_pt_len);
1885 /* increment n to account for length field */
1886 n += encoded_pt_len;
1887
1888 /* Free allocated memory */
1889 BN_CTX_free(bn_ctx);
1890 bn_ctx = NULL;
1891 OPENSSL_free(encodedPoint);
1892 encodedPoint = NULL;
1893 EC_KEY_free(clnt_ecdh);
1894 clnt_ecdh = NULL;
1895 EVP_PKEY_free(srvr_pub_pkey);
1896 srvr_pub_pkey = NULL;
1897 } else if (alg_k & SSL_kPSK) {
1898 /* For plain PSK, other_secret is a block of 0s with the same length as
1899 * the pre-shared key. */
1900 pms_len = psk_len;
1901 pms = OPENSSL_malloc(pms_len);
1902 if (pms == NULL) {
1903 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1904 ERR_R_MALLOC_FAILURE);
1905 goto err;
1906 }
1907 memset(pms, 0, pms_len);
1908 } else {
1909 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1910 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1911 ERR_R_INTERNAL_ERROR);
1912 goto err;
1913 }
1914
1915 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1916 * key. */
1917 if (alg_a & SSL_aPSK) {
1918 CBB cbb, child;
1919 uint8_t *new_pms;
1920 size_t new_pms_len;
1921
1922 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1923 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1924 ERR_R_MALLOC_FAILURE);
1925 goto err;
1926 }
1927 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1928 !CBB_add_bytes(&child, pms, pms_len) ||
1929 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1930 !CBB_add_bytes(&child, psk, psk_len) ||
1931 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1932 CBB_cleanup(&cbb);
1933 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1934 ERR_R_INTERNAL_ERROR);
1935 goto err;
1936 }
1937 OPENSSL_cleanse(pms, pms_len);
1938 OPENSSL_free(pms);
1939 pms = new_pms;
1940 pms_len = new_pms_len;
1941 }
1942
1943 /* The message must be added to the finished hash before calculating the
1944 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001945 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1946 goto err;
1947 }
Adam Langley1bea1732014-12-17 19:06:57 -08001948 s->state = SSL3_ST_CW_KEY_EXCH_B;
1949
1950 s->session->master_key_length = s->enc_method->generate_master_secret(
1951 s, s->session->master_key, pms, pms_len);
1952 if (s->session->master_key_length == 0) {
1953 goto err;
1954 }
1955 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1956 OPENSSL_cleanse(pms, pms_len);
1957 OPENSSL_free(pms);
1958 }
1959
1960 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001961 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001962
1963err:
Adam Langley1bea1732014-12-17 19:06:57 -08001964 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001965 OPENSSL_free(encodedPoint);
1966 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001967 EVP_PKEY_free(srvr_pub_pkey);
1968 if (pms) {
1969 OPENSSL_cleanse(pms, pms_len);
1970 OPENSSL_free(pms);
1971 }
1972 return -1;
1973}
Adam Langley1258b6a2014-06-20 12:00:00 -07001974
Adam Langley1bea1732014-12-17 19:06:57 -08001975int ssl3_send_cert_verify(SSL *s) {
1976 uint8_t *buf, *p;
1977 const EVP_MD *md = NULL;
1978 uint8_t digest[EVP_MAX_MD_SIZE];
1979 size_t digest_length;
1980 EVP_PKEY *pkey;
1981 EVP_PKEY_CTX *pctx = NULL;
1982 size_t signature_length = 0;
1983 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001984
Adam Langley1bea1732014-12-17 19:06:57 -08001985 buf = (uint8_t *)s->init_buf->data;
1986
1987 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1988 p = ssl_handshake_start(s);
1989 pkey = s->cert->key->privatekey;
1990
1991 /* Write out the digest type if needbe. */
1992 if (SSL_USE_SIGALGS(s)) {
1993 md = tls1_choose_signing_digest(s, pkey);
1994 if (!tls12_get_sigandhash(p, pkey, md)) {
1995 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
1996 goto err;
1997 }
1998 p += 2;
1999 n += 2;
2000 }
2001
2002 /* Compute the digest. */
2003 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2004 goto err;
2005 }
2006
2007 /* The handshake buffer is no longer necessary. */
2008 if (s->s3->handshake_buffer &&
2009 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2010 goto err;
2011 }
2012
2013 /* Sign the digest. */
2014 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2015 if (pctx == NULL) {
2016 goto err;
2017 }
2018
2019 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2020 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2021 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2022 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2023 goto err;
2024 }
2025
2026 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2027 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2028 goto err;
2029 }
2030
2031 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2032 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2033 goto err;
2034 }
2035
2036 s2n(signature_length, p);
2037 n += signature_length + 2;
2038
David Benjaminfbdfefb2015-02-16 19:33:53 -05002039 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2040 goto err;
2041 }
Adam Langley1bea1732014-12-17 19:06:57 -08002042 s->state = SSL3_ST_CW_CERT_VRFY_B;
2043 }
2044
2045 EVP_PKEY_CTX_free(pctx);
2046 return ssl_do_write(s);
2047
2048err:
2049 EVP_PKEY_CTX_free(pctx);
2050 return -1;
2051}
2052
2053/* ssl3_has_client_certificate returns true if a client certificate is
2054 * configured. */
2055static int ssl3_has_client_certificate(SSL *s) {
2056 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2057}
2058
2059int ssl3_send_client_certificate(SSL *s) {
2060 X509 *x509 = NULL;
2061 EVP_PKEY *pkey = NULL;
2062 int i;
2063
2064 if (s->state == SSL3_ST_CW_CERT_A) {
2065 /* Let cert callback update client certificates if required */
2066 if (s->cert->cert_cb) {
2067 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2068 if (i < 0) {
2069 s->rwstate = SSL_X509_LOOKUP;
2070 return -1;
2071 }
2072 if (i == 0) {
2073 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2074 return 0;
2075 }
2076 s->rwstate = SSL_NOTHING;
2077 }
2078
2079 if (ssl3_has_client_certificate(s)) {
2080 s->state = SSL3_ST_CW_CERT_C;
2081 } else {
2082 s->state = SSL3_ST_CW_CERT_B;
2083 }
2084 }
2085
2086 /* We need to get a client cert */
2087 if (s->state == SSL3_ST_CW_CERT_B) {
2088 /* If we get an error, we need to:
2089 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2090 * We then get retried later */
2091 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2092 if (i < 0) {
2093 s->rwstate = SSL_X509_LOOKUP;
2094 return -1;
2095 }
2096 s->rwstate = SSL_NOTHING;
2097 if (i == 1 && pkey != NULL && x509 != NULL) {
2098 s->state = SSL3_ST_CW_CERT_B;
2099 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2100 i = 0;
2101 }
2102 } else if (i == 1) {
2103 i = 0;
2104 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2105 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2106 }
2107
David Benjamin2755a3e2015-04-22 16:17:58 -04002108 X509_free(x509);
2109 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002110 if (i && !ssl3_has_client_certificate(s)) {
2111 i = 0;
2112 }
2113 if (i == 0) {
2114 if (s->version == SSL3_VERSION) {
2115 s->s3->tmp.cert_req = 0;
2116 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2117 return 1;
2118 } else {
2119 s->s3->tmp.cert_req = 2;
2120 }
2121 }
2122
2123 /* Ok, we have a cert */
2124 s->state = SSL3_ST_CW_CERT_C;
2125 }
2126
2127 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002128 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2129 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2130 return -1;
2131 }
Adam Langley1bea1732014-12-17 19:06:57 -08002132 }
2133
2134 /* SSL3_ST_CW_CERT_D */
2135 return ssl_do_write(s);
2136}
2137
2138#define has_bits(i, m) (((i) & (m)) == (m))
2139
2140int ssl3_check_cert_and_algorithm(SSL *s) {
2141 int i, idx;
2142 long alg_k, alg_a;
2143 EVP_PKEY *pkey = NULL;
2144 SESS_CERT *sc;
2145 DH *dh;
2146
2147 /* we don't have a certificate */
2148 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2149 return 1;
2150 }
2151
2152 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2153 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2154
2155 sc = s->session->sess_cert;
2156 if (sc == NULL) {
2157 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2158 goto err;
2159 }
2160
2161 dh = s->session->sess_cert->peer_dh_tmp;
2162
2163 /* This is the passed certificate */
2164
2165 idx = sc->peer_cert_type;
2166 if (idx == SSL_PKEY_ECC) {
2167 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2168 /* check failed */
2169 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2170 goto f_err;
2171 } else {
2172 return 1;
2173 }
2174 } else if (alg_a & SSL_aECDSA) {
2175 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2176 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2177 goto f_err;
2178 }
2179 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2180 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2181 EVP_PKEY_free(pkey);
2182
2183 /* Check that we have a certificate if we require one */
2184 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2185 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2186 SSL_R_MISSING_RSA_SIGNING_CERT);
2187 goto f_err;
2188 }
2189
2190 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2191 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2192 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2193 goto f_err;
2194 }
2195
David Benjamin7061e282015-03-19 11:10:48 -04002196 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002197 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2198 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2199 goto f_err;
2200 }
2201
2202 return 1;
2203
2204f_err:
2205 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2206err:
2207 return 0;
2208}
2209
2210int ssl3_send_next_proto(SSL *s) {
2211 unsigned int len, padding_len;
2212 uint8_t *d, *p;
2213
2214 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2215 len = s->next_proto_negotiated_len;
2216 padding_len = 32 - ((len + 2) % 32);
2217
2218 d = p = ssl_handshake_start(s);
2219 *(p++) = len;
2220 memcpy(p, s->next_proto_negotiated, len);
2221 p += len;
2222 *(p++) = padding_len;
2223 memset(p, 0, padding_len);
2224 p += padding_len;
2225
David Benjaminfbdfefb2015-02-16 19:33:53 -05002226 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2227 return -1;
2228 }
Adam Langley1bea1732014-12-17 19:06:57 -08002229 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2230 }
2231
2232 return ssl_do_write(s);
2233}
2234
2235int ssl3_send_channel_id(SSL *s) {
2236 uint8_t *d;
2237 int ret = -1, public_key_len;
2238 EVP_MD_CTX md_ctx;
2239 size_t sig_len;
2240 ECDSA_SIG *sig = NULL;
2241 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2242
2243 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2244 return ssl_do_write(s);
2245 }
2246
2247 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2248 EVP_PKEY *key = NULL;
2249 s->ctx->channel_id_cb(s, &key);
2250 if (key != NULL) {
2251 s->tlsext_channel_id_private = key;
2252 }
2253 }
2254
2255 if (!s->tlsext_channel_id_private) {
2256 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2257 return -1;
2258 }
2259 s->rwstate = SSL_NOTHING;
2260
2261 d = ssl_handshake_start(s);
2262 if (s->s3->tlsext_channel_id_new) {
2263 s2n(TLSEXT_TYPE_channel_id_new, d);
2264 } else {
2265 s2n(TLSEXT_TYPE_channel_id, d);
2266 }
2267 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2268
2269 EVP_MD_CTX_init(&md_ctx);
2270
2271 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2272 if (public_key_len <= 0) {
2273 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2274 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2275 goto err;
2276 }
2277
2278 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2279 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2280 * field elements as 32-byte, big-endian numbers. */
2281 if (public_key_len != 65) {
2282 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2283 goto err;
2284 }
2285 public_key = OPENSSL_malloc(public_key_len);
2286 if (!public_key) {
2287 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2288 goto err;
2289 }
2290
2291 derp = public_key;
2292 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2293
2294 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2295 s->tlsext_channel_id_private) != 1) {
2296 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2297 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2298 goto err;
2299 }
2300
2301 if (!tls1_channel_id_hash(&md_ctx, s)) {
2302 goto err;
2303 }
2304
2305 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2306 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2307 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2308 goto err;
2309 }
2310
2311 der_sig = OPENSSL_malloc(sig_len);
2312 if (!der_sig) {
2313 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2314 goto err;
2315 }
2316
2317 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2318 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2319 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2320 goto err;
2321 }
2322
2323 derp = der_sig;
2324 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2325 if (sig == NULL) {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2327 goto err;
2328 }
2329
2330 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2331 memcpy(d, public_key + 1, 64);
2332 d += 64;
2333 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2334 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2335 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2336 goto err;
2337 }
2338
David Benjaminfbdfefb2015-02-16 19:33:53 -05002339 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2340 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2341 goto err;
2342 }
Adam Langley1bea1732014-12-17 19:06:57 -08002343 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2344
2345 ret = ssl_do_write(s);
2346
2347err:
2348 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002349 OPENSSL_free(public_key);
2350 OPENSSL_free(der_sig);
2351 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002352
2353 return ret;
2354}
2355
2356int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2357 int i = 0;
2358 if (s->ctx->client_cert_cb) {
2359 i = s->ctx->client_cert_cb(s, px509, ppkey);
2360 }
2361 return i;
2362}