blob: c6752ebeb4194c6df173cf57c0f5687e446ac336 [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:
David Benjamin6eb000d2015-02-11 01:17:41 -0500202 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800203 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley1bea1732014-12-17 19:06:57 -0800206 if (s->init_buf == NULL) {
207 buf = BUF_MEM_new();
208 if (buf == NULL ||
209 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
210 ret = -1;
211 goto end;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langley1bea1732014-12-17 19:06:57 -0800214 s->init_buf = buf;
215 buf = NULL;
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley1bea1732014-12-17 19:06:57 -0800218 if (!ssl3_setup_buffers(s) ||
219 !ssl_init_wbio_buffer(s, 0)) {
220 ret = -1;
221 goto end;
222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley1bea1732014-12-17 19:06:57 -0800224 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langley1bea1732014-12-17 19:06:57 -0800226 if (!ssl3_init_finished_mac(s)) {
227 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
228 ret = -1;
229 goto end;
230 }
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langley1bea1732014-12-17 19:06:57 -0800232 s->state = SSL3_ST_CW_CLNT_HELLO_A;
233 s->ctx->stats.sess_connect++;
234 s->init_num = 0;
235 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langley1bea1732014-12-17 19:06:57 -0800237 case SSL3_ST_CW_CLNT_HELLO_A:
238 case SSL3_ST_CW_CLNT_HELLO_B:
239 s->shutdown = 0;
240 ret = ssl3_send_client_hello(s);
241 if (ret <= 0) {
242 goto end;
243 }
244 s->state = SSL3_ST_CR_SRVR_HELLO_A;
245 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley1bea1732014-12-17 19:06:57 -0800247 /* turn on buffering for the next lot of output */
248 if (s->bbio != s->wbio) {
249 s->wbio = BIO_push(s->bbio, s->wbio);
250 }
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langley1bea1732014-12-17 19:06:57 -0800252 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley1bea1732014-12-17 19:06:57 -0800254 case SSL3_ST_CR_SRVR_HELLO_A:
255 case SSL3_ST_CR_SRVR_HELLO_B:
256 ret = ssl3_get_server_hello(s);
257 if (ret <= 0) {
258 goto end;
259 }
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langley1bea1732014-12-17 19:06:57 -0800261 if (s->hit) {
262 s->state = SSL3_ST_CR_CHANGE;
263 if (s->tlsext_ticket_expected) {
264 /* receive renewed session ticket */
265 s->state = SSL3_ST_CR_SESSION_TICKET_A;
266 }
267 } else {
268 s->state = SSL3_ST_CR_CERT_A;
269 }
270 s->init_num = 0;
271 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400272
Adam Langley1bea1732014-12-17 19:06:57 -0800273 case SSL3_ST_CR_CERT_A:
274 case SSL3_ST_CR_CERT_B:
275 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
276 ret = ssl3_get_server_certificate(s);
277 if (ret <= 0) {
278 goto end;
279 }
280 if (s->s3->tmp.certificate_status_expected) {
281 s->state = SSL3_ST_CR_CERT_STATUS_A;
282 } else {
283 s->state = SSL3_ST_CR_KEY_EXCH_A;
284 }
285 } else {
286 skip = 1;
287 s->state = SSL3_ST_CR_KEY_EXCH_A;
288 }
289 s->init_num = 0;
290 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley1bea1732014-12-17 19:06:57 -0800292 case SSL3_ST_CR_KEY_EXCH_A:
293 case SSL3_ST_CR_KEY_EXCH_B:
294 ret = ssl3_get_server_key_exchange(s);
295 if (ret <= 0) {
296 goto end;
297 }
298 s->state = SSL3_ST_CR_CERT_REQ_A;
299 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700300
Adam Langley1bea1732014-12-17 19:06:57 -0800301 /* at this point we check that we have the
302 * required stuff from the server */
303 if (!ssl3_check_cert_and_algorithm(s)) {
304 ret = -1;
305 goto end;
306 }
307 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley1bea1732014-12-17 19:06:57 -0800309 case SSL3_ST_CR_CERT_REQ_A:
310 case SSL3_ST_CR_CERT_REQ_B:
311 ret = ssl3_get_certificate_request(s);
312 if (ret <= 0) {
313 goto end;
314 }
315 s->state = SSL3_ST_CR_SRVR_DONE_A;
316 s->init_num = 0;
317 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley1bea1732014-12-17 19:06:57 -0800319 case SSL3_ST_CR_SRVR_DONE_A:
320 case SSL3_ST_CR_SRVR_DONE_B:
321 ret = ssl3_get_server_done(s);
322 if (ret <= 0) {
323 goto end;
324 }
325 if (s->s3->tmp.cert_req) {
326 s->state = SSL3_ST_CW_CERT_A;
327 } else {
328 s->state = SSL3_ST_CW_KEY_EXCH_A;
329 }
330 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langley1bea1732014-12-17 19:06:57 -0800332 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700333
Adam Langley1bea1732014-12-17 19:06:57 -0800334 case SSL3_ST_CW_CERT_A:
335 case SSL3_ST_CW_CERT_B:
336 case SSL3_ST_CW_CERT_C:
337 case SSL3_ST_CW_CERT_D:
338 ret = ssl3_send_client_certificate(s);
339 if (ret <= 0) {
340 goto end;
341 }
342 s->state = SSL3_ST_CW_KEY_EXCH_A;
343 s->init_num = 0;
344 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley1bea1732014-12-17 19:06:57 -0800346 case SSL3_ST_CW_KEY_EXCH_A:
347 case SSL3_ST_CW_KEY_EXCH_B:
348 ret = ssl3_send_client_key_exchange(s);
349 if (ret <= 0) {
350 goto end;
351 }
352 /* For TLS, cert_req is set to 2, so a cert chain
353 * of nothing is sent, but no verify packet is sent */
354 if (s->s3->tmp.cert_req == 1) {
355 s->state = SSL3_ST_CW_CERT_VRFY_A;
356 } else {
357 s->state = SSL3_ST_CW_CHANGE_A;
358 s->s3->change_cipher_spec = 0;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langley1bea1732014-12-17 19:06:57 -0800361 s->init_num = 0;
362 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley1bea1732014-12-17 19:06:57 -0800364 case SSL3_ST_CW_CERT_VRFY_A:
365 case SSL3_ST_CW_CERT_VRFY_B:
366 ret = ssl3_send_cert_verify(s);
367 if (ret <= 0) {
368 goto end;
369 }
370 s->state = SSL3_ST_CW_CHANGE_A;
371 s->init_num = 0;
372 s->s3->change_cipher_spec = 0;
373 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langley1bea1732014-12-17 19:06:57 -0800375 case SSL3_ST_CW_CHANGE_A:
376 case SSL3_ST_CW_CHANGE_B:
377 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
378 SSL3_ST_CW_CHANGE_B);
379 if (ret <= 0) {
380 goto end;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langley1bea1732014-12-17 19:06:57 -0800383 s->state = SSL3_ST_CW_FINISHED_A;
384 if (s->s3->tlsext_channel_id_valid) {
385 s->state = SSL3_ST_CW_CHANNEL_ID_A;
386 }
387 if (s->s3->next_proto_neg_seen) {
388 s->state = SSL3_ST_CW_NEXT_PROTO_A;
389 }
390 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley1bea1732014-12-17 19:06:57 -0800392 s->session->cipher = s->s3->tmp.new_cipher;
393 if (!s->enc_method->setup_key_block(s)) {
394 ret = -1;
395 goto end;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley1bea1732014-12-17 19:06:57 -0800398 if (!s->enc_method->change_cipher_state(
399 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
400 ret = -1;
401 goto end;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley1bea1732014-12-17 19:06:57 -0800404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley1bea1732014-12-17 19:06:57 -0800406 case SSL3_ST_CW_NEXT_PROTO_A:
407 case SSL3_ST_CW_NEXT_PROTO_B:
408 ret = ssl3_send_next_proto(s);
409 if (ret <= 0) {
410 goto end;
411 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700412
Adam Langley1bea1732014-12-17 19:06:57 -0800413 if (s->s3->tlsext_channel_id_valid) {
414 s->state = SSL3_ST_CW_CHANNEL_ID_A;
415 } else {
416 s->state = SSL3_ST_CW_FINISHED_A;
417 }
418 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langley1bea1732014-12-17 19:06:57 -0800420 case SSL3_ST_CW_CHANNEL_ID_A:
421 case SSL3_ST_CW_CHANNEL_ID_B:
422 ret = ssl3_send_channel_id(s);
423 if (ret <= 0) {
424 goto end;
425 }
426 s->state = SSL3_ST_CW_FINISHED_A;
427 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley1bea1732014-12-17 19:06:57 -0800429 case SSL3_ST_CW_FINISHED_A:
430 case SSL3_ST_CW_FINISHED_B:
431 ret =
432 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
433 s->enc_method->client_finished_label,
434 s->enc_method->client_finished_label_len);
435 if (ret <= 0) {
436 goto end;
437 }
438 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langley1bea1732014-12-17 19:06:57 -0800440 if (s->hit) {
441 s->s3->tmp.next_state = SSL_ST_OK;
442 } else {
443 /* This is a non-resumption handshake. If it involves ChannelID, then
444 * record the handshake hashes at this point in the session so that
445 * any resumption of this session with ChannelID can sign those
446 * hashes. */
447 if (s->s3->tlsext_channel_id_new) {
448 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500449 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800450 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500451 }
Adam Langley1bea1732014-12-17 19:06:57 -0800452 }
453 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) &&
454 ssl3_can_cutthrough(s) &&
455 /* no cutthrough on renegotiation (would complicate the state
456 * machine) */
457 s->s3->previous_server_finished_len == 0) {
458 s->s3->tmp.next_state = SSL3_ST_CUTTHROUGH_COMPLETE;
459 } else {
460 /* Allow NewSessionTicket if ticket expected */
461 if (s->tlsext_ticket_expected) {
462 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
463 } else {
464 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
465 }
466 }
467 }
468 s->init_num = 0;
469 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langley1bea1732014-12-17 19:06:57 -0800471 case SSL3_ST_CR_SESSION_TICKET_A:
472 case SSL3_ST_CR_SESSION_TICKET_B:
473 ret = ssl3_get_new_session_ticket(s);
474 if (ret <= 0) {
475 goto end;
476 }
477 s->state = SSL3_ST_CR_CHANGE;
478 s->init_num = 0;
479 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700480
Adam Langley1bea1732014-12-17 19:06:57 -0800481 case SSL3_ST_CR_CERT_STATUS_A:
482 case SSL3_ST_CR_CERT_STATUS_B:
483 ret = ssl3_get_cert_status(s);
484 if (ret <= 0) {
485 goto end;
486 }
487 s->state = SSL3_ST_CR_KEY_EXCH_A;
488 s->init_num = 0;
489 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400490
Adam Langley1bea1732014-12-17 19:06:57 -0800491 case SSL3_ST_CR_CHANGE:
492 /* At this point, the next message must be entirely behind a
493 * ChangeCipherSpec. */
494 if (!ssl3_expect_change_cipher_spec(s)) {
495 ret = -1;
496 goto end;
497 }
498 s->state = SSL3_ST_CR_FINISHED_A;
499 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley1bea1732014-12-17 19:06:57 -0800501 case SSL3_ST_CR_FINISHED_A:
502 case SSL3_ST_CR_FINISHED_B:
503 ret =
504 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
505 if (ret <= 0) {
506 goto end;
507 }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langley1bea1732014-12-17 19:06:57 -0800509 if (s->hit) {
510 s->state = SSL3_ST_CW_CHANGE_A;
511 } else {
512 s->state = SSL_ST_OK;
513 }
514 s->init_num = 0;
515 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langley1bea1732014-12-17 19:06:57 -0800517 case SSL3_ST_CW_FLUSH:
518 s->rwstate = SSL_WRITING;
519 if (BIO_flush(s->wbio) <= 0) {
520 ret = -1;
521 goto end;
522 }
523 s->rwstate = SSL_NOTHING;
524 s->state = s->s3->tmp.next_state;
525 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700526
Adam Langley1bea1732014-12-17 19:06:57 -0800527 case SSL3_ST_CUTTHROUGH_COMPLETE:
528 /* Allow NewSessionTicket if ticket expected */
529 if (s->tlsext_ticket_expected) {
530 s->state = SSL3_ST_CR_SESSION_TICKET_A;
531 } else {
532 s->state = SSL3_ST_CR_CHANGE;
533 }
David Benjaminccf74f82015-02-09 00:01:31 -0500534 s->s3->tmp.cutthrough_complete = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 ssl_free_wbio_buffer(s);
537 ret = 1;
538 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 case SSL_ST_OK:
541 /* clean a few things up */
542 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langley1bea1732014-12-17 19:06:57 -0800544 if (s->init_buf != NULL) {
545 BUF_MEM_free(s->init_buf);
546 s->init_buf = NULL;
547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley1bea1732014-12-17 19:06:57 -0800549 /* Remove write buffering now. */
550 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langley1bea1732014-12-17 19:06:57 -0800552 s->init_num = 0;
553 s->renegotiate = 0;
554 s->new_session = 0;
David Benjaminccf74f82015-02-09 00:01:31 -0500555 s->s3->tmp.cutthrough_complete = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langley1bea1732014-12-17 19:06:57 -0800557 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
558 if (s->hit) {
559 s->ctx->stats.sess_hit++;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langley1bea1732014-12-17 19:06:57 -0800562 ret = 1;
563 /* s->server=0; */
564 s->ctx->stats.sess_connect_good++;
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langley1bea1732014-12-17 19:06:57 -0800566 if (cb != NULL) {
567 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langley1bea1732014-12-17 19:06:57 -0800570 goto end;
571
572 default:
573 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
574 ret = -1;
575 goto end;
576 }
577
578 if (!s->s3->tmp.reuse_message && !skip) {
579 if (cb != NULL && s->state != state) {
580 new_state = s->state;
581 s->state = state;
582 cb(s, SSL_CB_CONNECT_LOOP, 1);
583 s->state = new_state;
584 }
585 }
586 skip = 0;
587 }
588
Adam Langley95c29f32014-06-20 12:00:00 -0700589end:
Adam Langley1bea1732014-12-17 19:06:57 -0800590 s->in_handshake--;
591 if (buf != NULL) {
592 BUF_MEM_free(buf);
593 }
594 if (cb != NULL) {
595 cb(s, SSL_CB_CONNECT_EXIT, ret);
596 }
597 return ret;
598}
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley1bea1732014-12-17 19:06:57 -0800600int ssl3_send_client_hello(SSL *s) {
601 uint8_t *buf, *p, *d;
602 int i;
603 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langley1bea1732014-12-17 19:06:57 -0800605 buf = (uint8_t *)s->init_buf->data;
606 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
607 if (!s->s3->have_version) {
608 uint16_t max_version = ssl3_get_max_client_version(s);
609 /* Disabling all versions is silly: return an error. */
610 if (max_version == 0) {
611 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
612 goto err;
613 }
614 s->version = max_version;
615 s->client_version = max_version;
616 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500617
Adam Langley1bea1732014-12-17 19:06:57 -0800618 /* If the configured session was created at a version higher than our
619 * maximum version, drop it. */
620 if (s->session &&
621 (s->session->session_id_length == 0 || s->session->not_resumable ||
622 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
623 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
624 SSL_set_session(s, NULL);
625 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500626
Adam Langley1bea1732014-12-17 19:06:57 -0800627 /* else use the pre-loaded session */
628 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langley1bea1732014-12-17 19:06:57 -0800630 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
631 * renegerate the client_random. The random must be reused. */
632 if (!SSL_IS_DTLS(s) || !s->d1->send_cookie) {
633 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
634 }
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley1bea1732014-12-17 19:06:57 -0800636 /* Do the message type and length last. Note: the final argument to
637 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
638 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langley1bea1732014-12-17 19:06:57 -0800640 /* version indicates the negotiated version: for example from an SSLv2/v3
641 * compatible client hello). The client_version field is the maximum
642 * version we permit and it is also used in RSA encrypted premaster
643 * secrets. Some servers can choke if we initially report a higher version
644 * then renegotiate to a lower one in the premaster secret. This didn't
645 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
646 * or later if the server only supports 1.0.
647 *
648 * Possible scenario with previous logic:
649 * 1. Client hello indicates TLS 1.2
650 * 2. Server hello says TLS 1.0
651 * 3. RSA encrypted premaster secret uses 1.2.
652 * 4. Handhaked proceeds using TLS 1.0.
653 * 5. Server sends hello request to renegotiate.
654 * 6. Client hello indicates TLS v1.0 as we now
655 * know that is maximum server supports.
656 * 7. Server chokes on RSA encrypted premaster secret
657 * containing version 1.0.
658 *
659 * For interoperability it should be OK to always use the maximum version
660 * we support in client hello and then rely on the checking of version to
661 * ensure the servers isn't being inconsistent: for example initially
662 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
663 * using client_version in client hello and not resetting it to the
664 * negotiated version. */
665 *(p++) = s->client_version >> 8;
666 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langley1bea1732014-12-17 19:06:57 -0800668 /* Random stuff */
669 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
670 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langley1bea1732014-12-17 19:06:57 -0800672 /* Session ID */
673 if (s->new_session || s->session == NULL) {
674 i = 0;
675 } else {
676 i = s->session->session_id_length;
677 }
678 *(p++) = i;
679 if (i != 0) {
680 if (i > (int)sizeof(s->session->session_id)) {
681 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
682 goto err;
683 }
684 memcpy(p, s->session->session_id, i);
685 p += i;
686 }
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langley1bea1732014-12-17 19:06:57 -0800688 /* cookie stuff for DTLS */
689 if (SSL_IS_DTLS(s)) {
690 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
691 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
692 goto err;
693 }
694 *(p++) = s->d1->cookie_len;
695 memcpy(p, s->d1->cookie, s->d1->cookie_len);
696 p += s->d1->cookie_len;
697 }
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langley1bea1732014-12-17 19:06:57 -0800699 /* Ciphers supported */
700 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
701 if (i == 0) {
702 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
703 SSL_R_NO_CIPHERS_AVAILABLE);
704 goto err;
705 }
706 s2n(i, p);
707 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley1bea1732014-12-17 19:06:57 -0800709 /* COMPRESSION */
710 *(p++) = 1;
711 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langley1bea1732014-12-17 19:06:57 -0800713 /* TLS extensions*/
714 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
715 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
716 goto err;
717 }
718
719 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
720 p - buf);
721 if (p == NULL) {
722 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
723 goto err;
724 }
725
726 l = p - d;
727 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
728 s->state = SSL3_ST_CW_CLNT_HELLO_B;
729 }
730
731 /* SSL3_ST_CW_CLNT_HELLO_B */
732 return ssl_do_write(s);
733
Adam Langley95c29f32014-06-20 12:00:00 -0700734err:
Adam Langley1bea1732014-12-17 19:06:57 -0800735 return -1;
736}
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langley1bea1732014-12-17 19:06:57 -0800738int ssl3_get_server_hello(SSL *s) {
739 STACK_OF(SSL_CIPHER) * sk;
740 const SSL_CIPHER *c;
741 CERT *ct = s->cert;
742 int al = SSL_AD_INTERNAL_ERROR, ok;
743 long n;
744 CBS server_hello, server_random, session_id;
745 uint16_t server_version, cipher_suite;
746 uint8_t compression_method;
747 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langley1bea1732014-12-17 19:06:57 -0800749 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
750 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
751 20000, /* ?? */
752 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langley1bea1732014-12-17 19:06:57 -0800754 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500755 uint32_t err = ERR_peek_error();
756 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
757 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
758 /* Add a dedicated error code to the queue for a handshake_failure alert
759 * in response to ClientHello. This matches NSS's client behavior and
760 * gives a better error on a (probable) failure to negotiate initial
761 * parameters. Note: this error code comes after the original one.
762 *
763 * See https://crbug.com/446505. */
764 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
765 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
766 }
Adam Langley1bea1732014-12-17 19:06:57 -0800767 return n;
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langley1bea1732014-12-17 19:06:57 -0800770 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400771
Adam Langley1bea1732014-12-17 19:06:57 -0800772 if (!CBS_get_u16(&server_hello, &server_version) ||
773 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
774 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
775 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
776 !CBS_get_u16(&server_hello, &cipher_suite) ||
777 !CBS_get_u8(&server_hello, &compression_method)) {
778 al = SSL_AD_DECODE_ERROR;
779 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
780 goto f_err;
781 }
David Benjamina03d95d2014-07-12 19:49:07 -0400782
Adam Langley1bea1732014-12-17 19:06:57 -0800783 if (!s->s3->have_version) {
784 if (!ssl3_is_version_enabled(s, server_version)) {
785 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
786 s->version = server_version;
787 /* Mark the version as fixed so the record-layer version is not clamped
788 * to TLS 1.0. */
789 s->s3->have_version = 1;
790 al = SSL_AD_PROTOCOL_VERSION;
791 goto f_err;
792 }
793 s->version = server_version;
794 s->enc_method = ssl3_get_enc_method(server_version);
795 assert(s->enc_method != NULL);
796 /* At this point, the connection's version is known and s->version is
797 * fixed. Begin enforcing the record-layer version. */
798 s->s3->have_version = 1;
799 } else if (server_version != s->version) {
800 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
801 al = SSL_AD_PROTOCOL_VERSION;
802 goto f_err;
803 }
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langley1bea1732014-12-17 19:06:57 -0800805 /* Copy over the server random. */
806 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langley1bea1732014-12-17 19:06:57 -0800808 assert(s->session == NULL || s->session->session_id_length > 0);
809 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
810 s->session->session_id_length)) {
811 if (s->sid_ctx_length != s->session->sid_ctx_length ||
812 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
813 /* actually a client application bug */
814 al = SSL_AD_ILLEGAL_PARAMETER;
815 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
816 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
817 goto f_err;
818 }
819 s->hit = 1;
820 } else {
821 /* The session wasn't resumed. Create a fresh SSL_SESSION to
822 * fill out. */
823 s->hit = 0;
824 if (!ssl_get_new_session(s, 0)) {
825 goto f_err;
826 }
827 /* Note: session_id could be empty. */
828 s->session->session_id_length = CBS_len(&session_id);
829 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
830 }
David Benjamina03d95d2014-07-12 19:49:07 -0400831
Adam Langley1bea1732014-12-17 19:06:57 -0800832 c = ssl3_get_cipher_by_value(cipher_suite);
833 if (c == NULL) {
834 /* unknown cipher */
835 al = SSL_AD_ILLEGAL_PARAMETER;
836 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
837 SSL_R_UNKNOWN_CIPHER_RETURNED);
838 goto f_err;
839 }
840 /* ct->mask_ssl was computed from client capabilities. Now
841 * that the final version is known, compute a new mask_ssl. */
842 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
843 mask_ssl = SSL_TLSV1_2;
844 } else {
845 mask_ssl = 0;
846 }
847 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
848 * the server selected it, it's an error. */
849 if ((c->algorithm_ssl & mask_ssl) ||
850 (c->algorithm_mkey & ct->mask_k) ||
851 (c->algorithm_auth & ct->mask_a)) {
852 al = SSL_AD_ILLEGAL_PARAMETER;
853 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
854 goto f_err;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langley1bea1732014-12-17 19:06:57 -0800857 sk = ssl_get_ciphers_by_id(s);
858 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
859 /* we did not say we would use this cipher */
860 al = SSL_AD_ILLEGAL_PARAMETER;
861 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
862 goto f_err;
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
David Benjaminf3a8b122014-12-25 23:11:49 -0500865 if (s->hit && s->session->cipher != c) {
Adam Langley1bea1732014-12-17 19:06:57 -0800866 al = SSL_AD_ILLEGAL_PARAMETER;
867 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
868 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
869 goto f_err;
870 }
871 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500872
Adam Langley1bea1732014-12-17 19:06:57 -0800873 /* Most clients also require that the negotiated version match the session's
874 * version if resuming. However OpenSSL has historically not had the
875 * corresponding logic on the server, so this may not be compatible,
876 * depending on other factors. (Whether the ClientHello version is clamped to
877 * the session's version and whether the session cache is keyed on IP
878 * address.)
879 *
880 * TODO(davidben): See if we can still enforce this? Perhaps for the future
881 * TLS 1.3 and forward if this is fixed upstream. */
David Benjaminbdf5e722014-11-11 00:52:15 -0500882
Adam Langley1bea1732014-12-17 19:06:57 -0800883 /* Don't digest cached records if no sigalgs: we may need them for client
884 * authentication. */
885 if (!SSL_USE_SIGALGS(s) &&
886 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
887 goto f_err;
888 }
David Benjamina03d95d2014-07-12 19:49:07 -0400889
Adam Langley1bea1732014-12-17 19:06:57 -0800890 /* Only the NULL compression algorithm is supported. */
891 if (compression_method != 0) {
892 al = SSL_AD_ILLEGAL_PARAMETER;
893 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
894 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
895 goto f_err;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langley1bea1732014-12-17 19:06:57 -0800898 /* TLS extensions */
899 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
900 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
901 goto err;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langley1bea1732014-12-17 19:06:57 -0800904 /* There should be nothing left over in the record. */
905 if (CBS_len(&server_hello) != 0) {
906 /* wrong packet length */
907 al = SSL_AD_DECODE_ERROR;
908 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
909 goto f_err;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langley1bea1732014-12-17 19:06:57 -0800912 return 1;
913
Adam Langley95c29f32014-06-20 12:00:00 -0700914f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800915 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700916err:
Adam Langley1bea1732014-12-17 19:06:57 -0800917 return -1;
918}
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langley1bea1732014-12-17 19:06:57 -0800920int ssl3_get_server_certificate(SSL *s) {
921 int al, i, ok, ret = -1;
922 unsigned long n;
923 X509 *x = NULL;
924 STACK_OF(X509) *sk = NULL;
925 SESS_CERT *sc;
926 EVP_PKEY *pkey = NULL;
927 CBS cbs, certificate_list;
928 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langley1bea1732014-12-17 19:06:57 -0800930 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
931 SSL3_MT_CERTIFICATE, s->max_cert_list,
932 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langley1bea1732014-12-17 19:06:57 -0800934 if (!ok) {
935 return n;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langley1bea1732014-12-17 19:06:57 -0800938 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langley1bea1732014-12-17 19:06:57 -0800940 sk = sk_X509_new_null();
941 if (sk == NULL) {
942 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
943 goto err;
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langley1bea1732014-12-17 19:06:57 -0800946 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
947 CBS_len(&cbs) != 0) {
948 al = SSL_AD_DECODE_ERROR;
949 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
950 goto f_err;
951 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400952
Adam Langley1bea1732014-12-17 19:06:57 -0800953 while (CBS_len(&certificate_list) > 0) {
954 CBS certificate;
955 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
956 al = SSL_AD_DECODE_ERROR;
957 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
958 SSL_R_CERT_LENGTH_MISMATCH);
959 goto f_err;
960 }
961 data = CBS_data(&certificate);
962 x = d2i_X509(NULL, &data, CBS_len(&certificate));
963 if (x == NULL) {
964 al = SSL_AD_BAD_CERTIFICATE;
965 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
966 goto f_err;
967 }
968 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
969 al = SSL_AD_DECODE_ERROR;
970 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
971 SSL_R_CERT_LENGTH_MISMATCH);
972 goto f_err;
973 }
974 if (!sk_X509_push(sk, x)) {
975 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
976 goto err;
977 }
978 x = NULL;
979 }
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langley1bea1732014-12-17 19:06:57 -0800981 i = ssl_verify_cert_chain(s, sk);
982 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
983 al = ssl_verify_alarm_type(s->verify_result);
984 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
985 SSL_R_CERTIFICATE_VERIFY_FAILED);
986 goto f_err;
987 }
988 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langley1bea1732014-12-17 19:06:57 -0800990 sc = ssl_sess_cert_new();
991 if (sc == NULL) {
992 goto err;
993 }
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langley1bea1732014-12-17 19:06:57 -0800995 if (s->session->sess_cert) {
996 ssl_sess_cert_free(s->session->sess_cert);
997 }
998 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langley1bea1732014-12-17 19:06:57 -08001000 sc->cert_chain = sk;
1001 /* Inconsistency alert: cert_chain does include the peer's certificate, which
1002 * we don't include in s3_srvr.c */
1003 x = sk_X509_value(sk, 0);
1004 sk = NULL;
1005 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langley1bea1732014-12-17 19:06:57 -08001007 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langley1bea1732014-12-17 19:06:57 -08001009 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1010 x = NULL;
1011 al = SSL3_AL_FATAL;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1013 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1014 goto f_err;
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langley1bea1732014-12-17 19:06:57 -08001017 i = ssl_cert_type(pkey);
1018 if (i < 0) {
1019 x = NULL;
1020 al = SSL3_AL_FATAL;
1021 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1022 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1023 goto f_err;
1024 }
Adam Langley95c29f32014-06-20 12:00:00 -07001025
Adam Langley1bea1732014-12-17 19:06:57 -08001026 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1027 if (exp_idx >= 0 && i != exp_idx) {
1028 x = NULL;
1029 al = SSL_AD_ILLEGAL_PARAMETER;
1030 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1031 SSL_R_WRONG_CERTIFICATE_TYPE);
1032 goto f_err;
1033 }
1034 sc->peer_cert_type = i;
1035 /* Why would the following ever happen? We just created sc a couple of lines
1036 * ago. */
1037 if (sc->peer_pkeys[i].x509 != NULL) {
1038 X509_free(sc->peer_pkeys[i].x509);
1039 }
1040 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1041 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langley1bea1732014-12-17 19:06:57 -08001043 if (s->session->peer != NULL) {
1044 X509_free(s->session->peer);
1045 }
1046 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001047
Adam Langley1bea1732014-12-17 19:06:57 -08001048 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langley1bea1732014-12-17 19:06:57 -08001050 x = NULL;
1051 ret = 1;
1052
1053 if (0) {
1054 f_err:
1055 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1056 }
1057
1058err:
1059 EVP_PKEY_free(pkey);
1060 X509_free(x);
1061 sk_X509_pop_free(sk, X509_free);
1062 return ret;
1063}
1064
1065int ssl3_get_server_key_exchange(SSL *s) {
1066 EVP_MD_CTX md_ctx;
1067 int al, ok;
1068 long n, alg_k, alg_a;
1069 EVP_PKEY *pkey = NULL;
1070 const EVP_MD *md = NULL;
1071 RSA *rsa = NULL;
1072 DH *dh = NULL;
1073 EC_KEY *ecdh = NULL;
1074 BN_CTX *bn_ctx = NULL;
1075 EC_POINT *srvr_ecpoint = NULL;
1076 CBS server_key_exchange, server_key_exchange_orig, parameter;
1077
1078 /* use same message size as in ssl3_get_certificate_request() as
1079 * ServerKeyExchange message may be skipped */
1080 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1081 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
1082 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
1083 if (!ok) {
1084 return n;
1085 }
1086
1087 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1088 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1089 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1090 SSL_R_UNEXPECTED_MESSAGE);
1091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1092 return -1;
1093 }
1094
1095 /* In plain PSK ciphersuite, ServerKeyExchange can be
1096 omitted if no identity hint is sent. Set session->sess_cert anyway to
1097 avoid problems later.*/
1098 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1099 /* PSK ciphersuites that also send a Certificate would have already
1100 * initialized |sess_cert|. */
1101 if (s->session->sess_cert == NULL) {
1102 s->session->sess_cert = ssl_sess_cert_new();
1103 }
1104
1105 /* TODO(davidben): This should be reset in one place with the rest of the
1106 * handshake state. */
1107 if (s->s3->tmp.peer_psk_identity_hint) {
1108 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1109 s->s3->tmp.peer_psk_identity_hint = NULL;
1110 }
1111 }
1112 s->s3->tmp.reuse_message = 1;
1113 return 1;
1114 }
1115
1116 /* Retain a copy of the original CBS to compute the signature over. */
1117 CBS_init(&server_key_exchange, s->init_msg, n);
1118 server_key_exchange_orig = server_key_exchange;
1119
1120 if (s->session->sess_cert != NULL) {
1121 if (s->session->sess_cert->peer_dh_tmp) {
1122 DH_free(s->session->sess_cert->peer_dh_tmp);
1123 s->session->sess_cert->peer_dh_tmp = NULL;
1124 }
1125 if (s->session->sess_cert->peer_ecdh_tmp) {
1126 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1127 s->session->sess_cert->peer_ecdh_tmp = NULL;
1128 }
1129 } else {
1130 s->session->sess_cert = ssl_sess_cert_new();
1131 }
1132
1133 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1134 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1135 EVP_MD_CTX_init(&md_ctx);
1136
1137 if (alg_a & SSL_aPSK) {
1138 CBS psk_identity_hint;
1139
1140 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1141 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1142 &psk_identity_hint)) {
1143 al = SSL_AD_DECODE_ERROR;
1144 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1145 goto f_err;
1146 }
1147
1148 /* Store PSK identity hint for later use, hint is used in
1149 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1150 * identity hint can be as long as the maximum length of a PSK identity.
1151 * Also do not allow NULL characters; identities are saved as C strings.
1152 *
1153 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1154 * a specific identity. */
1155 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1156 CBS_contains_zero_byte(&psk_identity_hint)) {
1157 al = SSL_AD_HANDSHAKE_FAILURE;
1158 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1159 SSL_R_DATA_LENGTH_TOO_LONG);
1160 goto f_err;
1161 }
1162
1163 /* Save the identity hint as a C string. */
1164 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1165 al = SSL_AD_INTERNAL_ERROR;
1166 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1167 ERR_R_MALLOC_FAILURE);
1168 goto f_err;
1169 }
1170 }
1171
1172 if (alg_k & SSL_kEDH) {
1173 CBS dh_p, dh_g, dh_Ys;
1174
1175 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1176 CBS_len(&dh_p) == 0 ||
1177 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1178 CBS_len(&dh_g) == 0 ||
1179 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1180 CBS_len(&dh_Ys) == 0) {
1181 al = SSL_AD_DECODE_ERROR;
1182 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1183 goto f_err;
1184 }
1185
1186 dh = DH_new();
1187 if (dh == NULL) {
1188 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1189 goto err;
1190 }
1191
1192 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1193 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1194 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1195 NULL) {
1196 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1197 goto err;
1198 }
1199
1200 if (DH_size(dh) < 512 / 8) {
1201 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1202 SSL_R_BAD_DH_P_LENGTH);
1203 goto err;
1204 }
1205
1206 if (alg_a & SSL_aRSA) {
1207 pkey = X509_get_pubkey(
1208 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1209 }
1210 /* else anonymous DH, so no certificate or pkey. */
1211
1212 s->session->sess_cert->peer_dh_tmp = dh;
1213 dh = NULL;
1214 } else if (alg_k & SSL_kEECDH) {
1215 uint16_t curve_id;
1216 int curve_nid = 0;
1217 EC_GROUP *ngroup;
1218 const EC_GROUP *group;
1219 CBS point;
1220
1221 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1222 * key. Check curve is one of our preferences, if not server has sent an
1223 * invalid curve. */
1224 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1225 al = SSL_AD_DECODE_ERROR;
1226 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1227 goto f_err;
1228 }
1229
1230 curve_nid = tls1_ec_curve_id2nid(curve_id);
1231 if (curve_nid == 0) {
1232 al = SSL_AD_INTERNAL_ERROR;
1233 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1234 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1235 goto f_err;
1236 }
1237
1238 ecdh = EC_KEY_new();
1239 if (ecdh == NULL) {
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1241 ERR_R_MALLOC_FAILURE);
1242 goto err;
1243 }
1244
1245 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
David Benjaminc35fb012015-02-12 00:48:45 -05001246 if (ngroup == NULL) {
1247 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
1248 goto err;
1249 }
1250 if (!EC_KEY_set_group(ecdh, ngroup)) {
1251 EC_GROUP_free(ngroup);
Adam Langley1bea1732014-12-17 19:06:57 -08001252 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
1253 goto err;
1254 }
1255 EC_GROUP_free(ngroup);
1256
1257 group = EC_KEY_get0_group(ecdh);
1258
1259 /* Next, get the encoded ECPoint */
1260 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1261 al = SSL_AD_DECODE_ERROR;
1262 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1263 goto f_err;
1264 }
1265
1266 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1267 ((bn_ctx = BN_CTX_new()) == NULL)) {
1268 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1269 ERR_R_MALLOC_FAILURE);
1270 goto err;
1271 }
1272
1273 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1274 CBS_len(&point), bn_ctx)) {
1275 al = SSL_AD_DECODE_ERROR;
1276 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1277 goto f_err;
1278 }
1279
1280 /* The ECC/TLS specification does not mention the use of DSA to sign
1281 * ECParameters in the server key exchange message. We do support RSA and
1282 * ECDSA. */
1283 if (alg_a & SSL_aRSA) {
1284 pkey = X509_get_pubkey(
1285 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1286 } else if (alg_a & SSL_aECDSA) {
1287 pkey =
1288 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1289 }
1290 /* else anonymous ECDH, so no certificate or pkey. */
1291 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1292 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1293 ecdh = NULL;
1294 BN_CTX_free(bn_ctx);
1295 bn_ctx = NULL;
1296 EC_POINT_free(srvr_ecpoint);
1297 srvr_ecpoint = NULL;
1298 } else if (!(alg_k & SSL_kPSK)) {
1299 al = SSL_AD_UNEXPECTED_MESSAGE;
1300 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1301 SSL_R_UNEXPECTED_MESSAGE);
1302 goto f_err;
1303 }
1304
1305 /* At this point, |server_key_exchange| contains the signature, if any, while
1306 * |server_key_exchange_orig| contains the entire message. From that, derive
1307 * a CBS containing just the parameter. */
1308 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1309 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1310
1311 /* if it was signed, check the signature */
1312 if (pkey != NULL) {
1313 CBS signature;
1314
1315 if (SSL_USE_SIGALGS(s)) {
1316 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1317 goto f_err;
1318 }
1319 } else if (pkey->type == EVP_PKEY_RSA) {
1320 md = EVP_md5_sha1();
1321 } else {
1322 md = EVP_sha1();
1323 }
1324
1325 /* The last field in |server_key_exchange| is the signature. */
1326 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1327 CBS_len(&server_key_exchange) != 0) {
1328 al = SSL_AD_DECODE_ERROR;
1329 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1330 goto f_err;
1331 }
1332
1333 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1334 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1335 SSL3_RANDOM_SIZE) ||
1336 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1337 SSL3_RANDOM_SIZE) ||
1338 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1339 CBS_len(&parameter)) ||
1340 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1341 CBS_len(&signature))) {
1342 /* bad signature */
1343 al = SSL_AD_DECRYPT_ERROR;
1344 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1345 goto f_err;
1346 }
1347 } else {
1348 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1349 /* Might be wrong key type, check it */
1350 if (ssl3_check_cert_and_algorithm(s)) {
1351 /* Otherwise this shouldn't happen */
1352 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1353 ERR_R_INTERNAL_ERROR);
1354 }
1355 goto err;
1356 }
1357 /* still data left over */
1358 if (CBS_len(&server_key_exchange) > 0) {
1359 al = SSL_AD_DECODE_ERROR;
1360 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1361 SSL_R_EXTRA_DATA_IN_MESSAGE);
1362 goto f_err;
1363 }
1364 }
1365 EVP_PKEY_free(pkey);
1366 EVP_MD_CTX_cleanup(&md_ctx);
1367 return 1;
1368
Adam Langley95c29f32014-06-20 12:00:00 -07001369f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001370 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001371err:
Adam Langley1bea1732014-12-17 19:06:57 -08001372 EVP_PKEY_free(pkey);
1373 if (rsa != NULL) {
1374 RSA_free(rsa);
1375 }
1376 if (dh != NULL) {
1377 DH_free(dh);
1378 }
1379 BN_CTX_free(bn_ctx);
1380 EC_POINT_free(srvr_ecpoint);
1381 if (ecdh != NULL) {
1382 EC_KEY_free(ecdh);
1383 }
1384 EVP_MD_CTX_cleanup(&md_ctx);
1385 return -1;
1386}
Adam Langley95c29f32014-06-20 12:00:00 -07001387
Adam Langley1bea1732014-12-17 19:06:57 -08001388static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1389 return X509_NAME_cmp(*a, *b);
1390}
Adam Langley95c29f32014-06-20 12:00:00 -07001391
Adam Langley1bea1732014-12-17 19:06:57 -08001392int ssl3_get_certificate_request(SSL *s) {
1393 int ok, ret = 0;
1394 unsigned long n;
1395 X509_NAME *xn = NULL;
1396 STACK_OF(X509_NAME) *ca_sk = NULL;
1397 CBS cbs;
1398 CBS certificate_types;
1399 CBS certificate_authorities;
1400 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langley1bea1732014-12-17 19:06:57 -08001402 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1403 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
1404 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001405
Adam Langley1bea1732014-12-17 19:06:57 -08001406 if (!ok) {
1407 return n;
1408 }
David Benjamin688d8df2014-11-02 23:06:42 -05001409
Adam Langley1bea1732014-12-17 19:06:57 -08001410 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
Adam Langley1bea1732014-12-17 19:06:57 -08001412 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1413 s->s3->tmp.reuse_message = 1;
1414 /* If we get here we don't need any cached handshake records as we wont be
1415 * doing client auth. */
1416 if (s->s3->handshake_buffer &&
1417 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1418 goto err;
1419 }
1420 return 1;
1421 }
David Benjamined439582014-07-14 19:13:02 -04001422
Adam Langley1bea1732014-12-17 19:06:57 -08001423 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1424 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1425 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1426 SSL_R_WRONG_MESSAGE_TYPE);
1427 goto err;
1428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
Adam Langley1bea1732014-12-17 19:06:57 -08001430 /* TLS does not like anon-DH with client cert */
1431 if (s->version > SSL3_VERSION &&
1432 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1433 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1434 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1435 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1436 goto err;
1437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langley1bea1732014-12-17 19:06:57 -08001441 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1442 if (ca_sk == NULL) {
1443 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1444 goto err;
1445 }
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langley1bea1732014-12-17 19:06:57 -08001447 /* get the certificate types */
1448 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1449 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1450 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1451 goto err;
1452 }
David Benjamined439582014-07-14 19:13:02 -04001453
Adam Langley1bea1732014-12-17 19:06:57 -08001454 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1455 &s->s3->tmp.num_certificate_types)) {
1456 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1457 goto err;
1458 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 if (SSL_USE_SIGALGS(s)) {
1461 CBS supported_signature_algorithms;
1462 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1463 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1464 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1465 goto err;
1466 }
David Benjamined439582014-07-14 19:13:02 -04001467
Adam Langley1bea1732014-12-17 19:06:57 -08001468 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1469 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1470 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1471 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1472 goto err;
1473 }
1474 }
David Benjamined439582014-07-14 19:13:02 -04001475
Adam Langley1bea1732014-12-17 19:06:57 -08001476 /* get the CA RDNs */
1477 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1478 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1479 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1480 goto err;
1481 }
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langley1bea1732014-12-17 19:06:57 -08001483 while (CBS_len(&certificate_authorities) > 0) {
1484 CBS distinguished_name;
1485 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1486 &distinguished_name)) {
1487 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1488 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1489 SSL_R_CA_DN_TOO_LONG);
1490 goto err;
1491 }
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langley1bea1732014-12-17 19:06:57 -08001493 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001494
Adam Langley1bea1732014-12-17 19:06:57 -08001495 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1496 if (xn == NULL) {
1497 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1498 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1499 goto err;
1500 }
Adam Langley95c29f32014-06-20 12:00:00 -07001501
Adam Langley1bea1732014-12-17 19:06:57 -08001502 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1503 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1504 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1505 goto err;
1506 }
Adam Langley95c29f32014-06-20 12:00:00 -07001507
Adam Langley1bea1732014-12-17 19:06:57 -08001508 if (CBS_len(&distinguished_name) != 0) {
1509 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1510 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1511 SSL_R_CA_DN_LENGTH_MISMATCH);
1512 goto err;
1513 }
Adam Langley95c29f32014-06-20 12:00:00 -07001514
Adam Langley1bea1732014-12-17 19:06:57 -08001515 if (!sk_X509_NAME_push(ca_sk, xn)) {
1516 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1517 ERR_R_MALLOC_FAILURE);
1518 goto err;
1519 }
1520 }
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langley1bea1732014-12-17 19:06:57 -08001522 /* we should setup a certificate to return.... */
1523 s->s3->tmp.cert_req = 1;
1524 if (s->s3->tmp.ca_names != NULL) {
1525 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1526 }
1527 s->s3->tmp.ca_names = ca_sk;
1528 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langley1bea1732014-12-17 19:06:57 -08001530 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langley1bea1732014-12-17 19:06:57 -08001532err:
1533 if (ca_sk != NULL) {
1534 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1535 }
1536 return ret;
1537}
Adam Langley95c29f32014-06-20 12:00:00 -07001538
Adam Langley1bea1732014-12-17 19:06:57 -08001539int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001540 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001541 long n;
1542 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001543
Adam Langley1bea1732014-12-17 19:06:57 -08001544 n = s->method->ssl_get_message(
1545 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
1546 SSL3_MT_NEWSESSION_TICKET, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001547
Adam Langley1bea1732014-12-17 19:06:57 -08001548 if (!ok) {
1549 return n;
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langley1bea1732014-12-17 19:06:57 -08001552 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001553
Adam Langley1bea1732014-12-17 19:06:57 -08001554 if (!CBS_get_u32(&new_session_ticket,
1555 &s->session->tlsext_tick_lifetime_hint) ||
1556 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1557 CBS_len(&new_session_ticket) != 0) {
1558 al = SSL_AD_DECODE_ERROR;
1559 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1560 goto f_err;
1561 }
Adam Langley95c29f32014-06-20 12:00:00 -07001562
Adam Langley1bea1732014-12-17 19:06:57 -08001563 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1564 &s->session->tlsext_ticklen)) {
1565 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1566 goto err;
1567 }
Adam Langley95c29f32014-06-20 12:00:00 -07001568
David Benjamin68070622015-02-08 23:44:59 -05001569 /* Generate a session ID for this session based on the session ticket. We use
1570 * the session ID mechanism for detecting ticket resumption. This also fits in
1571 * with assumptions elsewhere in OpenSSL.*/
1572 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1573 &s->session->session_id_length, EVP_sha256(), NULL)) {
1574 goto err;
1575 }
1576
1577 return 1;
David Benjamined439582014-07-14 19:13:02 -04001578
Adam Langley95c29f32014-06-20 12:00:00 -07001579f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001580 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001581err:
Adam Langley1bea1732014-12-17 19:06:57 -08001582 return -1;
1583}
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585int ssl3_get_cert_status(SSL *s) {
1586 int ok, al;
1587 long n;
1588 CBS certificate_status, ocsp_response;
1589 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001590
Adam Langley1bea1732014-12-17 19:06:57 -08001591 n = s->method->ssl_get_message(
1592 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
1593 SSL3_MT_CERTIFICATE_STATUS, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langley1bea1732014-12-17 19:06:57 -08001595 if (!ok) {
1596 return n;
1597 }
Adam Langley95c29f32014-06-20 12:00:00 -07001598
Adam Langley1bea1732014-12-17 19:06:57 -08001599 CBS_init(&certificate_status, s->init_msg, n);
1600 if (!CBS_get_u8(&certificate_status, &status_type) ||
1601 status_type != TLSEXT_STATUSTYPE_ocsp ||
1602 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1603 CBS_len(&ocsp_response) == 0 ||
1604 CBS_len(&certificate_status) != 0) {
1605 al = SSL_AD_DECODE_ERROR;
1606 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1607 goto f_err;
1608 }
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langley1bea1732014-12-17 19:06:57 -08001610 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1611 &s->session->ocsp_response_length)) {
1612 al = SSL_AD_INTERNAL_ERROR;
1613 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1614 goto f_err;
1615 }
1616 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langley95c29f32014-06-20 12:00:00 -07001618f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001619 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1620 return -1;
1621}
Adam Langley95c29f32014-06-20 12:00:00 -07001622
Adam Langley1bea1732014-12-17 19:06:57 -08001623int ssl3_get_server_done(SSL *s) {
1624 int ok;
1625 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1628 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1629 30, /* should be very small, like 0 :-) */
1630 SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langley1bea1732014-12-17 19:06:57 -08001632 if (!ok) {
1633 return n;
1634 }
David Benjamin46062682014-07-14 19:14:32 -04001635
Adam Langley1bea1732014-12-17 19:06:57 -08001636 if (n > 0) {
1637 /* should contain no data */
1638 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1639 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1640 return -1;
1641 }
David Benjamin46062682014-07-14 19:14:32 -04001642
Adam Langley1bea1732014-12-17 19:06:57 -08001643 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001644}
Adam Langley1258b6a2014-06-20 12:00:00 -07001645
Adam Langley1258b6a2014-06-20 12:00:00 -07001646
Adam Langley1bea1732014-12-17 19:06:57 -08001647int ssl3_send_client_key_exchange(SSL *s) {
1648 uint8_t *p;
1649 int n = 0;
1650 unsigned long alg_k;
1651 unsigned long alg_a;
1652 uint8_t *q;
1653 EVP_PKEY *pkey = NULL;
1654 EC_KEY *clnt_ecdh = NULL;
1655 const EC_POINT *srvr_ecpoint = NULL;
1656 EVP_PKEY *srvr_pub_pkey = NULL;
1657 uint8_t *encodedPoint = NULL;
1658 int encoded_pt_len = 0;
1659 BN_CTX *bn_ctx = NULL;
1660 unsigned int psk_len = 0;
1661 uint8_t psk[PSK_MAX_PSK_LEN];
1662 uint8_t *pms = NULL;
1663 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001664
Adam Langley1bea1732014-12-17 19:06:57 -08001665 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1666 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001667
Adam Langley1bea1732014-12-17 19:06:57 -08001668 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1669 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langley1bea1732014-12-17 19:06:57 -08001671 /* If using a PSK key exchange, prepare the pre-shared key. */
1672 if (alg_a & SSL_aPSK) {
1673 char identity[PSK_MAX_IDENTITY_LEN + 1];
1674 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001675
Adam Langley1bea1732014-12-17 19:06:57 -08001676 if (s->psk_client_callback == NULL) {
1677 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1678 SSL_R_PSK_NO_CLIENT_CB);
1679 goto err;
1680 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001681
Adam Langley1bea1732014-12-17 19:06:57 -08001682 memset(identity, 0, sizeof(identity));
1683 psk_len =
1684 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1685 sizeof(identity), psk, sizeof(psk));
1686 if (psk_len > PSK_MAX_PSK_LEN) {
1687 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1688 ERR_R_INTERNAL_ERROR);
1689 goto err;
1690 } else if (psk_len == 0) {
1691 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1692 SSL_R_PSK_IDENTITY_NOT_FOUND);
1693 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1694 goto err;
1695 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001696
Adam Langley1bea1732014-12-17 19:06:57 -08001697 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1698 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1699 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1700 ERR_R_INTERNAL_ERROR);
1701 goto err;
1702 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001703
Adam Langley1bea1732014-12-17 19:06:57 -08001704 if (s->session->psk_identity != NULL) {
1705 OPENSSL_free(s->session->psk_identity);
1706 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001707
Adam Langley1bea1732014-12-17 19:06:57 -08001708 s->session->psk_identity = BUF_strdup(identity);
1709 if (s->session->psk_identity == NULL) {
1710 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1711 ERR_R_MALLOC_FAILURE);
1712 goto err;
1713 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001714
Adam Langley1bea1732014-12-17 19:06:57 -08001715 /* Write out psk_identity. */
1716 s2n(identity_len, p);
1717 memcpy(p, identity, identity_len);
1718 p += identity_len;
1719 n = 2 + identity_len;
1720 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001721
Adam Langley1bea1732014-12-17 19:06:57 -08001722 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1723 if (alg_k & SSL_kRSA) {
1724 RSA *rsa;
1725 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001726
Adam Langley1bea1732014-12-17 19:06:57 -08001727 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1728 pms = OPENSSL_malloc(pms_len);
1729 if (pms == NULL) {
1730 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1731 ERR_R_MALLOC_FAILURE);
1732 goto err;
1733 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001734
Adam Langley1bea1732014-12-17 19:06:57 -08001735 if (s->session->sess_cert == NULL) {
1736 /* We should always have a server certificate with SSL_kRSA. */
1737 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1738 ERR_R_INTERNAL_ERROR);
1739 goto err;
1740 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001741
Adam Langley1bea1732014-12-17 19:06:57 -08001742 pkey = X509_get_pubkey(
1743 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1744 if (pkey == NULL ||
1745 pkey->type != EVP_PKEY_RSA ||
1746 pkey->pkey.rsa == NULL) {
1747 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1748 ERR_R_INTERNAL_ERROR);
1749 if (pkey != NULL) {
1750 EVP_PKEY_free(pkey);
1751 }
1752 goto err;
1753 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001754
Adam Langley1bea1732014-12-17 19:06:57 -08001755 rsa = pkey->pkey.rsa;
1756 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001757
Adam Langley1bea1732014-12-17 19:06:57 -08001758 pms[0] = s->client_version >> 8;
1759 pms[1] = s->client_version & 0xff;
1760 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1761 goto err;
1762 }
1763
1764 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1765
1766 q = p;
1767 /* In TLS and beyond, reserve space for the length prefix. */
1768 if (s->version > SSL3_VERSION) {
1769 p += 2;
1770 n += 2;
1771 }
1772 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1773 RSA_PKCS1_PADDING)) {
1774 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1775 SSL_R_BAD_RSA_ENCRYPT);
1776 goto err;
1777 }
1778 n += enc_pms_len;
1779
1780 /* Log the premaster secret, if logging is enabled. */
1781 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1782 pms_len)) {
1783 goto err;
1784 }
1785
1786 /* Fill in the length prefix. */
1787 if (s->version > SSL3_VERSION) {
1788 s2n(enc_pms_len, q);
1789 }
1790 } else if (alg_k & SSL_kEDH) {
1791 DH *dh_srvr, *dh_clnt;
1792 SESS_CERT *scert = s->session->sess_cert;
1793 int dh_len;
1794 size_t pub_len;
1795
1796 if (scert == NULL) {
1797 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1798 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1799 SSL_R_UNEXPECTED_MESSAGE);
1800 goto err;
1801 }
1802
1803 if (scert->peer_dh_tmp == NULL) {
1804 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1805 ERR_R_INTERNAL_ERROR);
1806 goto err;
1807 }
1808 dh_srvr = scert->peer_dh_tmp;
1809
1810 /* generate a new random key */
1811 dh_clnt = DHparams_dup(dh_srvr);
1812 if (dh_clnt == NULL) {
1813 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1814 goto err;
1815 }
1816 if (!DH_generate_key(dh_clnt)) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1818 DH_free(dh_clnt);
1819 goto err;
1820 }
1821
1822 pms_len = DH_size(dh_clnt);
1823 pms = OPENSSL_malloc(pms_len);
1824 if (pms == NULL) {
1825 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1826 ERR_R_MALLOC_FAILURE);
1827 DH_free(dh_clnt);
1828 goto err;
1829 }
1830
1831 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1832 if (dh_len <= 0) {
1833 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1834 DH_free(dh_clnt);
1835 goto err;
1836 }
1837 pms_len = dh_len;
1838
1839 /* send off the data */
1840 pub_len = BN_num_bytes(dh_clnt->pub_key);
1841 s2n(pub_len, p);
1842 BN_bn2bin(dh_clnt->pub_key, p);
1843 n += 2 + pub_len;
1844
1845 DH_free(dh_clnt);
1846 } else if (alg_k & SSL_kEECDH) {
1847 const EC_GROUP *srvr_group = NULL;
1848 EC_KEY *tkey;
1849 int field_size = 0, ecdh_len;
1850
1851 if (s->session->sess_cert == NULL) {
1852 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1853 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1854 SSL_R_UNEXPECTED_MESSAGE);
1855 goto err;
1856 }
1857
1858 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1859 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1860 ERR_R_INTERNAL_ERROR);
1861 goto err;
1862 }
1863
1864 tkey = s->session->sess_cert->peer_ecdh_tmp;
1865
1866 srvr_group = EC_KEY_get0_group(tkey);
1867 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1868 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1869 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1870 ERR_R_INTERNAL_ERROR);
1871 goto err;
1872 }
1873
1874 clnt_ecdh = EC_KEY_new();
1875 if (clnt_ecdh == NULL) {
1876 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1877 ERR_R_MALLOC_FAILURE);
1878 goto err;
1879 }
1880
1881 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1882 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1883 goto err;
1884 }
1885
1886 /* Generate a new ECDH key pair */
1887 if (!EC_KEY_generate_key(clnt_ecdh)) {
1888 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1889 goto err;
1890 }
1891
1892 field_size = EC_GROUP_get_degree(srvr_group);
1893 if (field_size <= 0) {
1894 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1895 goto err;
1896 }
1897
1898 pms_len = (field_size + 7) / 8;
1899 pms = OPENSSL_malloc(pms_len);
1900 if (pms == NULL) {
1901 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1902 ERR_R_MALLOC_FAILURE);
1903 goto err;
1904 }
1905
1906 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1907 if (ecdh_len <= 0) {
1908 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1909 goto err;
1910 }
1911 pms_len = ecdh_len;
1912
1913 /* First check the size of encoding and allocate memory accordingly. */
1914 encoded_pt_len =
1915 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1916 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1917
1918 encodedPoint =
1919 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1920 bn_ctx = BN_CTX_new();
1921 if (encodedPoint == NULL || bn_ctx == NULL) {
1922 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1923 ERR_R_MALLOC_FAILURE);
1924 goto err;
1925 }
1926
1927 /* Encode the public key */
1928 encoded_pt_len = EC_POINT_point2oct(
1929 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1930 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1931
1932 *p = encoded_pt_len; /* length of encoded point */
1933 /* Encoded point will be copied here */
1934 p += 1;
1935 n += 1;
1936 /* copy the point */
1937 memcpy(p, encodedPoint, encoded_pt_len);
1938 /* increment n to account for length field */
1939 n += encoded_pt_len;
1940
1941 /* Free allocated memory */
1942 BN_CTX_free(bn_ctx);
1943 bn_ctx = NULL;
1944 OPENSSL_free(encodedPoint);
1945 encodedPoint = NULL;
1946 EC_KEY_free(clnt_ecdh);
1947 clnt_ecdh = NULL;
1948 EVP_PKEY_free(srvr_pub_pkey);
1949 srvr_pub_pkey = NULL;
1950 } else if (alg_k & SSL_kPSK) {
1951 /* For plain PSK, other_secret is a block of 0s with the same length as
1952 * the pre-shared key. */
1953 pms_len = psk_len;
1954 pms = OPENSSL_malloc(pms_len);
1955 if (pms == NULL) {
1956 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1957 ERR_R_MALLOC_FAILURE);
1958 goto err;
1959 }
1960 memset(pms, 0, pms_len);
1961 } else {
1962 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1963 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1964 ERR_R_INTERNAL_ERROR);
1965 goto err;
1966 }
1967
1968 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1969 * key. */
1970 if (alg_a & SSL_aPSK) {
1971 CBB cbb, child;
1972 uint8_t *new_pms;
1973 size_t new_pms_len;
1974
1975 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1976 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1977 ERR_R_MALLOC_FAILURE);
1978 goto err;
1979 }
1980 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1981 !CBB_add_bytes(&child, pms, pms_len) ||
1982 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1983 !CBB_add_bytes(&child, psk, psk_len) ||
1984 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1985 CBB_cleanup(&cbb);
1986 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1987 ERR_R_INTERNAL_ERROR);
1988 goto err;
1989 }
1990 OPENSSL_cleanse(pms, pms_len);
1991 OPENSSL_free(pms);
1992 pms = new_pms;
1993 pms_len = new_pms_len;
1994 }
1995
1996 /* The message must be added to the finished hash before calculating the
1997 * master secret. */
1998 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
1999 s->state = SSL3_ST_CW_KEY_EXCH_B;
2000
2001 s->session->master_key_length = s->enc_method->generate_master_secret(
2002 s, s->session->master_key, pms, pms_len);
2003 if (s->session->master_key_length == 0) {
2004 goto err;
2005 }
2006 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2007 OPENSSL_cleanse(pms, pms_len);
2008 OPENSSL_free(pms);
2009 }
2010
2011 /* SSL3_ST_CW_KEY_EXCH_B */
2012 return s->enc_method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002013
2014err:
Adam Langley1bea1732014-12-17 19:06:57 -08002015 BN_CTX_free(bn_ctx);
2016 if (encodedPoint != NULL) {
2017 OPENSSL_free(encodedPoint);
2018 }
2019 if (clnt_ecdh != NULL) {
2020 EC_KEY_free(clnt_ecdh);
2021 }
2022 EVP_PKEY_free(srvr_pub_pkey);
2023 if (pms) {
2024 OPENSSL_cleanse(pms, pms_len);
2025 OPENSSL_free(pms);
2026 }
2027 return -1;
2028}
Adam Langley1258b6a2014-06-20 12:00:00 -07002029
Adam Langley1bea1732014-12-17 19:06:57 -08002030int ssl3_send_cert_verify(SSL *s) {
2031 uint8_t *buf, *p;
2032 const EVP_MD *md = NULL;
2033 uint8_t digest[EVP_MAX_MD_SIZE];
2034 size_t digest_length;
2035 EVP_PKEY *pkey;
2036 EVP_PKEY_CTX *pctx = NULL;
2037 size_t signature_length = 0;
2038 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002039
Adam Langley1bea1732014-12-17 19:06:57 -08002040 buf = (uint8_t *)s->init_buf->data;
2041
2042 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2043 p = ssl_handshake_start(s);
2044 pkey = s->cert->key->privatekey;
2045
2046 /* Write out the digest type if needbe. */
2047 if (SSL_USE_SIGALGS(s)) {
2048 md = tls1_choose_signing_digest(s, pkey);
2049 if (!tls12_get_sigandhash(p, pkey, md)) {
2050 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2051 goto err;
2052 }
2053 p += 2;
2054 n += 2;
2055 }
2056
2057 /* Compute the digest. */
2058 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2059 goto err;
2060 }
2061
2062 /* The handshake buffer is no longer necessary. */
2063 if (s->s3->handshake_buffer &&
2064 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2065 goto err;
2066 }
2067
2068 /* Sign the digest. */
2069 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2070 if (pctx == NULL) {
2071 goto err;
2072 }
2073
2074 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2075 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2076 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2077 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2078 goto err;
2079 }
2080
2081 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2082 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2083 goto err;
2084 }
2085
2086 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2087 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2088 goto err;
2089 }
2090
2091 s2n(signature_length, p);
2092 n += signature_length + 2;
2093
2094 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2095 s->state = SSL3_ST_CW_CERT_VRFY_B;
2096 }
2097
2098 EVP_PKEY_CTX_free(pctx);
2099 return ssl_do_write(s);
2100
2101err:
2102 EVP_PKEY_CTX_free(pctx);
2103 return -1;
2104}
2105
2106/* ssl3_has_client_certificate returns true if a client certificate is
2107 * configured. */
2108static int ssl3_has_client_certificate(SSL *s) {
2109 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2110}
2111
2112int ssl3_send_client_certificate(SSL *s) {
2113 X509 *x509 = NULL;
2114 EVP_PKEY *pkey = NULL;
2115 int i;
2116
2117 if (s->state == SSL3_ST_CW_CERT_A) {
2118 /* Let cert callback update client certificates if required */
2119 if (s->cert->cert_cb) {
2120 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2121 if (i < 0) {
2122 s->rwstate = SSL_X509_LOOKUP;
2123 return -1;
2124 }
2125 if (i == 0) {
2126 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2127 return 0;
2128 }
2129 s->rwstate = SSL_NOTHING;
2130 }
2131
2132 if (ssl3_has_client_certificate(s)) {
2133 s->state = SSL3_ST_CW_CERT_C;
2134 } else {
2135 s->state = SSL3_ST_CW_CERT_B;
2136 }
2137 }
2138
2139 /* We need to get a client cert */
2140 if (s->state == SSL3_ST_CW_CERT_B) {
2141 /* If we get an error, we need to:
2142 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2143 * We then get retried later */
2144 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2145 if (i < 0) {
2146 s->rwstate = SSL_X509_LOOKUP;
2147 return -1;
2148 }
2149 s->rwstate = SSL_NOTHING;
2150 if (i == 1 && pkey != NULL && x509 != NULL) {
2151 s->state = SSL3_ST_CW_CERT_B;
2152 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2153 i = 0;
2154 }
2155 } else if (i == 1) {
2156 i = 0;
2157 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2158 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2159 }
2160
2161 if (x509 != NULL) {
2162 X509_free(x509);
2163 }
2164 if (pkey != NULL) {
2165 EVP_PKEY_free(pkey);
2166 }
2167 if (i && !ssl3_has_client_certificate(s)) {
2168 i = 0;
2169 }
2170 if (i == 0) {
2171 if (s->version == SSL3_VERSION) {
2172 s->s3->tmp.cert_req = 0;
2173 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2174 return 1;
2175 } else {
2176 s->s3->tmp.cert_req = 2;
2177 }
2178 }
2179
2180 /* Ok, we have a cert */
2181 s->state = SSL3_ST_CW_CERT_C;
2182 }
2183
2184 if (s->state == SSL3_ST_CW_CERT_C) {
2185 s->state = SSL3_ST_CW_CERT_D;
2186 ssl3_output_cert_chain(s, (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key);
2187 }
2188
2189 /* SSL3_ST_CW_CERT_D */
2190 return ssl_do_write(s);
2191}
2192
2193#define has_bits(i, m) (((i) & (m)) == (m))
2194
2195int ssl3_check_cert_and_algorithm(SSL *s) {
2196 int i, idx;
2197 long alg_k, alg_a;
2198 EVP_PKEY *pkey = NULL;
2199 SESS_CERT *sc;
2200 DH *dh;
2201
2202 /* we don't have a certificate */
2203 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2204 return 1;
2205 }
2206
2207 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2208 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2209
2210 sc = s->session->sess_cert;
2211 if (sc == NULL) {
2212 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2213 goto err;
2214 }
2215
2216 dh = s->session->sess_cert->peer_dh_tmp;
2217
2218 /* This is the passed certificate */
2219
2220 idx = sc->peer_cert_type;
2221 if (idx == SSL_PKEY_ECC) {
2222 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2223 /* check failed */
2224 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2225 goto f_err;
2226 } else {
2227 return 1;
2228 }
2229 } else if (alg_a & SSL_aECDSA) {
2230 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2231 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2232 goto f_err;
2233 }
2234 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2235 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2236 EVP_PKEY_free(pkey);
2237
2238 /* Check that we have a certificate if we require one */
2239 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2240 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2241 SSL_R_MISSING_RSA_SIGNING_CERT);
2242 goto f_err;
2243 }
2244
2245 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2246 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2247 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2248 goto f_err;
2249 }
2250
2251 if ((alg_k & SSL_kEDH) &&
2252 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2253 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2254 goto f_err;
2255 }
2256
2257 return 1;
2258
2259f_err:
2260 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2261err:
2262 return 0;
2263}
2264
2265int ssl3_send_next_proto(SSL *s) {
2266 unsigned int len, padding_len;
2267 uint8_t *d, *p;
2268
2269 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2270 len = s->next_proto_negotiated_len;
2271 padding_len = 32 - ((len + 2) % 32);
2272
2273 d = p = ssl_handshake_start(s);
2274 *(p++) = len;
2275 memcpy(p, s->next_proto_negotiated, len);
2276 p += len;
2277 *(p++) = padding_len;
2278 memset(p, 0, padding_len);
2279 p += padding_len;
2280
2281 ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d);
2282 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2283 }
2284
2285 return ssl_do_write(s);
2286}
2287
2288int ssl3_send_channel_id(SSL *s) {
2289 uint8_t *d;
2290 int ret = -1, public_key_len;
2291 EVP_MD_CTX md_ctx;
2292 size_t sig_len;
2293 ECDSA_SIG *sig = NULL;
2294 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2295
2296 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2297 return ssl_do_write(s);
2298 }
2299
2300 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2301 EVP_PKEY *key = NULL;
2302 s->ctx->channel_id_cb(s, &key);
2303 if (key != NULL) {
2304 s->tlsext_channel_id_private = key;
2305 }
2306 }
2307
2308 if (!s->tlsext_channel_id_private) {
2309 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2310 return -1;
2311 }
2312 s->rwstate = SSL_NOTHING;
2313
2314 d = ssl_handshake_start(s);
2315 if (s->s3->tlsext_channel_id_new) {
2316 s2n(TLSEXT_TYPE_channel_id_new, d);
2317 } else {
2318 s2n(TLSEXT_TYPE_channel_id, d);
2319 }
2320 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2321
2322 EVP_MD_CTX_init(&md_ctx);
2323
2324 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2325 if (public_key_len <= 0) {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2327 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2328 goto err;
2329 }
2330
2331 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2332 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2333 * field elements as 32-byte, big-endian numbers. */
2334 if (public_key_len != 65) {
2335 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2336 goto err;
2337 }
2338 public_key = OPENSSL_malloc(public_key_len);
2339 if (!public_key) {
2340 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2341 goto err;
2342 }
2343
2344 derp = public_key;
2345 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2346
2347 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2348 s->tlsext_channel_id_private) != 1) {
2349 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2350 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2351 goto err;
2352 }
2353
2354 if (!tls1_channel_id_hash(&md_ctx, s)) {
2355 goto err;
2356 }
2357
2358 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2359 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2360 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2361 goto err;
2362 }
2363
2364 der_sig = OPENSSL_malloc(sig_len);
2365 if (!der_sig) {
2366 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2367 goto err;
2368 }
2369
2370 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2371 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2372 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2373 goto err;
2374 }
2375
2376 derp = der_sig;
2377 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2378 if (sig == NULL) {
2379 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2380 goto err;
2381 }
2382
2383 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2384 memcpy(d, public_key + 1, 64);
2385 d += 64;
2386 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2387 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2388 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2389 goto err;
2390 }
2391
2392 ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2393 2 + 2 + TLSEXT_CHANNEL_ID_SIZE);
2394 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2395
2396 ret = ssl_do_write(s);
2397
2398err:
2399 EVP_MD_CTX_cleanup(&md_ctx);
2400 if (public_key) {
2401 OPENSSL_free(public_key);
2402 }
2403 if (der_sig) {
2404 OPENSSL_free(der_sig);
2405 }
2406 if (sig) {
2407 ECDSA_SIG_free(sig);
2408 }
2409
2410 return ret;
2411}
2412
2413int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2414 int i = 0;
2415 if (s->ctx->client_cert_cb) {
2416 i = s->ctx->client_cert_cb(s, px509, ppkey);
2417 }
2418 return i;
2419}