blob: b9154ab993cc6cbec59ea7356651f62d0b346560 [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;
David Benjamin8b368412015-03-14 01:54:17 -0400393 if (!s->enc_method->setup_key_block(s) ||
394 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800395 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
396 ret = -1;
397 goto end;
398 }
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langley1bea1732014-12-17 19:06:57 -0800400 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langley1bea1732014-12-17 19:06:57 -0800402 case SSL3_ST_CW_NEXT_PROTO_A:
403 case SSL3_ST_CW_NEXT_PROTO_B:
404 ret = ssl3_send_next_proto(s);
405 if (ret <= 0) {
406 goto end;
407 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700408
Adam Langley1bea1732014-12-17 19:06:57 -0800409 if (s->s3->tlsext_channel_id_valid) {
410 s->state = SSL3_ST_CW_CHANNEL_ID_A;
411 } else {
412 s->state = SSL3_ST_CW_FINISHED_A;
413 }
414 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langley1bea1732014-12-17 19:06:57 -0800416 case SSL3_ST_CW_CHANNEL_ID_A:
417 case SSL3_ST_CW_CHANNEL_ID_B:
418 ret = ssl3_send_channel_id(s);
419 if (ret <= 0) {
420 goto end;
421 }
422 s->state = SSL3_ST_CW_FINISHED_A;
423 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langley1bea1732014-12-17 19:06:57 -0800425 case SSL3_ST_CW_FINISHED_A:
426 case SSL3_ST_CW_FINISHED_B:
427 ret =
428 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
429 s->enc_method->client_finished_label,
430 s->enc_method->client_finished_label_len);
431 if (ret <= 0) {
432 goto end;
433 }
434 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley1bea1732014-12-17 19:06:57 -0800436 if (s->hit) {
437 s->s3->tmp.next_state = SSL_ST_OK;
438 } else {
439 /* This is a non-resumption handshake. If it involves ChannelID, then
440 * record the handshake hashes at this point in the session so that
441 * any resumption of this session with ChannelID can sign those
442 * hashes. */
443 if (s->s3->tlsext_channel_id_new) {
444 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500445 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800446 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500447 }
Adam Langley1bea1732014-12-17 19:06:57 -0800448 }
David Benjamined7c4752015-02-16 19:16:46 -0500449 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
450 ssl3_can_false_start(s) &&
451 /* No False Start on renegotiation (would complicate the state
452 * machine). */
Adam Langley1bea1732014-12-17 19:06:57 -0800453 s->s3->previous_server_finished_len == 0) {
David Benjamined7c4752015-02-16 19:16:46 -0500454 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800455 } else {
456 /* Allow NewSessionTicket if ticket expected */
457 if (s->tlsext_ticket_expected) {
458 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
459 } else {
460 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
461 }
462 }
463 }
464 s->init_num = 0;
465 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langley1bea1732014-12-17 19:06:57 -0800467 case SSL3_ST_CR_SESSION_TICKET_A:
468 case SSL3_ST_CR_SESSION_TICKET_B:
469 ret = ssl3_get_new_session_ticket(s);
470 if (ret <= 0) {
471 goto end;
472 }
473 s->state = SSL3_ST_CR_CHANGE;
474 s->init_num = 0;
475 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langley1bea1732014-12-17 19:06:57 -0800477 case SSL3_ST_CR_CERT_STATUS_A:
478 case SSL3_ST_CR_CERT_STATUS_B:
479 ret = ssl3_get_cert_status(s);
480 if (ret <= 0) {
481 goto end;
482 }
483 s->state = SSL3_ST_CR_KEY_EXCH_A;
484 s->init_num = 0;
485 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400486
Adam Langley1bea1732014-12-17 19:06:57 -0800487 case SSL3_ST_CR_CHANGE:
488 /* At this point, the next message must be entirely behind a
489 * ChangeCipherSpec. */
490 if (!ssl3_expect_change_cipher_spec(s)) {
491 ret = -1;
492 goto end;
493 }
494 s->state = SSL3_ST_CR_FINISHED_A;
495 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langley1bea1732014-12-17 19:06:57 -0800497 case SSL3_ST_CR_FINISHED_A:
498 case SSL3_ST_CR_FINISHED_B:
499 ret =
500 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
501 if (ret <= 0) {
502 goto end;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langley1bea1732014-12-17 19:06:57 -0800505 if (s->hit) {
506 s->state = SSL3_ST_CW_CHANGE_A;
507 } else {
508 s->state = SSL_ST_OK;
509 }
510 s->init_num = 0;
511 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langley1bea1732014-12-17 19:06:57 -0800513 case SSL3_ST_CW_FLUSH:
514 s->rwstate = SSL_WRITING;
515 if (BIO_flush(s->wbio) <= 0) {
516 ret = -1;
517 goto end;
518 }
519 s->rwstate = SSL_NOTHING;
520 s->state = s->s3->tmp.next_state;
521 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700522
David Benjamined7c4752015-02-16 19:16:46 -0500523 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800524 /* Allow NewSessionTicket if ticket expected */
525 if (s->tlsext_ticket_expected) {
526 s->state = SSL3_ST_CR_SESSION_TICKET_A;
527 } else {
528 s->state = SSL3_ST_CR_CHANGE;
529 }
David Benjamined7c4752015-02-16 19:16:46 -0500530 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700531
Adam Langley1bea1732014-12-17 19:06:57 -0800532 ssl_free_wbio_buffer(s);
533 ret = 1;
534 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 case SSL_ST_OK:
537 /* clean a few things up */
538 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 if (s->init_buf != NULL) {
541 BUF_MEM_free(s->init_buf);
542 s->init_buf = NULL;
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley1bea1732014-12-17 19:06:57 -0800545 /* Remove write buffering now. */
546 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langley1bea1732014-12-17 19:06:57 -0800548 s->init_num = 0;
549 s->renegotiate = 0;
550 s->new_session = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500551 s->s3->tmp.in_false_start = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
554 if (s->hit) {
555 s->ctx->stats.sess_hit++;
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langley1bea1732014-12-17 19:06:57 -0800558 ret = 1;
559 /* s->server=0; */
560 s->ctx->stats.sess_connect_good++;
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langley1bea1732014-12-17 19:06:57 -0800562 if (cb != NULL) {
563 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
564 }
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langley1bea1732014-12-17 19:06:57 -0800566 goto end;
567
568 default:
569 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
570 ret = -1;
571 goto end;
572 }
573
574 if (!s->s3->tmp.reuse_message && !skip) {
575 if (cb != NULL && s->state != state) {
576 new_state = s->state;
577 s->state = state;
578 cb(s, SSL_CB_CONNECT_LOOP, 1);
579 s->state = new_state;
580 }
581 }
582 skip = 0;
583 }
584
Adam Langley95c29f32014-06-20 12:00:00 -0700585end:
Adam Langley1bea1732014-12-17 19:06:57 -0800586 s->in_handshake--;
587 if (buf != NULL) {
588 BUF_MEM_free(buf);
589 }
590 if (cb != NULL) {
591 cb(s, SSL_CB_CONNECT_EXIT, ret);
592 }
593 return ret;
594}
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langley1bea1732014-12-17 19:06:57 -0800596int ssl3_send_client_hello(SSL *s) {
597 uint8_t *buf, *p, *d;
598 int i;
599 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langley1bea1732014-12-17 19:06:57 -0800601 buf = (uint8_t *)s->init_buf->data;
602 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
603 if (!s->s3->have_version) {
604 uint16_t max_version = ssl3_get_max_client_version(s);
605 /* Disabling all versions is silly: return an error. */
606 if (max_version == 0) {
607 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
608 goto err;
609 }
610 s->version = max_version;
611 s->client_version = max_version;
612 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500613
Adam Langley1bea1732014-12-17 19:06:57 -0800614 /* If the configured session was created at a version higher than our
615 * maximum version, drop it. */
616 if (s->session &&
617 (s->session->session_id_length == 0 || s->session->not_resumable ||
618 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
619 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
620 SSL_set_session(s, NULL);
621 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500622
Adam Langley1bea1732014-12-17 19:06:57 -0800623 /* else use the pre-loaded session */
624 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley1bea1732014-12-17 19:06:57 -0800626 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
627 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400628 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
629 !ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random))) {
630 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800631 }
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langley1bea1732014-12-17 19:06:57 -0800633 /* Do the message type and length last. Note: the final argument to
634 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
635 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langley1bea1732014-12-17 19:06:57 -0800637 /* version indicates the negotiated version: for example from an SSLv2/v3
638 * compatible client hello). The client_version field is the maximum
639 * version we permit and it is also used in RSA encrypted premaster
640 * secrets. Some servers can choke if we initially report a higher version
641 * then renegotiate to a lower one in the premaster secret. This didn't
642 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
643 * or later if the server only supports 1.0.
644 *
645 * Possible scenario with previous logic:
646 * 1. Client hello indicates TLS 1.2
647 * 2. Server hello says TLS 1.0
648 * 3. RSA encrypted premaster secret uses 1.2.
649 * 4. Handhaked proceeds using TLS 1.0.
650 * 5. Server sends hello request to renegotiate.
651 * 6. Client hello indicates TLS v1.0 as we now
652 * know that is maximum server supports.
653 * 7. Server chokes on RSA encrypted premaster secret
654 * containing version 1.0.
655 *
656 * For interoperability it should be OK to always use the maximum version
657 * we support in client hello and then rely on the checking of version to
658 * ensure the servers isn't being inconsistent: for example initially
659 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
660 * using client_version in client hello and not resetting it to the
661 * negotiated version. */
662 *(p++) = s->client_version >> 8;
663 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley1bea1732014-12-17 19:06:57 -0800665 /* Random stuff */
666 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
667 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley1bea1732014-12-17 19:06:57 -0800669 /* Session ID */
670 if (s->new_session || s->session == NULL) {
671 i = 0;
672 } else {
673 i = s->session->session_id_length;
674 }
675 *(p++) = i;
676 if (i != 0) {
677 if (i > (int)sizeof(s->session->session_id)) {
678 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
679 goto err;
680 }
681 memcpy(p, s->session->session_id, i);
682 p += i;
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langley1bea1732014-12-17 19:06:57 -0800685 /* cookie stuff for DTLS */
686 if (SSL_IS_DTLS(s)) {
687 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
688 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
689 goto err;
690 }
691 *(p++) = s->d1->cookie_len;
692 memcpy(p, s->d1->cookie, s->d1->cookie_len);
693 p += s->d1->cookie_len;
694 }
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langley1bea1732014-12-17 19:06:57 -0800696 /* Ciphers supported */
697 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
698 if (i == 0) {
699 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
700 SSL_R_NO_CIPHERS_AVAILABLE);
701 goto err;
702 }
703 s2n(i, p);
704 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langley1bea1732014-12-17 19:06:57 -0800706 /* COMPRESSION */
707 *(p++) = 1;
708 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley1bea1732014-12-17 19:06:57 -0800710 /* TLS extensions*/
711 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
712 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
713 goto err;
714 }
715
716 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
717 p - buf);
718 if (p == NULL) {
719 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
720 goto err;
721 }
722
723 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500724 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
725 goto err;
726 }
Adam Langley1bea1732014-12-17 19:06:57 -0800727 s->state = SSL3_ST_CW_CLNT_HELLO_B;
728 }
729
730 /* SSL3_ST_CW_CLNT_HELLO_B */
731 return ssl_do_write(s);
732
Adam Langley95c29f32014-06-20 12:00:00 -0700733err:
Adam Langley1bea1732014-12-17 19:06:57 -0800734 return -1;
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langley1bea1732014-12-17 19:06:57 -0800737int ssl3_get_server_hello(SSL *s) {
738 STACK_OF(SSL_CIPHER) * sk;
739 const SSL_CIPHER *c;
740 CERT *ct = s->cert;
741 int al = SSL_AD_INTERNAL_ERROR, ok;
742 long n;
743 CBS server_hello, server_random, session_id;
744 uint16_t server_version, cipher_suite;
745 uint8_t compression_method;
746 unsigned long mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langley1bea1732014-12-17 19:06:57 -0800748 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
749 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
750 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500751 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langley1bea1732014-12-17 19:06:57 -0800753 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500754 uint32_t err = ERR_peek_error();
755 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
756 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
757 /* Add a dedicated error code to the queue for a handshake_failure alert
758 * in response to ClientHello. This matches NSS's client behavior and
759 * gives a better error on a (probable) failure to negotiate initial
760 * parameters. Note: this error code comes after the original one.
761 *
762 * See https://crbug.com/446505. */
763 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
764 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
765 }
Adam Langley1bea1732014-12-17 19:06:57 -0800766 return n;
767 }
Adam Langley95c29f32014-06-20 12:00:00 -0700768
Adam Langley1bea1732014-12-17 19:06:57 -0800769 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400770
Adam Langley1bea1732014-12-17 19:06:57 -0800771 if (!CBS_get_u16(&server_hello, &server_version) ||
772 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
773 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
774 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
775 !CBS_get_u16(&server_hello, &cipher_suite) ||
776 !CBS_get_u8(&server_hello, &compression_method)) {
777 al = SSL_AD_DECODE_ERROR;
778 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
779 goto f_err;
780 }
David Benjamina03d95d2014-07-12 19:49:07 -0400781
Adam Langley1bea1732014-12-17 19:06:57 -0800782 if (!s->s3->have_version) {
783 if (!ssl3_is_version_enabled(s, server_version)) {
784 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
785 s->version = server_version;
786 /* Mark the version as fixed so the record-layer version is not clamped
787 * to TLS 1.0. */
788 s->s3->have_version = 1;
789 al = SSL_AD_PROTOCOL_VERSION;
790 goto f_err;
791 }
792 s->version = server_version;
793 s->enc_method = ssl3_get_enc_method(server_version);
794 assert(s->enc_method != NULL);
795 /* At this point, the connection's version is known and s->version is
796 * fixed. Begin enforcing the record-layer version. */
797 s->s3->have_version = 1;
798 } else if (server_version != s->version) {
799 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
800 al = SSL_AD_PROTOCOL_VERSION;
801 goto f_err;
802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langley1bea1732014-12-17 19:06:57 -0800804 /* Copy over the server random. */
805 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langley1bea1732014-12-17 19:06:57 -0800807 assert(s->session == NULL || s->session->session_id_length > 0);
808 if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
809 s->session->session_id_length)) {
810 if (s->sid_ctx_length != s->session->sid_ctx_length ||
811 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
812 /* actually a client application bug */
813 al = SSL_AD_ILLEGAL_PARAMETER;
814 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
815 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
816 goto f_err;
817 }
818 s->hit = 1;
819 } else {
820 /* The session wasn't resumed. Create a fresh SSL_SESSION to
821 * fill out. */
822 s->hit = 0;
823 if (!ssl_get_new_session(s, 0)) {
824 goto f_err;
825 }
826 /* Note: session_id could be empty. */
827 s->session->session_id_length = CBS_len(&session_id);
828 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
829 }
David Benjamina03d95d2014-07-12 19:49:07 -0400830
Adam Langley1bea1732014-12-17 19:06:57 -0800831 c = ssl3_get_cipher_by_value(cipher_suite);
832 if (c == NULL) {
833 /* unknown cipher */
834 al = SSL_AD_ILLEGAL_PARAMETER;
835 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
836 SSL_R_UNKNOWN_CIPHER_RETURNED);
837 goto f_err;
838 }
839 /* ct->mask_ssl was computed from client capabilities. Now
840 * that the final version is known, compute a new mask_ssl. */
841 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
842 mask_ssl = SSL_TLSV1_2;
843 } else {
844 mask_ssl = 0;
845 }
846 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
847 * the server selected it, it's an error. */
848 if ((c->algorithm_ssl & mask_ssl) ||
849 (c->algorithm_mkey & ct->mask_k) ||
850 (c->algorithm_auth & ct->mask_a)) {
851 al = SSL_AD_ILLEGAL_PARAMETER;
852 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
853 goto f_err;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langley1bea1732014-12-17 19:06:57 -0800856 sk = ssl_get_ciphers_by_id(s);
857 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
858 /* we did not say we would use this cipher */
859 al = SSL_AD_ILLEGAL_PARAMETER;
860 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
861 goto f_err;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjaminf3a8b122014-12-25 23:11:49 -0500864 if (s->hit && s->session->cipher != c) {
Adam Langley1bea1732014-12-17 19:06:57 -0800865 al = SSL_AD_ILLEGAL_PARAMETER;
866 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
867 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
868 goto f_err;
869 }
870 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500871
Adam Langley1bea1732014-12-17 19:06:57 -0800872 /* Most clients also require that the negotiated version match the session's
873 * version if resuming. However OpenSSL has historically not had the
874 * corresponding logic on the server, so this may not be compatible,
875 * depending on other factors. (Whether the ClientHello version is clamped to
876 * the session's version and whether the session cache is keyed on IP
877 * address.)
878 *
879 * TODO(davidben): See if we can still enforce this? Perhaps for the future
880 * TLS 1.3 and forward if this is fixed upstream. */
David Benjaminbdf5e722014-11-11 00:52:15 -0500881
Adam Langley1bea1732014-12-17 19:06:57 -0800882 /* Don't digest cached records if no sigalgs: we may need them for client
883 * authentication. */
884 if (!SSL_USE_SIGALGS(s) &&
885 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
886 goto f_err;
887 }
David Benjamina03d95d2014-07-12 19:49:07 -0400888
Adam Langley1bea1732014-12-17 19:06:57 -0800889 /* Only the NULL compression algorithm is supported. */
890 if (compression_method != 0) {
891 al = SSL_AD_ILLEGAL_PARAMETER;
892 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
893 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
894 goto f_err;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langley1bea1732014-12-17 19:06:57 -0800897 /* TLS extensions */
898 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
899 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
900 goto err;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langley1bea1732014-12-17 19:06:57 -0800903 /* There should be nothing left over in the record. */
904 if (CBS_len(&server_hello) != 0) {
905 /* wrong packet length */
906 al = SSL_AD_DECODE_ERROR;
907 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
908 goto f_err;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langley1bea1732014-12-17 19:06:57 -0800911 return 1;
912
Adam Langley95c29f32014-06-20 12:00:00 -0700913f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800914 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700915err:
Adam Langley1bea1732014-12-17 19:06:57 -0800916 return -1;
917}
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langley1bea1732014-12-17 19:06:57 -0800919int ssl3_get_server_certificate(SSL *s) {
920 int al, i, ok, ret = -1;
921 unsigned long n;
922 X509 *x = NULL;
923 STACK_OF(X509) *sk = NULL;
924 SESS_CERT *sc;
925 EVP_PKEY *pkey = NULL;
926 CBS cbs, certificate_list;
927 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langley1bea1732014-12-17 19:06:57 -0800929 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
930 SSL3_MT_CERTIFICATE, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500931 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langley1bea1732014-12-17 19:06:57 -0800933 if (!ok) {
934 return n;
935 }
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langley1bea1732014-12-17 19:06:57 -0800937 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langley1bea1732014-12-17 19:06:57 -0800939 sk = sk_X509_new_null();
940 if (sk == NULL) {
941 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
942 goto err;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langley1bea1732014-12-17 19:06:57 -0800945 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
946 CBS_len(&cbs) != 0) {
947 al = SSL_AD_DECODE_ERROR;
948 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
949 goto f_err;
950 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400951
Adam Langley1bea1732014-12-17 19:06:57 -0800952 while (CBS_len(&certificate_list) > 0) {
953 CBS certificate;
954 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
955 al = SSL_AD_DECODE_ERROR;
956 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
957 SSL_R_CERT_LENGTH_MISMATCH);
958 goto f_err;
959 }
960 data = CBS_data(&certificate);
961 x = d2i_X509(NULL, &data, CBS_len(&certificate));
962 if (x == NULL) {
963 al = SSL_AD_BAD_CERTIFICATE;
964 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
965 goto f_err;
966 }
967 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
968 al = SSL_AD_DECODE_ERROR;
969 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
970 SSL_R_CERT_LENGTH_MISMATCH);
971 goto f_err;
972 }
973 if (!sk_X509_push(sk, x)) {
974 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
975 goto err;
976 }
977 x = NULL;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langley1bea1732014-12-17 19:06:57 -0800980 i = ssl_verify_cert_chain(s, sk);
981 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
982 al = ssl_verify_alarm_type(s->verify_result);
983 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
984 SSL_R_CERTIFICATE_VERIFY_FAILED);
985 goto f_err;
986 }
987 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langley1bea1732014-12-17 19:06:57 -0800989 sc = ssl_sess_cert_new();
990 if (sc == NULL) {
991 goto err;
992 }
Adam Langley95c29f32014-06-20 12:00:00 -0700993
Adam Langley1bea1732014-12-17 19:06:57 -0800994 if (s->session->sess_cert) {
995 ssl_sess_cert_free(s->session->sess_cert);
996 }
997 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langley1bea1732014-12-17 19:06:57 -0800999 sc->cert_chain = sk;
1000 /* Inconsistency alert: cert_chain does include the peer's certificate, which
1001 * we don't include in s3_srvr.c */
1002 x = sk_X509_value(sk, 0);
1003 sk = NULL;
1004 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langley1bea1732014-12-17 19:06:57 -08001006 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langley1bea1732014-12-17 19:06:57 -08001008 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1009 x = NULL;
1010 al = SSL3_AL_FATAL;
1011 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1012 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1013 goto f_err;
1014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langley1bea1732014-12-17 19:06:57 -08001016 i = ssl_cert_type(pkey);
1017 if (i < 0) {
1018 x = NULL;
1019 al = SSL3_AL_FATAL;
1020 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1021 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1022 goto f_err;
1023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langley1bea1732014-12-17 19:06:57 -08001025 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1026 if (exp_idx >= 0 && i != exp_idx) {
1027 x = NULL;
1028 al = SSL_AD_ILLEGAL_PARAMETER;
1029 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1030 SSL_R_WRONG_CERTIFICATE_TYPE);
1031 goto f_err;
1032 }
1033 sc->peer_cert_type = i;
1034 /* Why would the following ever happen? We just created sc a couple of lines
1035 * ago. */
1036 if (sc->peer_pkeys[i].x509 != NULL) {
1037 X509_free(sc->peer_pkeys[i].x509);
1038 }
1039 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1040 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langley1bea1732014-12-17 19:06:57 -08001042 if (s->session->peer != NULL) {
1043 X509_free(s->session->peer);
1044 }
1045 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001046
Adam Langley1bea1732014-12-17 19:06:57 -08001047 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Adam Langley1bea1732014-12-17 19:06:57 -08001049 x = NULL;
1050 ret = 1;
1051
1052 if (0) {
1053 f_err:
1054 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1055 }
1056
1057err:
1058 EVP_PKEY_free(pkey);
1059 X509_free(x);
1060 sk_X509_pop_free(sk, X509_free);
1061 return ret;
1062}
1063
1064int ssl3_get_server_key_exchange(SSL *s) {
1065 EVP_MD_CTX md_ctx;
1066 int al, ok;
1067 long n, alg_k, alg_a;
1068 EVP_PKEY *pkey = NULL;
1069 const EVP_MD *md = NULL;
1070 RSA *rsa = NULL;
1071 DH *dh = NULL;
1072 EC_KEY *ecdh = NULL;
1073 BN_CTX *bn_ctx = NULL;
1074 EC_POINT *srvr_ecpoint = NULL;
1075 CBS server_key_exchange, server_key_exchange_orig, parameter;
1076
1077 /* use same message size as in ssl3_get_certificate_request() as
1078 * ServerKeyExchange message may be skipped */
1079 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1080 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001081 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001082 if (!ok) {
1083 return n;
1084 }
1085
1086 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1087 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1088 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1089 SSL_R_UNEXPECTED_MESSAGE);
1090 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1091 return -1;
1092 }
1093
1094 /* In plain PSK ciphersuite, ServerKeyExchange can be
1095 omitted if no identity hint is sent. Set session->sess_cert anyway to
1096 avoid problems later.*/
1097 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1098 /* PSK ciphersuites that also send a Certificate would have already
1099 * initialized |sess_cert|. */
1100 if (s->session->sess_cert == NULL) {
1101 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001102 if (s->session->sess_cert == NULL) {
1103 return -1;
1104 }
Adam Langley1bea1732014-12-17 19:06:57 -08001105 }
1106
1107 /* TODO(davidben): This should be reset in one place with the rest of the
1108 * handshake state. */
1109 if (s->s3->tmp.peer_psk_identity_hint) {
1110 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1111 s->s3->tmp.peer_psk_identity_hint = NULL;
1112 }
1113 }
1114 s->s3->tmp.reuse_message = 1;
1115 return 1;
1116 }
1117
1118 /* Retain a copy of the original CBS to compute the signature over. */
1119 CBS_init(&server_key_exchange, s->init_msg, n);
1120 server_key_exchange_orig = server_key_exchange;
1121
1122 if (s->session->sess_cert != NULL) {
1123 if (s->session->sess_cert->peer_dh_tmp) {
1124 DH_free(s->session->sess_cert->peer_dh_tmp);
1125 s->session->sess_cert->peer_dh_tmp = NULL;
1126 }
1127 if (s->session->sess_cert->peer_ecdh_tmp) {
1128 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1129 s->session->sess_cert->peer_ecdh_tmp = NULL;
1130 }
1131 } else {
1132 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001133 if (s->session->sess_cert == NULL) {
1134 return -1;
1135 }
Adam Langley1bea1732014-12-17 19:06:57 -08001136 }
1137
1138 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1139 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1140 EVP_MD_CTX_init(&md_ctx);
1141
1142 if (alg_a & SSL_aPSK) {
1143 CBS psk_identity_hint;
1144
1145 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1146 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1147 &psk_identity_hint)) {
1148 al = SSL_AD_DECODE_ERROR;
1149 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1150 goto f_err;
1151 }
1152
1153 /* Store PSK identity hint for later use, hint is used in
1154 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1155 * identity hint can be as long as the maximum length of a PSK identity.
1156 * Also do not allow NULL characters; identities are saved as C strings.
1157 *
1158 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1159 * a specific identity. */
1160 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1161 CBS_contains_zero_byte(&psk_identity_hint)) {
1162 al = SSL_AD_HANDSHAKE_FAILURE;
1163 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1164 SSL_R_DATA_LENGTH_TOO_LONG);
1165 goto f_err;
1166 }
1167
1168 /* Save the identity hint as a C string. */
1169 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1170 al = SSL_AD_INTERNAL_ERROR;
1171 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1172 ERR_R_MALLOC_FAILURE);
1173 goto f_err;
1174 }
1175 }
1176
David Benjamin7061e282015-03-19 11:10:48 -04001177 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001178 CBS dh_p, dh_g, dh_Ys;
1179
1180 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1181 CBS_len(&dh_p) == 0 ||
1182 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1183 CBS_len(&dh_g) == 0 ||
1184 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1185 CBS_len(&dh_Ys) == 0) {
1186 al = SSL_AD_DECODE_ERROR;
1187 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1188 goto f_err;
1189 }
1190
1191 dh = DH_new();
1192 if (dh == NULL) {
1193 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1194 goto err;
1195 }
1196
1197 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1198 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1199 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1200 NULL) {
1201 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1202 goto err;
1203 }
1204
1205 if (DH_size(dh) < 512 / 8) {
1206 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1207 SSL_R_BAD_DH_P_LENGTH);
1208 goto err;
1209 }
1210
1211 if (alg_a & SSL_aRSA) {
1212 pkey = X509_get_pubkey(
1213 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1214 }
1215 /* else anonymous DH, so no certificate or pkey. */
1216
1217 s->session->sess_cert->peer_dh_tmp = dh;
1218 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001219 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001220 uint16_t curve_id;
1221 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001222 const EC_GROUP *group;
1223 CBS point;
1224
1225 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1226 * key. Check curve is one of our preferences, if not server has sent an
1227 * invalid curve. */
1228 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1229 al = SSL_AD_DECODE_ERROR;
1230 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1231 goto f_err;
1232 }
1233
1234 curve_nid = tls1_ec_curve_id2nid(curve_id);
1235 if (curve_nid == 0) {
1236 al = SSL_AD_INTERNAL_ERROR;
1237 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1238 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1239 goto f_err;
1240 }
1241
David Benjamin4f7783e2015-03-05 03:05:11 -05001242 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001243 if (ecdh == NULL) {
1244 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001245 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001246 goto err;
1247 }
1248
Adam Langley1bea1732014-12-17 19:06:57 -08001249 group = EC_KEY_get0_group(ecdh);
1250
1251 /* Next, get the encoded ECPoint */
1252 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1253 al = SSL_AD_DECODE_ERROR;
1254 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1255 goto f_err;
1256 }
1257
1258 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1259 ((bn_ctx = BN_CTX_new()) == NULL)) {
1260 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1261 ERR_R_MALLOC_FAILURE);
1262 goto err;
1263 }
1264
1265 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1266 CBS_len(&point), bn_ctx)) {
1267 al = SSL_AD_DECODE_ERROR;
1268 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1269 goto f_err;
1270 }
1271
1272 /* The ECC/TLS specification does not mention the use of DSA to sign
1273 * ECParameters in the server key exchange message. We do support RSA and
1274 * ECDSA. */
1275 if (alg_a & SSL_aRSA) {
1276 pkey = X509_get_pubkey(
1277 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1278 } else if (alg_a & SSL_aECDSA) {
1279 pkey =
1280 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1281 }
1282 /* else anonymous ECDH, so no certificate or pkey. */
1283 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1284 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1285 ecdh = NULL;
1286 BN_CTX_free(bn_ctx);
1287 bn_ctx = NULL;
1288 EC_POINT_free(srvr_ecpoint);
1289 srvr_ecpoint = NULL;
1290 } else if (!(alg_k & SSL_kPSK)) {
1291 al = SSL_AD_UNEXPECTED_MESSAGE;
1292 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1293 SSL_R_UNEXPECTED_MESSAGE);
1294 goto f_err;
1295 }
1296
1297 /* At this point, |server_key_exchange| contains the signature, if any, while
1298 * |server_key_exchange_orig| contains the entire message. From that, derive
1299 * a CBS containing just the parameter. */
1300 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1301 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1302
1303 /* if it was signed, check the signature */
1304 if (pkey != NULL) {
1305 CBS signature;
1306
1307 if (SSL_USE_SIGALGS(s)) {
1308 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1309 goto f_err;
1310 }
1311 } else if (pkey->type == EVP_PKEY_RSA) {
1312 md = EVP_md5_sha1();
1313 } else {
1314 md = EVP_sha1();
1315 }
1316
1317 /* The last field in |server_key_exchange| is the signature. */
1318 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1319 CBS_len(&server_key_exchange) != 0) {
1320 al = SSL_AD_DECODE_ERROR;
1321 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1322 goto f_err;
1323 }
1324
1325 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1326 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1327 SSL3_RANDOM_SIZE) ||
1328 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1329 SSL3_RANDOM_SIZE) ||
1330 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1331 CBS_len(&parameter)) ||
1332 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1333 CBS_len(&signature))) {
1334 /* bad signature */
1335 al = SSL_AD_DECRYPT_ERROR;
1336 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1337 goto f_err;
1338 }
1339 } else {
1340 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1341 /* Might be wrong key type, check it */
1342 if (ssl3_check_cert_and_algorithm(s)) {
1343 /* Otherwise this shouldn't happen */
1344 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1345 ERR_R_INTERNAL_ERROR);
1346 }
1347 goto err;
1348 }
1349 /* still data left over */
1350 if (CBS_len(&server_key_exchange) > 0) {
1351 al = SSL_AD_DECODE_ERROR;
1352 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1353 SSL_R_EXTRA_DATA_IN_MESSAGE);
1354 goto f_err;
1355 }
1356 }
1357 EVP_PKEY_free(pkey);
1358 EVP_MD_CTX_cleanup(&md_ctx);
1359 return 1;
1360
Adam Langley95c29f32014-06-20 12:00:00 -07001361f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001362 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001363err:
Adam Langley1bea1732014-12-17 19:06:57 -08001364 EVP_PKEY_free(pkey);
1365 if (rsa != NULL) {
1366 RSA_free(rsa);
1367 }
1368 if (dh != NULL) {
1369 DH_free(dh);
1370 }
1371 BN_CTX_free(bn_ctx);
1372 EC_POINT_free(srvr_ecpoint);
1373 if (ecdh != NULL) {
1374 EC_KEY_free(ecdh);
1375 }
1376 EVP_MD_CTX_cleanup(&md_ctx);
1377 return -1;
1378}
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1381 return X509_NAME_cmp(*a, *b);
1382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
Adam Langley1bea1732014-12-17 19:06:57 -08001384int ssl3_get_certificate_request(SSL *s) {
1385 int ok, ret = 0;
1386 unsigned long n;
1387 X509_NAME *xn = NULL;
1388 STACK_OF(X509_NAME) *ca_sk = NULL;
1389 CBS cbs;
1390 CBS certificate_types;
1391 CBS certificate_authorities;
1392 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langley1bea1732014-12-17 19:06:57 -08001394 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1395 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001396 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001397
Adam Langley1bea1732014-12-17 19:06:57 -08001398 if (!ok) {
1399 return n;
1400 }
David Benjamin688d8df2014-11-02 23:06:42 -05001401
Adam Langley1bea1732014-12-17 19:06:57 -08001402 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1405 s->s3->tmp.reuse_message = 1;
1406 /* If we get here we don't need any cached handshake records as we wont be
1407 * doing client auth. */
1408 if (s->s3->handshake_buffer &&
1409 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1410 goto err;
1411 }
1412 return 1;
1413 }
David Benjamined439582014-07-14 19:13:02 -04001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1416 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1417 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1418 SSL_R_WRONG_MESSAGE_TYPE);
1419 goto err;
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
Adam Langley1bea1732014-12-17 19:06:57 -08001422 /* TLS does not like anon-DH with client cert */
1423 if (s->version > SSL3_VERSION &&
1424 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1425 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1426 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1427 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1428 goto err;
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
Adam Langley1bea1732014-12-17 19:06:57 -08001431 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langley1bea1732014-12-17 19:06:57 -08001433 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1434 if (ca_sk == NULL) {
1435 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1436 goto err;
1437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 /* get the certificate types */
1440 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1441 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1442 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1443 goto err;
1444 }
David Benjamined439582014-07-14 19:13:02 -04001445
Adam Langley1bea1732014-12-17 19:06:57 -08001446 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1447 &s->s3->tmp.num_certificate_types)) {
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1449 goto err;
1450 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001451
Adam Langley1bea1732014-12-17 19:06:57 -08001452 if (SSL_USE_SIGALGS(s)) {
1453 CBS supported_signature_algorithms;
1454 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1455 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1456 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1457 goto err;
1458 }
David Benjamined439582014-07-14 19:13:02 -04001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1461 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1462 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1463 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1464 goto err;
1465 }
1466 }
David Benjamined439582014-07-14 19:13:02 -04001467
Adam Langley1bea1732014-12-17 19:06:57 -08001468 /* get the CA RDNs */
1469 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1470 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1471 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1472 goto err;
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langley1bea1732014-12-17 19:06:57 -08001475 while (CBS_len(&certificate_authorities) > 0) {
1476 CBS distinguished_name;
1477 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1478 &distinguished_name)) {
1479 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1480 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1481 SSL_R_CA_DN_TOO_LONG);
1482 goto err;
1483 }
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langley1bea1732014-12-17 19:06:57 -08001485 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001486
Adam Langley1bea1732014-12-17 19:06:57 -08001487 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1488 if (xn == NULL) {
1489 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1490 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1491 goto err;
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langley1bea1732014-12-17 19:06:57 -08001494 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1495 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001496 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001497 goto err;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langley1bea1732014-12-17 19:06:57 -08001500 if (CBS_len(&distinguished_name) != 0) {
1501 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1502 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1503 SSL_R_CA_DN_LENGTH_MISMATCH);
1504 goto err;
1505 }
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langley1bea1732014-12-17 19:06:57 -08001507 if (!sk_X509_NAME_push(ca_sk, xn)) {
1508 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1509 ERR_R_MALLOC_FAILURE);
1510 goto err;
1511 }
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langley1bea1732014-12-17 19:06:57 -08001514 /* we should setup a certificate to return.... */
1515 s->s3->tmp.cert_req = 1;
1516 if (s->s3->tmp.ca_names != NULL) {
1517 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1518 }
1519 s->s3->tmp.ca_names = ca_sk;
1520 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langley1bea1732014-12-17 19:06:57 -08001522 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langley1bea1732014-12-17 19:06:57 -08001524err:
1525 if (ca_sk != NULL) {
1526 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1527 }
1528 return ret;
1529}
Adam Langley95c29f32014-06-20 12:00:00 -07001530
Adam Langley1bea1732014-12-17 19:06:57 -08001531int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001532 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001533 long n;
1534 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001535
Adam Langley1bea1732014-12-17 19:06:57 -08001536 n = s->method->ssl_get_message(
1537 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001538 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001539
Adam Langley1bea1732014-12-17 19:06:57 -08001540 if (!ok) {
1541 return n;
1542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langley1bea1732014-12-17 19:06:57 -08001544 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001545
Adam Langley1bea1732014-12-17 19:06:57 -08001546 if (!CBS_get_u32(&new_session_ticket,
1547 &s->session->tlsext_tick_lifetime_hint) ||
1548 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1549 CBS_len(&new_session_ticket) != 0) {
1550 al = SSL_AD_DECODE_ERROR;
1551 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1552 goto f_err;
1553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554
Adam Langley1bea1732014-12-17 19:06:57 -08001555 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1556 &s->session->tlsext_ticklen)) {
1557 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1558 goto err;
1559 }
Adam Langley95c29f32014-06-20 12:00:00 -07001560
David Benjamin68070622015-02-08 23:44:59 -05001561 /* Generate a session ID for this session based on the session ticket. We use
1562 * the session ID mechanism for detecting ticket resumption. This also fits in
1563 * with assumptions elsewhere in OpenSSL.*/
1564 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1565 &s->session->session_id_length, EVP_sha256(), NULL)) {
1566 goto err;
1567 }
1568
1569 return 1;
David Benjamined439582014-07-14 19:13:02 -04001570
Adam Langley95c29f32014-06-20 12:00:00 -07001571f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001572 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001573err:
Adam Langley1bea1732014-12-17 19:06:57 -08001574 return -1;
1575}
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Adam Langley1bea1732014-12-17 19:06:57 -08001577int ssl3_get_cert_status(SSL *s) {
1578 int ok, al;
1579 long n;
1580 CBS certificate_status, ocsp_response;
1581 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Adam Langley1bea1732014-12-17 19:06:57 -08001583 n = s->method->ssl_get_message(
1584 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001585 SSL3_MT_CERTIFICATE_STATUS, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001586
Adam Langley1bea1732014-12-17 19:06:57 -08001587 if (!ok) {
1588 return n;
1589 }
Adam Langley95c29f32014-06-20 12:00:00 -07001590
Adam Langley1bea1732014-12-17 19:06:57 -08001591 CBS_init(&certificate_status, s->init_msg, n);
1592 if (!CBS_get_u8(&certificate_status, &status_type) ||
1593 status_type != TLSEXT_STATUSTYPE_ocsp ||
1594 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1595 CBS_len(&ocsp_response) == 0 ||
1596 CBS_len(&certificate_status) != 0) {
1597 al = SSL_AD_DECODE_ERROR;
1598 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1599 goto f_err;
1600 }
Adam Langley95c29f32014-06-20 12:00:00 -07001601
Adam Langley1bea1732014-12-17 19:06:57 -08001602 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1603 &s->session->ocsp_response_length)) {
1604 al = SSL_AD_INTERNAL_ERROR;
1605 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1606 goto f_err;
1607 }
1608 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langley95c29f32014-06-20 12:00:00 -07001610f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001611 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1612 return -1;
1613}
Adam Langley95c29f32014-06-20 12:00:00 -07001614
Adam Langley1bea1732014-12-17 19:06:57 -08001615int ssl3_get_server_done(SSL *s) {
1616 int ok;
1617 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001618
Adam Langley1bea1732014-12-17 19:06:57 -08001619 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1620 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1621 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001622 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 if (!ok) {
1625 return n;
1626 }
David Benjamin46062682014-07-14 19:14:32 -04001627
Adam Langley1bea1732014-12-17 19:06:57 -08001628 if (n > 0) {
1629 /* should contain no data */
1630 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1631 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1632 return -1;
1633 }
David Benjamin46062682014-07-14 19:14:32 -04001634
Adam Langley1bea1732014-12-17 19:06:57 -08001635 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001636}
Adam Langley1258b6a2014-06-20 12:00:00 -07001637
Adam Langley1258b6a2014-06-20 12:00:00 -07001638
Adam Langley1bea1732014-12-17 19:06:57 -08001639int ssl3_send_client_key_exchange(SSL *s) {
1640 uint8_t *p;
1641 int n = 0;
1642 unsigned long alg_k;
1643 unsigned long alg_a;
1644 uint8_t *q;
1645 EVP_PKEY *pkey = NULL;
1646 EC_KEY *clnt_ecdh = NULL;
1647 const EC_POINT *srvr_ecpoint = NULL;
1648 EVP_PKEY *srvr_pub_pkey = NULL;
1649 uint8_t *encodedPoint = NULL;
1650 int encoded_pt_len = 0;
1651 BN_CTX *bn_ctx = NULL;
1652 unsigned int psk_len = 0;
1653 uint8_t psk[PSK_MAX_PSK_LEN];
1654 uint8_t *pms = NULL;
1655 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001656
Adam Langley1bea1732014-12-17 19:06:57 -08001657 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1658 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001659
Adam Langley1bea1732014-12-17 19:06:57 -08001660 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1661 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 /* If using a PSK key exchange, prepare the pre-shared key. */
1664 if (alg_a & SSL_aPSK) {
1665 char identity[PSK_MAX_IDENTITY_LEN + 1];
1666 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001667
Adam Langley1bea1732014-12-17 19:06:57 -08001668 if (s->psk_client_callback == NULL) {
1669 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1670 SSL_R_PSK_NO_CLIENT_CB);
1671 goto err;
1672 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001673
Adam Langley1bea1732014-12-17 19:06:57 -08001674 memset(identity, 0, sizeof(identity));
1675 psk_len =
1676 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1677 sizeof(identity), psk, sizeof(psk));
1678 if (psk_len > PSK_MAX_PSK_LEN) {
1679 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1680 ERR_R_INTERNAL_ERROR);
1681 goto err;
1682 } else if (psk_len == 0) {
1683 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1684 SSL_R_PSK_IDENTITY_NOT_FOUND);
1685 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1686 goto err;
1687 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001688
Adam Langley1bea1732014-12-17 19:06:57 -08001689 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1690 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1691 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1692 ERR_R_INTERNAL_ERROR);
1693 goto err;
1694 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001695
Adam Langley1bea1732014-12-17 19:06:57 -08001696 if (s->session->psk_identity != NULL) {
1697 OPENSSL_free(s->session->psk_identity);
1698 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001699
Adam Langley1bea1732014-12-17 19:06:57 -08001700 s->session->psk_identity = BUF_strdup(identity);
1701 if (s->session->psk_identity == NULL) {
1702 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1703 ERR_R_MALLOC_FAILURE);
1704 goto err;
1705 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001706
Adam Langley1bea1732014-12-17 19:06:57 -08001707 /* Write out psk_identity. */
1708 s2n(identity_len, p);
1709 memcpy(p, identity, identity_len);
1710 p += identity_len;
1711 n = 2 + identity_len;
1712 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001713
Adam Langley1bea1732014-12-17 19:06:57 -08001714 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1715 if (alg_k & SSL_kRSA) {
1716 RSA *rsa;
1717 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001718
Adam Langley1bea1732014-12-17 19:06:57 -08001719 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1720 pms = OPENSSL_malloc(pms_len);
1721 if (pms == NULL) {
1722 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1723 ERR_R_MALLOC_FAILURE);
1724 goto err;
1725 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001726
Adam Langley1bea1732014-12-17 19:06:57 -08001727 if (s->session->sess_cert == NULL) {
1728 /* We should always have a server certificate with SSL_kRSA. */
1729 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1730 ERR_R_INTERNAL_ERROR);
1731 goto err;
1732 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001733
Adam Langley1bea1732014-12-17 19:06:57 -08001734 pkey = X509_get_pubkey(
1735 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1736 if (pkey == NULL ||
1737 pkey->type != EVP_PKEY_RSA ||
1738 pkey->pkey.rsa == NULL) {
1739 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1740 ERR_R_INTERNAL_ERROR);
1741 if (pkey != NULL) {
1742 EVP_PKEY_free(pkey);
1743 }
1744 goto err;
1745 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001746
Adam Langley1bea1732014-12-17 19:06:57 -08001747 rsa = pkey->pkey.rsa;
1748 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001749
Adam Langley1bea1732014-12-17 19:06:57 -08001750 pms[0] = s->client_version >> 8;
1751 pms[1] = s->client_version & 0xff;
1752 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1753 goto err;
1754 }
1755
1756 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1757
1758 q = p;
1759 /* In TLS and beyond, reserve space for the length prefix. */
1760 if (s->version > SSL3_VERSION) {
1761 p += 2;
1762 n += 2;
1763 }
1764 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1765 RSA_PKCS1_PADDING)) {
1766 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1767 SSL_R_BAD_RSA_ENCRYPT);
1768 goto err;
1769 }
1770 n += enc_pms_len;
1771
1772 /* Log the premaster secret, if logging is enabled. */
1773 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1774 pms_len)) {
1775 goto err;
1776 }
1777
1778 /* Fill in the length prefix. */
1779 if (s->version > SSL3_VERSION) {
1780 s2n(enc_pms_len, q);
1781 }
David Benjamin7061e282015-03-19 11:10:48 -04001782 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001783 DH *dh_srvr, *dh_clnt;
1784 SESS_CERT *scert = s->session->sess_cert;
1785 int dh_len;
1786 size_t pub_len;
1787
1788 if (scert == NULL) {
1789 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1790 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1791 SSL_R_UNEXPECTED_MESSAGE);
1792 goto err;
1793 }
1794
1795 if (scert->peer_dh_tmp == NULL) {
1796 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1797 ERR_R_INTERNAL_ERROR);
1798 goto err;
1799 }
1800 dh_srvr = scert->peer_dh_tmp;
1801
1802 /* generate a new random key */
1803 dh_clnt = DHparams_dup(dh_srvr);
1804 if (dh_clnt == NULL) {
1805 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1806 goto err;
1807 }
1808 if (!DH_generate_key(dh_clnt)) {
1809 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1810 DH_free(dh_clnt);
1811 goto err;
1812 }
1813
1814 pms_len = DH_size(dh_clnt);
1815 pms = OPENSSL_malloc(pms_len);
1816 if (pms == NULL) {
1817 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1818 ERR_R_MALLOC_FAILURE);
1819 DH_free(dh_clnt);
1820 goto err;
1821 }
1822
1823 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1824 if (dh_len <= 0) {
1825 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1826 DH_free(dh_clnt);
1827 goto err;
1828 }
1829 pms_len = dh_len;
1830
1831 /* send off the data */
1832 pub_len = BN_num_bytes(dh_clnt->pub_key);
1833 s2n(pub_len, p);
1834 BN_bn2bin(dh_clnt->pub_key, p);
1835 n += 2 + pub_len;
1836
1837 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001838 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001839 const EC_GROUP *srvr_group = NULL;
1840 EC_KEY *tkey;
1841 int field_size = 0, ecdh_len;
1842
1843 if (s->session->sess_cert == NULL) {
1844 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1845 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1846 SSL_R_UNEXPECTED_MESSAGE);
1847 goto err;
1848 }
1849
1850 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1851 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1852 ERR_R_INTERNAL_ERROR);
1853 goto err;
1854 }
1855
1856 tkey = s->session->sess_cert->peer_ecdh_tmp;
1857
1858 srvr_group = EC_KEY_get0_group(tkey);
1859 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1860 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1861 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1862 ERR_R_INTERNAL_ERROR);
1863 goto err;
1864 }
1865
1866 clnt_ecdh = EC_KEY_new();
1867 if (clnt_ecdh == NULL) {
1868 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1869 ERR_R_MALLOC_FAILURE);
1870 goto err;
1871 }
1872
1873 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1874 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1875 goto err;
1876 }
1877
1878 /* Generate a new ECDH key pair */
1879 if (!EC_KEY_generate_key(clnt_ecdh)) {
1880 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1881 goto err;
1882 }
1883
1884 field_size = EC_GROUP_get_degree(srvr_group);
1885 if (field_size <= 0) {
1886 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1887 goto err;
1888 }
1889
1890 pms_len = (field_size + 7) / 8;
1891 pms = OPENSSL_malloc(pms_len);
1892 if (pms == NULL) {
1893 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1894 ERR_R_MALLOC_FAILURE);
1895 goto err;
1896 }
1897
1898 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1899 if (ecdh_len <= 0) {
1900 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1901 goto err;
1902 }
1903 pms_len = ecdh_len;
1904
1905 /* First check the size of encoding and allocate memory accordingly. */
1906 encoded_pt_len =
1907 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1908 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1909
1910 encodedPoint =
1911 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1912 bn_ctx = BN_CTX_new();
1913 if (encodedPoint == NULL || bn_ctx == NULL) {
1914 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1915 ERR_R_MALLOC_FAILURE);
1916 goto err;
1917 }
1918
1919 /* Encode the public key */
1920 encoded_pt_len = EC_POINT_point2oct(
1921 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1922 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1923
1924 *p = encoded_pt_len; /* length of encoded point */
1925 /* Encoded point will be copied here */
1926 p += 1;
1927 n += 1;
1928 /* copy the point */
1929 memcpy(p, encodedPoint, encoded_pt_len);
1930 /* increment n to account for length field */
1931 n += encoded_pt_len;
1932
1933 /* Free allocated memory */
1934 BN_CTX_free(bn_ctx);
1935 bn_ctx = NULL;
1936 OPENSSL_free(encodedPoint);
1937 encodedPoint = NULL;
1938 EC_KEY_free(clnt_ecdh);
1939 clnt_ecdh = NULL;
1940 EVP_PKEY_free(srvr_pub_pkey);
1941 srvr_pub_pkey = NULL;
1942 } else if (alg_k & SSL_kPSK) {
1943 /* For plain PSK, other_secret is a block of 0s with the same length as
1944 * the pre-shared key. */
1945 pms_len = psk_len;
1946 pms = OPENSSL_malloc(pms_len);
1947 if (pms == NULL) {
1948 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1949 ERR_R_MALLOC_FAILURE);
1950 goto err;
1951 }
1952 memset(pms, 0, pms_len);
1953 } else {
1954 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1955 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1956 ERR_R_INTERNAL_ERROR);
1957 goto err;
1958 }
1959
1960 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1961 * key. */
1962 if (alg_a & SSL_aPSK) {
1963 CBB cbb, child;
1964 uint8_t *new_pms;
1965 size_t new_pms_len;
1966
1967 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1968 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1969 ERR_R_MALLOC_FAILURE);
1970 goto err;
1971 }
1972 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1973 !CBB_add_bytes(&child, pms, pms_len) ||
1974 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1975 !CBB_add_bytes(&child, psk, psk_len) ||
1976 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1977 CBB_cleanup(&cbb);
1978 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1979 ERR_R_INTERNAL_ERROR);
1980 goto err;
1981 }
1982 OPENSSL_cleanse(pms, pms_len);
1983 OPENSSL_free(pms);
1984 pms = new_pms;
1985 pms_len = new_pms_len;
1986 }
1987
1988 /* The message must be added to the finished hash before calculating the
1989 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001990 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1991 goto err;
1992 }
Adam Langley1bea1732014-12-17 19:06:57 -08001993 s->state = SSL3_ST_CW_KEY_EXCH_B;
1994
1995 s->session->master_key_length = s->enc_method->generate_master_secret(
1996 s, s->session->master_key, pms, pms_len);
1997 if (s->session->master_key_length == 0) {
1998 goto err;
1999 }
2000 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
2001 OPENSSL_cleanse(pms, pms_len);
2002 OPENSSL_free(pms);
2003 }
2004
2005 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05002006 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002007
2008err:
Adam Langley1bea1732014-12-17 19:06:57 -08002009 BN_CTX_free(bn_ctx);
2010 if (encodedPoint != NULL) {
2011 OPENSSL_free(encodedPoint);
2012 }
2013 if (clnt_ecdh != NULL) {
2014 EC_KEY_free(clnt_ecdh);
2015 }
2016 EVP_PKEY_free(srvr_pub_pkey);
2017 if (pms) {
2018 OPENSSL_cleanse(pms, pms_len);
2019 OPENSSL_free(pms);
2020 }
2021 return -1;
2022}
Adam Langley1258b6a2014-06-20 12:00:00 -07002023
Adam Langley1bea1732014-12-17 19:06:57 -08002024int ssl3_send_cert_verify(SSL *s) {
2025 uint8_t *buf, *p;
2026 const EVP_MD *md = NULL;
2027 uint8_t digest[EVP_MAX_MD_SIZE];
2028 size_t digest_length;
2029 EVP_PKEY *pkey;
2030 EVP_PKEY_CTX *pctx = NULL;
2031 size_t signature_length = 0;
2032 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002033
Adam Langley1bea1732014-12-17 19:06:57 -08002034 buf = (uint8_t *)s->init_buf->data;
2035
2036 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2037 p = ssl_handshake_start(s);
2038 pkey = s->cert->key->privatekey;
2039
2040 /* Write out the digest type if needbe. */
2041 if (SSL_USE_SIGALGS(s)) {
2042 md = tls1_choose_signing_digest(s, pkey);
2043 if (!tls12_get_sigandhash(p, pkey, md)) {
2044 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2045 goto err;
2046 }
2047 p += 2;
2048 n += 2;
2049 }
2050
2051 /* Compute the digest. */
2052 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2053 goto err;
2054 }
2055
2056 /* The handshake buffer is no longer necessary. */
2057 if (s->s3->handshake_buffer &&
2058 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2059 goto err;
2060 }
2061
2062 /* Sign the digest. */
2063 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2064 if (pctx == NULL) {
2065 goto err;
2066 }
2067
2068 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2069 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2070 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2071 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2072 goto err;
2073 }
2074
2075 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2076 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2077 goto err;
2078 }
2079
2080 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2081 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2082 goto err;
2083 }
2084
2085 s2n(signature_length, p);
2086 n += signature_length + 2;
2087
David Benjaminfbdfefb2015-02-16 19:33:53 -05002088 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2089 goto err;
2090 }
Adam Langley1bea1732014-12-17 19:06:57 -08002091 s->state = SSL3_ST_CW_CERT_VRFY_B;
2092 }
2093
2094 EVP_PKEY_CTX_free(pctx);
2095 return ssl_do_write(s);
2096
2097err:
2098 EVP_PKEY_CTX_free(pctx);
2099 return -1;
2100}
2101
2102/* ssl3_has_client_certificate returns true if a client certificate is
2103 * configured. */
2104static int ssl3_has_client_certificate(SSL *s) {
2105 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2106}
2107
2108int ssl3_send_client_certificate(SSL *s) {
2109 X509 *x509 = NULL;
2110 EVP_PKEY *pkey = NULL;
2111 int i;
2112
2113 if (s->state == SSL3_ST_CW_CERT_A) {
2114 /* Let cert callback update client certificates if required */
2115 if (s->cert->cert_cb) {
2116 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2117 if (i < 0) {
2118 s->rwstate = SSL_X509_LOOKUP;
2119 return -1;
2120 }
2121 if (i == 0) {
2122 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2123 return 0;
2124 }
2125 s->rwstate = SSL_NOTHING;
2126 }
2127
2128 if (ssl3_has_client_certificate(s)) {
2129 s->state = SSL3_ST_CW_CERT_C;
2130 } else {
2131 s->state = SSL3_ST_CW_CERT_B;
2132 }
2133 }
2134
2135 /* We need to get a client cert */
2136 if (s->state == SSL3_ST_CW_CERT_B) {
2137 /* If we get an error, we need to:
2138 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2139 * We then get retried later */
2140 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2141 if (i < 0) {
2142 s->rwstate = SSL_X509_LOOKUP;
2143 return -1;
2144 }
2145 s->rwstate = SSL_NOTHING;
2146 if (i == 1 && pkey != NULL && x509 != NULL) {
2147 s->state = SSL3_ST_CW_CERT_B;
2148 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2149 i = 0;
2150 }
2151 } else if (i == 1) {
2152 i = 0;
2153 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2154 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2155 }
2156
2157 if (x509 != NULL) {
2158 X509_free(x509);
2159 }
2160 if (pkey != NULL) {
2161 EVP_PKEY_free(pkey);
2162 }
2163 if (i && !ssl3_has_client_certificate(s)) {
2164 i = 0;
2165 }
2166 if (i == 0) {
2167 if (s->version == SSL3_VERSION) {
2168 s->s3->tmp.cert_req = 0;
2169 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2170 return 1;
2171 } else {
2172 s->s3->tmp.cert_req = 2;
2173 }
2174 }
2175
2176 /* Ok, we have a cert */
2177 s->state = SSL3_ST_CW_CERT_C;
2178 }
2179
2180 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002181 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2182 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2183 return -1;
2184 }
Adam Langley1bea1732014-12-17 19:06:57 -08002185 }
2186
2187 /* SSL3_ST_CW_CERT_D */
2188 return ssl_do_write(s);
2189}
2190
2191#define has_bits(i, m) (((i) & (m)) == (m))
2192
2193int ssl3_check_cert_and_algorithm(SSL *s) {
2194 int i, idx;
2195 long alg_k, alg_a;
2196 EVP_PKEY *pkey = NULL;
2197 SESS_CERT *sc;
2198 DH *dh;
2199
2200 /* we don't have a certificate */
2201 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2202 return 1;
2203 }
2204
2205 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2206 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2207
2208 sc = s->session->sess_cert;
2209 if (sc == NULL) {
2210 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2211 goto err;
2212 }
2213
2214 dh = s->session->sess_cert->peer_dh_tmp;
2215
2216 /* This is the passed certificate */
2217
2218 idx = sc->peer_cert_type;
2219 if (idx == SSL_PKEY_ECC) {
2220 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2221 /* check failed */
2222 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2223 goto f_err;
2224 } else {
2225 return 1;
2226 }
2227 } else if (alg_a & SSL_aECDSA) {
2228 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2229 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2230 goto f_err;
2231 }
2232 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2233 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2234 EVP_PKEY_free(pkey);
2235
2236 /* Check that we have a certificate if we require one */
2237 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2238 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2239 SSL_R_MISSING_RSA_SIGNING_CERT);
2240 goto f_err;
2241 }
2242
2243 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2244 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2245 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2246 goto f_err;
2247 }
2248
David Benjamin7061e282015-03-19 11:10:48 -04002249 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002250 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2251 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2252 goto f_err;
2253 }
2254
2255 return 1;
2256
2257f_err:
2258 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2259err:
2260 return 0;
2261}
2262
2263int ssl3_send_next_proto(SSL *s) {
2264 unsigned int len, padding_len;
2265 uint8_t *d, *p;
2266
2267 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2268 len = s->next_proto_negotiated_len;
2269 padding_len = 32 - ((len + 2) % 32);
2270
2271 d = p = ssl_handshake_start(s);
2272 *(p++) = len;
2273 memcpy(p, s->next_proto_negotiated, len);
2274 p += len;
2275 *(p++) = padding_len;
2276 memset(p, 0, padding_len);
2277 p += padding_len;
2278
David Benjaminfbdfefb2015-02-16 19:33:53 -05002279 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2280 return -1;
2281 }
Adam Langley1bea1732014-12-17 19:06:57 -08002282 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
David Benjaminfbdfefb2015-02-16 19:33:53 -05002392 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2393 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2394 goto err;
2395 }
Adam Langley1bea1732014-12-17 19:06:57 -08002396 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2397
2398 ret = ssl_do_write(s);
2399
2400err:
2401 EVP_MD_CTX_cleanup(&md_ctx);
2402 if (public_key) {
2403 OPENSSL_free(public_key);
2404 }
2405 if (der_sig) {
2406 OPENSSL_free(der_sig);
2407 }
2408 if (sig) {
2409 ECDSA_SIG_free(sig);
2410 }
2411
2412 return ret;
2413}
2414
2415int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2416 int i = 0;
2417 if (s->ctx->client_cert_cb) {
2418 i = s->ctx->client_cert_cb(s, px509, ppkey);
2419 }
2420 return i;
2421}