blob: 6571c2e232dd9f42f60b1a2e43b945dfc8f201e5 [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
155#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/rand.h>
158#include <openssl/obj.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400159#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/evp.h>
161#include <openssl/mem.h>
162#include <openssl/md5.h>
163#include <openssl/dh.h>
164#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400166#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin2ee94aa2015-04-07 22:38:30 -0400168#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700169#include "../crypto/dh/internal.h"
170
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley1bea1732014-12-17 19:06:57 -0800172int ssl3_connect(SSL *s) {
173 BUF_MEM *buf = NULL;
174 void (*cb)(const SSL *ssl, int type, int val) = NULL;
175 int ret = -1;
176 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500177
Adam Langley1bea1732014-12-17 19:06:57 -0800178 assert(s->handshake_func == ssl3_connect);
179 assert(!s->server);
180 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley1bea1732014-12-17 19:06:57 -0800182 ERR_clear_error();
183 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley1bea1732014-12-17 19:06:57 -0800185 if (s->info_callback != NULL) {
186 cb = s->info_callback;
187 } else if (s->ctx->info_callback != NULL) {
188 cb = s->ctx->info_callback;
189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley1bea1732014-12-17 19:06:57 -0800191 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley1bea1732014-12-17 19:06:57 -0800193 for (;;) {
194 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley1bea1732014-12-17 19:06:57 -0800196 switch (s->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800197 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500198 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800199 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley1bea1732014-12-17 19:06:57 -0800202 if (s->init_buf == NULL) {
203 buf = BUF_MEM_new();
204 if (buf == NULL ||
205 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
206 ret = -1;
207 goto end;
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langley1bea1732014-12-17 19:06:57 -0800210 s->init_buf = buf;
211 buf = NULL;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin6a08da22015-05-08 22:58:12 -0400214 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800215 ret = -1;
216 goto end;
217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langley1bea1732014-12-17 19:06:57 -0800219 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley1bea1732014-12-17 19:06:57 -0800221 if (!ssl3_init_finished_mac(s)) {
222 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
223 ret = -1;
224 goto end;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley1bea1732014-12-17 19:06:57 -0800227 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800228 s->init_num = 0;
229 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langley1bea1732014-12-17 19:06:57 -0800231 case SSL3_ST_CW_CLNT_HELLO_A:
232 case SSL3_ST_CW_CLNT_HELLO_B:
233 s->shutdown = 0;
234 ret = ssl3_send_client_hello(s);
235 if (ret <= 0) {
236 goto end;
237 }
238 s->state = SSL3_ST_CR_SRVR_HELLO_A;
239 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley1bea1732014-12-17 19:06:57 -0800241 /* turn on buffering for the next lot of output */
242 if (s->bbio != s->wbio) {
243 s->wbio = BIO_push(s->bbio, s->wbio);
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langley1bea1732014-12-17 19:06:57 -0800246 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley1bea1732014-12-17 19:06:57 -0800248 case SSL3_ST_CR_SRVR_HELLO_A:
249 case SSL3_ST_CR_SRVR_HELLO_B:
250 ret = ssl3_get_server_hello(s);
251 if (ret <= 0) {
252 goto end;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley1bea1732014-12-17 19:06:57 -0800255 if (s->hit) {
256 s->state = SSL3_ST_CR_CHANGE;
257 if (s->tlsext_ticket_expected) {
258 /* receive renewed session ticket */
259 s->state = SSL3_ST_CR_SESSION_TICKET_A;
260 }
261 } else {
262 s->state = SSL3_ST_CR_CERT_A;
263 }
264 s->init_num = 0;
265 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400266
Adam Langley1bea1732014-12-17 19:06:57 -0800267 case SSL3_ST_CR_CERT_A:
268 case SSL3_ST_CR_CERT_B:
269 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
270 ret = ssl3_get_server_certificate(s);
271 if (ret <= 0) {
272 goto end;
273 }
274 if (s->s3->tmp.certificate_status_expected) {
275 s->state = SSL3_ST_CR_CERT_STATUS_A;
276 } else {
277 s->state = SSL3_ST_CR_KEY_EXCH_A;
278 }
279 } else {
280 skip = 1;
281 s->state = SSL3_ST_CR_KEY_EXCH_A;
282 }
283 s->init_num = 0;
284 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langley1bea1732014-12-17 19:06:57 -0800286 case SSL3_ST_CR_KEY_EXCH_A:
287 case SSL3_ST_CR_KEY_EXCH_B:
288 ret = ssl3_get_server_key_exchange(s);
289 if (ret <= 0) {
290 goto end;
291 }
292 s->state = SSL3_ST_CR_CERT_REQ_A;
293 s->init_num = 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800294 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley1bea1732014-12-17 19:06:57 -0800296 case SSL3_ST_CR_CERT_REQ_A:
297 case SSL3_ST_CR_CERT_REQ_B:
298 ret = ssl3_get_certificate_request(s);
299 if (ret <= 0) {
300 goto end;
301 }
302 s->state = SSL3_ST_CR_SRVR_DONE_A;
303 s->init_num = 0;
304 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley1bea1732014-12-17 19:06:57 -0800306 case SSL3_ST_CR_SRVR_DONE_A:
307 case SSL3_ST_CR_SRVR_DONE_B:
308 ret = ssl3_get_server_done(s);
309 if (ret <= 0) {
310 goto end;
311 }
312 if (s->s3->tmp.cert_req) {
313 s->state = SSL3_ST_CW_CERT_A;
314 } else {
315 s->state = SSL3_ST_CW_KEY_EXCH_A;
316 }
317 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CW_CERT_A:
322 case SSL3_ST_CW_CERT_B:
323 case SSL3_ST_CW_CERT_C:
324 case SSL3_ST_CW_CERT_D:
325 ret = ssl3_send_client_certificate(s);
326 if (ret <= 0) {
327 goto end;
328 }
329 s->state = SSL3_ST_CW_KEY_EXCH_A;
330 s->init_num = 0;
331 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley1bea1732014-12-17 19:06:57 -0800333 case SSL3_ST_CW_KEY_EXCH_A:
334 case SSL3_ST_CW_KEY_EXCH_B:
335 ret = ssl3_send_client_key_exchange(s);
336 if (ret <= 0) {
337 goto end;
338 }
339 /* For TLS, cert_req is set to 2, so a cert chain
340 * of nothing is sent, but no verify packet is sent */
341 if (s->s3->tmp.cert_req == 1) {
342 s->state = SSL3_ST_CW_CERT_VRFY_A;
343 } else {
344 s->state = SSL3_ST_CW_CHANGE_A;
345 s->s3->change_cipher_spec = 0;
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley1bea1732014-12-17 19:06:57 -0800348 s->init_num = 0;
349 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langley1bea1732014-12-17 19:06:57 -0800351 case SSL3_ST_CW_CERT_VRFY_A:
352 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400353 case SSL3_ST_CW_CERT_VRFY_C:
Adam Langley1bea1732014-12-17 19:06:57 -0800354 ret = ssl3_send_cert_verify(s);
355 if (ret <= 0) {
356 goto end;
357 }
358 s->state = SSL3_ST_CW_CHANGE_A;
359 s->init_num = 0;
360 s->s3->change_cipher_spec = 0;
361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langley1bea1732014-12-17 19:06:57 -0800363 case SSL3_ST_CW_CHANGE_A:
364 case SSL3_ST_CW_CHANGE_B:
365 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
366 SSL3_ST_CW_CHANGE_B);
367 if (ret <= 0) {
368 goto end;
369 }
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langley1bea1732014-12-17 19:06:57 -0800371 s->state = SSL3_ST_CW_FINISHED_A;
372 if (s->s3->tlsext_channel_id_valid) {
373 s->state = SSL3_ST_CW_CHANNEL_ID_A;
374 }
375 if (s->s3->next_proto_neg_seen) {
376 s->state = SSL3_ST_CW_NEXT_PROTO_A;
377 }
378 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langley1bea1732014-12-17 19:06:57 -0800380 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400381 if (!s->enc_method->setup_key_block(s) ||
382 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800383 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
384 ret = -1;
385 goto end;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley1bea1732014-12-17 19:06:57 -0800388 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley1bea1732014-12-17 19:06:57 -0800390 case SSL3_ST_CW_NEXT_PROTO_A:
391 case SSL3_ST_CW_NEXT_PROTO_B:
392 ret = ssl3_send_next_proto(s);
393 if (ret <= 0) {
394 goto end;
395 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700396
Adam Langley1bea1732014-12-17 19:06:57 -0800397 if (s->s3->tlsext_channel_id_valid) {
398 s->state = SSL3_ST_CW_CHANNEL_ID_A;
399 } else {
400 s->state = SSL3_ST_CW_FINISHED_A;
401 }
402 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley1bea1732014-12-17 19:06:57 -0800404 case SSL3_ST_CW_CHANNEL_ID_A:
405 case SSL3_ST_CW_CHANNEL_ID_B:
406 ret = ssl3_send_channel_id(s);
407 if (ret <= 0) {
408 goto end;
409 }
410 s->state = SSL3_ST_CW_FINISHED_A;
411 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langley1bea1732014-12-17 19:06:57 -0800413 case SSL3_ST_CW_FINISHED_A:
414 case SSL3_ST_CW_FINISHED_B:
415 ret =
416 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
417 s->enc_method->client_finished_label,
418 s->enc_method->client_finished_label_len);
419 if (ret <= 0) {
420 goto end;
421 }
422 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langley1bea1732014-12-17 19:06:57 -0800424 if (s->hit) {
425 s->s3->tmp.next_state = SSL_ST_OK;
426 } else {
427 /* This is a non-resumption handshake. If it involves ChannelID, then
428 * record the handshake hashes at this point in the session so that
429 * any resumption of this session with ChannelID can sign those
430 * hashes. */
431 if (s->s3->tlsext_channel_id_new) {
432 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500433 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800434 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500435 }
Adam Langley1bea1732014-12-17 19:06:57 -0800436 }
David Benjamined7c4752015-02-16 19:16:46 -0500437 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
438 ssl3_can_false_start(s) &&
439 /* No False Start on renegotiation (would complicate the state
440 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400441 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500442 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800443 } else {
444 /* Allow NewSessionTicket if ticket expected */
445 if (s->tlsext_ticket_expected) {
446 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
447 } else {
448 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
449 }
450 }
451 }
452 s->init_num = 0;
453 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langley1bea1732014-12-17 19:06:57 -0800455 case SSL3_ST_CR_SESSION_TICKET_A:
456 case SSL3_ST_CR_SESSION_TICKET_B:
457 ret = ssl3_get_new_session_ticket(s);
458 if (ret <= 0) {
459 goto end;
460 }
461 s->state = SSL3_ST_CR_CHANGE;
462 s->init_num = 0;
463 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langley1bea1732014-12-17 19:06:57 -0800465 case SSL3_ST_CR_CERT_STATUS_A:
466 case SSL3_ST_CR_CERT_STATUS_B:
467 ret = ssl3_get_cert_status(s);
468 if (ret <= 0) {
469 goto end;
470 }
471 s->state = SSL3_ST_CR_KEY_EXCH_A;
472 s->init_num = 0;
473 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400474
Adam Langley1bea1732014-12-17 19:06:57 -0800475 case SSL3_ST_CR_CHANGE:
476 /* At this point, the next message must be entirely behind a
477 * ChangeCipherSpec. */
478 if (!ssl3_expect_change_cipher_spec(s)) {
479 ret = -1;
480 goto end;
481 }
482 s->state = SSL3_ST_CR_FINISHED_A;
483 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langley1bea1732014-12-17 19:06:57 -0800485 case SSL3_ST_CR_FINISHED_A:
486 case SSL3_ST_CR_FINISHED_B:
487 ret =
488 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
489 if (ret <= 0) {
490 goto end;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langley1bea1732014-12-17 19:06:57 -0800493 if (s->hit) {
494 s->state = SSL3_ST_CW_CHANGE_A;
495 } else {
496 s->state = SSL_ST_OK;
497 }
498 s->init_num = 0;
499 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley1bea1732014-12-17 19:06:57 -0800501 case SSL3_ST_CW_FLUSH:
502 s->rwstate = SSL_WRITING;
503 if (BIO_flush(s->wbio) <= 0) {
504 ret = -1;
505 goto end;
506 }
507 s->rwstate = SSL_NOTHING;
508 s->state = s->s3->tmp.next_state;
509 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700510
David Benjamined7c4752015-02-16 19:16:46 -0500511 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800512 /* Allow NewSessionTicket if ticket expected */
513 if (s->tlsext_ticket_expected) {
514 s->state = SSL3_ST_CR_SESSION_TICKET_A;
515 } else {
516 s->state = SSL3_ST_CR_CHANGE;
517 }
David Benjamined7c4752015-02-16 19:16:46 -0500518 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700519
Adam Langley1bea1732014-12-17 19:06:57 -0800520 ssl_free_wbio_buffer(s);
521 ret = 1;
522 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley1bea1732014-12-17 19:06:57 -0800524 case SSL_ST_OK:
525 /* clean a few things up */
526 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamin2755a3e2015-04-22 16:17:58 -0400528 BUF_MEM_free(s->init_buf);
529 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langley1bea1732014-12-17 19:06:57 -0800531 /* Remove write buffering now. */
532 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjaminba4594a2015-06-18 18:36:15 -0400534 const int is_initial_handshake = !s->s3->initial_handshake_complete;
535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500537 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400538 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjaminba4594a2015-06-18 18:36:15 -0400540 if (is_initial_handshake) {
541 /* Renegotiations do not participate in session resumption. */
542 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley1bea1732014-12-17 19:06:57 -0800545 ret = 1;
546 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langley1bea1732014-12-17 19:06:57 -0800548 if (cb != NULL) {
549 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langley1bea1732014-12-17 19:06:57 -0800552 goto end;
553
554 default:
555 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
556 ret = -1;
557 goto end;
558 }
559
560 if (!s->s3->tmp.reuse_message && !skip) {
561 if (cb != NULL && s->state != state) {
562 new_state = s->state;
563 s->state = state;
564 cb(s, SSL_CB_CONNECT_LOOP, 1);
565 s->state = new_state;
566 }
567 }
568 skip = 0;
569 }
570
Adam Langley95c29f32014-06-20 12:00:00 -0700571end:
Adam Langley1bea1732014-12-17 19:06:57 -0800572 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400573 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800574 if (cb != NULL) {
575 cb(s, SSL_CB_CONNECT_EXIT, ret);
576 }
577 return ret;
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langley1bea1732014-12-17 19:06:57 -0800580int ssl3_send_client_hello(SSL *s) {
581 uint8_t *buf, *p, *d;
582 int i;
583 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langley1bea1732014-12-17 19:06:57 -0800585 buf = (uint8_t *)s->init_buf->data;
586 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
587 if (!s->s3->have_version) {
588 uint16_t max_version = ssl3_get_max_client_version(s);
589 /* Disabling all versions is silly: return an error. */
590 if (max_version == 0) {
591 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
592 goto err;
593 }
594 s->version = max_version;
595 s->client_version = max_version;
596 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500597
Adam Langley1bea1732014-12-17 19:06:57 -0800598 /* If the configured session was created at a version higher than our
599 * maximum version, drop it. */
600 if (s->session &&
601 (s->session->session_id_length == 0 || s->session->not_resumable ||
602 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
603 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
604 SSL_set_session(s, NULL);
605 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500606
Adam Langley1bea1732014-12-17 19:06:57 -0800607 /* else use the pre-loaded session */
608 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley1bea1732014-12-17 19:06:57 -0800610 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
611 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400612 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
David Benjamin74d8bc22015-05-21 02:16:53 -0400613 !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
614 0 /* client */)) {
David Benjamin2ddba8c2015-03-18 19:40:51 -0400615 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langley1bea1732014-12-17 19:06:57 -0800618 /* Do the message type and length last. Note: the final argument to
619 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
620 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langley1bea1732014-12-17 19:06:57 -0800622 /* version indicates the negotiated version: for example from an SSLv2/v3
623 * compatible client hello). The client_version field is the maximum
624 * version we permit and it is also used in RSA encrypted premaster
625 * secrets. Some servers can choke if we initially report a higher version
626 * then renegotiate to a lower one in the premaster secret. This didn't
627 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
628 * or later if the server only supports 1.0.
629 *
630 * Possible scenario with previous logic:
631 * 1. Client hello indicates TLS 1.2
632 * 2. Server hello says TLS 1.0
633 * 3. RSA encrypted premaster secret uses 1.2.
634 * 4. Handhaked proceeds using TLS 1.0.
635 * 5. Server sends hello request to renegotiate.
636 * 6. Client hello indicates TLS v1.0 as we now
637 * know that is maximum server supports.
638 * 7. Server chokes on RSA encrypted premaster secret
639 * containing version 1.0.
640 *
641 * For interoperability it should be OK to always use the maximum version
642 * we support in client hello and then rely on the checking of version to
643 * ensure the servers isn't being inconsistent: for example initially
644 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
645 * using client_version in client hello and not resetting it to the
646 * negotiated version. */
647 *(p++) = s->client_version >> 8;
648 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langley1bea1732014-12-17 19:06:57 -0800650 /* Random stuff */
651 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
652 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langley1bea1732014-12-17 19:06:57 -0800654 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400655 if (s->s3->initial_handshake_complete || s->session == NULL) {
656 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800657 i = 0;
658 } else {
659 i = s->session->session_id_length;
660 }
661 *(p++) = i;
662 if (i != 0) {
663 if (i > (int)sizeof(s->session->session_id)) {
664 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
665 goto err;
666 }
667 memcpy(p, s->session->session_id, i);
668 p += i;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langley1bea1732014-12-17 19:06:57 -0800671 /* cookie stuff for DTLS */
672 if (SSL_IS_DTLS(s)) {
673 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
674 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
675 goto err;
676 }
677 *(p++) = s->d1->cookie_len;
678 memcpy(p, s->d1->cookie, s->d1->cookie_len);
679 p += s->d1->cookie_len;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langley1bea1732014-12-17 19:06:57 -0800682 /* Ciphers supported */
683 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
684 if (i == 0) {
685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
686 SSL_R_NO_CIPHERS_AVAILABLE);
687 goto err;
688 }
689 s2n(i, p);
690 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley1bea1732014-12-17 19:06:57 -0800692 /* COMPRESSION */
693 *(p++) = 1;
694 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langley1bea1732014-12-17 19:06:57 -0800696 /* TLS extensions*/
697 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
698 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
699 goto err;
700 }
701
702 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
703 p - buf);
704 if (p == NULL) {
705 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
706 goto err;
707 }
708
709 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500710 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
711 goto err;
712 }
Adam Langley1bea1732014-12-17 19:06:57 -0800713 s->state = SSL3_ST_CW_CLNT_HELLO_B;
714 }
715
716 /* SSL3_ST_CW_CLNT_HELLO_B */
717 return ssl_do_write(s);
718
Adam Langley95c29f32014-06-20 12:00:00 -0700719err:
Adam Langley1bea1732014-12-17 19:06:57 -0800720 return -1;
721}
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langley1bea1732014-12-17 19:06:57 -0800723int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400724 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800725 const SSL_CIPHER *c;
726 CERT *ct = s->cert;
727 int al = SSL_AD_INTERNAL_ERROR, ok;
728 long n;
729 CBS server_hello, server_random, session_id;
730 uint16_t server_version, cipher_suite;
731 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400732 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langley1bea1732014-12-17 19:06:57 -0800734 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
735 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
736 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500737 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langley1bea1732014-12-17 19:06:57 -0800739 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500740 uint32_t err = ERR_peek_error();
741 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
742 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
743 /* Add a dedicated error code to the queue for a handshake_failure alert
744 * in response to ClientHello. This matches NSS's client behavior and
745 * gives a better error on a (probable) failure to negotiate initial
746 * parameters. Note: this error code comes after the original one.
747 *
748 * See https://crbug.com/446505. */
749 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
750 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
751 }
Adam Langley1bea1732014-12-17 19:06:57 -0800752 return n;
753 }
Adam Langley95c29f32014-06-20 12:00:00 -0700754
Adam Langley1bea1732014-12-17 19:06:57 -0800755 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400756
Adam Langley1bea1732014-12-17 19:06:57 -0800757 if (!CBS_get_u16(&server_hello, &server_version) ||
758 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
759 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
760 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
761 !CBS_get_u16(&server_hello, &cipher_suite) ||
762 !CBS_get_u8(&server_hello, &compression_method)) {
763 al = SSL_AD_DECODE_ERROR;
764 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
765 goto f_err;
766 }
David Benjamina03d95d2014-07-12 19:49:07 -0400767
David Benjamine6df0542015-05-12 22:02:08 -0400768 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800769 if (!s->s3->have_version) {
770 if (!ssl3_is_version_enabled(s, server_version)) {
771 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
772 s->version = server_version;
773 /* Mark the version as fixed so the record-layer version is not clamped
774 * to TLS 1.0. */
775 s->s3->have_version = 1;
776 al = SSL_AD_PROTOCOL_VERSION;
777 goto f_err;
778 }
779 s->version = server_version;
780 s->enc_method = ssl3_get_enc_method(server_version);
781 assert(s->enc_method != NULL);
782 /* At this point, the connection's version is known and s->version is
783 * fixed. Begin enforcing the record-layer version. */
784 s->s3->have_version = 1;
785 } else if (server_version != s->version) {
786 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
787 al = SSL_AD_PROTOCOL_VERSION;
788 goto f_err;
789 }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langley1bea1732014-12-17 19:06:57 -0800791 /* Copy over the server random. */
792 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langley1bea1732014-12-17 19:06:57 -0800794 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400795 if (!s->s3->initial_handshake_complete && s->session != NULL &&
796 CBS_mem_equal(&session_id, s->session->session_id,
797 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800798 if (s->sid_ctx_length != s->session->sid_ctx_length ||
799 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
800 /* actually a client application bug */
801 al = SSL_AD_ILLEGAL_PARAMETER;
802 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
803 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
804 goto f_err;
805 }
806 s->hit = 1;
807 } else {
808 /* The session wasn't resumed. Create a fresh SSL_SESSION to
809 * fill out. */
810 s->hit = 0;
811 if (!ssl_get_new_session(s, 0)) {
812 goto f_err;
813 }
814 /* Note: session_id could be empty. */
815 s->session->session_id_length = CBS_len(&session_id);
816 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
817 }
David Benjamina03d95d2014-07-12 19:49:07 -0400818
David Benjamina1c90a52015-05-30 17:03:14 -0400819 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800820 if (c == NULL) {
821 /* unknown cipher */
822 al = SSL_AD_ILLEGAL_PARAMETER;
823 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
824 SSL_R_UNKNOWN_CIPHER_RETURNED);
825 goto f_err;
826 }
827 /* ct->mask_ssl was computed from client capabilities. Now
828 * that the final version is known, compute a new mask_ssl. */
829 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
830 mask_ssl = SSL_TLSV1_2;
831 } else {
832 mask_ssl = 0;
833 }
834 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
835 * the server selected it, it's an error. */
836 if ((c->algorithm_ssl & mask_ssl) ||
837 (c->algorithm_mkey & ct->mask_k) ||
838 (c->algorithm_auth & ct->mask_a)) {
839 al = SSL_AD_ILLEGAL_PARAMETER;
840 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
841 goto f_err;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langley1bea1732014-12-17 19:06:57 -0800844 sk = ssl_get_ciphers_by_id(s);
845 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
846 /* we did not say we would use this cipher */
847 al = SSL_AD_ILLEGAL_PARAMETER;
848 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
849 goto f_err;
850 }
Adam Langley95c29f32014-06-20 12:00:00 -0700851
David Benjaminece3de92015-03-16 18:02:20 -0400852 if (s->hit) {
853 if (s->session->cipher != c) {
854 al = SSL_AD_ILLEGAL_PARAMETER;
855 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
856 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
857 goto f_err;
858 }
859 if (s->session->ssl_version != s->version) {
860 al = SSL_AD_ILLEGAL_PARAMETER;
861 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
862 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
863 goto f_err;
864 }
Adam Langley1bea1732014-12-17 19:06:57 -0800865 }
866 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500867
David Benjamin5f04b652015-05-26 17:30:10 -0400868 /* If doing a full handshake with TLS 1.2, the server may request a client
869 * certificate which requires hashing the handshake transcript under a
870 * different hash. Otherwise, release the handshake buffer. */
871 if ((!SSL_USE_SIGALGS(s) || s->hit) &&
Adam Langley1bea1732014-12-17 19:06:57 -0800872 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
873 goto f_err;
874 }
David Benjamina03d95d2014-07-12 19:49:07 -0400875
Adam Langley1bea1732014-12-17 19:06:57 -0800876 /* Only the NULL compression algorithm is supported. */
877 if (compression_method != 0) {
878 al = SSL_AD_ILLEGAL_PARAMETER;
879 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
880 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
881 goto f_err;
882 }
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley1bea1732014-12-17 19:06:57 -0800884 /* TLS extensions */
885 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
886 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
887 goto err;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langley1bea1732014-12-17 19:06:57 -0800890 /* There should be nothing left over in the record. */
891 if (CBS_len(&server_hello) != 0) {
892 /* wrong packet length */
893 al = SSL_AD_DECODE_ERROR;
894 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
895 goto f_err;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyba5934b2015-06-02 10:50:35 -0700898 if (s->hit &&
899 s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
900 al = SSL_AD_HANDSHAKE_FAILURE;
901 if (s->session->extended_master_secret) {
902 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
903 SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
904 } else {
905 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
906 SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
907 }
908 goto f_err;
909 }
910
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
David Benjamin436bf822015-06-07 19:50:37 -0400919/* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
920 * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
921 * on the error queue. */
922static int ssl3_check_certificate_for_cipher(X509 *leaf,
923 const SSL_CIPHER *cipher) {
924 int ret = 0;
925 EVP_PKEY *pkey = X509_get_pubkey(leaf);
David Benjamind1c1c8e2015-06-08 18:36:21 -0400926 if (pkey == NULL) {
David Benjamin436bf822015-06-07 19:50:37 -0400927 goto err;
928 }
929
930 /* Check the certificate's type matches the cipher. */
931 int cert_type = ssl_cert_type(pkey);
932 if (cert_type < 0) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400933 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400934 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
935 goto err;
936 }
937 int expected_type = ssl_cipher_get_cert_index(cipher);
938 assert(expected_type >= 0);
939 if (cert_type != expected_type) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400940 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400941 SSL_R_WRONG_CERTIFICATE_TYPE);
942 goto err;
943 }
944
945 /* TODO(davidben): This behavior is preserved from upstream. Should key usages
946 * be checked in other cases as well? */
947 if (cipher->algorithm_auth & SSL_aECDSA) {
948 /* This call populates the ex_flags field correctly */
949 X509_check_purpose(leaf, -1, 0);
950 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
951 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400952 OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
David Benjamin436bf822015-06-07 19:50:37 -0400953 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
954 goto err;
955 }
956 }
957
958 ret = 1;
959
960err:
961 EVP_PKEY_free(pkey);
962 return ret;
963}
964
Adam Langley1bea1732014-12-17 19:06:57 -0800965int ssl3_get_server_certificate(SSL *s) {
966 int al, i, ok, ret = -1;
967 unsigned long n;
968 X509 *x = NULL;
969 STACK_OF(X509) *sk = NULL;
970 SESS_CERT *sc;
971 EVP_PKEY *pkey = NULL;
972 CBS cbs, certificate_list;
973 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langley1bea1732014-12-17 19:06:57 -0800975 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400976 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500977 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langley1bea1732014-12-17 19:06:57 -0800979 if (!ok) {
980 return n;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langley1bea1732014-12-17 19:06:57 -0800983 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langley1bea1732014-12-17 19:06:57 -0800985 sk = sk_X509_new_null();
986 if (sk == NULL) {
987 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
988 goto err;
989 }
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langley1bea1732014-12-17 19:06:57 -0800991 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
David Benjamin8923c0b2015-06-07 11:42:34 -0400992 CBS_len(&certificate_list) == 0 ||
Adam Langley1bea1732014-12-17 19:06:57 -0800993 CBS_len(&cbs) != 0) {
994 al = SSL_AD_DECODE_ERROR;
David Benjamin8923c0b2015-06-07 11:42:34 -0400995 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800996 goto f_err;
997 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400998
Adam Langley1bea1732014-12-17 19:06:57 -0800999 while (CBS_len(&certificate_list) > 0) {
1000 CBS certificate;
1001 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1002 al = SSL_AD_DECODE_ERROR;
1003 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1004 SSL_R_CERT_LENGTH_MISMATCH);
1005 goto f_err;
1006 }
1007 data = CBS_data(&certificate);
1008 x = d2i_X509(NULL, &data, CBS_len(&certificate));
1009 if (x == NULL) {
1010 al = SSL_AD_BAD_CERTIFICATE;
1011 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1012 goto f_err;
1013 }
1014 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1015 al = SSL_AD_DECODE_ERROR;
1016 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1017 SSL_R_CERT_LENGTH_MISMATCH);
1018 goto f_err;
1019 }
1020 if (!sk_X509_push(sk, x)) {
1021 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1022 goto err;
1023 }
1024 x = NULL;
1025 }
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langley1bea1732014-12-17 19:06:57 -08001027 i = ssl_verify_cert_chain(s, sk);
1028 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1029 al = ssl_verify_alarm_type(s->verify_result);
1030 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1031 SSL_R_CERTIFICATE_VERIFY_FAILED);
1032 goto f_err;
1033 }
1034 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -07001035
David Benjamin436bf822015-06-07 19:50:37 -04001036 X509 *leaf = sk_X509_value(sk, 0);
1037 if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
1038 al = SSL_AD_ILLEGAL_PARAMETER;
1039 goto f_err;
1040 }
1041
Adam Langley1bea1732014-12-17 19:06:57 -08001042 sc = ssl_sess_cert_new();
1043 if (sc == NULL) {
1044 goto err;
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
David Benjamin2755a3e2015-04-22 16:17:58 -04001047 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001048 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
David Benjamin436bf822015-06-07 19:50:37 -04001050 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1051 * the leaf. */
Adam Langley1bea1732014-12-17 19:06:57 -08001052 sc->cert_chain = sk;
Adam Langley1bea1732014-12-17 19:06:57 -08001053 sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001054
David Benjaminb31040d2015-06-07 10:53:32 -04001055 X509_free(sc->peer_cert);
David Benjamin436bf822015-06-07 19:50:37 -04001056 sc->peer_cert = X509_up_ref(leaf);
Adam Langley95c29f32014-06-20 12:00:00 -07001057
David Benjamin2755a3e2015-04-22 16:17:58 -04001058 X509_free(s->session->peer);
David Benjamin436bf822015-06-07 19:50:37 -04001059 s->session->peer = X509_up_ref(leaf);
David Benjamind26aea62014-07-12 00:13:56 -04001060
Adam Langley1bea1732014-12-17 19:06:57 -08001061 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langley1bea1732014-12-17 19:06:57 -08001063 ret = 1;
1064
1065 if (0) {
1066 f_err:
1067 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1068 }
1069
1070err:
1071 EVP_PKEY_free(pkey);
1072 X509_free(x);
1073 sk_X509_pop_free(sk, X509_free);
1074 return ret;
1075}
1076
1077int ssl3_get_server_key_exchange(SSL *s) {
1078 EVP_MD_CTX md_ctx;
1079 int al, ok;
1080 long n, alg_k, alg_a;
1081 EVP_PKEY *pkey = NULL;
1082 const EVP_MD *md = NULL;
1083 RSA *rsa = NULL;
1084 DH *dh = NULL;
1085 EC_KEY *ecdh = NULL;
1086 BN_CTX *bn_ctx = NULL;
1087 EC_POINT *srvr_ecpoint = NULL;
1088 CBS server_key_exchange, server_key_exchange_orig, parameter;
1089
1090 /* use same message size as in ssl3_get_certificate_request() as
1091 * ServerKeyExchange message may be skipped */
1092 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1093 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001094 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001095 if (!ok) {
1096 return n;
1097 }
1098
1099 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1100 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1101 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1102 SSL_R_UNEXPECTED_MESSAGE);
1103 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1104 return -1;
1105 }
1106
1107 /* In plain PSK ciphersuite, ServerKeyExchange can be
1108 omitted if no identity hint is sent. Set session->sess_cert anyway to
1109 avoid problems later.*/
1110 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1111 /* PSK ciphersuites that also send a Certificate would have already
1112 * initialized |sess_cert|. */
1113 if (s->session->sess_cert == NULL) {
1114 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001115 if (s->session->sess_cert == NULL) {
1116 return -1;
1117 }
Adam Langley1bea1732014-12-17 19:06:57 -08001118 }
1119
1120 /* TODO(davidben): This should be reset in one place with the rest of the
1121 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001122 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1123 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001124 }
1125 s->s3->tmp.reuse_message = 1;
1126 return 1;
1127 }
1128
1129 /* Retain a copy of the original CBS to compute the signature over. */
1130 CBS_init(&server_key_exchange, s->init_msg, n);
1131 server_key_exchange_orig = server_key_exchange;
1132
1133 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001134 DH_free(s->session->sess_cert->peer_dh_tmp);
1135 s->session->sess_cert->peer_dh_tmp = NULL;
1136 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1137 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001138 } else {
1139 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001140 if (s->session->sess_cert == NULL) {
1141 return -1;
1142 }
Adam Langley1bea1732014-12-17 19:06:57 -08001143 }
1144
1145 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1146 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1147 EVP_MD_CTX_init(&md_ctx);
1148
1149 if (alg_a & SSL_aPSK) {
1150 CBS psk_identity_hint;
1151
1152 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1153 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1154 &psk_identity_hint)) {
1155 al = SSL_AD_DECODE_ERROR;
1156 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1157 goto f_err;
1158 }
1159
1160 /* Store PSK identity hint for later use, hint is used in
1161 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1162 * identity hint can be as long as the maximum length of a PSK identity.
1163 * Also do not allow NULL characters; identities are saved as C strings.
1164 *
1165 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1166 * a specific identity. */
1167 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1168 CBS_contains_zero_byte(&psk_identity_hint)) {
1169 al = SSL_AD_HANDSHAKE_FAILURE;
1170 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1171 SSL_R_DATA_LENGTH_TOO_LONG);
1172 goto f_err;
1173 }
1174
1175 /* Save the identity hint as a C string. */
1176 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1177 al = SSL_AD_INTERNAL_ERROR;
1178 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1179 ERR_R_MALLOC_FAILURE);
1180 goto f_err;
1181 }
1182 }
1183
David Benjamin7061e282015-03-19 11:10:48 -04001184 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001185 CBS dh_p, dh_g, dh_Ys;
1186
1187 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1188 CBS_len(&dh_p) == 0 ||
1189 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1190 CBS_len(&dh_g) == 0 ||
1191 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1192 CBS_len(&dh_Ys) == 0) {
1193 al = SSL_AD_DECODE_ERROR;
1194 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1195 goto f_err;
1196 }
1197
1198 dh = DH_new();
1199 if (dh == NULL) {
1200 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1201 goto err;
1202 }
1203
1204 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1205 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1206 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1207 NULL) {
1208 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1209 goto err;
1210 }
1211
Adam Langleya7997f12015-05-14 17:38:50 -07001212 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001213 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1214 SSL_R_BAD_DH_P_LENGTH);
1215 goto err;
1216 }
Adam Langley1bea1732014-12-17 19:06:57 -08001217 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 }
Adam Langley1bea1732014-12-17 19:06:57 -08001271 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1272 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1273 ecdh = NULL;
1274 BN_CTX_free(bn_ctx);
1275 bn_ctx = NULL;
1276 EC_POINT_free(srvr_ecpoint);
1277 srvr_ecpoint = NULL;
1278 } else if (!(alg_k & SSL_kPSK)) {
1279 al = SSL_AD_UNEXPECTED_MESSAGE;
1280 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1281 SSL_R_UNEXPECTED_MESSAGE);
1282 goto f_err;
1283 }
1284
1285 /* At this point, |server_key_exchange| contains the signature, if any, while
1286 * |server_key_exchange_orig| contains the entire message. From that, derive
1287 * a CBS containing just the parameter. */
1288 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1289 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1290
David Benjaminf4958e72015-06-07 11:00:53 -04001291 /* ServerKeyExchange should be signed by the server's public key. */
1292 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1293 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1294 if (pkey == NULL) {
1295 goto err;
1296 }
Adam Langley1bea1732014-12-17 19:06:57 -08001297
1298 if (SSL_USE_SIGALGS(s)) {
1299 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1300 goto f_err;
1301 }
1302 } else if (pkey->type == EVP_PKEY_RSA) {
1303 md = EVP_md5_sha1();
1304 } else {
1305 md = EVP_sha1();
1306 }
1307
1308 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001309 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001310 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1311 CBS_len(&server_key_exchange) != 0) {
1312 al = SSL_AD_DECODE_ERROR;
1313 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1314 goto f_err;
1315 }
1316
1317 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1318 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1319 SSL3_RANDOM_SIZE) ||
1320 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1321 SSL3_RANDOM_SIZE) ||
1322 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1323 CBS_len(&parameter)) ||
1324 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1325 CBS_len(&signature))) {
1326 /* bad signature */
1327 al = SSL_AD_DECRYPT_ERROR;
1328 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1329 goto f_err;
1330 }
1331 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001332 /* PSK ciphers are the only supported certificate-less ciphers. */
1333 assert(alg_a == SSL_aPSK);
1334
Adam Langley1bea1732014-12-17 19:06:57 -08001335 if (CBS_len(&server_key_exchange) > 0) {
1336 al = SSL_AD_DECODE_ERROR;
1337 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1338 SSL_R_EXTRA_DATA_IN_MESSAGE);
1339 goto f_err;
1340 }
1341 }
1342 EVP_PKEY_free(pkey);
1343 EVP_MD_CTX_cleanup(&md_ctx);
1344 return 1;
1345
Adam Langley95c29f32014-06-20 12:00:00 -07001346f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001347 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001348err:
Adam Langley1bea1732014-12-17 19:06:57 -08001349 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001350 RSA_free(rsa);
1351 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001352 BN_CTX_free(bn_ctx);
1353 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001354 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001355 EVP_MD_CTX_cleanup(&md_ctx);
1356 return -1;
1357}
Adam Langley95c29f32014-06-20 12:00:00 -07001358
Adam Langley1bea1732014-12-17 19:06:57 -08001359static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1360 return X509_NAME_cmp(*a, *b);
1361}
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langley1bea1732014-12-17 19:06:57 -08001363int ssl3_get_certificate_request(SSL *s) {
1364 int ok, ret = 0;
1365 unsigned long n;
1366 X509_NAME *xn = NULL;
1367 STACK_OF(X509_NAME) *ca_sk = NULL;
1368 CBS cbs;
1369 CBS certificate_types;
1370 CBS certificate_authorities;
1371 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langley1bea1732014-12-17 19:06:57 -08001373 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1374 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001375 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001376
Adam Langley1bea1732014-12-17 19:06:57 -08001377 if (!ok) {
1378 return n;
1379 }
David Benjamin688d8df2014-11-02 23:06:42 -05001380
Adam Langley1bea1732014-12-17 19:06:57 -08001381 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001382
Adam Langley1bea1732014-12-17 19:06:57 -08001383 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1384 s->s3->tmp.reuse_message = 1;
1385 /* If we get here we don't need any cached handshake records as we wont be
1386 * doing client auth. */
1387 if (s->s3->handshake_buffer &&
1388 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1389 goto err;
1390 }
1391 return 1;
1392 }
David Benjamined439582014-07-14 19:13:02 -04001393
Adam Langley1bea1732014-12-17 19:06:57 -08001394 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1395 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1396 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1397 SSL_R_WRONG_MESSAGE_TYPE);
1398 goto err;
1399 }
Adam Langley95c29f32014-06-20 12:00:00 -07001400
Adam Langley1bea1732014-12-17 19:06:57 -08001401 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langley1bea1732014-12-17 19:06:57 -08001403 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1404 if (ca_sk == NULL) {
1405 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1406 goto err;
1407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langley1bea1732014-12-17 19:06:57 -08001409 /* get the certificate types */
1410 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1411 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1412 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1413 goto err;
1414 }
David Benjamined439582014-07-14 19:13:02 -04001415
Adam Langley1bea1732014-12-17 19:06:57 -08001416 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1417 &s->s3->tmp.num_certificate_types)) {
1418 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1419 goto err;
1420 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001421
Adam Langley1bea1732014-12-17 19:06:57 -08001422 if (SSL_USE_SIGALGS(s)) {
1423 CBS supported_signature_algorithms;
1424 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1425 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1426 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1427 goto err;
1428 }
David Benjamined439582014-07-14 19:13:02 -04001429
Adam Langley1bea1732014-12-17 19:06:57 -08001430 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1431 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1432 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1433 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1434 goto err;
1435 }
1436 }
David Benjamined439582014-07-14 19:13:02 -04001437
Adam Langley1bea1732014-12-17 19:06:57 -08001438 /* get the CA RDNs */
1439 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1440 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1441 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1442 goto err;
1443 }
Adam Langley95c29f32014-06-20 12:00:00 -07001444
Adam Langley1bea1732014-12-17 19:06:57 -08001445 while (CBS_len(&certificate_authorities) > 0) {
1446 CBS distinguished_name;
1447 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1448 &distinguished_name)) {
1449 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1450 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1451 SSL_R_CA_DN_TOO_LONG);
1452 goto err;
1453 }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langley1bea1732014-12-17 19:06:57 -08001455 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001456
Adam Langley1bea1732014-12-17 19:06:57 -08001457 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1458 if (xn == NULL) {
1459 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1460 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1461 goto err;
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463
Adam Langley1bea1732014-12-17 19:06:57 -08001464 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1465 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001466 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001467 goto err;
1468 }
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langley1bea1732014-12-17 19:06:57 -08001470 if (CBS_len(&distinguished_name) != 0) {
1471 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1472 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1473 SSL_R_CA_DN_LENGTH_MISMATCH);
1474 goto err;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langley1bea1732014-12-17 19:06:57 -08001477 if (!sk_X509_NAME_push(ca_sk, xn)) {
1478 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1479 ERR_R_MALLOC_FAILURE);
1480 goto err;
1481 }
1482 }
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langley1bea1732014-12-17 19:06:57 -08001484 /* we should setup a certificate to return.... */
1485 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001486 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001487 s->s3->tmp.ca_names = ca_sk;
1488 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001489
Adam Langley1bea1732014-12-17 19:06:57 -08001490 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001491
Adam Langley1bea1732014-12-17 19:06:57 -08001492err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001493 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001494 return ret;
1495}
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langley1bea1732014-12-17 19:06:57 -08001497int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001498 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001499 long n;
1500 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001501
Adam Langley1bea1732014-12-17 19:06:57 -08001502 n = s->method->ssl_get_message(
1503 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001504 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langley1bea1732014-12-17 19:06:57 -08001506 if (!ok) {
1507 return n;
1508 }
Adam Langley95c29f32014-06-20 12:00:00 -07001509
David Benjamin95d31822015-06-15 19:53:32 -04001510 if (s->hit) {
1511 /* The server is sending a new ticket for an existing session. Sessions are
1512 * immutable once established, so duplicate all but the ticket of the
1513 * existing session. */
1514 uint8_t *bytes;
1515 size_t bytes_len;
1516 if (!SSL_SESSION_to_bytes_for_ticket(s->session, &bytes, &bytes_len)) {
1517 goto err;
1518 }
1519 SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1520 OPENSSL_free(bytes);
1521 if (new_session == NULL) {
1522 /* This should never happen. */
1523 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_INTERNAL_ERROR);
1524 goto err;
1525 }
David Benjamin680ca962015-06-18 12:37:23 -04001526 if (s->session->sess_cert != NULL) {
1527 /* |sess_cert| is not serialized and must be duplicated explicitly. */
1528 assert(new_session->sess_cert == NULL);
1529 new_session->sess_cert = ssl_sess_cert_dup(s->session->sess_cert);
1530 if (new_session->sess_cert == NULL) {
1531 SSL_SESSION_free(new_session);
1532 goto err;
1533 }
1534 }
David Benjamin95d31822015-06-15 19:53:32 -04001535
1536 SSL_SESSION_free(s->session);
1537 s->session = new_session;
1538 }
1539
Adam Langley1bea1732014-12-17 19:06:57 -08001540 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001541
Adam Langley1bea1732014-12-17 19:06:57 -08001542 if (!CBS_get_u32(&new_session_ticket,
1543 &s->session->tlsext_tick_lifetime_hint) ||
1544 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1545 CBS_len(&new_session_ticket) != 0) {
1546 al = SSL_AD_DECODE_ERROR;
1547 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1548 goto f_err;
1549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
Adam Langley1bea1732014-12-17 19:06:57 -08001551 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1552 &s->session->tlsext_ticklen)) {
1553 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1554 goto err;
1555 }
Adam Langley95c29f32014-06-20 12:00:00 -07001556
David Benjamin68070622015-02-08 23:44:59 -05001557 /* Generate a session ID for this session based on the session ticket. We use
1558 * the session ID mechanism for detecting ticket resumption. This also fits in
1559 * with assumptions elsewhere in OpenSSL.*/
1560 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1561 &s->session->session_id_length, EVP_sha256(), NULL)) {
1562 goto err;
1563 }
1564
1565 return 1;
David Benjamined439582014-07-14 19:13:02 -04001566
Adam Langley95c29f32014-06-20 12:00:00 -07001567f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001568 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001569err:
Adam Langley1bea1732014-12-17 19:06:57 -08001570 return -1;
1571}
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langley1bea1732014-12-17 19:06:57 -08001573int ssl3_get_cert_status(SSL *s) {
1574 int ok, al;
1575 long n;
1576 CBS certificate_status, ocsp_response;
1577 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001578
Adam Langley1bea1732014-12-17 19:06:57 -08001579 n = s->method->ssl_get_message(
1580 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001581 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Adam Langley1bea1732014-12-17 19:06:57 -08001583 if (!ok) {
1584 return n;
1585 }
Adam Langley95c29f32014-06-20 12:00:00 -07001586
David Benjamindcd979f2015-04-20 18:26:52 -04001587 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1588 /* A server may send status_request in ServerHello and then change
1589 * its mind about sending CertificateStatus. */
1590 s->s3->tmp.reuse_message = 1;
1591 return 1;
1592 }
1593
Adam Langley1bea1732014-12-17 19:06:57 -08001594 CBS_init(&certificate_status, s->init_msg, n);
1595 if (!CBS_get_u8(&certificate_status, &status_type) ||
1596 status_type != TLSEXT_STATUSTYPE_ocsp ||
1597 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1598 CBS_len(&ocsp_response) == 0 ||
1599 CBS_len(&certificate_status) != 0) {
1600 al = SSL_AD_DECODE_ERROR;
1601 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1602 goto f_err;
1603 }
Adam Langley95c29f32014-06-20 12:00:00 -07001604
Adam Langley1bea1732014-12-17 19:06:57 -08001605 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1606 &s->session->ocsp_response_length)) {
1607 al = SSL_AD_INTERNAL_ERROR;
1608 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1609 goto f_err;
1610 }
1611 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001612
Adam Langley95c29f32014-06-20 12:00:00 -07001613f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001614 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1615 return -1;
1616}
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langley1bea1732014-12-17 19:06:57 -08001618int ssl3_get_server_done(SSL *s) {
1619 int ok;
1620 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Adam Langley1bea1732014-12-17 19:06:57 -08001622 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1623 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1624 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001625 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001626
Adam Langley1bea1732014-12-17 19:06:57 -08001627 if (!ok) {
1628 return n;
1629 }
David Benjamin46062682014-07-14 19:14:32 -04001630
Adam Langley1bea1732014-12-17 19:06:57 -08001631 if (n > 0) {
1632 /* should contain no data */
1633 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1634 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1635 return -1;
1636 }
David Benjamin46062682014-07-14 19:14:32 -04001637
Adam Langley1bea1732014-12-17 19:06:57 -08001638 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001639}
Adam Langley1258b6a2014-06-20 12:00:00 -07001640
Adam Langley1258b6a2014-06-20 12:00:00 -07001641
Adam Langley1bea1732014-12-17 19:06:57 -08001642int ssl3_send_client_key_exchange(SSL *s) {
1643 uint8_t *p;
1644 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001645 uint32_t alg_k;
1646 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001647 uint8_t *q;
1648 EVP_PKEY *pkey = NULL;
1649 EC_KEY *clnt_ecdh = NULL;
1650 const EC_POINT *srvr_ecpoint = NULL;
1651 EVP_PKEY *srvr_pub_pkey = NULL;
1652 uint8_t *encodedPoint = NULL;
1653 int encoded_pt_len = 0;
1654 BN_CTX *bn_ctx = NULL;
1655 unsigned int psk_len = 0;
1656 uint8_t psk[PSK_MAX_PSK_LEN];
1657 uint8_t *pms = NULL;
1658 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001659
Adam Langley1bea1732014-12-17 19:06:57 -08001660 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1661 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1664 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 /* If using a PSK key exchange, prepare the pre-shared key. */
1667 if (alg_a & SSL_aPSK) {
1668 char identity[PSK_MAX_IDENTITY_LEN + 1];
1669 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langley1bea1732014-12-17 19:06:57 -08001671 if (s->psk_client_callback == NULL) {
1672 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1673 SSL_R_PSK_NO_CLIENT_CB);
1674 goto err;
1675 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001676
Adam Langley1bea1732014-12-17 19:06:57 -08001677 memset(identity, 0, sizeof(identity));
1678 psk_len =
1679 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1680 sizeof(identity), psk, sizeof(psk));
1681 if (psk_len > PSK_MAX_PSK_LEN) {
1682 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1683 ERR_R_INTERNAL_ERROR);
1684 goto err;
1685 } else if (psk_len == 0) {
1686 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1687 SSL_R_PSK_IDENTITY_NOT_FOUND);
1688 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1689 goto err;
1690 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001691
Adam Langley1bea1732014-12-17 19:06:57 -08001692 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1693 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1694 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1695 ERR_R_INTERNAL_ERROR);
1696 goto err;
1697 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001698
David Benjamin2755a3e2015-04-22 16:17:58 -04001699 OPENSSL_free(s->session->psk_identity);
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
David Benjaminb31040d2015-06-07 10:53:32 -04001734 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001735 if (pkey == NULL ||
1736 pkey->type != EVP_PKEY_RSA ||
1737 pkey->pkey.rsa == NULL) {
1738 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1739 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001740 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001741 goto err;
1742 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001743
Adam Langley1bea1732014-12-17 19:06:57 -08001744 rsa = pkey->pkey.rsa;
1745 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001746
Adam Langley1bea1732014-12-17 19:06:57 -08001747 pms[0] = s->client_version >> 8;
1748 pms[1] = s->client_version & 0xff;
1749 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1750 goto err;
1751 }
1752
1753 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1754
1755 q = p;
1756 /* In TLS and beyond, reserve space for the length prefix. */
1757 if (s->version > SSL3_VERSION) {
1758 p += 2;
1759 n += 2;
1760 }
1761 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1762 RSA_PKCS1_PADDING)) {
1763 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1764 SSL_R_BAD_RSA_ENCRYPT);
1765 goto err;
1766 }
1767 n += enc_pms_len;
1768
1769 /* Log the premaster secret, if logging is enabled. */
1770 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1771 pms_len)) {
1772 goto err;
1773 }
1774
1775 /* Fill in the length prefix. */
1776 if (s->version > SSL3_VERSION) {
1777 s2n(enc_pms_len, q);
1778 }
David Benjamin7061e282015-03-19 11:10:48 -04001779 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001780 DH *dh_srvr, *dh_clnt;
1781 SESS_CERT *scert = s->session->sess_cert;
1782 int dh_len;
1783 size_t pub_len;
1784
1785 if (scert == NULL) {
1786 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1787 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1788 SSL_R_UNEXPECTED_MESSAGE);
1789 goto err;
1790 }
1791
1792 if (scert->peer_dh_tmp == NULL) {
1793 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1794 ERR_R_INTERNAL_ERROR);
1795 goto err;
1796 }
1797 dh_srvr = scert->peer_dh_tmp;
1798
1799 /* generate a new random key */
1800 dh_clnt = DHparams_dup(dh_srvr);
1801 if (dh_clnt == NULL) {
1802 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1803 goto err;
1804 }
1805 if (!DH_generate_key(dh_clnt)) {
1806 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1807 DH_free(dh_clnt);
1808 goto err;
1809 }
1810
1811 pms_len = DH_size(dh_clnt);
1812 pms = OPENSSL_malloc(pms_len);
1813 if (pms == NULL) {
1814 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1815 ERR_R_MALLOC_FAILURE);
1816 DH_free(dh_clnt);
1817 goto err;
1818 }
1819
1820 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1821 if (dh_len <= 0) {
1822 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1823 DH_free(dh_clnt);
1824 goto err;
1825 }
1826 pms_len = dh_len;
1827
1828 /* send off the data */
1829 pub_len = BN_num_bytes(dh_clnt->pub_key);
1830 s2n(pub_len, p);
1831 BN_bn2bin(dh_clnt->pub_key, p);
1832 n += 2 + pub_len;
1833
1834 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001835 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001836 const EC_GROUP *srvr_group = NULL;
1837 EC_KEY *tkey;
1838 int field_size = 0, ecdh_len;
1839
1840 if (s->session->sess_cert == NULL) {
1841 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1842 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1843 SSL_R_UNEXPECTED_MESSAGE);
1844 goto err;
1845 }
1846
1847 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1848 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1849 ERR_R_INTERNAL_ERROR);
1850 goto err;
1851 }
1852
1853 tkey = s->session->sess_cert->peer_ecdh_tmp;
1854
1855 srvr_group = EC_KEY_get0_group(tkey);
1856 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1857 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1858 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1859 ERR_R_INTERNAL_ERROR);
1860 goto err;
1861 }
1862
1863 clnt_ecdh = EC_KEY_new();
1864 if (clnt_ecdh == NULL) {
1865 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1866 ERR_R_MALLOC_FAILURE);
1867 goto err;
1868 }
1869
1870 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1871 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1872 goto err;
1873 }
1874
1875 /* Generate a new ECDH key pair */
1876 if (!EC_KEY_generate_key(clnt_ecdh)) {
1877 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1878 goto err;
1879 }
1880
1881 field_size = EC_GROUP_get_degree(srvr_group);
1882 if (field_size <= 0) {
1883 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1884 goto err;
1885 }
1886
1887 pms_len = (field_size + 7) / 8;
1888 pms = OPENSSL_malloc(pms_len);
1889 if (pms == NULL) {
1890 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1891 ERR_R_MALLOC_FAILURE);
1892 goto err;
1893 }
1894
1895 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1896 if (ecdh_len <= 0) {
1897 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1898 goto err;
1899 }
1900 pms_len = ecdh_len;
1901
1902 /* First check the size of encoding and allocate memory accordingly. */
1903 encoded_pt_len =
1904 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1905 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1906
1907 encodedPoint =
1908 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1909 bn_ctx = BN_CTX_new();
1910 if (encodedPoint == NULL || bn_ctx == NULL) {
1911 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1912 ERR_R_MALLOC_FAILURE);
1913 goto err;
1914 }
1915
1916 /* Encode the public key */
1917 encoded_pt_len = EC_POINT_point2oct(
1918 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1919 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1920
1921 *p = encoded_pt_len; /* length of encoded point */
1922 /* Encoded point will be copied here */
1923 p += 1;
1924 n += 1;
1925 /* copy the point */
1926 memcpy(p, encodedPoint, encoded_pt_len);
1927 /* increment n to account for length field */
1928 n += encoded_pt_len;
1929
1930 /* Free allocated memory */
1931 BN_CTX_free(bn_ctx);
1932 bn_ctx = NULL;
1933 OPENSSL_free(encodedPoint);
1934 encodedPoint = NULL;
1935 EC_KEY_free(clnt_ecdh);
1936 clnt_ecdh = NULL;
1937 EVP_PKEY_free(srvr_pub_pkey);
1938 srvr_pub_pkey = NULL;
1939 } else if (alg_k & SSL_kPSK) {
1940 /* For plain PSK, other_secret is a block of 0s with the same length as
1941 * the pre-shared key. */
1942 pms_len = psk_len;
1943 pms = OPENSSL_malloc(pms_len);
1944 if (pms == NULL) {
1945 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1946 ERR_R_MALLOC_FAILURE);
1947 goto err;
1948 }
1949 memset(pms, 0, pms_len);
1950 } else {
1951 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1952 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1953 ERR_R_INTERNAL_ERROR);
1954 goto err;
1955 }
1956
1957 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1958 * key. */
1959 if (alg_a & SSL_aPSK) {
1960 CBB cbb, child;
1961 uint8_t *new_pms;
1962 size_t new_pms_len;
1963
1964 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1965 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1966 ERR_R_MALLOC_FAILURE);
1967 goto err;
1968 }
1969 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1970 !CBB_add_bytes(&child, pms, pms_len) ||
1971 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1972 !CBB_add_bytes(&child, psk, psk_len) ||
1973 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1974 CBB_cleanup(&cbb);
1975 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1976 ERR_R_INTERNAL_ERROR);
1977 goto err;
1978 }
1979 OPENSSL_cleanse(pms, pms_len);
1980 OPENSSL_free(pms);
1981 pms = new_pms;
1982 pms_len = new_pms_len;
1983 }
1984
1985 /* The message must be added to the finished hash before calculating the
1986 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001987 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1988 goto err;
1989 }
Adam Langley1bea1732014-12-17 19:06:57 -08001990 s->state = SSL3_ST_CW_KEY_EXCH_B;
1991
1992 s->session->master_key_length = s->enc_method->generate_master_secret(
1993 s, s->session->master_key, pms, pms_len);
1994 if (s->session->master_key_length == 0) {
1995 goto err;
1996 }
1997 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1998 OPENSSL_cleanse(pms, pms_len);
1999 OPENSSL_free(pms);
2000 }
2001
2002 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05002003 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07002004
2005err:
Adam Langley1bea1732014-12-17 19:06:57 -08002006 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002007 OPENSSL_free(encodedPoint);
2008 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08002009 EVP_PKEY_free(srvr_pub_pkey);
2010 if (pms) {
2011 OPENSSL_cleanse(pms, pms_len);
2012 OPENSSL_free(pms);
2013 }
2014 return -1;
2015}
Adam Langley1258b6a2014-06-20 12:00:00 -07002016
Adam Langley1bea1732014-12-17 19:06:57 -08002017int ssl3_send_cert_verify(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002018 if (s->state == SSL3_ST_CW_CERT_VRFY_A ||
2019 s->state == SSL3_ST_CW_CERT_VRFY_B) {
2020 enum ssl_private_key_result_t sign_result;
2021 uint8_t *p = ssl_handshake_start(s);
2022 size_t signature_length = 0;
2023 unsigned long n = 0;
2024 EVP_PKEY *pkey = s->cert->key->privatekey;
2025 assert(pkey != NULL || s->cert->key_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002026
David Benjaminb4d65fd2015-05-29 17:11:21 -04002027 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2028 uint8_t *buf = (uint8_t *)s->init_buf->data;
2029 const EVP_MD *md = NULL;
2030 uint8_t digest[EVP_MAX_MD_SIZE];
2031 size_t digest_length;
Adam Langley1bea1732014-12-17 19:06:57 -08002032
David Benjaminb4d65fd2015-05-29 17:11:21 -04002033 /* Write out the digest type if need be. */
2034 if (SSL_USE_SIGALGS(s)) {
2035 md = tls1_choose_signing_digest(s, pkey);
2036 if (!tls12_get_sigandhash(s, p, pkey, md)) {
2037 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2038 return -1;
2039 }
2040 p += 2;
2041 n += 2;
Adam Langley1bea1732014-12-17 19:06:57 -08002042 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002043
2044 /* Compute the digest. */
2045 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2046 return -1;
2047 }
2048
2049 /* The handshake buffer is no longer necessary. */
2050 if (s->s3->handshake_buffer &&
2051 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2052 return -1;
2053 }
2054
2055 /* Sign the digest. */
2056 signature_length = ssl_private_key_max_signature_len(s, pkey);
2057 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2058 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify,
2059 SSL_R_DATA_LENGTH_TOO_LONG);
2060 return -1;
2061 }
2062
2063 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2064 sign_result = ssl_private_key_sign(s, pkey, &p[2], &signature_length,
2065 signature_length, md, digest,
2066 digest_length);
2067 } else {
2068 if (SSL_USE_SIGALGS(s)) {
2069 /* The digest has already been selected and written. */
2070 p += 2;
2071 n += 2;
2072 }
2073 signature_length = ssl_private_key_max_signature_len(s, pkey);
2074 s->rwstate = SSL_PRIVATE_KEY_OPERATION;
2075 sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length,
2076 signature_length);
Adam Langley1bea1732014-12-17 19:06:57 -08002077 }
2078
David Benjaminb4d65fd2015-05-29 17:11:21 -04002079 if (sign_result == ssl_private_key_retry) {
2080 s->state = SSL3_ST_CW_CERT_VRFY_B;
2081 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002082 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002083 s->rwstate = SSL_NOTHING;
2084 if (sign_result != ssl_private_key_success) {
2085 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08002086 }
2087
2088 s2n(signature_length, p);
2089 n += signature_length + 2;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002090 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002091 return -1;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002092 }
David Benjaminb4d65fd2015-05-29 17:11:21 -04002093 s->state = SSL3_ST_CW_CERT_VRFY_C;
Adam Langley1bea1732014-12-17 19:06:57 -08002094 }
2095
Adam Langley1bea1732014-12-17 19:06:57 -08002096 return ssl_do_write(s);
Adam Langley1bea1732014-12-17 19:06:57 -08002097}
2098
2099/* ssl3_has_client_certificate returns true if a client certificate is
2100 * configured. */
2101static int ssl3_has_client_certificate(SSL *s) {
David Benjaminb4d65fd2015-05-29 17:11:21 -04002102 return s->cert && s->cert->key->x509 && (s->cert->key->privatekey ||
2103 s->cert->key_method);
Adam Langley1bea1732014-12-17 19:06:57 -08002104}
2105
2106int ssl3_send_client_certificate(SSL *s) {
2107 X509 *x509 = NULL;
2108 EVP_PKEY *pkey = NULL;
2109 int i;
2110
2111 if (s->state == SSL3_ST_CW_CERT_A) {
2112 /* Let cert callback update client certificates if required */
2113 if (s->cert->cert_cb) {
2114 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2115 if (i < 0) {
2116 s->rwstate = SSL_X509_LOOKUP;
2117 return -1;
2118 }
2119 if (i == 0) {
2120 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2121 return 0;
2122 }
2123 s->rwstate = SSL_NOTHING;
2124 }
2125
2126 if (ssl3_has_client_certificate(s)) {
2127 s->state = SSL3_ST_CW_CERT_C;
2128 } else {
2129 s->state = SSL3_ST_CW_CERT_B;
2130 }
2131 }
2132
2133 /* We need to get a client cert */
2134 if (s->state == SSL3_ST_CW_CERT_B) {
2135 /* If we get an error, we need to:
2136 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2137 * We then get retried later */
2138 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2139 if (i < 0) {
2140 s->rwstate = SSL_X509_LOOKUP;
2141 return -1;
2142 }
2143 s->rwstate = SSL_NOTHING;
2144 if (i == 1 && pkey != NULL && x509 != NULL) {
2145 s->state = SSL3_ST_CW_CERT_B;
2146 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2147 i = 0;
2148 }
2149 } else if (i == 1) {
2150 i = 0;
2151 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2152 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2153 }
2154
David Benjamin2755a3e2015-04-22 16:17:58 -04002155 X509_free(x509);
2156 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002157 if (i && !ssl3_has_client_certificate(s)) {
2158 i = 0;
2159 }
2160 if (i == 0) {
2161 if (s->version == SSL3_VERSION) {
2162 s->s3->tmp.cert_req = 0;
2163 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2164 return 1;
2165 } else {
2166 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002167 /* There is no client certificate, so the handshake buffer may be
2168 * released. */
2169 if (s->s3->handshake_buffer &&
2170 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2171 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2172 return -1;
2173 }
Adam Langley1bea1732014-12-17 19:06:57 -08002174 }
2175 }
2176
2177 /* Ok, we have a cert */
2178 s->state = SSL3_ST_CW_CERT_C;
2179 }
2180
2181 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002182 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2183 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2184 return -1;
2185 }
Adam Langley1bea1732014-12-17 19:06:57 -08002186 }
2187
2188 /* SSL3_ST_CW_CERT_D */
2189 return ssl_do_write(s);
2190}
2191
Adam Langley1bea1732014-12-17 19:06:57 -08002192int ssl3_send_next_proto(SSL *s) {
2193 unsigned int len, padding_len;
2194 uint8_t *d, *p;
2195
2196 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2197 len = s->next_proto_negotiated_len;
2198 padding_len = 32 - ((len + 2) % 32);
2199
2200 d = p = ssl_handshake_start(s);
2201 *(p++) = len;
2202 memcpy(p, s->next_proto_negotiated, len);
2203 p += len;
2204 *(p++) = padding_len;
2205 memset(p, 0, padding_len);
2206 p += padding_len;
2207
David Benjaminfbdfefb2015-02-16 19:33:53 -05002208 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2209 return -1;
2210 }
Adam Langley1bea1732014-12-17 19:06:57 -08002211 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2212 }
2213
2214 return ssl_do_write(s);
2215}
2216
2217int ssl3_send_channel_id(SSL *s) {
2218 uint8_t *d;
2219 int ret = -1, public_key_len;
2220 EVP_MD_CTX md_ctx;
2221 size_t sig_len;
2222 ECDSA_SIG *sig = NULL;
2223 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2224
2225 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2226 return ssl_do_write(s);
2227 }
2228
2229 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2230 EVP_PKEY *key = NULL;
2231 s->ctx->channel_id_cb(s, &key);
2232 if (key != NULL) {
2233 s->tlsext_channel_id_private = key;
2234 }
2235 }
2236
2237 if (!s->tlsext_channel_id_private) {
2238 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2239 return -1;
2240 }
2241 s->rwstate = SSL_NOTHING;
2242
2243 d = ssl_handshake_start(s);
2244 if (s->s3->tlsext_channel_id_new) {
2245 s2n(TLSEXT_TYPE_channel_id_new, d);
2246 } else {
2247 s2n(TLSEXT_TYPE_channel_id, d);
2248 }
2249 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2250
2251 EVP_MD_CTX_init(&md_ctx);
2252
2253 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2254 if (public_key_len <= 0) {
2255 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2256 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2257 goto err;
2258 }
2259
2260 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2261 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2262 * field elements as 32-byte, big-endian numbers. */
2263 if (public_key_len != 65) {
2264 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2265 goto err;
2266 }
2267 public_key = OPENSSL_malloc(public_key_len);
2268 if (!public_key) {
2269 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2270 goto err;
2271 }
2272
2273 derp = public_key;
2274 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2275
2276 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2277 s->tlsext_channel_id_private) != 1) {
2278 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2279 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2280 goto err;
2281 }
2282
2283 if (!tls1_channel_id_hash(&md_ctx, s)) {
2284 goto err;
2285 }
2286
2287 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2288 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2289 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2290 goto err;
2291 }
2292
2293 der_sig = OPENSSL_malloc(sig_len);
2294 if (!der_sig) {
2295 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2296 goto err;
2297 }
2298
2299 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2300 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2301 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2302 goto err;
2303 }
2304
2305 derp = der_sig;
2306 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2307 if (sig == NULL) {
2308 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2309 goto err;
2310 }
2311
2312 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2313 memcpy(d, public_key + 1, 64);
2314 d += 64;
2315 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2316 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2317 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2318 goto err;
2319 }
2320
David Benjaminfbdfefb2015-02-16 19:33:53 -05002321 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2322 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2323 goto err;
2324 }
Adam Langley1bea1732014-12-17 19:06:57 -08002325 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2326
2327 ret = ssl_do_write(s);
2328
2329err:
2330 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002331 OPENSSL_free(public_key);
2332 OPENSSL_free(der_sig);
2333 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002334
2335 return ret;
2336}
2337
2338int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2339 int i = 0;
2340 if (s->ctx->client_cert_cb) {
2341 i = s->ctx->client_cert_cb(s, px509, ppkey);
2342 }
2343 return i;
2344}