blob: 5f891d4e331a43e47329c581d2037e1bc0c2d8ce [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 }
David Benjamined7c4752015-02-16 19:16:46 -0500453 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
454 ssl3_can_false_start(s) &&
455 /* No False Start on renegotiation (would complicate the state
456 * machine). */
Adam Langley1bea1732014-12-17 19:06:57 -0800457 s->s3->previous_server_finished_len == 0) {
David Benjamined7c4752015-02-16 19:16:46 -0500458 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800459 } 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
David Benjamined7c4752015-02-16 19:16:46 -0500527 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800528 /* 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 Benjamined7c4752015-02-16 19:16:46 -0500534 s->s3->tmp.in_false_start = 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 Benjamined7c4752015-02-16 19:16:46 -0500555 s->s3->tmp.in_false_start = 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;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500727 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
728 goto err;
729 }
Adam Langley1bea1732014-12-17 19:06:57 -0800730 s->state = SSL3_ST_CW_CLNT_HELLO_B;
731 }
732
733 /* SSL3_ST_CW_CLNT_HELLO_B */
734 return ssl_do_write(s);
735
Adam Langley95c29f32014-06-20 12:00:00 -0700736err:
Adam Langley1bea1732014-12-17 19:06:57 -0800737 return -1;
738}
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langley1bea1732014-12-17 19:06:57 -0800740int ssl3_get_server_hello(SSL *s) {
741 STACK_OF(SSL_CIPHER) * sk;
742 const SSL_CIPHER *c;
743 CERT *ct = s->cert;
744 int al = SSL_AD_INTERNAL_ERROR, ok;
745 long n;
746 CBS server_hello, server_random, session_id;
747 uint16_t server_version, cipher_suite;
748 uint8_t compression_method;
749 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Adam Langley1bea1732014-12-17 19:06:57 -0800751 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
752 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
753 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500754 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langley1bea1732014-12-17 19:06:57 -0800756 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500757 uint32_t err = ERR_peek_error();
758 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
759 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
760 /* Add a dedicated error code to the queue for a handshake_failure alert
761 * in response to ClientHello. This matches NSS's client behavior and
762 * gives a better error on a (probable) failure to negotiate initial
763 * parameters. Note: this error code comes after the original one.
764 *
765 * See https://crbug.com/446505. */
766 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
767 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
768 }
Adam Langley1bea1732014-12-17 19:06:57 -0800769 return n;
770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
Adam Langley1bea1732014-12-17 19:06:57 -0800772 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400773
Adam Langley1bea1732014-12-17 19:06:57 -0800774 if (!CBS_get_u16(&server_hello, &server_version) ||
775 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
776 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
777 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
778 !CBS_get_u16(&server_hello, &cipher_suite) ||
779 !CBS_get_u8(&server_hello, &compression_method)) {
780 al = SSL_AD_DECODE_ERROR;
781 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
782 goto f_err;
783 }
David Benjamina03d95d2014-07-12 19:49:07 -0400784
Adam Langley1bea1732014-12-17 19:06:57 -0800785 if (!s->s3->have_version) {
786 if (!ssl3_is_version_enabled(s, server_version)) {
787 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
788 s->version = server_version;
789 /* Mark the version as fixed so the record-layer version is not clamped
790 * to TLS 1.0. */
791 s->s3->have_version = 1;
792 al = SSL_AD_PROTOCOL_VERSION;
793 goto f_err;
794 }
795 s->version = server_version;
796 s->enc_method = ssl3_get_enc_method(server_version);
797 assert(s->enc_method != NULL);
798 /* At this point, the connection's version is known and s->version is
799 * fixed. Begin enforcing the record-layer version. */
800 s->s3->have_version = 1;
801 } else if (server_version != s->version) {
802 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
803 al = SSL_AD_PROTOCOL_VERSION;
804 goto f_err;
805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langley1bea1732014-12-17 19:06:57 -0800807 /* Copy over the server random. */
808 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langley1bea1732014-12-17 19:06:57 -0800810 assert(s->session == NULL || s->session->session_id_length > 0);
811 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
812 s->session->session_id_length)) {
813 if (s->sid_ctx_length != s->session->sid_ctx_length ||
814 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
815 /* actually a client application bug */
816 al = SSL_AD_ILLEGAL_PARAMETER;
817 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
818 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
819 goto f_err;
820 }
821 s->hit = 1;
822 } else {
823 /* The session wasn't resumed. Create a fresh SSL_SESSION to
824 * fill out. */
825 s->hit = 0;
826 if (!ssl_get_new_session(s, 0)) {
827 goto f_err;
828 }
829 /* Note: session_id could be empty. */
830 s->session->session_id_length = CBS_len(&session_id);
831 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
832 }
David Benjamina03d95d2014-07-12 19:49:07 -0400833
Adam Langley1bea1732014-12-17 19:06:57 -0800834 c = ssl3_get_cipher_by_value(cipher_suite);
835 if (c == NULL) {
836 /* unknown cipher */
837 al = SSL_AD_ILLEGAL_PARAMETER;
838 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
839 SSL_R_UNKNOWN_CIPHER_RETURNED);
840 goto f_err;
841 }
842 /* ct->mask_ssl was computed from client capabilities. Now
843 * that the final version is known, compute a new mask_ssl. */
844 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
845 mask_ssl = SSL_TLSV1_2;
846 } else {
847 mask_ssl = 0;
848 }
849 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
850 * the server selected it, it's an error. */
851 if ((c->algorithm_ssl & mask_ssl) ||
852 (c->algorithm_mkey & ct->mask_k) ||
853 (c->algorithm_auth & ct->mask_a)) {
854 al = SSL_AD_ILLEGAL_PARAMETER;
855 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
856 goto f_err;
857 }
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langley1bea1732014-12-17 19:06:57 -0800859 sk = ssl_get_ciphers_by_id(s);
860 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
861 /* we did not say we would use this cipher */
862 al = SSL_AD_ILLEGAL_PARAMETER;
863 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
864 goto f_err;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
David Benjaminf3a8b122014-12-25 23:11:49 -0500867 if (s->hit && s->session->cipher != c) {
Adam Langley1bea1732014-12-17 19:06:57 -0800868 al = SSL_AD_ILLEGAL_PARAMETER;
869 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
870 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
871 goto f_err;
872 }
873 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500874
Adam Langley1bea1732014-12-17 19:06:57 -0800875 /* Most clients also require that the negotiated version match the session's
876 * version if resuming. However OpenSSL has historically not had the
877 * corresponding logic on the server, so this may not be compatible,
878 * depending on other factors. (Whether the ClientHello version is clamped to
879 * the session's version and whether the session cache is keyed on IP
880 * address.)
881 *
882 * TODO(davidben): See if we can still enforce this? Perhaps for the future
883 * TLS 1.3 and forward if this is fixed upstream. */
David Benjaminbdf5e722014-11-11 00:52:15 -0500884
Adam Langley1bea1732014-12-17 19:06:57 -0800885 /* Don't digest cached records if no sigalgs: we may need them for client
886 * authentication. */
887 if (!SSL_USE_SIGALGS(s) &&
888 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
889 goto f_err;
890 }
David Benjamina03d95d2014-07-12 19:49:07 -0400891
Adam Langley1bea1732014-12-17 19:06:57 -0800892 /* Only the NULL compression algorithm is supported. */
893 if (compression_method != 0) {
894 al = SSL_AD_ILLEGAL_PARAMETER;
895 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
896 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
897 goto f_err;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langley1bea1732014-12-17 19:06:57 -0800900 /* TLS extensions */
901 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
902 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
903 goto err;
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langley1bea1732014-12-17 19:06:57 -0800906 /* There should be nothing left over in the record. */
907 if (CBS_len(&server_hello) != 0) {
908 /* wrong packet length */
909 al = SSL_AD_DECODE_ERROR;
910 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
911 goto f_err;
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langley1bea1732014-12-17 19:06:57 -0800914 return 1;
915
Adam Langley95c29f32014-06-20 12:00:00 -0700916f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800917 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700918err:
Adam Langley1bea1732014-12-17 19:06:57 -0800919 return -1;
920}
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langley1bea1732014-12-17 19:06:57 -0800922int ssl3_get_server_certificate(SSL *s) {
923 int al, i, ok, ret = -1;
924 unsigned long n;
925 X509 *x = NULL;
926 STACK_OF(X509) *sk = NULL;
927 SESS_CERT *sc;
928 EVP_PKEY *pkey = NULL;
929 CBS cbs, certificate_list;
930 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langley1bea1732014-12-17 19:06:57 -0800932 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
933 SSL3_MT_CERTIFICATE, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500934 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langley1bea1732014-12-17 19:06:57 -0800936 if (!ok) {
937 return n;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langley1bea1732014-12-17 19:06:57 -0800940 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700941
Adam Langley1bea1732014-12-17 19:06:57 -0800942 sk = sk_X509_new_null();
943 if (sk == NULL) {
944 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
945 goto err;
946 }
Adam Langley95c29f32014-06-20 12:00:00 -0700947
Adam Langley1bea1732014-12-17 19:06:57 -0800948 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
949 CBS_len(&cbs) != 0) {
950 al = SSL_AD_DECODE_ERROR;
951 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
952 goto f_err;
953 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400954
Adam Langley1bea1732014-12-17 19:06:57 -0800955 while (CBS_len(&certificate_list) > 0) {
956 CBS certificate;
957 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
958 al = SSL_AD_DECODE_ERROR;
959 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
960 SSL_R_CERT_LENGTH_MISMATCH);
961 goto f_err;
962 }
963 data = CBS_data(&certificate);
964 x = d2i_X509(NULL, &data, CBS_len(&certificate));
965 if (x == NULL) {
966 al = SSL_AD_BAD_CERTIFICATE;
967 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
968 goto f_err;
969 }
970 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
971 al = SSL_AD_DECODE_ERROR;
972 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
973 SSL_R_CERT_LENGTH_MISMATCH);
974 goto f_err;
975 }
976 if (!sk_X509_push(sk, x)) {
977 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
978 goto err;
979 }
980 x = NULL;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langley1bea1732014-12-17 19:06:57 -0800983 i = ssl_verify_cert_chain(s, sk);
984 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
985 al = ssl_verify_alarm_type(s->verify_result);
986 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
987 SSL_R_CERTIFICATE_VERIFY_FAILED);
988 goto f_err;
989 }
990 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langley1bea1732014-12-17 19:06:57 -0800992 sc = ssl_sess_cert_new();
993 if (sc == NULL) {
994 goto err;
995 }
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langley1bea1732014-12-17 19:06:57 -0800997 if (s->session->sess_cert) {
998 ssl_sess_cert_free(s->session->sess_cert);
999 }
1000 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langley1bea1732014-12-17 19:06:57 -08001002 sc->cert_chain = sk;
1003 /* Inconsistency alert: cert_chain does include the peer's certificate, which
1004 * we don't include in s3_srvr.c */
1005 x = sk_X509_value(sk, 0);
1006 sk = NULL;
1007 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langley1bea1732014-12-17 19:06:57 -08001009 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langley1bea1732014-12-17 19:06:57 -08001011 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1012 x = NULL;
1013 al = SSL3_AL_FATAL;
1014 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1015 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1016 goto f_err;
1017 }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langley1bea1732014-12-17 19:06:57 -08001019 i = ssl_cert_type(pkey);
1020 if (i < 0) {
1021 x = NULL;
1022 al = SSL3_AL_FATAL;
1023 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1024 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1025 goto f_err;
1026 }
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langley1bea1732014-12-17 19:06:57 -08001028 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1029 if (exp_idx >= 0 && i != exp_idx) {
1030 x = NULL;
1031 al = SSL_AD_ILLEGAL_PARAMETER;
1032 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1033 SSL_R_WRONG_CERTIFICATE_TYPE);
1034 goto f_err;
1035 }
1036 sc->peer_cert_type = i;
1037 /* Why would the following ever happen? We just created sc a couple of lines
1038 * ago. */
1039 if (sc->peer_pkeys[i].x509 != NULL) {
1040 X509_free(sc->peer_pkeys[i].x509);
1041 }
1042 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1043 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langley1bea1732014-12-17 19:06:57 -08001045 if (s->session->peer != NULL) {
1046 X509_free(s->session->peer);
1047 }
1048 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001049
Adam Langley1bea1732014-12-17 19:06:57 -08001050 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langley1bea1732014-12-17 19:06:57 -08001052 x = NULL;
1053 ret = 1;
1054
1055 if (0) {
1056 f_err:
1057 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1058 }
1059
1060err:
1061 EVP_PKEY_free(pkey);
1062 X509_free(x);
1063 sk_X509_pop_free(sk, X509_free);
1064 return ret;
1065}
1066
1067int ssl3_get_server_key_exchange(SSL *s) {
1068 EVP_MD_CTX md_ctx;
1069 int al, ok;
1070 long n, alg_k, alg_a;
1071 EVP_PKEY *pkey = NULL;
1072 const EVP_MD *md = NULL;
1073 RSA *rsa = NULL;
1074 DH *dh = NULL;
1075 EC_KEY *ecdh = NULL;
1076 BN_CTX *bn_ctx = NULL;
1077 EC_POINT *srvr_ecpoint = NULL;
1078 CBS server_key_exchange, server_key_exchange_orig, parameter;
1079
1080 /* use same message size as in ssl3_get_certificate_request() as
1081 * ServerKeyExchange message may be skipped */
1082 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1083 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001084 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001085 if (!ok) {
1086 return n;
1087 }
1088
1089 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1090 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1091 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1092 SSL_R_UNEXPECTED_MESSAGE);
1093 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1094 return -1;
1095 }
1096
1097 /* In plain PSK ciphersuite, ServerKeyExchange can be
1098 omitted if no identity hint is sent. Set session->sess_cert anyway to
1099 avoid problems later.*/
1100 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1101 /* PSK ciphersuites that also send a Certificate would have already
1102 * initialized |sess_cert|. */
1103 if (s->session->sess_cert == NULL) {
1104 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001105 if (s->session->sess_cert == NULL) {
1106 return -1;
1107 }
Adam Langley1bea1732014-12-17 19:06:57 -08001108 }
1109
1110 /* TODO(davidben): This should be reset in one place with the rest of the
1111 * handshake state. */
1112 if (s->s3->tmp.peer_psk_identity_hint) {
1113 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1114 s->s3->tmp.peer_psk_identity_hint = NULL;
1115 }
1116 }
1117 s->s3->tmp.reuse_message = 1;
1118 return 1;
1119 }
1120
1121 /* Retain a copy of the original CBS to compute the signature over. */
1122 CBS_init(&server_key_exchange, s->init_msg, n);
1123 server_key_exchange_orig = server_key_exchange;
1124
1125 if (s->session->sess_cert != NULL) {
1126 if (s->session->sess_cert->peer_dh_tmp) {
1127 DH_free(s->session->sess_cert->peer_dh_tmp);
1128 s->session->sess_cert->peer_dh_tmp = NULL;
1129 }
1130 if (s->session->sess_cert->peer_ecdh_tmp) {
1131 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1132 s->session->sess_cert->peer_ecdh_tmp = NULL;
1133 }
1134 } else {
1135 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001136 if (s->session->sess_cert == NULL) {
1137 return -1;
1138 }
Adam Langley1bea1732014-12-17 19:06:57 -08001139 }
1140
1141 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1142 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1143 EVP_MD_CTX_init(&md_ctx);
1144
1145 if (alg_a & SSL_aPSK) {
1146 CBS psk_identity_hint;
1147
1148 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1149 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1150 &psk_identity_hint)) {
1151 al = SSL_AD_DECODE_ERROR;
1152 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1153 goto f_err;
1154 }
1155
1156 /* Store PSK identity hint for later use, hint is used in
1157 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1158 * identity hint can be as long as the maximum length of a PSK identity.
1159 * Also do not allow NULL characters; identities are saved as C strings.
1160 *
1161 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1162 * a specific identity. */
1163 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1164 CBS_contains_zero_byte(&psk_identity_hint)) {
1165 al = SSL_AD_HANDSHAKE_FAILURE;
1166 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1167 SSL_R_DATA_LENGTH_TOO_LONG);
1168 goto f_err;
1169 }
1170
1171 /* Save the identity hint as a C string. */
1172 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1173 al = SSL_AD_INTERNAL_ERROR;
1174 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1175 ERR_R_MALLOC_FAILURE);
1176 goto f_err;
1177 }
1178 }
1179
1180 if (alg_k & SSL_kEDH) {
1181 CBS dh_p, dh_g, dh_Ys;
1182
1183 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1184 CBS_len(&dh_p) == 0 ||
1185 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1186 CBS_len(&dh_g) == 0 ||
1187 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1188 CBS_len(&dh_Ys) == 0) {
1189 al = SSL_AD_DECODE_ERROR;
1190 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1191 goto f_err;
1192 }
1193
1194 dh = DH_new();
1195 if (dh == NULL) {
1196 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1197 goto err;
1198 }
1199
1200 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1201 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1202 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1203 NULL) {
1204 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1205 goto err;
1206 }
1207
1208 if (DH_size(dh) < 512 / 8) {
1209 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1210 SSL_R_BAD_DH_P_LENGTH);
1211 goto err;
1212 }
1213
1214 if (alg_a & SSL_aRSA) {
1215 pkey = X509_get_pubkey(
1216 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1217 }
1218 /* else anonymous DH, so no certificate or pkey. */
1219
1220 s->session->sess_cert->peer_dh_tmp = dh;
1221 dh = NULL;
1222 } else if (alg_k & SSL_kEECDH) {
1223 uint16_t curve_id;
1224 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001225 const EC_GROUP *group;
1226 CBS point;
1227
1228 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1229 * key. Check curve is one of our preferences, if not server has sent an
1230 * invalid curve. */
1231 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1232 al = SSL_AD_DECODE_ERROR;
1233 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1234 goto f_err;
1235 }
1236
1237 curve_nid = tls1_ec_curve_id2nid(curve_id);
1238 if (curve_nid == 0) {
1239 al = SSL_AD_INTERNAL_ERROR;
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1241 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1242 goto f_err;
1243 }
1244
David Benjamin4f7783e2015-03-05 03:05:11 -05001245 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001246 if (ecdh == NULL) {
1247 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001248 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001249 goto err;
1250 }
1251
Adam Langley1bea1732014-12-17 19:06:57 -08001252 group = EC_KEY_get0_group(ecdh);
1253
1254 /* Next, get the encoded ECPoint */
1255 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1256 al = SSL_AD_DECODE_ERROR;
1257 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1258 goto f_err;
1259 }
1260
1261 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1262 ((bn_ctx = BN_CTX_new()) == NULL)) {
1263 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1264 ERR_R_MALLOC_FAILURE);
1265 goto err;
1266 }
1267
1268 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1269 CBS_len(&point), bn_ctx)) {
1270 al = SSL_AD_DECODE_ERROR;
1271 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1272 goto f_err;
1273 }
1274
1275 /* The ECC/TLS specification does not mention the use of DSA to sign
1276 * ECParameters in the server key exchange message. We do support RSA and
1277 * ECDSA. */
1278 if (alg_a & SSL_aRSA) {
1279 pkey = X509_get_pubkey(
1280 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1281 } else if (alg_a & SSL_aECDSA) {
1282 pkey =
1283 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1284 }
1285 /* else anonymous ECDH, so no certificate or pkey. */
1286 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1287 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1288 ecdh = NULL;
1289 BN_CTX_free(bn_ctx);
1290 bn_ctx = NULL;
1291 EC_POINT_free(srvr_ecpoint);
1292 srvr_ecpoint = NULL;
1293 } else if (!(alg_k & SSL_kPSK)) {
1294 al = SSL_AD_UNEXPECTED_MESSAGE;
1295 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1296 SSL_R_UNEXPECTED_MESSAGE);
1297 goto f_err;
1298 }
1299
1300 /* At this point, |server_key_exchange| contains the signature, if any, while
1301 * |server_key_exchange_orig| contains the entire message. From that, derive
1302 * a CBS containing just the parameter. */
1303 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1304 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1305
1306 /* if it was signed, check the signature */
1307 if (pkey != NULL) {
1308 CBS signature;
1309
1310 if (SSL_USE_SIGALGS(s)) {
1311 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1312 goto f_err;
1313 }
1314 } else if (pkey->type == EVP_PKEY_RSA) {
1315 md = EVP_md5_sha1();
1316 } else {
1317 md = EVP_sha1();
1318 }
1319
1320 /* The last field in |server_key_exchange| is the signature. */
1321 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1322 CBS_len(&server_key_exchange) != 0) {
1323 al = SSL_AD_DECODE_ERROR;
1324 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1325 goto f_err;
1326 }
1327
1328 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1329 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1330 SSL3_RANDOM_SIZE) ||
1331 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1332 SSL3_RANDOM_SIZE) ||
1333 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1334 CBS_len(&parameter)) ||
1335 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1336 CBS_len(&signature))) {
1337 /* bad signature */
1338 al = SSL_AD_DECRYPT_ERROR;
1339 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1340 goto f_err;
1341 }
1342 } else {
1343 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1344 /* Might be wrong key type, check it */
1345 if (ssl3_check_cert_and_algorithm(s)) {
1346 /* Otherwise this shouldn't happen */
1347 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1348 ERR_R_INTERNAL_ERROR);
1349 }
1350 goto err;
1351 }
1352 /* still data left over */
1353 if (CBS_len(&server_key_exchange) > 0) {
1354 al = SSL_AD_DECODE_ERROR;
1355 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1356 SSL_R_EXTRA_DATA_IN_MESSAGE);
1357 goto f_err;
1358 }
1359 }
1360 EVP_PKEY_free(pkey);
1361 EVP_MD_CTX_cleanup(&md_ctx);
1362 return 1;
1363
Adam Langley95c29f32014-06-20 12:00:00 -07001364f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001365 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001366err:
Adam Langley1bea1732014-12-17 19:06:57 -08001367 EVP_PKEY_free(pkey);
1368 if (rsa != NULL) {
1369 RSA_free(rsa);
1370 }
1371 if (dh != NULL) {
1372 DH_free(dh);
1373 }
1374 BN_CTX_free(bn_ctx);
1375 EC_POINT_free(srvr_ecpoint);
1376 if (ecdh != NULL) {
1377 EC_KEY_free(ecdh);
1378 }
1379 EVP_MD_CTX_cleanup(&md_ctx);
1380 return -1;
1381}
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langley1bea1732014-12-17 19:06:57 -08001383static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1384 return X509_NAME_cmp(*a, *b);
1385}
Adam Langley95c29f32014-06-20 12:00:00 -07001386
Adam Langley1bea1732014-12-17 19:06:57 -08001387int ssl3_get_certificate_request(SSL *s) {
1388 int ok, ret = 0;
1389 unsigned long n;
1390 X509_NAME *xn = NULL;
1391 STACK_OF(X509_NAME) *ca_sk = NULL;
1392 CBS cbs;
1393 CBS certificate_types;
1394 CBS certificate_authorities;
1395 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langley1bea1732014-12-17 19:06:57 -08001397 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1398 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001399 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001400
Adam Langley1bea1732014-12-17 19:06:57 -08001401 if (!ok) {
1402 return n;
1403 }
David Benjamin688d8df2014-11-02 23:06:42 -05001404
Adam Langley1bea1732014-12-17 19:06:57 -08001405 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langley1bea1732014-12-17 19:06:57 -08001407 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1408 s->s3->tmp.reuse_message = 1;
1409 /* If we get here we don't need any cached handshake records as we wont be
1410 * doing client auth. */
1411 if (s->s3->handshake_buffer &&
1412 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1413 goto err;
1414 }
1415 return 1;
1416 }
David Benjamined439582014-07-14 19:13:02 -04001417
Adam Langley1bea1732014-12-17 19:06:57 -08001418 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1420 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1421 SSL_R_WRONG_MESSAGE_TYPE);
1422 goto err;
1423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langley1bea1732014-12-17 19:06:57 -08001425 /* TLS does not like anon-DH with client cert */
1426 if (s->version > SSL3_VERSION &&
1427 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1428 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1429 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1430 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1431 goto err;
1432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langley1bea1732014-12-17 19:06:57 -08001434 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001435
Adam Langley1bea1732014-12-17 19:06:57 -08001436 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1437 if (ca_sk == NULL) {
1438 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1439 goto err;
1440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langley1bea1732014-12-17 19:06:57 -08001442 /* get the certificate types */
1443 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1444 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1445 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1446 goto err;
1447 }
David Benjamined439582014-07-14 19:13:02 -04001448
Adam Langley1bea1732014-12-17 19:06:57 -08001449 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1450 &s->s3->tmp.num_certificate_types)) {
1451 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1452 goto err;
1453 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001454
Adam Langley1bea1732014-12-17 19:06:57 -08001455 if (SSL_USE_SIGALGS(s)) {
1456 CBS supported_signature_algorithms;
1457 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1458 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1459 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1460 goto err;
1461 }
David Benjamined439582014-07-14 19:13:02 -04001462
Adam Langley1bea1732014-12-17 19:06:57 -08001463 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1464 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1465 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1466 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1467 goto err;
1468 }
1469 }
David Benjamined439582014-07-14 19:13:02 -04001470
Adam Langley1bea1732014-12-17 19:06:57 -08001471 /* get the CA RDNs */
1472 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1473 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1474 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1475 goto err;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langley1bea1732014-12-17 19:06:57 -08001478 while (CBS_len(&certificate_authorities) > 0) {
1479 CBS distinguished_name;
1480 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1481 &distinguished_name)) {
1482 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1483 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1484 SSL_R_CA_DN_TOO_LONG);
1485 goto err;
1486 }
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langley1bea1732014-12-17 19:06:57 -08001488 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001489
Adam Langley1bea1732014-12-17 19:06:57 -08001490 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1491 if (xn == NULL) {
1492 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1493 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1494 goto err;
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langley1bea1732014-12-17 19:06:57 -08001497 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1498 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1499 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1500 goto err;
1501 }
Adam Langley95c29f32014-06-20 12:00:00 -07001502
Adam Langley1bea1732014-12-17 19:06:57 -08001503 if (CBS_len(&distinguished_name) != 0) {
1504 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1505 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1506 SSL_R_CA_DN_LENGTH_MISMATCH);
1507 goto err;
1508 }
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langley1bea1732014-12-17 19:06:57 -08001510 if (!sk_X509_NAME_push(ca_sk, xn)) {
1511 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1512 ERR_R_MALLOC_FAILURE);
1513 goto err;
1514 }
1515 }
Adam Langley95c29f32014-06-20 12:00:00 -07001516
Adam Langley1bea1732014-12-17 19:06:57 -08001517 /* we should setup a certificate to return.... */
1518 s->s3->tmp.cert_req = 1;
1519 if (s->s3->tmp.ca_names != NULL) {
1520 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1521 }
1522 s->s3->tmp.ca_names = ca_sk;
1523 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001524
Adam Langley1bea1732014-12-17 19:06:57 -08001525 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001526
Adam Langley1bea1732014-12-17 19:06:57 -08001527err:
1528 if (ca_sk != NULL) {
1529 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1530 }
1531 return ret;
1532}
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langley1bea1732014-12-17 19:06:57 -08001534int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001535 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001536 long n;
1537 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001538
Adam Langley1bea1732014-12-17 19:06:57 -08001539 n = s->method->ssl_get_message(
1540 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001541 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001542
Adam Langley1bea1732014-12-17 19:06:57 -08001543 if (!ok) {
1544 return n;
1545 }
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langley1bea1732014-12-17 19:06:57 -08001547 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001548
Adam Langley1bea1732014-12-17 19:06:57 -08001549 if (!CBS_get_u32(&new_session_ticket,
1550 &s->session->tlsext_tick_lifetime_hint) ||
1551 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1552 CBS_len(&new_session_ticket) != 0) {
1553 al = SSL_AD_DECODE_ERROR;
1554 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1555 goto f_err;
1556 }
Adam Langley95c29f32014-06-20 12:00:00 -07001557
Adam Langley1bea1732014-12-17 19:06:57 -08001558 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1559 &s->session->tlsext_ticklen)) {
1560 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1561 goto err;
1562 }
Adam Langley95c29f32014-06-20 12:00:00 -07001563
David Benjamin68070622015-02-08 23:44:59 -05001564 /* Generate a session ID for this session based on the session ticket. We use
1565 * the session ID mechanism for detecting ticket resumption. This also fits in
1566 * with assumptions elsewhere in OpenSSL.*/
1567 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1568 &s->session->session_id_length, EVP_sha256(), NULL)) {
1569 goto err;
1570 }
1571
1572 return 1;
David Benjamined439582014-07-14 19:13:02 -04001573
Adam Langley95c29f32014-06-20 12:00:00 -07001574f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001575 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001576err:
Adam Langley1bea1732014-12-17 19:06:57 -08001577 return -1;
1578}
Adam Langley95c29f32014-06-20 12:00:00 -07001579
Adam Langley1bea1732014-12-17 19:06:57 -08001580int ssl3_get_cert_status(SSL *s) {
1581 int ok, al;
1582 long n;
1583 CBS certificate_status, ocsp_response;
1584 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001585
Adam Langley1bea1732014-12-17 19:06:57 -08001586 n = s->method->ssl_get_message(
1587 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001588 SSL3_MT_CERTIFICATE_STATUS, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langley1bea1732014-12-17 19:06:57 -08001590 if (!ok) {
1591 return n;
1592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
Adam Langley1bea1732014-12-17 19:06:57 -08001594 CBS_init(&certificate_status, s->init_msg, n);
1595 if (!CBS_get_u8(&certificate_status, &status_type) ||
1596 status_type != TLSEXT_STATUSTYPE_ocsp ||
1597 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1598 CBS_len(&ocsp_response) == 0 ||
1599 CBS_len(&certificate_status) != 0) {
1600 al = SSL_AD_DECODE_ERROR;
1601 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1602 goto f_err;
1603 }
Adam Langley95c29f32014-06-20 12:00:00 -07001604
Adam Langley1bea1732014-12-17 19:06:57 -08001605 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1606 &s->session->ocsp_response_length)) {
1607 al = SSL_AD_INTERNAL_ERROR;
1608 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1609 goto f_err;
1610 }
1611 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langley95c29f32014-06-20 12:00:00 -07001613f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001614 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1615 return -1;
1616}
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langley1bea1732014-12-17 19:06:57 -08001618int ssl3_get_server_done(SSL *s) {
1619 int ok;
1620 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langley1bea1732014-12-17 19:06:57 -08001622 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1623 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1624 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001625 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 if (!ok) {
1628 return n;
1629 }
David Benjamin46062682014-07-14 19:14:32 -04001630
Adam Langley1bea1732014-12-17 19:06:57 -08001631 if (n > 0) {
1632 /* should contain no data */
1633 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1634 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1635 return -1;
1636 }
David Benjamin46062682014-07-14 19:14:32 -04001637
Adam Langley1bea1732014-12-17 19:06:57 -08001638 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001639}
Adam Langley1258b6a2014-06-20 12:00:00 -07001640
Adam Langley1258b6a2014-06-20 12:00:00 -07001641
Adam Langley1bea1732014-12-17 19:06:57 -08001642int ssl3_send_client_key_exchange(SSL *s) {
1643 uint8_t *p;
1644 int n = 0;
1645 unsigned long alg_k;
1646 unsigned long alg_a;
1647 uint8_t *q;
1648 EVP_PKEY *pkey = NULL;
1649 EC_KEY *clnt_ecdh = NULL;
1650 const EC_POINT *srvr_ecpoint = NULL;
1651 EVP_PKEY *srvr_pub_pkey = NULL;
1652 uint8_t *encodedPoint = NULL;
1653 int encoded_pt_len = 0;
1654 BN_CTX *bn_ctx = NULL;
1655 unsigned int psk_len = 0;
1656 uint8_t psk[PSK_MAX_PSK_LEN];
1657 uint8_t *pms = NULL;
1658 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001659
Adam Langley1bea1732014-12-17 19:06:57 -08001660 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1661 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1664 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 /* If using a PSK key exchange, prepare the pre-shared key. */
1667 if (alg_a & SSL_aPSK) {
1668 char identity[PSK_MAX_IDENTITY_LEN + 1];
1669 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langley1bea1732014-12-17 19:06:57 -08001671 if (s->psk_client_callback == NULL) {
1672 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1673 SSL_R_PSK_NO_CLIENT_CB);
1674 goto err;
1675 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001676
Adam Langley1bea1732014-12-17 19:06:57 -08001677 memset(identity, 0, sizeof(identity));
1678 psk_len =
1679 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1680 sizeof(identity), psk, sizeof(psk));
1681 if (psk_len > PSK_MAX_PSK_LEN) {
1682 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1683 ERR_R_INTERNAL_ERROR);
1684 goto err;
1685 } else if (psk_len == 0) {
1686 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1687 SSL_R_PSK_IDENTITY_NOT_FOUND);
1688 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1689 goto err;
1690 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001691
Adam Langley1bea1732014-12-17 19:06:57 -08001692 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1693 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1694 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1695 ERR_R_INTERNAL_ERROR);
1696 goto err;
1697 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001698
Adam Langley1bea1732014-12-17 19:06:57 -08001699 if (s->session->psk_identity != NULL) {
1700 OPENSSL_free(s->session->psk_identity);
1701 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001702
Adam Langley1bea1732014-12-17 19:06:57 -08001703 s->session->psk_identity = BUF_strdup(identity);
1704 if (s->session->psk_identity == NULL) {
1705 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1706 ERR_R_MALLOC_FAILURE);
1707 goto err;
1708 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001709
Adam Langley1bea1732014-12-17 19:06:57 -08001710 /* Write out psk_identity. */
1711 s2n(identity_len, p);
1712 memcpy(p, identity, identity_len);
1713 p += identity_len;
1714 n = 2 + identity_len;
1715 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001716
Adam Langley1bea1732014-12-17 19:06:57 -08001717 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1718 if (alg_k & SSL_kRSA) {
1719 RSA *rsa;
1720 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001721
Adam Langley1bea1732014-12-17 19:06:57 -08001722 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1723 pms = OPENSSL_malloc(pms_len);
1724 if (pms == NULL) {
1725 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1726 ERR_R_MALLOC_FAILURE);
1727 goto err;
1728 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001729
Adam Langley1bea1732014-12-17 19:06:57 -08001730 if (s->session->sess_cert == NULL) {
1731 /* We should always have a server certificate with SSL_kRSA. */
1732 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1733 ERR_R_INTERNAL_ERROR);
1734 goto err;
1735 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001736
Adam Langley1bea1732014-12-17 19:06:57 -08001737 pkey = X509_get_pubkey(
1738 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1739 if (pkey == NULL ||
1740 pkey->type != EVP_PKEY_RSA ||
1741 pkey->pkey.rsa == NULL) {
1742 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1743 ERR_R_INTERNAL_ERROR);
1744 if (pkey != NULL) {
1745 EVP_PKEY_free(pkey);
1746 }
1747 goto err;
1748 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001749
Adam Langley1bea1732014-12-17 19:06:57 -08001750 rsa = pkey->pkey.rsa;
1751 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001752
Adam Langley1bea1732014-12-17 19:06:57 -08001753 pms[0] = s->client_version >> 8;
1754 pms[1] = s->client_version & 0xff;
1755 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1756 goto err;
1757 }
1758
1759 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1760
1761 q = p;
1762 /* In TLS and beyond, reserve space for the length prefix. */
1763 if (s->version > SSL3_VERSION) {
1764 p += 2;
1765 n += 2;
1766 }
1767 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1768 RSA_PKCS1_PADDING)) {
1769 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1770 SSL_R_BAD_RSA_ENCRYPT);
1771 goto err;
1772 }
1773 n += enc_pms_len;
1774
1775 /* Log the premaster secret, if logging is enabled. */
1776 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1777 pms_len)) {
1778 goto err;
1779 }
1780
1781 /* Fill in the length prefix. */
1782 if (s->version > SSL3_VERSION) {
1783 s2n(enc_pms_len, q);
1784 }
1785 } else if (alg_k & SSL_kEDH) {
1786 DH *dh_srvr, *dh_clnt;
1787 SESS_CERT *scert = s->session->sess_cert;
1788 int dh_len;
1789 size_t pub_len;
1790
1791 if (scert == NULL) {
1792 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1793 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1794 SSL_R_UNEXPECTED_MESSAGE);
1795 goto err;
1796 }
1797
1798 if (scert->peer_dh_tmp == NULL) {
1799 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1800 ERR_R_INTERNAL_ERROR);
1801 goto err;
1802 }
1803 dh_srvr = scert->peer_dh_tmp;
1804
1805 /* generate a new random key */
1806 dh_clnt = DHparams_dup(dh_srvr);
1807 if (dh_clnt == NULL) {
1808 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1809 goto err;
1810 }
1811 if (!DH_generate_key(dh_clnt)) {
1812 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1813 DH_free(dh_clnt);
1814 goto err;
1815 }
1816
1817 pms_len = DH_size(dh_clnt);
1818 pms = OPENSSL_malloc(pms_len);
1819 if (pms == NULL) {
1820 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1821 ERR_R_MALLOC_FAILURE);
1822 DH_free(dh_clnt);
1823 goto err;
1824 }
1825
1826 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1827 if (dh_len <= 0) {
1828 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1829 DH_free(dh_clnt);
1830 goto err;
1831 }
1832 pms_len = dh_len;
1833
1834 /* send off the data */
1835 pub_len = BN_num_bytes(dh_clnt->pub_key);
1836 s2n(pub_len, p);
1837 BN_bn2bin(dh_clnt->pub_key, p);
1838 n += 2 + pub_len;
1839
1840 DH_free(dh_clnt);
1841 } else if (alg_k & SSL_kEECDH) {
1842 const EC_GROUP *srvr_group = NULL;
1843 EC_KEY *tkey;
1844 int field_size = 0, ecdh_len;
1845
1846 if (s->session->sess_cert == NULL) {
1847 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1848 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1849 SSL_R_UNEXPECTED_MESSAGE);
1850 goto err;
1851 }
1852
1853 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1854 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1855 ERR_R_INTERNAL_ERROR);
1856 goto err;
1857 }
1858
1859 tkey = s->session->sess_cert->peer_ecdh_tmp;
1860
1861 srvr_group = EC_KEY_get0_group(tkey);
1862 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1863 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1864 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1865 ERR_R_INTERNAL_ERROR);
1866 goto err;
1867 }
1868
1869 clnt_ecdh = EC_KEY_new();
1870 if (clnt_ecdh == NULL) {
1871 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1872 ERR_R_MALLOC_FAILURE);
1873 goto err;
1874 }
1875
1876 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1877 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1878 goto err;
1879 }
1880
1881 /* Generate a new ECDH key pair */
1882 if (!EC_KEY_generate_key(clnt_ecdh)) {
1883 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1884 goto err;
1885 }
1886
1887 field_size = EC_GROUP_get_degree(srvr_group);
1888 if (field_size <= 0) {
1889 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1890 goto err;
1891 }
1892
1893 pms_len = (field_size + 7) / 8;
1894 pms = OPENSSL_malloc(pms_len);
1895 if (pms == NULL) {
1896 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1897 ERR_R_MALLOC_FAILURE);
1898 goto err;
1899 }
1900
1901 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1902 if (ecdh_len <= 0) {
1903 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1904 goto err;
1905 }
1906 pms_len = ecdh_len;
1907
1908 /* First check the size of encoding and allocate memory accordingly. */
1909 encoded_pt_len =
1910 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1911 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1912
1913 encodedPoint =
1914 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1915 bn_ctx = BN_CTX_new();
1916 if (encodedPoint == NULL || bn_ctx == NULL) {
1917 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1918 ERR_R_MALLOC_FAILURE);
1919 goto err;
1920 }
1921
1922 /* Encode the public key */
1923 encoded_pt_len = EC_POINT_point2oct(
1924 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1925 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1926
1927 *p = encoded_pt_len; /* length of encoded point */
1928 /* Encoded point will be copied here */
1929 p += 1;
1930 n += 1;
1931 /* copy the point */
1932 memcpy(p, encodedPoint, encoded_pt_len);
1933 /* increment n to account for length field */
1934 n += encoded_pt_len;
1935
1936 /* Free allocated memory */
1937 BN_CTX_free(bn_ctx);
1938 bn_ctx = NULL;
1939 OPENSSL_free(encodedPoint);
1940 encodedPoint = NULL;
1941 EC_KEY_free(clnt_ecdh);
1942 clnt_ecdh = NULL;
1943 EVP_PKEY_free(srvr_pub_pkey);
1944 srvr_pub_pkey = NULL;
1945 } else if (alg_k & SSL_kPSK) {
1946 /* For plain PSK, other_secret is a block of 0s with the same length as
1947 * the pre-shared key. */
1948 pms_len = psk_len;
1949 pms = OPENSSL_malloc(pms_len);
1950 if (pms == NULL) {
1951 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1952 ERR_R_MALLOC_FAILURE);
1953 goto err;
1954 }
1955 memset(pms, 0, pms_len);
1956 } else {
1957 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1958 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1959 ERR_R_INTERNAL_ERROR);
1960 goto err;
1961 }
1962
1963 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1964 * key. */
1965 if (alg_a & SSL_aPSK) {
1966 CBB cbb, child;
1967 uint8_t *new_pms;
1968 size_t new_pms_len;
1969
1970 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1971 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1972 ERR_R_MALLOC_FAILURE);
1973 goto err;
1974 }
1975 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1976 !CBB_add_bytes(&child, pms, pms_len) ||
1977 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1978 !CBB_add_bytes(&child, psk, psk_len) ||
1979 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1980 CBB_cleanup(&cbb);
1981 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1982 ERR_R_INTERNAL_ERROR);
1983 goto err;
1984 }
1985 OPENSSL_cleanse(pms, pms_len);
1986 OPENSSL_free(pms);
1987 pms = new_pms;
1988 pms_len = new_pms_len;
1989 }
1990
1991 /* The message must be added to the finished hash before calculating the
1992 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001993 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1994 goto err;
1995 }
Adam Langley1bea1732014-12-17 19:06:57 -08001996 s->state = SSL3_ST_CW_KEY_EXCH_B;
1997
1998 s->session->master_key_length = s->enc_method->generate_master_secret(
1999 s, s->session->master_key, pms, pms_len);
2000 if (s->session->master_key_length == 0) {
2001 goto err;
2002 }
2003 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2004 OPENSSL_cleanse(pms, pms_len);
2005 OPENSSL_free(pms);
2006 }
2007
2008 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05002009 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002010
2011err:
Adam Langley1bea1732014-12-17 19:06:57 -08002012 BN_CTX_free(bn_ctx);
2013 if (encodedPoint != NULL) {
2014 OPENSSL_free(encodedPoint);
2015 }
2016 if (clnt_ecdh != NULL) {
2017 EC_KEY_free(clnt_ecdh);
2018 }
2019 EVP_PKEY_free(srvr_pub_pkey);
2020 if (pms) {
2021 OPENSSL_cleanse(pms, pms_len);
2022 OPENSSL_free(pms);
2023 }
2024 return -1;
2025}
Adam Langley1258b6a2014-06-20 12:00:00 -07002026
Adam Langley1bea1732014-12-17 19:06:57 -08002027int ssl3_send_cert_verify(SSL *s) {
2028 uint8_t *buf, *p;
2029 const EVP_MD *md = NULL;
2030 uint8_t digest[EVP_MAX_MD_SIZE];
2031 size_t digest_length;
2032 EVP_PKEY *pkey;
2033 EVP_PKEY_CTX *pctx = NULL;
2034 size_t signature_length = 0;
2035 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002036
Adam Langley1bea1732014-12-17 19:06:57 -08002037 buf = (uint8_t *)s->init_buf->data;
2038
2039 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2040 p = ssl_handshake_start(s);
2041 pkey = s->cert->key->privatekey;
2042
2043 /* Write out the digest type if needbe. */
2044 if (SSL_USE_SIGALGS(s)) {
2045 md = tls1_choose_signing_digest(s, pkey);
2046 if (!tls12_get_sigandhash(p, pkey, md)) {
2047 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2048 goto err;
2049 }
2050 p += 2;
2051 n += 2;
2052 }
2053
2054 /* Compute the digest. */
2055 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2056 goto err;
2057 }
2058
2059 /* The handshake buffer is no longer necessary. */
2060 if (s->s3->handshake_buffer &&
2061 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2062 goto err;
2063 }
2064
2065 /* Sign the digest. */
2066 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2067 if (pctx == NULL) {
2068 goto err;
2069 }
2070
2071 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2072 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2073 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2074 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2075 goto err;
2076 }
2077
2078 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2079 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2080 goto err;
2081 }
2082
2083 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2084 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2085 goto err;
2086 }
2087
2088 s2n(signature_length, p);
2089 n += signature_length + 2;
2090
David Benjaminfbdfefb2015-02-16 19:33:53 -05002091 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2092 goto err;
2093 }
Adam Langley1bea1732014-12-17 19:06:57 -08002094 s->state = SSL3_ST_CW_CERT_VRFY_B;
2095 }
2096
2097 EVP_PKEY_CTX_free(pctx);
2098 return ssl_do_write(s);
2099
2100err:
2101 EVP_PKEY_CTX_free(pctx);
2102 return -1;
2103}
2104
2105/* ssl3_has_client_certificate returns true if a client certificate is
2106 * configured. */
2107static int ssl3_has_client_certificate(SSL *s) {
2108 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2109}
2110
2111int ssl3_send_client_certificate(SSL *s) {
2112 X509 *x509 = NULL;
2113 EVP_PKEY *pkey = NULL;
2114 int i;
2115
2116 if (s->state == SSL3_ST_CW_CERT_A) {
2117 /* Let cert callback update client certificates if required */
2118 if (s->cert->cert_cb) {
2119 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2120 if (i < 0) {
2121 s->rwstate = SSL_X509_LOOKUP;
2122 return -1;
2123 }
2124 if (i == 0) {
2125 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2126 return 0;
2127 }
2128 s->rwstate = SSL_NOTHING;
2129 }
2130
2131 if (ssl3_has_client_certificate(s)) {
2132 s->state = SSL3_ST_CW_CERT_C;
2133 } else {
2134 s->state = SSL3_ST_CW_CERT_B;
2135 }
2136 }
2137
2138 /* We need to get a client cert */
2139 if (s->state == SSL3_ST_CW_CERT_B) {
2140 /* If we get an error, we need to:
2141 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2142 * We then get retried later */
2143 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2144 if (i < 0) {
2145 s->rwstate = SSL_X509_LOOKUP;
2146 return -1;
2147 }
2148 s->rwstate = SSL_NOTHING;
2149 if (i == 1 && pkey != NULL && x509 != NULL) {
2150 s->state = SSL3_ST_CW_CERT_B;
2151 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2152 i = 0;
2153 }
2154 } else if (i == 1) {
2155 i = 0;
2156 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2157 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2158 }
2159
2160 if (x509 != NULL) {
2161 X509_free(x509);
2162 }
2163 if (pkey != NULL) {
2164 EVP_PKEY_free(pkey);
2165 }
2166 if (i && !ssl3_has_client_certificate(s)) {
2167 i = 0;
2168 }
2169 if (i == 0) {
2170 if (s->version == SSL3_VERSION) {
2171 s->s3->tmp.cert_req = 0;
2172 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2173 return 1;
2174 } else {
2175 s->s3->tmp.cert_req = 2;
2176 }
2177 }
2178
2179 /* Ok, we have a cert */
2180 s->state = SSL3_ST_CW_CERT_C;
2181 }
2182
2183 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002184 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2185 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2186 return -1;
2187 }
Adam Langley1bea1732014-12-17 19:06:57 -08002188 }
2189
2190 /* SSL3_ST_CW_CERT_D */
2191 return ssl_do_write(s);
2192}
2193
2194#define has_bits(i, m) (((i) & (m)) == (m))
2195
2196int ssl3_check_cert_and_algorithm(SSL *s) {
2197 int i, idx;
2198 long alg_k, alg_a;
2199 EVP_PKEY *pkey = NULL;
2200 SESS_CERT *sc;
2201 DH *dh;
2202
2203 /* we don't have a certificate */
2204 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2205 return 1;
2206 }
2207
2208 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2209 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2210
2211 sc = s->session->sess_cert;
2212 if (sc == NULL) {
2213 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2214 goto err;
2215 }
2216
2217 dh = s->session->sess_cert->peer_dh_tmp;
2218
2219 /* This is the passed certificate */
2220
2221 idx = sc->peer_cert_type;
2222 if (idx == SSL_PKEY_ECC) {
2223 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2224 /* check failed */
2225 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2226 goto f_err;
2227 } else {
2228 return 1;
2229 }
2230 } else if (alg_a & SSL_aECDSA) {
2231 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2232 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2233 goto f_err;
2234 }
2235 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2236 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2237 EVP_PKEY_free(pkey);
2238
2239 /* Check that we have a certificate if we require one */
2240 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2241 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2242 SSL_R_MISSING_RSA_SIGNING_CERT);
2243 goto f_err;
2244 }
2245
2246 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2247 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2248 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2249 goto f_err;
2250 }
2251
2252 if ((alg_k & SSL_kEDH) &&
2253 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2254 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2255 goto f_err;
2256 }
2257
2258 return 1;
2259
2260f_err:
2261 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2262err:
2263 return 0;
2264}
2265
2266int ssl3_send_next_proto(SSL *s) {
2267 unsigned int len, padding_len;
2268 uint8_t *d, *p;
2269
2270 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2271 len = s->next_proto_negotiated_len;
2272 padding_len = 32 - ((len + 2) % 32);
2273
2274 d = p = ssl_handshake_start(s);
2275 *(p++) = len;
2276 memcpy(p, s->next_proto_negotiated, len);
2277 p += len;
2278 *(p++) = padding_len;
2279 memset(p, 0, padding_len);
2280 p += padding_len;
2281
David Benjaminfbdfefb2015-02-16 19:33:53 -05002282 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2283 return -1;
2284 }
Adam Langley1bea1732014-12-17 19:06:57 -08002285 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2286 }
2287
2288 return ssl_do_write(s);
2289}
2290
2291int ssl3_send_channel_id(SSL *s) {
2292 uint8_t *d;
2293 int ret = -1, public_key_len;
2294 EVP_MD_CTX md_ctx;
2295 size_t sig_len;
2296 ECDSA_SIG *sig = NULL;
2297 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2298
2299 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2300 return ssl_do_write(s);
2301 }
2302
2303 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2304 EVP_PKEY *key = NULL;
2305 s->ctx->channel_id_cb(s, &key);
2306 if (key != NULL) {
2307 s->tlsext_channel_id_private = key;
2308 }
2309 }
2310
2311 if (!s->tlsext_channel_id_private) {
2312 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2313 return -1;
2314 }
2315 s->rwstate = SSL_NOTHING;
2316
2317 d = ssl_handshake_start(s);
2318 if (s->s3->tlsext_channel_id_new) {
2319 s2n(TLSEXT_TYPE_channel_id_new, d);
2320 } else {
2321 s2n(TLSEXT_TYPE_channel_id, d);
2322 }
2323 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2324
2325 EVP_MD_CTX_init(&md_ctx);
2326
2327 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2328 if (public_key_len <= 0) {
2329 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2330 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2331 goto err;
2332 }
2333
2334 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2335 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2336 * field elements as 32-byte, big-endian numbers. */
2337 if (public_key_len != 65) {
2338 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2339 goto err;
2340 }
2341 public_key = OPENSSL_malloc(public_key_len);
2342 if (!public_key) {
2343 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2344 goto err;
2345 }
2346
2347 derp = public_key;
2348 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2349
2350 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2351 s->tlsext_channel_id_private) != 1) {
2352 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2353 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2354 goto err;
2355 }
2356
2357 if (!tls1_channel_id_hash(&md_ctx, s)) {
2358 goto err;
2359 }
2360
2361 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2362 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2363 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2364 goto err;
2365 }
2366
2367 der_sig = OPENSSL_malloc(sig_len);
2368 if (!der_sig) {
2369 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2370 goto err;
2371 }
2372
2373 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2374 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2375 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2376 goto err;
2377 }
2378
2379 derp = der_sig;
2380 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2381 if (sig == NULL) {
2382 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2383 goto err;
2384 }
2385
2386 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2387 memcpy(d, public_key + 1, 64);
2388 d += 64;
2389 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2390 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2391 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2392 goto err;
2393 }
2394
David Benjaminfbdfefb2015-02-16 19:33:53 -05002395 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2396 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2397 goto err;
2398 }
Adam Langley1bea1732014-12-17 19:06:57 -08002399 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2400
2401 ret = ssl_do_write(s);
2402
2403err:
2404 EVP_MD_CTX_cleanup(&md_ctx);
2405 if (public_key) {
2406 OPENSSL_free(public_key);
2407 }
2408 if (der_sig) {
2409 OPENSSL_free(der_sig);
2410 }
2411 if (sig) {
2412 ECDSA_SIG_free(sig);
2413 }
2414
2415 return ret;
2416}
2417
2418int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2419 int i = 0;
2420 if (s->ctx->client_cert_cb) {
2421 i = s->ctx->client_cert_cb(s, px509, ppkey);
2422 }
2423 return i;
2424}