blob: eb0bbd0b0034a9895825cd9f849105d9f6f1a8b5 [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) {
196 case SSL_ST_RENEGOTIATE:
197 s->renegotiate = 1;
198 s->state = SSL_ST_CONNECT;
Adam Langley1bea1732014-12-17 19:06:57 -0800199 /* fallthrough */
200 case SSL_ST_CONNECT:
201 case SSL_ST_BEFORE | SSL_ST_CONNECT:
David Benjamin6eb000d2015-02-11 01:17:41 -0500202 if (cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800203 cb(s, SSL_CB_HANDSHAKE_START, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjaminb16346b2015-04-08 19:16:58 -0400206 if ((s->version >> 8) != 3) {
207 /* TODO(davidben): Some consumers clear |s->version| to break the
208 * handshake in a callback. Remove this when they're using proper
209 * APIs. */
210 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
211 ret = -1;
212 goto end;
213 }
214
Adam Langley1bea1732014-12-17 19:06:57 -0800215 if (s->init_buf == NULL) {
216 buf = BUF_MEM_new();
217 if (buf == NULL ||
218 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
219 ret = -1;
220 goto end;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley1bea1732014-12-17 19:06:57 -0800223 s->init_buf = buf;
224 buf = NULL;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
David Benjamin6a08da22015-05-08 22:58:12 -0400227 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800228 ret = -1;
229 goto end;
230 }
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langley1bea1732014-12-17 19:06:57 -0800232 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley1bea1732014-12-17 19:06:57 -0800234 if (!ssl3_init_finished_mac(s)) {
235 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
236 ret = -1;
237 goto end;
238 }
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley1bea1732014-12-17 19:06:57 -0800240 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800241 s->init_num = 0;
242 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langley1bea1732014-12-17 19:06:57 -0800244 case SSL3_ST_CW_CLNT_HELLO_A:
245 case SSL3_ST_CW_CLNT_HELLO_B:
246 s->shutdown = 0;
247 ret = ssl3_send_client_hello(s);
248 if (ret <= 0) {
249 goto end;
250 }
251 s->state = SSL3_ST_CR_SRVR_HELLO_A;
252 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley1bea1732014-12-17 19:06:57 -0800254 /* turn on buffering for the next lot of output */
255 if (s->bbio != s->wbio) {
256 s->wbio = BIO_push(s->bbio, s->wbio);
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley1bea1732014-12-17 19:06:57 -0800259 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langley1bea1732014-12-17 19:06:57 -0800261 case SSL3_ST_CR_SRVR_HELLO_A:
262 case SSL3_ST_CR_SRVR_HELLO_B:
263 ret = ssl3_get_server_hello(s);
264 if (ret <= 0) {
265 goto end;
266 }
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langley1bea1732014-12-17 19:06:57 -0800268 if (s->hit) {
269 s->state = SSL3_ST_CR_CHANGE;
270 if (s->tlsext_ticket_expected) {
271 /* receive renewed session ticket */
272 s->state = SSL3_ST_CR_SESSION_TICKET_A;
273 }
274 } else {
275 s->state = SSL3_ST_CR_CERT_A;
276 }
277 s->init_num = 0;
278 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400279
Adam Langley1bea1732014-12-17 19:06:57 -0800280 case SSL3_ST_CR_CERT_A:
281 case SSL3_ST_CR_CERT_B:
282 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
283 ret = ssl3_get_server_certificate(s);
284 if (ret <= 0) {
285 goto end;
286 }
287 if (s->s3->tmp.certificate_status_expected) {
288 s->state = SSL3_ST_CR_CERT_STATUS_A;
289 } else {
290 s->state = SSL3_ST_CR_KEY_EXCH_A;
291 }
292 } else {
293 skip = 1;
294 s->state = SSL3_ST_CR_KEY_EXCH_A;
295 }
296 s->init_num = 0;
297 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langley1bea1732014-12-17 19:06:57 -0800299 case SSL3_ST_CR_KEY_EXCH_A:
300 case SSL3_ST_CR_KEY_EXCH_B:
301 ret = ssl3_get_server_key_exchange(s);
302 if (ret <= 0) {
303 goto end;
304 }
305 s->state = SSL3_ST_CR_CERT_REQ_A;
306 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley1bea1732014-12-17 19:06:57 -0800308 /* at this point we check that we have the
309 * required stuff from the server */
310 if (!ssl3_check_cert_and_algorithm(s)) {
311 ret = -1;
312 goto end;
313 }
314 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langley1bea1732014-12-17 19:06:57 -0800316 case SSL3_ST_CR_CERT_REQ_A:
317 case SSL3_ST_CR_CERT_REQ_B:
318 ret = ssl3_get_certificate_request(s);
319 if (ret <= 0) {
320 goto end;
321 }
322 s->state = SSL3_ST_CR_SRVR_DONE_A;
323 s->init_num = 0;
324 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Adam Langley1bea1732014-12-17 19:06:57 -0800326 case SSL3_ST_CR_SRVR_DONE_A:
327 case SSL3_ST_CR_SRVR_DONE_B:
328 ret = ssl3_get_server_done(s);
329 if (ret <= 0) {
330 goto end;
331 }
332 if (s->s3->tmp.cert_req) {
333 s->state = SSL3_ST_CW_CERT_A;
334 } else {
335 s->state = SSL3_ST_CW_KEY_EXCH_A;
336 }
337 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langley1bea1732014-12-17 19:06:57 -0800339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langley1bea1732014-12-17 19:06:57 -0800341 case SSL3_ST_CW_CERT_A:
342 case SSL3_ST_CW_CERT_B:
343 case SSL3_ST_CW_CERT_C:
344 case SSL3_ST_CW_CERT_D:
345 ret = ssl3_send_client_certificate(s);
346 if (ret <= 0) {
347 goto end;
348 }
349 s->state = SSL3_ST_CW_KEY_EXCH_A;
350 s->init_num = 0;
351 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley1bea1732014-12-17 19:06:57 -0800353 case SSL3_ST_CW_KEY_EXCH_A:
354 case SSL3_ST_CW_KEY_EXCH_B:
355 ret = ssl3_send_client_key_exchange(s);
356 if (ret <= 0) {
357 goto end;
358 }
359 /* For TLS, cert_req is set to 2, so a cert chain
360 * of nothing is sent, but no verify packet is sent */
361 if (s->s3->tmp.cert_req == 1) {
362 s->state = SSL3_ST_CW_CERT_VRFY_A;
363 } else {
364 s->state = SSL3_ST_CW_CHANGE_A;
365 s->s3->change_cipher_spec = 0;
366 }
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley1bea1732014-12-17 19:06:57 -0800368 s->init_num = 0;
369 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langley1bea1732014-12-17 19:06:57 -0800371 case SSL3_ST_CW_CERT_VRFY_A:
372 case SSL3_ST_CW_CERT_VRFY_B:
373 ret = ssl3_send_cert_verify(s);
374 if (ret <= 0) {
375 goto end;
376 }
377 s->state = SSL3_ST_CW_CHANGE_A;
378 s->init_num = 0;
379 s->s3->change_cipher_spec = 0;
380 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley1bea1732014-12-17 19:06:57 -0800382 case SSL3_ST_CW_CHANGE_A:
383 case SSL3_ST_CW_CHANGE_B:
384 ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
385 SSL3_ST_CW_CHANGE_B);
386 if (ret <= 0) {
387 goto end;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley1bea1732014-12-17 19:06:57 -0800390 s->state = SSL3_ST_CW_FINISHED_A;
391 if (s->s3->tlsext_channel_id_valid) {
392 s->state = SSL3_ST_CW_CHANNEL_ID_A;
393 }
394 if (s->s3->next_proto_neg_seen) {
395 s->state = SSL3_ST_CW_NEXT_PROTO_A;
396 }
397 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langley1bea1732014-12-17 19:06:57 -0800399 s->session->cipher = s->s3->tmp.new_cipher;
David Benjamin8b368412015-03-14 01:54:17 -0400400 if (!s->enc_method->setup_key_block(s) ||
401 !s->enc_method->change_cipher_state(
Adam Langley1bea1732014-12-17 19:06:57 -0800402 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
403 ret = -1;
404 goto end;
405 }
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langley1bea1732014-12-17 19:06:57 -0800407 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley1bea1732014-12-17 19:06:57 -0800409 case SSL3_ST_CW_NEXT_PROTO_A:
410 case SSL3_ST_CW_NEXT_PROTO_B:
411 ret = ssl3_send_next_proto(s);
412 if (ret <= 0) {
413 goto end;
414 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700415
Adam Langley1bea1732014-12-17 19:06:57 -0800416 if (s->s3->tlsext_channel_id_valid) {
417 s->state = SSL3_ST_CW_CHANNEL_ID_A;
418 } else {
419 s->state = SSL3_ST_CW_FINISHED_A;
420 }
421 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langley1bea1732014-12-17 19:06:57 -0800423 case SSL3_ST_CW_CHANNEL_ID_A:
424 case SSL3_ST_CW_CHANNEL_ID_B:
425 ret = ssl3_send_channel_id(s);
426 if (ret <= 0) {
427 goto end;
428 }
429 s->state = SSL3_ST_CW_FINISHED_A;
430 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langley1bea1732014-12-17 19:06:57 -0800432 case SSL3_ST_CW_FINISHED_A:
433 case SSL3_ST_CW_FINISHED_B:
434 ret =
435 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
436 s->enc_method->client_finished_label,
437 s->enc_method->client_finished_label_len);
438 if (ret <= 0) {
439 goto end;
440 }
441 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langley1bea1732014-12-17 19:06:57 -0800443 if (s->hit) {
444 s->s3->tmp.next_state = SSL_ST_OK;
445 } else {
446 /* This is a non-resumption handshake. If it involves ChannelID, then
447 * record the handshake hashes at this point in the session so that
448 * any resumption of this session with ChannelID can sign those
449 * hashes. */
450 if (s->s3->tlsext_channel_id_new) {
451 ret = tls1_record_handshake_hashes_for_channel_id(s);
David Benjamin6eb000d2015-02-11 01:17:41 -0500452 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800453 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500454 }
Adam Langley1bea1732014-12-17 19:06:57 -0800455 }
David Benjamined7c4752015-02-16 19:16:46 -0500456 if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
457 ssl3_can_false_start(s) &&
458 /* No False Start on renegotiation (would complicate the state
459 * machine). */
David Benjamine6df0542015-05-12 22:02:08 -0400460 !s->s3->initial_handshake_complete) {
David Benjamined7c4752015-02-16 19:16:46 -0500461 s->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800462 } else {
463 /* Allow NewSessionTicket if ticket expected */
464 if (s->tlsext_ticket_expected) {
465 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
466 } else {
467 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
468 }
469 }
470 }
471 s->init_num = 0;
472 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langley1bea1732014-12-17 19:06:57 -0800474 case SSL3_ST_CR_SESSION_TICKET_A:
475 case SSL3_ST_CR_SESSION_TICKET_B:
476 ret = ssl3_get_new_session_ticket(s);
477 if (ret <= 0) {
478 goto end;
479 }
480 s->state = SSL3_ST_CR_CHANGE;
481 s->init_num = 0;
482 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langley1bea1732014-12-17 19:06:57 -0800484 case SSL3_ST_CR_CERT_STATUS_A:
485 case SSL3_ST_CR_CERT_STATUS_B:
486 ret = ssl3_get_cert_status(s);
487 if (ret <= 0) {
488 goto end;
489 }
490 s->state = SSL3_ST_CR_KEY_EXCH_A;
491 s->init_num = 0;
492 break;
David Benjamincb5abad2014-07-25 12:14:28 -0400493
Adam Langley1bea1732014-12-17 19:06:57 -0800494 case SSL3_ST_CR_CHANGE:
495 /* At this point, the next message must be entirely behind a
496 * ChangeCipherSpec. */
497 if (!ssl3_expect_change_cipher_spec(s)) {
498 ret = -1;
499 goto end;
500 }
501 s->state = SSL3_ST_CR_FINISHED_A;
502 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langley1bea1732014-12-17 19:06:57 -0800504 case SSL3_ST_CR_FINISHED_A:
505 case SSL3_ST_CR_FINISHED_B:
506 ret =
507 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
508 if (ret <= 0) {
509 goto end;
510 }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langley1bea1732014-12-17 19:06:57 -0800512 if (s->hit) {
513 s->state = SSL3_ST_CW_CHANGE_A;
514 } else {
515 s->state = SSL_ST_OK;
516 }
517 s->init_num = 0;
518 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langley1bea1732014-12-17 19:06:57 -0800520 case SSL3_ST_CW_FLUSH:
521 s->rwstate = SSL_WRITING;
522 if (BIO_flush(s->wbio) <= 0) {
523 ret = -1;
524 goto end;
525 }
526 s->rwstate = SSL_NOTHING;
527 s->state = s->s3->tmp.next_state;
528 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700529
David Benjamined7c4752015-02-16 19:16:46 -0500530 case SSL3_ST_FALSE_START:
Adam Langley1bea1732014-12-17 19:06:57 -0800531 /* Allow NewSessionTicket if ticket expected */
532 if (s->tlsext_ticket_expected) {
533 s->state = SSL3_ST_CR_SESSION_TICKET_A;
534 } else {
535 s->state = SSL3_ST_CR_CHANGE;
536 }
David Benjamined7c4752015-02-16 19:16:46 -0500537 s->s3->tmp.in_false_start = 1;
Adam Langleyadb739e2014-06-20 12:00:00 -0700538
Adam Langley1bea1732014-12-17 19:06:57 -0800539 ssl_free_wbio_buffer(s);
540 ret = 1;
541 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley1bea1732014-12-17 19:06:57 -0800543 case SSL_ST_OK:
544 /* clean a few things up */
545 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700546
David Benjamin2755a3e2015-04-22 16:17:58 -0400547 BUF_MEM_free(s->init_buf);
548 s->init_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700549
Adam Langley1bea1732014-12-17 19:06:57 -0800550 /* Remove write buffering now. */
551 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 s->init_num = 0;
554 s->renegotiate = 0;
David Benjamined7c4752015-02-16 19:16:46 -0500555 s->s3->tmp.in_false_start = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400556 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langley1bea1732014-12-17 19:06:57 -0800558 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700559
Adam Langley1bea1732014-12-17 19:06:57 -0800560 ret = 1;
561 /* s->server=0; */
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langley1bea1732014-12-17 19:06:57 -0800563 if (cb != NULL) {
564 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
565 }
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langley1bea1732014-12-17 19:06:57 -0800567 goto end;
568
569 default:
570 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
571 ret = -1;
572 goto end;
573 }
574
575 if (!s->s3->tmp.reuse_message && !skip) {
576 if (cb != NULL && s->state != state) {
577 new_state = s->state;
578 s->state = state;
579 cb(s, SSL_CB_CONNECT_LOOP, 1);
580 s->state = new_state;
581 }
582 }
583 skip = 0;
584 }
585
Adam Langley95c29f32014-06-20 12:00:00 -0700586end:
Adam Langley1bea1732014-12-17 19:06:57 -0800587 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400588 BUF_MEM_free(buf);
Adam Langley1bea1732014-12-17 19:06:57 -0800589 if (cb != NULL) {
590 cb(s, SSL_CB_CONNECT_EXIT, ret);
591 }
592 return ret;
593}
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langley1bea1732014-12-17 19:06:57 -0800595int ssl3_send_client_hello(SSL *s) {
596 uint8_t *buf, *p, *d;
597 int i;
598 unsigned long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley1bea1732014-12-17 19:06:57 -0800600 buf = (uint8_t *)s->init_buf->data;
601 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
602 if (!s->s3->have_version) {
603 uint16_t max_version = ssl3_get_max_client_version(s);
604 /* Disabling all versions is silly: return an error. */
605 if (max_version == 0) {
606 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
607 goto err;
608 }
609 s->version = max_version;
610 s->client_version = max_version;
611 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500612
Adam Langley1bea1732014-12-17 19:06:57 -0800613 /* If the configured session was created at a version higher than our
614 * maximum version, drop it. */
615 if (s->session &&
616 (s->session->session_id_length == 0 || s->session->not_resumable ||
617 (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
618 (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
619 SSL_set_session(s, NULL);
620 }
David Benjamin81ea0bf2014-11-23 04:20:17 -0500621
Adam Langley1bea1732014-12-17 19:06:57 -0800622 /* else use the pre-loaded session */
623 p = s->s3->client_random;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langley1bea1732014-12-17 19:06:57 -0800625 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
626 * renegerate the client_random. The random must be reused. */
David Benjamin2ddba8c2015-03-18 19:40:51 -0400627 if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
628 !ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random))) {
629 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -0800630 }
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langley1bea1732014-12-17 19:06:57 -0800632 /* Do the message type and length last. Note: the final argument to
633 * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
634 d = p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley1bea1732014-12-17 19:06:57 -0800636 /* version indicates the negotiated version: for example from an SSLv2/v3
637 * compatible client hello). The client_version field is the maximum
638 * version we permit and it is also used in RSA encrypted premaster
639 * secrets. Some servers can choke if we initially report a higher version
640 * then renegotiate to a lower one in the premaster secret. This didn't
641 * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
642 * or later if the server only supports 1.0.
643 *
644 * Possible scenario with previous logic:
645 * 1. Client hello indicates TLS 1.2
646 * 2. Server hello says TLS 1.0
647 * 3. RSA encrypted premaster secret uses 1.2.
648 * 4. Handhaked proceeds using TLS 1.0.
649 * 5. Server sends hello request to renegotiate.
650 * 6. Client hello indicates TLS v1.0 as we now
651 * know that is maximum server supports.
652 * 7. Server chokes on RSA encrypted premaster secret
653 * containing version 1.0.
654 *
655 * For interoperability it should be OK to always use the maximum version
656 * we support in client hello and then rely on the checking of version to
657 * ensure the servers isn't being inconsistent: for example initially
658 * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
659 * using client_version in client hello and not resetting it to the
660 * negotiated version. */
661 *(p++) = s->client_version >> 8;
662 *(p++) = s->client_version & 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langley1bea1732014-12-17 19:06:57 -0800664 /* Random stuff */
665 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
666 p += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langley1bea1732014-12-17 19:06:57 -0800668 /* Session ID */
David Benjamin4b27d9f2015-05-12 22:42:52 -0400669 if (s->s3->initial_handshake_complete || s->session == NULL) {
670 /* Renegotiations do not participate in session resumption. */
Adam Langley1bea1732014-12-17 19:06:57 -0800671 i = 0;
672 } else {
673 i = s->session->session_id_length;
674 }
675 *(p++) = i;
676 if (i != 0) {
677 if (i > (int)sizeof(s->session->session_id)) {
678 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
679 goto err;
680 }
681 memcpy(p, s->session->session_id, i);
682 p += i;
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langley1bea1732014-12-17 19:06:57 -0800685 /* cookie stuff for DTLS */
686 if (SSL_IS_DTLS(s)) {
687 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
688 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
689 goto err;
690 }
691 *(p++) = s->d1->cookie_len;
692 memcpy(p, s->d1->cookie, s->d1->cookie_len);
693 p += s->d1->cookie_len;
694 }
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langley1bea1732014-12-17 19:06:57 -0800696 /* Ciphers supported */
697 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
698 if (i == 0) {
699 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
700 SSL_R_NO_CIPHERS_AVAILABLE);
701 goto err;
702 }
703 s2n(i, p);
704 p += i;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langley1bea1732014-12-17 19:06:57 -0800706 /* COMPRESSION */
707 *(p++) = 1;
708 *(p++) = 0; /* Add the NULL method */
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley1bea1732014-12-17 19:06:57 -0800710 /* TLS extensions*/
711 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
712 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
713 goto err;
714 }
715
716 p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
717 p - buf);
718 if (p == NULL) {
719 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
720 goto err;
721 }
722
723 l = p - d;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500724 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
725 goto err;
726 }
Adam Langley1bea1732014-12-17 19:06:57 -0800727 s->state = SSL3_ST_CW_CLNT_HELLO_B;
728 }
729
730 /* SSL3_ST_CW_CLNT_HELLO_B */
731 return ssl_do_write(s);
732
Adam Langley95c29f32014-06-20 12:00:00 -0700733err:
Adam Langley1bea1732014-12-17 19:06:57 -0800734 return -1;
735}
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langley1bea1732014-12-17 19:06:57 -0800737int ssl3_get_server_hello(SSL *s) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400738 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800739 const SSL_CIPHER *c;
740 CERT *ct = s->cert;
741 int al = SSL_AD_INTERNAL_ERROR, ok;
742 long n;
743 CBS server_hello, server_random, session_id;
744 uint16_t server_version, cipher_suite;
745 uint8_t compression_method;
David Benjamin107db582015-04-08 00:41:59 -0400746 uint32_t mask_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langley1bea1732014-12-17 19:06:57 -0800748 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
749 SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
750 20000, /* ?? */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500751 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langley1bea1732014-12-17 19:06:57 -0800753 if (!ok) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500754 uint32_t err = ERR_peek_error();
755 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
756 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
757 /* Add a dedicated error code to the queue for a handshake_failure alert
758 * in response to ClientHello. This matches NSS's client behavior and
759 * gives a better error on a (probable) failure to negotiate initial
760 * parameters. Note: this error code comes after the original one.
761 *
762 * See https://crbug.com/446505. */
763 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
764 SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
765 }
Adam Langley1bea1732014-12-17 19:06:57 -0800766 return n;
767 }
Adam Langley95c29f32014-06-20 12:00:00 -0700768
Adam Langley1bea1732014-12-17 19:06:57 -0800769 CBS_init(&server_hello, s->init_msg, n);
David Benjamina03d95d2014-07-12 19:49:07 -0400770
Adam Langley1bea1732014-12-17 19:06:57 -0800771 if (!CBS_get_u16(&server_hello, &server_version) ||
772 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
773 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
774 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
775 !CBS_get_u16(&server_hello, &cipher_suite) ||
776 !CBS_get_u8(&server_hello, &compression_method)) {
777 al = SSL_AD_DECODE_ERROR;
778 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
779 goto f_err;
780 }
David Benjamina03d95d2014-07-12 19:49:07 -0400781
David Benjamine6df0542015-05-12 22:02:08 -0400782 assert(s->s3->have_version == s->s3->initial_handshake_complete);
Adam Langley1bea1732014-12-17 19:06:57 -0800783 if (!s->s3->have_version) {
784 if (!ssl3_is_version_enabled(s, server_version)) {
785 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
786 s->version = server_version;
787 /* Mark the version as fixed so the record-layer version is not clamped
788 * to TLS 1.0. */
789 s->s3->have_version = 1;
790 al = SSL_AD_PROTOCOL_VERSION;
791 goto f_err;
792 }
793 s->version = server_version;
794 s->enc_method = ssl3_get_enc_method(server_version);
795 assert(s->enc_method != NULL);
796 /* At this point, the connection's version is known and s->version is
797 * fixed. Begin enforcing the record-layer version. */
798 s->s3->have_version = 1;
799 } else if (server_version != s->version) {
800 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
801 al = SSL_AD_PROTOCOL_VERSION;
802 goto f_err;
803 }
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langley1bea1732014-12-17 19:06:57 -0800805 /* Copy over the server random. */
806 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langley1bea1732014-12-17 19:06:57 -0800808 assert(s->session == NULL || s->session->session_id_length > 0);
David Benjamin4b27d9f2015-05-12 22:42:52 -0400809 if (!s->s3->initial_handshake_complete && s->session != NULL &&
810 CBS_mem_equal(&session_id, s->session->session_id,
811 s->session->session_id_length)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800812 if (s->sid_ctx_length != s->session->sid_ctx_length ||
813 memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
814 /* actually a client application bug */
815 al = SSL_AD_ILLEGAL_PARAMETER;
816 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
817 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
818 goto f_err;
819 }
820 s->hit = 1;
821 } else {
822 /* The session wasn't resumed. Create a fresh SSL_SESSION to
823 * fill out. */
824 s->hit = 0;
825 if (!ssl_get_new_session(s, 0)) {
826 goto f_err;
827 }
828 /* Note: session_id could be empty. */
829 s->session->session_id_length = CBS_len(&session_id);
830 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
831 }
David Benjamina03d95d2014-07-12 19:49:07 -0400832
Adam Langley1bea1732014-12-17 19:06:57 -0800833 c = ssl3_get_cipher_by_value(cipher_suite);
834 if (c == NULL) {
835 /* unknown cipher */
836 al = SSL_AD_ILLEGAL_PARAMETER;
837 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
838 SSL_R_UNKNOWN_CIPHER_RETURNED);
839 goto f_err;
840 }
841 /* ct->mask_ssl was computed from client capabilities. Now
842 * that the final version is known, compute a new mask_ssl. */
843 if (!SSL_USE_TLS1_2_CIPHERS(s)) {
844 mask_ssl = SSL_TLSV1_2;
845 } else {
846 mask_ssl = 0;
847 }
848 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
849 * the server selected it, it's an error. */
850 if ((c->algorithm_ssl & mask_ssl) ||
851 (c->algorithm_mkey & ct->mask_k) ||
852 (c->algorithm_auth & ct->mask_a)) {
853 al = SSL_AD_ILLEGAL_PARAMETER;
854 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
855 goto f_err;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langley1bea1732014-12-17 19:06:57 -0800858 sk = ssl_get_ciphers_by_id(s);
859 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
860 /* we did not say we would use this cipher */
861 al = SSL_AD_ILLEGAL_PARAMETER;
862 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
863 goto f_err;
864 }
Adam Langley95c29f32014-06-20 12:00:00 -0700865
David Benjaminece3de92015-03-16 18:02:20 -0400866 if (s->hit) {
867 if (s->session->cipher != c) {
868 al = SSL_AD_ILLEGAL_PARAMETER;
869 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
870 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
871 goto f_err;
872 }
873 if (s->session->ssl_version != s->version) {
874 al = SSL_AD_ILLEGAL_PARAMETER;
875 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
876 SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
877 goto f_err;
878 }
Adam Langley1bea1732014-12-17 19:06:57 -0800879 }
880 s->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500881
Adam Langley1bea1732014-12-17 19:06:57 -0800882 /* Don't digest cached records if no sigalgs: we may need them for client
883 * authentication. */
884 if (!SSL_USE_SIGALGS(s) &&
885 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
886 goto f_err;
887 }
David Benjamina03d95d2014-07-12 19:49:07 -0400888
Adam Langley1bea1732014-12-17 19:06:57 -0800889 /* Only the NULL compression algorithm is supported. */
890 if (compression_method != 0) {
891 al = SSL_AD_ILLEGAL_PARAMETER;
892 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
893 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
894 goto f_err;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langley1bea1732014-12-17 19:06:57 -0800897 /* TLS extensions */
898 if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
899 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
900 goto err;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langley1bea1732014-12-17 19:06:57 -0800903 /* There should be nothing left over in the record. */
904 if (CBS_len(&server_hello) != 0) {
905 /* wrong packet length */
906 al = SSL_AD_DECODE_ERROR;
907 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
908 goto f_err;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langley1bea1732014-12-17 19:06:57 -0800911 return 1;
912
Adam Langley95c29f32014-06-20 12:00:00 -0700913f_err:
Adam Langley1bea1732014-12-17 19:06:57 -0800914 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700915err:
Adam Langley1bea1732014-12-17 19:06:57 -0800916 return -1;
917}
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langley1bea1732014-12-17 19:06:57 -0800919int ssl3_get_server_certificate(SSL *s) {
920 int al, i, ok, ret = -1;
921 unsigned long n;
922 X509 *x = NULL;
923 STACK_OF(X509) *sk = NULL;
924 SESS_CERT *sc;
925 EVP_PKEY *pkey = NULL;
926 CBS cbs, certificate_list;
927 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langley1bea1732014-12-17 19:06:57 -0800929 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
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 }
1031 sc->peer_cert_type = i;
David Benjamin2755a3e2015-04-22 16:17:58 -04001032 X509_free(sc->peer_pkeys[i].x509);
Adam Langley1bea1732014-12-17 19:06:57 -08001033 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1034 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001035
David Benjamin2755a3e2015-04-22 16:17:58 -04001036 X509_free(s->session->peer);
Adam Langley1bea1732014-12-17 19:06:57 -08001037 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 s->session->verify_result = s->verify_result;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langley1bea1732014-12-17 19:06:57 -08001041 x = NULL;
1042 ret = 1;
1043
1044 if (0) {
1045 f_err:
1046 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1047 }
1048
1049err:
1050 EVP_PKEY_free(pkey);
1051 X509_free(x);
1052 sk_X509_pop_free(sk, X509_free);
1053 return ret;
1054}
1055
1056int ssl3_get_server_key_exchange(SSL *s) {
1057 EVP_MD_CTX md_ctx;
1058 int al, ok;
1059 long n, alg_k, alg_a;
1060 EVP_PKEY *pkey = NULL;
1061 const EVP_MD *md = NULL;
1062 RSA *rsa = NULL;
1063 DH *dh = NULL;
1064 EC_KEY *ecdh = NULL;
1065 BN_CTX *bn_ctx = NULL;
1066 EC_POINT *srvr_ecpoint = NULL;
1067 CBS server_key_exchange, server_key_exchange_orig, parameter;
1068
1069 /* use same message size as in ssl3_get_certificate_request() as
1070 * ServerKeyExchange message may be skipped */
1071 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1072 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001073 ssl_hash_message, &ok);
Adam Langley1bea1732014-12-17 19:06:57 -08001074 if (!ok) {
1075 return n;
1076 }
1077
1078 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1079 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
1080 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1081 SSL_R_UNEXPECTED_MESSAGE);
1082 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1083 return -1;
1084 }
1085
1086 /* In plain PSK ciphersuite, ServerKeyExchange can be
1087 omitted if no identity hint is sent. Set session->sess_cert anyway to
1088 avoid problems later.*/
1089 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1090 /* PSK ciphersuites that also send a Certificate would have already
1091 * initialized |sess_cert|. */
1092 if (s->session->sess_cert == NULL) {
1093 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001094 if (s->session->sess_cert == NULL) {
1095 return -1;
1096 }
Adam Langley1bea1732014-12-17 19:06:57 -08001097 }
1098
1099 /* TODO(davidben): This should be reset in one place with the rest of the
1100 * handshake state. */
David Benjamin2755a3e2015-04-22 16:17:58 -04001101 OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
1102 s->s3->tmp.peer_psk_identity_hint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001103 }
1104 s->s3->tmp.reuse_message = 1;
1105 return 1;
1106 }
1107
1108 /* Retain a copy of the original CBS to compute the signature over. */
1109 CBS_init(&server_key_exchange, s->init_msg, n);
1110 server_key_exchange_orig = server_key_exchange;
1111
1112 if (s->session->sess_cert != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001113 DH_free(s->session->sess_cert->peer_dh_tmp);
1114 s->session->sess_cert->peer_dh_tmp = NULL;
1115 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1116 s->session->sess_cert->peer_ecdh_tmp = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001117 } else {
1118 s->session->sess_cert = ssl_sess_cert_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05001119 if (s->session->sess_cert == NULL) {
1120 return -1;
1121 }
Adam Langley1bea1732014-12-17 19:06:57 -08001122 }
1123
1124 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1125 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1126 EVP_MD_CTX_init(&md_ctx);
1127
1128 if (alg_a & SSL_aPSK) {
1129 CBS psk_identity_hint;
1130
1131 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1132 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1133 &psk_identity_hint)) {
1134 al = SSL_AD_DECODE_ERROR;
1135 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1136 goto f_err;
1137 }
1138
1139 /* Store PSK identity hint for later use, hint is used in
1140 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1141 * identity hint can be as long as the maximum length of a PSK identity.
1142 * Also do not allow NULL characters; identities are saved as C strings.
1143 *
1144 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1145 * a specific identity. */
1146 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1147 CBS_contains_zero_byte(&psk_identity_hint)) {
1148 al = SSL_AD_HANDSHAKE_FAILURE;
1149 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1150 SSL_R_DATA_LENGTH_TOO_LONG);
1151 goto f_err;
1152 }
1153
1154 /* Save the identity hint as a C string. */
1155 if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
1156 al = SSL_AD_INTERNAL_ERROR;
1157 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1158 ERR_R_MALLOC_FAILURE);
1159 goto f_err;
1160 }
1161 }
1162
David Benjamin7061e282015-03-19 11:10:48 -04001163 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001164 CBS dh_p, dh_g, dh_Ys;
1165
1166 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1167 CBS_len(&dh_p) == 0 ||
1168 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1169 CBS_len(&dh_g) == 0 ||
1170 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1171 CBS_len(&dh_Ys) == 0) {
1172 al = SSL_AD_DECODE_ERROR;
1173 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1174 goto f_err;
1175 }
1176
1177 dh = DH_new();
1178 if (dh == NULL) {
1179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
1180 goto err;
1181 }
1182
1183 if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
1184 (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
1185 (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
1186 NULL) {
1187 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
1188 goto err;
1189 }
1190
Adam Langleya7997f12015-05-14 17:38:50 -07001191 if (DH_num_bits(dh) < 1024) {
Adam Langley1bea1732014-12-17 19:06:57 -08001192 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1193 SSL_R_BAD_DH_P_LENGTH);
1194 goto err;
1195 }
1196
1197 if (alg_a & SSL_aRSA) {
1198 pkey = X509_get_pubkey(
1199 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1200 }
1201 /* else anonymous DH, so no certificate or pkey. */
1202
1203 s->session->sess_cert->peer_dh_tmp = dh;
1204 dh = NULL;
David Benjamin7061e282015-03-19 11:10:48 -04001205 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001206 uint16_t curve_id;
1207 int curve_nid = 0;
Adam Langley1bea1732014-12-17 19:06:57 -08001208 const EC_GROUP *group;
1209 CBS point;
1210
1211 /* Extract elliptic curve parameters and the server's ephemeral ECDH public
1212 * key. Check curve is one of our preferences, if not server has sent an
1213 * invalid curve. */
1214 if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
1215 al = SSL_AD_DECODE_ERROR;
1216 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
1217 goto f_err;
1218 }
1219
1220 curve_nid = tls1_ec_curve_id2nid(curve_id);
1221 if (curve_nid == 0) {
1222 al = SSL_AD_INTERNAL_ERROR;
1223 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1224 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1225 goto f_err;
1226 }
1227
David Benjamin4f7783e2015-03-05 03:05:11 -05001228 ecdh = EC_KEY_new_by_curve_name(curve_nid);
Adam Langley1bea1732014-12-17 19:06:57 -08001229 if (ecdh == NULL) {
1230 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
David Benjamin4f7783e2015-03-05 03:05:11 -05001231 ERR_R_EC_LIB);
Adam Langley1bea1732014-12-17 19:06:57 -08001232 goto err;
1233 }
1234
Adam Langley1bea1732014-12-17 19:06:57 -08001235 group = EC_KEY_get0_group(ecdh);
1236
1237 /* Next, get the encoded ECPoint */
1238 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1239 al = SSL_AD_DECODE_ERROR;
1240 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1241 goto f_err;
1242 }
1243
1244 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1245 ((bn_ctx = BN_CTX_new()) == NULL)) {
1246 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1247 ERR_R_MALLOC_FAILURE);
1248 goto err;
1249 }
1250
1251 if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
1252 CBS_len(&point), bn_ctx)) {
1253 al = SSL_AD_DECODE_ERROR;
1254 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
1255 goto f_err;
1256 }
1257
1258 /* The ECC/TLS specification does not mention the use of DSA to sign
1259 * ECParameters in the server key exchange message. We do support RSA and
1260 * ECDSA. */
1261 if (alg_a & SSL_aRSA) {
1262 pkey = X509_get_pubkey(
1263 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1264 } else if (alg_a & SSL_aECDSA) {
1265 pkey =
1266 X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1267 }
1268 /* else anonymous ECDH, so no certificate or pkey. */
1269 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1270 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1271 ecdh = NULL;
1272 BN_CTX_free(bn_ctx);
1273 bn_ctx = NULL;
1274 EC_POINT_free(srvr_ecpoint);
1275 srvr_ecpoint = NULL;
1276 } else if (!(alg_k & SSL_kPSK)) {
1277 al = SSL_AD_UNEXPECTED_MESSAGE;
1278 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1279 SSL_R_UNEXPECTED_MESSAGE);
1280 goto f_err;
1281 }
1282
1283 /* At this point, |server_key_exchange| contains the signature, if any, while
1284 * |server_key_exchange_orig| contains the entire message. From that, derive
1285 * a CBS containing just the parameter. */
1286 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1287 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1288
1289 /* if it was signed, check the signature */
1290 if (pkey != NULL) {
1291 CBS signature;
1292
1293 if (SSL_USE_SIGALGS(s)) {
1294 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
1295 goto f_err;
1296 }
1297 } else if (pkey->type == EVP_PKEY_RSA) {
1298 md = EVP_md5_sha1();
1299 } else {
1300 md = EVP_sha1();
1301 }
1302
1303 /* The last field in |server_key_exchange| is the signature. */
1304 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1305 CBS_len(&server_key_exchange) != 0) {
1306 al = SSL_AD_DECODE_ERROR;
1307 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
1308 goto f_err;
1309 }
1310
1311 if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1312 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
1313 SSL3_RANDOM_SIZE) ||
1314 !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
1315 SSL3_RANDOM_SIZE) ||
1316 !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1317 CBS_len(&parameter)) ||
1318 !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1319 CBS_len(&signature))) {
1320 /* bad signature */
1321 al = SSL_AD_DECRYPT_ERROR;
1322 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
1323 goto f_err;
1324 }
1325 } else {
1326 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
1327 /* Might be wrong key type, check it */
1328 if (ssl3_check_cert_and_algorithm(s)) {
1329 /* Otherwise this shouldn't happen */
1330 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1331 ERR_R_INTERNAL_ERROR);
1332 }
1333 goto err;
1334 }
1335 /* still data left over */
1336 if (CBS_len(&server_key_exchange) > 0) {
1337 al = SSL_AD_DECODE_ERROR;
1338 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
1339 SSL_R_EXTRA_DATA_IN_MESSAGE);
1340 goto f_err;
1341 }
1342 }
1343 EVP_PKEY_free(pkey);
1344 EVP_MD_CTX_cleanup(&md_ctx);
1345 return 1;
1346
Adam Langley95c29f32014-06-20 12:00:00 -07001347f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001348 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001349err:
Adam Langley1bea1732014-12-17 19:06:57 -08001350 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001351 RSA_free(rsa);
1352 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001353 BN_CTX_free(bn_ctx);
1354 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001355 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001356 EVP_MD_CTX_cleanup(&md_ctx);
1357 return -1;
1358}
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langley1bea1732014-12-17 19:06:57 -08001360static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1361 return X509_NAME_cmp(*a, *b);
1362}
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Adam Langley1bea1732014-12-17 19:06:57 -08001364int ssl3_get_certificate_request(SSL *s) {
1365 int ok, ret = 0;
1366 unsigned long n;
1367 X509_NAME *xn = NULL;
1368 STACK_OF(X509_NAME) *ca_sk = NULL;
1369 CBS cbs;
1370 CBS certificate_types;
1371 CBS certificate_authorities;
1372 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langley1bea1732014-12-17 19:06:57 -08001374 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1375 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001376 ssl_hash_message, &ok);
David Benjamin9c651c92014-07-12 13:27:45 -04001377
Adam Langley1bea1732014-12-17 19:06:57 -08001378 if (!ok) {
1379 return n;
1380 }
David Benjamin688d8df2014-11-02 23:06:42 -05001381
Adam Langley1bea1732014-12-17 19:06:57 -08001382 s->s3->tmp.cert_req = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001383
Adam Langley1bea1732014-12-17 19:06:57 -08001384 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1385 s->s3->tmp.reuse_message = 1;
1386 /* If we get here we don't need any cached handshake records as we wont be
1387 * doing client auth. */
1388 if (s->s3->handshake_buffer &&
1389 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1390 goto err;
1391 }
1392 return 1;
1393 }
David Benjamined439582014-07-14 19:13:02 -04001394
Adam Langley1bea1732014-12-17 19:06:57 -08001395 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1396 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1397 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1398 SSL_R_WRONG_MESSAGE_TYPE);
1399 goto err;
1400 }
Adam Langley95c29f32014-06-20 12:00:00 -07001401
Adam Langley1bea1732014-12-17 19:06:57 -08001402 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langley1bea1732014-12-17 19:06:57 -08001404 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1405 if (ca_sk == NULL) {
1406 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1407 goto err;
1408 }
Adam Langley95c29f32014-06-20 12:00:00 -07001409
Adam Langley1bea1732014-12-17 19:06:57 -08001410 /* get the certificate types */
1411 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1412 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1413 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1414 goto err;
1415 }
David Benjamined439582014-07-14 19:13:02 -04001416
Adam Langley1bea1732014-12-17 19:06:57 -08001417 if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
1418 &s->s3->tmp.num_certificate_types)) {
1419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1420 goto err;
1421 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001422
Adam Langley1bea1732014-12-17 19:06:57 -08001423 if (SSL_USE_SIGALGS(s)) {
1424 CBS supported_signature_algorithms;
1425 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
1426 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1427 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1428 goto err;
1429 }
David Benjamined439582014-07-14 19:13:02 -04001430
Adam Langley1bea1732014-12-17 19:06:57 -08001431 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1432 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1433 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1434 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1435 goto err;
1436 }
1437 }
David Benjamined439582014-07-14 19:13:02 -04001438
Adam Langley1bea1732014-12-17 19:06:57 -08001439 /* get the CA RDNs */
1440 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1441 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1442 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1443 goto err;
1444 }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langley1bea1732014-12-17 19:06:57 -08001446 while (CBS_len(&certificate_authorities) > 0) {
1447 CBS distinguished_name;
1448 if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1449 &distinguished_name)) {
1450 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1451 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1452 SSL_R_CA_DN_TOO_LONG);
1453 goto err;
1454 }
Adam Langley95c29f32014-06-20 12:00:00 -07001455
Adam Langley1bea1732014-12-17 19:06:57 -08001456 data = CBS_data(&distinguished_name);
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001457
Adam Langley1bea1732014-12-17 19:06:57 -08001458 xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
1459 if (xn == NULL) {
1460 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1461 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1462 goto err;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langley1bea1732014-12-17 19:06:57 -08001465 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1466 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001467 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001468 goto err;
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
Adam Langley1bea1732014-12-17 19:06:57 -08001471 if (CBS_len(&distinguished_name) != 0) {
1472 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1473 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1474 SSL_R_CA_DN_LENGTH_MISMATCH);
1475 goto err;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
Adam Langley1bea1732014-12-17 19:06:57 -08001478 if (!sk_X509_NAME_push(ca_sk, xn)) {
1479 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
1480 ERR_R_MALLOC_FAILURE);
1481 goto err;
1482 }
1483 }
Adam Langley95c29f32014-06-20 12:00:00 -07001484
Adam Langley1bea1732014-12-17 19:06:57 -08001485 /* we should setup a certificate to return.... */
1486 s->s3->tmp.cert_req = 1;
David Benjamin2755a3e2015-04-22 16:17:58 -04001487 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001488 s->s3->tmp.ca_names = ca_sk;
1489 ca_sk = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langley1bea1732014-12-17 19:06:57 -08001491 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001492
Adam Langley1bea1732014-12-17 19:06:57 -08001493err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001494 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Adam Langley1bea1732014-12-17 19:06:57 -08001495 return ret;
1496}
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langley1bea1732014-12-17 19:06:57 -08001498int ssl3_get_new_session_ticket(SSL *s) {
David Benjamin68070622015-02-08 23:44:59 -05001499 int ok, al;
Adam Langley1bea1732014-12-17 19:06:57 -08001500 long n;
1501 CBS new_session_ticket, ticket;
David Benjamined439582014-07-14 19:13:02 -04001502
Adam Langley1bea1732014-12-17 19:06:57 -08001503 n = s->method->ssl_get_message(
1504 s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin5ca39fb2015-03-01 23:57:54 -05001505 SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001506
Adam Langley1bea1732014-12-17 19:06:57 -08001507 if (!ok) {
1508 return n;
1509 }
Adam Langley95c29f32014-06-20 12:00:00 -07001510
Adam Langley1bea1732014-12-17 19:06:57 -08001511 CBS_init(&new_session_ticket, s->init_msg, n);
Adam Langleyc26c8022014-06-20 12:00:00 -07001512
Adam Langley1bea1732014-12-17 19:06:57 -08001513 if (!CBS_get_u32(&new_session_ticket,
1514 &s->session->tlsext_tick_lifetime_hint) ||
1515 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1516 CBS_len(&new_session_ticket) != 0) {
1517 al = SSL_AD_DECODE_ERROR;
1518 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
1519 goto f_err;
1520 }
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langley1bea1732014-12-17 19:06:57 -08001522 if (!CBS_stow(&ticket, &s->session->tlsext_tick,
1523 &s->session->tlsext_ticklen)) {
1524 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1525 goto err;
1526 }
Adam Langley95c29f32014-06-20 12:00:00 -07001527
David Benjamin68070622015-02-08 23:44:59 -05001528 /* Generate a session ID for this session based on the session ticket. We use
1529 * the session ID mechanism for detecting ticket resumption. This also fits in
1530 * with assumptions elsewhere in OpenSSL.*/
1531 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
1532 &s->session->session_id_length, EVP_sha256(), NULL)) {
1533 goto err;
1534 }
1535
1536 return 1;
David Benjamined439582014-07-14 19:13:02 -04001537
Adam Langley95c29f32014-06-20 12:00:00 -07001538f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001539 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001540err:
Adam Langley1bea1732014-12-17 19:06:57 -08001541 return -1;
1542}
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langley1bea1732014-12-17 19:06:57 -08001544int ssl3_get_cert_status(SSL *s) {
1545 int ok, al;
1546 long n;
1547 CBS certificate_status, ocsp_response;
1548 uint8_t status_type;
Adam Langley95c29f32014-06-20 12:00:00 -07001549
Adam Langley1bea1732014-12-17 19:06:57 -08001550 n = s->method->ssl_get_message(
1551 s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
David Benjamindcd979f2015-04-20 18:26:52 -04001552 -1, 16384, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001553
Adam Langley1bea1732014-12-17 19:06:57 -08001554 if (!ok) {
1555 return n;
1556 }
Adam Langley95c29f32014-06-20 12:00:00 -07001557
David Benjamindcd979f2015-04-20 18:26:52 -04001558 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1559 /* A server may send status_request in ServerHello and then change
1560 * its mind about sending CertificateStatus. */
1561 s->s3->tmp.reuse_message = 1;
1562 return 1;
1563 }
1564
Adam Langley1bea1732014-12-17 19:06:57 -08001565 CBS_init(&certificate_status, s->init_msg, n);
1566 if (!CBS_get_u8(&certificate_status, &status_type) ||
1567 status_type != TLSEXT_STATUSTYPE_ocsp ||
1568 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1569 CBS_len(&ocsp_response) == 0 ||
1570 CBS_len(&certificate_status) != 0) {
1571 al = SSL_AD_DECODE_ERROR;
1572 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
1573 goto f_err;
1574 }
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langley1bea1732014-12-17 19:06:57 -08001576 if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
1577 &s->session->ocsp_response_length)) {
1578 al = SSL_AD_INTERNAL_ERROR;
1579 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1580 goto f_err;
1581 }
1582 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001583
Adam Langley95c29f32014-06-20 12:00:00 -07001584f_err:
Adam Langley1bea1732014-12-17 19:06:57 -08001585 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1586 return -1;
1587}
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Adam Langley1bea1732014-12-17 19:06:57 -08001589int ssl3_get_server_done(SSL *s) {
1590 int ok;
1591 long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001592
Adam Langley1bea1732014-12-17 19:06:57 -08001593 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1594 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1595 30, /* should be very small, like 0 :-) */
David Benjamin5ca39fb2015-03-01 23:57:54 -05001596 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001597
Adam Langley1bea1732014-12-17 19:06:57 -08001598 if (!ok) {
1599 return n;
1600 }
David Benjamin46062682014-07-14 19:14:32 -04001601
Adam Langley1bea1732014-12-17 19:06:57 -08001602 if (n > 0) {
1603 /* should contain no data */
1604 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1605 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1606 return -1;
1607 }
David Benjamin46062682014-07-14 19:14:32 -04001608
Adam Langley1bea1732014-12-17 19:06:57 -08001609 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001610}
Adam Langley1258b6a2014-06-20 12:00:00 -07001611
Adam Langley1258b6a2014-06-20 12:00:00 -07001612
Adam Langley1bea1732014-12-17 19:06:57 -08001613int ssl3_send_client_key_exchange(SSL *s) {
1614 uint8_t *p;
1615 int n = 0;
David Benjamin107db582015-04-08 00:41:59 -04001616 uint32_t alg_k;
1617 uint32_t alg_a;
Adam Langley1bea1732014-12-17 19:06:57 -08001618 uint8_t *q;
1619 EVP_PKEY *pkey = NULL;
1620 EC_KEY *clnt_ecdh = NULL;
1621 const EC_POINT *srvr_ecpoint = NULL;
1622 EVP_PKEY *srvr_pub_pkey = NULL;
1623 uint8_t *encodedPoint = NULL;
1624 int encoded_pt_len = 0;
1625 BN_CTX *bn_ctx = NULL;
1626 unsigned int psk_len = 0;
1627 uint8_t psk[PSK_MAX_PSK_LEN];
1628 uint8_t *pms = NULL;
1629 size_t pms_len = 0;
Adam Langley1258b6a2014-06-20 12:00:00 -07001630
Adam Langley1bea1732014-12-17 19:06:57 -08001631 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1632 p = ssl_handshake_start(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001633
Adam Langley1bea1732014-12-17 19:06:57 -08001634 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1635 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001636
Adam Langley1bea1732014-12-17 19:06:57 -08001637 /* If using a PSK key exchange, prepare the pre-shared key. */
1638 if (alg_a & SSL_aPSK) {
1639 char identity[PSK_MAX_IDENTITY_LEN + 1];
1640 size_t identity_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001641
Adam Langley1bea1732014-12-17 19:06:57 -08001642 if (s->psk_client_callback == NULL) {
1643 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1644 SSL_R_PSK_NO_CLIENT_CB);
1645 goto err;
1646 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001647
Adam Langley1bea1732014-12-17 19:06:57 -08001648 memset(identity, 0, sizeof(identity));
1649 psk_len =
1650 s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
1651 sizeof(identity), psk, sizeof(psk));
1652 if (psk_len > PSK_MAX_PSK_LEN) {
1653 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1654 ERR_R_INTERNAL_ERROR);
1655 goto err;
1656 } else if (psk_len == 0) {
1657 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1658 SSL_R_PSK_IDENTITY_NOT_FOUND);
1659 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1660 goto err;
1661 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001662
Adam Langley1bea1732014-12-17 19:06:57 -08001663 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1664 if (identity_len > PSK_MAX_IDENTITY_LEN) {
1665 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1666 ERR_R_INTERNAL_ERROR);
1667 goto err;
1668 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001669
David Benjamin2755a3e2015-04-22 16:17:58 -04001670 OPENSSL_free(s->session->psk_identity);
Adam Langley1bea1732014-12-17 19:06:57 -08001671 s->session->psk_identity = BUF_strdup(identity);
1672 if (s->session->psk_identity == NULL) {
1673 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1674 ERR_R_MALLOC_FAILURE);
1675 goto err;
1676 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001677
Adam Langley1bea1732014-12-17 19:06:57 -08001678 /* Write out psk_identity. */
1679 s2n(identity_len, p);
1680 memcpy(p, identity, identity_len);
1681 p += identity_len;
1682 n = 2 + identity_len;
1683 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001684
Adam Langley1bea1732014-12-17 19:06:57 -08001685 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1686 if (alg_k & SSL_kRSA) {
1687 RSA *rsa;
1688 size_t enc_pms_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001689
Adam Langley1bea1732014-12-17 19:06:57 -08001690 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1691 pms = OPENSSL_malloc(pms_len);
1692 if (pms == NULL) {
1693 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1694 ERR_R_MALLOC_FAILURE);
1695 goto err;
1696 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001697
Adam Langley1bea1732014-12-17 19:06:57 -08001698 if (s->session->sess_cert == NULL) {
1699 /* We should always have a server certificate with SSL_kRSA. */
1700 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1701 ERR_R_INTERNAL_ERROR);
1702 goto err;
1703 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001704
Adam Langley1bea1732014-12-17 19:06:57 -08001705 pkey = X509_get_pubkey(
1706 s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1707 if (pkey == NULL ||
1708 pkey->type != EVP_PKEY_RSA ||
1709 pkey->pkey.rsa == NULL) {
1710 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1711 ERR_R_INTERNAL_ERROR);
David Benjamin2755a3e2015-04-22 16:17:58 -04001712 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001713 goto err;
1714 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001715
Adam Langley1bea1732014-12-17 19:06:57 -08001716 rsa = pkey->pkey.rsa;
1717 EVP_PKEY_free(pkey);
Adam Langley1258b6a2014-06-20 12:00:00 -07001718
Adam Langley1bea1732014-12-17 19:06:57 -08001719 pms[0] = s->client_version >> 8;
1720 pms[1] = s->client_version & 0xff;
1721 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1722 goto err;
1723 }
1724
1725 s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
1726
1727 q = p;
1728 /* In TLS and beyond, reserve space for the length prefix. */
1729 if (s->version > SSL3_VERSION) {
1730 p += 2;
1731 n += 2;
1732 }
1733 if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
1734 RSA_PKCS1_PADDING)) {
1735 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1736 SSL_R_BAD_RSA_ENCRYPT);
1737 goto err;
1738 }
1739 n += enc_pms_len;
1740
1741 /* Log the premaster secret, if logging is enabled. */
1742 if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
1743 pms_len)) {
1744 goto err;
1745 }
1746
1747 /* Fill in the length prefix. */
1748 if (s->version > SSL3_VERSION) {
1749 s2n(enc_pms_len, q);
1750 }
David Benjamin7061e282015-03-19 11:10:48 -04001751 } else if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001752 DH *dh_srvr, *dh_clnt;
1753 SESS_CERT *scert = s->session->sess_cert;
1754 int dh_len;
1755 size_t pub_len;
1756
1757 if (scert == NULL) {
1758 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1759 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1760 SSL_R_UNEXPECTED_MESSAGE);
1761 goto err;
1762 }
1763
1764 if (scert->peer_dh_tmp == NULL) {
1765 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1766 ERR_R_INTERNAL_ERROR);
1767 goto err;
1768 }
1769 dh_srvr = scert->peer_dh_tmp;
1770
1771 /* generate a new random key */
1772 dh_clnt = DHparams_dup(dh_srvr);
1773 if (dh_clnt == NULL) {
1774 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1775 goto err;
1776 }
1777 if (!DH_generate_key(dh_clnt)) {
1778 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1779 DH_free(dh_clnt);
1780 goto err;
1781 }
1782
1783 pms_len = DH_size(dh_clnt);
1784 pms = OPENSSL_malloc(pms_len);
1785 if (pms == NULL) {
1786 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1787 ERR_R_MALLOC_FAILURE);
1788 DH_free(dh_clnt);
1789 goto err;
1790 }
1791
1792 dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1793 if (dh_len <= 0) {
1794 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
1795 DH_free(dh_clnt);
1796 goto err;
1797 }
1798 pms_len = dh_len;
1799
1800 /* send off the data */
1801 pub_len = BN_num_bytes(dh_clnt->pub_key);
1802 s2n(pub_len, p);
1803 BN_bn2bin(dh_clnt->pub_key, p);
1804 n += 2 + pub_len;
1805
1806 DH_free(dh_clnt);
David Benjamin7061e282015-03-19 11:10:48 -04001807 } else if (alg_k & SSL_kECDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001808 const EC_GROUP *srvr_group = NULL;
1809 EC_KEY *tkey;
1810 int field_size = 0, ecdh_len;
1811
1812 if (s->session->sess_cert == NULL) {
1813 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1814 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1815 SSL_R_UNEXPECTED_MESSAGE);
1816 goto err;
1817 }
1818
1819 if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
1820 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1821 ERR_R_INTERNAL_ERROR);
1822 goto err;
1823 }
1824
1825 tkey = s->session->sess_cert->peer_ecdh_tmp;
1826
1827 srvr_group = EC_KEY_get0_group(tkey);
1828 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1829 if (srvr_group == NULL || srvr_ecpoint == NULL) {
1830 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1831 ERR_R_INTERNAL_ERROR);
1832 goto err;
1833 }
1834
1835 clnt_ecdh = EC_KEY_new();
1836 if (clnt_ecdh == NULL) {
1837 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1838 ERR_R_MALLOC_FAILURE);
1839 goto err;
1840 }
1841
1842 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1843 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
1844 goto err;
1845 }
1846
1847 /* Generate a new ECDH key pair */
1848 if (!EC_KEY_generate_key(clnt_ecdh)) {
1849 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1850 goto err;
1851 }
1852
1853 field_size = EC_GROUP_get_degree(srvr_group);
1854 if (field_size <= 0) {
1855 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1856 goto err;
1857 }
1858
1859 pms_len = (field_size + 7) / 8;
1860 pms = OPENSSL_malloc(pms_len);
1861 if (pms == NULL) {
1862 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1863 ERR_R_MALLOC_FAILURE);
1864 goto err;
1865 }
1866
1867 ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
1868 if (ecdh_len <= 0) {
1869 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
1870 goto err;
1871 }
1872 pms_len = ecdh_len;
1873
1874 /* First check the size of encoding and allocate memory accordingly. */
1875 encoded_pt_len =
1876 EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1877 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1878
1879 encodedPoint =
1880 (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
1881 bn_ctx = BN_CTX_new();
1882 if (encodedPoint == NULL || bn_ctx == NULL) {
1883 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1884 ERR_R_MALLOC_FAILURE);
1885 goto err;
1886 }
1887
1888 /* Encode the public key */
1889 encoded_pt_len = EC_POINT_point2oct(
1890 srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
1891 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
1892
1893 *p = encoded_pt_len; /* length of encoded point */
1894 /* Encoded point will be copied here */
1895 p += 1;
1896 n += 1;
1897 /* copy the point */
1898 memcpy(p, encodedPoint, encoded_pt_len);
1899 /* increment n to account for length field */
1900 n += encoded_pt_len;
1901
1902 /* Free allocated memory */
1903 BN_CTX_free(bn_ctx);
1904 bn_ctx = NULL;
1905 OPENSSL_free(encodedPoint);
1906 encodedPoint = NULL;
1907 EC_KEY_free(clnt_ecdh);
1908 clnt_ecdh = NULL;
1909 EVP_PKEY_free(srvr_pub_pkey);
1910 srvr_pub_pkey = NULL;
1911 } else if (alg_k & SSL_kPSK) {
1912 /* For plain PSK, other_secret is a block of 0s with the same length as
1913 * the pre-shared key. */
1914 pms_len = psk_len;
1915 pms = OPENSSL_malloc(pms_len);
1916 if (pms == NULL) {
1917 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1918 ERR_R_MALLOC_FAILURE);
1919 goto err;
1920 }
1921 memset(pms, 0, pms_len);
1922 } else {
1923 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1924 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1925 ERR_R_INTERNAL_ERROR);
1926 goto err;
1927 }
1928
1929 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1930 * key. */
1931 if (alg_a & SSL_aPSK) {
1932 CBB cbb, child;
1933 uint8_t *new_pms;
1934 size_t new_pms_len;
1935
1936 if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
1937 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1938 ERR_R_MALLOC_FAILURE);
1939 goto err;
1940 }
1941 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1942 !CBB_add_bytes(&child, pms, pms_len) ||
1943 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1944 !CBB_add_bytes(&child, psk, psk_len) ||
1945 !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
1946 CBB_cleanup(&cbb);
1947 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
1948 ERR_R_INTERNAL_ERROR);
1949 goto err;
1950 }
1951 OPENSSL_cleanse(pms, pms_len);
1952 OPENSSL_free(pms);
1953 pms = new_pms;
1954 pms_len = new_pms_len;
1955 }
1956
1957 /* The message must be added to the finished hash before calculating the
1958 * master secret. */
David Benjaminfbdfefb2015-02-16 19:33:53 -05001959 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
1960 goto err;
1961 }
Adam Langley1bea1732014-12-17 19:06:57 -08001962 s->state = SSL3_ST_CW_KEY_EXCH_B;
1963
1964 s->session->master_key_length = s->enc_method->generate_master_secret(
1965 s, s->session->master_key, pms, pms_len);
1966 if (s->session->master_key_length == 0) {
1967 goto err;
1968 }
1969 s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
1970 OPENSSL_cleanse(pms, pms_len);
1971 OPENSSL_free(pms);
1972 }
1973
1974 /* SSL3_ST_CW_KEY_EXCH_B */
David Benjamin2fa83de2015-02-08 01:40:08 -05001975 return s->method->do_write(s);
Adam Langley1258b6a2014-06-20 12:00:00 -07001976
1977err:
Adam Langley1bea1732014-12-17 19:06:57 -08001978 BN_CTX_free(bn_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04001979 OPENSSL_free(encodedPoint);
1980 EC_KEY_free(clnt_ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001981 EVP_PKEY_free(srvr_pub_pkey);
1982 if (pms) {
1983 OPENSSL_cleanse(pms, pms_len);
1984 OPENSSL_free(pms);
1985 }
1986 return -1;
1987}
Adam Langley1258b6a2014-06-20 12:00:00 -07001988
Adam Langley1bea1732014-12-17 19:06:57 -08001989int ssl3_send_cert_verify(SSL *s) {
1990 uint8_t *buf, *p;
1991 const EVP_MD *md = NULL;
1992 uint8_t digest[EVP_MAX_MD_SIZE];
1993 size_t digest_length;
1994 EVP_PKEY *pkey;
1995 EVP_PKEY_CTX *pctx = NULL;
1996 size_t signature_length = 0;
1997 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001998
Adam Langley1bea1732014-12-17 19:06:57 -08001999 buf = (uint8_t *)s->init_buf->data;
2000
2001 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2002 p = ssl_handshake_start(s);
2003 pkey = s->cert->key->privatekey;
2004
2005 /* Write out the digest type if needbe. */
2006 if (SSL_USE_SIGALGS(s)) {
2007 md = tls1_choose_signing_digest(s, pkey);
2008 if (!tls12_get_sigandhash(p, pkey, md)) {
2009 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
2010 goto err;
2011 }
2012 p += 2;
2013 n += 2;
2014 }
2015
2016 /* Compute the digest. */
2017 if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
2018 goto err;
2019 }
2020
2021 /* The handshake buffer is no longer necessary. */
2022 if (s->s3->handshake_buffer &&
2023 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2024 goto err;
2025 }
2026
2027 /* Sign the digest. */
2028 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2029 if (pctx == NULL) {
2030 goto err;
2031 }
2032
2033 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2034 if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
2035 !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
2036 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2037 goto err;
2038 }
2039
2040 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
2041 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2042 goto err;
2043 }
2044
2045 if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
2046 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
2047 goto err;
2048 }
2049
2050 s2n(signature_length, p);
2051 n += signature_length + 2;
2052
David Benjaminfbdfefb2015-02-16 19:33:53 -05002053 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2054 goto err;
2055 }
Adam Langley1bea1732014-12-17 19:06:57 -08002056 s->state = SSL3_ST_CW_CERT_VRFY_B;
2057 }
2058
2059 EVP_PKEY_CTX_free(pctx);
2060 return ssl_do_write(s);
2061
2062err:
2063 EVP_PKEY_CTX_free(pctx);
2064 return -1;
2065}
2066
2067/* ssl3_has_client_certificate returns true if a client certificate is
2068 * configured. */
2069static int ssl3_has_client_certificate(SSL *s) {
2070 return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
2071}
2072
2073int ssl3_send_client_certificate(SSL *s) {
2074 X509 *x509 = NULL;
2075 EVP_PKEY *pkey = NULL;
2076 int i;
2077
2078 if (s->state == SSL3_ST_CW_CERT_A) {
2079 /* Let cert callback update client certificates if required */
2080 if (s->cert->cert_cb) {
2081 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2082 if (i < 0) {
2083 s->rwstate = SSL_X509_LOOKUP;
2084 return -1;
2085 }
2086 if (i == 0) {
2087 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2088 return 0;
2089 }
2090 s->rwstate = SSL_NOTHING;
2091 }
2092
2093 if (ssl3_has_client_certificate(s)) {
2094 s->state = SSL3_ST_CW_CERT_C;
2095 } else {
2096 s->state = SSL3_ST_CW_CERT_B;
2097 }
2098 }
2099
2100 /* We need to get a client cert */
2101 if (s->state == SSL3_ST_CW_CERT_B) {
2102 /* If we get an error, we need to:
2103 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2104 * We then get retried later */
2105 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2106 if (i < 0) {
2107 s->rwstate = SSL_X509_LOOKUP;
2108 return -1;
2109 }
2110 s->rwstate = SSL_NOTHING;
2111 if (i == 1 && pkey != NULL && x509 != NULL) {
2112 s->state = SSL3_ST_CW_CERT_B;
2113 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
2114 i = 0;
2115 }
2116 } else if (i == 1) {
2117 i = 0;
2118 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
2119 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2120 }
2121
David Benjamin2755a3e2015-04-22 16:17:58 -04002122 X509_free(x509);
2123 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08002124 if (i && !ssl3_has_client_certificate(s)) {
2125 i = 0;
2126 }
2127 if (i == 0) {
2128 if (s->version == SSL3_VERSION) {
2129 s->s3->tmp.cert_req = 0;
2130 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2131 return 1;
2132 } else {
2133 s->s3->tmp.cert_req = 2;
2134 }
2135 }
2136
2137 /* Ok, we have a cert */
2138 s->state = SSL3_ST_CW_CERT_C;
2139 }
2140
2141 if (s->state == SSL3_ST_CW_CERT_C) {
David Benjamin9d0847a2015-02-16 03:57:55 -05002142 CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
2143 if (!ssl3_output_cert_chain(s, cert_pkey)) {
2144 return -1;
2145 }
Adam Langley1bea1732014-12-17 19:06:57 -08002146 }
2147
2148 /* SSL3_ST_CW_CERT_D */
2149 return ssl_do_write(s);
2150}
2151
2152#define has_bits(i, m) (((i) & (m)) == (m))
2153
2154int ssl3_check_cert_and_algorithm(SSL *s) {
2155 int i, idx;
2156 long alg_k, alg_a;
2157 EVP_PKEY *pkey = NULL;
2158 SESS_CERT *sc;
2159 DH *dh;
2160
2161 /* we don't have a certificate */
2162 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
2163 return 1;
2164 }
2165
2166 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2167 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2168
2169 sc = s->session->sess_cert;
2170 if (sc == NULL) {
2171 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
2172 goto err;
2173 }
2174
2175 dh = s->session->sess_cert->peer_dh_tmp;
2176
2177 /* This is the passed certificate */
2178
2179 idx = sc->peer_cert_type;
2180 if (idx == SSL_PKEY_ECC) {
2181 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
2182 /* check failed */
2183 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
2184 goto f_err;
2185 } else {
2186 return 1;
2187 }
2188 } else if (alg_a & SSL_aECDSA) {
2189 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2190 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2191 goto f_err;
2192 }
2193 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2194 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2195 EVP_PKEY_free(pkey);
2196
2197 /* Check that we have a certificate if we require one */
2198 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2199 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2200 SSL_R_MISSING_RSA_SIGNING_CERT);
2201 goto f_err;
2202 }
2203
2204 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2205 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
2206 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2207 goto f_err;
2208 }
2209
David Benjamin7061e282015-03-19 11:10:48 -04002210 if ((alg_k & SSL_kDHE) &&
Adam Langley1bea1732014-12-17 19:06:57 -08002211 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
2212 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
2213 goto f_err;
2214 }
2215
2216 return 1;
2217
2218f_err:
2219 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2220err:
2221 return 0;
2222}
2223
2224int ssl3_send_next_proto(SSL *s) {
2225 unsigned int len, padding_len;
2226 uint8_t *d, *p;
2227
2228 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2229 len = s->next_proto_negotiated_len;
2230 padding_len = 32 - ((len + 2) % 32);
2231
2232 d = p = ssl_handshake_start(s);
2233 *(p++) = len;
2234 memcpy(p, s->next_proto_negotiated, len);
2235 p += len;
2236 *(p++) = padding_len;
2237 memset(p, 0, padding_len);
2238 p += padding_len;
2239
David Benjaminfbdfefb2015-02-16 19:33:53 -05002240 if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
2241 return -1;
2242 }
Adam Langley1bea1732014-12-17 19:06:57 -08002243 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2244 }
2245
2246 return ssl_do_write(s);
2247}
2248
2249int ssl3_send_channel_id(SSL *s) {
2250 uint8_t *d;
2251 int ret = -1, public_key_len;
2252 EVP_MD_CTX md_ctx;
2253 size_t sig_len;
2254 ECDSA_SIG *sig = NULL;
2255 uint8_t *public_key = NULL, *derp, *der_sig = NULL;
2256
2257 if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
2258 return ssl_do_write(s);
2259 }
2260
2261 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
2262 EVP_PKEY *key = NULL;
2263 s->ctx->channel_id_cb(s, &key);
2264 if (key != NULL) {
2265 s->tlsext_channel_id_private = key;
2266 }
2267 }
2268
2269 if (!s->tlsext_channel_id_private) {
2270 s->rwstate = SSL_CHANNEL_ID_LOOKUP;
2271 return -1;
2272 }
2273 s->rwstate = SSL_NOTHING;
2274
2275 d = ssl_handshake_start(s);
2276 if (s->s3->tlsext_channel_id_new) {
2277 s2n(TLSEXT_TYPE_channel_id_new, d);
2278 } else {
2279 s2n(TLSEXT_TYPE_channel_id, d);
2280 }
2281 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2282
2283 EVP_MD_CTX_init(&md_ctx);
2284
2285 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2286 if (public_key_len <= 0) {
2287 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2288 SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2289 goto err;
2290 }
2291
2292 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2293 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2294 * field elements as 32-byte, big-endian numbers. */
2295 if (public_key_len != 65) {
2296 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2297 goto err;
2298 }
2299 public_key = OPENSSL_malloc(public_key_len);
2300 if (!public_key) {
2301 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2302 goto err;
2303 }
2304
2305 derp = public_key;
2306 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2307
2308 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2309 s->tlsext_channel_id_private) != 1) {
2310 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2311 SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2312 goto err;
2313 }
2314
2315 if (!tls1_channel_id_hash(&md_ctx, s)) {
2316 goto err;
2317 }
2318
2319 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
2320 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2321 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2322 goto err;
2323 }
2324
2325 der_sig = OPENSSL_malloc(sig_len);
2326 if (!der_sig) {
2327 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2328 goto err;
2329 }
2330
2331 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
2333 SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2334 goto err;
2335 }
2336
2337 derp = der_sig;
2338 sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
2339 if (sig == NULL) {
2340 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2341 goto err;
2342 }
2343
2344 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2345 memcpy(d, public_key + 1, 64);
2346 d += 64;
2347 if (!BN_bn2bin_padded(d, 32, sig->r) ||
2348 !BN_bn2bin_padded(d + 32, 32, sig->s)) {
2349 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
2350 goto err;
2351 }
2352
David Benjaminfbdfefb2015-02-16 19:33:53 -05002353 if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
2354 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
2355 goto err;
2356 }
Adam Langley1bea1732014-12-17 19:06:57 -08002357 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2358
2359 ret = ssl_do_write(s);
2360
2361err:
2362 EVP_MD_CTX_cleanup(&md_ctx);
David Benjamin2755a3e2015-04-22 16:17:58 -04002363 OPENSSL_free(public_key);
2364 OPENSSL_free(der_sig);
2365 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08002366
2367 return ret;
2368}
2369
2370int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
2371 int i = 0;
2372 if (s->ctx->client_cert_cb) {
2373 i = s->ctx->client_cert_cb(s, px509, ppkey);
2374 }
2375 return i;
2376}