blob: 8bf3f0d4ac601815a8f2702522a562053faa9765 [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) {
196 case SSL_ST_RENEGOTIATE:
197 s->renegotiate = 1;
198 s->state = SSL_ST_CONNECT;
Adam Langley1bea1732014-12-17 19:06:57 -0800199 /* fallthrough */
200 case SSL_ST_CONNECT:
201 case SSL_ST_BEFORE | SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500202 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800203 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjaminb16346b2015-04-08 19:16:58 -0400206 if ((s->version >> 8) != 3) {
207 /* TODO(davidben): Some consumers clear |s->version| to break the
208 * handshake in a callback. Remove this when they're using proper
209 * APIs. */
210 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
211 ret = -1;
212 goto end;
213 }
214
Adam Langley1bea1732014-12-17 19:06:57 -0800215 if (s->init_buf == NULL) {
216 buf = BUF_MEM_new();
217 if (buf == NULL ||
218 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
219 ret = -1;
220 goto end;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley1bea1732014-12-17 19:06:57 -0800223 s->init_buf = buf;
224 buf = NULL;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley1bea1732014-12-17 19:06:57 -0800227 if (!ssl3_setup_buffers(s) ||
228 !ssl_init_wbio_buffer(s, 0)) {
229 ret = -1;
230 goto end;
231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley1bea1732014-12-17 19:06:57 -0800233 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley1bea1732014-12-17 19:06:57 -0800235 if (!ssl3_init_finished_mac(s)) {
236 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
237 ret = -1;
238 goto end;
239 }
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley1bea1732014-12-17 19:06:57 -0800241 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800242 s->init_num = 0;
243 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley1bea1732014-12-17 19:06:57 -0800245 case SSL3_ST_CW_CLNT_HELLO_A:
246 case SSL3_ST_CW_CLNT_HELLO_B:
247 s->shutdown = 0;
248 ret = ssl3_send_client_hello(s);
249 if (ret <= 0) {
250 goto end;
251 }
252 s->state = SSL3_ST_CR_SRVR_HELLO_A;
253 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley1bea1732014-12-17 19:06:57 -0800255 /* turn on buffering for the next lot of output */
256 if (s->bbio != s->wbio) {
257 s->wbio = BIO_push(s->bbio, s->wbio);
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley1bea1732014-12-17 19:06:57 -0800260 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700261
Adam Langley1bea1732014-12-17 19:06:57 -0800262 case SSL3_ST_CR_SRVR_HELLO_A:
263 case SSL3_ST_CR_SRVR_HELLO_B:
264 ret = ssl3_get_server_hello(s);
265 if (ret <= 0) {
266 goto end;
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley1bea1732014-12-17 19:06:57 -0800269 if (s->hit) {
270 s->state = SSL3_ST_CR_CHANGE;
271 if (s->tlsext_ticket_expected) {
272 /* receive renewed session ticket */
273 s->state = SSL3_ST_CR_SESSION_TICKET_A;
274 }
275 } else {
276 s->state = SSL3_ST_CR_CERT_A;
277 }
278 s->init_num = 0;
279 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400280
Adam Langley1bea1732014-12-17 19:06:57 -0800281 case SSL3_ST_CR_CERT_A:
282 case SSL3_ST_CR_CERT_B:
283 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
284 ret = ssl3_get_server_certificate(s);
285 if (ret <= 0) {
286 goto end;
287 }
288 if (s->s3->tmp.certificate_status_expected) {
289 s->state = SSL3_ST_CR_CERT_STATUS_A;
290 } else {
291 s->state = SSL3_ST_CR_KEY_EXCH_A;
292 }
293 } else {
294 skip = 1;
295 s->state = SSL3_ST_CR_KEY_EXCH_A;
296 }
297 s->init_num = 0;
298 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley1bea1732014-12-17 19:06:57 -0800300 case SSL3_ST_CR_KEY_EXCH_A:
301 case SSL3_ST_CR_KEY_EXCH_B:
302 ret = ssl3_get_server_key_exchange(s);
303 if (ret <= 0) {
304 goto end;
305 }
306 s->state = SSL3_ST_CR_CERT_REQ_A;
307 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley1bea1732014-12-17 19:06:57 -0800309 /* at this point we check that we have the
310 * required stuff from the server */
311 if (!ssl3_check_cert_and_algorithm(s)) {
312 ret = -1;
313 goto end;
314 }
315 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley1bea1732014-12-17 19:06:57 -0800317 case SSL3_ST_CR_CERT_REQ_A:
318 case SSL3_ST_CR_CERT_REQ_B:
319 ret = ssl3_get_certificate_request(s);
320 if (ret <= 0) {
321 goto end;
322 }
323 s->state = SSL3_ST_CR_SRVR_DONE_A;
324 s->init_num = 0;
325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CR_SRVR_DONE_A:
328 case SSL3_ST_CR_SRVR_DONE_B:
329 ret = ssl3_get_server_done(s);
330 if (ret <= 0) {
331 goto end;
332 }
333 if (s->s3->tmp.cert_req) {
334 s->state = SSL3_ST_CW_CERT_A;
335 } else {
336 s->state = SSL3_ST_CW_KEY_EXCH_A;
337 }
338 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley1bea1732014-12-17 19:06:57 -0800340 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langley1bea1732014-12-17 19:06:57 -0800342 case SSL3_ST_CW_CERT_A:
343 case SSL3_ST_CW_CERT_B:
344 case SSL3_ST_CW_CERT_C:
345 case SSL3_ST_CW_CERT_D:
346 ret = ssl3_send_client_certificate(s);
347 if (ret <= 0) {
348 goto end;
349 }
350 s->state = SSL3_ST_CW_KEY_EXCH_A;
351 s->init_num = 0;
352 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley1bea1732014-12-17 19:06:57 -0800354 case SSL3_ST_CW_KEY_EXCH_A:
355 case SSL3_ST_CW_KEY_EXCH_B:
356 ret = ssl3_send_client_key_exchange(s);
357 if (ret <= 0) {
358 goto end;
359 }
360 /* For TLS, cert_req is set to 2, so a cert chain
361 * of nothing is sent, but no verify packet is sent */
362 if (s->s3->tmp.cert_req == 1) {
363 s->state = SSL3_ST_CW_CERT_VRFY_A;
364 } else {
365 s->state = SSL3_ST_CW_CHANGE_A;
366 s->s3->change_cipher_spec = 0;
367 }
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langley1bea1732014-12-17 19:06:57 -0800369 s->init_num = 0;
370 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley1bea1732014-12-17 19:06:57 -0800372 case SSL3_ST_CW_CERT_VRFY_A:
373 case SSL3_ST_CW_CERT_VRFY_B:
374 ret = ssl3_send_cert_verify(s);
375 if (ret <= 0) {
376 goto end;
377 }
378 s->state = SSL3_ST_CW_CHANGE_A;
379 s->init_num = 0;
380 s->s3->change_cipher_spec = 0;
381 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langley1bea1732014-12-17 19:06:57 -0800383 case SSL3_ST_CW_CHANGE_A:
384 case SSL3_ST_CW_CHANGE_B:
385 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
386 SSL3_ST_CW_CHANGE_B);
387 if (ret <= 0) {
388 goto end;
389 }
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langley1bea1732014-12-17 19:06:57 -0800391 s->state = SSL3_ST_CW_FINISHED_A;
392 if (s->s3->tlsext_channel_id_valid) {
393 s->state = SSL3_ST_CW_CHANNEL_ID_A;
394 }
395 if (s->s3->next_proto_neg_seen) {
396 s->state = SSL3_ST_CW_NEXT_PROTO_A;
397 }
398 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langley1bea1732014-12-17 19:06:57 -0800400 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400401 if (!s->enc_method->setup_key_block(s) ||
402 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800403 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
404 ret = -1;
405 goto end;
406 }
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langley1bea1732014-12-17 19:06:57 -0800408 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langley1bea1732014-12-17 19:06:57 -0800410 case SSL3_ST_CW_NEXT_PROTO_A:
411 case SSL3_ST_CW_NEXT_PROTO_B:
412 ret = ssl3_send_next_proto(s);
413 if (ret <= 0) {
414 goto end;
415 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700416
Adam Langley1bea1732014-12-17 19:06:57 -0800417 if (s->s3->tlsext_channel_id_valid) {
418 s->state = SSL3_ST_CW_CHANNEL_ID_A;
419 } else {
420 s->state = SSL3_ST_CW_FINISHED_A;
421 }
422 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langley1bea1732014-12-17 19:06:57 -0800424 case SSL3_ST_CW_CHANNEL_ID_A:
425 case SSL3_ST_CW_CHANNEL_ID_B:
426 ret = ssl3_send_channel_id(s);
427 if (ret <= 0) {
428 goto end;
429 }
430 s->state = SSL3_ST_CW_FINISHED_A;
431 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langley1bea1732014-12-17 19:06:57 -0800433 case SSL3_ST_CW_FINISHED_A:
434 case SSL3_ST_CW_FINISHED_B:
435 ret =
436 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
437 s->enc_method->client_finished_label,
438 s->enc_method->client_finished_label_len);
439 if (ret <= 0) {
440 goto end;
441 }
442 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langley1bea1732014-12-17 19:06:57 -0800444 if (s->hit) {
445 s->s3->tmp.next_state = SSL_ST_OK;
446 } else {
447 /* This is a non-resumption handshake. If it involves ChannelID, then
448 * record the handshake hashes at this point in the session so that
449 * any resumption of this session with ChannelID can sign those
450 * hashes. */
451 if (s->s3->tlsext_channel_id_new) {
452 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500453 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800454 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500455 }
Adam Langley1bea1732014-12-17 19:06:57 -0800456 }
David Benjamined7c4752015-02-16 19:16:46 -0500457 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
458 ssl3_can_false_start(s) &&
459 /* No False Start on renegotiation (would complicate the state
460 * machine). */
Adam Langley1bea1732014-12-17 19:06:57 -0800461 s->s3->previous_server_finished_len == 0) {
David Benjamined7c4752015-02-16 19:16:46 -0500462 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800463 } else {
464 /* Allow NewSessionTicket if ticket expected */
465 if (s->tlsext_ticket_expected) {
466 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
467 } else {
468 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
469 }
470 }
471 }
472 s->init_num = 0;
473 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langley1bea1732014-12-17 19:06:57 -0800475 case SSL3_ST_CR_SESSION_TICKET_A:
476 case SSL3_ST_CR_SESSION_TICKET_B:
477 ret = ssl3_get_new_session_ticket(s);
478 if (ret <= 0) {
479 goto end;
480 }
481 s->state = SSL3_ST_CR_CHANGE;
482 s->init_num = 0;
483 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langley1bea1732014-12-17 19:06:57 -0800485 case SSL3_ST_CR_CERT_STATUS_A:
486 case SSL3_ST_CR_CERT_STATUS_B:
487 ret = ssl3_get_cert_status(s);
488 if (ret <= 0) {
489 goto end;
490 }
491 s->state = SSL3_ST_CR_KEY_EXCH_A;
492 s->init_num = 0;
493 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400494
Adam Langley1bea1732014-12-17 19:06:57 -0800495 case SSL3_ST_CR_CHANGE:
496 /* At this point, the next message must be entirely behind a
497 * ChangeCipherSpec. */
498 if (!ssl3_expect_change_cipher_spec(s)) {
499 ret = -1;
500 goto end;
501 }
502 s->state = SSL3_ST_CR_FINISHED_A;
503 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langley1bea1732014-12-17 19:06:57 -0800505 case SSL3_ST_CR_FINISHED_A:
506 case SSL3_ST_CR_FINISHED_B:
507 ret =
508 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
509 if (ret <= 0) {
510 goto end;
511 }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langley1bea1732014-12-17 19:06:57 -0800513 if (s->hit) {
514 s->state = SSL3_ST_CW_CHANGE_A;
515 } else {
516 s->state = SSL_ST_OK;
517 }
518 s->init_num = 0;
519 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langley1bea1732014-12-17 19:06:57 -0800521 case SSL3_ST_CW_FLUSH:
522 s->rwstate = SSL_WRITING;
523 if (BIO_flush(s->wbio) <= 0) {
524 ret = -1;
525 goto end;
526 }
527 s->rwstate = SSL_NOTHING;
528 s->state = s->s3->tmp.next_state;
529 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700530
David Benjamined7c4752015-02-16 19:16:46 -0500531 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800532 /* Allow NewSessionTicket if ticket expected */
533 if (s->tlsext_ticket_expected) {
534 s->state = SSL3_ST_CR_SESSION_TICKET_A;
535 } else {
536 s->state = SSL3_ST_CR_CHANGE;
537 }
David Benjamined7c4752015-02-16 19:16:46 -0500538 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 ssl_free_wbio_buffer(s);
541 ret = 1;
542 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langley1bea1732014-12-17 19:06:57 -0800544 case SSL_ST_OK:
545 /* clean a few things up */
546 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin2755a3e2015-04-22 16:17:58 -0400548 BUF_MEM_free(s->init_buf);
549 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langley1bea1732014-12-17 19:06:57 -0800551 /* Remove write buffering now. */
552 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langley1bea1732014-12-17 19:06:57 -0800554 s->init_num = 0;
555 s->renegotiate = 0;
556 s->new_session = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500557 s->s3->tmp.in_false_start = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langley1bea1732014-12-17 19:06:57 -0800559 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langley1bea1732014-12-17 19:06:57 -0800561 ret = 1;
562 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langley1bea1732014-12-17 19:06:57 -0800564 if (cb != NULL) {
565 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
Adam Langley1bea1732014-12-17 19:06:57 -0800568 goto end;
569
570 default:
571 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
572 ret = -1;
573 goto end;
574 }
575
576 if (!s->s3->tmp.reuse_message && !skip) {
577 if (cb != NULL && s->state != state) {
578 new_state = s->state;
579 s->state = state;
580 cb(s, SSL_CB_CONNECT_LOOP, 1);
581 s->state = new_state;
582 }
583 }
584 skip = 0;
585 }
586
Adam Langley95c29f32014-06-20 12:00:00 -0700587end:
Adam Langley1bea1732014-12-17 19:06:57 -0800588 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400589 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800590 if (cb != NULL) {
591 cb(s, SSL_CB_CONNECT_EXIT, ret);
592 }
593 return ret;
594}
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langley1bea1732014-12-17 19:06:57 -0800596int ssl3_send_client_hello(SSL *s) {
597 uint8_t *buf, *p, *d;
598 int i;
599 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langley1bea1732014-12-17 19:06:57 -0800601 buf = (uint8_t *)s->init_buf->data;
602 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
603 if (!s->s3->have_version) {
604 uint16_t max_version = ssl3_get_max_client_version(s);
605 /* Disabling all versions is silly: return an error. */
606 if (max_version == 0) {
607 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
608 goto err;
609 }
610 s->version = max_version;
611 s->client_version = max_version;
612 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500613
Adam Langley1bea1732014-12-17 19:06:57 -0800614 /* If the configured session was created at a version higher than our
615 * maximum version, drop it. */
616 if (s->session &&
617 (s->session->session_id_length == 0 || s->session->not_resumable ||
618 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
619 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
620 SSL_set_session(s, NULL);
621 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500622
Adam Langley1bea1732014-12-17 19:06:57 -0800623 /* else use the pre-loaded session */
624 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley1bea1732014-12-17 19:06:57 -0800626 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
627 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400628 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
629 !ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random))) {
630 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800631 }
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langley1bea1732014-12-17 19:06:57 -0800633 /* Do the message type and length last. Note: the final argument to
634 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
635 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langley1bea1732014-12-17 19:06:57 -0800637 /* version indicates the negotiated version: for example from an SSLv2/v3
638 * compatible client hello). The client_version field is the maximum
639 * version we permit and it is also used in RSA encrypted premaster
640 * secrets. Some servers can choke if we initially report a higher version
641 * then renegotiate to a lower one in the premaster secret. This didn't
642 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
643 * or later if the server only supports 1.0.
644 *
645 * Possible scenario with previous logic:
646 * 1. Client hello indicates TLS 1.2
647 * 2. Server hello says TLS 1.0
648 * 3. RSA encrypted premaster secret uses 1.2.
649 * 4. Handhaked proceeds using TLS 1.0.
650 * 5. Server sends hello request to renegotiate.
651 * 6. Client hello indicates TLS v1.0 as we now
652 * know that is maximum server supports.
653 * 7. Server chokes on RSA encrypted premaster secret
654 * containing version 1.0.
655 *
656 * For interoperability it should be OK to always use the maximum version
657 * we support in client hello and then rely on the checking of version to
658 * ensure the servers isn't being inconsistent: for example initially
659 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
660 * using client_version in client hello and not resetting it to the
661 * negotiated version. */
662 *(p++) = s->client_version >> 8;
663 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley1bea1732014-12-17 19:06:57 -0800665 /* Random stuff */
666 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
667 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley1bea1732014-12-17 19:06:57 -0800669 /* Session ID */
670 if (s->new_session || s->session == NULL) {
671 i = 0;
672 } else {
673 i = s->session->session_id_length;
674 }
675 *(p++) = i;
676 if (i != 0) {
677 if (i > (int)sizeof(s->session->session_id)) {
678 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
679 goto err;
680 }
681 memcpy(p, s->session->session_id, i);
682 p += i;
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langley1bea1732014-12-17 19:06:57 -0800685 /* cookie stuff for DTLS */
686 if (SSL_IS_DTLS(s)) {
687 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
688 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
689 goto err;
690 }
691 *(p++) = s->d1->cookie_len;
692 memcpy(p, s->d1->cookie, s->d1->cookie_len);
693 p += s->d1->cookie_len;
694 }
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langley1bea1732014-12-17 19:06:57 -0800696 /* Ciphers supported */
697 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
698 if (i == 0) {
699 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
700 SSL_R_NO_CIPHERS_AVAILABLE);
701 goto err;
702 }
703 s2n(i, p);
704 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langley1bea1732014-12-17 19:06:57 -0800706 /* COMPRESSION */
707 *(p++) = 1;
708 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley1bea1732014-12-17 19:06:57 -0800710 /* TLS extensions*/
711 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
712 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
713 goto err;
714 }
715
716 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
717 p - buf);
718 if (p == NULL) {
719 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
720 goto err;
721 }
722
723 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500724 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
725 goto err;
726 }
Adam Langley1bea1732014-12-17 19:06:57 -0800727 s->state = SSL3_ST_CW_CLNT_HELLO_B;
728 }
729
730 /* SSL3_ST_CW_CLNT_HELLO_B */
731 return ssl_do_write(s);
732
Adam Langley95c29f32014-06-20 12:00:00 -0700733err:
Adam Langley1bea1732014-12-17 19:06:57 -0800734 return -1;
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langley1bea1732014-12-17 19:06:57 -0800737int ssl3_get_server_hello(SSL *s) {
738 STACK_OF(SSL_CIPHER) * sk;
739 const SSL_CIPHER *c;
740 CERT *ct = s->cert;
741 int al = SSL_AD_INTERNAL_ERROR, ok;
742 long n;
743 CBS server_hello, server_random, session_id;
744 uint16_t server_version, cipher_suite;
745 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400746 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langley1bea1732014-12-17 19:06:57 -0800748 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
749 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
750 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500751 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langley1bea1732014-12-17 19:06:57 -0800753 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500754 uint32_t err = ERR_peek_error();
755 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
756 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
757 /* Add a dedicated error code to the queue for a handshake_failure alert
758 * in response to ClientHello. This matches NSS's client behavior and
759 * gives a better error on a (probable) failure to negotiate initial
760 * parameters. Note: this error code comes after the original one.
761 *
762 * See https://crbug.com/446505. */
763 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
764 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
765 }
Adam Langley1bea1732014-12-17 19:06:57 -0800766 return n;
767 }
Adam Langley95c29f32014-06-20 12:00:00 -0700768
Adam Langley1bea1732014-12-17 19:06:57 -0800769 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400770
Adam Langley1bea1732014-12-17 19:06:57 -0800771 if (!CBS_get_u16(&server_hello, &server_version) ||
772 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
773 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
774 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
775 !CBS_get_u16(&server_hello, &cipher_suite) ||
776 !CBS_get_u8(&server_hello, &compression_method)) {
777 al = SSL_AD_DECODE_ERROR;
778 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
779 goto f_err;
780 }
David Benjamina03d95d2014-07-12 19:49:07 -0400781
Adam Langley1bea1732014-12-17 19:06:57 -0800782 if (!s->s3->have_version) {
783 if (!ssl3_is_version_enabled(s, server_version)) {
784 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
785 s->version = server_version;
786 /* Mark the version as fixed so the record-layer version is not clamped
787 * to TLS 1.0. */
788 s->s3->have_version = 1;
789 al = SSL_AD_PROTOCOL_VERSION;
790 goto f_err;
791 }
792 s->version = server_version;
793 s->enc_method = ssl3_get_enc_method(server_version);
794 assert(s->enc_method != NULL);
795 /* At this point, the connection's version is known and s->version is
796 * fixed. Begin enforcing the record-layer version. */
797 s->s3->have_version = 1;
798 } else if (server_version != s->version) {
799 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
800 al = SSL_AD_PROTOCOL_VERSION;
801 goto f_err;
802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langley1bea1732014-12-17 19:06:57 -0800804 /* Copy over the server random. */
805 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langley1bea1732014-12-17 19:06:57 -0800807 assert(s->session == NULL || s->session->session_id_length > 0);
808 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
809 s->session->session_id_length)) {
810 if (s->sid_ctx_length != s->session->sid_ctx_length ||
811 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
812 /* actually a client application bug */
813 al = SSL_AD_ILLEGAL_PARAMETER;
814 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
815 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
816 goto f_err;
817 }
818 s->hit = 1;
819 } else {
820 /* The session wasn't resumed. Create a fresh SSL_SESSION to
821 * fill out. */
822 s->hit = 0;
823 if (!ssl_get_new_session(s, 0)) {
824 goto f_err;
825 }
826 /* Note: session_id could be empty. */
827 s->session->session_id_length = CBS_len(&session_id);
828 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
829 }
David Benjamina03d95d2014-07-12 19:49:07 -0400830
Adam Langley1bea1732014-12-17 19:06:57 -0800831 c = ssl3_get_cipher_by_value(cipher_suite);
832 if (c == NULL) {
833 /* unknown cipher */
834 al = SSL_AD_ILLEGAL_PARAMETER;
835 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
836 SSL_R_UNKNOWN_CIPHER_RETURNED);
837 goto f_err;
838 }
839 /* ct->mask_ssl was computed from client capabilities. Now
840 * that the final version is known, compute a new mask_ssl. */
841 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
842 mask_ssl = SSL_TLSV1_2;
843 } else {
844 mask_ssl = 0;
845 }
846 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
847 * the server selected it, it's an error. */
848 if ((c->algorithm_ssl & mask_ssl) ||
849 (c->algorithm_mkey & ct->mask_k) ||
850 (c->algorithm_auth & ct->mask_a)) {
851 al = SSL_AD_ILLEGAL_PARAMETER;
852 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
853 goto f_err;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langley1bea1732014-12-17 19:06:57 -0800856 sk = ssl_get_ciphers_by_id(s);
857 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
858 /* we did not say we would use this cipher */
859 al = SSL_AD_ILLEGAL_PARAMETER;
860 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
861 goto f_err;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjaminece3de92015-03-16 18:02:20 -0400864 if (s->hit) {
865 if (s->session->cipher != c) {
866 al = SSL_AD_ILLEGAL_PARAMETER;
867 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
868 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
869 goto f_err;
870 }
871 if (s->session->ssl_version != s->version) {
872 al = SSL_AD_ILLEGAL_PARAMETER;
873 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
874 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
875 goto f_err;
876 }
Adam Langley1bea1732014-12-17 19:06:57 -0800877 }
878 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500879
Adam Langley1bea1732014-12-17 19:06:57 -0800880 /* Don't digest cached records if no sigalgs: we may need them for client
881 * authentication. */
882 if (!SSL_USE_SIGALGS(s) &&
883 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
884 goto f_err;
885 }
David Benjamina03d95d2014-07-12 19:49:07 -0400886
Adam Langley1bea1732014-12-17 19:06:57 -0800887 /* Only the NULL compression algorithm is supported. */
888 if (compression_method != 0) {
889 al = SSL_AD_ILLEGAL_PARAMETER;
890 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
891 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
892 goto f_err;
893 }
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langley1bea1732014-12-17 19:06:57 -0800895 /* TLS extensions */
896 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
897 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
898 goto err;
899 }
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langley1bea1732014-12-17 19:06:57 -0800901 /* There should be nothing left over in the record. */
902 if (CBS_len(&server_hello) != 0) {
903 /* wrong packet length */
904 al = SSL_AD_DECODE_ERROR;
905 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
906 goto f_err;
907 }
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langley1bea1732014-12-17 19:06:57 -0800909 return 1;
910
Adam Langley95c29f32014-06-20 12:00:00 -0700911f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800912 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700913err:
Adam Langley1bea1732014-12-17 19:06:57 -0800914 return -1;
915}
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langley1bea1732014-12-17 19:06:57 -0800917int ssl3_get_server_certificate(SSL *s) {
918 int al, i, ok, ret = -1;
919 unsigned long n;
920 X509 *x = NULL;
921 STACK_OF(X509) *sk = NULL;
922 SESS_CERT *sc;
923 EVP_PKEY *pkey = NULL;
924 CBS cbs, certificate_list;
925 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langley1bea1732014-12-17 19:06:57 -0800927 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400928 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500929 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langley1bea1732014-12-17 19:06:57 -0800931 if (!ok) {
932 return n;
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langley1bea1732014-12-17 19:06:57 -0800935 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langley1bea1732014-12-17 19:06:57 -0800937 sk = sk_X509_new_null();
938 if (sk == NULL) {
939 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
940 goto err;
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langley1bea1732014-12-17 19:06:57 -0800943 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
944 CBS_len(&cbs) != 0) {
945 al = SSL_AD_DECODE_ERROR;
946 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
947 goto f_err;
948 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400949
Adam Langley1bea1732014-12-17 19:06:57 -0800950 while (CBS_len(&certificate_list) > 0) {
951 CBS certificate;
952 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
953 al = SSL_AD_DECODE_ERROR;
954 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
955 SSL_R_CERT_LENGTH_MISMATCH);
956 goto f_err;
957 }
958 data = CBS_data(&certificate);
959 x = d2i_X509(NULL, &data, CBS_len(&certificate));
960 if (x == NULL) {
961 al = SSL_AD_BAD_CERTIFICATE;
962 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
963 goto f_err;
964 }
965 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
966 al = SSL_AD_DECODE_ERROR;
967 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
968 SSL_R_CERT_LENGTH_MISMATCH);
969 goto f_err;
970 }
971 if (!sk_X509_push(sk, x)) {
972 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
973 goto err;
974 }
975 x = NULL;
976 }
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langley1bea1732014-12-17 19:06:57 -0800978 i = ssl_verify_cert_chain(s, sk);
979 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
980 al = ssl_verify_alarm_type(s->verify_result);
981 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
982 SSL_R_CERTIFICATE_VERIFY_FAILED);
983 goto f_err;
984 }
985 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langley1bea1732014-12-17 19:06:57 -0800987 sc = ssl_sess_cert_new();
988 if (sc == NULL) {
989 goto err;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
David Benjamin2755a3e2015-04-22 16:17:58 -0400992 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -0800993 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langley1bea1732014-12-17 19:06:57 -0800995 sc->cert_chain = sk;
996 /* Inconsistency alert: cert_chain does include the peer's certificate, which
997 * we don't include in s3_srvr.c */
998 x = sk_X509_value(sk, 0);
999 sk = NULL;
1000 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langley1bea1732014-12-17 19:06:57 -08001002 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langley1bea1732014-12-17 19:06:57 -08001004 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1005 x = NULL;
1006 al = SSL3_AL_FATAL;
1007 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1008 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1009 goto f_err;
1010 }
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langley1bea1732014-12-17 19:06:57 -08001012 i = ssl_cert_type(pkey);
1013 if (i < 0) {
1014 x = NULL;
1015 al = SSL3_AL_FATAL;
1016 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1017 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1018 goto f_err;
1019 }
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langley1bea1732014-12-17 19:06:57 -08001021 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1022 if (exp_idx >= 0 && i != exp_idx) {
1023 x = NULL;
1024 al = SSL_AD_ILLEGAL_PARAMETER;
1025 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1026 SSL_R_WRONG_CERTIFICATE_TYPE);
1027 goto f_err;
1028 }
1029 sc->peer_cert_type = i;
David Benjamin2755a3e2015-04-22 16:17:58 -04001030 X509_free(sc->peer_pkeys[i].x509);
Adam Langley1bea1732014-12-17 19:06:57 -08001031 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1032 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001033
David Benjamin2755a3e2015-04-22 16:17:58 -04001034 X509_free(s->session->peer);
Adam Langley1bea1732014-12-17 19:06:57 -08001035 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001036
Adam Langley1bea1732014-12-17 19:06:57 -08001037 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 x = NULL;
1040 ret = 1;
1041
1042 if (0) {
1043 f_err:
1044 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1045 }
1046
1047err:
1048 EVP_PKEY_free(pkey);
1049 X509_free(x);
1050 sk_X509_pop_free(sk, X509_free);
1051 return ret;
1052}
1053
1054int ssl3_get_server_key_exchange(SSL *s) {
1055 EVP_MD_CTX md_ctx;
1056 int al, ok;
1057 long n, alg_k, alg_a;
1058 EVP_PKEY *pkey = NULL;
1059 const EVP_MD *md = NULL;
1060 RSA *rsa = NULL;
1061 DH *dh = NULL;
1062 EC_KEY *ecdh = NULL;
1063 BN_CTX *bn_ctx = NULL;
1064 EC_POINT *srvr_ecpoint = NULL;
1065 CBS server_key_exchange, server_key_exchange_orig, parameter;
1066
1067 /* use same message size as in ssl3_get_certificate_request() as
1068 * ServerKeyExchange message may be skipped */
1069 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1070 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001071 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001072 if (!ok) {
1073 return n;
1074 }
1075
1076 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1077 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1078 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1079 SSL_R_UNEXPECTED_MESSAGE);
1080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1081 return -1;
1082 }
1083
1084 /* In plain PSK ciphersuite, ServerKeyExchange can be
1085 omitted if no identity hint is sent. Set session->sess_cert anyway to
1086 avoid problems later.*/
1087 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1088 /* PSK ciphersuites that also send a Certificate would have already
1089 * initialized |sess_cert|. */
1090 if (s->session->sess_cert == NULL) {
1091 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001092 if (s->session->sess_cert == NULL) {
1093 return -1;
1094 }
Adam Langley1bea1732014-12-17 19:06:57 -08001095 }
1096
1097 /* TODO(davidben): This should be reset in one place with the rest of the
1098 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001099 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1100 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001101 }
1102 s->s3->tmp.reuse_message = 1;
1103 return 1;
1104 }
1105
1106 /* Retain a copy of the original CBS to compute the signature over. */
1107 CBS_init(&server_key_exchange, s->init_msg, n);
1108 server_key_exchange_orig = server_key_exchange;
1109
1110 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001111 DH_free(s->session->sess_cert->peer_dh_tmp);
1112 s->session->sess_cert->peer_dh_tmp = NULL;
1113 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1114 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001115 } else {
1116 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001117 if (s->session->sess_cert == NULL) {
1118 return -1;
1119 }
Adam Langley1bea1732014-12-17 19:06:57 -08001120 }
1121
1122 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1123 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1124 EVP_MD_CTX_init(&md_ctx);
1125
1126 if (alg_a & SSL_aPSK) {
1127 CBS psk_identity_hint;
1128
1129 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1130 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1131 &psk_identity_hint)) {
1132 al = SSL_AD_DECODE_ERROR;
1133 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1134 goto f_err;
1135 }
1136
1137 /* Store PSK identity hint for later use, hint is used in
1138 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1139 * identity hint can be as long as the maximum length of a PSK identity.
1140 * Also do not allow NULL characters; identities are saved as C strings.
1141 *
1142 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1143 * a specific identity. */
1144 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1145 CBS_contains_zero_byte(&psk_identity_hint)) {
1146 al = SSL_AD_HANDSHAKE_FAILURE;
1147 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1148 SSL_R_DATA_LENGTH_TOO_LONG);
1149 goto f_err;
1150 }
1151
1152 /* Save the identity hint as a C string. */
1153 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1154 al = SSL_AD_INTERNAL_ERROR;
1155 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1156 ERR_R_MALLOC_FAILURE);
1157 goto f_err;
1158 }
1159 }
1160
David Benjamin7061e282015-03-19 11:10:48 -04001161 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001162 CBS dh_p, dh_g, dh_Ys;
1163
1164 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1165 CBS_len(&dh_p) == 0 ||
1166 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1167 CBS_len(&dh_g) == 0 ||
1168 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1169 CBS_len(&dh_Ys) == 0) {
1170 al = SSL_AD_DECODE_ERROR;
1171 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1172 goto f_err;
1173 }
1174
1175 dh = DH_new();
1176 if (dh == NULL) {
1177 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1178 goto err;
1179 }
1180
1181 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1182 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1183 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1184 NULL) {
1185 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1186 goto err;
1187 }
1188
1189 if (DH_size(dh) < 512 / 8) {
1190 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1191 SSL_R_BAD_DH_P_LENGTH);
1192 goto err;
1193 }
1194
1195 if (alg_a & SSL_aRSA) {
1196 pkey = X509_get_pubkey(
1197 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1198 }
1199 /* else anonymous DH, so no certificate or pkey. */
1200
1201 s->session->sess_cert->peer_dh_tmp = dh;
1202 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001203 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001204 uint16_t curve_id;
1205 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001206 const EC_GROUP *group;
1207 CBS point;
1208
1209 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1210 * key. Check curve is one of our preferences, if not server has sent an
1211 * invalid curve. */
1212 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1213 al = SSL_AD_DECODE_ERROR;
1214 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1215 goto f_err;
1216 }
1217
1218 curve_nid = tls1_ec_curve_id2nid(curve_id);
1219 if (curve_nid == 0) {
1220 al = SSL_AD_INTERNAL_ERROR;
1221 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1222 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1223 goto f_err;
1224 }
1225
David Benjamin4f7783e2015-03-05 03:05:11 -05001226 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001227 if (ecdh == NULL) {
1228 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001229 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001230 goto err;
1231 }
1232
Adam Langley1bea1732014-12-17 19:06:57 -08001233 group = EC_KEY_get0_group(ecdh);
1234
1235 /* Next, get the encoded ECPoint */
1236 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1237 al = SSL_AD_DECODE_ERROR;
1238 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1239 goto f_err;
1240 }
1241
1242 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1243 ((bn_ctx = BN_CTX_new()) == NULL)) {
1244 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1245 ERR_R_MALLOC_FAILURE);
1246 goto err;
1247 }
1248
1249 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1250 CBS_len(&point), bn_ctx)) {
1251 al = SSL_AD_DECODE_ERROR;
1252 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1253 goto f_err;
1254 }
1255
1256 /* The ECC/TLS specification does not mention the use of DSA to sign
1257 * ECParameters in the server key exchange message. We do support RSA and
1258 * ECDSA. */
1259 if (alg_a & SSL_aRSA) {
1260 pkey = X509_get_pubkey(
1261 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1262 } else if (alg_a & SSL_aECDSA) {
1263 pkey =
1264 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1265 }
1266 /* else anonymous ECDH, so no certificate or pkey. */
1267 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1268 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1269 ecdh = NULL;
1270 BN_CTX_free(bn_ctx);
1271 bn_ctx = NULL;
1272 EC_POINT_free(srvr_ecpoint);
1273 srvr_ecpoint = NULL;
1274 } else if (!(alg_k & SSL_kPSK)) {
1275 al = SSL_AD_UNEXPECTED_MESSAGE;
1276 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1277 SSL_R_UNEXPECTED_MESSAGE);
1278 goto f_err;
1279 }
1280
1281 /* At this point, |server_key_exchange| contains the signature, if any, while
1282 * |server_key_exchange_orig| contains the entire message. From that, derive
1283 * a CBS containing just the parameter. */
1284 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1285 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1286
1287 /* if it was signed, check the signature */
1288 if (pkey != NULL) {
1289 CBS signature;
1290
1291 if (SSL_USE_SIGALGS(s)) {
1292 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1293 goto f_err;
1294 }
1295 } else if (pkey->type == EVP_PKEY_RSA) {
1296 md = EVP_md5_sha1();
1297 } else {
1298 md = EVP_sha1();
1299 }
1300
1301 /* The last field in |server_key_exchange| is the signature. */
1302 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1303 CBS_len(&server_key_exchange) != 0) {
1304 al = SSL_AD_DECODE_ERROR;
1305 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1306 goto f_err;
1307 }
1308
1309 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1310 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1311 SSL3_RANDOM_SIZE) ||
1312 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1313 SSL3_RANDOM_SIZE) ||
1314 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1315 CBS_len(&parameter)) ||
1316 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1317 CBS_len(&signature))) {
1318 /* bad signature */
1319 al = SSL_AD_DECRYPT_ERROR;
1320 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1321 goto f_err;
1322 }
1323 } else {
1324 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1325 /* Might be wrong key type, check it */
1326 if (ssl3_check_cert_and_algorithm(s)) {
1327 /* Otherwise this shouldn't happen */
1328 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1329 ERR_R_INTERNAL_ERROR);
1330 }
1331 goto err;
1332 }
1333 /* still data left over */
1334 if (CBS_len(&server_key_exchange) > 0) {
1335 al = SSL_AD_DECODE_ERROR;
1336 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1337 SSL_R_EXTRA_DATA_IN_MESSAGE);
1338 goto f_err;
1339 }
1340 }
1341 EVP_PKEY_free(pkey);
1342 EVP_MD_CTX_cleanup(&md_ctx);
1343 return 1;
1344
Adam Langley95c29f32014-06-20 12:00:00 -07001345f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001346 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001347err:
Adam Langley1bea1732014-12-17 19:06:57 -08001348 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001349 RSA_free(rsa);
1350 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001351 BN_CTX_free(bn_ctx);
1352 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001353 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001354 EVP_MD_CTX_cleanup(&md_ctx);
1355 return -1;
1356}
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langley1bea1732014-12-17 19:06:57 -08001358static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1359 return X509_NAME_cmp(*a, *b);
1360}
Adam Langley95c29f32014-06-20 12:00:00 -07001361
Adam Langley1bea1732014-12-17 19:06:57 -08001362int ssl3_get_certificate_request(SSL *s) {
1363 int ok, ret = 0;
1364 unsigned long n;
1365 X509_NAME *xn = NULL;
1366 STACK_OF(X509_NAME) *ca_sk = NULL;
1367 CBS cbs;
1368 CBS certificate_types;
1369 CBS certificate_authorities;
1370 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langley1bea1732014-12-17 19:06:57 -08001372 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1373 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001374 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001375
Adam Langley1bea1732014-12-17 19:06:57 -08001376 if (!ok) {
1377 return n;
1378 }
David Benjamin688d8df2014-11-02 23:06:42 -05001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langley1bea1732014-12-17 19:06:57 -08001382 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1383 s->s3->tmp.reuse_message = 1;
1384 /* If we get here we don't need any cached handshake records as we wont be
1385 * doing client auth. */
1386 if (s->s3->handshake_buffer &&
1387 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1388 goto err;
1389 }
1390 return 1;
1391 }
David Benjamined439582014-07-14 19:13:02 -04001392
Adam Langley1bea1732014-12-17 19:06:57 -08001393 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1394 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1395 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1396 SSL_R_WRONG_MESSAGE_TYPE);
1397 goto err;
1398 }
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langley1bea1732014-12-17 19:06:57 -08001400 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langley1bea1732014-12-17 19:06:57 -08001402 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1403 if (ca_sk == NULL) {
1404 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1405 goto err;
1406 }
Adam Langley95c29f32014-06-20 12:00:00 -07001407
Adam Langley1bea1732014-12-17 19:06:57 -08001408 /* get the certificate types */
1409 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1410 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1411 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1412 goto err;
1413 }
David Benjamined439582014-07-14 19:13:02 -04001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1416 &s->s3->tmp.num_certificate_types)) {
1417 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1418 goto err;
1419 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001420
Adam Langley1bea1732014-12-17 19:06:57 -08001421 if (SSL_USE_SIGALGS(s)) {
1422 CBS supported_signature_algorithms;
1423 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1424 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1425 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1426 goto err;
1427 }
David Benjamined439582014-07-14 19:13:02 -04001428
Adam Langley1bea1732014-12-17 19:06:57 -08001429 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1430 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1431 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1432 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1433 goto err;
1434 }
1435 }
David Benjamined439582014-07-14 19:13:02 -04001436
Adam Langley1bea1732014-12-17 19:06:57 -08001437 /* get the CA RDNs */
1438 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1439 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1440 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1441 goto err;
1442 }
Adam Langley95c29f32014-06-20 12:00:00 -07001443
Adam Langley1bea1732014-12-17 19:06:57 -08001444 while (CBS_len(&certificate_authorities) > 0) {
1445 CBS distinguished_name;
1446 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1447 &distinguished_name)) {
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1449 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1450 SSL_R_CA_DN_TOO_LONG);
1451 goto err;
1452 }
Adam Langley95c29f32014-06-20 12:00:00 -07001453
Adam Langley1bea1732014-12-17 19:06:57 -08001454 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001455
Adam Langley1bea1732014-12-17 19:06:57 -08001456 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1457 if (xn == NULL) {
1458 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1459 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1460 goto err;
1461 }
Adam Langley95c29f32014-06-20 12:00:00 -07001462
Adam Langley1bea1732014-12-17 19:06:57 -08001463 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1464 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001465 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001466 goto err;
1467 }
Adam Langley95c29f32014-06-20 12:00:00 -07001468
Adam Langley1bea1732014-12-17 19:06:57 -08001469 if (CBS_len(&distinguished_name) != 0) {
1470 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1471 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1472 SSL_R_CA_DN_LENGTH_MISMATCH);
1473 goto err;
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
Adam Langley1bea1732014-12-17 19:06:57 -08001476 if (!sk_X509_NAME_push(ca_sk, xn)) {
1477 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1478 ERR_R_MALLOC_FAILURE);
1479 goto err;
1480 }
1481 }
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langley1bea1732014-12-17 19:06:57 -08001483 /* we should setup a certificate to return.... */
1484 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001485 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001486 s->s3->tmp.ca_names = ca_sk;
1487 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001488
Adam Langley1bea1732014-12-17 19:06:57 -08001489 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langley1bea1732014-12-17 19:06:57 -08001491err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001492 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001493 return ret;
1494}
Adam Langley95c29f32014-06-20 12:00:00 -07001495
Adam Langley1bea1732014-12-17 19:06:57 -08001496int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001497 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001498 long n;
1499 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001500
Adam Langley1bea1732014-12-17 19:06:57 -08001501 n = s->method->ssl_get_message(
1502 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001503 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langley1bea1732014-12-17 19:06:57 -08001505 if (!ok) {
1506 return n;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
Adam Langley1bea1732014-12-17 19:06:57 -08001509 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001510
Adam Langley1bea1732014-12-17 19:06:57 -08001511 if (!CBS_get_u32(&new_session_ticket,
1512 &s->session->tlsext_tick_lifetime_hint) ||
1513 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1514 CBS_len(&new_session_ticket) != 0) {
1515 al = SSL_AD_DECODE_ERROR;
1516 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1517 goto f_err;
1518 }
Adam Langley95c29f32014-06-20 12:00:00 -07001519
Adam Langley1bea1732014-12-17 19:06:57 -08001520 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1521 &s->session->tlsext_ticklen)) {
1522 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1523 goto err;
1524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525
David Benjamin68070622015-02-08 23:44:59 -05001526 /* Generate a session ID for this session based on the session ticket. We use
1527 * the session ID mechanism for detecting ticket resumption. This also fits in
1528 * with assumptions elsewhere in OpenSSL.*/
1529 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1530 &s->session->session_id_length, EVP_sha256(), NULL)) {
1531 goto err;
1532 }
1533
1534 return 1;
David Benjamined439582014-07-14 19:13:02 -04001535
Adam Langley95c29f32014-06-20 12:00:00 -07001536f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001537 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001538err:
Adam Langley1bea1732014-12-17 19:06:57 -08001539 return -1;
1540}
Adam Langley95c29f32014-06-20 12:00:00 -07001541
Adam Langley1bea1732014-12-17 19:06:57 -08001542int ssl3_get_cert_status(SSL *s) {
1543 int ok, al;
1544 long n;
1545 CBS certificate_status, ocsp_response;
1546 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001547
Adam Langley1bea1732014-12-17 19:06:57 -08001548 n = s->method->ssl_get_message(
1549 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001550 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langley1bea1732014-12-17 19:06:57 -08001552 if (!ok) {
1553 return n;
1554 }
Adam Langley95c29f32014-06-20 12:00:00 -07001555
David Benjamindcd979f2015-04-20 18:26:52 -04001556 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1557 /* A server may send status_request in ServerHello and then change
1558 * its mind about sending CertificateStatus. */
1559 s->s3->tmp.reuse_message = 1;
1560 return 1;
1561 }
1562
Adam Langley1bea1732014-12-17 19:06:57 -08001563 CBS_init(&certificate_status, s->init_msg, n);
1564 if (!CBS_get_u8(&certificate_status, &status_type) ||
1565 status_type != TLSEXT_STATUSTYPE_ocsp ||
1566 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1567 CBS_len(&ocsp_response) == 0 ||
1568 CBS_len(&certificate_status) != 0) {
1569 al = SSL_AD_DECODE_ERROR;
1570 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1571 goto f_err;
1572 }
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langley1bea1732014-12-17 19:06:57 -08001574 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1575 &s->session->ocsp_response_length)) {
1576 al = SSL_AD_INTERNAL_ERROR;
1577 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1578 goto f_err;
1579 }
1580 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langley95c29f32014-06-20 12:00:00 -07001582f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001583 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1584 return -1;
1585}
Adam Langley95c29f32014-06-20 12:00:00 -07001586
Adam Langley1bea1732014-12-17 19:06:57 -08001587int ssl3_get_server_done(SSL *s) {
1588 int ok;
1589 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001590
Adam Langley1bea1732014-12-17 19:06:57 -08001591 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1592 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1593 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001594 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001595
Adam Langley1bea1732014-12-17 19:06:57 -08001596 if (!ok) {
1597 return n;
1598 }
David Benjamin46062682014-07-14 19:14:32 -04001599
Adam Langley1bea1732014-12-17 19:06:57 -08001600 if (n > 0) {
1601 /* should contain no data */
1602 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1603 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1604 return -1;
1605 }
David Benjamin46062682014-07-14 19:14:32 -04001606
Adam Langley1bea1732014-12-17 19:06:57 -08001607 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001608}
Adam Langley1258b6a2014-06-20 12:00:00 -07001609
Adam Langley1258b6a2014-06-20 12:00:00 -07001610
Adam Langley1bea1732014-12-17 19:06:57 -08001611int ssl3_send_client_key_exchange(SSL *s) {
1612 uint8_t *p;
1613 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001614 uint32_t alg_k;
1615 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001616 uint8_t *q;
1617 EVP_PKEY *pkey = NULL;
1618 EC_KEY *clnt_ecdh = NULL;
1619 const EC_POINT *srvr_ecpoint = NULL;
1620 EVP_PKEY *srvr_pub_pkey = NULL;
1621 uint8_t *encodedPoint = NULL;
1622 int encoded_pt_len = 0;
1623 BN_CTX *bn_ctx = NULL;
1624 unsigned int psk_len = 0;
1625 uint8_t psk[PSK_MAX_PSK_LEN];
1626 uint8_t *pms = NULL;
1627 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001628
Adam Langley1bea1732014-12-17 19:06:57 -08001629 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1630 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001631
Adam Langley1bea1732014-12-17 19:06:57 -08001632 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1633 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001634
Adam Langley1bea1732014-12-17 19:06:57 -08001635 /* If using a PSK key exchange, prepare the pre-shared key. */
1636 if (alg_a & SSL_aPSK) {
1637 char identity[PSK_MAX_IDENTITY_LEN + 1];
1638 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001639
Adam Langley1bea1732014-12-17 19:06:57 -08001640 if (s->psk_client_callback == NULL) {
1641 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1642 SSL_R_PSK_NO_CLIENT_CB);
1643 goto err;
1644 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001645
Adam Langley1bea1732014-12-17 19:06:57 -08001646 memset(identity, 0, sizeof(identity));
1647 psk_len =
1648 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1649 sizeof(identity), psk, sizeof(psk));
1650 if (psk_len > PSK_MAX_PSK_LEN) {
1651 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1652 ERR_R_INTERNAL_ERROR);
1653 goto err;
1654 } else if (psk_len == 0) {
1655 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1656 SSL_R_PSK_IDENTITY_NOT_FOUND);
1657 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1658 goto err;
1659 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001660
Adam Langley1bea1732014-12-17 19:06:57 -08001661 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1662 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1663 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1664 ERR_R_INTERNAL_ERROR);
1665 goto err;
1666 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001667
David Benjamin2755a3e2015-04-22 16:17:58 -04001668 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001669 s->session->psk_identity = BUF_strdup(identity);
1670 if (s->session->psk_identity == NULL) {
1671 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1672 ERR_R_MALLOC_FAILURE);
1673 goto err;
1674 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001675
Adam Langley1bea1732014-12-17 19:06:57 -08001676 /* Write out psk_identity. */
1677 s2n(identity_len, p);
1678 memcpy(p, identity, identity_len);
1679 p += identity_len;
1680 n = 2 + identity_len;
1681 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001682
Adam Langley1bea1732014-12-17 19:06:57 -08001683 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1684 if (alg_k & SSL_kRSA) {
1685 RSA *rsa;
1686 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001687
Adam Langley1bea1732014-12-17 19:06:57 -08001688 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1689 pms = OPENSSL_malloc(pms_len);
1690 if (pms == NULL) {
1691 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1692 ERR_R_MALLOC_FAILURE);
1693 goto err;
1694 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001695
Adam Langley1bea1732014-12-17 19:06:57 -08001696 if (s->session->sess_cert == NULL) {
1697 /* We should always have a server certificate with SSL_kRSA. */
1698 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1699 ERR_R_INTERNAL_ERROR);
1700 goto err;
1701 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001702
Adam Langley1bea1732014-12-17 19:06:57 -08001703 pkey = X509_get_pubkey(
1704 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1705 if (pkey == NULL ||
1706 pkey->type != EVP_PKEY_RSA ||
1707 pkey->pkey.rsa == NULL) {
1708 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1709 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001710 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001711 goto err;
1712 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001713
Adam Langley1bea1732014-12-17 19:06:57 -08001714 rsa = pkey->pkey.rsa;
1715 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001716
Adam Langley1bea1732014-12-17 19:06:57 -08001717 pms[0] = s->client_version >> 8;
1718 pms[1] = s->client_version & 0xff;
1719 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1720 goto err;
1721 }
1722
1723 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1724
1725 q = p;
1726 /* In TLS and beyond, reserve space for the length prefix. */
1727 if (s->version > SSL3_VERSION) {
1728 p += 2;
1729 n += 2;
1730 }
1731 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1732 RSA_PKCS1_PADDING)) {
1733 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1734 SSL_R_BAD_RSA_ENCRYPT);
1735 goto err;
1736 }
1737 n += enc_pms_len;
1738
1739 /* Log the premaster secret, if logging is enabled. */
1740 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1741 pms_len)) {
1742 goto err;
1743 }
1744
1745 /* Fill in the length prefix. */
1746 if (s->version > SSL3_VERSION) {
1747 s2n(enc_pms_len, q);
1748 }
David Benjamin7061e282015-03-19 11:10:48 -04001749 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001750 DH *dh_srvr, *dh_clnt;
1751 SESS_CERT *scert = s->session->sess_cert;
1752 int dh_len;
1753 size_t pub_len;
1754
1755 if (scert == NULL) {
1756 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1757 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1758 SSL_R_UNEXPECTED_MESSAGE);
1759 goto err;
1760 }
1761
1762 if (scert->peer_dh_tmp == NULL) {
1763 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1764 ERR_R_INTERNAL_ERROR);
1765 goto err;
1766 }
1767 dh_srvr = scert->peer_dh_tmp;
1768
1769 /* generate a new random key */
1770 dh_clnt = DHparams_dup(dh_srvr);
1771 if (dh_clnt == NULL) {
1772 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1773 goto err;
1774 }
1775 if (!DH_generate_key(dh_clnt)) {
1776 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1777 DH_free(dh_clnt);
1778 goto err;
1779 }
1780
1781 pms_len = DH_size(dh_clnt);
1782 pms = OPENSSL_malloc(pms_len);
1783 if (pms == NULL) {
1784 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1785 ERR_R_MALLOC_FAILURE);
1786 DH_free(dh_clnt);
1787 goto err;
1788 }
1789
1790 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1791 if (dh_len <= 0) {
1792 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1793 DH_free(dh_clnt);
1794 goto err;
1795 }
1796 pms_len = dh_len;
1797
1798 /* send off the data */
1799 pub_len = BN_num_bytes(dh_clnt->pub_key);
1800 s2n(pub_len, p);
1801 BN_bn2bin(dh_clnt->pub_key, p);
1802 n += 2 + pub_len;
1803
1804 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001805 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001806 const EC_GROUP *srvr_group = NULL;
1807 EC_KEY *tkey;
1808 int field_size = 0, ecdh_len;
1809
1810 if (s->session->sess_cert == NULL) {
1811 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1812 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1813 SSL_R_UNEXPECTED_MESSAGE);
1814 goto err;
1815 }
1816
1817 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1818 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1819 ERR_R_INTERNAL_ERROR);
1820 goto err;
1821 }
1822
1823 tkey = s->session->sess_cert->peer_ecdh_tmp;
1824
1825 srvr_group = EC_KEY_get0_group(tkey);
1826 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1827 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1828 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1829 ERR_R_INTERNAL_ERROR);
1830 goto err;
1831 }
1832
1833 clnt_ecdh = EC_KEY_new();
1834 if (clnt_ecdh == NULL) {
1835 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1836 ERR_R_MALLOC_FAILURE);
1837 goto err;
1838 }
1839
1840 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1841 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1842 goto err;
1843 }
1844
1845 /* Generate a new ECDH key pair */
1846 if (!EC_KEY_generate_key(clnt_ecdh)) {
1847 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1848 goto err;
1849 }
1850
1851 field_size = EC_GROUP_get_degree(srvr_group);
1852 if (field_size <= 0) {
1853 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1854 goto err;
1855 }
1856
1857 pms_len = (field_size + 7) / 8;
1858 pms = OPENSSL_malloc(pms_len);
1859 if (pms == NULL) {
1860 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1861 ERR_R_MALLOC_FAILURE);
1862 goto err;
1863 }
1864
1865 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1866 if (ecdh_len <= 0) {
1867 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1868 goto err;
1869 }
1870 pms_len = ecdh_len;
1871
1872 /* First check the size of encoding and allocate memory accordingly. */
1873 encoded_pt_len =
1874 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1875 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1876
1877 encodedPoint =
1878 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1879 bn_ctx = BN_CTX_new();
1880 if (encodedPoint == NULL || bn_ctx == NULL) {
1881 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1882 ERR_R_MALLOC_FAILURE);
1883 goto err;
1884 }
1885
1886 /* Encode the public key */
1887 encoded_pt_len = EC_POINT_point2oct(
1888 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1889 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1890
1891 *p = encoded_pt_len; /* length of encoded point */
1892 /* Encoded point will be copied here */
1893 p += 1;
1894 n += 1;
1895 /* copy the point */
1896 memcpy(p, encodedPoint, encoded_pt_len);
1897 /* increment n to account for length field */
1898 n += encoded_pt_len;
1899
1900 /* Free allocated memory */
1901 BN_CTX_free(bn_ctx);
1902 bn_ctx = NULL;
1903 OPENSSL_free(encodedPoint);
1904 encodedPoint = NULL;
1905 EC_KEY_free(clnt_ecdh);
1906 clnt_ecdh = NULL;
1907 EVP_PKEY_free(srvr_pub_pkey);
1908 srvr_pub_pkey = NULL;
1909 } else if (alg_k & SSL_kPSK) {
1910 /* For plain PSK, other_secret is a block of 0s with the same length as
1911 * the pre-shared key. */
1912 pms_len = psk_len;
1913 pms = OPENSSL_malloc(pms_len);
1914 if (pms == NULL) {
1915 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1916 ERR_R_MALLOC_FAILURE);
1917 goto err;
1918 }
1919 memset(pms, 0, pms_len);
1920 } else {
1921 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1922 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1923 ERR_R_INTERNAL_ERROR);
1924 goto err;
1925 }
1926
1927 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1928 * key. */
1929 if (alg_a & SSL_aPSK) {
1930 CBB cbb, child;
1931 uint8_t *new_pms;
1932 size_t new_pms_len;
1933
1934 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1935 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1936 ERR_R_MALLOC_FAILURE);
1937 goto err;
1938 }
1939 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1940 !CBB_add_bytes(&child, pms, pms_len) ||
1941 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1942 !CBB_add_bytes(&child, psk, psk_len) ||
1943 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1944 CBB_cleanup(&cbb);
1945 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1946 ERR_R_INTERNAL_ERROR);
1947 goto err;
1948 }
1949 OPENSSL_cleanse(pms, pms_len);
1950 OPENSSL_free(pms);
1951 pms = new_pms;
1952 pms_len = new_pms_len;
1953 }
1954
1955 /* The message must be added to the finished hash before calculating the
1956 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001957 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1958 goto err;
1959 }
Adam Langley1bea1732014-12-17 19:06:57 -08001960 s->state = SSL3_ST_CW_KEY_EXCH_B;
1961
1962 s->session->master_key_length = s->enc_method->generate_master_secret(
1963 s, s->session->master_key, pms, pms_len);
1964 if (s->session->master_key_length == 0) {
1965 goto err;
1966 }
1967 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1968 OPENSSL_cleanse(pms, pms_len);
1969 OPENSSL_free(pms);
1970 }
1971
1972 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001973 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001974
1975err:
Adam Langley1bea1732014-12-17 19:06:57 -08001976 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001977 OPENSSL_free(encodedPoint);
1978 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001979 EVP_PKEY_free(srvr_pub_pkey);
1980 if (pms) {
1981 OPENSSL_cleanse(pms, pms_len);
1982 OPENSSL_free(pms);
1983 }
1984 return -1;
1985}
Adam Langley1258b6a2014-06-20 12:00:00 -07001986
Adam Langley1bea1732014-12-17 19:06:57 -08001987int ssl3_send_cert_verify(SSL *s) {
1988 uint8_t *buf, *p;
1989 const EVP_MD *md = NULL;
1990 uint8_t digest[EVP_MAX_MD_SIZE];
1991 size_t digest_length;
1992 EVP_PKEY *pkey;
1993 EVP_PKEY_CTX *pctx = NULL;
1994 size_t signature_length = 0;
1995 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langley1bea1732014-12-17 19:06:57 -08001997 buf = (uint8_t *)s->init_buf->data;
1998
1999 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2000 p = ssl_handshake_start(s);
2001 pkey = s->cert->key->privatekey;
2002
2003 /* Write out the digest type if needbe. */
2004 if (SSL_USE_SIGALGS(s)) {
2005 md = tls1_choose_signing_digest(s, pkey);
2006 if (!tls12_get_sigandhash(p, pkey, md)) {
2007 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2008 goto err;
2009 }
2010 p += 2;
2011 n += 2;
2012 }
2013
2014 /* Compute the digest. */
2015 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2016 goto err;
2017 }
2018
2019 /* The handshake buffer is no longer necessary. */
2020 if (s->s3->handshake_buffer &&
2021 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2022 goto err;
2023 }
2024
2025 /* Sign the digest. */
2026 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2027 if (pctx == NULL) {
2028 goto err;
2029 }
2030
2031 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2032 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2033 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2034 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2035 goto err;
2036 }
2037
2038 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2039 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2040 goto err;
2041 }
2042
2043 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2044 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2045 goto err;
2046 }
2047
2048 s2n(signature_length, p);
2049 n += signature_length + 2;
2050
David Benjaminfbdfefb2015-02-16 19:33:53 -05002051 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2052 goto err;
2053 }
Adam Langley1bea1732014-12-17 19:06:57 -08002054 s->state = SSL3_ST_CW_CERT_VRFY_B;
2055 }
2056
2057 EVP_PKEY_CTX_free(pctx);
2058 return ssl_do_write(s);
2059
2060err:
2061 EVP_PKEY_CTX_free(pctx);
2062 return -1;
2063}
2064
2065/* ssl3_has_client_certificate returns true if a client certificate is
2066 * configured. */
2067static int ssl3_has_client_certificate(SSL *s) {
2068 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2069}
2070
2071int ssl3_send_client_certificate(SSL *s) {
2072 X509 *x509 = NULL;
2073 EVP_PKEY *pkey = NULL;
2074 int i;
2075
2076 if (s->state == SSL3_ST_CW_CERT_A) {
2077 /* Let cert callback update client certificates if required */
2078 if (s->cert->cert_cb) {
2079 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2080 if (i < 0) {
2081 s->rwstate = SSL_X509_LOOKUP;
2082 return -1;
2083 }
2084 if (i == 0) {
2085 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2086 return 0;
2087 }
2088 s->rwstate = SSL_NOTHING;
2089 }
2090
2091 if (ssl3_has_client_certificate(s)) {
2092 s->state = SSL3_ST_CW_CERT_C;
2093 } else {
2094 s->state = SSL3_ST_CW_CERT_B;
2095 }
2096 }
2097
2098 /* We need to get a client cert */
2099 if (s->state == SSL3_ST_CW_CERT_B) {
2100 /* If we get an error, we need to:
2101 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2102 * We then get retried later */
2103 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2104 if (i < 0) {
2105 s->rwstate = SSL_X509_LOOKUP;
2106 return -1;
2107 }
2108 s->rwstate = SSL_NOTHING;
2109 if (i == 1 && pkey != NULL && x509 != NULL) {
2110 s->state = SSL3_ST_CW_CERT_B;
2111 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2112 i = 0;
2113 }
2114 } else if (i == 1) {
2115 i = 0;
2116 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2117 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2118 }
2119
David Benjamin2755a3e2015-04-22 16:17:58 -04002120 X509_free(x509);
2121 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002122 if (i && !ssl3_has_client_certificate(s)) {
2123 i = 0;
2124 }
2125 if (i == 0) {
2126 if (s->version == SSL3_VERSION) {
2127 s->s3->tmp.cert_req = 0;
2128 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2129 return 1;
2130 } else {
2131 s->s3->tmp.cert_req = 2;
2132 }
2133 }
2134
2135 /* Ok, we have a cert */
2136 s->state = SSL3_ST_CW_CERT_C;
2137 }
2138
2139 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002140 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2141 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2142 return -1;
2143 }
Adam Langley1bea1732014-12-17 19:06:57 -08002144 }
2145
2146 /* SSL3_ST_CW_CERT_D */
2147 return ssl_do_write(s);
2148}
2149
2150#define has_bits(i, m) (((i) & (m)) == (m))
2151
2152int ssl3_check_cert_and_algorithm(SSL *s) {
2153 int i, idx;
2154 long alg_k, alg_a;
2155 EVP_PKEY *pkey = NULL;
2156 SESS_CERT *sc;
2157 DH *dh;
2158
2159 /* we don't have a certificate */
2160 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2161 return 1;
2162 }
2163
2164 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2165 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2166
2167 sc = s->session->sess_cert;
2168 if (sc == NULL) {
2169 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2170 goto err;
2171 }
2172
2173 dh = s->session->sess_cert->peer_dh_tmp;
2174
2175 /* This is the passed certificate */
2176
2177 idx = sc->peer_cert_type;
2178 if (idx == SSL_PKEY_ECC) {
2179 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2180 /* check failed */
2181 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2182 goto f_err;
2183 } else {
2184 return 1;
2185 }
2186 } else if (alg_a & SSL_aECDSA) {
2187 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2188 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2189 goto f_err;
2190 }
2191 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2192 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2193 EVP_PKEY_free(pkey);
2194
2195 /* Check that we have a certificate if we require one */
2196 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2197 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2198 SSL_R_MISSING_RSA_SIGNING_CERT);
2199 goto f_err;
2200 }
2201
2202 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2203 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2204 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2205 goto f_err;
2206 }
2207
David Benjamin7061e282015-03-19 11:10:48 -04002208 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002209 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2210 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2211 goto f_err;
2212 }
2213
2214 return 1;
2215
2216f_err:
2217 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2218err:
2219 return 0;
2220}
2221
2222int ssl3_send_next_proto(SSL *s) {
2223 unsigned int len, padding_len;
2224 uint8_t *d, *p;
2225
2226 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2227 len = s->next_proto_negotiated_len;
2228 padding_len = 32 - ((len + 2) % 32);
2229
2230 d = p = ssl_handshake_start(s);
2231 *(p++) = len;
2232 memcpy(p, s->next_proto_negotiated, len);
2233 p += len;
2234 *(p++) = padding_len;
2235 memset(p, 0, padding_len);
2236 p += padding_len;
2237
David Benjaminfbdfefb2015-02-16 19:33:53 -05002238 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2239 return -1;
2240 }
Adam Langley1bea1732014-12-17 19:06:57 -08002241 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2242 }
2243
2244 return ssl_do_write(s);
2245}
2246
2247int ssl3_send_channel_id(SSL *s) {
2248 uint8_t *d;
2249 int ret = -1, public_key_len;
2250 EVP_MD_CTX md_ctx;
2251 size_t sig_len;
2252 ECDSA_SIG *sig = NULL;
2253 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2254
2255 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2256 return ssl_do_write(s);
2257 }
2258
2259 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2260 EVP_PKEY *key = NULL;
2261 s->ctx->channel_id_cb(s, &key);
2262 if (key != NULL) {
2263 s->tlsext_channel_id_private = key;
2264 }
2265 }
2266
2267 if (!s->tlsext_channel_id_private) {
2268 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2269 return -1;
2270 }
2271 s->rwstate = SSL_NOTHING;
2272
2273 d = ssl_handshake_start(s);
2274 if (s->s3->tlsext_channel_id_new) {
2275 s2n(TLSEXT_TYPE_channel_id_new, d);
2276 } else {
2277 s2n(TLSEXT_TYPE_channel_id, d);
2278 }
2279 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2280
2281 EVP_MD_CTX_init(&md_ctx);
2282
2283 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2284 if (public_key_len <= 0) {
2285 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2286 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2287 goto err;
2288 }
2289
2290 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2291 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2292 * field elements as 32-byte, big-endian numbers. */
2293 if (public_key_len != 65) {
2294 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2295 goto err;
2296 }
2297 public_key = OPENSSL_malloc(public_key_len);
2298 if (!public_key) {
2299 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2300 goto err;
2301 }
2302
2303 derp = public_key;
2304 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2305
2306 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2307 s->tlsext_channel_id_private) != 1) {
2308 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2309 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2310 goto err;
2311 }
2312
2313 if (!tls1_channel_id_hash(&md_ctx, s)) {
2314 goto err;
2315 }
2316
2317 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2318 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2319 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2320 goto err;
2321 }
2322
2323 der_sig = OPENSSL_malloc(sig_len);
2324 if (!der_sig) {
2325 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2326 goto err;
2327 }
2328
2329 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2330 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2331 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2332 goto err;
2333 }
2334
2335 derp = der_sig;
2336 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2337 if (sig == NULL) {
2338 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2339 goto err;
2340 }
2341
2342 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2343 memcpy(d, public_key + 1, 64);
2344 d += 64;
2345 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2346 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2347 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2348 goto err;
2349 }
2350
David Benjaminfbdfefb2015-02-16 19:33:53 -05002351 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2352 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2353 goto err;
2354 }
Adam Langley1bea1732014-12-17 19:06:57 -08002355 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2356
2357 ret = ssl_do_write(s);
2358
2359err:
2360 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002361 OPENSSL_free(public_key);
2362 OPENSSL_free(der_sig);
2363 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002364
2365 return ret;
2366}
2367
2368int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2369 int i = 0;
2370 if (s->ctx->client_cert_cb) {
2371 i = s->ctx->client_cert_cb(s, px509, ppkey);
2372 }
2373 return i;
2374}