blob: 231cc658278fbcbd60feb2a984de045944f31c31 [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;
198 s->ctx->stats.sess_connect_renegotiate++;
199 /* fallthrough */
200 case SSL_ST_CONNECT:
201 case SSL_ST_BEFORE | SSL_ST_CONNECT:
202 if (cb != NULL)
203 cb(s, SSL_CB_HANDSHAKE_START, 1);
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;
232 s->ctx->stats.sess_connect++;
233 s->init_num = 0;
234 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700235
Adam Langley1bea1732014-12-17 19:06:57 -0800236 case SSL3_ST_CW_CLNT_HELLO_A:
237 case SSL3_ST_CW_CLNT_HELLO_B:
238 s->shutdown = 0;
239 ret = ssl3_send_client_hello(s);
240 if (ret <= 0) {
241 goto end;
242 }
243 s->state = SSL3_ST_CR_SRVR_HELLO_A;
244 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langley1bea1732014-12-17 19:06:57 -0800246 /* turn on buffering for the next lot of output */
247 if (s->bbio != s->wbio) {
248 s->wbio = BIO_push(s->bbio, s->wbio);
249 }
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley1bea1732014-12-17 19:06:57 -0800251 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langley1bea1732014-12-17 19:06:57 -0800253 case SSL3_ST_CR_SRVR_HELLO_A:
254 case SSL3_ST_CR_SRVR_HELLO_B:
255 ret = ssl3_get_server_hello(s);
256 if (ret <= 0) {
257 goto end;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley1bea1732014-12-17 19:06:57 -0800260 if (s->hit) {
261 s->state = SSL3_ST_CR_CHANGE;
262 if (s->tlsext_ticket_expected) {
263 /* receive renewed session ticket */
264 s->state = SSL3_ST_CR_SESSION_TICKET_A;
265 }
266 } else {
267 s->state = SSL3_ST_CR_CERT_A;
268 }
269 s->init_num = 0;
270 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400271
Adam Langley1bea1732014-12-17 19:06:57 -0800272 case SSL3_ST_CR_CERT_A:
273 case SSL3_ST_CR_CERT_B:
274 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
275 ret = ssl3_get_server_certificate(s);
276 if (ret <= 0) {
277 goto end;
278 }
279 if (s->s3->tmp.certificate_status_expected) {
280 s->state = SSL3_ST_CR_CERT_STATUS_A;
281 } else {
282 s->state = SSL3_ST_CR_KEY_EXCH_A;
283 }
284 } else {
285 skip = 1;
286 s->state = SSL3_ST_CR_KEY_EXCH_A;
287 }
288 s->init_num = 0;
289 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700290
Adam Langley1bea1732014-12-17 19:06:57 -0800291 case SSL3_ST_CR_KEY_EXCH_A:
292 case SSL3_ST_CR_KEY_EXCH_B:
293 ret = ssl3_get_server_key_exchange(s);
294 if (ret <= 0) {
295 goto end;
296 }
297 s->state = SSL3_ST_CR_CERT_REQ_A;
298 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley1bea1732014-12-17 19:06:57 -0800300 /* at this point we check that we have the
301 * required stuff from the server */
302 if (!ssl3_check_cert_and_algorithm(s)) {
303 ret = -1;
304 goto end;
305 }
306 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley1bea1732014-12-17 19:06:57 -0800308 case SSL3_ST_CR_CERT_REQ_A:
309 case SSL3_ST_CR_CERT_REQ_B:
310 ret = ssl3_get_certificate_request(s);
311 if (ret <= 0) {
312 goto end;
313 }
314 s->state = SSL3_ST_CR_SRVR_DONE_A;
315 s->init_num = 0;
316 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley1bea1732014-12-17 19:06:57 -0800318 case SSL3_ST_CR_SRVR_DONE_A:
319 case SSL3_ST_CR_SRVR_DONE_B:
320 ret = ssl3_get_server_done(s);
321 if (ret <= 0) {
322 goto end;
323 }
324 if (s->s3->tmp.cert_req) {
325 s->state = SSL3_ST_CW_CERT_A;
326 } else {
327 s->state = SSL3_ST_CW_KEY_EXCH_A;
328 }
329 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley1bea1732014-12-17 19:06:57 -0800331 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley1bea1732014-12-17 19:06:57 -0800333 case SSL3_ST_CW_CERT_A:
334 case SSL3_ST_CW_CERT_B:
335 case SSL3_ST_CW_CERT_C:
336 case SSL3_ST_CW_CERT_D:
337 ret = ssl3_send_client_certificate(s);
338 if (ret <= 0) {
339 goto end;
340 }
341 s->state = SSL3_ST_CW_KEY_EXCH_A;
342 s->init_num = 0;
343 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langley1bea1732014-12-17 19:06:57 -0800345 case SSL3_ST_CW_KEY_EXCH_A:
346 case SSL3_ST_CW_KEY_EXCH_B:
347 ret = ssl3_send_client_key_exchange(s);
348 if (ret <= 0) {
349 goto end;
350 }
351 /* For TLS, cert_req is set to 2, so a cert chain
352 * of nothing is sent, but no verify packet is sent */
353 if (s->s3->tmp.cert_req == 1) {
354 s->state = SSL3_ST_CW_CERT_VRFY_A;
355 } else {
356 s->state = SSL3_ST_CW_CHANGE_A;
357 s->s3->change_cipher_spec = 0;
358 }
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley1bea1732014-12-17 19:06:57 -0800360 s->init_num = 0;
361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langley1bea1732014-12-17 19:06:57 -0800363 case SSL3_ST_CW_CERT_VRFY_A:
364 case SSL3_ST_CW_CERT_VRFY_B:
365 ret = ssl3_send_cert_verify(s);
366 if (ret <= 0) {
367 goto end;
368 }
369 s->state = SSL3_ST_CW_CHANGE_A;
370 s->init_num = 0;
371 s->s3->change_cipher_spec = 0;
372 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley1bea1732014-12-17 19:06:57 -0800374 case SSL3_ST_CW_CHANGE_A:
375 case SSL3_ST_CW_CHANGE_B:
376 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
377 SSL3_ST_CW_CHANGE_B);
378 if (ret <= 0) {
379 goto end;
380 }
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley1bea1732014-12-17 19:06:57 -0800382 s->state = SSL3_ST_CW_FINISHED_A;
383 if (s->s3->tlsext_channel_id_valid) {
384 s->state = SSL3_ST_CW_CHANNEL_ID_A;
385 }
386 if (s->s3->next_proto_neg_seen) {
387 s->state = SSL3_ST_CW_NEXT_PROTO_A;
388 }
389 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langley1bea1732014-12-17 19:06:57 -0800391 s->session->cipher = s->s3->tmp.new_cipher;
392 if (!s->enc_method->setup_key_block(s)) {
393 ret = -1;
394 goto end;
395 }
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langley1bea1732014-12-17 19:06:57 -0800397 if (!s->enc_method->change_cipher_state(
398 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
399 ret = -1;
400 goto end;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langley1bea1732014-12-17 19:06:57 -0800403 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langley1bea1732014-12-17 19:06:57 -0800405 case SSL3_ST_CW_NEXT_PROTO_A:
406 case SSL3_ST_CW_NEXT_PROTO_B:
407 ret = ssl3_send_next_proto(s);
408 if (ret <= 0) {
409 goto end;
410 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700411
Adam Langley1bea1732014-12-17 19:06:57 -0800412 if (s->s3->tlsext_channel_id_valid) {
413 s->state = SSL3_ST_CW_CHANNEL_ID_A;
414 } else {
415 s->state = SSL3_ST_CW_FINISHED_A;
416 }
417 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langley1bea1732014-12-17 19:06:57 -0800419 case SSL3_ST_CW_CHANNEL_ID_A:
420 case SSL3_ST_CW_CHANNEL_ID_B:
421 ret = ssl3_send_channel_id(s);
422 if (ret <= 0) {
423 goto end;
424 }
425 s->state = SSL3_ST_CW_FINISHED_A;
426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langley1bea1732014-12-17 19:06:57 -0800428 case SSL3_ST_CW_FINISHED_A:
429 case SSL3_ST_CW_FINISHED_B:
430 ret =
431 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
432 s->enc_method->client_finished_label,
433 s->enc_method->client_finished_label_len);
434 if (ret <= 0) {
435 goto end;
436 }
437 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langley1bea1732014-12-17 19:06:57 -0800439 if (s->hit) {
440 s->s3->tmp.next_state = SSL_ST_OK;
441 } else {
442 /* This is a non-resumption handshake. If it involves ChannelID, then
443 * record the handshake hashes at this point in the session so that
444 * any resumption of this session with ChannelID can sign those
445 * hashes. */
446 if (s->s3->tlsext_channel_id_new) {
447 ret = tls1_record_handshake_hashes_for_channel_id(s);
448 if (ret <= 0)
449 goto end;
450 }
451 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) &&
452 ssl3_can_cutthrough(s) &&
453 /* no cutthrough on renegotiation (would complicate the state
454 * machine) */
455 s->s3->previous_server_finished_len == 0) {
456 s->s3->tmp.next_state = SSL3_ST_CUTTHROUGH_COMPLETE;
457 } else {
458 /* Allow NewSessionTicket if ticket expected */
459 if (s->tlsext_ticket_expected) {
460 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
461 } else {
462 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
463 }
464 }
465 }
466 s->init_num = 0;
467 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley1bea1732014-12-17 19:06:57 -0800469 case SSL3_ST_CR_SESSION_TICKET_A:
470 case SSL3_ST_CR_SESSION_TICKET_B:
471 ret = ssl3_get_new_session_ticket(s);
472 if (ret <= 0) {
473 goto end;
474 }
475 s->state = SSL3_ST_CR_CHANGE;
476 s->init_num = 0;
477 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langley1bea1732014-12-17 19:06:57 -0800479 case SSL3_ST_CR_CERT_STATUS_A:
480 case SSL3_ST_CR_CERT_STATUS_B:
481 ret = ssl3_get_cert_status(s);
482 if (ret <= 0) {
483 goto end;
484 }
485 s->state = SSL3_ST_CR_KEY_EXCH_A;
486 s->init_num = 0;
487 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400488
Adam Langley1bea1732014-12-17 19:06:57 -0800489 case SSL3_ST_CR_CHANGE:
490 /* At this point, the next message must be entirely behind a
491 * ChangeCipherSpec. */
492 if (!ssl3_expect_change_cipher_spec(s)) {
493 ret = -1;
494 goto end;
495 }
496 s->state = SSL3_ST_CR_FINISHED_A;
497 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700498
Adam Langley1bea1732014-12-17 19:06:57 -0800499 case SSL3_ST_CR_FINISHED_A:
500 case SSL3_ST_CR_FINISHED_B:
501 ret =
502 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
503 if (ret <= 0) {
504 goto end;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langley1bea1732014-12-17 19:06:57 -0800507 if (s->hit) {
508 s->state = SSL3_ST_CW_CHANGE_A;
509 } else {
510 s->state = SSL_ST_OK;
511 }
512 s->init_num = 0;
513 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langley1bea1732014-12-17 19:06:57 -0800515 case SSL3_ST_CW_FLUSH:
516 s->rwstate = SSL_WRITING;
517 if (BIO_flush(s->wbio) <= 0) {
518 ret = -1;
519 goto end;
520 }
521 s->rwstate = SSL_NOTHING;
522 s->state = s->s3->tmp.next_state;
523 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700524
Adam Langley1bea1732014-12-17 19:06:57 -0800525 case SSL3_ST_CUTTHROUGH_COMPLETE:
526 /* Allow NewSessionTicket if ticket expected */
527 if (s->tlsext_ticket_expected) {
528 s->state = SSL3_ST_CR_SESSION_TICKET_A;
529 } else {
530 s->state = SSL3_ST_CR_CHANGE;
531 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700532
Adam Langley1bea1732014-12-17 19:06:57 -0800533 ssl_free_wbio_buffer(s);
534 ret = 1;
535 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langley1bea1732014-12-17 19:06:57 -0800537 case SSL_ST_OK:
538 /* clean a few things up */
539 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700540
Adam Langley1bea1732014-12-17 19:06:57 -0800541 if (s->init_buf != NULL) {
542 BUF_MEM_free(s->init_buf);
543 s->init_buf = NULL;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langley1bea1732014-12-17 19:06:57 -0800546 /* Remove write buffering now. */
547 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley1bea1732014-12-17 19:06:57 -0800549 s->init_num = 0;
550 s->renegotiate = 0;
551 s->new_session = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
554 if (s->hit) {
555 s->ctx->stats.sess_hit++;
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langley1bea1732014-12-17 19:06:57 -0800558 ret = 1;
559 /* s->server=0; */
560 s->ctx->stats.sess_connect_good++;
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langley1bea1732014-12-17 19:06:57 -0800562 if (cb != NULL) {
563 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
564 }
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langley1bea1732014-12-17 19:06:57 -0800566 goto end;
567
568 default:
569 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
570 ret = -1;
571 goto end;
572 }
573
574 if (!s->s3->tmp.reuse_message && !skip) {
575 if (cb != NULL && s->state != state) {
576 new_state = s->state;
577 s->state = state;
578 cb(s, SSL_CB_CONNECT_LOOP, 1);
579 s->state = new_state;
580 }
581 }
582 skip = 0;
583 }
584
Adam Langley95c29f32014-06-20 12:00:00 -0700585end:
Adam Langley1bea1732014-12-17 19:06:57 -0800586 s->in_handshake--;
587 if (buf != NULL) {
588 BUF_MEM_free(buf);
589 }
590 if (cb != NULL) {
591 cb(s, SSL_CB_CONNECT_EXIT, ret);
592 }
593 return ret;
594}
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langley1bea1732014-12-17 19:06:57 -0800596int ssl3_send_client_hello(SSL *s) {
597 uint8_t *buf, *p, *d;
598 int i;
599 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langley1bea1732014-12-17 19:06:57 -0800601 buf = (uint8_t *)s->init_buf->data;
602 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
603 if (!s->s3->have_version) {
604 uint16_t max_version = ssl3_get_max_client_version(s);
605 /* Disabling all versions is silly: return an error. */
606 if (max_version == 0) {
607 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
608 goto err;
609 }
610 s->version = max_version;
611 s->client_version = max_version;
612 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500613
Adam Langley1bea1732014-12-17 19:06:57 -0800614 /* If the configured session was created at a version higher than our
615 * maximum version, drop it. */
616 if (s->session &&
617 (s->session->session_id_length == 0 || s->session->not_resumable ||
618 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
619 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
620 SSL_set_session(s, NULL);
621 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500622
Adam Langley1bea1732014-12-17 19:06:57 -0800623 /* else use the pre-loaded session */
624 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley1bea1732014-12-17 19:06:57 -0800626 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
627 * renegerate the client_random. The random must be reused. */
628 if (!SSL_IS_DTLS(s) || !s->d1->send_cookie) {
629 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
630 }
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langley1bea1732014-12-17 19:06:57 -0800632 /* Do the message type and length last. Note: the final argument to
633 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
634 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley1bea1732014-12-17 19:06:57 -0800636 /* version indicates the negotiated version: for example from an SSLv2/v3
637 * compatible client hello). The client_version field is the maximum
638 * version we permit and it is also used in RSA encrypted premaster
639 * secrets. Some servers can choke if we initially report a higher version
640 * then renegotiate to a lower one in the premaster secret. This didn't
641 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
642 * or later if the server only supports 1.0.
643 *
644 * Possible scenario with previous logic:
645 * 1. Client hello indicates TLS 1.2
646 * 2. Server hello says TLS 1.0
647 * 3. RSA encrypted premaster secret uses 1.2.
648 * 4. Handhaked proceeds using TLS 1.0.
649 * 5. Server sends hello request to renegotiate.
650 * 6. Client hello indicates TLS v1.0 as we now
651 * know that is maximum server supports.
652 * 7. Server chokes on RSA encrypted premaster secret
653 * containing version 1.0.
654 *
655 * For interoperability it should be OK to always use the maximum version
656 * we support in client hello and then rely on the checking of version to
657 * ensure the servers isn't being inconsistent: for example initially
658 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
659 * using client_version in client hello and not resetting it to the
660 * negotiated version. */
661 *(p++) = s->client_version >> 8;
662 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langley1bea1732014-12-17 19:06:57 -0800664 /* Random stuff */
665 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
666 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langley1bea1732014-12-17 19:06:57 -0800668 /* Session ID */
669 if (s->new_session || s->session == NULL) {
670 i = 0;
671 } else {
672 i = s->session->session_id_length;
673 }
674 *(p++) = i;
675 if (i != 0) {
676 if (i > (int)sizeof(s->session->session_id)) {
677 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
678 goto err;
679 }
680 memcpy(p, s->session->session_id, i);
681 p += i;
682 }
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langley1bea1732014-12-17 19:06:57 -0800684 /* cookie stuff for DTLS */
685 if (SSL_IS_DTLS(s)) {
686 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
687 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
688 goto err;
689 }
690 *(p++) = s->d1->cookie_len;
691 memcpy(p, s->d1->cookie, s->d1->cookie_len);
692 p += s->d1->cookie_len;
693 }
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langley1bea1732014-12-17 19:06:57 -0800695 /* Ciphers supported */
696 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
697 if (i == 0) {
698 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
699 SSL_R_NO_CIPHERS_AVAILABLE);
700 goto err;
701 }
702 s2n(i, p);
703 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langley1bea1732014-12-17 19:06:57 -0800705 /* COMPRESSION */
706 *(p++) = 1;
707 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley1bea1732014-12-17 19:06:57 -0800709 /* TLS extensions*/
710 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
711 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
712 goto err;
713 }
714
715 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
716 p - buf);
717 if (p == NULL) {
718 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
719 goto err;
720 }
721
722 l = p - d;
723 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
724 s->state = SSL3_ST_CW_CLNT_HELLO_B;
725 }
726
727 /* SSL3_ST_CW_CLNT_HELLO_B */
728 return ssl_do_write(s);
729
Adam Langley95c29f32014-06-20 12:00:00 -0700730err:
Adam Langley1bea1732014-12-17 19:06:57 -0800731 return -1;
732}
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langley1bea1732014-12-17 19:06:57 -0800734int ssl3_get_server_hello(SSL *s) {
735 STACK_OF(SSL_CIPHER) * sk;
736 const SSL_CIPHER *c;
737 CERT *ct = s->cert;
738 int al = SSL_AD_INTERNAL_ERROR, ok;
739 long n;
740 CBS server_hello, server_random, session_id;
741 uint16_t server_version, cipher_suite;
742 uint8_t compression_method;
743 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langley1bea1732014-12-17 19:06:57 -0800745 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
746 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
747 20000, /* ?? */
748 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langley1bea1732014-12-17 19:06:57 -0800750 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500751 uint32_t err = ERR_peek_error();
752 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
753 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
754 /* Add a dedicated error code to the queue for a handshake_failure alert
755 * in response to ClientHello. This matches NSS's client behavior and
756 * gives a better error on a (probable) failure to negotiate initial
757 * parameters. Note: this error code comes after the original one.
758 *
759 * See https://crbug.com/446505. */
760 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
761 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
762 }
Adam Langley1bea1732014-12-17 19:06:57 -0800763 return n;
764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langley1bea1732014-12-17 19:06:57 -0800766 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400767
Adam Langley1bea1732014-12-17 19:06:57 -0800768 if (!CBS_get_u16(&server_hello, &server_version) ||
769 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
770 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
771 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
772 !CBS_get_u16(&server_hello, &cipher_suite) ||
773 !CBS_get_u8(&server_hello, &compression_method)) {
774 al = SSL_AD_DECODE_ERROR;
775 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
776 goto f_err;
777 }
David Benjamina03d95d2014-07-12 19:49:07 -0400778
Adam Langley1bea1732014-12-17 19:06:57 -0800779 if (!s->s3->have_version) {
780 if (!ssl3_is_version_enabled(s, server_version)) {
781 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
782 s->version = server_version;
783 /* Mark the version as fixed so the record-layer version is not clamped
784 * to TLS 1.0. */
785 s->s3->have_version = 1;
786 al = SSL_AD_PROTOCOL_VERSION;
787 goto f_err;
788 }
789 s->version = server_version;
790 s->enc_method = ssl3_get_enc_method(server_version);
791 assert(s->enc_method != NULL);
792 /* At this point, the connection's version is known and s->version is
793 * fixed. Begin enforcing the record-layer version. */
794 s->s3->have_version = 1;
795 } else if (server_version != s->version) {
796 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
797 al = SSL_AD_PROTOCOL_VERSION;
798 goto f_err;
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langley1bea1732014-12-17 19:06:57 -0800801 /* Copy over the server random. */
802 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langley1bea1732014-12-17 19:06:57 -0800804 assert(s->session == NULL || s->session->session_id_length > 0);
805 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
806 s->session->session_id_length)) {
807 if (s->sid_ctx_length != s->session->sid_ctx_length ||
808 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
809 /* actually a client application bug */
810 al = SSL_AD_ILLEGAL_PARAMETER;
811 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
812 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
813 goto f_err;
814 }
815 s->hit = 1;
816 } else {
817 /* The session wasn't resumed. Create a fresh SSL_SESSION to
818 * fill out. */
819 s->hit = 0;
820 if (!ssl_get_new_session(s, 0)) {
821 goto f_err;
822 }
823 /* Note: session_id could be empty. */
824 s->session->session_id_length = CBS_len(&session_id);
825 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
826 }
David Benjamina03d95d2014-07-12 19:49:07 -0400827
Adam Langley1bea1732014-12-17 19:06:57 -0800828 c = ssl3_get_cipher_by_value(cipher_suite);
829 if (c == NULL) {
830 /* unknown cipher */
831 al = SSL_AD_ILLEGAL_PARAMETER;
832 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
833 SSL_R_UNKNOWN_CIPHER_RETURNED);
834 goto f_err;
835 }
836 /* ct->mask_ssl was computed from client capabilities. Now
837 * that the final version is known, compute a new mask_ssl. */
838 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
839 mask_ssl = SSL_TLSV1_2;
840 } else {
841 mask_ssl = 0;
842 }
843 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
844 * the server selected it, it's an error. */
845 if ((c->algorithm_ssl & mask_ssl) ||
846 (c->algorithm_mkey & ct->mask_k) ||
847 (c->algorithm_auth & ct->mask_a)) {
848 al = SSL_AD_ILLEGAL_PARAMETER;
849 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
850 goto f_err;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langley1bea1732014-12-17 19:06:57 -0800853 sk = ssl_get_ciphers_by_id(s);
854 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
855 /* we did not say we would use this cipher */
856 al = SSL_AD_ILLEGAL_PARAMETER;
857 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
858 goto f_err;
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
David Benjaminf3a8b122014-12-25 23:11:49 -0500861 if (s->hit && s->session->cipher != c) {
Adam Langley1bea1732014-12-17 19:06:57 -0800862 al = SSL_AD_ILLEGAL_PARAMETER;
863 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
864 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
865 goto f_err;
866 }
867 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500868
Adam Langley1bea1732014-12-17 19:06:57 -0800869 /* Most clients also require that the negotiated version match the session's
870 * version if resuming. However OpenSSL has historically not had the
871 * corresponding logic on the server, so this may not be compatible,
872 * depending on other factors. (Whether the ClientHello version is clamped to
873 * the session's version and whether the session cache is keyed on IP
874 * address.)
875 *
876 * TODO(davidben): See if we can still enforce this? Perhaps for the future
877 * TLS 1.3 and forward if this is fixed upstream. */
David Benjaminbdf5e722014-11-11 00:52:15 -0500878
Adam Langley1bea1732014-12-17 19:06:57 -0800879 /* Don't digest cached records if no sigalgs: we may need them for client
880 * authentication. */
881 if (!SSL_USE_SIGALGS(s) &&
882 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
883 goto f_err;
884 }
David Benjamina03d95d2014-07-12 19:49:07 -0400885
Adam Langley1bea1732014-12-17 19:06:57 -0800886 /* Only the NULL compression algorithm is supported. */
887 if (compression_method != 0) {
888 al = SSL_AD_ILLEGAL_PARAMETER;
889 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
890 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
891 goto f_err;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langley1bea1732014-12-17 19:06:57 -0800894 /* TLS extensions */
895 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
896 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
897 goto err;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langley1bea1732014-12-17 19:06:57 -0800900 /* There should be nothing left over in the record. */
901 if (CBS_len(&server_hello) != 0) {
902 /* wrong packet length */
903 al = SSL_AD_DECODE_ERROR;
904 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
905 goto f_err;
906 }
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langley1bea1732014-12-17 19:06:57 -0800908 return 1;
909
Adam Langley95c29f32014-06-20 12:00:00 -0700910f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800911 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700912err:
Adam Langley1bea1732014-12-17 19:06:57 -0800913 return -1;
914}
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langley1bea1732014-12-17 19:06:57 -0800916int ssl3_get_server_certificate(SSL *s) {
917 int al, i, ok, ret = -1;
918 unsigned long n;
919 X509 *x = NULL;
920 STACK_OF(X509) *sk = NULL;
921 SESS_CERT *sc;
922 EVP_PKEY *pkey = NULL;
923 CBS cbs, certificate_list;
924 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langley1bea1732014-12-17 19:06:57 -0800926 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
927 SSL3_MT_CERTIFICATE, s->max_cert_list,
928 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langley1bea1732014-12-17 19:06:57 -0800930 if (!ok) {
931 return n;
932 }
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langley1bea1732014-12-17 19:06:57 -0800934 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langley1bea1732014-12-17 19:06:57 -0800936 sk = sk_X509_new_null();
937 if (sk == NULL) {
938 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
939 goto err;
940 }
Adam Langley95c29f32014-06-20 12:00:00 -0700941
Adam Langley1bea1732014-12-17 19:06:57 -0800942 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
943 CBS_len(&cbs) != 0) {
944 al = SSL_AD_DECODE_ERROR;
945 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
946 goto f_err;
947 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400948
Adam Langley1bea1732014-12-17 19:06:57 -0800949 while (CBS_len(&certificate_list) > 0) {
950 CBS certificate;
951 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
952 al = SSL_AD_DECODE_ERROR;
953 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
954 SSL_R_CERT_LENGTH_MISMATCH);
955 goto f_err;
956 }
957 data = CBS_data(&certificate);
958 x = d2i_X509(NULL, &data, CBS_len(&certificate));
959 if (x == NULL) {
960 al = SSL_AD_BAD_CERTIFICATE;
961 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
962 goto f_err;
963 }
964 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
965 al = SSL_AD_DECODE_ERROR;
966 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
967 SSL_R_CERT_LENGTH_MISMATCH);
968 goto f_err;
969 }
970 if (!sk_X509_push(sk, x)) {
971 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
972 goto err;
973 }
974 x = NULL;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langley1bea1732014-12-17 19:06:57 -0800977 i = ssl_verify_cert_chain(s, sk);
978 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
979 al = ssl_verify_alarm_type(s->verify_result);
980 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
981 SSL_R_CERTIFICATE_VERIFY_FAILED);
982 goto f_err;
983 }
984 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langley1bea1732014-12-17 19:06:57 -0800986 sc = ssl_sess_cert_new();
987 if (sc == NULL) {
988 goto err;
989 }
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langley1bea1732014-12-17 19:06:57 -0800991 if (s->session->sess_cert) {
992 ssl_sess_cert_free(s->session->sess_cert);
993 }
994 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langley1bea1732014-12-17 19:06:57 -0800996 sc->cert_chain = sk;
997 /* Inconsistency alert: cert_chain does include the peer's certificate, which
998 * we don't include in s3_srvr.c */
999 x = sk_X509_value(sk, 0);
1000 sk = NULL;
1001 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langley1bea1732014-12-17 19:06:57 -08001003 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langley1bea1732014-12-17 19:06:57 -08001005 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1006 x = NULL;
1007 al = SSL3_AL_FATAL;
1008 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1009 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1010 goto f_err;
1011 }
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langley1bea1732014-12-17 19:06:57 -08001013 i = ssl_cert_type(pkey);
1014 if (i < 0) {
1015 x = NULL;
1016 al = SSL3_AL_FATAL;
1017 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1018 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1019 goto f_err;
1020 }
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langley1bea1732014-12-17 19:06:57 -08001022 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1023 if (exp_idx >= 0 && i != exp_idx) {
1024 x = NULL;
1025 al = SSL_AD_ILLEGAL_PARAMETER;
1026 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1027 SSL_R_WRONG_CERTIFICATE_TYPE);
1028 goto f_err;
1029 }
1030 sc->peer_cert_type = i;
1031 /* Why would the following ever happen? We just created sc a couple of lines
1032 * ago. */
1033 if (sc->peer_pkeys[i].x509 != NULL) {
1034 X509_free(sc->peer_pkeys[i].x509);
1035 }
1036 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1037 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 if (s->session->peer != NULL) {
1040 X509_free(s->session->peer);
1041 }
1042 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001043
Adam Langley1bea1732014-12-17 19:06:57 -08001044 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langley1bea1732014-12-17 19:06:57 -08001046 x = NULL;
1047 ret = 1;
1048
1049 if (0) {
1050 f_err:
1051 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1052 }
1053
1054err:
1055 EVP_PKEY_free(pkey);
1056 X509_free(x);
1057 sk_X509_pop_free(sk, X509_free);
1058 return ret;
1059}
1060
1061int ssl3_get_server_key_exchange(SSL *s) {
1062 EVP_MD_CTX md_ctx;
1063 int al, ok;
1064 long n, alg_k, alg_a;
1065 EVP_PKEY *pkey = NULL;
1066 const EVP_MD *md = NULL;
1067 RSA *rsa = NULL;
1068 DH *dh = NULL;
1069 EC_KEY *ecdh = NULL;
1070 BN_CTX *bn_ctx = NULL;
1071 EC_POINT *srvr_ecpoint = NULL;
1072 CBS server_key_exchange, server_key_exchange_orig, parameter;
1073
1074 /* use same message size as in ssl3_get_certificate_request() as
1075 * ServerKeyExchange message may be skipped */
1076 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1077 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
1078 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
1079 if (!ok) {
1080 return n;
1081 }
1082
1083 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1084 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1085 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1086 SSL_R_UNEXPECTED_MESSAGE);
1087 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1088 return -1;
1089 }
1090
1091 /* In plain PSK ciphersuite, ServerKeyExchange can be
1092 omitted if no identity hint is sent. Set session->sess_cert anyway to
1093 avoid problems later.*/
1094 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1095 /* PSK ciphersuites that also send a Certificate would have already
1096 * initialized |sess_cert|. */
1097 if (s->session->sess_cert == NULL) {
1098 s->session->sess_cert = ssl_sess_cert_new();
1099 }
1100
1101 /* TODO(davidben): This should be reset in one place with the rest of the
1102 * handshake state. */
1103 if (s->s3->tmp.peer_psk_identity_hint) {
1104 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1105 s->s3->tmp.peer_psk_identity_hint = NULL;
1106 }
1107 }
1108 s->s3->tmp.reuse_message = 1;
1109 return 1;
1110 }
1111
1112 /* Retain a copy of the original CBS to compute the signature over. */
1113 CBS_init(&server_key_exchange, s->init_msg, n);
1114 server_key_exchange_orig = server_key_exchange;
1115
1116 if (s->session->sess_cert != NULL) {
1117 if (s->session->sess_cert->peer_dh_tmp) {
1118 DH_free(s->session->sess_cert->peer_dh_tmp);
1119 s->session->sess_cert->peer_dh_tmp = NULL;
1120 }
1121 if (s->session->sess_cert->peer_ecdh_tmp) {
1122 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1123 s->session->sess_cert->peer_ecdh_tmp = NULL;
1124 }
1125 } else {
1126 s->session->sess_cert = ssl_sess_cert_new();
1127 }
1128
1129 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1130 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1131 EVP_MD_CTX_init(&md_ctx);
1132
1133 if (alg_a & SSL_aPSK) {
1134 CBS psk_identity_hint;
1135
1136 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1137 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1138 &psk_identity_hint)) {
1139 al = SSL_AD_DECODE_ERROR;
1140 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1141 goto f_err;
1142 }
1143
1144 /* Store PSK identity hint for later use, hint is used in
1145 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1146 * identity hint can be as long as the maximum length of a PSK identity.
1147 * Also do not allow NULL characters; identities are saved as C strings.
1148 *
1149 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1150 * a specific identity. */
1151 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1152 CBS_contains_zero_byte(&psk_identity_hint)) {
1153 al = SSL_AD_HANDSHAKE_FAILURE;
1154 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1155 SSL_R_DATA_LENGTH_TOO_LONG);
1156 goto f_err;
1157 }
1158
1159 /* Save the identity hint as a C string. */
1160 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1161 al = SSL_AD_INTERNAL_ERROR;
1162 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1163 ERR_R_MALLOC_FAILURE);
1164 goto f_err;
1165 }
1166 }
1167
1168 if (alg_k & SSL_kEDH) {
1169 CBS dh_p, dh_g, dh_Ys;
1170
1171 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1172 CBS_len(&dh_p) == 0 ||
1173 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1174 CBS_len(&dh_g) == 0 ||
1175 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1176 CBS_len(&dh_Ys) == 0) {
1177 al = SSL_AD_DECODE_ERROR;
1178 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1179 goto f_err;
1180 }
1181
1182 dh = DH_new();
1183 if (dh == NULL) {
1184 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1185 goto err;
1186 }
1187
1188 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1189 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1190 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1191 NULL) {
1192 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1193 goto err;
1194 }
1195
1196 if (DH_size(dh) < 512 / 8) {
1197 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1198 SSL_R_BAD_DH_P_LENGTH);
1199 goto err;
1200 }
1201
1202 if (alg_a & SSL_aRSA) {
1203 pkey = X509_get_pubkey(
1204 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1205 }
1206 /* else anonymous DH, so no certificate or pkey. */
1207
1208 s->session->sess_cert->peer_dh_tmp = dh;
1209 dh = NULL;
1210 } else if (alg_k & SSL_kEECDH) {
1211 uint16_t curve_id;
1212 int curve_nid = 0;
1213 EC_GROUP *ngroup;
1214 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
1234 ecdh = EC_KEY_new();
1235 if (ecdh == NULL) {
1236 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1237 ERR_R_MALLOC_FAILURE);
1238 goto err;
1239 }
1240
1241 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1242 if (ngroup == NULL ||
1243 EC_KEY_set_group(ecdh, ngroup) == 0) {
1244 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
1245 goto err;
1246 }
1247 EC_GROUP_free(ngroup);
1248
1249 group = EC_KEY_get0_group(ecdh);
1250
1251 /* Next, get the encoded ECPoint */
1252 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1253 al = SSL_AD_DECODE_ERROR;
1254 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1255 goto f_err;
1256 }
1257
1258 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1259 ((bn_ctx = BN_CTX_new()) == NULL)) {
1260 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1261 ERR_R_MALLOC_FAILURE);
1262 goto err;
1263 }
1264
1265 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1266 CBS_len(&point), bn_ctx)) {
1267 al = SSL_AD_DECODE_ERROR;
1268 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1269 goto f_err;
1270 }
1271
1272 /* The ECC/TLS specification does not mention the use of DSA to sign
1273 * ECParameters in the server key exchange message. We do support RSA and
1274 * ECDSA. */
1275 if (alg_a & SSL_aRSA) {
1276 pkey = X509_get_pubkey(
1277 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1278 } else if (alg_a & SSL_aECDSA) {
1279 pkey =
1280 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1281 }
1282 /* else anonymous ECDH, so no certificate or pkey. */
1283 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1284 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1285 ecdh = NULL;
1286 BN_CTX_free(bn_ctx);
1287 bn_ctx = NULL;
1288 EC_POINT_free(srvr_ecpoint);
1289 srvr_ecpoint = NULL;
1290 } else if (!(alg_k & SSL_kPSK)) {
1291 al = SSL_AD_UNEXPECTED_MESSAGE;
1292 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1293 SSL_R_UNEXPECTED_MESSAGE);
1294 goto f_err;
1295 }
1296
1297 /* At this point, |server_key_exchange| contains the signature, if any, while
1298 * |server_key_exchange_orig| contains the entire message. From that, derive
1299 * a CBS containing just the parameter. */
1300 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1301 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1302
1303 /* if it was signed, check the signature */
1304 if (pkey != NULL) {
1305 CBS signature;
1306
1307 if (SSL_USE_SIGALGS(s)) {
1308 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1309 goto f_err;
1310 }
1311 } else if (pkey->type == EVP_PKEY_RSA) {
1312 md = EVP_md5_sha1();
1313 } else {
1314 md = EVP_sha1();
1315 }
1316
1317 /* The last field in |server_key_exchange| is the signature. */
1318 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1319 CBS_len(&server_key_exchange) != 0) {
1320 al = SSL_AD_DECODE_ERROR;
1321 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1322 goto f_err;
1323 }
1324
1325 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1326 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1327 SSL3_RANDOM_SIZE) ||
1328 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1329 SSL3_RANDOM_SIZE) ||
1330 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1331 CBS_len(&parameter)) ||
1332 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1333 CBS_len(&signature))) {
1334 /* bad signature */
1335 al = SSL_AD_DECRYPT_ERROR;
1336 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1337 goto f_err;
1338 }
1339 } else {
1340 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1341 /* Might be wrong key type, check it */
1342 if (ssl3_check_cert_and_algorithm(s)) {
1343 /* Otherwise this shouldn't happen */
1344 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1345 ERR_R_INTERNAL_ERROR);
1346 }
1347 goto err;
1348 }
1349 /* still data left over */
1350 if (CBS_len(&server_key_exchange) > 0) {
1351 al = SSL_AD_DECODE_ERROR;
1352 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1353 SSL_R_EXTRA_DATA_IN_MESSAGE);
1354 goto f_err;
1355 }
1356 }
1357 EVP_PKEY_free(pkey);
1358 EVP_MD_CTX_cleanup(&md_ctx);
1359 return 1;
1360
Adam Langley95c29f32014-06-20 12:00:00 -07001361f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001362 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001363err:
Adam Langley1bea1732014-12-17 19:06:57 -08001364 EVP_PKEY_free(pkey);
1365 if (rsa != NULL) {
1366 RSA_free(rsa);
1367 }
1368 if (dh != NULL) {
1369 DH_free(dh);
1370 }
1371 BN_CTX_free(bn_ctx);
1372 EC_POINT_free(srvr_ecpoint);
1373 if (ecdh != NULL) {
1374 EC_KEY_free(ecdh);
1375 }
1376 EVP_MD_CTX_cleanup(&md_ctx);
1377 return -1;
1378}
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1381 return X509_NAME_cmp(*a, *b);
1382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
Adam Langley1bea1732014-12-17 19:06:57 -08001384int ssl3_get_certificate_request(SSL *s) {
1385 int ok, ret = 0;
1386 unsigned long n;
1387 X509_NAME *xn = NULL;
1388 STACK_OF(X509_NAME) *ca_sk = NULL;
1389 CBS cbs;
1390 CBS certificate_types;
1391 CBS certificate_authorities;
1392 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langley1bea1732014-12-17 19:06:57 -08001394 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1395 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
1396 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001397
Adam Langley1bea1732014-12-17 19:06:57 -08001398 if (!ok) {
1399 return n;
1400 }
David Benjamin688d8df2014-11-02 23:06:42 -05001401
Adam Langley1bea1732014-12-17 19:06:57 -08001402 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1405 s->s3->tmp.reuse_message = 1;
1406 /* If we get here we don't need any cached handshake records as we wont be
1407 * doing client auth. */
1408 if (s->s3->handshake_buffer &&
1409 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1410 goto err;
1411 }
1412 return 1;
1413 }
David Benjamined439582014-07-14 19:13:02 -04001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1416 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1417 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1418 SSL_R_WRONG_MESSAGE_TYPE);
1419 goto err;
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
Adam Langley1bea1732014-12-17 19:06:57 -08001422 /* TLS does not like anon-DH with client cert */
1423 if (s->version > SSL3_VERSION &&
1424 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1425 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1426 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1427 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1428 goto err;
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langley1bea1732014-12-17 19:06:57 -08001431 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langley1bea1732014-12-17 19:06:57 -08001433 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1434 if (ca_sk == NULL) {
1435 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1436 goto err;
1437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 /* get the certificate types */
1440 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1441 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1442 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1443 goto err;
1444 }
David Benjamined439582014-07-14 19:13:02 -04001445
Adam Langley1bea1732014-12-17 19:06:57 -08001446 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1447 &s->s3->tmp.num_certificate_types)) {
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1449 goto err;
1450 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001451
Adam Langley1bea1732014-12-17 19:06:57 -08001452 if (SSL_USE_SIGALGS(s)) {
1453 CBS supported_signature_algorithms;
1454 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1455 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1456 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1457 goto err;
1458 }
David Benjamined439582014-07-14 19:13:02 -04001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1461 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1462 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1463 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1464 goto err;
1465 }
1466 }
David Benjamined439582014-07-14 19:13:02 -04001467
Adam Langley1bea1732014-12-17 19:06:57 -08001468 /* get the CA RDNs */
1469 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1470 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1471 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1472 goto err;
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langley1bea1732014-12-17 19:06:57 -08001475 while (CBS_len(&certificate_authorities) > 0) {
1476 CBS distinguished_name;
1477 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1478 &distinguished_name)) {
1479 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1480 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1481 SSL_R_CA_DN_TOO_LONG);
1482 goto err;
1483 }
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langley1bea1732014-12-17 19:06:57 -08001485 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001486
Adam Langley1bea1732014-12-17 19:06:57 -08001487 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1488 if (xn == NULL) {
1489 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1490 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1491 goto err;
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langley1bea1732014-12-17 19:06:57 -08001494 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1495 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1496 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1497 goto err;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langley1bea1732014-12-17 19:06:57 -08001500 if (CBS_len(&distinguished_name) != 0) {
1501 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1502 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1503 SSL_R_CA_DN_LENGTH_MISMATCH);
1504 goto err;
1505 }
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langley1bea1732014-12-17 19:06:57 -08001507 if (!sk_X509_NAME_push(ca_sk, xn)) {
1508 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1509 ERR_R_MALLOC_FAILURE);
1510 goto err;
1511 }
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langley1bea1732014-12-17 19:06:57 -08001514 /* we should setup a certificate to return.... */
1515 s->s3->tmp.cert_req = 1;
1516 if (s->s3->tmp.ca_names != NULL) {
1517 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1518 }
1519 s->s3->tmp.ca_names = ca_sk;
1520 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langley1bea1732014-12-17 19:06:57 -08001522 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langley1bea1732014-12-17 19:06:57 -08001524err:
1525 if (ca_sk != NULL) {
1526 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1527 }
1528 return ret;
1529}
Adam Langley95c29f32014-06-20 12:00:00 -07001530
Adam Langley1bea1732014-12-17 19:06:57 -08001531int ssl3_get_new_session_ticket(SSL *s) {
1532 int ok, al, ret = 0;
1533 long n;
1534 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001535
Adam Langley1bea1732014-12-17 19:06:57 -08001536 n = s->method->ssl_get_message(
1537 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
1538 SSL3_MT_NEWSESSION_TICKET, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001539
Adam Langley1bea1732014-12-17 19:06:57 -08001540 if (!ok) {
1541 return n;
1542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langley1bea1732014-12-17 19:06:57 -08001544 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001545
Adam Langley1bea1732014-12-17 19:06:57 -08001546 if (!CBS_get_u32(&new_session_ticket,
1547 &s->session->tlsext_tick_lifetime_hint) ||
1548 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1549 CBS_len(&new_session_ticket) != 0) {
1550 al = SSL_AD_DECODE_ERROR;
1551 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1552 goto f_err;
1553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554
Adam Langley1bea1732014-12-17 19:06:57 -08001555 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1556 &s->session->tlsext_ticklen)) {
1557 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1558 goto err;
1559 }
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langley1bea1732014-12-17 19:06:57 -08001561 /* There are two ways to detect a resumed ticket sesion. One is to set an
1562 * appropriate session ID and then the server must return a match in
1563 * ServerHello. This allows the normal client session ID matching to work and
1564 * we know much earlier that the ticket has been accepted.
1565 *
1566 * The other way is to set zero length session ID when the ticket is
1567 * presented and rely on the handshake to determine session resumption.
1568 *
1569 * We choose the former approach because this fits in with assumptions
1570 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1571 * SHA256 is disabled) hash of the ticket. */
1572 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1573 &s->session->session_id_length, EVP_sha256(), NULL);
1574 ret = 1;
1575 return ret;
David Benjamined439582014-07-14 19:13:02 -04001576
Adam Langley95c29f32014-06-20 12:00:00 -07001577f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001578 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001579err:
Adam Langley1bea1732014-12-17 19:06:57 -08001580 return -1;
1581}
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Adam Langley1bea1732014-12-17 19:06:57 -08001583int ssl3_get_cert_status(SSL *s) {
1584 int ok, al;
1585 long n;
1586 CBS certificate_status, ocsp_response;
1587 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langley1bea1732014-12-17 19:06:57 -08001589 n = s->method->ssl_get_message(
1590 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
1591 SSL3_MT_CERTIFICATE_STATUS, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langley1bea1732014-12-17 19:06:57 -08001593 if (!ok) {
1594 return n;
1595 }
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langley1bea1732014-12-17 19:06:57 -08001597 CBS_init(&certificate_status, s->init_msg, n);
1598 if (!CBS_get_u8(&certificate_status, &status_type) ||
1599 status_type != TLSEXT_STATUSTYPE_ocsp ||
1600 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1601 CBS_len(&ocsp_response) == 0 ||
1602 CBS_len(&certificate_status) != 0) {
1603 al = SSL_AD_DECODE_ERROR;
1604 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1605 goto f_err;
1606 }
Adam Langley95c29f32014-06-20 12:00:00 -07001607
Adam Langley1bea1732014-12-17 19:06:57 -08001608 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1609 &s->session->ocsp_response_length)) {
1610 al = SSL_AD_INTERNAL_ERROR;
1611 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1612 goto f_err;
1613 }
1614 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langley95c29f32014-06-20 12:00:00 -07001616f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001617 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1618 return -1;
1619}
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langley1bea1732014-12-17 19:06:57 -08001621int ssl3_get_server_done(SSL *s) {
1622 int ok;
1623 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001624
Adam Langley1bea1732014-12-17 19:06:57 -08001625 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1626 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1627 30, /* should be very small, like 0 :-) */
1628 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langley1bea1732014-12-17 19:06:57 -08001630 if (!ok) {
1631 return n;
1632 }
David Benjamin46062682014-07-14 19:14:32 -04001633
Adam Langley1bea1732014-12-17 19:06:57 -08001634 if (n > 0) {
1635 /* should contain no data */
1636 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1637 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1638 return -1;
1639 }
David Benjamin46062682014-07-14 19:14:32 -04001640
Adam Langley1bea1732014-12-17 19:06:57 -08001641 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001642}
Adam Langley1258b6a2014-06-20 12:00:00 -07001643
Adam Langley1258b6a2014-06-20 12:00:00 -07001644
Adam Langley1bea1732014-12-17 19:06:57 -08001645int ssl3_send_client_key_exchange(SSL *s) {
1646 uint8_t *p;
1647 int n = 0;
1648 unsigned long alg_k;
1649 unsigned long alg_a;
1650 uint8_t *q;
1651 EVP_PKEY *pkey = NULL;
1652 EC_KEY *clnt_ecdh = NULL;
1653 const EC_POINT *srvr_ecpoint = NULL;
1654 EVP_PKEY *srvr_pub_pkey = NULL;
1655 uint8_t *encodedPoint = NULL;
1656 int encoded_pt_len = 0;
1657 BN_CTX *bn_ctx = NULL;
1658 unsigned int psk_len = 0;
1659 uint8_t psk[PSK_MAX_PSK_LEN];
1660 uint8_t *pms = NULL;
1661 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1664 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1667 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001668
Adam Langley1bea1732014-12-17 19:06:57 -08001669 /* If using a PSK key exchange, prepare the pre-shared key. */
1670 if (alg_a & SSL_aPSK) {
1671 char identity[PSK_MAX_IDENTITY_LEN + 1];
1672 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001673
Adam Langley1bea1732014-12-17 19:06:57 -08001674 if (s->psk_client_callback == NULL) {
1675 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1676 SSL_R_PSK_NO_CLIENT_CB);
1677 goto err;
1678 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001679
Adam Langley1bea1732014-12-17 19:06:57 -08001680 memset(identity, 0, sizeof(identity));
1681 psk_len =
1682 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1683 sizeof(identity), psk, sizeof(psk));
1684 if (psk_len > PSK_MAX_PSK_LEN) {
1685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1686 ERR_R_INTERNAL_ERROR);
1687 goto err;
1688 } else if (psk_len == 0) {
1689 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1690 SSL_R_PSK_IDENTITY_NOT_FOUND);
1691 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1692 goto err;
1693 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001694
Adam Langley1bea1732014-12-17 19:06:57 -08001695 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1696 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1697 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1698 ERR_R_INTERNAL_ERROR);
1699 goto err;
1700 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001701
Adam Langley1bea1732014-12-17 19:06:57 -08001702 if (s->session->psk_identity != NULL) {
1703 OPENSSL_free(s->session->psk_identity);
1704 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001705
Adam Langley1bea1732014-12-17 19:06:57 -08001706 s->session->psk_identity = BUF_strdup(identity);
1707 if (s->session->psk_identity == NULL) {
1708 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1709 ERR_R_MALLOC_FAILURE);
1710 goto err;
1711 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001712
Adam Langley1bea1732014-12-17 19:06:57 -08001713 /* Write out psk_identity. */
1714 s2n(identity_len, p);
1715 memcpy(p, identity, identity_len);
1716 p += identity_len;
1717 n = 2 + identity_len;
1718 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001719
Adam Langley1bea1732014-12-17 19:06:57 -08001720 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1721 if (alg_k & SSL_kRSA) {
1722 RSA *rsa;
1723 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001724
Adam Langley1bea1732014-12-17 19:06:57 -08001725 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1726 pms = OPENSSL_malloc(pms_len);
1727 if (pms == NULL) {
1728 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1729 ERR_R_MALLOC_FAILURE);
1730 goto err;
1731 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001732
Adam Langley1bea1732014-12-17 19:06:57 -08001733 if (s->session->sess_cert == NULL) {
1734 /* We should always have a server certificate with SSL_kRSA. */
1735 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1736 ERR_R_INTERNAL_ERROR);
1737 goto err;
1738 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001739
Adam Langley1bea1732014-12-17 19:06:57 -08001740 pkey = X509_get_pubkey(
1741 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1742 if (pkey == NULL ||
1743 pkey->type != EVP_PKEY_RSA ||
1744 pkey->pkey.rsa == NULL) {
1745 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1746 ERR_R_INTERNAL_ERROR);
1747 if (pkey != NULL) {
1748 EVP_PKEY_free(pkey);
1749 }
1750 goto err;
1751 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001752
Adam Langley1bea1732014-12-17 19:06:57 -08001753 rsa = pkey->pkey.rsa;
1754 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001755
Adam Langley1bea1732014-12-17 19:06:57 -08001756 pms[0] = s->client_version >> 8;
1757 pms[1] = s->client_version & 0xff;
1758 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1759 goto err;
1760 }
1761
1762 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1763
1764 q = p;
1765 /* In TLS and beyond, reserve space for the length prefix. */
1766 if (s->version > SSL3_VERSION) {
1767 p += 2;
1768 n += 2;
1769 }
1770 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1771 RSA_PKCS1_PADDING)) {
1772 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1773 SSL_R_BAD_RSA_ENCRYPT);
1774 goto err;
1775 }
1776 n += enc_pms_len;
1777
1778 /* Log the premaster secret, if logging is enabled. */
1779 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1780 pms_len)) {
1781 goto err;
1782 }
1783
1784 /* Fill in the length prefix. */
1785 if (s->version > SSL3_VERSION) {
1786 s2n(enc_pms_len, q);
1787 }
1788 } else if (alg_k & SSL_kEDH) {
1789 DH *dh_srvr, *dh_clnt;
1790 SESS_CERT *scert = s->session->sess_cert;
1791 int dh_len;
1792 size_t pub_len;
1793
1794 if (scert == NULL) {
1795 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1796 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1797 SSL_R_UNEXPECTED_MESSAGE);
1798 goto err;
1799 }
1800
1801 if (scert->peer_dh_tmp == NULL) {
1802 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1803 ERR_R_INTERNAL_ERROR);
1804 goto err;
1805 }
1806 dh_srvr = scert->peer_dh_tmp;
1807
1808 /* generate a new random key */
1809 dh_clnt = DHparams_dup(dh_srvr);
1810 if (dh_clnt == NULL) {
1811 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1812 goto err;
1813 }
1814 if (!DH_generate_key(dh_clnt)) {
1815 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1816 DH_free(dh_clnt);
1817 goto err;
1818 }
1819
1820 pms_len = DH_size(dh_clnt);
1821 pms = OPENSSL_malloc(pms_len);
1822 if (pms == NULL) {
1823 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1824 ERR_R_MALLOC_FAILURE);
1825 DH_free(dh_clnt);
1826 goto err;
1827 }
1828
1829 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1830 if (dh_len <= 0) {
1831 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1832 DH_free(dh_clnt);
1833 goto err;
1834 }
1835 pms_len = dh_len;
1836
1837 /* send off the data */
1838 pub_len = BN_num_bytes(dh_clnt->pub_key);
1839 s2n(pub_len, p);
1840 BN_bn2bin(dh_clnt->pub_key, p);
1841 n += 2 + pub_len;
1842
1843 DH_free(dh_clnt);
1844 } else if (alg_k & SSL_kEECDH) {
1845 const EC_GROUP *srvr_group = NULL;
1846 EC_KEY *tkey;
1847 int field_size = 0, ecdh_len;
1848
1849 if (s->session->sess_cert == NULL) {
1850 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1851 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1852 SSL_R_UNEXPECTED_MESSAGE);
1853 goto err;
1854 }
1855
1856 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1857 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1858 ERR_R_INTERNAL_ERROR);
1859 goto err;
1860 }
1861
1862 tkey = s->session->sess_cert->peer_ecdh_tmp;
1863
1864 srvr_group = EC_KEY_get0_group(tkey);
1865 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1866 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1867 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1868 ERR_R_INTERNAL_ERROR);
1869 goto err;
1870 }
1871
1872 clnt_ecdh = EC_KEY_new();
1873 if (clnt_ecdh == NULL) {
1874 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1875 ERR_R_MALLOC_FAILURE);
1876 goto err;
1877 }
1878
1879 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1880 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1881 goto err;
1882 }
1883
1884 /* Generate a new ECDH key pair */
1885 if (!EC_KEY_generate_key(clnt_ecdh)) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1887 goto err;
1888 }
1889
1890 field_size = EC_GROUP_get_degree(srvr_group);
1891 if (field_size <= 0) {
1892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1893 goto err;
1894 }
1895
1896 pms_len = (field_size + 7) / 8;
1897 pms = OPENSSL_malloc(pms_len);
1898 if (pms == NULL) {
1899 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1900 ERR_R_MALLOC_FAILURE);
1901 goto err;
1902 }
1903
1904 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1905 if (ecdh_len <= 0) {
1906 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1907 goto err;
1908 }
1909 pms_len = ecdh_len;
1910
1911 /* First check the size of encoding and allocate memory accordingly. */
1912 encoded_pt_len =
1913 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1914 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1915
1916 encodedPoint =
1917 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1918 bn_ctx = BN_CTX_new();
1919 if (encodedPoint == NULL || bn_ctx == NULL) {
1920 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1921 ERR_R_MALLOC_FAILURE);
1922 goto err;
1923 }
1924
1925 /* Encode the public key */
1926 encoded_pt_len = EC_POINT_point2oct(
1927 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1928 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1929
1930 *p = encoded_pt_len; /* length of encoded point */
1931 /* Encoded point will be copied here */
1932 p += 1;
1933 n += 1;
1934 /* copy the point */
1935 memcpy(p, encodedPoint, encoded_pt_len);
1936 /* increment n to account for length field */
1937 n += encoded_pt_len;
1938
1939 /* Free allocated memory */
1940 BN_CTX_free(bn_ctx);
1941 bn_ctx = NULL;
1942 OPENSSL_free(encodedPoint);
1943 encodedPoint = NULL;
1944 EC_KEY_free(clnt_ecdh);
1945 clnt_ecdh = NULL;
1946 EVP_PKEY_free(srvr_pub_pkey);
1947 srvr_pub_pkey = NULL;
1948 } else if (alg_k & SSL_kPSK) {
1949 /* For plain PSK, other_secret is a block of 0s with the same length as
1950 * the pre-shared key. */
1951 pms_len = psk_len;
1952 pms = OPENSSL_malloc(pms_len);
1953 if (pms == NULL) {
1954 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1955 ERR_R_MALLOC_FAILURE);
1956 goto err;
1957 }
1958 memset(pms, 0, pms_len);
1959 } else {
1960 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1961 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1962 ERR_R_INTERNAL_ERROR);
1963 goto err;
1964 }
1965
1966 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1967 * key. */
1968 if (alg_a & SSL_aPSK) {
1969 CBB cbb, child;
1970 uint8_t *new_pms;
1971 size_t new_pms_len;
1972
1973 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1974 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1975 ERR_R_MALLOC_FAILURE);
1976 goto err;
1977 }
1978 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1979 !CBB_add_bytes(&child, pms, pms_len) ||
1980 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1981 !CBB_add_bytes(&child, psk, psk_len) ||
1982 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1983 CBB_cleanup(&cbb);
1984 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1985 ERR_R_INTERNAL_ERROR);
1986 goto err;
1987 }
1988 OPENSSL_cleanse(pms, pms_len);
1989 OPENSSL_free(pms);
1990 pms = new_pms;
1991 pms_len = new_pms_len;
1992 }
1993
1994 /* The message must be added to the finished hash before calculating the
1995 * master secret. */
1996 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
1997 s->state = SSL3_ST_CW_KEY_EXCH_B;
1998
1999 s->session->master_key_length = s->enc_method->generate_master_secret(
2000 s, s->session->master_key, pms, pms_len);
2001 if (s->session->master_key_length == 0) {
2002 goto err;
2003 }
2004 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2005 OPENSSL_cleanse(pms, pms_len);
2006 OPENSSL_free(pms);
2007 }
2008
2009 /* SSL3_ST_CW_KEY_EXCH_B */
2010 return s->enc_method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002011
2012err:
Adam Langley1bea1732014-12-17 19:06:57 -08002013 BN_CTX_free(bn_ctx);
2014 if (encodedPoint != NULL) {
2015 OPENSSL_free(encodedPoint);
2016 }
2017 if (clnt_ecdh != NULL) {
2018 EC_KEY_free(clnt_ecdh);
2019 }
2020 EVP_PKEY_free(srvr_pub_pkey);
2021 if (pms) {
2022 OPENSSL_cleanse(pms, pms_len);
2023 OPENSSL_free(pms);
2024 }
2025 return -1;
2026}
Adam Langley1258b6a2014-06-20 12:00:00 -07002027
Adam Langley1bea1732014-12-17 19:06:57 -08002028int ssl3_send_cert_verify(SSL *s) {
2029 uint8_t *buf, *p;
2030 const EVP_MD *md = NULL;
2031 uint8_t digest[EVP_MAX_MD_SIZE];
2032 size_t digest_length;
2033 EVP_PKEY *pkey;
2034 EVP_PKEY_CTX *pctx = NULL;
2035 size_t signature_length = 0;
2036 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002037
Adam Langley1bea1732014-12-17 19:06:57 -08002038 buf = (uint8_t *)s->init_buf->data;
2039
2040 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2041 p = ssl_handshake_start(s);
2042 pkey = s->cert->key->privatekey;
2043
2044 /* Write out the digest type if needbe. */
2045 if (SSL_USE_SIGALGS(s)) {
2046 md = tls1_choose_signing_digest(s, pkey);
2047 if (!tls12_get_sigandhash(p, pkey, md)) {
2048 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2049 goto err;
2050 }
2051 p += 2;
2052 n += 2;
2053 }
2054
2055 /* Compute the digest. */
2056 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2057 goto err;
2058 }
2059
2060 /* The handshake buffer is no longer necessary. */
2061 if (s->s3->handshake_buffer &&
2062 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2063 goto err;
2064 }
2065
2066 /* Sign the digest. */
2067 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2068 if (pctx == NULL) {
2069 goto err;
2070 }
2071
2072 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2073 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2074 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2075 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2076 goto err;
2077 }
2078
2079 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2080 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2081 goto err;
2082 }
2083
2084 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2085 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2086 goto err;
2087 }
2088
2089 s2n(signature_length, p);
2090 n += signature_length + 2;
2091
2092 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2093 s->state = SSL3_ST_CW_CERT_VRFY_B;
2094 }
2095
2096 EVP_PKEY_CTX_free(pctx);
2097 return ssl_do_write(s);
2098
2099err:
2100 EVP_PKEY_CTX_free(pctx);
2101 return -1;
2102}
2103
2104/* ssl3_has_client_certificate returns true if a client certificate is
2105 * configured. */
2106static int ssl3_has_client_certificate(SSL *s) {
2107 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2108}
2109
2110int ssl3_send_client_certificate(SSL *s) {
2111 X509 *x509 = NULL;
2112 EVP_PKEY *pkey = NULL;
2113 int i;
2114
2115 if (s->state == SSL3_ST_CW_CERT_A) {
2116 /* Let cert callback update client certificates if required */
2117 if (s->cert->cert_cb) {
2118 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2119 if (i < 0) {
2120 s->rwstate = SSL_X509_LOOKUP;
2121 return -1;
2122 }
2123 if (i == 0) {
2124 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2125 return 0;
2126 }
2127 s->rwstate = SSL_NOTHING;
2128 }
2129
2130 if (ssl3_has_client_certificate(s)) {
2131 s->state = SSL3_ST_CW_CERT_C;
2132 } else {
2133 s->state = SSL3_ST_CW_CERT_B;
2134 }
2135 }
2136
2137 /* We need to get a client cert */
2138 if (s->state == SSL3_ST_CW_CERT_B) {
2139 /* If we get an error, we need to:
2140 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2141 * We then get retried later */
2142 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2143 if (i < 0) {
2144 s->rwstate = SSL_X509_LOOKUP;
2145 return -1;
2146 }
2147 s->rwstate = SSL_NOTHING;
2148 if (i == 1 && pkey != NULL && x509 != NULL) {
2149 s->state = SSL3_ST_CW_CERT_B;
2150 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2151 i = 0;
2152 }
2153 } else if (i == 1) {
2154 i = 0;
2155 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2156 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2157 }
2158
2159 if (x509 != NULL) {
2160 X509_free(x509);
2161 }
2162 if (pkey != NULL) {
2163 EVP_PKEY_free(pkey);
2164 }
2165 if (i && !ssl3_has_client_certificate(s)) {
2166 i = 0;
2167 }
2168 if (i == 0) {
2169 if (s->version == SSL3_VERSION) {
2170 s->s3->tmp.cert_req = 0;
2171 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2172 return 1;
2173 } else {
2174 s->s3->tmp.cert_req = 2;
2175 }
2176 }
2177
2178 /* Ok, we have a cert */
2179 s->state = SSL3_ST_CW_CERT_C;
2180 }
2181
2182 if (s->state == SSL3_ST_CW_CERT_C) {
2183 s->state = SSL3_ST_CW_CERT_D;
2184 ssl3_output_cert_chain(s, (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key);
2185 }
2186
2187 /* SSL3_ST_CW_CERT_D */
2188 return ssl_do_write(s);
2189}
2190
2191#define has_bits(i, m) (((i) & (m)) == (m))
2192
2193int ssl3_check_cert_and_algorithm(SSL *s) {
2194 int i, idx;
2195 long alg_k, alg_a;
2196 EVP_PKEY *pkey = NULL;
2197 SESS_CERT *sc;
2198 DH *dh;
2199
2200 /* we don't have a certificate */
2201 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2202 return 1;
2203 }
2204
2205 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2206 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2207
2208 sc = s->session->sess_cert;
2209 if (sc == NULL) {
2210 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2211 goto err;
2212 }
2213
2214 dh = s->session->sess_cert->peer_dh_tmp;
2215
2216 /* This is the passed certificate */
2217
2218 idx = sc->peer_cert_type;
2219 if (idx == SSL_PKEY_ECC) {
2220 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2221 /* check failed */
2222 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2223 goto f_err;
2224 } else {
2225 return 1;
2226 }
2227 } else if (alg_a & SSL_aECDSA) {
2228 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2229 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2230 goto f_err;
2231 }
2232 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2233 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2234 EVP_PKEY_free(pkey);
2235
2236 /* Check that we have a certificate if we require one */
2237 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2238 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2239 SSL_R_MISSING_RSA_SIGNING_CERT);
2240 goto f_err;
2241 }
2242
2243 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2244 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2245 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2246 goto f_err;
2247 }
2248
2249 if ((alg_k & SSL_kEDH) &&
2250 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2251 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2252 goto f_err;
2253 }
2254
2255 return 1;
2256
2257f_err:
2258 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2259err:
2260 return 0;
2261}
2262
2263int ssl3_send_next_proto(SSL *s) {
2264 unsigned int len, padding_len;
2265 uint8_t *d, *p;
2266
2267 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2268 len = s->next_proto_negotiated_len;
2269 padding_len = 32 - ((len + 2) % 32);
2270
2271 d = p = ssl_handshake_start(s);
2272 *(p++) = len;
2273 memcpy(p, s->next_proto_negotiated, len);
2274 p += len;
2275 *(p++) = padding_len;
2276 memset(p, 0, padding_len);
2277 p += padding_len;
2278
2279 ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d);
2280 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2281 }
2282
2283 return ssl_do_write(s);
2284}
2285
2286int ssl3_send_channel_id(SSL *s) {
2287 uint8_t *d;
2288 int ret = -1, public_key_len;
2289 EVP_MD_CTX md_ctx;
2290 size_t sig_len;
2291 ECDSA_SIG *sig = NULL;
2292 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2293
2294 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2295 return ssl_do_write(s);
2296 }
2297
2298 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2299 EVP_PKEY *key = NULL;
2300 s->ctx->channel_id_cb(s, &key);
2301 if (key != NULL) {
2302 s->tlsext_channel_id_private = key;
2303 }
2304 }
2305
2306 if (!s->tlsext_channel_id_private) {
2307 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2308 return -1;
2309 }
2310 s->rwstate = SSL_NOTHING;
2311
2312 d = ssl_handshake_start(s);
2313 if (s->s3->tlsext_channel_id_new) {
2314 s2n(TLSEXT_TYPE_channel_id_new, d);
2315 } else {
2316 s2n(TLSEXT_TYPE_channel_id, d);
2317 }
2318 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2319
2320 EVP_MD_CTX_init(&md_ctx);
2321
2322 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2323 if (public_key_len <= 0) {
2324 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2325 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2326 goto err;
2327 }
2328
2329 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2330 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2331 * field elements as 32-byte, big-endian numbers. */
2332 if (public_key_len != 65) {
2333 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2334 goto err;
2335 }
2336 public_key = OPENSSL_malloc(public_key_len);
2337 if (!public_key) {
2338 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2339 goto err;
2340 }
2341
2342 derp = public_key;
2343 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2344
2345 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2346 s->tlsext_channel_id_private) != 1) {
2347 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2348 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2349 goto err;
2350 }
2351
2352 if (!tls1_channel_id_hash(&md_ctx, s)) {
2353 goto err;
2354 }
2355
2356 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2357 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2358 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2359 goto err;
2360 }
2361
2362 der_sig = OPENSSL_malloc(sig_len);
2363 if (!der_sig) {
2364 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2365 goto err;
2366 }
2367
2368 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2369 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2370 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2371 goto err;
2372 }
2373
2374 derp = der_sig;
2375 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2376 if (sig == NULL) {
2377 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2378 goto err;
2379 }
2380
2381 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2382 memcpy(d, public_key + 1, 64);
2383 d += 64;
2384 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2385 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2386 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2387 goto err;
2388 }
2389
2390 ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2391 2 + 2 + TLSEXT_CHANNEL_ID_SIZE);
2392 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2393
2394 ret = ssl_do_write(s);
2395
2396err:
2397 EVP_MD_CTX_cleanup(&md_ctx);
2398 if (public_key) {
2399 OPENSSL_free(public_key);
2400 }
2401 if (der_sig) {
2402 OPENSSL_free(der_sig);
2403 }
2404 if (sig) {
2405 ECDSA_SIG_free(sig);
2406 }
2407
2408 return ret;
2409}
2410
2411int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2412 int i = 0;
2413 if (s->ctx->client_cert_cb) {
2414 i = s->ctx->client_cert_cb(s, px509, ppkey);
2415 }
2416 return i;
2417}