blob: a6e76c978abbff08db0fe11027b97e10418cd757 [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
166#include "ssl_locl.h"
167#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 /* TLS does not like anon-DH with client cert */
1415 if (s->version > SSL3_VERSION &&
1416 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1417 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1418 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1419 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1420 goto err;
1421 }
Adam Langley95c29f32014-06-20 12:00:00 -07001422
Adam Langley1bea1732014-12-17 19:06:57 -08001423 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langley1bea1732014-12-17 19:06:57 -08001425 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1426 if (ca_sk == NULL) {
1427 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1428 goto err;
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langley1bea1732014-12-17 19:06:57 -08001431 /* get the certificate types */
1432 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1433 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1434 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1435 goto err;
1436 }
David Benjamined439582014-07-14 19:13:02 -04001437
Adam Langley1bea1732014-12-17 19:06:57 -08001438 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1439 &s->s3->tmp.num_certificate_types)) {
1440 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1441 goto err;
1442 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001443
Adam Langley1bea1732014-12-17 19:06:57 -08001444 if (SSL_USE_SIGALGS(s)) {
1445 CBS supported_signature_algorithms;
1446 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1447 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1448 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1449 goto err;
1450 }
David Benjamined439582014-07-14 19:13:02 -04001451
Adam Langley1bea1732014-12-17 19:06:57 -08001452 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1454 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1455 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1456 goto err;
1457 }
1458 }
David Benjamined439582014-07-14 19:13:02 -04001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 /* get the CA RDNs */
1461 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1462 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1463 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1464 goto err;
1465 }
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langley1bea1732014-12-17 19:06:57 -08001467 while (CBS_len(&certificate_authorities) > 0) {
1468 CBS distinguished_name;
1469 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1470 &distinguished_name)) {
1471 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1472 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1473 SSL_R_CA_DN_TOO_LONG);
1474 goto err;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langley1bea1732014-12-17 19:06:57 -08001477 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001478
Adam Langley1bea1732014-12-17 19:06:57 -08001479 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1480 if (xn == NULL) {
1481 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1482 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1483 goto err;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
Adam Langley1bea1732014-12-17 19:06:57 -08001486 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1487 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001488 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001489 goto err;
1490 }
Adam Langley95c29f32014-06-20 12:00:00 -07001491
Adam Langley1bea1732014-12-17 19:06:57 -08001492 if (CBS_len(&distinguished_name) != 0) {
1493 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1494 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1495 SSL_R_CA_DN_LENGTH_MISMATCH);
1496 goto err;
1497 }
Adam Langley95c29f32014-06-20 12:00:00 -07001498
Adam Langley1bea1732014-12-17 19:06:57 -08001499 if (!sk_X509_NAME_push(ca_sk, xn)) {
1500 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1501 ERR_R_MALLOC_FAILURE);
1502 goto err;
1503 }
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langley1bea1732014-12-17 19:06:57 -08001506 /* we should setup a certificate to return.... */
1507 s->s3->tmp.cert_req = 1;
1508 if (s->s3->tmp.ca_names != NULL) {
1509 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1510 }
1511 s->s3->tmp.ca_names = ca_sk;
1512 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langley1bea1732014-12-17 19:06:57 -08001514 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langley1bea1732014-12-17 19:06:57 -08001516err:
1517 if (ca_sk != NULL) {
1518 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1519 }
1520 return ret;
1521}
Adam Langley95c29f32014-06-20 12:00:00 -07001522
Adam Langley1bea1732014-12-17 19:06:57 -08001523int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001524 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001525 long n;
1526 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001527
Adam Langley1bea1732014-12-17 19:06:57 -08001528 n = s->method->ssl_get_message(
1529 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001530 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langley1bea1732014-12-17 19:06:57 -08001532 if (!ok) {
1533 return n;
1534 }
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langley1bea1732014-12-17 19:06:57 -08001536 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001537
Adam Langley1bea1732014-12-17 19:06:57 -08001538 if (!CBS_get_u32(&new_session_ticket,
1539 &s->session->tlsext_tick_lifetime_hint) ||
1540 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1541 CBS_len(&new_session_ticket) != 0) {
1542 al = SSL_AD_DECODE_ERROR;
1543 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1544 goto f_err;
1545 }
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langley1bea1732014-12-17 19:06:57 -08001547 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1548 &s->session->tlsext_ticklen)) {
1549 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1550 goto err;
1551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamin68070622015-02-08 23:44:59 -05001553 /* Generate a session ID for this session based on the session ticket. We use
1554 * the session ID mechanism for detecting ticket resumption. This also fits in
1555 * with assumptions elsewhere in OpenSSL.*/
1556 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1557 &s->session->session_id_length, EVP_sha256(), NULL)) {
1558 goto err;
1559 }
1560
1561 return 1;
David Benjamined439582014-07-14 19:13:02 -04001562
Adam Langley95c29f32014-06-20 12:00:00 -07001563f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001564 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001565err:
Adam Langley1bea1732014-12-17 19:06:57 -08001566 return -1;
1567}
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langley1bea1732014-12-17 19:06:57 -08001569int ssl3_get_cert_status(SSL *s) {
1570 int ok, al;
1571 long n;
1572 CBS certificate_status, ocsp_response;
1573 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001574
Adam Langley1bea1732014-12-17 19:06:57 -08001575 n = s->method->ssl_get_message(
1576 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001577 SSL3_MT_CERTIFICATE_STATUS, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langley1bea1732014-12-17 19:06:57 -08001579 if (!ok) {
1580 return n;
1581 }
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Adam Langley1bea1732014-12-17 19:06:57 -08001583 CBS_init(&certificate_status, s->init_msg, n);
1584 if (!CBS_get_u8(&certificate_status, &status_type) ||
1585 status_type != TLSEXT_STATUSTYPE_ocsp ||
1586 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1587 CBS_len(&ocsp_response) == 0 ||
1588 CBS_len(&certificate_status) != 0) {
1589 al = SSL_AD_DECODE_ERROR;
1590 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1591 goto f_err;
1592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langley1bea1732014-12-17 19:06:57 -08001594 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1595 &s->session->ocsp_response_length)) {
1596 al = SSL_AD_INTERNAL_ERROR;
1597 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1598 goto f_err;
1599 }
1600 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langley95c29f32014-06-20 12:00:00 -07001602f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001603 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1604 return -1;
1605}
Adam Langley95c29f32014-06-20 12:00:00 -07001606
Adam Langley1bea1732014-12-17 19:06:57 -08001607int ssl3_get_server_done(SSL *s) {
1608 int ok;
1609 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001610
Adam Langley1bea1732014-12-17 19:06:57 -08001611 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1612 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1613 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001614 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langley1bea1732014-12-17 19:06:57 -08001616 if (!ok) {
1617 return n;
1618 }
David Benjamin46062682014-07-14 19:14:32 -04001619
Adam Langley1bea1732014-12-17 19:06:57 -08001620 if (n > 0) {
1621 /* should contain no data */
1622 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1623 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1624 return -1;
1625 }
David Benjamin46062682014-07-14 19:14:32 -04001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001628}
Adam Langley1258b6a2014-06-20 12:00:00 -07001629
Adam Langley1258b6a2014-06-20 12:00:00 -07001630
Adam Langley1bea1732014-12-17 19:06:57 -08001631int ssl3_send_client_key_exchange(SSL *s) {
1632 uint8_t *p;
1633 int n = 0;
1634 unsigned long alg_k;
1635 unsigned long alg_a;
1636 uint8_t *q;
1637 EVP_PKEY *pkey = NULL;
1638 EC_KEY *clnt_ecdh = NULL;
1639 const EC_POINT *srvr_ecpoint = NULL;
1640 EVP_PKEY *srvr_pub_pkey = NULL;
1641 uint8_t *encodedPoint = NULL;
1642 int encoded_pt_len = 0;
1643 BN_CTX *bn_ctx = NULL;
1644 unsigned int psk_len = 0;
1645 uint8_t psk[PSK_MAX_PSK_LEN];
1646 uint8_t *pms = NULL;
1647 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001648
Adam Langley1bea1732014-12-17 19:06:57 -08001649 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1650 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001651
Adam Langley1bea1732014-12-17 19:06:57 -08001652 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1653 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001654
Adam Langley1bea1732014-12-17 19:06:57 -08001655 /* If using a PSK key exchange, prepare the pre-shared key. */
1656 if (alg_a & SSL_aPSK) {
1657 char identity[PSK_MAX_IDENTITY_LEN + 1];
1658 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001659
Adam Langley1bea1732014-12-17 19:06:57 -08001660 if (s->psk_client_callback == NULL) {
1661 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1662 SSL_R_PSK_NO_CLIENT_CB);
1663 goto err;
1664 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 memset(identity, 0, sizeof(identity));
1667 psk_len =
1668 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1669 sizeof(identity), psk, sizeof(psk));
1670 if (psk_len > PSK_MAX_PSK_LEN) {
1671 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1672 ERR_R_INTERNAL_ERROR);
1673 goto err;
1674 } else if (psk_len == 0) {
1675 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1676 SSL_R_PSK_IDENTITY_NOT_FOUND);
1677 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1678 goto err;
1679 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001680
Adam Langley1bea1732014-12-17 19:06:57 -08001681 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1682 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1683 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1684 ERR_R_INTERNAL_ERROR);
1685 goto err;
1686 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001687
Adam Langley1bea1732014-12-17 19:06:57 -08001688 if (s->session->psk_identity != NULL) {
1689 OPENSSL_free(s->session->psk_identity);
1690 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001691
Adam Langley1bea1732014-12-17 19:06:57 -08001692 s->session->psk_identity = BUF_strdup(identity);
1693 if (s->session->psk_identity == NULL) {
1694 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1695 ERR_R_MALLOC_FAILURE);
1696 goto err;
1697 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001698
Adam Langley1bea1732014-12-17 19:06:57 -08001699 /* Write out psk_identity. */
1700 s2n(identity_len, p);
1701 memcpy(p, identity, identity_len);
1702 p += identity_len;
1703 n = 2 + identity_len;
1704 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001705
Adam Langley1bea1732014-12-17 19:06:57 -08001706 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1707 if (alg_k & SSL_kRSA) {
1708 RSA *rsa;
1709 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001710
Adam Langley1bea1732014-12-17 19:06:57 -08001711 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1712 pms = OPENSSL_malloc(pms_len);
1713 if (pms == NULL) {
1714 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1715 ERR_R_MALLOC_FAILURE);
1716 goto err;
1717 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001718
Adam Langley1bea1732014-12-17 19:06:57 -08001719 if (s->session->sess_cert == NULL) {
1720 /* We should always have a server certificate with SSL_kRSA. */
1721 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1722 ERR_R_INTERNAL_ERROR);
1723 goto err;
1724 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001725
Adam Langley1bea1732014-12-17 19:06:57 -08001726 pkey = X509_get_pubkey(
1727 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1728 if (pkey == NULL ||
1729 pkey->type != EVP_PKEY_RSA ||
1730 pkey->pkey.rsa == NULL) {
1731 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1732 ERR_R_INTERNAL_ERROR);
1733 if (pkey != NULL) {
1734 EVP_PKEY_free(pkey);
1735 }
1736 goto err;
1737 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001738
Adam Langley1bea1732014-12-17 19:06:57 -08001739 rsa = pkey->pkey.rsa;
1740 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001741
Adam Langley1bea1732014-12-17 19:06:57 -08001742 pms[0] = s->client_version >> 8;
1743 pms[1] = s->client_version & 0xff;
1744 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1745 goto err;
1746 }
1747
1748 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1749
1750 q = p;
1751 /* In TLS and beyond, reserve space for the length prefix. */
1752 if (s->version > SSL3_VERSION) {
1753 p += 2;
1754 n += 2;
1755 }
1756 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1757 RSA_PKCS1_PADDING)) {
1758 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1759 SSL_R_BAD_RSA_ENCRYPT);
1760 goto err;
1761 }
1762 n += enc_pms_len;
1763
1764 /* Log the premaster secret, if logging is enabled. */
1765 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1766 pms_len)) {
1767 goto err;
1768 }
1769
1770 /* Fill in the length prefix. */
1771 if (s->version > SSL3_VERSION) {
1772 s2n(enc_pms_len, q);
1773 }
David Benjamin7061e282015-03-19 11:10:48 -04001774 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001775 DH *dh_srvr, *dh_clnt;
1776 SESS_CERT *scert = s->session->sess_cert;
1777 int dh_len;
1778 size_t pub_len;
1779
1780 if (scert == NULL) {
1781 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1782 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1783 SSL_R_UNEXPECTED_MESSAGE);
1784 goto err;
1785 }
1786
1787 if (scert->peer_dh_tmp == NULL) {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1789 ERR_R_INTERNAL_ERROR);
1790 goto err;
1791 }
1792 dh_srvr = scert->peer_dh_tmp;
1793
1794 /* generate a new random key */
1795 dh_clnt = DHparams_dup(dh_srvr);
1796 if (dh_clnt == NULL) {
1797 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1798 goto err;
1799 }
1800 if (!DH_generate_key(dh_clnt)) {
1801 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1802 DH_free(dh_clnt);
1803 goto err;
1804 }
1805
1806 pms_len = DH_size(dh_clnt);
1807 pms = OPENSSL_malloc(pms_len);
1808 if (pms == NULL) {
1809 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1810 ERR_R_MALLOC_FAILURE);
1811 DH_free(dh_clnt);
1812 goto err;
1813 }
1814
1815 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1816 if (dh_len <= 0) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1818 DH_free(dh_clnt);
1819 goto err;
1820 }
1821 pms_len = dh_len;
1822
1823 /* send off the data */
1824 pub_len = BN_num_bytes(dh_clnt->pub_key);
1825 s2n(pub_len, p);
1826 BN_bn2bin(dh_clnt->pub_key, p);
1827 n += 2 + pub_len;
1828
1829 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001830 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001831 const EC_GROUP *srvr_group = NULL;
1832 EC_KEY *tkey;
1833 int field_size = 0, ecdh_len;
1834
1835 if (s->session->sess_cert == NULL) {
1836 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1837 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1838 SSL_R_UNEXPECTED_MESSAGE);
1839 goto err;
1840 }
1841
1842 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1843 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1844 ERR_R_INTERNAL_ERROR);
1845 goto err;
1846 }
1847
1848 tkey = s->session->sess_cert->peer_ecdh_tmp;
1849
1850 srvr_group = EC_KEY_get0_group(tkey);
1851 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1852 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1853 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1854 ERR_R_INTERNAL_ERROR);
1855 goto err;
1856 }
1857
1858 clnt_ecdh = EC_KEY_new();
1859 if (clnt_ecdh == NULL) {
1860 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1861 ERR_R_MALLOC_FAILURE);
1862 goto err;
1863 }
1864
1865 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1866 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1867 goto err;
1868 }
1869
1870 /* Generate a new ECDH key pair */
1871 if (!EC_KEY_generate_key(clnt_ecdh)) {
1872 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1873 goto err;
1874 }
1875
1876 field_size = EC_GROUP_get_degree(srvr_group);
1877 if (field_size <= 0) {
1878 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1879 goto err;
1880 }
1881
1882 pms_len = (field_size + 7) / 8;
1883 pms = OPENSSL_malloc(pms_len);
1884 if (pms == NULL) {
1885 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1886 ERR_R_MALLOC_FAILURE);
1887 goto err;
1888 }
1889
1890 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1891 if (ecdh_len <= 0) {
1892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1893 goto err;
1894 }
1895 pms_len = ecdh_len;
1896
1897 /* First check the size of encoding and allocate memory accordingly. */
1898 encoded_pt_len =
1899 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1900 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1901
1902 encodedPoint =
1903 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1904 bn_ctx = BN_CTX_new();
1905 if (encodedPoint == NULL || bn_ctx == NULL) {
1906 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1907 ERR_R_MALLOC_FAILURE);
1908 goto err;
1909 }
1910
1911 /* Encode the public key */
1912 encoded_pt_len = EC_POINT_point2oct(
1913 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1914 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1915
1916 *p = encoded_pt_len; /* length of encoded point */
1917 /* Encoded point will be copied here */
1918 p += 1;
1919 n += 1;
1920 /* copy the point */
1921 memcpy(p, encodedPoint, encoded_pt_len);
1922 /* increment n to account for length field */
1923 n += encoded_pt_len;
1924
1925 /* Free allocated memory */
1926 BN_CTX_free(bn_ctx);
1927 bn_ctx = NULL;
1928 OPENSSL_free(encodedPoint);
1929 encodedPoint = NULL;
1930 EC_KEY_free(clnt_ecdh);
1931 clnt_ecdh = NULL;
1932 EVP_PKEY_free(srvr_pub_pkey);
1933 srvr_pub_pkey = NULL;
1934 } else if (alg_k & SSL_kPSK) {
1935 /* For plain PSK, other_secret is a block of 0s with the same length as
1936 * the pre-shared key. */
1937 pms_len = psk_len;
1938 pms = OPENSSL_malloc(pms_len);
1939 if (pms == NULL) {
1940 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1941 ERR_R_MALLOC_FAILURE);
1942 goto err;
1943 }
1944 memset(pms, 0, pms_len);
1945 } else {
1946 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1947 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1948 ERR_R_INTERNAL_ERROR);
1949 goto err;
1950 }
1951
1952 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1953 * key. */
1954 if (alg_a & SSL_aPSK) {
1955 CBB cbb, child;
1956 uint8_t *new_pms;
1957 size_t new_pms_len;
1958
1959 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1960 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1961 ERR_R_MALLOC_FAILURE);
1962 goto err;
1963 }
1964 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1965 !CBB_add_bytes(&child, pms, pms_len) ||
1966 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1967 !CBB_add_bytes(&child, psk, psk_len) ||
1968 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1969 CBB_cleanup(&cbb);
1970 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1971 ERR_R_INTERNAL_ERROR);
1972 goto err;
1973 }
1974 OPENSSL_cleanse(pms, pms_len);
1975 OPENSSL_free(pms);
1976 pms = new_pms;
1977 pms_len = new_pms_len;
1978 }
1979
1980 /* The message must be added to the finished hash before calculating the
1981 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001982 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1983 goto err;
1984 }
Adam Langley1bea1732014-12-17 19:06:57 -08001985 s->state = SSL3_ST_CW_KEY_EXCH_B;
1986
1987 s->session->master_key_length = s->enc_method->generate_master_secret(
1988 s, s->session->master_key, pms, pms_len);
1989 if (s->session->master_key_length == 0) {
1990 goto err;
1991 }
1992 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1993 OPENSSL_cleanse(pms, pms_len);
1994 OPENSSL_free(pms);
1995 }
1996
1997 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001998 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001999
2000err:
Adam Langley1bea1732014-12-17 19:06:57 -08002001 BN_CTX_free(bn_ctx);
2002 if (encodedPoint != NULL) {
2003 OPENSSL_free(encodedPoint);
2004 }
2005 if (clnt_ecdh != NULL) {
2006 EC_KEY_free(clnt_ecdh);
2007 }
2008 EVP_PKEY_free(srvr_pub_pkey);
2009 if (pms) {
2010 OPENSSL_cleanse(pms, pms_len);
2011 OPENSSL_free(pms);
2012 }
2013 return -1;
2014}
Adam Langley1258b6a2014-06-20 12:00:00 -07002015
Adam Langley1bea1732014-12-17 19:06:57 -08002016int ssl3_send_cert_verify(SSL *s) {
2017 uint8_t *buf, *p;
2018 const EVP_MD *md = NULL;
2019 uint8_t digest[EVP_MAX_MD_SIZE];
2020 size_t digest_length;
2021 EVP_PKEY *pkey;
2022 EVP_PKEY_CTX *pctx = NULL;
2023 size_t signature_length = 0;
2024 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langley1bea1732014-12-17 19:06:57 -08002026 buf = (uint8_t *)s->init_buf->data;
2027
2028 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2029 p = ssl_handshake_start(s);
2030 pkey = s->cert->key->privatekey;
2031
2032 /* Write out the digest type if needbe. */
2033 if (SSL_USE_SIGALGS(s)) {
2034 md = tls1_choose_signing_digest(s, pkey);
2035 if (!tls12_get_sigandhash(p, pkey, md)) {
2036 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2037 goto err;
2038 }
2039 p += 2;
2040 n += 2;
2041 }
2042
2043 /* Compute the digest. */
2044 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2045 goto err;
2046 }
2047
2048 /* The handshake buffer is no longer necessary. */
2049 if (s->s3->handshake_buffer &&
2050 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2051 goto err;
2052 }
2053
2054 /* Sign the digest. */
2055 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2056 if (pctx == NULL) {
2057 goto err;
2058 }
2059
2060 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2061 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2062 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2063 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2064 goto err;
2065 }
2066
2067 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2068 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2069 goto err;
2070 }
2071
2072 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2073 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2074 goto err;
2075 }
2076
2077 s2n(signature_length, p);
2078 n += signature_length + 2;
2079
David Benjaminfbdfefb2015-02-16 19:33:53 -05002080 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2081 goto err;
2082 }
Adam Langley1bea1732014-12-17 19:06:57 -08002083 s->state = SSL3_ST_CW_CERT_VRFY_B;
2084 }
2085
2086 EVP_PKEY_CTX_free(pctx);
2087 return ssl_do_write(s);
2088
2089err:
2090 EVP_PKEY_CTX_free(pctx);
2091 return -1;
2092}
2093
2094/* ssl3_has_client_certificate returns true if a client certificate is
2095 * configured. */
2096static int ssl3_has_client_certificate(SSL *s) {
2097 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2098}
2099
2100int ssl3_send_client_certificate(SSL *s) {
2101 X509 *x509 = NULL;
2102 EVP_PKEY *pkey = NULL;
2103 int i;
2104
2105 if (s->state == SSL3_ST_CW_CERT_A) {
2106 /* Let cert callback update client certificates if required */
2107 if (s->cert->cert_cb) {
2108 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2109 if (i < 0) {
2110 s->rwstate = SSL_X509_LOOKUP;
2111 return -1;
2112 }
2113 if (i == 0) {
2114 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2115 return 0;
2116 }
2117 s->rwstate = SSL_NOTHING;
2118 }
2119
2120 if (ssl3_has_client_certificate(s)) {
2121 s->state = SSL3_ST_CW_CERT_C;
2122 } else {
2123 s->state = SSL3_ST_CW_CERT_B;
2124 }
2125 }
2126
2127 /* We need to get a client cert */
2128 if (s->state == SSL3_ST_CW_CERT_B) {
2129 /* If we get an error, we need to:
2130 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2131 * We then get retried later */
2132 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2133 if (i < 0) {
2134 s->rwstate = SSL_X509_LOOKUP;
2135 return -1;
2136 }
2137 s->rwstate = SSL_NOTHING;
2138 if (i == 1 && pkey != NULL && x509 != NULL) {
2139 s->state = SSL3_ST_CW_CERT_B;
2140 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2141 i = 0;
2142 }
2143 } else if (i == 1) {
2144 i = 0;
2145 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2146 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2147 }
2148
2149 if (x509 != NULL) {
2150 X509_free(x509);
2151 }
2152 if (pkey != NULL) {
2153 EVP_PKEY_free(pkey);
2154 }
2155 if (i && !ssl3_has_client_certificate(s)) {
2156 i = 0;
2157 }
2158 if (i == 0) {
2159 if (s->version == SSL3_VERSION) {
2160 s->s3->tmp.cert_req = 0;
2161 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2162 return 1;
2163 } else {
2164 s->s3->tmp.cert_req = 2;
2165 }
2166 }
2167
2168 /* Ok, we have a cert */
2169 s->state = SSL3_ST_CW_CERT_C;
2170 }
2171
2172 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002173 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2174 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2175 return -1;
2176 }
Adam Langley1bea1732014-12-17 19:06:57 -08002177 }
2178
2179 /* SSL3_ST_CW_CERT_D */
2180 return ssl_do_write(s);
2181}
2182
2183#define has_bits(i, m) (((i) & (m)) == (m))
2184
2185int ssl3_check_cert_and_algorithm(SSL *s) {
2186 int i, idx;
2187 long alg_k, alg_a;
2188 EVP_PKEY *pkey = NULL;
2189 SESS_CERT *sc;
2190 DH *dh;
2191
2192 /* we don't have a certificate */
2193 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2194 return 1;
2195 }
2196
2197 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2198 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2199
2200 sc = s->session->sess_cert;
2201 if (sc == NULL) {
2202 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2203 goto err;
2204 }
2205
2206 dh = s->session->sess_cert->peer_dh_tmp;
2207
2208 /* This is the passed certificate */
2209
2210 idx = sc->peer_cert_type;
2211 if (idx == SSL_PKEY_ECC) {
2212 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2213 /* check failed */
2214 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2215 goto f_err;
2216 } else {
2217 return 1;
2218 }
2219 } else if (alg_a & SSL_aECDSA) {
2220 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2221 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2222 goto f_err;
2223 }
2224 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2225 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2226 EVP_PKEY_free(pkey);
2227
2228 /* Check that we have a certificate if we require one */
2229 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2230 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2231 SSL_R_MISSING_RSA_SIGNING_CERT);
2232 goto f_err;
2233 }
2234
2235 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2236 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2237 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2238 goto f_err;
2239 }
2240
David Benjamin7061e282015-03-19 11:10:48 -04002241 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002242 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2243 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2244 goto f_err;
2245 }
2246
2247 return 1;
2248
2249f_err:
2250 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2251err:
2252 return 0;
2253}
2254
2255int ssl3_send_next_proto(SSL *s) {
2256 unsigned int len, padding_len;
2257 uint8_t *d, *p;
2258
2259 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2260 len = s->next_proto_negotiated_len;
2261 padding_len = 32 - ((len + 2) % 32);
2262
2263 d = p = ssl_handshake_start(s);
2264 *(p++) = len;
2265 memcpy(p, s->next_proto_negotiated, len);
2266 p += len;
2267 *(p++) = padding_len;
2268 memset(p, 0, padding_len);
2269 p += padding_len;
2270
David Benjaminfbdfefb2015-02-16 19:33:53 -05002271 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2272 return -1;
2273 }
Adam Langley1bea1732014-12-17 19:06:57 -08002274 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2275 }
2276
2277 return ssl_do_write(s);
2278}
2279
2280int ssl3_send_channel_id(SSL *s) {
2281 uint8_t *d;
2282 int ret = -1, public_key_len;
2283 EVP_MD_CTX md_ctx;
2284 size_t sig_len;
2285 ECDSA_SIG *sig = NULL;
2286 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2287
2288 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2289 return ssl_do_write(s);
2290 }
2291
2292 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2293 EVP_PKEY *key = NULL;
2294 s->ctx->channel_id_cb(s, &key);
2295 if (key != NULL) {
2296 s->tlsext_channel_id_private = key;
2297 }
2298 }
2299
2300 if (!s->tlsext_channel_id_private) {
2301 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2302 return -1;
2303 }
2304 s->rwstate = SSL_NOTHING;
2305
2306 d = ssl_handshake_start(s);
2307 if (s->s3->tlsext_channel_id_new) {
2308 s2n(TLSEXT_TYPE_channel_id_new, d);
2309 } else {
2310 s2n(TLSEXT_TYPE_channel_id, d);
2311 }
2312 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2313
2314 EVP_MD_CTX_init(&md_ctx);
2315
2316 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2317 if (public_key_len <= 0) {
2318 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2319 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2320 goto err;
2321 }
2322
2323 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2324 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2325 * field elements as 32-byte, big-endian numbers. */
2326 if (public_key_len != 65) {
2327 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2328 goto err;
2329 }
2330 public_key = OPENSSL_malloc(public_key_len);
2331 if (!public_key) {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2333 goto err;
2334 }
2335
2336 derp = public_key;
2337 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2338
2339 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2340 s->tlsext_channel_id_private) != 1) {
2341 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2342 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2343 goto err;
2344 }
2345
2346 if (!tls1_channel_id_hash(&md_ctx, s)) {
2347 goto err;
2348 }
2349
2350 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2351 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2352 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2353 goto err;
2354 }
2355
2356 der_sig = OPENSSL_malloc(sig_len);
2357 if (!der_sig) {
2358 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2359 goto err;
2360 }
2361
2362 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2363 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2364 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2365 goto err;
2366 }
2367
2368 derp = der_sig;
2369 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2370 if (sig == NULL) {
2371 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2372 goto err;
2373 }
2374
2375 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2376 memcpy(d, public_key + 1, 64);
2377 d += 64;
2378 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2379 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2380 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2381 goto err;
2382 }
2383
David Benjaminfbdfefb2015-02-16 19:33:53 -05002384 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2385 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2386 goto err;
2387 }
Adam Langley1bea1732014-12-17 19:06:57 -08002388 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2389
2390 ret = ssl_do_write(s);
2391
2392err:
2393 EVP_MD_CTX_cleanup(&md_ctx);
2394 if (public_key) {
2395 OPENSSL_free(public_key);
2396 }
2397 if (der_sig) {
2398 OPENSSL_free(der_sig);
2399 }
2400 if (sig) {
2401 ECDSA_SIG_free(sig);
2402 }
2403
2404 return ret;
2405}
2406
2407int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2408 int i = 0;
2409 if (s->ctx->client_cert_cb) {
2410 i = s->ctx->client_cert_cb(s, px509, ppkey);
2411 }
2412 return i;
2413}