blob: 81e76c4cbb0b60a70772760b86ea6b2a29cd878a [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>
153
154#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/rand.h>
157#include <openssl/obj.h>
158#include <openssl/evp.h>
159#include <openssl/mem.h>
160#include <openssl/md5.h>
161#include <openssl/dh.h>
162#include <openssl/bn.h>
163#include <openssl/engine.h>
164#include <openssl/x509.h>
165
David Benjamin2ee94aa2015-04-07 22:38:30 -0400166#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include "../crypto/dh/internal.h"
168
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley1bea1732014-12-17 19:06:57 -0800170int ssl3_connect(SSL *s) {
171 BUF_MEM *buf = NULL;
172 void (*cb)(const SSL *ssl, int type, int val) = NULL;
173 int ret = -1;
174 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500175
Adam Langley1bea1732014-12-17 19:06:57 -0800176 assert(s->handshake_func == ssl3_connect);
177 assert(!s->server);
178 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langley1bea1732014-12-17 19:06:57 -0800180 ERR_clear_error();
181 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langley1bea1732014-12-17 19:06:57 -0800183 if (s->info_callback != NULL) {
184 cb = s->info_callback;
185 } else if (s->ctx->info_callback != NULL) {
186 cb = s->ctx->info_callback;
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langley1bea1732014-12-17 19:06:57 -0800189 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley1bea1732014-12-17 19:06:57 -0800191 for (;;) {
192 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley1bea1732014-12-17 19:06:57 -0800194 switch (s->state) {
195 case SSL_ST_RENEGOTIATE:
196 s->renegotiate = 1;
197 s->state = SSL_ST_CONNECT;
Adam Langley1bea1732014-12-17 19:06:57 -0800198 /* fallthrough */
199 case SSL_ST_CONNECT:
200 case SSL_ST_BEFORE | SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500201 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800202 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley1bea1732014-12-17 19:06:57 -0800205 if (s->init_buf == NULL) {
206 buf = BUF_MEM_new();
207 if (buf == NULL ||
208 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
209 ret = -1;
210 goto end;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
Adam Langley1bea1732014-12-17 19:06:57 -0800213 s->init_buf = buf;
214 buf = NULL;
215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley1bea1732014-12-17 19:06:57 -0800217 if (!ssl3_setup_buffers(s) ||
218 !ssl_init_wbio_buffer(s, 0)) {
219 ret = -1;
220 goto end;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley1bea1732014-12-17 19:06:57 -0800223 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langley1bea1732014-12-17 19:06:57 -0800225 if (!ssl3_init_finished_mac(s)) {
226 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
227 ret = -1;
228 goto end;
229 }
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langley1bea1732014-12-17 19:06:57 -0800231 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800232 s->init_num = 0;
233 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley1bea1732014-12-17 19:06:57 -0800235 case SSL3_ST_CW_CLNT_HELLO_A:
236 case SSL3_ST_CW_CLNT_HELLO_B:
237 s->shutdown = 0;
238 ret = ssl3_send_client_hello(s);
239 if (ret <= 0) {
240 goto end;
241 }
242 s->state = SSL3_ST_CR_SRVR_HELLO_A;
243 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley1bea1732014-12-17 19:06:57 -0800245 /* turn on buffering for the next lot of output */
246 if (s->bbio != s->wbio) {
247 s->wbio = BIO_push(s->bbio, s->wbio);
248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langley1bea1732014-12-17 19:06:57 -0800250 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langley1bea1732014-12-17 19:06:57 -0800252 case SSL3_ST_CR_SRVR_HELLO_A:
253 case SSL3_ST_CR_SRVR_HELLO_B:
254 ret = ssl3_get_server_hello(s);
255 if (ret <= 0) {
256 goto end;
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley1bea1732014-12-17 19:06:57 -0800259 if (s->hit) {
260 s->state = SSL3_ST_CR_CHANGE;
261 if (s->tlsext_ticket_expected) {
262 /* receive renewed session ticket */
263 s->state = SSL3_ST_CR_SESSION_TICKET_A;
264 }
265 } else {
266 s->state = SSL3_ST_CR_CERT_A;
267 }
268 s->init_num = 0;
269 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400270
Adam Langley1bea1732014-12-17 19:06:57 -0800271 case SSL3_ST_CR_CERT_A:
272 case SSL3_ST_CR_CERT_B:
273 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
274 ret = ssl3_get_server_certificate(s);
275 if (ret <= 0) {
276 goto end;
277 }
278 if (s->s3->tmp.certificate_status_expected) {
279 s->state = SSL3_ST_CR_CERT_STATUS_A;
280 } else {
281 s->state = SSL3_ST_CR_KEY_EXCH_A;
282 }
283 } else {
284 skip = 1;
285 s->state = SSL3_ST_CR_KEY_EXCH_A;
286 }
287 s->init_num = 0;
288 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley1bea1732014-12-17 19:06:57 -0800290 case SSL3_ST_CR_KEY_EXCH_A:
291 case SSL3_ST_CR_KEY_EXCH_B:
292 ret = ssl3_get_server_key_exchange(s);
293 if (ret <= 0) {
294 goto end;
295 }
296 s->state = SSL3_ST_CR_CERT_REQ_A;
297 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langley1bea1732014-12-17 19:06:57 -0800299 /* at this point we check that we have the
300 * required stuff from the server */
301 if (!ssl3_check_cert_and_algorithm(s)) {
302 ret = -1;
303 goto end;
304 }
305 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley1bea1732014-12-17 19:06:57 -0800307 case SSL3_ST_CR_CERT_REQ_A:
308 case SSL3_ST_CR_CERT_REQ_B:
309 ret = ssl3_get_certificate_request(s);
310 if (ret <= 0) {
311 goto end;
312 }
313 s->state = SSL3_ST_CR_SRVR_DONE_A;
314 s->init_num = 0;
315 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley1bea1732014-12-17 19:06:57 -0800317 case SSL3_ST_CR_SRVR_DONE_A:
318 case SSL3_ST_CR_SRVR_DONE_B:
319 ret = ssl3_get_server_done(s);
320 if (ret <= 0) {
321 goto end;
322 }
323 if (s->s3->tmp.cert_req) {
324 s->state = SSL3_ST_CW_CERT_A;
325 } else {
326 s->state = SSL3_ST_CW_KEY_EXCH_A;
327 }
328 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langley1bea1732014-12-17 19:06:57 -0800330 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langley1bea1732014-12-17 19:06:57 -0800332 case SSL3_ST_CW_CERT_A:
333 case SSL3_ST_CW_CERT_B:
334 case SSL3_ST_CW_CERT_C:
335 case SSL3_ST_CW_CERT_D:
336 ret = ssl3_send_client_certificate(s);
337 if (ret <= 0) {
338 goto end;
339 }
340 s->state = SSL3_ST_CW_KEY_EXCH_A;
341 s->init_num = 0;
342 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langley1bea1732014-12-17 19:06:57 -0800344 case SSL3_ST_CW_KEY_EXCH_A:
345 case SSL3_ST_CW_KEY_EXCH_B:
346 ret = ssl3_send_client_key_exchange(s);
347 if (ret <= 0) {
348 goto end;
349 }
350 /* For TLS, cert_req is set to 2, so a cert chain
351 * of nothing is sent, but no verify packet is sent */
352 if (s->s3->tmp.cert_req == 1) {
353 s->state = SSL3_ST_CW_CERT_VRFY_A;
354 } else {
355 s->state = SSL3_ST_CW_CHANGE_A;
356 s->s3->change_cipher_spec = 0;
357 }
Adam Langley95c29f32014-06-20 12:00:00 -0700358
Adam Langley1bea1732014-12-17 19:06:57 -0800359 s->init_num = 0;
360 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langley1bea1732014-12-17 19:06:57 -0800362 case SSL3_ST_CW_CERT_VRFY_A:
363 case SSL3_ST_CW_CERT_VRFY_B:
364 ret = ssl3_send_cert_verify(s);
365 if (ret <= 0) {
366 goto end;
367 }
368 s->state = SSL3_ST_CW_CHANGE_A;
369 s->init_num = 0;
370 s->s3->change_cipher_spec = 0;
371 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langley1bea1732014-12-17 19:06:57 -0800373 case SSL3_ST_CW_CHANGE_A:
374 case SSL3_ST_CW_CHANGE_B:
375 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
376 SSL3_ST_CW_CHANGE_B);
377 if (ret <= 0) {
378 goto end;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langley1bea1732014-12-17 19:06:57 -0800381 s->state = SSL3_ST_CW_FINISHED_A;
382 if (s->s3->tlsext_channel_id_valid) {
383 s->state = SSL3_ST_CW_CHANNEL_ID_A;
384 }
385 if (s->s3->next_proto_neg_seen) {
386 s->state = SSL3_ST_CW_NEXT_PROTO_A;
387 }
388 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley1bea1732014-12-17 19:06:57 -0800390 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400391 if (!s->enc_method->setup_key_block(s) ||
392 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800393 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
394 ret = -1;
395 goto end;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley1bea1732014-12-17 19:06:57 -0800398 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langley1bea1732014-12-17 19:06:57 -0800400 case SSL3_ST_CW_NEXT_PROTO_A:
401 case SSL3_ST_CW_NEXT_PROTO_B:
402 ret = ssl3_send_next_proto(s);
403 if (ret <= 0) {
404 goto end;
405 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700406
Adam Langley1bea1732014-12-17 19:06:57 -0800407 if (s->s3->tlsext_channel_id_valid) {
408 s->state = SSL3_ST_CW_CHANNEL_ID_A;
409 } else {
410 s->state = SSL3_ST_CW_FINISHED_A;
411 }
412 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langley1bea1732014-12-17 19:06:57 -0800414 case SSL3_ST_CW_CHANNEL_ID_A:
415 case SSL3_ST_CW_CHANNEL_ID_B:
416 ret = ssl3_send_channel_id(s);
417 if (ret <= 0) {
418 goto end;
419 }
420 s->state = SSL3_ST_CW_FINISHED_A;
421 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langley1bea1732014-12-17 19:06:57 -0800423 case SSL3_ST_CW_FINISHED_A:
424 case SSL3_ST_CW_FINISHED_B:
425 ret =
426 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
427 s->enc_method->client_finished_label,
428 s->enc_method->client_finished_label_len);
429 if (ret <= 0) {
430 goto end;
431 }
432 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700433
Adam Langley1bea1732014-12-17 19:06:57 -0800434 if (s->hit) {
435 s->s3->tmp.next_state = SSL_ST_OK;
436 } else {
437 /* This is a non-resumption handshake. If it involves ChannelID, then
438 * record the handshake hashes at this point in the session so that
439 * any resumption of this session with ChannelID can sign those
440 * hashes. */
441 if (s->s3->tlsext_channel_id_new) {
442 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500443 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800444 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500445 }
Adam Langley1bea1732014-12-17 19:06:57 -0800446 }
David Benjamined7c4752015-02-16 19:16:46 -0500447 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
448 ssl3_can_false_start(s) &&
449 /* No False Start on renegotiation (would complicate the state
450 * machine). */
Adam Langley1bea1732014-12-17 19:06:57 -0800451 s->s3->previous_server_finished_len == 0) {
David Benjamined7c4752015-02-16 19:16:46 -0500452 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800453 } else {
454 /* Allow NewSessionTicket if ticket expected */
455 if (s->tlsext_ticket_expected) {
456 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
457 } else {
458 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
459 }
460 }
461 }
462 s->init_num = 0;
463 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langley1bea1732014-12-17 19:06:57 -0800465 case SSL3_ST_CR_SESSION_TICKET_A:
466 case SSL3_ST_CR_SESSION_TICKET_B:
467 ret = ssl3_get_new_session_ticket(s);
468 if (ret <= 0) {
469 goto end;
470 }
471 s->state = SSL3_ST_CR_CHANGE;
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_CERT_STATUS_A:
476 case SSL3_ST_CR_CERT_STATUS_B:
477 ret = ssl3_get_cert_status(s);
478 if (ret <= 0) {
479 goto end;
480 }
481 s->state = SSL3_ST_CR_KEY_EXCH_A;
482 s->init_num = 0;
483 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400484
Adam Langley1bea1732014-12-17 19:06:57 -0800485 case SSL3_ST_CR_CHANGE:
486 /* At this point, the next message must be entirely behind a
487 * ChangeCipherSpec. */
488 if (!ssl3_expect_change_cipher_spec(s)) {
489 ret = -1;
490 goto end;
491 }
492 s->state = SSL3_ST_CR_FINISHED_A;
493 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langley1bea1732014-12-17 19:06:57 -0800495 case SSL3_ST_CR_FINISHED_A:
496 case SSL3_ST_CR_FINISHED_B:
497 ret =
498 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
499 if (ret <= 0) {
500 goto end;
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley1bea1732014-12-17 19:06:57 -0800503 if (s->hit) {
504 s->state = SSL3_ST_CW_CHANGE_A;
505 } else {
506 s->state = SSL_ST_OK;
507 }
508 s->init_num = 0;
509 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langley1bea1732014-12-17 19:06:57 -0800511 case SSL3_ST_CW_FLUSH:
512 s->rwstate = SSL_WRITING;
513 if (BIO_flush(s->wbio) <= 0) {
514 ret = -1;
515 goto end;
516 }
517 s->rwstate = SSL_NOTHING;
518 s->state = s->s3->tmp.next_state;
519 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700520
David Benjamined7c4752015-02-16 19:16:46 -0500521 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800522 /* Allow NewSessionTicket if ticket expected */
523 if (s->tlsext_ticket_expected) {
524 s->state = SSL3_ST_CR_SESSION_TICKET_A;
525 } else {
526 s->state = SSL3_ST_CR_CHANGE;
527 }
David Benjamined7c4752015-02-16 19:16:46 -0500528 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700529
Adam Langley1bea1732014-12-17 19:06:57 -0800530 ssl_free_wbio_buffer(s);
531 ret = 1;
532 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langley1bea1732014-12-17 19:06:57 -0800534 case SSL_ST_OK:
535 /* clean a few things up */
536 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langley1bea1732014-12-17 19:06:57 -0800538 if (s->init_buf != NULL) {
539 BUF_MEM_free(s->init_buf);
540 s->init_buf = NULL;
541 }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley1bea1732014-12-17 19:06:57 -0800543 /* Remove write buffering now. */
544 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langley1bea1732014-12-17 19:06:57 -0800546 s->init_num = 0;
547 s->renegotiate = 0;
548 s->new_session = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500549 s->s3->tmp.in_false_start = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langley1bea1732014-12-17 19:06:57 -0800551 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 ret = 1;
554 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langley1bea1732014-12-17 19:06:57 -0800556 if (cb != NULL) {
557 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langley1bea1732014-12-17 19:06:57 -0800560 goto end;
561
562 default:
563 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
564 ret = -1;
565 goto end;
566 }
567
568 if (!s->s3->tmp.reuse_message && !skip) {
569 if (cb != NULL && s->state != state) {
570 new_state = s->state;
571 s->state = state;
572 cb(s, SSL_CB_CONNECT_LOOP, 1);
573 s->state = new_state;
574 }
575 }
576 skip = 0;
577 }
578
Adam Langley95c29f32014-06-20 12:00:00 -0700579end:
Adam Langley1bea1732014-12-17 19:06:57 -0800580 s->in_handshake--;
581 if (buf != NULL) {
582 BUF_MEM_free(buf);
583 }
584 if (cb != NULL) {
585 cb(s, SSL_CB_CONNECT_EXIT, ret);
586 }
587 return ret;
588}
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langley1bea1732014-12-17 19:06:57 -0800590int ssl3_send_client_hello(SSL *s) {
591 uint8_t *buf, *p, *d;
592 int i;
593 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langley1bea1732014-12-17 19:06:57 -0800595 buf = (uint8_t *)s->init_buf->data;
596 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
597 if (!s->s3->have_version) {
598 uint16_t max_version = ssl3_get_max_client_version(s);
599 /* Disabling all versions is silly: return an error. */
600 if (max_version == 0) {
601 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
602 goto err;
603 }
604 s->version = max_version;
605 s->client_version = max_version;
606 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500607
Adam Langley1bea1732014-12-17 19:06:57 -0800608 /* If the configured session was created at a version higher than our
609 * maximum version, drop it. */
610 if (s->session &&
611 (s->session->session_id_length == 0 || s->session->not_resumable ||
612 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
613 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
614 SSL_set_session(s, NULL);
615 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500616
Adam Langley1bea1732014-12-17 19:06:57 -0800617 /* else use the pre-loaded session */
618 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langley1bea1732014-12-17 19:06:57 -0800620 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
621 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400622 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
623 !ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random))) {
624 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langley1bea1732014-12-17 19:06:57 -0800627 /* Do the message type and length last. Note: the final argument to
628 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
629 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langley1bea1732014-12-17 19:06:57 -0800631 /* version indicates the negotiated version: for example from an SSLv2/v3
632 * compatible client hello). The client_version field is the maximum
633 * version we permit and it is also used in RSA encrypted premaster
634 * secrets. Some servers can choke if we initially report a higher version
635 * then renegotiate to a lower one in the premaster secret. This didn't
636 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
637 * or later if the server only supports 1.0.
638 *
639 * Possible scenario with previous logic:
640 * 1. Client hello indicates TLS 1.2
641 * 2. Server hello says TLS 1.0
642 * 3. RSA encrypted premaster secret uses 1.2.
643 * 4. Handhaked proceeds using TLS 1.0.
644 * 5. Server sends hello request to renegotiate.
645 * 6. Client hello indicates TLS v1.0 as we now
646 * know that is maximum server supports.
647 * 7. Server chokes on RSA encrypted premaster secret
648 * containing version 1.0.
649 *
650 * For interoperability it should be OK to always use the maximum version
651 * we support in client hello and then rely on the checking of version to
652 * ensure the servers isn't being inconsistent: for example initially
653 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
654 * using client_version in client hello and not resetting it to the
655 * negotiated version. */
656 *(p++) = s->client_version >> 8;
657 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langley1bea1732014-12-17 19:06:57 -0800659 /* Random stuff */
660 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
661 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langley1bea1732014-12-17 19:06:57 -0800663 /* Session ID */
664 if (s->new_session || s->session == NULL) {
665 i = 0;
666 } else {
667 i = s->session->session_id_length;
668 }
669 *(p++) = i;
670 if (i != 0) {
671 if (i > (int)sizeof(s->session->session_id)) {
672 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
673 goto err;
674 }
675 memcpy(p, s->session->session_id, i);
676 p += i;
677 }
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langley1bea1732014-12-17 19:06:57 -0800679 /* cookie stuff for DTLS */
680 if (SSL_IS_DTLS(s)) {
681 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
682 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
683 goto err;
684 }
685 *(p++) = s->d1->cookie_len;
686 memcpy(p, s->d1->cookie, s->d1->cookie_len);
687 p += s->d1->cookie_len;
688 }
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langley1bea1732014-12-17 19:06:57 -0800690 /* Ciphers supported */
691 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
692 if (i == 0) {
693 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
694 SSL_R_NO_CIPHERS_AVAILABLE);
695 goto err;
696 }
697 s2n(i, p);
698 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langley1bea1732014-12-17 19:06:57 -0800700 /* COMPRESSION */
701 *(p++) = 1;
702 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700703
Adam Langley1bea1732014-12-17 19:06:57 -0800704 /* TLS extensions*/
705 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
706 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
707 goto err;
708 }
709
710 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
711 p - buf);
712 if (p == NULL) {
713 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
714 goto err;
715 }
716
717 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500718 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
719 goto err;
720 }
Adam Langley1bea1732014-12-17 19:06:57 -0800721 s->state = SSL3_ST_CW_CLNT_HELLO_B;
722 }
723
724 /* SSL3_ST_CW_CLNT_HELLO_B */
725 return ssl_do_write(s);
726
Adam Langley95c29f32014-06-20 12:00:00 -0700727err:
Adam Langley1bea1732014-12-17 19:06:57 -0800728 return -1;
729}
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langley1bea1732014-12-17 19:06:57 -0800731int ssl3_get_server_hello(SSL *s) {
732 STACK_OF(SSL_CIPHER) * sk;
733 const SSL_CIPHER *c;
734 CERT *ct = s->cert;
735 int al = SSL_AD_INTERNAL_ERROR, ok;
736 long n;
737 CBS server_hello, server_random, session_id;
738 uint16_t server_version, cipher_suite;
739 uint8_t compression_method;
740 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700741
Adam Langley1bea1732014-12-17 19:06:57 -0800742 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
743 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
744 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500745 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700746
Adam Langley1bea1732014-12-17 19:06:57 -0800747 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500748 uint32_t err = ERR_peek_error();
749 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
750 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
751 /* Add a dedicated error code to the queue for a handshake_failure alert
752 * in response to ClientHello. This matches NSS's client behavior and
753 * gives a better error on a (probable) failure to negotiate initial
754 * parameters. Note: this error code comes after the original one.
755 *
756 * See https://crbug.com/446505. */
757 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
758 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
759 }
Adam Langley1bea1732014-12-17 19:06:57 -0800760 return n;
761 }
Adam Langley95c29f32014-06-20 12:00:00 -0700762
Adam Langley1bea1732014-12-17 19:06:57 -0800763 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400764
Adam Langley1bea1732014-12-17 19:06:57 -0800765 if (!CBS_get_u16(&server_hello, &server_version) ||
766 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
767 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
768 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
769 !CBS_get_u16(&server_hello, &cipher_suite) ||
770 !CBS_get_u8(&server_hello, &compression_method)) {
771 al = SSL_AD_DECODE_ERROR;
772 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
773 goto f_err;
774 }
David Benjamina03d95d2014-07-12 19:49:07 -0400775
Adam Langley1bea1732014-12-17 19:06:57 -0800776 if (!s->s3->have_version) {
777 if (!ssl3_is_version_enabled(s, server_version)) {
778 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
779 s->version = server_version;
780 /* Mark the version as fixed so the record-layer version is not clamped
781 * to TLS 1.0. */
782 s->s3->have_version = 1;
783 al = SSL_AD_PROTOCOL_VERSION;
784 goto f_err;
785 }
786 s->version = server_version;
787 s->enc_method = ssl3_get_enc_method(server_version);
788 assert(s->enc_method != NULL);
789 /* At this point, the connection's version is known and s->version is
790 * fixed. Begin enforcing the record-layer version. */
791 s->s3->have_version = 1;
792 } else if (server_version != s->version) {
793 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
794 al = SSL_AD_PROTOCOL_VERSION;
795 goto f_err;
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langley1bea1732014-12-17 19:06:57 -0800798 /* Copy over the server random. */
799 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langley1bea1732014-12-17 19:06:57 -0800801 assert(s->session == NULL || s->session->session_id_length > 0);
802 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
803 s->session->session_id_length)) {
804 if (s->sid_ctx_length != s->session->sid_ctx_length ||
805 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
806 /* actually a client application bug */
807 al = SSL_AD_ILLEGAL_PARAMETER;
808 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
809 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
810 goto f_err;
811 }
812 s->hit = 1;
813 } else {
814 /* The session wasn't resumed. Create a fresh SSL_SESSION to
815 * fill out. */
816 s->hit = 0;
817 if (!ssl_get_new_session(s, 0)) {
818 goto f_err;
819 }
820 /* Note: session_id could be empty. */
821 s->session->session_id_length = CBS_len(&session_id);
822 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
823 }
David Benjamina03d95d2014-07-12 19:49:07 -0400824
Adam Langley1bea1732014-12-17 19:06:57 -0800825 c = ssl3_get_cipher_by_value(cipher_suite);
826 if (c == NULL) {
827 /* unknown cipher */
828 al = SSL_AD_ILLEGAL_PARAMETER;
829 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
830 SSL_R_UNKNOWN_CIPHER_RETURNED);
831 goto f_err;
832 }
833 /* ct->mask_ssl was computed from client capabilities. Now
834 * that the final version is known, compute a new mask_ssl. */
835 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
836 mask_ssl = SSL_TLSV1_2;
837 } else {
838 mask_ssl = 0;
839 }
840 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
841 * the server selected it, it's an error. */
842 if ((c->algorithm_ssl & mask_ssl) ||
843 (c->algorithm_mkey & ct->mask_k) ||
844 (c->algorithm_auth & ct->mask_a)) {
845 al = SSL_AD_ILLEGAL_PARAMETER;
846 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
847 goto f_err;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langley1bea1732014-12-17 19:06:57 -0800850 sk = ssl_get_ciphers_by_id(s);
851 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
852 /* we did not say we would use this cipher */
853 al = SSL_AD_ILLEGAL_PARAMETER;
854 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
855 goto f_err;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
David Benjaminece3de92015-03-16 18:02:20 -0400858 if (s->hit) {
859 if (s->session->cipher != c) {
860 al = SSL_AD_ILLEGAL_PARAMETER;
861 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
862 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
863 goto f_err;
864 }
865 if (s->session->ssl_version != s->version) {
866 al = SSL_AD_ILLEGAL_PARAMETER;
867 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
868 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
869 goto f_err;
870 }
Adam Langley1bea1732014-12-17 19:06:57 -0800871 }
872 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500873
Adam Langley1bea1732014-12-17 19:06:57 -0800874 /* Don't digest cached records if no sigalgs: we may need them for client
875 * authentication. */
876 if (!SSL_USE_SIGALGS(s) &&
877 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
878 goto f_err;
879 }
David Benjamina03d95d2014-07-12 19:49:07 -0400880
Adam Langley1bea1732014-12-17 19:06:57 -0800881 /* Only the NULL compression algorithm is supported. */
882 if (compression_method != 0) {
883 al = SSL_AD_ILLEGAL_PARAMETER;
884 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
885 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
886 goto f_err;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langley1bea1732014-12-17 19:06:57 -0800889 /* TLS extensions */
890 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
891 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
892 goto err;
893 }
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langley1bea1732014-12-17 19:06:57 -0800895 /* There should be nothing left over in the record. */
896 if (CBS_len(&server_hello) != 0) {
897 /* wrong packet length */
898 al = SSL_AD_DECODE_ERROR;
899 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
900 goto f_err;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langley1bea1732014-12-17 19:06:57 -0800903 return 1;
904
Adam Langley95c29f32014-06-20 12:00:00 -0700905f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800906 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700907err:
Adam Langley1bea1732014-12-17 19:06:57 -0800908 return -1;
909}
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langley1bea1732014-12-17 19:06:57 -0800911int ssl3_get_server_certificate(SSL *s) {
912 int al, i, ok, ret = -1;
913 unsigned long n;
914 X509 *x = NULL;
915 STACK_OF(X509) *sk = NULL;
916 SESS_CERT *sc;
917 EVP_PKEY *pkey = NULL;
918 CBS cbs, certificate_list;
919 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langley1bea1732014-12-17 19:06:57 -0800921 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
922 SSL3_MT_CERTIFICATE, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500923 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langley1bea1732014-12-17 19:06:57 -0800925 if (!ok) {
926 return n;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langley1bea1732014-12-17 19:06:57 -0800929 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langley1bea1732014-12-17 19:06:57 -0800931 sk = sk_X509_new_null();
932 if (sk == NULL) {
933 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
934 goto err;
935 }
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langley1bea1732014-12-17 19:06:57 -0800937 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
938 CBS_len(&cbs) != 0) {
939 al = SSL_AD_DECODE_ERROR;
940 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
941 goto f_err;
942 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400943
Adam Langley1bea1732014-12-17 19:06:57 -0800944 while (CBS_len(&certificate_list) > 0) {
945 CBS certificate;
946 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
947 al = SSL_AD_DECODE_ERROR;
948 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
949 SSL_R_CERT_LENGTH_MISMATCH);
950 goto f_err;
951 }
952 data = CBS_data(&certificate);
953 x = d2i_X509(NULL, &data, CBS_len(&certificate));
954 if (x == NULL) {
955 al = SSL_AD_BAD_CERTIFICATE;
956 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
957 goto f_err;
958 }
959 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
960 al = SSL_AD_DECODE_ERROR;
961 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
962 SSL_R_CERT_LENGTH_MISMATCH);
963 goto f_err;
964 }
965 if (!sk_X509_push(sk, x)) {
966 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
967 goto err;
968 }
969 x = NULL;
970 }
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langley1bea1732014-12-17 19:06:57 -0800972 i = ssl_verify_cert_chain(s, sk);
973 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
974 al = ssl_verify_alarm_type(s->verify_result);
975 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
976 SSL_R_CERTIFICATE_VERIFY_FAILED);
977 goto f_err;
978 }
979 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langley1bea1732014-12-17 19:06:57 -0800981 sc = ssl_sess_cert_new();
982 if (sc == NULL) {
983 goto err;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langley1bea1732014-12-17 19:06:57 -0800986 if (s->session->sess_cert) {
987 ssl_sess_cert_free(s->session->sess_cert);
988 }
989 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langley1bea1732014-12-17 19:06:57 -0800991 sc->cert_chain = sk;
992 /* Inconsistency alert: cert_chain does include the peer's certificate, which
993 * we don't include in s3_srvr.c */
994 x = sk_X509_value(sk, 0);
995 sk = NULL;
996 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langley1bea1732014-12-17 19:06:57 -0800998 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langley1bea1732014-12-17 19:06:57 -08001000 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1001 x = NULL;
1002 al = SSL3_AL_FATAL;
1003 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1004 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1005 goto f_err;
1006 }
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langley1bea1732014-12-17 19:06:57 -08001008 i = ssl_cert_type(pkey);
1009 if (i < 0) {
1010 x = NULL;
1011 al = SSL3_AL_FATAL;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1013 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1014 goto f_err;
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langley1bea1732014-12-17 19:06:57 -08001017 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1018 if (exp_idx >= 0 && i != exp_idx) {
1019 x = NULL;
1020 al = SSL_AD_ILLEGAL_PARAMETER;
1021 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1022 SSL_R_WRONG_CERTIFICATE_TYPE);
1023 goto f_err;
1024 }
1025 sc->peer_cert_type = i;
1026 /* Why would the following ever happen? We just created sc a couple of lines
1027 * ago. */
1028 if (sc->peer_pkeys[i].x509 != NULL) {
1029 X509_free(sc->peer_pkeys[i].x509);
1030 }
1031 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1032 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langley1bea1732014-12-17 19:06:57 -08001034 if (s->session->peer != NULL) {
1035 X509_free(s->session->peer);
1036 }
1037 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langley1bea1732014-12-17 19:06:57 -08001041 x = NULL;
1042 ret = 1;
1043
1044 if (0) {
1045 f_err:
1046 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1047 }
1048
1049err:
1050 EVP_PKEY_free(pkey);
1051 X509_free(x);
1052 sk_X509_pop_free(sk, X509_free);
1053 return ret;
1054}
1055
1056int ssl3_get_server_key_exchange(SSL *s) {
1057 EVP_MD_CTX md_ctx;
1058 int al, ok;
1059 long n, alg_k, alg_a;
1060 EVP_PKEY *pkey = NULL;
1061 const EVP_MD *md = NULL;
1062 RSA *rsa = NULL;
1063 DH *dh = NULL;
1064 EC_KEY *ecdh = NULL;
1065 BN_CTX *bn_ctx = NULL;
1066 EC_POINT *srvr_ecpoint = NULL;
1067 CBS server_key_exchange, server_key_exchange_orig, parameter;
1068
1069 /* use same message size as in ssl3_get_certificate_request() as
1070 * ServerKeyExchange message may be skipped */
1071 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1072 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001073 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001074 if (!ok) {
1075 return n;
1076 }
1077
1078 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1079 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1080 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1081 SSL_R_UNEXPECTED_MESSAGE);
1082 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1083 return -1;
1084 }
1085
1086 /* In plain PSK ciphersuite, ServerKeyExchange can be
1087 omitted if no identity hint is sent. Set session->sess_cert anyway to
1088 avoid problems later.*/
1089 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1090 /* PSK ciphersuites that also send a Certificate would have already
1091 * initialized |sess_cert|. */
1092 if (s->session->sess_cert == NULL) {
1093 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001094 if (s->session->sess_cert == NULL) {
1095 return -1;
1096 }
Adam Langley1bea1732014-12-17 19:06:57 -08001097 }
1098
1099 /* TODO(davidben): This should be reset in one place with the rest of the
1100 * handshake state. */
1101 if (s->s3->tmp.peer_psk_identity_hint) {
1102 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1103 s->s3->tmp.peer_psk_identity_hint = NULL;
1104 }
1105 }
1106 s->s3->tmp.reuse_message = 1;
1107 return 1;
1108 }
1109
1110 /* Retain a copy of the original CBS to compute the signature over. */
1111 CBS_init(&server_key_exchange, s->init_msg, n);
1112 server_key_exchange_orig = server_key_exchange;
1113
1114 if (s->session->sess_cert != NULL) {
1115 if (s->session->sess_cert->peer_dh_tmp) {
1116 DH_free(s->session->sess_cert->peer_dh_tmp);
1117 s->session->sess_cert->peer_dh_tmp = NULL;
1118 }
1119 if (s->session->sess_cert->peer_ecdh_tmp) {
1120 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1121 s->session->sess_cert->peer_ecdh_tmp = NULL;
1122 }
1123 } else {
1124 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001125 if (s->session->sess_cert == NULL) {
1126 return -1;
1127 }
Adam Langley1bea1732014-12-17 19:06:57 -08001128 }
1129
1130 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1131 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1132 EVP_MD_CTX_init(&md_ctx);
1133
1134 if (alg_a & SSL_aPSK) {
1135 CBS psk_identity_hint;
1136
1137 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1138 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1139 &psk_identity_hint)) {
1140 al = SSL_AD_DECODE_ERROR;
1141 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1142 goto f_err;
1143 }
1144
1145 /* Store PSK identity hint for later use, hint is used in
1146 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1147 * identity hint can be as long as the maximum length of a PSK identity.
1148 * Also do not allow NULL characters; identities are saved as C strings.
1149 *
1150 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1151 * a specific identity. */
1152 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1153 CBS_contains_zero_byte(&psk_identity_hint)) {
1154 al = SSL_AD_HANDSHAKE_FAILURE;
1155 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1156 SSL_R_DATA_LENGTH_TOO_LONG);
1157 goto f_err;
1158 }
1159
1160 /* Save the identity hint as a C string. */
1161 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1162 al = SSL_AD_INTERNAL_ERROR;
1163 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1164 ERR_R_MALLOC_FAILURE);
1165 goto f_err;
1166 }
1167 }
1168
David Benjamin7061e282015-03-19 11:10:48 -04001169 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001170 CBS dh_p, dh_g, dh_Ys;
1171
1172 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1173 CBS_len(&dh_p) == 0 ||
1174 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1175 CBS_len(&dh_g) == 0 ||
1176 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1177 CBS_len(&dh_Ys) == 0) {
1178 al = SSL_AD_DECODE_ERROR;
1179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1180 goto f_err;
1181 }
1182
1183 dh = DH_new();
1184 if (dh == NULL) {
1185 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1186 goto err;
1187 }
1188
1189 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1190 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1191 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1192 NULL) {
1193 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1194 goto err;
1195 }
1196
1197 if (DH_size(dh) < 512 / 8) {
1198 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1199 SSL_R_BAD_DH_P_LENGTH);
1200 goto err;
1201 }
1202
1203 if (alg_a & SSL_aRSA) {
1204 pkey = X509_get_pubkey(
1205 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1206 }
1207 /* else anonymous DH, so no certificate or pkey. */
1208
1209 s->session->sess_cert->peer_dh_tmp = dh;
1210 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001211 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001212 uint16_t curve_id;
1213 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001214 const EC_GROUP *group;
1215 CBS point;
1216
1217 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1218 * key. Check curve is one of our preferences, if not server has sent an
1219 * invalid curve. */
1220 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1221 al = SSL_AD_DECODE_ERROR;
1222 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1223 goto f_err;
1224 }
1225
1226 curve_nid = tls1_ec_curve_id2nid(curve_id);
1227 if (curve_nid == 0) {
1228 al = SSL_AD_INTERNAL_ERROR;
1229 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1230 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1231 goto f_err;
1232 }
1233
David Benjamin4f7783e2015-03-05 03:05:11 -05001234 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001235 if (ecdh == NULL) {
1236 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001237 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001238 goto err;
1239 }
1240
Adam Langley1bea1732014-12-17 19:06:57 -08001241 group = EC_KEY_get0_group(ecdh);
1242
1243 /* Next, get the encoded ECPoint */
1244 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1245 al = SSL_AD_DECODE_ERROR;
1246 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1247 goto f_err;
1248 }
1249
1250 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1251 ((bn_ctx = BN_CTX_new()) == NULL)) {
1252 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1253 ERR_R_MALLOC_FAILURE);
1254 goto err;
1255 }
1256
1257 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1258 CBS_len(&point), bn_ctx)) {
1259 al = SSL_AD_DECODE_ERROR;
1260 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1261 goto f_err;
1262 }
1263
1264 /* The ECC/TLS specification does not mention the use of DSA to sign
1265 * ECParameters in the server key exchange message. We do support RSA and
1266 * ECDSA. */
1267 if (alg_a & SSL_aRSA) {
1268 pkey = X509_get_pubkey(
1269 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1270 } else if (alg_a & SSL_aECDSA) {
1271 pkey =
1272 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1273 }
1274 /* else anonymous ECDH, so no certificate or pkey. */
1275 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1276 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1277 ecdh = NULL;
1278 BN_CTX_free(bn_ctx);
1279 bn_ctx = NULL;
1280 EC_POINT_free(srvr_ecpoint);
1281 srvr_ecpoint = NULL;
1282 } else if (!(alg_k & SSL_kPSK)) {
1283 al = SSL_AD_UNEXPECTED_MESSAGE;
1284 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1285 SSL_R_UNEXPECTED_MESSAGE);
1286 goto f_err;
1287 }
1288
1289 /* At this point, |server_key_exchange| contains the signature, if any, while
1290 * |server_key_exchange_orig| contains the entire message. From that, derive
1291 * a CBS containing just the parameter. */
1292 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1293 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1294
1295 /* if it was signed, check the signature */
1296 if (pkey != NULL) {
1297 CBS signature;
1298
1299 if (SSL_USE_SIGALGS(s)) {
1300 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1301 goto f_err;
1302 }
1303 } else if (pkey->type == EVP_PKEY_RSA) {
1304 md = EVP_md5_sha1();
1305 } else {
1306 md = EVP_sha1();
1307 }
1308
1309 /* The last field in |server_key_exchange| is the signature. */
1310 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1311 CBS_len(&server_key_exchange) != 0) {
1312 al = SSL_AD_DECODE_ERROR;
1313 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1314 goto f_err;
1315 }
1316
1317 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1318 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1319 SSL3_RANDOM_SIZE) ||
1320 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1321 SSL3_RANDOM_SIZE) ||
1322 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1323 CBS_len(&parameter)) ||
1324 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1325 CBS_len(&signature))) {
1326 /* bad signature */
1327 al = SSL_AD_DECRYPT_ERROR;
1328 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1329 goto f_err;
1330 }
1331 } else {
1332 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1333 /* Might be wrong key type, check it */
1334 if (ssl3_check_cert_and_algorithm(s)) {
1335 /* Otherwise this shouldn't happen */
1336 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1337 ERR_R_INTERNAL_ERROR);
1338 }
1339 goto err;
1340 }
1341 /* still data left over */
1342 if (CBS_len(&server_key_exchange) > 0) {
1343 al = SSL_AD_DECODE_ERROR;
1344 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1345 SSL_R_EXTRA_DATA_IN_MESSAGE);
1346 goto f_err;
1347 }
1348 }
1349 EVP_PKEY_free(pkey);
1350 EVP_MD_CTX_cleanup(&md_ctx);
1351 return 1;
1352
Adam Langley95c29f32014-06-20 12:00:00 -07001353f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001354 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001355err:
Adam Langley1bea1732014-12-17 19:06:57 -08001356 EVP_PKEY_free(pkey);
1357 if (rsa != NULL) {
1358 RSA_free(rsa);
1359 }
1360 if (dh != NULL) {
1361 DH_free(dh);
1362 }
1363 BN_CTX_free(bn_ctx);
1364 EC_POINT_free(srvr_ecpoint);
1365 if (ecdh != NULL) {
1366 EC_KEY_free(ecdh);
1367 }
1368 EVP_MD_CTX_cleanup(&md_ctx);
1369 return -1;
1370}
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langley1bea1732014-12-17 19:06:57 -08001372static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1373 return X509_NAME_cmp(*a, *b);
1374}
Adam Langley95c29f32014-06-20 12:00:00 -07001375
Adam Langley1bea1732014-12-17 19:06:57 -08001376int ssl3_get_certificate_request(SSL *s) {
1377 int ok, ret = 0;
1378 unsigned long n;
1379 X509_NAME *xn = NULL;
1380 STACK_OF(X509_NAME) *ca_sk = NULL;
1381 CBS cbs;
1382 CBS certificate_types;
1383 CBS certificate_authorities;
1384 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langley1bea1732014-12-17 19:06:57 -08001386 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1387 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001388 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001389
Adam Langley1bea1732014-12-17 19:06:57 -08001390 if (!ok) {
1391 return n;
1392 }
David Benjamin688d8df2014-11-02 23:06:42 -05001393
Adam Langley1bea1732014-12-17 19:06:57 -08001394 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langley1bea1732014-12-17 19:06:57 -08001396 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1397 s->s3->tmp.reuse_message = 1;
1398 /* If we get here we don't need any cached handshake records as we wont be
1399 * doing client auth. */
1400 if (s->s3->handshake_buffer &&
1401 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1402 goto err;
1403 }
1404 return 1;
1405 }
David Benjamined439582014-07-14 19:13:02 -04001406
Adam Langley1bea1732014-12-17 19:06:57 -08001407 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1408 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1409 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1410 SSL_R_WRONG_MESSAGE_TYPE);
1411 goto err;
1412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langley1bea1732014-12-17 19:06:57 -08001414 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001415
Adam Langley1bea1732014-12-17 19:06:57 -08001416 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1417 if (ca_sk == NULL) {
1418 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1419 goto err;
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
Adam Langley1bea1732014-12-17 19:06:57 -08001422 /* get the certificate types */
1423 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1424 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1425 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1426 goto err;
1427 }
David Benjamined439582014-07-14 19:13:02 -04001428
Adam Langley1bea1732014-12-17 19:06:57 -08001429 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1430 &s->s3->tmp.num_certificate_types)) {
1431 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1432 goto err;
1433 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001434
Adam Langley1bea1732014-12-17 19:06:57 -08001435 if (SSL_USE_SIGALGS(s)) {
1436 CBS supported_signature_algorithms;
1437 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1438 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1439 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1440 goto err;
1441 }
David Benjamined439582014-07-14 19:13:02 -04001442
Adam Langley1bea1732014-12-17 19:06:57 -08001443 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1444 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1445 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1446 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1447 goto err;
1448 }
1449 }
David Benjamined439582014-07-14 19:13:02 -04001450
Adam Langley1bea1732014-12-17 19:06:57 -08001451 /* get the CA RDNs */
1452 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1454 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1455 goto err;
1456 }
Adam Langley95c29f32014-06-20 12:00:00 -07001457
Adam Langley1bea1732014-12-17 19:06:57 -08001458 while (CBS_len(&certificate_authorities) > 0) {
1459 CBS distinguished_name;
1460 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1461 &distinguished_name)) {
1462 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1463 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1464 SSL_R_CA_DN_TOO_LONG);
1465 goto err;
1466 }
Adam Langley95c29f32014-06-20 12:00:00 -07001467
Adam Langley1bea1732014-12-17 19:06:57 -08001468 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001469
Adam Langley1bea1732014-12-17 19:06:57 -08001470 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1471 if (xn == NULL) {
1472 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1473 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1474 goto err;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langley1bea1732014-12-17 19:06:57 -08001477 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1478 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001479 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001480 goto err;
1481 }
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langley1bea1732014-12-17 19:06:57 -08001483 if (CBS_len(&distinguished_name) != 0) {
1484 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1485 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1486 SSL_R_CA_DN_LENGTH_MISMATCH);
1487 goto err;
1488 }
Adam Langley95c29f32014-06-20 12:00:00 -07001489
Adam Langley1bea1732014-12-17 19:06:57 -08001490 if (!sk_X509_NAME_push(ca_sk, xn)) {
1491 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1492 ERR_R_MALLOC_FAILURE);
1493 goto err;
1494 }
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langley1bea1732014-12-17 19:06:57 -08001497 /* we should setup a certificate to return.... */
1498 s->s3->tmp.cert_req = 1;
1499 if (s->s3->tmp.ca_names != NULL) {
1500 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1501 }
1502 s->s3->tmp.ca_names = ca_sk;
1503 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001504
Adam Langley1bea1732014-12-17 19:06:57 -08001505 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langley1bea1732014-12-17 19:06:57 -08001507err:
1508 if (ca_sk != NULL) {
1509 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1510 }
1511 return ret;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langley1bea1732014-12-17 19:06:57 -08001514int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001515 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001516 long n;
1517 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001518
Adam Langley1bea1732014-12-17 19:06:57 -08001519 n = s->method->ssl_get_message(
1520 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001521 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001522
Adam Langley1bea1732014-12-17 19:06:57 -08001523 if (!ok) {
1524 return n;
1525 }
Adam Langley95c29f32014-06-20 12:00:00 -07001526
Adam Langley1bea1732014-12-17 19:06:57 -08001527 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001528
Adam Langley1bea1732014-12-17 19:06:57 -08001529 if (!CBS_get_u32(&new_session_ticket,
1530 &s->session->tlsext_tick_lifetime_hint) ||
1531 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1532 CBS_len(&new_session_ticket) != 0) {
1533 al = SSL_AD_DECODE_ERROR;
1534 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1535 goto f_err;
1536 }
Adam Langley95c29f32014-06-20 12:00:00 -07001537
Adam Langley1bea1732014-12-17 19:06:57 -08001538 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1539 &s->session->tlsext_ticklen)) {
1540 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1541 goto err;
1542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
David Benjamin68070622015-02-08 23:44:59 -05001544 /* Generate a session ID for this session based on the session ticket. We use
1545 * the session ID mechanism for detecting ticket resumption. This also fits in
1546 * with assumptions elsewhere in OpenSSL.*/
1547 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1548 &s->session->session_id_length, EVP_sha256(), NULL)) {
1549 goto err;
1550 }
1551
1552 return 1;
David Benjamined439582014-07-14 19:13:02 -04001553
Adam Langley95c29f32014-06-20 12:00:00 -07001554f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001555 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001556err:
Adam Langley1bea1732014-12-17 19:06:57 -08001557 return -1;
1558}
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langley1bea1732014-12-17 19:06:57 -08001560int ssl3_get_cert_status(SSL *s) {
1561 int ok, al;
1562 long n;
1563 CBS certificate_status, ocsp_response;
1564 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Adam Langley1bea1732014-12-17 19:06:57 -08001566 n = s->method->ssl_get_message(
1567 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001568 SSL3_MT_CERTIFICATE_STATUS, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001569
Adam Langley1bea1732014-12-17 19:06:57 -08001570 if (!ok) {
1571 return n;
1572 }
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langley1bea1732014-12-17 19:06:57 -08001574 CBS_init(&certificate_status, s->init_msg, n);
1575 if (!CBS_get_u8(&certificate_status, &status_type) ||
1576 status_type != TLSEXT_STATUSTYPE_ocsp ||
1577 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1578 CBS_len(&ocsp_response) == 0 ||
1579 CBS_len(&certificate_status) != 0) {
1580 al = SSL_AD_DECODE_ERROR;
1581 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1582 goto f_err;
1583 }
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1586 &s->session->ocsp_response_length)) {
1587 al = SSL_AD_INTERNAL_ERROR;
1588 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1589 goto f_err;
1590 }
1591 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langley95c29f32014-06-20 12:00:00 -07001593f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001594 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1595 return -1;
1596}
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langley1bea1732014-12-17 19:06:57 -08001598int ssl3_get_server_done(SSL *s) {
1599 int ok;
1600 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langley1bea1732014-12-17 19:06:57 -08001602 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1603 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1604 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001605 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001606
Adam Langley1bea1732014-12-17 19:06:57 -08001607 if (!ok) {
1608 return n;
1609 }
David Benjamin46062682014-07-14 19:14:32 -04001610
Adam Langley1bea1732014-12-17 19:06:57 -08001611 if (n > 0) {
1612 /* should contain no data */
1613 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1614 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1615 return -1;
1616 }
David Benjamin46062682014-07-14 19:14:32 -04001617
Adam Langley1bea1732014-12-17 19:06:57 -08001618 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001619}
Adam Langley1258b6a2014-06-20 12:00:00 -07001620
Adam Langley1258b6a2014-06-20 12:00:00 -07001621
Adam Langley1bea1732014-12-17 19:06:57 -08001622int ssl3_send_client_key_exchange(SSL *s) {
1623 uint8_t *p;
1624 int n = 0;
1625 unsigned long alg_k;
1626 unsigned long alg_a;
1627 uint8_t *q;
1628 EVP_PKEY *pkey = NULL;
1629 EC_KEY *clnt_ecdh = NULL;
1630 const EC_POINT *srvr_ecpoint = NULL;
1631 EVP_PKEY *srvr_pub_pkey = NULL;
1632 uint8_t *encodedPoint = NULL;
1633 int encoded_pt_len = 0;
1634 BN_CTX *bn_ctx = NULL;
1635 unsigned int psk_len = 0;
1636 uint8_t psk[PSK_MAX_PSK_LEN];
1637 uint8_t *pms = NULL;
1638 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001639
Adam Langley1bea1732014-12-17 19:06:57 -08001640 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1641 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001642
Adam Langley1bea1732014-12-17 19:06:57 -08001643 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1644 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001645
Adam Langley1bea1732014-12-17 19:06:57 -08001646 /* If using a PSK key exchange, prepare the pre-shared key. */
1647 if (alg_a & SSL_aPSK) {
1648 char identity[PSK_MAX_IDENTITY_LEN + 1];
1649 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001650
Adam Langley1bea1732014-12-17 19:06:57 -08001651 if (s->psk_client_callback == NULL) {
1652 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1653 SSL_R_PSK_NO_CLIENT_CB);
1654 goto err;
1655 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001656
Adam Langley1bea1732014-12-17 19:06:57 -08001657 memset(identity, 0, sizeof(identity));
1658 psk_len =
1659 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1660 sizeof(identity), psk, sizeof(psk));
1661 if (psk_len > PSK_MAX_PSK_LEN) {
1662 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1663 ERR_R_INTERNAL_ERROR);
1664 goto err;
1665 } else if (psk_len == 0) {
1666 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1667 SSL_R_PSK_IDENTITY_NOT_FOUND);
1668 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1669 goto err;
1670 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001671
Adam Langley1bea1732014-12-17 19:06:57 -08001672 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1673 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1674 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1675 ERR_R_INTERNAL_ERROR);
1676 goto err;
1677 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001678
Adam Langley1bea1732014-12-17 19:06:57 -08001679 if (s->session->psk_identity != NULL) {
1680 OPENSSL_free(s->session->psk_identity);
1681 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001682
Adam Langley1bea1732014-12-17 19:06:57 -08001683 s->session->psk_identity = BUF_strdup(identity);
1684 if (s->session->psk_identity == NULL) {
1685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1686 ERR_R_MALLOC_FAILURE);
1687 goto err;
1688 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001689
Adam Langley1bea1732014-12-17 19:06:57 -08001690 /* Write out psk_identity. */
1691 s2n(identity_len, p);
1692 memcpy(p, identity, identity_len);
1693 p += identity_len;
1694 n = 2 + identity_len;
1695 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001696
Adam Langley1bea1732014-12-17 19:06:57 -08001697 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1698 if (alg_k & SSL_kRSA) {
1699 RSA *rsa;
1700 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001701
Adam Langley1bea1732014-12-17 19:06:57 -08001702 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1703 pms = OPENSSL_malloc(pms_len);
1704 if (pms == NULL) {
1705 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1706 ERR_R_MALLOC_FAILURE);
1707 goto err;
1708 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001709
Adam Langley1bea1732014-12-17 19:06:57 -08001710 if (s->session->sess_cert == NULL) {
1711 /* We should always have a server certificate with SSL_kRSA. */
1712 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1713 ERR_R_INTERNAL_ERROR);
1714 goto err;
1715 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001716
Adam Langley1bea1732014-12-17 19:06:57 -08001717 pkey = X509_get_pubkey(
1718 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1719 if (pkey == NULL ||
1720 pkey->type != EVP_PKEY_RSA ||
1721 pkey->pkey.rsa == NULL) {
1722 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1723 ERR_R_INTERNAL_ERROR);
1724 if (pkey != NULL) {
1725 EVP_PKEY_free(pkey);
1726 }
1727 goto err;
1728 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001729
Adam Langley1bea1732014-12-17 19:06:57 -08001730 rsa = pkey->pkey.rsa;
1731 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001732
Adam Langley1bea1732014-12-17 19:06:57 -08001733 pms[0] = s->client_version >> 8;
1734 pms[1] = s->client_version & 0xff;
1735 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1736 goto err;
1737 }
1738
1739 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1740
1741 q = p;
1742 /* In TLS and beyond, reserve space for the length prefix. */
1743 if (s->version > SSL3_VERSION) {
1744 p += 2;
1745 n += 2;
1746 }
1747 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1748 RSA_PKCS1_PADDING)) {
1749 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1750 SSL_R_BAD_RSA_ENCRYPT);
1751 goto err;
1752 }
1753 n += enc_pms_len;
1754
1755 /* Log the premaster secret, if logging is enabled. */
1756 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1757 pms_len)) {
1758 goto err;
1759 }
1760
1761 /* Fill in the length prefix. */
1762 if (s->version > SSL3_VERSION) {
1763 s2n(enc_pms_len, q);
1764 }
David Benjamin7061e282015-03-19 11:10:48 -04001765 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001766 DH *dh_srvr, *dh_clnt;
1767 SESS_CERT *scert = s->session->sess_cert;
1768 int dh_len;
1769 size_t pub_len;
1770
1771 if (scert == NULL) {
1772 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1773 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1774 SSL_R_UNEXPECTED_MESSAGE);
1775 goto err;
1776 }
1777
1778 if (scert->peer_dh_tmp == NULL) {
1779 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1780 ERR_R_INTERNAL_ERROR);
1781 goto err;
1782 }
1783 dh_srvr = scert->peer_dh_tmp;
1784
1785 /* generate a new random key */
1786 dh_clnt = DHparams_dup(dh_srvr);
1787 if (dh_clnt == NULL) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1789 goto err;
1790 }
1791 if (!DH_generate_key(dh_clnt)) {
1792 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1793 DH_free(dh_clnt);
1794 goto err;
1795 }
1796
1797 pms_len = DH_size(dh_clnt);
1798 pms = OPENSSL_malloc(pms_len);
1799 if (pms == NULL) {
1800 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1801 ERR_R_MALLOC_FAILURE);
1802 DH_free(dh_clnt);
1803 goto err;
1804 }
1805
1806 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1807 if (dh_len <= 0) {
1808 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1809 DH_free(dh_clnt);
1810 goto err;
1811 }
1812 pms_len = dh_len;
1813
1814 /* send off the data */
1815 pub_len = BN_num_bytes(dh_clnt->pub_key);
1816 s2n(pub_len, p);
1817 BN_bn2bin(dh_clnt->pub_key, p);
1818 n += 2 + pub_len;
1819
1820 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001821 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001822 const EC_GROUP *srvr_group = NULL;
1823 EC_KEY *tkey;
1824 int field_size = 0, ecdh_len;
1825
1826 if (s->session->sess_cert == NULL) {
1827 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1828 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1829 SSL_R_UNEXPECTED_MESSAGE);
1830 goto err;
1831 }
1832
1833 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1834 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1835 ERR_R_INTERNAL_ERROR);
1836 goto err;
1837 }
1838
1839 tkey = s->session->sess_cert->peer_ecdh_tmp;
1840
1841 srvr_group = EC_KEY_get0_group(tkey);
1842 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1843 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1844 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1845 ERR_R_INTERNAL_ERROR);
1846 goto err;
1847 }
1848
1849 clnt_ecdh = EC_KEY_new();
1850 if (clnt_ecdh == NULL) {
1851 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1852 ERR_R_MALLOC_FAILURE);
1853 goto err;
1854 }
1855
1856 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1857 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1858 goto err;
1859 }
1860
1861 /* Generate a new ECDH key pair */
1862 if (!EC_KEY_generate_key(clnt_ecdh)) {
1863 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1864 goto err;
1865 }
1866
1867 field_size = EC_GROUP_get_degree(srvr_group);
1868 if (field_size <= 0) {
1869 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1870 goto err;
1871 }
1872
1873 pms_len = (field_size + 7) / 8;
1874 pms = OPENSSL_malloc(pms_len);
1875 if (pms == NULL) {
1876 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1877 ERR_R_MALLOC_FAILURE);
1878 goto err;
1879 }
1880
1881 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1882 if (ecdh_len <= 0) {
1883 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1884 goto err;
1885 }
1886 pms_len = ecdh_len;
1887
1888 /* First check the size of encoding and allocate memory accordingly. */
1889 encoded_pt_len =
1890 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1891 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1892
1893 encodedPoint =
1894 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1895 bn_ctx = BN_CTX_new();
1896 if (encodedPoint == NULL || bn_ctx == NULL) {
1897 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1898 ERR_R_MALLOC_FAILURE);
1899 goto err;
1900 }
1901
1902 /* Encode the public key */
1903 encoded_pt_len = EC_POINT_point2oct(
1904 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1905 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1906
1907 *p = encoded_pt_len; /* length of encoded point */
1908 /* Encoded point will be copied here */
1909 p += 1;
1910 n += 1;
1911 /* copy the point */
1912 memcpy(p, encodedPoint, encoded_pt_len);
1913 /* increment n to account for length field */
1914 n += encoded_pt_len;
1915
1916 /* Free allocated memory */
1917 BN_CTX_free(bn_ctx);
1918 bn_ctx = NULL;
1919 OPENSSL_free(encodedPoint);
1920 encodedPoint = NULL;
1921 EC_KEY_free(clnt_ecdh);
1922 clnt_ecdh = NULL;
1923 EVP_PKEY_free(srvr_pub_pkey);
1924 srvr_pub_pkey = NULL;
1925 } else if (alg_k & SSL_kPSK) {
1926 /* For plain PSK, other_secret is a block of 0s with the same length as
1927 * the pre-shared key. */
1928 pms_len = psk_len;
1929 pms = OPENSSL_malloc(pms_len);
1930 if (pms == NULL) {
1931 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1932 ERR_R_MALLOC_FAILURE);
1933 goto err;
1934 }
1935 memset(pms, 0, pms_len);
1936 } else {
1937 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1938 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1939 ERR_R_INTERNAL_ERROR);
1940 goto err;
1941 }
1942
1943 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1944 * key. */
1945 if (alg_a & SSL_aPSK) {
1946 CBB cbb, child;
1947 uint8_t *new_pms;
1948 size_t new_pms_len;
1949
1950 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1951 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1952 ERR_R_MALLOC_FAILURE);
1953 goto err;
1954 }
1955 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1956 !CBB_add_bytes(&child, pms, pms_len) ||
1957 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1958 !CBB_add_bytes(&child, psk, psk_len) ||
1959 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1960 CBB_cleanup(&cbb);
1961 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1962 ERR_R_INTERNAL_ERROR);
1963 goto err;
1964 }
1965 OPENSSL_cleanse(pms, pms_len);
1966 OPENSSL_free(pms);
1967 pms = new_pms;
1968 pms_len = new_pms_len;
1969 }
1970
1971 /* The message must be added to the finished hash before calculating the
1972 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001973 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1974 goto err;
1975 }
Adam Langley1bea1732014-12-17 19:06:57 -08001976 s->state = SSL3_ST_CW_KEY_EXCH_B;
1977
1978 s->session->master_key_length = s->enc_method->generate_master_secret(
1979 s, s->session->master_key, pms, pms_len);
1980 if (s->session->master_key_length == 0) {
1981 goto err;
1982 }
1983 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1984 OPENSSL_cleanse(pms, pms_len);
1985 OPENSSL_free(pms);
1986 }
1987
1988 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001989 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001990
1991err:
Adam Langley1bea1732014-12-17 19:06:57 -08001992 BN_CTX_free(bn_ctx);
1993 if (encodedPoint != NULL) {
1994 OPENSSL_free(encodedPoint);
1995 }
1996 if (clnt_ecdh != NULL) {
1997 EC_KEY_free(clnt_ecdh);
1998 }
1999 EVP_PKEY_free(srvr_pub_pkey);
2000 if (pms) {
2001 OPENSSL_cleanse(pms, pms_len);
2002 OPENSSL_free(pms);
2003 }
2004 return -1;
2005}
Adam Langley1258b6a2014-06-20 12:00:00 -07002006
Adam Langley1bea1732014-12-17 19:06:57 -08002007int ssl3_send_cert_verify(SSL *s) {
2008 uint8_t *buf, *p;
2009 const EVP_MD *md = NULL;
2010 uint8_t digest[EVP_MAX_MD_SIZE];
2011 size_t digest_length;
2012 EVP_PKEY *pkey;
2013 EVP_PKEY_CTX *pctx = NULL;
2014 size_t signature_length = 0;
2015 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langley1bea1732014-12-17 19:06:57 -08002017 buf = (uint8_t *)s->init_buf->data;
2018
2019 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2020 p = ssl_handshake_start(s);
2021 pkey = s->cert->key->privatekey;
2022
2023 /* Write out the digest type if needbe. */
2024 if (SSL_USE_SIGALGS(s)) {
2025 md = tls1_choose_signing_digest(s, pkey);
2026 if (!tls12_get_sigandhash(p, pkey, md)) {
2027 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2028 goto err;
2029 }
2030 p += 2;
2031 n += 2;
2032 }
2033
2034 /* Compute the digest. */
2035 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2036 goto err;
2037 }
2038
2039 /* The handshake buffer is no longer necessary. */
2040 if (s->s3->handshake_buffer &&
2041 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2042 goto err;
2043 }
2044
2045 /* Sign the digest. */
2046 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2047 if (pctx == NULL) {
2048 goto err;
2049 }
2050
2051 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2052 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2053 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2054 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2055 goto err;
2056 }
2057
2058 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2059 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2060 goto err;
2061 }
2062
2063 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2064 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2065 goto err;
2066 }
2067
2068 s2n(signature_length, p);
2069 n += signature_length + 2;
2070
David Benjaminfbdfefb2015-02-16 19:33:53 -05002071 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2072 goto err;
2073 }
Adam Langley1bea1732014-12-17 19:06:57 -08002074 s->state = SSL3_ST_CW_CERT_VRFY_B;
2075 }
2076
2077 EVP_PKEY_CTX_free(pctx);
2078 return ssl_do_write(s);
2079
2080err:
2081 EVP_PKEY_CTX_free(pctx);
2082 return -1;
2083}
2084
2085/* ssl3_has_client_certificate returns true if a client certificate is
2086 * configured. */
2087static int ssl3_has_client_certificate(SSL *s) {
2088 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2089}
2090
2091int ssl3_send_client_certificate(SSL *s) {
2092 X509 *x509 = NULL;
2093 EVP_PKEY *pkey = NULL;
2094 int i;
2095
2096 if (s->state == SSL3_ST_CW_CERT_A) {
2097 /* Let cert callback update client certificates if required */
2098 if (s->cert->cert_cb) {
2099 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2100 if (i < 0) {
2101 s->rwstate = SSL_X509_LOOKUP;
2102 return -1;
2103 }
2104 if (i == 0) {
2105 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2106 return 0;
2107 }
2108 s->rwstate = SSL_NOTHING;
2109 }
2110
2111 if (ssl3_has_client_certificate(s)) {
2112 s->state = SSL3_ST_CW_CERT_C;
2113 } else {
2114 s->state = SSL3_ST_CW_CERT_B;
2115 }
2116 }
2117
2118 /* We need to get a client cert */
2119 if (s->state == SSL3_ST_CW_CERT_B) {
2120 /* If we get an error, we need to:
2121 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2122 * We then get retried later */
2123 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2124 if (i < 0) {
2125 s->rwstate = SSL_X509_LOOKUP;
2126 return -1;
2127 }
2128 s->rwstate = SSL_NOTHING;
2129 if (i == 1 && pkey != NULL && x509 != NULL) {
2130 s->state = SSL3_ST_CW_CERT_B;
2131 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2132 i = 0;
2133 }
2134 } else if (i == 1) {
2135 i = 0;
2136 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2137 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2138 }
2139
2140 if (x509 != NULL) {
2141 X509_free(x509);
2142 }
2143 if (pkey != NULL) {
2144 EVP_PKEY_free(pkey);
2145 }
2146 if (i && !ssl3_has_client_certificate(s)) {
2147 i = 0;
2148 }
2149 if (i == 0) {
2150 if (s->version == SSL3_VERSION) {
2151 s->s3->tmp.cert_req = 0;
2152 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2153 return 1;
2154 } else {
2155 s->s3->tmp.cert_req = 2;
2156 }
2157 }
2158
2159 /* Ok, we have a cert */
2160 s->state = SSL3_ST_CW_CERT_C;
2161 }
2162
2163 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002164 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2165 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2166 return -1;
2167 }
Adam Langley1bea1732014-12-17 19:06:57 -08002168 }
2169
2170 /* SSL3_ST_CW_CERT_D */
2171 return ssl_do_write(s);
2172}
2173
2174#define has_bits(i, m) (((i) & (m)) == (m))
2175
2176int ssl3_check_cert_and_algorithm(SSL *s) {
2177 int i, idx;
2178 long alg_k, alg_a;
2179 EVP_PKEY *pkey = NULL;
2180 SESS_CERT *sc;
2181 DH *dh;
2182
2183 /* we don't have a certificate */
2184 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2185 return 1;
2186 }
2187
2188 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2189 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2190
2191 sc = s->session->sess_cert;
2192 if (sc == NULL) {
2193 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2194 goto err;
2195 }
2196
2197 dh = s->session->sess_cert->peer_dh_tmp;
2198
2199 /* This is the passed certificate */
2200
2201 idx = sc->peer_cert_type;
2202 if (idx == SSL_PKEY_ECC) {
2203 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2204 /* check failed */
2205 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2206 goto f_err;
2207 } else {
2208 return 1;
2209 }
2210 } else if (alg_a & SSL_aECDSA) {
2211 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2212 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2213 goto f_err;
2214 }
2215 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2216 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2217 EVP_PKEY_free(pkey);
2218
2219 /* Check that we have a certificate if we require one */
2220 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2221 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2222 SSL_R_MISSING_RSA_SIGNING_CERT);
2223 goto f_err;
2224 }
2225
2226 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2227 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2228 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2229 goto f_err;
2230 }
2231
David Benjamin7061e282015-03-19 11:10:48 -04002232 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002233 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2234 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2235 goto f_err;
2236 }
2237
2238 return 1;
2239
2240f_err:
2241 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2242err:
2243 return 0;
2244}
2245
2246int ssl3_send_next_proto(SSL *s) {
2247 unsigned int len, padding_len;
2248 uint8_t *d, *p;
2249
2250 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2251 len = s->next_proto_negotiated_len;
2252 padding_len = 32 - ((len + 2) % 32);
2253
2254 d = p = ssl_handshake_start(s);
2255 *(p++) = len;
2256 memcpy(p, s->next_proto_negotiated, len);
2257 p += len;
2258 *(p++) = padding_len;
2259 memset(p, 0, padding_len);
2260 p += padding_len;
2261
David Benjaminfbdfefb2015-02-16 19:33:53 -05002262 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2263 return -1;
2264 }
Adam Langley1bea1732014-12-17 19:06:57 -08002265 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2266 }
2267
2268 return ssl_do_write(s);
2269}
2270
2271int ssl3_send_channel_id(SSL *s) {
2272 uint8_t *d;
2273 int ret = -1, public_key_len;
2274 EVP_MD_CTX md_ctx;
2275 size_t sig_len;
2276 ECDSA_SIG *sig = NULL;
2277 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2278
2279 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2280 return ssl_do_write(s);
2281 }
2282
2283 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2284 EVP_PKEY *key = NULL;
2285 s->ctx->channel_id_cb(s, &key);
2286 if (key != NULL) {
2287 s->tlsext_channel_id_private = key;
2288 }
2289 }
2290
2291 if (!s->tlsext_channel_id_private) {
2292 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2293 return -1;
2294 }
2295 s->rwstate = SSL_NOTHING;
2296
2297 d = ssl_handshake_start(s);
2298 if (s->s3->tlsext_channel_id_new) {
2299 s2n(TLSEXT_TYPE_channel_id_new, d);
2300 } else {
2301 s2n(TLSEXT_TYPE_channel_id, d);
2302 }
2303 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2304
2305 EVP_MD_CTX_init(&md_ctx);
2306
2307 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2308 if (public_key_len <= 0) {
2309 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2310 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2311 goto err;
2312 }
2313
2314 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2315 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2316 * field elements as 32-byte, big-endian numbers. */
2317 if (public_key_len != 65) {
2318 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2319 goto err;
2320 }
2321 public_key = OPENSSL_malloc(public_key_len);
2322 if (!public_key) {
2323 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2324 goto err;
2325 }
2326
2327 derp = public_key;
2328 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2329
2330 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2331 s->tlsext_channel_id_private) != 1) {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2333 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2334 goto err;
2335 }
2336
2337 if (!tls1_channel_id_hash(&md_ctx, s)) {
2338 goto err;
2339 }
2340
2341 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2342 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2343 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2344 goto err;
2345 }
2346
2347 der_sig = OPENSSL_malloc(sig_len);
2348 if (!der_sig) {
2349 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2350 goto err;
2351 }
2352
2353 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2354 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2355 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2356 goto err;
2357 }
2358
2359 derp = der_sig;
2360 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2361 if (sig == NULL) {
2362 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2363 goto err;
2364 }
2365
2366 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2367 memcpy(d, public_key + 1, 64);
2368 d += 64;
2369 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2370 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2371 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2372 goto err;
2373 }
2374
David Benjaminfbdfefb2015-02-16 19:33:53 -05002375 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2376 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2377 goto err;
2378 }
Adam Langley1bea1732014-12-17 19:06:57 -08002379 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2380
2381 ret = ssl_do_write(s);
2382
2383err:
2384 EVP_MD_CTX_cleanup(&md_ctx);
2385 if (public_key) {
2386 OPENSSL_free(public_key);
2387 }
2388 if (der_sig) {
2389 OPENSSL_free(der_sig);
2390 }
2391 if (sig) {
2392 ECDSA_SIG_free(sig);
2393 }
2394
2395 return ret;
2396}
2397
2398int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2399 int i = 0;
2400 if (s->ctx->client_cert_cb) {
2401 i = s->ctx->client_cert_cb(s, px509, ppkey);
2402 }
2403 return i;
2404}