blob: e1b8eb071040a0bb08ca0229a849961317f34cfc [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>
166
David Benjamin2ee94aa2015-04-07 22:38:30 -0400167#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700168#include "../crypto/dh/internal.h"
169
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langley1bea1732014-12-17 19:06:57 -0800171int ssl3_connect(SSL *s) {
172 BUF_MEM *buf = NULL;
173 void (*cb)(const SSL *ssl, int type, int val) = NULL;
174 int ret = -1;
175 int new_state, state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500176
Adam Langley1bea1732014-12-17 19:06:57 -0800177 assert(s->handshake_func == ssl3_connect);
178 assert(!s->server);
179 assert(!SSL_IS_DTLS(s));
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langley1bea1732014-12-17 19:06:57 -0800181 ERR_clear_error();
182 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley1bea1732014-12-17 19:06:57 -0800184 if (s->info_callback != NULL) {
185 cb = s->info_callback;
186 } else if (s->ctx->info_callback != NULL) {
187 cb = s->ctx->info_callback;
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley1bea1732014-12-17 19:06:57 -0800190 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langley1bea1732014-12-17 19:06:57 -0800192 for (;;) {
193 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley1bea1732014-12-17 19:06:57 -0800195 switch (s->state) {
Adam Langley1bea1732014-12-17 19:06:57 -0800196 case SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500197 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800198 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langley1bea1732014-12-17 19:06:57 -0800201 if (s->init_buf == NULL) {
202 buf = BUF_MEM_new();
203 if (buf == NULL ||
204 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
205 ret = -1;
206 goto end;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley1bea1732014-12-17 19:06:57 -0800209 s->init_buf = buf;
210 buf = NULL;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin6a08da22015-05-08 22:58:12 -0400213 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800214 ret = -1;
215 goto end;
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley1bea1732014-12-17 19:06:57 -0800218 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langley1bea1732014-12-17 19:06:57 -0800220 if (!ssl3_init_finished_mac(s)) {
221 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
222 ret = -1;
223 goto end;
224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langley1bea1732014-12-17 19:06:57 -0800226 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800227 s->init_num = 0;
228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langley1bea1732014-12-17 19:06:57 -0800230 case SSL3_ST_CW_CLNT_HELLO_A:
231 case SSL3_ST_CW_CLNT_HELLO_B:
232 s->shutdown = 0;
233 ret = ssl3_send_client_hello(s);
234 if (ret <= 0) {
235 goto end;
236 }
237 s->state = SSL3_ST_CR_SRVR_HELLO_A;
238 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley1bea1732014-12-17 19:06:57 -0800240 /* turn on buffering for the next lot of output */
241 if (s->bbio != s->wbio) {
242 s->wbio = BIO_push(s->bbio, s->wbio);
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley1bea1732014-12-17 19:06:57 -0800245 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley1bea1732014-12-17 19:06:57 -0800247 case SSL3_ST_CR_SRVR_HELLO_A:
248 case SSL3_ST_CR_SRVR_HELLO_B:
249 ret = ssl3_get_server_hello(s);
250 if (ret <= 0) {
251 goto end;
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley1bea1732014-12-17 19:06:57 -0800254 if (s->hit) {
255 s->state = SSL3_ST_CR_CHANGE;
256 if (s->tlsext_ticket_expected) {
257 /* receive renewed session ticket */
258 s->state = SSL3_ST_CR_SESSION_TICKET_A;
259 }
260 } else {
261 s->state = SSL3_ST_CR_CERT_A;
262 }
263 s->init_num = 0;
264 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400265
Adam Langley1bea1732014-12-17 19:06:57 -0800266 case SSL3_ST_CR_CERT_A:
267 case SSL3_ST_CR_CERT_B:
268 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
269 ret = ssl3_get_server_certificate(s);
270 if (ret <= 0) {
271 goto end;
272 }
273 if (s->s3->tmp.certificate_status_expected) {
274 s->state = SSL3_ST_CR_CERT_STATUS_A;
275 } else {
276 s->state = SSL3_ST_CR_KEY_EXCH_A;
277 }
278 } else {
279 skip = 1;
280 s->state = SSL3_ST_CR_KEY_EXCH_A;
281 }
282 s->init_num = 0;
283 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langley1bea1732014-12-17 19:06:57 -0800285 case SSL3_ST_CR_KEY_EXCH_A:
286 case SSL3_ST_CR_KEY_EXCH_B:
287 ret = ssl3_get_server_key_exchange(s);
288 if (ret <= 0) {
289 goto end;
290 }
291 s->state = SSL3_ST_CR_CERT_REQ_A;
292 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langley1bea1732014-12-17 19:06:57 -0800294 /* at this point we check that we have the
295 * required stuff from the server */
296 if (!ssl3_check_cert_and_algorithm(s)) {
297 ret = -1;
298 goto end;
299 }
300 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langley1bea1732014-12-17 19:06:57 -0800302 case SSL3_ST_CR_CERT_REQ_A:
303 case SSL3_ST_CR_CERT_REQ_B:
304 ret = ssl3_get_certificate_request(s);
305 if (ret <= 0) {
306 goto end;
307 }
308 s->state = SSL3_ST_CR_SRVR_DONE_A;
309 s->init_num = 0;
310 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley1bea1732014-12-17 19:06:57 -0800312 case SSL3_ST_CR_SRVR_DONE_A:
313 case SSL3_ST_CR_SRVR_DONE_B:
314 ret = ssl3_get_server_done(s);
315 if (ret <= 0) {
316 goto end;
317 }
318 if (s->s3->tmp.cert_req) {
319 s->state = SSL3_ST_CW_CERT_A;
320 } else {
321 s->state = SSL3_ST_CW_KEY_EXCH_A;
322 }
323 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langley1bea1732014-12-17 19:06:57 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CW_CERT_A:
328 case SSL3_ST_CW_CERT_B:
329 case SSL3_ST_CW_CERT_C:
330 case SSL3_ST_CW_CERT_D:
331 ret = ssl3_send_client_certificate(s);
332 if (ret <= 0) {
333 goto end;
334 }
335 s->state = SSL3_ST_CW_KEY_EXCH_A;
336 s->init_num = 0;
337 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langley1bea1732014-12-17 19:06:57 -0800339 case SSL3_ST_CW_KEY_EXCH_A:
340 case SSL3_ST_CW_KEY_EXCH_B:
341 ret = ssl3_send_client_key_exchange(s);
342 if (ret <= 0) {
343 goto end;
344 }
345 /* For TLS, cert_req is set to 2, so a cert chain
346 * of nothing is sent, but no verify packet is sent */
347 if (s->s3->tmp.cert_req == 1) {
348 s->state = SSL3_ST_CW_CERT_VRFY_A;
349 } else {
350 s->state = SSL3_ST_CW_CHANGE_A;
351 s->s3->change_cipher_spec = 0;
352 }
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley1bea1732014-12-17 19:06:57 -0800354 s->init_num = 0;
355 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langley1bea1732014-12-17 19:06:57 -0800357 case SSL3_ST_CW_CERT_VRFY_A:
358 case SSL3_ST_CW_CERT_VRFY_B:
359 ret = ssl3_send_cert_verify(s);
360 if (ret <= 0) {
361 goto end;
362 }
363 s->state = SSL3_ST_CW_CHANGE_A;
364 s->init_num = 0;
365 s->s3->change_cipher_spec = 0;
366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley1bea1732014-12-17 19:06:57 -0800368 case SSL3_ST_CW_CHANGE_A:
369 case SSL3_ST_CW_CHANGE_B:
370 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
371 SSL3_ST_CW_CHANGE_B);
372 if (ret <= 0) {
373 goto end;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 s->state = SSL3_ST_CW_FINISHED_A;
377 if (s->s3->tlsext_channel_id_valid) {
378 s->state = SSL3_ST_CW_CHANNEL_ID_A;
379 }
380 if (s->s3->next_proto_neg_seen) {
381 s->state = SSL3_ST_CW_NEXT_PROTO_A;
382 }
383 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Adam Langley1bea1732014-12-17 19:06:57 -0800385 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400386 if (!s->enc_method->setup_key_block(s) ||
387 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800388 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
389 ret = -1;
390 goto end;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langley1bea1732014-12-17 19:06:57 -0800393 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley1bea1732014-12-17 19:06:57 -0800395 case SSL3_ST_CW_NEXT_PROTO_A:
396 case SSL3_ST_CW_NEXT_PROTO_B:
397 ret = ssl3_send_next_proto(s);
398 if (ret <= 0) {
399 goto end;
400 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700401
Adam Langley1bea1732014-12-17 19:06:57 -0800402 if (s->s3->tlsext_channel_id_valid) {
403 s->state = SSL3_ST_CW_CHANNEL_ID_A;
404 } else {
405 s->state = SSL3_ST_CW_FINISHED_A;
406 }
407 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley1bea1732014-12-17 19:06:57 -0800409 case SSL3_ST_CW_CHANNEL_ID_A:
410 case SSL3_ST_CW_CHANNEL_ID_B:
411 ret = ssl3_send_channel_id(s);
412 if (ret <= 0) {
413 goto end;
414 }
415 s->state = SSL3_ST_CW_FINISHED_A;
416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley1bea1732014-12-17 19:06:57 -0800418 case SSL3_ST_CW_FINISHED_A:
419 case SSL3_ST_CW_FINISHED_B:
420 ret =
421 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
422 s->enc_method->client_finished_label,
423 s->enc_method->client_finished_label_len);
424 if (ret <= 0) {
425 goto end;
426 }
427 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley1bea1732014-12-17 19:06:57 -0800429 if (s->hit) {
430 s->s3->tmp.next_state = SSL_ST_OK;
431 } else {
432 /* This is a non-resumption handshake. If it involves ChannelID, then
433 * record the handshake hashes at this point in the session so that
434 * any resumption of this session with ChannelID can sign those
435 * hashes. */
436 if (s->s3->tlsext_channel_id_new) {
437 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500438 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800439 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500440 }
Adam Langley1bea1732014-12-17 19:06:57 -0800441 }
David Benjamined7c4752015-02-16 19:16:46 -0500442 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
443 ssl3_can_false_start(s) &&
444 /* No False Start on renegotiation (would complicate the state
445 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400446 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500447 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800448 } else {
449 /* Allow NewSessionTicket if ticket expected */
450 if (s->tlsext_ticket_expected) {
451 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
452 } else {
453 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
454 }
455 }
456 }
457 s->init_num = 0;
458 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700459
Adam Langley1bea1732014-12-17 19:06:57 -0800460 case SSL3_ST_CR_SESSION_TICKET_A:
461 case SSL3_ST_CR_SESSION_TICKET_B:
462 ret = ssl3_get_new_session_ticket(s);
463 if (ret <= 0) {
464 goto end;
465 }
466 s->state = SSL3_ST_CR_CHANGE;
467 s->init_num = 0;
468 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langley1bea1732014-12-17 19:06:57 -0800470 case SSL3_ST_CR_CERT_STATUS_A:
471 case SSL3_ST_CR_CERT_STATUS_B:
472 ret = ssl3_get_cert_status(s);
473 if (ret <= 0) {
474 goto end;
475 }
476 s->state = SSL3_ST_CR_KEY_EXCH_A;
477 s->init_num = 0;
478 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400479
Adam Langley1bea1732014-12-17 19:06:57 -0800480 case SSL3_ST_CR_CHANGE:
481 /* At this point, the next message must be entirely behind a
482 * ChangeCipherSpec. */
483 if (!ssl3_expect_change_cipher_spec(s)) {
484 ret = -1;
485 goto end;
486 }
487 s->state = SSL3_ST_CR_FINISHED_A;
488 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langley1bea1732014-12-17 19:06:57 -0800490 case SSL3_ST_CR_FINISHED_A:
491 case SSL3_ST_CR_FINISHED_B:
492 ret =
493 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
494 if (ret <= 0) {
495 goto end;
496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langley1bea1732014-12-17 19:06:57 -0800498 if (s->hit) {
499 s->state = SSL3_ST_CW_CHANGE_A;
500 } else {
501 s->state = SSL_ST_OK;
502 }
503 s->init_num = 0;
504 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langley1bea1732014-12-17 19:06:57 -0800506 case SSL3_ST_CW_FLUSH:
507 s->rwstate = SSL_WRITING;
508 if (BIO_flush(s->wbio) <= 0) {
509 ret = -1;
510 goto end;
511 }
512 s->rwstate = SSL_NOTHING;
513 s->state = s->s3->tmp.next_state;
514 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700515
David Benjamined7c4752015-02-16 19:16:46 -0500516 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800517 /* Allow NewSessionTicket if ticket expected */
518 if (s->tlsext_ticket_expected) {
519 s->state = SSL3_ST_CR_SESSION_TICKET_A;
520 } else {
521 s->state = SSL3_ST_CR_CHANGE;
522 }
David Benjamined7c4752015-02-16 19:16:46 -0500523 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700524
Adam Langley1bea1732014-12-17 19:06:57 -0800525 ssl_free_wbio_buffer(s);
526 ret = 1;
527 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley1bea1732014-12-17 19:06:57 -0800529 case SSL_ST_OK:
530 /* clean a few things up */
531 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin2755a3e2015-04-22 16:17:58 -0400533 BUF_MEM_free(s->init_buf);
534 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley1bea1732014-12-17 19:06:57 -0800536 /* Remove write buffering now. */
537 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langley1bea1732014-12-17 19:06:57 -0800539 s->init_num = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500540 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400541 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley1bea1732014-12-17 19:06:57 -0800543 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
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
Adam Langley1bea1732014-12-17 19:06:57 -0800919int ssl3_get_server_certificate(SSL *s) {
920 int al, i, ok, ret = -1;
921 unsigned long n;
922 X509 *x = NULL;
923 STACK_OF(X509) *sk = NULL;
924 SESS_CERT *sc;
925 EVP_PKEY *pkey = NULL;
926 CBS cbs, certificate_list;
927 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langley1bea1732014-12-17 19:06:57 -0800929 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
David Benjamin1d0a1942015-04-26 15:35:35 -0400930 SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500931 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langley1bea1732014-12-17 19:06:57 -0800933 if (!ok) {
934 return n;
935 }
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langley1bea1732014-12-17 19:06:57 -0800937 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langley1bea1732014-12-17 19:06:57 -0800939 sk = sk_X509_new_null();
940 if (sk == NULL) {
941 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
942 goto err;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langley1bea1732014-12-17 19:06:57 -0800945 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
946 CBS_len(&cbs) != 0) {
947 al = SSL_AD_DECODE_ERROR;
948 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
949 goto f_err;
950 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400951
Adam Langley1bea1732014-12-17 19:06:57 -0800952 while (CBS_len(&certificate_list) > 0) {
953 CBS certificate;
954 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
955 al = SSL_AD_DECODE_ERROR;
956 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
957 SSL_R_CERT_LENGTH_MISMATCH);
958 goto f_err;
959 }
960 data = CBS_data(&certificate);
961 x = d2i_X509(NULL, &data, CBS_len(&certificate));
962 if (x == NULL) {
963 al = SSL_AD_BAD_CERTIFICATE;
964 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
965 goto f_err;
966 }
967 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
968 al = SSL_AD_DECODE_ERROR;
969 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
970 SSL_R_CERT_LENGTH_MISMATCH);
971 goto f_err;
972 }
973 if (!sk_X509_push(sk, x)) {
974 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
975 goto err;
976 }
977 x = NULL;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langley1bea1732014-12-17 19:06:57 -0800980 i = ssl_verify_cert_chain(s, sk);
981 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
982 al = ssl_verify_alarm_type(s->verify_result);
983 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
984 SSL_R_CERTIFICATE_VERIFY_FAILED);
985 goto f_err;
986 }
987 ERR_clear_error(); /* but we keep s->verify_result */
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langley1bea1732014-12-17 19:06:57 -0800989 sc = ssl_sess_cert_new();
990 if (sc == NULL) {
991 goto err;
992 }
Adam Langley95c29f32014-06-20 12:00:00 -0700993
David Benjamin2755a3e2015-04-22 16:17:58 -0400994 ssl_sess_cert_free(s->session->sess_cert);
Adam Langley1bea1732014-12-17 19:06:57 -0800995 s->session->sess_cert = sc;
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langley1bea1732014-12-17 19:06:57 -0800997 sc->cert_chain = sk;
998 /* Inconsistency alert: cert_chain does include the peer's certificate, which
999 * we don't include in s3_srvr.c */
1000 x = sk_X509_value(sk, 0);
1001 sk = NULL;
1002 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langley1bea1732014-12-17 19:06:57 -08001004 pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langley1bea1732014-12-17 19:06:57 -08001006 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1007 x = NULL;
1008 al = SSL3_AL_FATAL;
1009 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1010 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1011 goto f_err;
1012 }
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Adam Langley1bea1732014-12-17 19:06:57 -08001014 i = ssl_cert_type(pkey);
1015 if (i < 0) {
1016 x = NULL;
1017 al = SSL3_AL_FATAL;
1018 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1019 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1020 goto f_err;
1021 }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langley1bea1732014-12-17 19:06:57 -08001023 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1024 if (exp_idx >= 0 && i != exp_idx) {
1025 x = NULL;
1026 al = SSL_AD_ILLEGAL_PARAMETER;
1027 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
1028 SSL_R_WRONG_CERTIFICATE_TYPE);
1029 goto f_err;
1030 }
David Benjaminb31040d2015-06-07 10:53:32 -04001031 X509_free(sc->peer_cert);
1032 sc->peer_cert = X509_up_ref(x);
Adam Langley95c29f32014-06-20 12:00:00 -07001033
David Benjamin2755a3e2015-04-22 16:17:58 -04001034 X509_free(s->session->peer);
Adam Langley1bea1732014-12-17 19:06:57 -08001035 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001036
Adam Langley1bea1732014-12-17 19:06:57 -08001037 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 x = NULL;
1040 ret = 1;
1041
1042 if (0) {
1043 f_err:
1044 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1045 }
1046
1047err:
1048 EVP_PKEY_free(pkey);
1049 X509_free(x);
1050 sk_X509_pop_free(sk, X509_free);
1051 return ret;
1052}
1053
1054int ssl3_get_server_key_exchange(SSL *s) {
1055 EVP_MD_CTX md_ctx;
1056 int al, ok;
1057 long n, alg_k, alg_a;
1058 EVP_PKEY *pkey = NULL;
1059 const EVP_MD *md = NULL;
1060 RSA *rsa = NULL;
1061 DH *dh = NULL;
1062 EC_KEY *ecdh = NULL;
1063 BN_CTX *bn_ctx = NULL;
1064 EC_POINT *srvr_ecpoint = NULL;
1065 CBS server_key_exchange, server_key_exchange_orig, parameter;
1066
1067 /* use same message size as in ssl3_get_certificate_request() as
1068 * ServerKeyExchange message may be skipped */
1069 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1070 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001071 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001072 if (!ok) {
1073 return n;
1074 }
1075
1076 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1077 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1078 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1079 SSL_R_UNEXPECTED_MESSAGE);
1080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1081 return -1;
1082 }
1083
1084 /* In plain PSK ciphersuite, ServerKeyExchange can be
1085 omitted if no identity hint is sent. Set session->sess_cert anyway to
1086 avoid problems later.*/
1087 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1088 /* PSK ciphersuites that also send a Certificate would have already
1089 * initialized |sess_cert|. */
1090 if (s->session->sess_cert == NULL) {
1091 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001092 if (s->session->sess_cert == NULL) {
1093 return -1;
1094 }
Adam Langley1bea1732014-12-17 19:06:57 -08001095 }
1096
1097 /* TODO(davidben): This should be reset in one place with the rest of the
1098 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001099 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1100 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001101 }
1102 s->s3->tmp.reuse_message = 1;
1103 return 1;
1104 }
1105
1106 /* Retain a copy of the original CBS to compute the signature over. */
1107 CBS_init(&server_key_exchange, s->init_msg, n);
1108 server_key_exchange_orig = server_key_exchange;
1109
1110 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001111 DH_free(s->session->sess_cert->peer_dh_tmp);
1112 s->session->sess_cert->peer_dh_tmp = NULL;
1113 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1114 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001115 } else {
1116 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001117 if (s->session->sess_cert == NULL) {
1118 return -1;
1119 }
Adam Langley1bea1732014-12-17 19:06:57 -08001120 }
1121
1122 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1123 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1124 EVP_MD_CTX_init(&md_ctx);
1125
1126 if (alg_a & SSL_aPSK) {
1127 CBS psk_identity_hint;
1128
1129 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1130 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1131 &psk_identity_hint)) {
1132 al = SSL_AD_DECODE_ERROR;
1133 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1134 goto f_err;
1135 }
1136
1137 /* Store PSK identity hint for later use, hint is used in
1138 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1139 * identity hint can be as long as the maximum length of a PSK identity.
1140 * Also do not allow NULL characters; identities are saved as C strings.
1141 *
1142 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1143 * a specific identity. */
1144 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1145 CBS_contains_zero_byte(&psk_identity_hint)) {
1146 al = SSL_AD_HANDSHAKE_FAILURE;
1147 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1148 SSL_R_DATA_LENGTH_TOO_LONG);
1149 goto f_err;
1150 }
1151
1152 /* Save the identity hint as a C string. */
1153 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1154 al = SSL_AD_INTERNAL_ERROR;
1155 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1156 ERR_R_MALLOC_FAILURE);
1157 goto f_err;
1158 }
1159 }
1160
David Benjamin7061e282015-03-19 11:10:48 -04001161 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001162 CBS dh_p, dh_g, dh_Ys;
1163
1164 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1165 CBS_len(&dh_p) == 0 ||
1166 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1167 CBS_len(&dh_g) == 0 ||
1168 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1169 CBS_len(&dh_Ys) == 0) {
1170 al = SSL_AD_DECODE_ERROR;
1171 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1172 goto f_err;
1173 }
1174
1175 dh = DH_new();
1176 if (dh == NULL) {
1177 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1178 goto err;
1179 }
1180
1181 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1182 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1183 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1184 NULL) {
1185 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1186 goto err;
1187 }
1188
Adam Langleya7997f12015-05-14 17:38:50 -07001189 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001190 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1191 SSL_R_BAD_DH_P_LENGTH);
1192 goto err;
1193 }
Adam Langley1bea1732014-12-17 19:06:57 -08001194 s->session->sess_cert->peer_dh_tmp = dh;
1195 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001196 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001197 uint16_t curve_id;
1198 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001199 const EC_GROUP *group;
1200 CBS point;
1201
1202 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1203 * key. Check curve is one of our preferences, if not server has sent an
1204 * invalid curve. */
1205 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1206 al = SSL_AD_DECODE_ERROR;
1207 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1208 goto f_err;
1209 }
1210
1211 curve_nid = tls1_ec_curve_id2nid(curve_id);
1212 if (curve_nid == 0) {
1213 al = SSL_AD_INTERNAL_ERROR;
1214 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1215 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1216 goto f_err;
1217 }
1218
David Benjamin4f7783e2015-03-05 03:05:11 -05001219 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001220 if (ecdh == NULL) {
1221 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001222 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001223 goto err;
1224 }
1225
Adam Langley1bea1732014-12-17 19:06:57 -08001226 group = EC_KEY_get0_group(ecdh);
1227
1228 /* Next, get the encoded ECPoint */
1229 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1230 al = SSL_AD_DECODE_ERROR;
1231 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1232 goto f_err;
1233 }
1234
1235 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1236 ((bn_ctx = BN_CTX_new()) == NULL)) {
1237 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1238 ERR_R_MALLOC_FAILURE);
1239 goto err;
1240 }
1241
1242 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1243 CBS_len(&point), bn_ctx)) {
1244 al = SSL_AD_DECODE_ERROR;
1245 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1246 goto f_err;
1247 }
Adam Langley1bea1732014-12-17 19:06:57 -08001248 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1249 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1250 ecdh = NULL;
1251 BN_CTX_free(bn_ctx);
1252 bn_ctx = NULL;
1253 EC_POINT_free(srvr_ecpoint);
1254 srvr_ecpoint = NULL;
1255 } else if (!(alg_k & SSL_kPSK)) {
1256 al = SSL_AD_UNEXPECTED_MESSAGE;
1257 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1258 SSL_R_UNEXPECTED_MESSAGE);
1259 goto f_err;
1260 }
1261
1262 /* At this point, |server_key_exchange| contains the signature, if any, while
1263 * |server_key_exchange_orig| contains the entire message. From that, derive
1264 * a CBS containing just the parameter. */
1265 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1266 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1267
David Benjaminf4958e72015-06-07 11:00:53 -04001268 /* ServerKeyExchange should be signed by the server's public key. */
1269 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1270 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
1271 if (pkey == NULL) {
1272 goto err;
1273 }
Adam Langley1bea1732014-12-17 19:06:57 -08001274
1275 if (SSL_USE_SIGALGS(s)) {
1276 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1277 goto f_err;
1278 }
1279 } else if (pkey->type == EVP_PKEY_RSA) {
1280 md = EVP_md5_sha1();
1281 } else {
1282 md = EVP_sha1();
1283 }
1284
1285 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001286 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001287 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1288 CBS_len(&server_key_exchange) != 0) {
1289 al = SSL_AD_DECODE_ERROR;
1290 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1291 goto f_err;
1292 }
1293
1294 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1295 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1296 SSL3_RANDOM_SIZE) ||
1297 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1298 SSL3_RANDOM_SIZE) ||
1299 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1300 CBS_len(&parameter)) ||
1301 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1302 CBS_len(&signature))) {
1303 /* bad signature */
1304 al = SSL_AD_DECRYPT_ERROR;
1305 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1306 goto f_err;
1307 }
1308 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001309 /* PSK ciphers are the only supported certificate-less ciphers. */
1310 assert(alg_a == SSL_aPSK);
1311
Adam Langley1bea1732014-12-17 19:06:57 -08001312 if (CBS_len(&server_key_exchange) > 0) {
1313 al = SSL_AD_DECODE_ERROR;
1314 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1315 SSL_R_EXTRA_DATA_IN_MESSAGE);
1316 goto f_err;
1317 }
1318 }
1319 EVP_PKEY_free(pkey);
1320 EVP_MD_CTX_cleanup(&md_ctx);
1321 return 1;
1322
Adam Langley95c29f32014-06-20 12:00:00 -07001323f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001324 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001325err:
Adam Langley1bea1732014-12-17 19:06:57 -08001326 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001327 RSA_free(rsa);
1328 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001329 BN_CTX_free(bn_ctx);
1330 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001331 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001332 EVP_MD_CTX_cleanup(&md_ctx);
1333 return -1;
1334}
Adam Langley95c29f32014-06-20 12:00:00 -07001335
Adam Langley1bea1732014-12-17 19:06:57 -08001336static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1337 return X509_NAME_cmp(*a, *b);
1338}
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langley1bea1732014-12-17 19:06:57 -08001340int ssl3_get_certificate_request(SSL *s) {
1341 int ok, ret = 0;
1342 unsigned long n;
1343 X509_NAME *xn = NULL;
1344 STACK_OF(X509_NAME) *ca_sk = NULL;
1345 CBS cbs;
1346 CBS certificate_types;
1347 CBS certificate_authorities;
1348 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001349
Adam Langley1bea1732014-12-17 19:06:57 -08001350 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1351 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001352 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001353
Adam Langley1bea1732014-12-17 19:06:57 -08001354 if (!ok) {
1355 return n;
1356 }
David Benjamin688d8df2014-11-02 23:06:42 -05001357
Adam Langley1bea1732014-12-17 19:06:57 -08001358 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langley1bea1732014-12-17 19:06:57 -08001360 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1361 s->s3->tmp.reuse_message = 1;
1362 /* If we get here we don't need any cached handshake records as we wont be
1363 * doing client auth. */
1364 if (s->s3->handshake_buffer &&
1365 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1366 goto err;
1367 }
1368 return 1;
1369 }
David Benjamined439582014-07-14 19:13:02 -04001370
Adam Langley1bea1732014-12-17 19:06:57 -08001371 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1372 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1373 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1374 SSL_R_WRONG_MESSAGE_TYPE);
1375 goto err;
1376 }
Adam Langley95c29f32014-06-20 12:00:00 -07001377
Adam Langley1bea1732014-12-17 19:06:57 -08001378 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langley1bea1732014-12-17 19:06:57 -08001380 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1381 if (ca_sk == NULL) {
1382 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1383 goto err;
1384 }
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langley1bea1732014-12-17 19:06:57 -08001386 /* get the certificate types */
1387 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1388 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1389 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1390 goto err;
1391 }
David Benjamined439582014-07-14 19:13:02 -04001392
Adam Langley1bea1732014-12-17 19:06:57 -08001393 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1394 &s->s3->tmp.num_certificate_types)) {
1395 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1396 goto err;
1397 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001398
Adam Langley1bea1732014-12-17 19:06:57 -08001399 if (SSL_USE_SIGALGS(s)) {
1400 CBS supported_signature_algorithms;
1401 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1402 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1403 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1404 goto err;
1405 }
David Benjamined439582014-07-14 19:13:02 -04001406
Adam Langley1bea1732014-12-17 19:06:57 -08001407 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1408 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1409 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1410 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1411 goto err;
1412 }
1413 }
David Benjamined439582014-07-14 19:13:02 -04001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 /* get the CA RDNs */
1416 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1417 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1418 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1419 goto err;
1420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
Adam Langley1bea1732014-12-17 19:06:57 -08001422 while (CBS_len(&certificate_authorities) > 0) {
1423 CBS distinguished_name;
1424 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1425 &distinguished_name)) {
1426 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1427 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1428 SSL_R_CA_DN_TOO_LONG);
1429 goto err;
1430 }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
Adam Langley1bea1732014-12-17 19:06:57 -08001432 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001433
Adam Langley1bea1732014-12-17 19:06:57 -08001434 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1435 if (xn == NULL) {
1436 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1437 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1438 goto err;
1439 }
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langley1bea1732014-12-17 19:06:57 -08001441 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1442 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001443 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001444 goto err;
1445 }
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langley1bea1732014-12-17 19:06:57 -08001447 if (CBS_len(&distinguished_name) != 0) {
1448 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1449 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1450 SSL_R_CA_DN_LENGTH_MISMATCH);
1451 goto err;
1452 }
Adam Langley95c29f32014-06-20 12:00:00 -07001453
Adam Langley1bea1732014-12-17 19:06:57 -08001454 if (!sk_X509_NAME_push(ca_sk, xn)) {
1455 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1456 ERR_R_MALLOC_FAILURE);
1457 goto err;
1458 }
1459 }
Adam Langley95c29f32014-06-20 12:00:00 -07001460
Adam Langley1bea1732014-12-17 19:06:57 -08001461 /* we should setup a certificate to return.... */
1462 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001463 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001464 s->s3->tmp.ca_names = ca_sk;
1465 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langley1bea1732014-12-17 19:06:57 -08001467 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001468
Adam Langley1bea1732014-12-17 19:06:57 -08001469err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001470 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001471 return ret;
1472}
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langley1bea1732014-12-17 19:06:57 -08001474int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001475 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001476 long n;
1477 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001478
Adam Langley1bea1732014-12-17 19:06:57 -08001479 n = s->method->ssl_get_message(
1480 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001481 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001482
Adam Langley1bea1732014-12-17 19:06:57 -08001483 if (!ok) {
1484 return n;
1485 }
Adam Langley95c29f32014-06-20 12:00:00 -07001486
Adam Langley1bea1732014-12-17 19:06:57 -08001487 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001488
Adam Langley1bea1732014-12-17 19:06:57 -08001489 if (!CBS_get_u32(&new_session_ticket,
1490 &s->session->tlsext_tick_lifetime_hint) ||
1491 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1492 CBS_len(&new_session_ticket) != 0) {
1493 al = SSL_AD_DECODE_ERROR;
1494 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1495 goto f_err;
1496 }
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langley1bea1732014-12-17 19:06:57 -08001498 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1499 &s->session->tlsext_ticklen)) {
1500 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1501 goto err;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin68070622015-02-08 23:44:59 -05001504 /* Generate a session ID for this session based on the session ticket. We use
1505 * the session ID mechanism for detecting ticket resumption. This also fits in
1506 * with assumptions elsewhere in OpenSSL.*/
1507 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1508 &s->session->session_id_length, EVP_sha256(), NULL)) {
1509 goto err;
1510 }
1511
1512 return 1;
David Benjamined439582014-07-14 19:13:02 -04001513
Adam Langley95c29f32014-06-20 12:00:00 -07001514f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001515 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001516err:
Adam Langley1bea1732014-12-17 19:06:57 -08001517 return -1;
1518}
Adam Langley95c29f32014-06-20 12:00:00 -07001519
Adam Langley1bea1732014-12-17 19:06:57 -08001520int ssl3_get_cert_status(SSL *s) {
1521 int ok, al;
1522 long n;
1523 CBS certificate_status, ocsp_response;
1524 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001525
Adam Langley1bea1732014-12-17 19:06:57 -08001526 n = s->method->ssl_get_message(
1527 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001528 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langley1bea1732014-12-17 19:06:57 -08001530 if (!ok) {
1531 return n;
1532 }
Adam Langley95c29f32014-06-20 12:00:00 -07001533
David Benjamindcd979f2015-04-20 18:26:52 -04001534 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1535 /* A server may send status_request in ServerHello and then change
1536 * its mind about sending CertificateStatus. */
1537 s->s3->tmp.reuse_message = 1;
1538 return 1;
1539 }
1540
Adam Langley1bea1732014-12-17 19:06:57 -08001541 CBS_init(&certificate_status, s->init_msg, n);
1542 if (!CBS_get_u8(&certificate_status, &status_type) ||
1543 status_type != TLSEXT_STATUSTYPE_ocsp ||
1544 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1545 CBS_len(&ocsp_response) == 0 ||
1546 CBS_len(&certificate_status) != 0) {
1547 al = SSL_AD_DECODE_ERROR;
1548 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1549 goto f_err;
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langley1bea1732014-12-17 19:06:57 -08001552 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1553 &s->session->ocsp_response_length)) {
1554 al = SSL_AD_INTERNAL_ERROR;
1555 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1556 goto f_err;
1557 }
1558 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langley95c29f32014-06-20 12:00:00 -07001560f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001561 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1562 return -1;
1563}
Adam Langley95c29f32014-06-20 12:00:00 -07001564
Adam Langley1bea1732014-12-17 19:06:57 -08001565int ssl3_get_server_done(SSL *s) {
1566 int ok;
1567 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langley1bea1732014-12-17 19:06:57 -08001569 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1570 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1571 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001572 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langley1bea1732014-12-17 19:06:57 -08001574 if (!ok) {
1575 return n;
1576 }
David Benjamin46062682014-07-14 19:14:32 -04001577
Adam Langley1bea1732014-12-17 19:06:57 -08001578 if (n > 0) {
1579 /* should contain no data */
1580 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1581 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1582 return -1;
1583 }
David Benjamin46062682014-07-14 19:14:32 -04001584
Adam Langley1bea1732014-12-17 19:06:57 -08001585 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001586}
Adam Langley1258b6a2014-06-20 12:00:00 -07001587
Adam Langley1258b6a2014-06-20 12:00:00 -07001588
Adam Langley1bea1732014-12-17 19:06:57 -08001589int ssl3_send_client_key_exchange(SSL *s) {
1590 uint8_t *p;
1591 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001592 uint32_t alg_k;
1593 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001594 uint8_t *q;
1595 EVP_PKEY *pkey = NULL;
1596 EC_KEY *clnt_ecdh = NULL;
1597 const EC_POINT *srvr_ecpoint = NULL;
1598 EVP_PKEY *srvr_pub_pkey = NULL;
1599 uint8_t *encodedPoint = NULL;
1600 int encoded_pt_len = 0;
1601 BN_CTX *bn_ctx = NULL;
1602 unsigned int psk_len = 0;
1603 uint8_t psk[PSK_MAX_PSK_LEN];
1604 uint8_t *pms = NULL;
1605 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001606
Adam Langley1bea1732014-12-17 19:06:57 -08001607 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1608 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001609
Adam Langley1bea1732014-12-17 19:06:57 -08001610 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1611 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001612
Adam Langley1bea1732014-12-17 19:06:57 -08001613 /* If using a PSK key exchange, prepare the pre-shared key. */
1614 if (alg_a & SSL_aPSK) {
1615 char identity[PSK_MAX_IDENTITY_LEN + 1];
1616 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001617
Adam Langley1bea1732014-12-17 19:06:57 -08001618 if (s->psk_client_callback == NULL) {
1619 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1620 SSL_R_PSK_NO_CLIENT_CB);
1621 goto err;
1622 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001623
Adam Langley1bea1732014-12-17 19:06:57 -08001624 memset(identity, 0, sizeof(identity));
1625 psk_len =
1626 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1627 sizeof(identity), psk, sizeof(psk));
1628 if (psk_len > PSK_MAX_PSK_LEN) {
1629 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1630 ERR_R_INTERNAL_ERROR);
1631 goto err;
1632 } else if (psk_len == 0) {
1633 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1634 SSL_R_PSK_IDENTITY_NOT_FOUND);
1635 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1636 goto err;
1637 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001638
Adam Langley1bea1732014-12-17 19:06:57 -08001639 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1640 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1641 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1642 ERR_R_INTERNAL_ERROR);
1643 goto err;
1644 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001645
David Benjamin2755a3e2015-04-22 16:17:58 -04001646 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001647 s->session->psk_identity = BUF_strdup(identity);
1648 if (s->session->psk_identity == NULL) {
1649 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1650 ERR_R_MALLOC_FAILURE);
1651 goto err;
1652 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001653
Adam Langley1bea1732014-12-17 19:06:57 -08001654 /* Write out psk_identity. */
1655 s2n(identity_len, p);
1656 memcpy(p, identity, identity_len);
1657 p += identity_len;
1658 n = 2 + identity_len;
1659 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001660
Adam Langley1bea1732014-12-17 19:06:57 -08001661 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1662 if (alg_k & SSL_kRSA) {
1663 RSA *rsa;
1664 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001665
Adam Langley1bea1732014-12-17 19:06:57 -08001666 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1667 pms = OPENSSL_malloc(pms_len);
1668 if (pms == NULL) {
1669 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1670 ERR_R_MALLOC_FAILURE);
1671 goto err;
1672 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001673
Adam Langley1bea1732014-12-17 19:06:57 -08001674 if (s->session->sess_cert == NULL) {
1675 /* We should always have a server certificate with SSL_kRSA. */
1676 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1677 ERR_R_INTERNAL_ERROR);
1678 goto err;
1679 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001680
David Benjaminb31040d2015-06-07 10:53:32 -04001681 pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
Adam Langley1bea1732014-12-17 19:06:57 -08001682 if (pkey == NULL ||
1683 pkey->type != EVP_PKEY_RSA ||
1684 pkey->pkey.rsa == NULL) {
1685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1686 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001687 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001688 goto err;
1689 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001690
Adam Langley1bea1732014-12-17 19:06:57 -08001691 rsa = pkey->pkey.rsa;
1692 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001693
Adam Langley1bea1732014-12-17 19:06:57 -08001694 pms[0] = s->client_version >> 8;
1695 pms[1] = s->client_version & 0xff;
1696 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1697 goto err;
1698 }
1699
1700 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1701
1702 q = p;
1703 /* In TLS and beyond, reserve space for the length prefix. */
1704 if (s->version > SSL3_VERSION) {
1705 p += 2;
1706 n += 2;
1707 }
1708 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1709 RSA_PKCS1_PADDING)) {
1710 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1711 SSL_R_BAD_RSA_ENCRYPT);
1712 goto err;
1713 }
1714 n += enc_pms_len;
1715
1716 /* Log the premaster secret, if logging is enabled. */
1717 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1718 pms_len)) {
1719 goto err;
1720 }
1721
1722 /* Fill in the length prefix. */
1723 if (s->version > SSL3_VERSION) {
1724 s2n(enc_pms_len, q);
1725 }
David Benjamin7061e282015-03-19 11:10:48 -04001726 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001727 DH *dh_srvr, *dh_clnt;
1728 SESS_CERT *scert = s->session->sess_cert;
1729 int dh_len;
1730 size_t pub_len;
1731
1732 if (scert == NULL) {
1733 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1734 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1735 SSL_R_UNEXPECTED_MESSAGE);
1736 goto err;
1737 }
1738
1739 if (scert->peer_dh_tmp == NULL) {
1740 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1741 ERR_R_INTERNAL_ERROR);
1742 goto err;
1743 }
1744 dh_srvr = scert->peer_dh_tmp;
1745
1746 /* generate a new random key */
1747 dh_clnt = DHparams_dup(dh_srvr);
1748 if (dh_clnt == NULL) {
1749 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1750 goto err;
1751 }
1752 if (!DH_generate_key(dh_clnt)) {
1753 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1754 DH_free(dh_clnt);
1755 goto err;
1756 }
1757
1758 pms_len = DH_size(dh_clnt);
1759 pms = OPENSSL_malloc(pms_len);
1760 if (pms == NULL) {
1761 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1762 ERR_R_MALLOC_FAILURE);
1763 DH_free(dh_clnt);
1764 goto err;
1765 }
1766
1767 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1768 if (dh_len <= 0) {
1769 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1770 DH_free(dh_clnt);
1771 goto err;
1772 }
1773 pms_len = dh_len;
1774
1775 /* send off the data */
1776 pub_len = BN_num_bytes(dh_clnt->pub_key);
1777 s2n(pub_len, p);
1778 BN_bn2bin(dh_clnt->pub_key, p);
1779 n += 2 + pub_len;
1780
1781 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001782 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001783 const EC_GROUP *srvr_group = NULL;
1784 EC_KEY *tkey;
1785 int field_size = 0, ecdh_len;
1786
1787 if (s->session->sess_cert == NULL) {
1788 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1789 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1790 SSL_R_UNEXPECTED_MESSAGE);
1791 goto err;
1792 }
1793
1794 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1795 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1796 ERR_R_INTERNAL_ERROR);
1797 goto err;
1798 }
1799
1800 tkey = s->session->sess_cert->peer_ecdh_tmp;
1801
1802 srvr_group = EC_KEY_get0_group(tkey);
1803 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1804 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1805 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1806 ERR_R_INTERNAL_ERROR);
1807 goto err;
1808 }
1809
1810 clnt_ecdh = EC_KEY_new();
1811 if (clnt_ecdh == NULL) {
1812 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1813 ERR_R_MALLOC_FAILURE);
1814 goto err;
1815 }
1816
1817 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1818 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1819 goto err;
1820 }
1821
1822 /* Generate a new ECDH key pair */
1823 if (!EC_KEY_generate_key(clnt_ecdh)) {
1824 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1825 goto err;
1826 }
1827
1828 field_size = EC_GROUP_get_degree(srvr_group);
1829 if (field_size <= 0) {
1830 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1831 goto err;
1832 }
1833
1834 pms_len = (field_size + 7) / 8;
1835 pms = OPENSSL_malloc(pms_len);
1836 if (pms == NULL) {
1837 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1838 ERR_R_MALLOC_FAILURE);
1839 goto err;
1840 }
1841
1842 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1843 if (ecdh_len <= 0) {
1844 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1845 goto err;
1846 }
1847 pms_len = ecdh_len;
1848
1849 /* First check the size of encoding and allocate memory accordingly. */
1850 encoded_pt_len =
1851 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1852 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1853
1854 encodedPoint =
1855 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1856 bn_ctx = BN_CTX_new();
1857 if (encodedPoint == NULL || bn_ctx == NULL) {
1858 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1859 ERR_R_MALLOC_FAILURE);
1860 goto err;
1861 }
1862
1863 /* Encode the public key */
1864 encoded_pt_len = EC_POINT_point2oct(
1865 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1866 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1867
1868 *p = encoded_pt_len; /* length of encoded point */
1869 /* Encoded point will be copied here */
1870 p += 1;
1871 n += 1;
1872 /* copy the point */
1873 memcpy(p, encodedPoint, encoded_pt_len);
1874 /* increment n to account for length field */
1875 n += encoded_pt_len;
1876
1877 /* Free allocated memory */
1878 BN_CTX_free(bn_ctx);
1879 bn_ctx = NULL;
1880 OPENSSL_free(encodedPoint);
1881 encodedPoint = NULL;
1882 EC_KEY_free(clnt_ecdh);
1883 clnt_ecdh = NULL;
1884 EVP_PKEY_free(srvr_pub_pkey);
1885 srvr_pub_pkey = NULL;
1886 } else if (alg_k & SSL_kPSK) {
1887 /* For plain PSK, other_secret is a block of 0s with the same length as
1888 * the pre-shared key. */
1889 pms_len = psk_len;
1890 pms = OPENSSL_malloc(pms_len);
1891 if (pms == NULL) {
1892 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1893 ERR_R_MALLOC_FAILURE);
1894 goto err;
1895 }
1896 memset(pms, 0, pms_len);
1897 } else {
1898 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1899 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1900 ERR_R_INTERNAL_ERROR);
1901 goto err;
1902 }
1903
1904 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1905 * key. */
1906 if (alg_a & SSL_aPSK) {
1907 CBB cbb, child;
1908 uint8_t *new_pms;
1909 size_t new_pms_len;
1910
1911 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1912 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1913 ERR_R_MALLOC_FAILURE);
1914 goto err;
1915 }
1916 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1917 !CBB_add_bytes(&child, pms, pms_len) ||
1918 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1919 !CBB_add_bytes(&child, psk, psk_len) ||
1920 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1921 CBB_cleanup(&cbb);
1922 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1923 ERR_R_INTERNAL_ERROR);
1924 goto err;
1925 }
1926 OPENSSL_cleanse(pms, pms_len);
1927 OPENSSL_free(pms);
1928 pms = new_pms;
1929 pms_len = new_pms_len;
1930 }
1931
1932 /* The message must be added to the finished hash before calculating the
1933 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001934 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1935 goto err;
1936 }
Adam Langley1bea1732014-12-17 19:06:57 -08001937 s->state = SSL3_ST_CW_KEY_EXCH_B;
1938
1939 s->session->master_key_length = s->enc_method->generate_master_secret(
1940 s, s->session->master_key, pms, pms_len);
1941 if (s->session->master_key_length == 0) {
1942 goto err;
1943 }
1944 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1945 OPENSSL_cleanse(pms, pms_len);
1946 OPENSSL_free(pms);
1947 }
1948
1949 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001950 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001951
1952err:
Adam Langley1bea1732014-12-17 19:06:57 -08001953 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001954 OPENSSL_free(encodedPoint);
1955 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001956 EVP_PKEY_free(srvr_pub_pkey);
1957 if (pms) {
1958 OPENSSL_cleanse(pms, pms_len);
1959 OPENSSL_free(pms);
1960 }
1961 return -1;
1962}
Adam Langley1258b6a2014-06-20 12:00:00 -07001963
Adam Langley1bea1732014-12-17 19:06:57 -08001964int ssl3_send_cert_verify(SSL *s) {
1965 uint8_t *buf, *p;
1966 const EVP_MD *md = NULL;
1967 uint8_t digest[EVP_MAX_MD_SIZE];
1968 size_t digest_length;
1969 EVP_PKEY *pkey;
1970 EVP_PKEY_CTX *pctx = NULL;
1971 size_t signature_length = 0;
1972 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001973
Adam Langley1bea1732014-12-17 19:06:57 -08001974 buf = (uint8_t *)s->init_buf->data;
1975
1976 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1977 p = ssl_handshake_start(s);
1978 pkey = s->cert->key->privatekey;
1979
1980 /* Write out the digest type if needbe. */
1981 if (SSL_USE_SIGALGS(s)) {
1982 md = tls1_choose_signing_digest(s, pkey);
1983 if (!tls12_get_sigandhash(p, pkey, md)) {
1984 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
1985 goto err;
1986 }
1987 p += 2;
1988 n += 2;
1989 }
1990
1991 /* Compute the digest. */
1992 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
1993 goto err;
1994 }
1995
1996 /* The handshake buffer is no longer necessary. */
1997 if (s->s3->handshake_buffer &&
1998 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1999 goto err;
2000 }
2001
2002 /* Sign the digest. */
2003 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2004 if (pctx == NULL) {
2005 goto err;
2006 }
2007
2008 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2009 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2010 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2011 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2012 goto err;
2013 }
2014
2015 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2016 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2017 goto err;
2018 }
2019
2020 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2021 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2022 goto err;
2023 }
2024
2025 s2n(signature_length, p);
2026 n += signature_length + 2;
2027
David Benjaminfbdfefb2015-02-16 19:33:53 -05002028 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2029 goto err;
2030 }
Adam Langley1bea1732014-12-17 19:06:57 -08002031 s->state = SSL3_ST_CW_CERT_VRFY_B;
2032 }
2033
2034 EVP_PKEY_CTX_free(pctx);
2035 return ssl_do_write(s);
2036
2037err:
2038 EVP_PKEY_CTX_free(pctx);
2039 return -1;
2040}
2041
2042/* ssl3_has_client_certificate returns true if a client certificate is
2043 * configured. */
2044static int ssl3_has_client_certificate(SSL *s) {
2045 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2046}
2047
2048int ssl3_send_client_certificate(SSL *s) {
2049 X509 *x509 = NULL;
2050 EVP_PKEY *pkey = NULL;
2051 int i;
2052
2053 if (s->state == SSL3_ST_CW_CERT_A) {
2054 /* Let cert callback update client certificates if required */
2055 if (s->cert->cert_cb) {
2056 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2057 if (i < 0) {
2058 s->rwstate = SSL_X509_LOOKUP;
2059 return -1;
2060 }
2061 if (i == 0) {
2062 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2063 return 0;
2064 }
2065 s->rwstate = SSL_NOTHING;
2066 }
2067
2068 if (ssl3_has_client_certificate(s)) {
2069 s->state = SSL3_ST_CW_CERT_C;
2070 } else {
2071 s->state = SSL3_ST_CW_CERT_B;
2072 }
2073 }
2074
2075 /* We need to get a client cert */
2076 if (s->state == SSL3_ST_CW_CERT_B) {
2077 /* If we get an error, we need to:
2078 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2079 * We then get retried later */
2080 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2081 if (i < 0) {
2082 s->rwstate = SSL_X509_LOOKUP;
2083 return -1;
2084 }
2085 s->rwstate = SSL_NOTHING;
2086 if (i == 1 && pkey != NULL && x509 != NULL) {
2087 s->state = SSL3_ST_CW_CERT_B;
2088 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2089 i = 0;
2090 }
2091 } else if (i == 1) {
2092 i = 0;
2093 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2094 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2095 }
2096
David Benjamin2755a3e2015-04-22 16:17:58 -04002097 X509_free(x509);
2098 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002099 if (i && !ssl3_has_client_certificate(s)) {
2100 i = 0;
2101 }
2102 if (i == 0) {
2103 if (s->version == SSL3_VERSION) {
2104 s->s3->tmp.cert_req = 0;
2105 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2106 return 1;
2107 } else {
2108 s->s3->tmp.cert_req = 2;
David Benjamine76ccae2015-05-26 17:41:50 -04002109 /* There is no client certificate, so the handshake buffer may be
2110 * released. */
2111 if (s->s3->handshake_buffer &&
2112 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2113 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2114 return -1;
2115 }
Adam Langley1bea1732014-12-17 19:06:57 -08002116 }
2117 }
2118
2119 /* Ok, we have a cert */
2120 s->state = SSL3_ST_CW_CERT_C;
2121 }
2122
2123 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002124 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2125 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2126 return -1;
2127 }
Adam Langley1bea1732014-12-17 19:06:57 -08002128 }
2129
2130 /* SSL3_ST_CW_CERT_D */
2131 return ssl_do_write(s);
2132}
2133
2134#define has_bits(i, m) (((i) & (m)) == (m))
2135
2136int ssl3_check_cert_and_algorithm(SSL *s) {
Adam Langley1bea1732014-12-17 19:06:57 -08002137 long alg_k, alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08002138 SESS_CERT *sc;
2139 DH *dh;
2140
2141 /* we don't have a certificate */
2142 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2143 return 1;
2144 }
2145
2146 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2147 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2148
2149 sc = s->session->sess_cert;
2150 if (sc == NULL) {
2151 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2152 goto err;
2153 }
2154
2155 dh = s->session->sess_cert->peer_dh_tmp;
2156
David Benjaminb31040d2015-06-07 10:53:32 -04002157 int cert_type = X509_certificate_type(sc->peer_cert, NULL);
2158 if (cert_type & EVP_PK_EC) {
2159 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_cert, s) == 0) {
Adam Langley1bea1732014-12-17 19:06:57 -08002160 /* check failed */
2161 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2162 goto f_err;
2163 } else {
2164 return 1;
2165 }
2166 } else if (alg_a & SSL_aECDSA) {
2167 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2168 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2169 goto f_err;
2170 }
Adam Langley1bea1732014-12-17 19:06:57 -08002171
2172 /* Check that we have a certificate if we require one */
David Benjaminb31040d2015-06-07 10:53:32 -04002173 if ((alg_a & SSL_aRSA) && !has_bits(cert_type, EVP_PK_RSA | EVP_PKT_SIGN)) {
Adam Langley1bea1732014-12-17 19:06:57 -08002174 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2175 SSL_R_MISSING_RSA_SIGNING_CERT);
2176 goto f_err;
2177 }
2178
David Benjaminb31040d2015-06-07 10:53:32 -04002179 if ((alg_k & SSL_kRSA) && !has_bits(cert_type, EVP_PK_RSA | EVP_PKT_ENC)) {
Adam Langley1bea1732014-12-17 19:06:57 -08002180 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2181 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2182 goto f_err;
2183 }
2184
David Benjaminb31040d2015-06-07 10:53:32 -04002185 if ((alg_k & SSL_kDHE) && dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08002186 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2187 goto f_err;
2188 }
2189
2190 return 1;
2191
2192f_err:
2193 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2194err:
2195 return 0;
2196}
2197
2198int ssl3_send_next_proto(SSL *s) {
2199 unsigned int len, padding_len;
2200 uint8_t *d, *p;
2201
2202 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2203 len = s->next_proto_negotiated_len;
2204 padding_len = 32 - ((len + 2) % 32);
2205
2206 d = p = ssl_handshake_start(s);
2207 *(p++) = len;
2208 memcpy(p, s->next_proto_negotiated, len);
2209 p += len;
2210 *(p++) = padding_len;
2211 memset(p, 0, padding_len);
2212 p += padding_len;
2213
David Benjaminfbdfefb2015-02-16 19:33:53 -05002214 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2215 return -1;
2216 }
Adam Langley1bea1732014-12-17 19:06:57 -08002217 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2218 }
2219
2220 return ssl_do_write(s);
2221}
2222
2223int ssl3_send_channel_id(SSL *s) {
2224 uint8_t *d;
2225 int ret = -1, public_key_len;
2226 EVP_MD_CTX md_ctx;
2227 size_t sig_len;
2228 ECDSA_SIG *sig = NULL;
2229 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2230
2231 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2232 return ssl_do_write(s);
2233 }
2234
2235 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2236 EVP_PKEY *key = NULL;
2237 s->ctx->channel_id_cb(s, &key);
2238 if (key != NULL) {
2239 s->tlsext_channel_id_private = key;
2240 }
2241 }
2242
2243 if (!s->tlsext_channel_id_private) {
2244 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2245 return -1;
2246 }
2247 s->rwstate = SSL_NOTHING;
2248
2249 d = ssl_handshake_start(s);
2250 if (s->s3->tlsext_channel_id_new) {
2251 s2n(TLSEXT_TYPE_channel_id_new, d);
2252 } else {
2253 s2n(TLSEXT_TYPE_channel_id, d);
2254 }
2255 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2256
2257 EVP_MD_CTX_init(&md_ctx);
2258
2259 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2260 if (public_key_len <= 0) {
2261 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2262 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2263 goto err;
2264 }
2265
2266 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2267 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2268 * field elements as 32-byte, big-endian numbers. */
2269 if (public_key_len != 65) {
2270 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2271 goto err;
2272 }
2273 public_key = OPENSSL_malloc(public_key_len);
2274 if (!public_key) {
2275 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2276 goto err;
2277 }
2278
2279 derp = public_key;
2280 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2281
2282 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2283 s->tlsext_channel_id_private) != 1) {
2284 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2285 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2286 goto err;
2287 }
2288
2289 if (!tls1_channel_id_hash(&md_ctx, s)) {
2290 goto err;
2291 }
2292
2293 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2294 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2295 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2296 goto err;
2297 }
2298
2299 der_sig = OPENSSL_malloc(sig_len);
2300 if (!der_sig) {
2301 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2302 goto err;
2303 }
2304
2305 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2306 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2307 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2308 goto err;
2309 }
2310
2311 derp = der_sig;
2312 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2313 if (sig == NULL) {
2314 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2315 goto err;
2316 }
2317
2318 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2319 memcpy(d, public_key + 1, 64);
2320 d += 64;
2321 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2322 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2323 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2324 goto err;
2325 }
2326
David Benjaminfbdfefb2015-02-16 19:33:53 -05002327 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2328 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2329 goto err;
2330 }
Adam Langley1bea1732014-12-17 19:06:57 -08002331 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2332
2333 ret = ssl_do_write(s);
2334
2335err:
2336 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002337 OPENSSL_free(public_key);
2338 OPENSSL_free(der_sig);
2339 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002340
2341 return ret;
2342}
2343
2344int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2345 int i = 0;
2346 if (s->ctx->client_cert_cb) {
2347 i = s->ctx->client_cert_cb(s, px509, ppkey);
2348 }
2349 return i;
2350}