blob: dda637df2b73e1d30b6b7d59535110e731d57376 [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
Adam Langley1bea1732014-12-17 19:06:57 -0800548 if (s->init_buf != NULL) {
549 BUF_MEM_free(s->init_buf);
550 s->init_buf = NULL;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 /* Remove write buffering now. */
554 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langley1bea1732014-12-17 19:06:57 -0800556 s->init_num = 0;
557 s->renegotiate = 0;
558 s->new_session = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500559 s->s3->tmp.in_false_start = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langley1bea1732014-12-17 19:06:57 -0800561 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langley1bea1732014-12-17 19:06:57 -0800563 ret = 1;
564 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langley1bea1732014-12-17 19:06:57 -0800566 if (cb != NULL) {
567 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langley1bea1732014-12-17 19:06:57 -0800570 goto end;
571
572 default:
573 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
574 ret = -1;
575 goto end;
576 }
577
578 if (!s->s3->tmp.reuse_message && !skip) {
579 if (cb != NULL && s->state != state) {
580 new_state = s->state;
581 s->state = state;
582 cb(s, SSL_CB_CONNECT_LOOP, 1);
583 s->state = new_state;
584 }
585 }
586 skip = 0;
587 }
588
Adam Langley95c29f32014-06-20 12:00:00 -0700589end:
Adam Langley1bea1732014-12-17 19:06:57 -0800590 s->in_handshake--;
591 if (buf != NULL) {
592 BUF_MEM_free(buf);
593 }
594 if (cb != NULL) {
595 cb(s, SSL_CB_CONNECT_EXIT, ret);
596 }
597 return ret;
598}
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley1bea1732014-12-17 19:06:57 -0800600int ssl3_send_client_hello(SSL *s) {
601 uint8_t *buf, *p, *d;
602 int i;
603 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langley1bea1732014-12-17 19:06:57 -0800605 buf = (uint8_t *)s->init_buf->data;
606 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
607 if (!s->s3->have_version) {
608 uint16_t max_version = ssl3_get_max_client_version(s);
609 /* Disabling all versions is silly: return an error. */
610 if (max_version == 0) {
611 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
612 goto err;
613 }
614 s->version = max_version;
615 s->client_version = max_version;
616 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500617
Adam Langley1bea1732014-12-17 19:06:57 -0800618 /* If the configured session was created at a version higher than our
619 * maximum version, drop it. */
620 if (s->session &&
621 (s->session->session_id_length == 0 || s->session->not_resumable ||
622 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
623 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
624 SSL_set_session(s, NULL);
625 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500626
Adam Langley1bea1732014-12-17 19:06:57 -0800627 /* else use the pre-loaded session */
628 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langley1bea1732014-12-17 19:06:57 -0800630 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
631 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400632 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
633 !ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random))) {
634 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800635 }
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langley1bea1732014-12-17 19:06:57 -0800637 /* Do the message type and length last. Note: the final argument to
638 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
639 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700640
Adam Langley1bea1732014-12-17 19:06:57 -0800641 /* version indicates the negotiated version: for example from an SSLv2/v3
642 * compatible client hello). The client_version field is the maximum
643 * version we permit and it is also used in RSA encrypted premaster
644 * secrets. Some servers can choke if we initially report a higher version
645 * then renegotiate to a lower one in the premaster secret. This didn't
646 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
647 * or later if the server only supports 1.0.
648 *
649 * Possible scenario with previous logic:
650 * 1. Client hello indicates TLS 1.2
651 * 2. Server hello says TLS 1.0
652 * 3. RSA encrypted premaster secret uses 1.2.
653 * 4. Handhaked proceeds using TLS 1.0.
654 * 5. Server sends hello request to renegotiate.
655 * 6. Client hello indicates TLS v1.0 as we now
656 * know that is maximum server supports.
657 * 7. Server chokes on RSA encrypted premaster secret
658 * containing version 1.0.
659 *
660 * For interoperability it should be OK to always use the maximum version
661 * we support in client hello and then rely on the checking of version to
662 * ensure the servers isn't being inconsistent: for example initially
663 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
664 * using client_version in client hello and not resetting it to the
665 * negotiated version. */
666 *(p++) = s->client_version >> 8;
667 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley1bea1732014-12-17 19:06:57 -0800669 /* Random stuff */
670 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
671 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langley1bea1732014-12-17 19:06:57 -0800673 /* Session ID */
674 if (s->new_session || s->session == NULL) {
675 i = 0;
676 } else {
677 i = s->session->session_id_length;
678 }
679 *(p++) = i;
680 if (i != 0) {
681 if (i > (int)sizeof(s->session->session_id)) {
682 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
683 goto err;
684 }
685 memcpy(p, s->session->session_id, i);
686 p += i;
687 }
Adam Langley95c29f32014-06-20 12:00:00 -0700688
Adam Langley1bea1732014-12-17 19:06:57 -0800689 /* cookie stuff for DTLS */
690 if (SSL_IS_DTLS(s)) {
691 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
692 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
693 goto err;
694 }
695 *(p++) = s->d1->cookie_len;
696 memcpy(p, s->d1->cookie, s->d1->cookie_len);
697 p += s->d1->cookie_len;
698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langley1bea1732014-12-17 19:06:57 -0800700 /* Ciphers supported */
701 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
702 if (i == 0) {
703 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
704 SSL_R_NO_CIPHERS_AVAILABLE);
705 goto err;
706 }
707 s2n(i, p);
708 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley1bea1732014-12-17 19:06:57 -0800710 /* COMPRESSION */
711 *(p++) = 1;
712 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langley1bea1732014-12-17 19:06:57 -0800714 /* TLS extensions*/
715 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
716 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
717 goto err;
718 }
719
720 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
721 p - buf);
722 if (p == NULL) {
723 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
724 goto err;
725 }
726
727 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500728 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
729 goto err;
730 }
Adam Langley1bea1732014-12-17 19:06:57 -0800731 s->state = SSL3_ST_CW_CLNT_HELLO_B;
732 }
733
734 /* SSL3_ST_CW_CLNT_HELLO_B */
735 return ssl_do_write(s);
736
Adam Langley95c29f32014-06-20 12:00:00 -0700737err:
Adam Langley1bea1732014-12-17 19:06:57 -0800738 return -1;
739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langley1bea1732014-12-17 19:06:57 -0800741int ssl3_get_server_hello(SSL *s) {
742 STACK_OF(SSL_CIPHER) * sk;
743 const SSL_CIPHER *c;
744 CERT *ct = s->cert;
745 int al = SSL_AD_INTERNAL_ERROR, ok;
746 long n;
747 CBS server_hello, server_random, session_id;
748 uint16_t server_version, cipher_suite;
749 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400750 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700751
Adam Langley1bea1732014-12-17 19:06:57 -0800752 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
753 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
754 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500755 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langley1bea1732014-12-17 19:06:57 -0800757 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500758 uint32_t err = ERR_peek_error();
759 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
760 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
761 /* Add a dedicated error code to the queue for a handshake_failure alert
762 * in response to ClientHello. This matches NSS's client behavior and
763 * gives a better error on a (probable) failure to negotiate initial
764 * parameters. Note: this error code comes after the original one.
765 *
766 * See https://crbug.com/446505. */
767 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
768 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
769 }
Adam Langley1bea1732014-12-17 19:06:57 -0800770 return n;
771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langley1bea1732014-12-17 19:06:57 -0800773 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400774
Adam Langley1bea1732014-12-17 19:06:57 -0800775 if (!CBS_get_u16(&server_hello, &server_version) ||
776 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
777 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
778 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
779 !CBS_get_u16(&server_hello, &cipher_suite) ||
780 !CBS_get_u8(&server_hello, &compression_method)) {
781 al = SSL_AD_DECODE_ERROR;
782 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
783 goto f_err;
784 }
David Benjamina03d95d2014-07-12 19:49:07 -0400785
Adam Langley1bea1732014-12-17 19:06:57 -0800786 if (!s->s3->have_version) {
787 if (!ssl3_is_version_enabled(s, server_version)) {
788 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
789 s->version = server_version;
790 /* Mark the version as fixed so the record-layer version is not clamped
791 * to TLS 1.0. */
792 s->s3->have_version = 1;
793 al = SSL_AD_PROTOCOL_VERSION;
794 goto f_err;
795 }
796 s->version = server_version;
797 s->enc_method = ssl3_get_enc_method(server_version);
798 assert(s->enc_method != NULL);
799 /* At this point, the connection's version is known and s->version is
800 * fixed. Begin enforcing the record-layer version. */
801 s->s3->have_version = 1;
802 } else if (server_version != s->version) {
803 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
804 al = SSL_AD_PROTOCOL_VERSION;
805 goto f_err;
806 }
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langley1bea1732014-12-17 19:06:57 -0800808 /* Copy over the server random. */
809 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langley1bea1732014-12-17 19:06:57 -0800811 assert(s->session == NULL || s->session->session_id_length > 0);
812 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
813 s->session->session_id_length)) {
814 if (s->sid_ctx_length != s->session->sid_ctx_length ||
815 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
816 /* actually a client application bug */
817 al = SSL_AD_ILLEGAL_PARAMETER;
818 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
819 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
820 goto f_err;
821 }
822 s->hit = 1;
823 } else {
824 /* The session wasn't resumed. Create a fresh SSL_SESSION to
825 * fill out. */
826 s->hit = 0;
827 if (!ssl_get_new_session(s, 0)) {
828 goto f_err;
829 }
830 /* Note: session_id could be empty. */
831 s->session->session_id_length = CBS_len(&session_id);
832 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
833 }
David Benjamina03d95d2014-07-12 19:49:07 -0400834
Adam Langley1bea1732014-12-17 19:06:57 -0800835 c = ssl3_get_cipher_by_value(cipher_suite);
836 if (c == NULL) {
837 /* unknown cipher */
838 al = SSL_AD_ILLEGAL_PARAMETER;
839 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
840 SSL_R_UNKNOWN_CIPHER_RETURNED);
841 goto f_err;
842 }
843 /* ct->mask_ssl was computed from client capabilities. Now
844 * that the final version is known, compute a new mask_ssl. */
845 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
846 mask_ssl = SSL_TLSV1_2;
847 } else {
848 mask_ssl = 0;
849 }
850 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
851 * the server selected it, it's an error. */
852 if ((c->algorithm_ssl & mask_ssl) ||
853 (c->algorithm_mkey & ct->mask_k) ||
854 (c->algorithm_auth & ct->mask_a)) {
855 al = SSL_AD_ILLEGAL_PARAMETER;
856 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
857 goto f_err;
858 }
Adam Langley95c29f32014-06-20 12:00:00 -0700859
Adam Langley1bea1732014-12-17 19:06:57 -0800860 sk = ssl_get_ciphers_by_id(s);
861 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
862 /* we did not say we would use this cipher */
863 al = SSL_AD_ILLEGAL_PARAMETER;
864 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
865 goto f_err;
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
David Benjaminece3de92015-03-16 18:02:20 -0400868 if (s->hit) {
869 if (s->session->cipher != c) {
870 al = SSL_AD_ILLEGAL_PARAMETER;
871 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
872 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
873 goto f_err;
874 }
875 if (s->session->ssl_version != s->version) {
876 al = SSL_AD_ILLEGAL_PARAMETER;
877 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
878 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
879 goto f_err;
880 }
Adam Langley1bea1732014-12-17 19:06:57 -0800881 }
882 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500883
Adam Langley1bea1732014-12-17 19:06:57 -0800884 /* Don't digest cached records if no sigalgs: we may need them for client
885 * authentication. */
886 if (!SSL_USE_SIGALGS(s) &&
887 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
888 goto f_err;
889 }
David Benjamina03d95d2014-07-12 19:49:07 -0400890
Adam Langley1bea1732014-12-17 19:06:57 -0800891 /* Only the NULL compression algorithm is supported. */
892 if (compression_method != 0) {
893 al = SSL_AD_ILLEGAL_PARAMETER;
894 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
895 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
896 goto f_err;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langley1bea1732014-12-17 19:06:57 -0800899 /* TLS extensions */
900 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
901 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
902 goto err;
903 }
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langley1bea1732014-12-17 19:06:57 -0800905 /* There should be nothing left over in the record. */
906 if (CBS_len(&server_hello) != 0) {
907 /* wrong packet length */
908 al = SSL_AD_DECODE_ERROR;
909 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
910 goto f_err;
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langley1bea1732014-12-17 19:06:57 -0800913 return 1;
914
Adam Langley95c29f32014-06-20 12:00:00 -0700915f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800916 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700917err:
Adam Langley1bea1732014-12-17 19:06:57 -0800918 return -1;
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langley1bea1732014-12-17 19:06:57 -0800921int ssl3_get_server_certificate(SSL *s) {
922 int al, i, ok, ret = -1;
923 unsigned long n;
924 X509 *x = NULL;
925 STACK_OF(X509) *sk = NULL;
926 SESS_CERT *sc;
927 EVP_PKEY *pkey = NULL;
928 CBS cbs, certificate_list;
929 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langley1bea1732014-12-17 19:06:57 -0800931 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
932 SSL3_MT_CERTIFICATE, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500933 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langley1bea1732014-12-17 19:06:57 -0800935 if (!ok) {
936 return n;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langley1bea1732014-12-17 19:06:57 -0800939 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langley1bea1732014-12-17 19:06:57 -0800941 sk = sk_X509_new_null();
942 if (sk == NULL) {
943 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
944 goto err;
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langley1bea1732014-12-17 19:06:57 -0800947 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
948 CBS_len(&cbs) != 0) {
949 al = SSL_AD_DECODE_ERROR;
950 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
951 goto f_err;
952 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400953
Adam Langley1bea1732014-12-17 19:06:57 -0800954 while (CBS_len(&certificate_list) > 0) {
955 CBS certificate;
956 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
957 al = SSL_AD_DECODE_ERROR;
958 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
959 SSL_R_CERT_LENGTH_MISMATCH);
960 goto f_err;
961 }
962 data = CBS_data(&certificate);
963 x = d2i_X509(NULL, &data, CBS_len(&certificate));
964 if (x == NULL) {
965 al = SSL_AD_BAD_CERTIFICATE;
966 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
967 goto f_err;
968 }
969 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
970 al = SSL_AD_DECODE_ERROR;
971 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
972 SSL_R_CERT_LENGTH_MISMATCH);
973 goto f_err;
974 }
975 if (!sk_X509_push(sk, x)) {
976 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
977 goto err;
978 }
979 x = NULL;
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langley1bea1732014-12-17 19:06:57 -0800982 i = ssl_verify_cert_chain(s, sk);
983 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
984 al = ssl_verify_alarm_type(s->verify_result);
985 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
986 SSL_R_CERTIFICATE_VERIFY_FAILED);
987 goto f_err;
988 }
989 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langley1bea1732014-12-17 19:06:57 -0800991 sc = ssl_sess_cert_new();
992 if (sc == NULL) {
993 goto err;
994 }
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langley1bea1732014-12-17 19:06:57 -0800996 if (s->session->sess_cert) {
997 ssl_sess_cert_free(s->session->sess_cert);
998 }
999 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langley1bea1732014-12-17 19:06:57 -08001001 sc->cert_chain = sk;
1002 /* Inconsistency alert: cert_chain does include the peer's certificate, which
1003 * we don't include in s3_srvr.c */
1004 x = sk_X509_value(sk, 0);
1005 sk = NULL;
1006 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langley1bea1732014-12-17 19:06:57 -08001008 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langley1bea1732014-12-17 19:06:57 -08001010 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1011 x = NULL;
1012 al = SSL3_AL_FATAL;
1013 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1014 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1015 goto f_err;
1016 }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langley1bea1732014-12-17 19:06:57 -08001018 i = ssl_cert_type(pkey);
1019 if (i < 0) {
1020 x = NULL;
1021 al = SSL3_AL_FATAL;
1022 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1023 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1024 goto f_err;
1025 }
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langley1bea1732014-12-17 19:06:57 -08001027 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1028 if (exp_idx >= 0 && i != exp_idx) {
1029 x = NULL;
1030 al = SSL_AD_ILLEGAL_PARAMETER;
1031 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1032 SSL_R_WRONG_CERTIFICATE_TYPE);
1033 goto f_err;
1034 }
1035 sc->peer_cert_type = i;
1036 /* Why would the following ever happen? We just created sc a couple of lines
1037 * ago. */
1038 if (sc->peer_pkeys[i].x509 != NULL) {
1039 X509_free(sc->peer_pkeys[i].x509);
1040 }
1041 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1042 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langley1bea1732014-12-17 19:06:57 -08001044 if (s->session->peer != NULL) {
1045 X509_free(s->session->peer);
1046 }
1047 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001048
Adam Langley1bea1732014-12-17 19:06:57 -08001049 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Adam Langley1bea1732014-12-17 19:06:57 -08001051 x = NULL;
1052 ret = 1;
1053
1054 if (0) {
1055 f_err:
1056 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1057 }
1058
1059err:
1060 EVP_PKEY_free(pkey);
1061 X509_free(x);
1062 sk_X509_pop_free(sk, X509_free);
1063 return ret;
1064}
1065
1066int ssl3_get_server_key_exchange(SSL *s) {
1067 EVP_MD_CTX md_ctx;
1068 int al, ok;
1069 long n, alg_k, alg_a;
1070 EVP_PKEY *pkey = NULL;
1071 const EVP_MD *md = NULL;
1072 RSA *rsa = NULL;
1073 DH *dh = NULL;
1074 EC_KEY *ecdh = NULL;
1075 BN_CTX *bn_ctx = NULL;
1076 EC_POINT *srvr_ecpoint = NULL;
1077 CBS server_key_exchange, server_key_exchange_orig, parameter;
1078
1079 /* use same message size as in ssl3_get_certificate_request() as
1080 * ServerKeyExchange message may be skipped */
1081 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1082 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001083 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001084 if (!ok) {
1085 return n;
1086 }
1087
1088 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1089 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1090 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1091 SSL_R_UNEXPECTED_MESSAGE);
1092 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1093 return -1;
1094 }
1095
1096 /* In plain PSK ciphersuite, ServerKeyExchange can be
1097 omitted if no identity hint is sent. Set session->sess_cert anyway to
1098 avoid problems later.*/
1099 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1100 /* PSK ciphersuites that also send a Certificate would have already
1101 * initialized |sess_cert|. */
1102 if (s->session->sess_cert == NULL) {
1103 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001104 if (s->session->sess_cert == NULL) {
1105 return -1;
1106 }
Adam Langley1bea1732014-12-17 19:06:57 -08001107 }
1108
1109 /* TODO(davidben): This should be reset in one place with the rest of the
1110 * handshake state. */
1111 if (s->s3->tmp.peer_psk_identity_hint) {
1112 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1113 s->s3->tmp.peer_psk_identity_hint = NULL;
1114 }
1115 }
1116 s->s3->tmp.reuse_message = 1;
1117 return 1;
1118 }
1119
1120 /* Retain a copy of the original CBS to compute the signature over. */
1121 CBS_init(&server_key_exchange, s->init_msg, n);
1122 server_key_exchange_orig = server_key_exchange;
1123
1124 if (s->session->sess_cert != NULL) {
1125 if (s->session->sess_cert->peer_dh_tmp) {
1126 DH_free(s->session->sess_cert->peer_dh_tmp);
1127 s->session->sess_cert->peer_dh_tmp = NULL;
1128 }
1129 if (s->session->sess_cert->peer_ecdh_tmp) {
1130 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1131 s->session->sess_cert->peer_ecdh_tmp = NULL;
1132 }
1133 } else {
1134 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001135 if (s->session->sess_cert == NULL) {
1136 return -1;
1137 }
Adam Langley1bea1732014-12-17 19:06:57 -08001138 }
1139
1140 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1141 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1142 EVP_MD_CTX_init(&md_ctx);
1143
1144 if (alg_a & SSL_aPSK) {
1145 CBS psk_identity_hint;
1146
1147 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1148 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1149 &psk_identity_hint)) {
1150 al = SSL_AD_DECODE_ERROR;
1151 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1152 goto f_err;
1153 }
1154
1155 /* Store PSK identity hint for later use, hint is used in
1156 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1157 * identity hint can be as long as the maximum length of a PSK identity.
1158 * Also do not allow NULL characters; identities are saved as C strings.
1159 *
1160 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1161 * a specific identity. */
1162 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1163 CBS_contains_zero_byte(&psk_identity_hint)) {
1164 al = SSL_AD_HANDSHAKE_FAILURE;
1165 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1166 SSL_R_DATA_LENGTH_TOO_LONG);
1167 goto f_err;
1168 }
1169
1170 /* Save the identity hint as a C string. */
1171 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1172 al = SSL_AD_INTERNAL_ERROR;
1173 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1174 ERR_R_MALLOC_FAILURE);
1175 goto f_err;
1176 }
1177 }
1178
David Benjamin7061e282015-03-19 11:10:48 -04001179 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001180 CBS dh_p, dh_g, dh_Ys;
1181
1182 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1183 CBS_len(&dh_p) == 0 ||
1184 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1185 CBS_len(&dh_g) == 0 ||
1186 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1187 CBS_len(&dh_Ys) == 0) {
1188 al = SSL_AD_DECODE_ERROR;
1189 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1190 goto f_err;
1191 }
1192
1193 dh = DH_new();
1194 if (dh == NULL) {
1195 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1196 goto err;
1197 }
1198
1199 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1200 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1201 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1202 NULL) {
1203 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1204 goto err;
1205 }
1206
1207 if (DH_size(dh) < 512 / 8) {
1208 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1209 SSL_R_BAD_DH_P_LENGTH);
1210 goto err;
1211 }
1212
1213 if (alg_a & SSL_aRSA) {
1214 pkey = X509_get_pubkey(
1215 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1216 }
1217 /* else anonymous DH, so no certificate or pkey. */
1218
1219 s->session->sess_cert->peer_dh_tmp = dh;
1220 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001221 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001222 uint16_t curve_id;
1223 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001224 const EC_GROUP *group;
1225 CBS point;
1226
1227 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1228 * key. Check curve is one of our preferences, if not server has sent an
1229 * invalid curve. */
1230 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1231 al = SSL_AD_DECODE_ERROR;
1232 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1233 goto f_err;
1234 }
1235
1236 curve_nid = tls1_ec_curve_id2nid(curve_id);
1237 if (curve_nid == 0) {
1238 al = SSL_AD_INTERNAL_ERROR;
1239 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1240 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1241 goto f_err;
1242 }
1243
David Benjamin4f7783e2015-03-05 03:05:11 -05001244 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001245 if (ecdh == NULL) {
1246 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001247 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001248 goto err;
1249 }
1250
Adam Langley1bea1732014-12-17 19:06:57 -08001251 group = EC_KEY_get0_group(ecdh);
1252
1253 /* Next, get the encoded ECPoint */
1254 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1255 al = SSL_AD_DECODE_ERROR;
1256 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1257 goto f_err;
1258 }
1259
1260 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1261 ((bn_ctx = BN_CTX_new()) == NULL)) {
1262 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1263 ERR_R_MALLOC_FAILURE);
1264 goto err;
1265 }
1266
1267 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1268 CBS_len(&point), bn_ctx)) {
1269 al = SSL_AD_DECODE_ERROR;
1270 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1271 goto f_err;
1272 }
1273
1274 /* The ECC/TLS specification does not mention the use of DSA to sign
1275 * ECParameters in the server key exchange message. We do support RSA and
1276 * ECDSA. */
1277 if (alg_a & SSL_aRSA) {
1278 pkey = X509_get_pubkey(
1279 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1280 } else if (alg_a & SSL_aECDSA) {
1281 pkey =
1282 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1283 }
1284 /* else anonymous ECDH, so no certificate or pkey. */
1285 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1286 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1287 ecdh = NULL;
1288 BN_CTX_free(bn_ctx);
1289 bn_ctx = NULL;
1290 EC_POINT_free(srvr_ecpoint);
1291 srvr_ecpoint = NULL;
1292 } else if (!(alg_k & SSL_kPSK)) {
1293 al = SSL_AD_UNEXPECTED_MESSAGE;
1294 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1295 SSL_R_UNEXPECTED_MESSAGE);
1296 goto f_err;
1297 }
1298
1299 /* At this point, |server_key_exchange| contains the signature, if any, while
1300 * |server_key_exchange_orig| contains the entire message. From that, derive
1301 * a CBS containing just the parameter. */
1302 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1303 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1304
1305 /* if it was signed, check the signature */
1306 if (pkey != NULL) {
1307 CBS signature;
1308
1309 if (SSL_USE_SIGALGS(s)) {
1310 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1311 goto f_err;
1312 }
1313 } else if (pkey->type == EVP_PKEY_RSA) {
1314 md = EVP_md5_sha1();
1315 } else {
1316 md = EVP_sha1();
1317 }
1318
1319 /* The last field in |server_key_exchange| is the signature. */
1320 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1321 CBS_len(&server_key_exchange) != 0) {
1322 al = SSL_AD_DECODE_ERROR;
1323 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1324 goto f_err;
1325 }
1326
1327 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1328 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1329 SSL3_RANDOM_SIZE) ||
1330 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1331 SSL3_RANDOM_SIZE) ||
1332 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1333 CBS_len(&parameter)) ||
1334 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1335 CBS_len(&signature))) {
1336 /* bad signature */
1337 al = SSL_AD_DECRYPT_ERROR;
1338 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1339 goto f_err;
1340 }
1341 } else {
1342 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1343 /* Might be wrong key type, check it */
1344 if (ssl3_check_cert_and_algorithm(s)) {
1345 /* Otherwise this shouldn't happen */
1346 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1347 ERR_R_INTERNAL_ERROR);
1348 }
1349 goto err;
1350 }
1351 /* still data left over */
1352 if (CBS_len(&server_key_exchange) > 0) {
1353 al = SSL_AD_DECODE_ERROR;
1354 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1355 SSL_R_EXTRA_DATA_IN_MESSAGE);
1356 goto f_err;
1357 }
1358 }
1359 EVP_PKEY_free(pkey);
1360 EVP_MD_CTX_cleanup(&md_ctx);
1361 return 1;
1362
Adam Langley95c29f32014-06-20 12:00:00 -07001363f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001364 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001365err:
Adam Langley1bea1732014-12-17 19:06:57 -08001366 EVP_PKEY_free(pkey);
1367 if (rsa != NULL) {
1368 RSA_free(rsa);
1369 }
1370 if (dh != NULL) {
1371 DH_free(dh);
1372 }
1373 BN_CTX_free(bn_ctx);
1374 EC_POINT_free(srvr_ecpoint);
1375 if (ecdh != NULL) {
1376 EC_KEY_free(ecdh);
1377 }
1378 EVP_MD_CTX_cleanup(&md_ctx);
1379 return -1;
1380}
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langley1bea1732014-12-17 19:06:57 -08001382static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1383 return X509_NAME_cmp(*a, *b);
1384}
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langley1bea1732014-12-17 19:06:57 -08001386int ssl3_get_certificate_request(SSL *s) {
1387 int ok, ret = 0;
1388 unsigned long n;
1389 X509_NAME *xn = NULL;
1390 STACK_OF(X509_NAME) *ca_sk = NULL;
1391 CBS cbs;
1392 CBS certificate_types;
1393 CBS certificate_authorities;
1394 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langley1bea1732014-12-17 19:06:57 -08001396 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1397 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001398 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001399
Adam Langley1bea1732014-12-17 19:06:57 -08001400 if (!ok) {
1401 return n;
1402 }
David Benjamin688d8df2014-11-02 23:06:42 -05001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001405
Adam Langley1bea1732014-12-17 19:06:57 -08001406 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1407 s->s3->tmp.reuse_message = 1;
1408 /* If we get here we don't need any cached handshake records as we wont be
1409 * doing client auth. */
1410 if (s->s3->handshake_buffer &&
1411 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1412 goto err;
1413 }
1414 return 1;
1415 }
David Benjamined439582014-07-14 19:13:02 -04001416
Adam Langley1bea1732014-12-17 19:06:57 -08001417 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1418 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1419 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1420 SSL_R_WRONG_MESSAGE_TYPE);
1421 goto err;
1422 }
Adam Langley95c29f32014-06-20 12:00:00 -07001423
Adam Langley1bea1732014-12-17 19:06:57 -08001424 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001425
Adam Langley1bea1732014-12-17 19:06:57 -08001426 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1427 if (ca_sk == NULL) {
1428 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1429 goto err;
1430 }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
Adam Langley1bea1732014-12-17 19:06:57 -08001432 /* get the certificate types */
1433 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1434 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1435 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1436 goto err;
1437 }
David Benjamined439582014-07-14 19:13:02 -04001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1440 &s->s3->tmp.num_certificate_types)) {
1441 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1442 goto err;
1443 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001444
Adam Langley1bea1732014-12-17 19:06:57 -08001445 if (SSL_USE_SIGALGS(s)) {
1446 CBS supported_signature_algorithms;
1447 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1449 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1450 goto err;
1451 }
David Benjamined439582014-07-14 19:13:02 -04001452
Adam Langley1bea1732014-12-17 19:06:57 -08001453 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1454 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1455 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1456 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1457 goto err;
1458 }
1459 }
David Benjamined439582014-07-14 19:13:02 -04001460
Adam Langley1bea1732014-12-17 19:06:57 -08001461 /* get the CA RDNs */
1462 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1463 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1464 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1465 goto err;
1466 }
Adam Langley95c29f32014-06-20 12:00:00 -07001467
Adam Langley1bea1732014-12-17 19:06:57 -08001468 while (CBS_len(&certificate_authorities) > 0) {
1469 CBS distinguished_name;
1470 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1471 &distinguished_name)) {
1472 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1473 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1474 SSL_R_CA_DN_TOO_LONG);
1475 goto err;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langley1bea1732014-12-17 19:06:57 -08001478 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001479
Adam Langley1bea1732014-12-17 19:06:57 -08001480 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1481 if (xn == NULL) {
1482 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1483 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1484 goto err;
1485 }
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langley1bea1732014-12-17 19:06:57 -08001487 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1488 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001489 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001490 goto err;
1491 }
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langley1bea1732014-12-17 19:06:57 -08001493 if (CBS_len(&distinguished_name) != 0) {
1494 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1495 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1496 SSL_R_CA_DN_LENGTH_MISMATCH);
1497 goto err;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langley1bea1732014-12-17 19:06:57 -08001500 if (!sk_X509_NAME_push(ca_sk, xn)) {
1501 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1502 ERR_R_MALLOC_FAILURE);
1503 goto err;
1504 }
1505 }
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langley1bea1732014-12-17 19:06:57 -08001507 /* we should setup a certificate to return.... */
1508 s->s3->tmp.cert_req = 1;
1509 if (s->s3->tmp.ca_names != NULL) {
1510 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1511 }
1512 s->s3->tmp.ca_names = ca_sk;
1513 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001514
Adam Langley1bea1732014-12-17 19:06:57 -08001515 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001516
Adam Langley1bea1732014-12-17 19:06:57 -08001517err:
1518 if (ca_sk != NULL) {
1519 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1520 }
1521 return ret;
1522}
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langley1bea1732014-12-17 19:06:57 -08001524int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001525 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001526 long n;
1527 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001528
Adam Langley1bea1732014-12-17 19:06:57 -08001529 n = s->method->ssl_get_message(
1530 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001531 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001532
Adam Langley1bea1732014-12-17 19:06:57 -08001533 if (!ok) {
1534 return n;
1535 }
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langley1bea1732014-12-17 19:06:57 -08001537 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001538
Adam Langley1bea1732014-12-17 19:06:57 -08001539 if (!CBS_get_u32(&new_session_ticket,
1540 &s->session->tlsext_tick_lifetime_hint) ||
1541 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1542 CBS_len(&new_session_ticket) != 0) {
1543 al = SSL_AD_DECODE_ERROR;
1544 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1545 goto f_err;
1546 }
Adam Langley95c29f32014-06-20 12:00:00 -07001547
Adam Langley1bea1732014-12-17 19:06:57 -08001548 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1549 &s->session->tlsext_ticklen)) {
1550 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1551 goto err;
1552 }
Adam Langley95c29f32014-06-20 12:00:00 -07001553
David Benjamin68070622015-02-08 23:44:59 -05001554 /* Generate a session ID for this session based on the session ticket. We use
1555 * the session ID mechanism for detecting ticket resumption. This also fits in
1556 * with assumptions elsewhere in OpenSSL.*/
1557 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1558 &s->session->session_id_length, EVP_sha256(), NULL)) {
1559 goto err;
1560 }
1561
1562 return 1;
David Benjamined439582014-07-14 19:13:02 -04001563
Adam Langley95c29f32014-06-20 12:00:00 -07001564f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001565 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001566err:
Adam Langley1bea1732014-12-17 19:06:57 -08001567 return -1;
1568}
Adam Langley95c29f32014-06-20 12:00:00 -07001569
Adam Langley1bea1732014-12-17 19:06:57 -08001570int ssl3_get_cert_status(SSL *s) {
1571 int ok, al;
1572 long n;
1573 CBS certificate_status, ocsp_response;
1574 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langley1bea1732014-12-17 19:06:57 -08001576 n = s->method->ssl_get_message(
1577 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001578 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langley1bea1732014-12-17 19:06:57 -08001580 if (!ok) {
1581 return n;
1582 }
Adam Langley95c29f32014-06-20 12:00:00 -07001583
David Benjamindcd979f2015-04-20 18:26:52 -04001584 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1585 /* A server may send status_request in ServerHello and then change
1586 * its mind about sending CertificateStatus. */
1587 s->s3->tmp.reuse_message = 1;
1588 return 1;
1589 }
1590
Adam Langley1bea1732014-12-17 19:06:57 -08001591 CBS_init(&certificate_status, s->init_msg, n);
1592 if (!CBS_get_u8(&certificate_status, &status_type) ||
1593 status_type != TLSEXT_STATUSTYPE_ocsp ||
1594 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1595 CBS_len(&ocsp_response) == 0 ||
1596 CBS_len(&certificate_status) != 0) {
1597 al = SSL_AD_DECODE_ERROR;
1598 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1599 goto f_err;
1600 }
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langley1bea1732014-12-17 19:06:57 -08001602 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1603 &s->session->ocsp_response_length)) {
1604 al = SSL_AD_INTERNAL_ERROR;
1605 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1606 goto f_err;
1607 }
1608 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langley95c29f32014-06-20 12:00:00 -07001610f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001611 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1612 return -1;
1613}
Adam Langley95c29f32014-06-20 12:00:00 -07001614
Adam Langley1bea1732014-12-17 19:06:57 -08001615int ssl3_get_server_done(SSL *s) {
1616 int ok;
1617 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001618
Adam Langley1bea1732014-12-17 19:06:57 -08001619 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1620 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1621 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001622 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 if (!ok) {
1625 return n;
1626 }
David Benjamin46062682014-07-14 19:14:32 -04001627
Adam Langley1bea1732014-12-17 19:06:57 -08001628 if (n > 0) {
1629 /* should contain no data */
1630 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1631 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1632 return -1;
1633 }
David Benjamin46062682014-07-14 19:14:32 -04001634
Adam Langley1bea1732014-12-17 19:06:57 -08001635 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001636}
Adam Langley1258b6a2014-06-20 12:00:00 -07001637
Adam Langley1258b6a2014-06-20 12:00:00 -07001638
Adam Langley1bea1732014-12-17 19:06:57 -08001639int ssl3_send_client_key_exchange(SSL *s) {
1640 uint8_t *p;
1641 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001642 uint32_t alg_k;
1643 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001644 uint8_t *q;
1645 EVP_PKEY *pkey = NULL;
1646 EC_KEY *clnt_ecdh = NULL;
1647 const EC_POINT *srvr_ecpoint = NULL;
1648 EVP_PKEY *srvr_pub_pkey = NULL;
1649 uint8_t *encodedPoint = NULL;
1650 int encoded_pt_len = 0;
1651 BN_CTX *bn_ctx = NULL;
1652 unsigned int psk_len = 0;
1653 uint8_t psk[PSK_MAX_PSK_LEN];
1654 uint8_t *pms = NULL;
1655 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001656
Adam Langley1bea1732014-12-17 19:06:57 -08001657 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1658 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001659
Adam Langley1bea1732014-12-17 19:06:57 -08001660 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1661 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 /* If using a PSK key exchange, prepare the pre-shared key. */
1664 if (alg_a & SSL_aPSK) {
1665 char identity[PSK_MAX_IDENTITY_LEN + 1];
1666 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001667
Adam Langley1bea1732014-12-17 19:06:57 -08001668 if (s->psk_client_callback == NULL) {
1669 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1670 SSL_R_PSK_NO_CLIENT_CB);
1671 goto err;
1672 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001673
Adam Langley1bea1732014-12-17 19:06:57 -08001674 memset(identity, 0, sizeof(identity));
1675 psk_len =
1676 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1677 sizeof(identity), psk, sizeof(psk));
1678 if (psk_len > PSK_MAX_PSK_LEN) {
1679 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1680 ERR_R_INTERNAL_ERROR);
1681 goto err;
1682 } else if (psk_len == 0) {
1683 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1684 SSL_R_PSK_IDENTITY_NOT_FOUND);
1685 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1686 goto err;
1687 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001688
Adam Langley1bea1732014-12-17 19:06:57 -08001689 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1690 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1691 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1692 ERR_R_INTERNAL_ERROR);
1693 goto err;
1694 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001695
Adam Langley1bea1732014-12-17 19:06:57 -08001696 if (s->session->psk_identity != NULL) {
1697 OPENSSL_free(s->session->psk_identity);
1698 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001699
Adam Langley1bea1732014-12-17 19:06:57 -08001700 s->session->psk_identity = BUF_strdup(identity);
1701 if (s->session->psk_identity == NULL) {
1702 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1703 ERR_R_MALLOC_FAILURE);
1704 goto err;
1705 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001706
Adam Langley1bea1732014-12-17 19:06:57 -08001707 /* Write out psk_identity. */
1708 s2n(identity_len, p);
1709 memcpy(p, identity, identity_len);
1710 p += identity_len;
1711 n = 2 + identity_len;
1712 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001713
Adam Langley1bea1732014-12-17 19:06:57 -08001714 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1715 if (alg_k & SSL_kRSA) {
1716 RSA *rsa;
1717 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001718
Adam Langley1bea1732014-12-17 19:06:57 -08001719 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1720 pms = OPENSSL_malloc(pms_len);
1721 if (pms == NULL) {
1722 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1723 ERR_R_MALLOC_FAILURE);
1724 goto err;
1725 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001726
Adam Langley1bea1732014-12-17 19:06:57 -08001727 if (s->session->sess_cert == NULL) {
1728 /* We should always have a server certificate with SSL_kRSA. */
1729 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1730 ERR_R_INTERNAL_ERROR);
1731 goto err;
1732 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001733
Adam Langley1bea1732014-12-17 19:06:57 -08001734 pkey = X509_get_pubkey(
1735 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1736 if (pkey == NULL ||
1737 pkey->type != EVP_PKEY_RSA ||
1738 pkey->pkey.rsa == NULL) {
1739 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1740 ERR_R_INTERNAL_ERROR);
1741 if (pkey != NULL) {
1742 EVP_PKEY_free(pkey);
1743 }
1744 goto err;
1745 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001746
Adam Langley1bea1732014-12-17 19:06:57 -08001747 rsa = pkey->pkey.rsa;
1748 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001749
Adam Langley1bea1732014-12-17 19:06:57 -08001750 pms[0] = s->client_version >> 8;
1751 pms[1] = s->client_version & 0xff;
1752 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1753 goto err;
1754 }
1755
1756 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1757
1758 q = p;
1759 /* In TLS and beyond, reserve space for the length prefix. */
1760 if (s->version > SSL3_VERSION) {
1761 p += 2;
1762 n += 2;
1763 }
1764 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1765 RSA_PKCS1_PADDING)) {
1766 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1767 SSL_R_BAD_RSA_ENCRYPT);
1768 goto err;
1769 }
1770 n += enc_pms_len;
1771
1772 /* Log the premaster secret, if logging is enabled. */
1773 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1774 pms_len)) {
1775 goto err;
1776 }
1777
1778 /* Fill in the length prefix. */
1779 if (s->version > SSL3_VERSION) {
1780 s2n(enc_pms_len, q);
1781 }
David Benjamin7061e282015-03-19 11:10:48 -04001782 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001783 DH *dh_srvr, *dh_clnt;
1784 SESS_CERT *scert = s->session->sess_cert;
1785 int dh_len;
1786 size_t pub_len;
1787
1788 if (scert == NULL) {
1789 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1790 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1791 SSL_R_UNEXPECTED_MESSAGE);
1792 goto err;
1793 }
1794
1795 if (scert->peer_dh_tmp == NULL) {
1796 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1797 ERR_R_INTERNAL_ERROR);
1798 goto err;
1799 }
1800 dh_srvr = scert->peer_dh_tmp;
1801
1802 /* generate a new random key */
1803 dh_clnt = DHparams_dup(dh_srvr);
1804 if (dh_clnt == NULL) {
1805 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1806 goto err;
1807 }
1808 if (!DH_generate_key(dh_clnt)) {
1809 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1810 DH_free(dh_clnt);
1811 goto err;
1812 }
1813
1814 pms_len = DH_size(dh_clnt);
1815 pms = OPENSSL_malloc(pms_len);
1816 if (pms == NULL) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1818 ERR_R_MALLOC_FAILURE);
1819 DH_free(dh_clnt);
1820 goto err;
1821 }
1822
1823 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1824 if (dh_len <= 0) {
1825 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1826 DH_free(dh_clnt);
1827 goto err;
1828 }
1829 pms_len = dh_len;
1830
1831 /* send off the data */
1832 pub_len = BN_num_bytes(dh_clnt->pub_key);
1833 s2n(pub_len, p);
1834 BN_bn2bin(dh_clnt->pub_key, p);
1835 n += 2 + pub_len;
1836
1837 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001838 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001839 const EC_GROUP *srvr_group = NULL;
1840 EC_KEY *tkey;
1841 int field_size = 0, ecdh_len;
1842
1843 if (s->session->sess_cert == NULL) {
1844 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1845 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1846 SSL_R_UNEXPECTED_MESSAGE);
1847 goto err;
1848 }
1849
1850 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1851 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1852 ERR_R_INTERNAL_ERROR);
1853 goto err;
1854 }
1855
1856 tkey = s->session->sess_cert->peer_ecdh_tmp;
1857
1858 srvr_group = EC_KEY_get0_group(tkey);
1859 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1860 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1861 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1862 ERR_R_INTERNAL_ERROR);
1863 goto err;
1864 }
1865
1866 clnt_ecdh = EC_KEY_new();
1867 if (clnt_ecdh == NULL) {
1868 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1869 ERR_R_MALLOC_FAILURE);
1870 goto err;
1871 }
1872
1873 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1874 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1875 goto err;
1876 }
1877
1878 /* Generate a new ECDH key pair */
1879 if (!EC_KEY_generate_key(clnt_ecdh)) {
1880 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1881 goto err;
1882 }
1883
1884 field_size = EC_GROUP_get_degree(srvr_group);
1885 if (field_size <= 0) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1887 goto err;
1888 }
1889
1890 pms_len = (field_size + 7) / 8;
1891 pms = OPENSSL_malloc(pms_len);
1892 if (pms == NULL) {
1893 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1894 ERR_R_MALLOC_FAILURE);
1895 goto err;
1896 }
1897
1898 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1899 if (ecdh_len <= 0) {
1900 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1901 goto err;
1902 }
1903 pms_len = ecdh_len;
1904
1905 /* First check the size of encoding and allocate memory accordingly. */
1906 encoded_pt_len =
1907 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1908 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1909
1910 encodedPoint =
1911 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1912 bn_ctx = BN_CTX_new();
1913 if (encodedPoint == NULL || bn_ctx == NULL) {
1914 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1915 ERR_R_MALLOC_FAILURE);
1916 goto err;
1917 }
1918
1919 /* Encode the public key */
1920 encoded_pt_len = EC_POINT_point2oct(
1921 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1922 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1923
1924 *p = encoded_pt_len; /* length of encoded point */
1925 /* Encoded point will be copied here */
1926 p += 1;
1927 n += 1;
1928 /* copy the point */
1929 memcpy(p, encodedPoint, encoded_pt_len);
1930 /* increment n to account for length field */
1931 n += encoded_pt_len;
1932
1933 /* Free allocated memory */
1934 BN_CTX_free(bn_ctx);
1935 bn_ctx = NULL;
1936 OPENSSL_free(encodedPoint);
1937 encodedPoint = NULL;
1938 EC_KEY_free(clnt_ecdh);
1939 clnt_ecdh = NULL;
1940 EVP_PKEY_free(srvr_pub_pkey);
1941 srvr_pub_pkey = NULL;
1942 } else if (alg_k & SSL_kPSK) {
1943 /* For plain PSK, other_secret is a block of 0s with the same length as
1944 * the pre-shared key. */
1945 pms_len = psk_len;
1946 pms = OPENSSL_malloc(pms_len);
1947 if (pms == NULL) {
1948 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1949 ERR_R_MALLOC_FAILURE);
1950 goto err;
1951 }
1952 memset(pms, 0, pms_len);
1953 } else {
1954 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1955 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1956 ERR_R_INTERNAL_ERROR);
1957 goto err;
1958 }
1959
1960 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1961 * key. */
1962 if (alg_a & SSL_aPSK) {
1963 CBB cbb, child;
1964 uint8_t *new_pms;
1965 size_t new_pms_len;
1966
1967 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1968 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1969 ERR_R_MALLOC_FAILURE);
1970 goto err;
1971 }
1972 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1973 !CBB_add_bytes(&child, pms, pms_len) ||
1974 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1975 !CBB_add_bytes(&child, psk, psk_len) ||
1976 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1977 CBB_cleanup(&cbb);
1978 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1979 ERR_R_INTERNAL_ERROR);
1980 goto err;
1981 }
1982 OPENSSL_cleanse(pms, pms_len);
1983 OPENSSL_free(pms);
1984 pms = new_pms;
1985 pms_len = new_pms_len;
1986 }
1987
1988 /* The message must be added to the finished hash before calculating the
1989 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001990 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1991 goto err;
1992 }
Adam Langley1bea1732014-12-17 19:06:57 -08001993 s->state = SSL3_ST_CW_KEY_EXCH_B;
1994
1995 s->session->master_key_length = s->enc_method->generate_master_secret(
1996 s, s->session->master_key, pms, pms_len);
1997 if (s->session->master_key_length == 0) {
1998 goto err;
1999 }
2000 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2001 OPENSSL_cleanse(pms, pms_len);
2002 OPENSSL_free(pms);
2003 }
2004
2005 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05002006 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002007
2008err:
Adam Langley1bea1732014-12-17 19:06:57 -08002009 BN_CTX_free(bn_ctx);
2010 if (encodedPoint != NULL) {
2011 OPENSSL_free(encodedPoint);
2012 }
2013 if (clnt_ecdh != NULL) {
2014 EC_KEY_free(clnt_ecdh);
2015 }
2016 EVP_PKEY_free(srvr_pub_pkey);
2017 if (pms) {
2018 OPENSSL_cleanse(pms, pms_len);
2019 OPENSSL_free(pms);
2020 }
2021 return -1;
2022}
Adam Langley1258b6a2014-06-20 12:00:00 -07002023
Adam Langley1bea1732014-12-17 19:06:57 -08002024int ssl3_send_cert_verify(SSL *s) {
2025 uint8_t *buf, *p;
2026 const EVP_MD *md = NULL;
2027 uint8_t digest[EVP_MAX_MD_SIZE];
2028 size_t digest_length;
2029 EVP_PKEY *pkey;
2030 EVP_PKEY_CTX *pctx = NULL;
2031 size_t signature_length = 0;
2032 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langley1bea1732014-12-17 19:06:57 -08002034 buf = (uint8_t *)s->init_buf->data;
2035
2036 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2037 p = ssl_handshake_start(s);
2038 pkey = s->cert->key->privatekey;
2039
2040 /* Write out the digest type if needbe. */
2041 if (SSL_USE_SIGALGS(s)) {
2042 md = tls1_choose_signing_digest(s, pkey);
2043 if (!tls12_get_sigandhash(p, pkey, md)) {
2044 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2045 goto err;
2046 }
2047 p += 2;
2048 n += 2;
2049 }
2050
2051 /* Compute the digest. */
2052 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2053 goto err;
2054 }
2055
2056 /* The handshake buffer is no longer necessary. */
2057 if (s->s3->handshake_buffer &&
2058 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2059 goto err;
2060 }
2061
2062 /* Sign the digest. */
2063 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2064 if (pctx == NULL) {
2065 goto err;
2066 }
2067
2068 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2069 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2070 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2071 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2072 goto err;
2073 }
2074
2075 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2076 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2077 goto err;
2078 }
2079
2080 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2081 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2082 goto err;
2083 }
2084
2085 s2n(signature_length, p);
2086 n += signature_length + 2;
2087
David Benjaminfbdfefb2015-02-16 19:33:53 -05002088 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2089 goto err;
2090 }
Adam Langley1bea1732014-12-17 19:06:57 -08002091 s->state = SSL3_ST_CW_CERT_VRFY_B;
2092 }
2093
2094 EVP_PKEY_CTX_free(pctx);
2095 return ssl_do_write(s);
2096
2097err:
2098 EVP_PKEY_CTX_free(pctx);
2099 return -1;
2100}
2101
2102/* ssl3_has_client_certificate returns true if a client certificate is
2103 * configured. */
2104static int ssl3_has_client_certificate(SSL *s) {
2105 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2106}
2107
2108int ssl3_send_client_certificate(SSL *s) {
2109 X509 *x509 = NULL;
2110 EVP_PKEY *pkey = NULL;
2111 int i;
2112
2113 if (s->state == SSL3_ST_CW_CERT_A) {
2114 /* Let cert callback update client certificates if required */
2115 if (s->cert->cert_cb) {
2116 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2117 if (i < 0) {
2118 s->rwstate = SSL_X509_LOOKUP;
2119 return -1;
2120 }
2121 if (i == 0) {
2122 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2123 return 0;
2124 }
2125 s->rwstate = SSL_NOTHING;
2126 }
2127
2128 if (ssl3_has_client_certificate(s)) {
2129 s->state = SSL3_ST_CW_CERT_C;
2130 } else {
2131 s->state = SSL3_ST_CW_CERT_B;
2132 }
2133 }
2134
2135 /* We need to get a client cert */
2136 if (s->state == SSL3_ST_CW_CERT_B) {
2137 /* If we get an error, we need to:
2138 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2139 * We then get retried later */
2140 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2141 if (i < 0) {
2142 s->rwstate = SSL_X509_LOOKUP;
2143 return -1;
2144 }
2145 s->rwstate = SSL_NOTHING;
2146 if (i == 1 && pkey != NULL && x509 != NULL) {
2147 s->state = SSL3_ST_CW_CERT_B;
2148 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2149 i = 0;
2150 }
2151 } else if (i == 1) {
2152 i = 0;
2153 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2154 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2155 }
2156
2157 if (x509 != NULL) {
2158 X509_free(x509);
2159 }
2160 if (pkey != NULL) {
2161 EVP_PKEY_free(pkey);
2162 }
2163 if (i && !ssl3_has_client_certificate(s)) {
2164 i = 0;
2165 }
2166 if (i == 0) {
2167 if (s->version == SSL3_VERSION) {
2168 s->s3->tmp.cert_req = 0;
2169 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2170 return 1;
2171 } else {
2172 s->s3->tmp.cert_req = 2;
2173 }
2174 }
2175
2176 /* Ok, we have a cert */
2177 s->state = SSL3_ST_CW_CERT_C;
2178 }
2179
2180 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002181 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2182 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2183 return -1;
2184 }
Adam Langley1bea1732014-12-17 19:06:57 -08002185 }
2186
2187 /* SSL3_ST_CW_CERT_D */
2188 return ssl_do_write(s);
2189}
2190
2191#define has_bits(i, m) (((i) & (m)) == (m))
2192
2193int ssl3_check_cert_and_algorithm(SSL *s) {
2194 int i, idx;
2195 long alg_k, alg_a;
2196 EVP_PKEY *pkey = NULL;
2197 SESS_CERT *sc;
2198 DH *dh;
2199
2200 /* we don't have a certificate */
2201 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2202 return 1;
2203 }
2204
2205 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2206 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2207
2208 sc = s->session->sess_cert;
2209 if (sc == NULL) {
2210 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2211 goto err;
2212 }
2213
2214 dh = s->session->sess_cert->peer_dh_tmp;
2215
2216 /* This is the passed certificate */
2217
2218 idx = sc->peer_cert_type;
2219 if (idx == SSL_PKEY_ECC) {
2220 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2221 /* check failed */
2222 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2223 goto f_err;
2224 } else {
2225 return 1;
2226 }
2227 } else if (alg_a & SSL_aECDSA) {
2228 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2229 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2230 goto f_err;
2231 }
2232 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2233 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2234 EVP_PKEY_free(pkey);
2235
2236 /* Check that we have a certificate if we require one */
2237 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2238 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2239 SSL_R_MISSING_RSA_SIGNING_CERT);
2240 goto f_err;
2241 }
2242
2243 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2244 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2245 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2246 goto f_err;
2247 }
2248
David Benjamin7061e282015-03-19 11:10:48 -04002249 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002250 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2251 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2252 goto f_err;
2253 }
2254
2255 return 1;
2256
2257f_err:
2258 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2259err:
2260 return 0;
2261}
2262
2263int ssl3_send_next_proto(SSL *s) {
2264 unsigned int len, padding_len;
2265 uint8_t *d, *p;
2266
2267 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2268 len = s->next_proto_negotiated_len;
2269 padding_len = 32 - ((len + 2) % 32);
2270
2271 d = p = ssl_handshake_start(s);
2272 *(p++) = len;
2273 memcpy(p, s->next_proto_negotiated, len);
2274 p += len;
2275 *(p++) = padding_len;
2276 memset(p, 0, padding_len);
2277 p += padding_len;
2278
David Benjaminfbdfefb2015-02-16 19:33:53 -05002279 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2280 return -1;
2281 }
Adam Langley1bea1732014-12-17 19:06:57 -08002282 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2283 }
2284
2285 return ssl_do_write(s);
2286}
2287
2288int ssl3_send_channel_id(SSL *s) {
2289 uint8_t *d;
2290 int ret = -1, public_key_len;
2291 EVP_MD_CTX md_ctx;
2292 size_t sig_len;
2293 ECDSA_SIG *sig = NULL;
2294 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2295
2296 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2297 return ssl_do_write(s);
2298 }
2299
2300 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2301 EVP_PKEY *key = NULL;
2302 s->ctx->channel_id_cb(s, &key);
2303 if (key != NULL) {
2304 s->tlsext_channel_id_private = key;
2305 }
2306 }
2307
2308 if (!s->tlsext_channel_id_private) {
2309 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2310 return -1;
2311 }
2312 s->rwstate = SSL_NOTHING;
2313
2314 d = ssl_handshake_start(s);
2315 if (s->s3->tlsext_channel_id_new) {
2316 s2n(TLSEXT_TYPE_channel_id_new, d);
2317 } else {
2318 s2n(TLSEXT_TYPE_channel_id, d);
2319 }
2320 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2321
2322 EVP_MD_CTX_init(&md_ctx);
2323
2324 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2325 if (public_key_len <= 0) {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2327 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2328 goto err;
2329 }
2330
2331 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2332 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2333 * field elements as 32-byte, big-endian numbers. */
2334 if (public_key_len != 65) {
2335 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2336 goto err;
2337 }
2338 public_key = OPENSSL_malloc(public_key_len);
2339 if (!public_key) {
2340 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2341 goto err;
2342 }
2343
2344 derp = public_key;
2345 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2346
2347 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2348 s->tlsext_channel_id_private) != 1) {
2349 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2350 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2351 goto err;
2352 }
2353
2354 if (!tls1_channel_id_hash(&md_ctx, s)) {
2355 goto err;
2356 }
2357
2358 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2359 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2360 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2361 goto err;
2362 }
2363
2364 der_sig = OPENSSL_malloc(sig_len);
2365 if (!der_sig) {
2366 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2367 goto err;
2368 }
2369
2370 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2371 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2372 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2373 goto err;
2374 }
2375
2376 derp = der_sig;
2377 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2378 if (sig == NULL) {
2379 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2380 goto err;
2381 }
2382
2383 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2384 memcpy(d, public_key + 1, 64);
2385 d += 64;
2386 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2387 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2388 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2389 goto err;
2390 }
2391
David Benjaminfbdfefb2015-02-16 19:33:53 -05002392 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2393 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2394 goto err;
2395 }
Adam Langley1bea1732014-12-17 19:06:57 -08002396 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2397
2398 ret = ssl_do_write(s);
2399
2400err:
2401 EVP_MD_CTX_cleanup(&md_ctx);
2402 if (public_key) {
2403 OPENSSL_free(public_key);
2404 }
2405 if (der_sig) {
2406 OPENSSL_free(der_sig);
2407 }
2408 if (sig) {
2409 ECDSA_SIG_free(sig);
2410 }
2411
2412 return ret;
2413}
2414
2415int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2416 int i = 0;
2417 if (s->ctx->client_cert_cb) {
2418 i = s->ctx->client_cert_cb(s, px509, ppkey);
2419 }
2420 return i;
2421}