blob: 13e3ae5d4c027383f15866f04fcccda4515817c0 [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
151#include <stdio.h>
152
153#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400154#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155#include <openssl/rand.h>
156#include <openssl/obj.h>
157#include <openssl/evp.h>
158#include <openssl/mem.h>
159#include <openssl/md5.h>
160#include <openssl/dh.h>
161#include <openssl/bn.h>
162#include <openssl/engine.h>
163#include <openssl/x509.h>
164
165#include "ssl_locl.h"
166#include "../crypto/dh/internal.h"
167
168static const SSL_METHOD *ssl3_get_client_method(int ver);
169
170static const SSL_METHOD *ssl3_get_client_method(int ver)
171 {
172 if (ver == SSL3_VERSION)
173 return(SSLv3_client_method());
174 else
175 return(NULL);
176 }
177
178IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
179 ssl_undefined_function,
180 ssl3_connect,
181 ssl3_get_client_method)
182
183int ssl3_connect(SSL *s)
184 {
185 BUF_MEM *buf=NULL;
186 void (*cb)(const SSL *ssl,int type,int val)=NULL;
187 int ret= -1;
188 int new_state,state,skip=0;
189
190 ERR_clear_error();
191 ERR_clear_system_error();
192
193 if (s->info_callback != NULL)
194 cb=s->info_callback;
195 else if (s->ctx->info_callback != NULL)
196 cb=s->ctx->info_callback;
197
198 s->in_handshake++;
199 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
200
Adam Langley95c29f32014-06-20 12:00:00 -0700201 for (;;)
202 {
203 state=s->state;
204
205 switch(s->state)
206 {
207 case SSL_ST_RENEGOTIATE:
208 s->renegotiate=1;
209 s->state=SSL_ST_CONNECT;
210 s->ctx->stats.sess_connect_renegotiate++;
211 /* break */
212 case SSL_ST_BEFORE:
213 case SSL_ST_CONNECT:
214 case SSL_ST_BEFORE|SSL_ST_CONNECT:
215 case SSL_ST_OK|SSL_ST_CONNECT:
216
217 s->server=0;
218 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
219
220 if ((s->version & 0xff00 ) != 0x0300)
221 {
222 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
223 ret = -1;
224 goto end;
225 }
226
227 /* s->version=SSL3_VERSION; */
228 s->type=SSL_ST_CONNECT;
229
230 if (s->init_buf == NULL)
231 {
232 if ((buf=BUF_MEM_new()) == NULL)
233 {
234 ret= -1;
235 goto end;
236 }
237 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
238 {
239 ret= -1;
240 goto end;
241 }
242 s->init_buf=buf;
243 buf=NULL;
244 }
245
246 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
247
248 /* setup buffing BIO */
249 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
250
251 /* don't push the buffering BIO quite yet */
252
253 ssl3_init_finished_mac(s);
254
255 s->state=SSL3_ST_CW_CLNT_HELLO_A;
256 s->ctx->stats.sess_connect++;
257 s->init_num=0;
258 break;
259
260 case SSL3_ST_CW_CLNT_HELLO_A:
261 case SSL3_ST_CW_CLNT_HELLO_B:
262
263 s->shutdown=0;
264 ret=ssl3_client_hello(s);
265 if (ret <= 0) goto end;
266 s->state=SSL3_ST_CR_SRVR_HELLO_A;
267 s->init_num=0;
268
269 /* turn on buffering for the next lot of output */
270 if (s->bbio != s->wbio)
271 s->wbio=BIO_push(s->bbio,s->wbio);
272
273 break;
274
275 case SSL3_ST_CR_SRVR_HELLO_A:
276 case SSL3_ST_CR_SRVR_HELLO_B:
277 ret=ssl3_get_server_hello(s);
278 if (ret <= 0) goto end;
279
280 if (s->hit)
281 {
David Benjamincb5abad2014-07-25 12:14:28 -0400282 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700283 if (s->tlsext_ticket_expected)
284 {
285 /* receive renewed session ticket */
286 s->state=SSL3_ST_CR_SESSION_TICKET_A;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288 }
289 else
290 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400291 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700292 }
293 s->init_num=0;
294 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400295
Adam Langley95c29f32014-06-20 12:00:00 -0700296 case SSL3_ST_CR_CERT_A:
297 case SSL3_ST_CR_CERT_B:
David Benjamine8f3d662014-07-12 01:10:19 -0400298 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700299 {
300 ret=ssl3_get_server_certificate(s);
301 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700302 if (s->tlsext_status_expected)
303 s->state=SSL3_ST_CR_CERT_STATUS_A;
304 else
305 s->state=SSL3_ST_CR_KEY_EXCH_A;
306 }
307 else
308 {
309 skip = 1;
310 s->state=SSL3_ST_CR_KEY_EXCH_A;
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312 s->init_num=0;
313 break;
314
315 case SSL3_ST_CR_KEY_EXCH_A:
316 case SSL3_ST_CR_KEY_EXCH_B:
David Benjamin8f8040d2014-07-14 19:14:46 -0400317 ret=ssl3_get_server_key_exchange(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (ret <= 0) goto end;
319 s->state=SSL3_ST_CR_CERT_REQ_A;
320 s->init_num=0;
321
322 /* at this point we check that we have the
323 * required stuff from the server */
324 if (!ssl3_check_cert_and_algorithm(s))
325 {
326 ret= -1;
327 goto end;
328 }
329 break;
330
331 case SSL3_ST_CR_CERT_REQ_A:
332 case SSL3_ST_CR_CERT_REQ_B:
333 ret=ssl3_get_certificate_request(s);
334 if (ret <= 0) goto end;
335 s->state=SSL3_ST_CR_SRVR_DONE_A;
336 s->init_num=0;
337 break;
338
339 case SSL3_ST_CR_SRVR_DONE_A:
340 case SSL3_ST_CR_SRVR_DONE_B:
341 ret=ssl3_get_server_done(s);
342 if (ret <= 0) goto end;
343 if (s->s3->tmp.cert_req)
344 s->state=SSL3_ST_CW_CERT_A;
345 else
346 s->state=SSL3_ST_CW_KEY_EXCH_A;
347 s->init_num=0;
348
349 break;
350
351 case SSL3_ST_CW_CERT_A:
352 case SSL3_ST_CW_CERT_B:
353 case SSL3_ST_CW_CERT_C:
354 case SSL3_ST_CW_CERT_D:
355 ret=ssl3_send_client_certificate(s);
356 if (ret <= 0) goto end;
357 s->state=SSL3_ST_CW_KEY_EXCH_A;
358 s->init_num=0;
359 break;
360
361 case SSL3_ST_CW_KEY_EXCH_A:
362 case SSL3_ST_CW_KEY_EXCH_B:
363 ret=ssl3_send_client_key_exchange(s);
364 if (ret <= 0) goto end;
365 /* EAY EAY EAY need to check for DH fix cert
366 * sent back */
367 /* For TLS, cert_req is set to 2, so a cert chain
368 * of nothing is sent, but no verify packet is sent */
369 /* XXX: For now, we do not support client
370 * authentication in ECDH cipher suites with
371 * ECDH (rather than ECDSA) certificates.
372 * We need to skip the certificate verify
373 * message when client's ECDH public key is sent
374 * inside the client certificate.
375 */
376 if (s->s3->tmp.cert_req == 1)
377 {
378 s->state=SSL3_ST_CW_CERT_VRFY_A;
379 }
380 else
381 {
382 s->state=SSL3_ST_CW_CHANGE_A;
383 s->s3->change_cipher_spec=0;
384 }
385 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
386 {
387 s->state=SSL3_ST_CW_CHANGE_A;
388 s->s3->change_cipher_spec=0;
389 }
390
391 s->init_num=0;
392 break;
393
394 case SSL3_ST_CW_CERT_VRFY_A:
395 case SSL3_ST_CW_CERT_VRFY_B:
396 ret=ssl3_send_client_verify(s);
397 if (ret <= 0) goto end;
398 s->state=SSL3_ST_CW_CHANGE_A;
399 s->init_num=0;
400 s->s3->change_cipher_spec=0;
401 break;
402
403 case SSL3_ST_CW_CHANGE_A:
404 case SSL3_ST_CW_CHANGE_B:
405 ret=ssl3_send_change_cipher_spec(s,
406 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
407 if (ret <= 0) goto end;
408
Adam Langley1258b6a2014-06-20 12:00:00 -0700409 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700410 if (s->s3->tlsext_channel_id_valid)
411 s->state=SSL3_ST_CW_CHANNEL_ID_A;
412# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700413 if (s->s3->next_proto_neg_seen)
414 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700415# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700416 s->init_num=0;
417
418 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700419 if (!s->method->ssl3_enc->setup_key_block(s))
420 {
421 ret= -1;
422 goto end;
423 }
424
425 if (!s->method->ssl3_enc->change_cipher_state(s,
426 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
427 {
428 ret= -1;
429 goto end;
430 }
431
432 break;
433
David Benjamin6dbd73d2014-07-03 15:59:49 -0400434#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700435 case SSL3_ST_CW_NEXT_PROTO_A:
436 case SSL3_ST_CW_NEXT_PROTO_B:
437 ret=ssl3_send_next_proto(s);
438 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700439 if (s->s3->tlsext_channel_id_valid)
440 s->state=SSL3_ST_CW_CHANNEL_ID_A;
441 else
442 s->state=SSL3_ST_CW_FINISHED_A;
443 break;
444#endif
445
Adam Langley1258b6a2014-06-20 12:00:00 -0700446 case SSL3_ST_CW_CHANNEL_ID_A:
447 case SSL3_ST_CW_CHANNEL_ID_B:
448 ret=ssl3_send_channel_id(s);
449 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700450 s->state=SSL3_ST_CW_FINISHED_A;
451 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
453 case SSL3_ST_CW_FINISHED_A:
454 case SSL3_ST_CW_FINISHED_B:
455 ret=ssl3_send_finished(s,
456 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
457 s->method->ssl3_enc->client_finished_label,
458 s->method->ssl3_enc->client_finished_label_len);
459 if (ret <= 0) goto end;
460 s->state=SSL3_ST_CW_FLUSH;
461
462 /* clear flags */
463 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
464 if (s->hit)
465 {
466 s->s3->tmp.next_state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700467 }
468 else
469 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700470 /* This is a non-resumption handshake. If it
471 * involves ChannelID, then record the
472 * handshake hashes at this point in the
473 * session so that any resumption of this
474 * session with ChannelID can sign those
475 * hashes. */
476 if (s->s3->tlsext_channel_id_new)
477 {
478 ret = tls1_record_handshake_hashes_for_channel_id(s);
479 if (ret <= 0)
480 goto end;
481 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700482 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
483 && ssl3_can_cutthrough(s)
484 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
485 )
486 {
Adam Langley045cc552014-07-16 10:45:57 -0700487 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700490 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700491 /* Allow NewSessionTicket if ticket expected */
492 if (s->tlsext_ticket_expected)
493 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
494 else
David Benjamincb5abad2014-07-25 12:14:28 -0400495 s->s3->tmp.next_state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497 }
498 s->init_num=0;
499 break;
500
Adam Langley95c29f32014-06-20 12:00:00 -0700501 case SSL3_ST_CR_SESSION_TICKET_A:
502 case SSL3_ST_CR_SESSION_TICKET_B:
503 ret=ssl3_get_new_session_ticket(s);
504 if (ret <= 0) goto end;
David Benjamincb5abad2014-07-25 12:14:28 -0400505 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700506 s->init_num=0;
507 break;
508
509 case SSL3_ST_CR_CERT_STATUS_A:
510 case SSL3_ST_CR_CERT_STATUS_B:
511 ret=ssl3_get_cert_status(s);
512 if (ret <= 0) goto end;
513 s->state=SSL3_ST_CR_KEY_EXCH_A;
514 s->init_num=0;
515 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamincb5abad2014-07-25 12:14:28 -0400517 case SSL3_ST_CR_CHANGE:
518 /* At this point, the next message must be entirely
519 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400520 if (!ssl3_expect_change_cipher_spec(s))
521 {
522 ret = -1;
523 goto end;
524 }
David Benjamincb5abad2014-07-25 12:14:28 -0400525 s->state = SSL3_ST_CR_FINISHED_A;
526 break;
527
Adam Langley95c29f32014-06-20 12:00:00 -0700528 case SSL3_ST_CR_FINISHED_A:
529 case SSL3_ST_CR_FINISHED_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700530 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
531 SSL3_ST_CR_FINISHED_B);
532 if (ret <= 0) goto end;
533
534 if (s->hit)
535 s->state=SSL3_ST_CW_CHANGE_A;
536 else
537 s->state=SSL_ST_OK;
538 s->init_num=0;
539 break;
540
541 case SSL3_ST_CW_FLUSH:
542 s->rwstate=SSL_WRITING;
543 if (BIO_flush(s->wbio) <= 0)
544 {
545 ret= -1;
546 goto end;
547 }
548 s->rwstate=SSL_NOTHING;
549 s->state=s->s3->tmp.next_state;
550 break;
551
Adam Langleyadb739e2014-06-20 12:00:00 -0700552 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700553 /* Allow NewSessionTicket if ticket expected */
554 if (s->tlsext_ticket_expected)
555 s->state=SSL3_ST_CR_SESSION_TICKET_A;
556 else
David Benjamin7e3305e2014-07-28 14:52:32 -0400557 s->state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700558
Adam Langley045cc552014-07-16 10:45:57 -0700559 ssl_free_wbio_buffer(s);
Adam Langleyadb739e2014-06-20 12:00:00 -0700560 ret = 1;
561 goto end;
562 /* break; */
563
Adam Langley95c29f32014-06-20 12:00:00 -0700564 case SSL_ST_OK:
565 /* clean a few things up */
566 ssl3_cleanup_key_block(s);
567
568 if (s->init_buf != NULL)
569 {
570 BUF_MEM_free(s->init_buf);
571 s->init_buf=NULL;
572 }
573
574 /* If we are not 'joining' the last two packets,
575 * remove the buffering now */
576 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
577 ssl_free_wbio_buffer(s);
578 /* else do it later in ssl3_write */
579
580 s->init_num=0;
581 s->renegotiate=0;
582 s->new_session=0;
583
584 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
585 if (s->hit) s->ctx->stats.sess_hit++;
586
587 ret=1;
588 /* s->server=0; */
589 s->handshake_func=ssl3_connect;
590 s->ctx->stats.sess_connect_good++;
591
592 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
593
594 goto end;
595 /* break; */
596
597 default:
598 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
599 ret= -1;
600 goto end;
601 /* break; */
602 }
603
604 /* did we do anything */
605 if (!s->s3->tmp.reuse_message && !skip)
606 {
607 if (s->debug)
608 {
609 if ((ret=BIO_flush(s->wbio)) <= 0)
610 goto end;
611 }
612
613 if ((cb != NULL) && (s->state != state))
614 {
615 new_state=s->state;
616 s->state=state;
617 cb(s,SSL_CB_CONNECT_LOOP,1);
618 s->state=new_state;
619 }
620 }
621 skip=0;
622 }
623end:
624 s->in_handshake--;
625 if (buf != NULL)
626 BUF_MEM_free(buf);
627 if (cb != NULL)
628 cb(s,SSL_CB_CONNECT_EXIT,ret);
629 return(ret);
630 }
631
632
633int ssl3_client_hello(SSL *s)
634 {
635 unsigned char *buf;
636 unsigned char *p,*d;
637 int i;
638 unsigned long l;
639
640 buf=(unsigned char *)s->init_buf->data;
641 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
642 {
643 SSL_SESSION *sess = s->session;
David Benjamin407886f2014-07-21 22:23:50 -0400644 if (sess == NULL ||
645 sess->ssl_version != s->version ||
646 !sess->session_id_length ||
647 sess->not_resumable)
Adam Langley95c29f32014-06-20 12:00:00 -0700648 {
649 if (!ssl_get_new_session(s,0))
650 goto err;
651 }
652 if (s->method->version == DTLS_ANY_VERSION)
653 {
654 /* Determine which DTLS version to use */
655 int options = s->options;
656 /* If DTLS 1.2 disabled correct the version number */
657 if (options & SSL_OP_NO_DTLSv1_2)
658 {
659 if (tls1_suiteb(s))
660 {
661 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
662 goto err;
663 }
664 /* Disabling all versions is silly: return an
665 * error.
666 */
667 if (options & SSL_OP_NO_DTLSv1)
668 {
669 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
670 goto err;
671 }
672 /* Update method so we don't use any DTLS 1.2
673 * features.
674 */
675 s->method = DTLSv1_client_method();
676 s->version = DTLS1_VERSION;
677 }
678 else
679 {
680 /* We only support one version: update method */
681 if (options & SSL_OP_NO_DTLSv1)
682 s->method = DTLSv1_2_client_method();
683 s->version = DTLS1_2_VERSION;
684 }
685 s->client_version = s->version;
686 }
687 /* else use the pre-loaded session */
688
689 p=s->s3->client_random;
690
691 /* for DTLS if client_random is initialized, reuse it, we are
692 * required to use same upon reply to HelloVerify */
693 if (SSL_IS_DTLS(s))
694 {
695 size_t idx;
696 i = 1;
697 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
698 {
699 if (p[idx])
700 {
701 i = 0;
702 break;
703 }
704 }
705 }
706 else
707 i = 1;
708
709 if (i)
710 ssl_fill_hello_random(s, 0, p,
711 sizeof(s->s3->client_random));
712
Adam Langleyb0c235e2014-06-20 12:00:00 -0700713 /* Do the message type and length last.
714 * Note: the final argument to ssl_add_clienthello_tlsext below
715 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700716 d=p= ssl_handshake_start(s);
717
718 /* version indicates the negotiated version: for example from
719 * an SSLv2/v3 compatible client hello). The client_version
720 * field is the maximum version we permit and it is also
721 * used in RSA encrypted premaster secrets. Some servers can
722 * choke if we initially report a higher version then
723 * renegotiate to a lower one in the premaster secret. This
724 * didn't happen with TLS 1.0 as most servers supported it
725 * but it can with TLS 1.1 or later if the server only supports
726 * 1.0.
727 *
728 * Possible scenario with previous logic:
729 * 1. Client hello indicates TLS 1.2
730 * 2. Server hello says TLS 1.0
731 * 3. RSA encrypted premaster secret uses 1.2.
732 * 4. Handhaked proceeds using TLS 1.0.
733 * 5. Server sends hello request to renegotiate.
734 * 6. Client hello indicates TLS v1.0 as we now
735 * know that is maximum server supports.
736 * 7. Server chokes on RSA encrypted premaster secret
737 * containing version 1.0.
738 *
739 * For interoperability it should be OK to always use the
740 * maximum version we support in client hello and then rely
741 * on the checking of version to ensure the servers isn't
742 * being inconsistent: for example initially negotiating with
743 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
744 * client_version in client hello and not resetting it to
745 * the negotiated version.
746 */
747#if 0
748 *(p++)=s->version>>8;
749 *(p++)=s->version&0xff;
750 s->client_version=s->version;
751#else
752 *(p++)=s->client_version>>8;
753 *(p++)=s->client_version&0xff;
754#endif
755
756 /* Random stuff */
757 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
758 p+=SSL3_RANDOM_SIZE;
759
760 /* Session ID */
761 if (s->new_session)
762 i=0;
763 else
764 i=s->session->session_id_length;
765 *(p++)=i;
766 if (i != 0)
767 {
768 if (i > (int)sizeof(s->session->session_id))
769 {
770 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
771 goto err;
772 }
773 memcpy(p,s->session->session_id,i);
774 p+=i;
775 }
776
777 /* cookie stuff for DTLS */
778 if (SSL_IS_DTLS(s))
779 {
780 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
781 {
782 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
783 goto err;
784 }
785 *(p++) = s->d1->cookie_len;
786 memcpy(p, s->d1->cookie, s->d1->cookie_len);
787 p += s->d1->cookie_len;
788 }
789
790 /* Ciphers supported */
David Benjamin39482a12014-07-20 13:30:15 -0400791 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
Adam Langley95c29f32014-06-20 12:00:00 -0700792 if (i == 0)
793 {
794 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
795 goto err;
796 }
797#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
798 /* Some servers hang if client hello > 256 bytes
799 * as hack workaround chop number of supported ciphers
800 * to keep it well below this if we use TLS v1.2
801 */
802 if (TLS1_get_version(s) >= TLS1_2_VERSION
803 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
804 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
805#endif
806 s2n(i,p);
807 p+=i;
808
809 /* COMPRESSION */
810 *(p++)=1;
811 *(p++)=0; /* Add the NULL method */
812
Adam Langley95c29f32014-06-20 12:00:00 -0700813 /* TLS extensions*/
814 if (ssl_prepare_clienthello_tlsext(s) <= 0)
815 {
816 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
817 goto err;
818 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700819 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700820 {
821 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
822 goto err;
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
825 l= p-d;
826 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
827 s->state=SSL3_ST_CW_CLNT_HELLO_B;
828 }
829
830 /* SSL3_ST_CW_CLNT_HELLO_B */
831 return ssl_do_write(s);
832err:
833 return(-1);
834 }
835
836int ssl3_get_server_hello(SSL *s)
837 {
838 STACK_OF(SSL_CIPHER) *sk;
839 const SSL_CIPHER *c;
840 CERT *ct = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -0700841 int al=SSL_AD_INTERNAL_ERROR,ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700842 long n;
David Benjamina03d95d2014-07-12 19:49:07 -0400843 CBS server_hello, server_random, session_id;
David Benjamin39482a12014-07-20 13:30:15 -0400844 uint16_t server_version, cipher_suite;
David Benjamina03d95d2014-07-12 19:49:07 -0400845 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700846 /* Hello verify request and/or server hello version may not
847 * match so set first packet if we're negotiating version.
848 */
849 if (SSL_IS_DTLS(s))
850 s->first_packet = 1;
851
852 n=s->method->ssl_get_message(s,
853 SSL3_ST_CR_SRVR_HELLO_A,
854 SSL3_ST_CR_SRVR_HELLO_B,
855 -1,
856 20000, /* ?? */
857 &ok);
858
859 if (!ok) return((int)n);
860
861 if (SSL_IS_DTLS(s))
862 {
863 s->first_packet = 0;
864 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
865 {
866 if ( s->d1->send_cookie == 0)
867 {
868 s->s3->tmp.reuse_message = 1;
869 return 1;
870 }
871 else /* already sent a cookie */
872 {
873 al=SSL_AD_UNEXPECTED_MESSAGE;
874 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
875 goto f_err;
876 }
877 }
878 }
879
880 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
881 {
882 al=SSL_AD_UNEXPECTED_MESSAGE;
883 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
884 goto f_err;
885 }
886
David Benjamina03d95d2014-07-12 19:49:07 -0400887 CBS_init(&server_hello, s->init_msg, n);
888
889 if (!CBS_get_u16(&server_hello, &server_version) ||
890 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
891 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
David Benjamin39482a12014-07-20 13:30:15 -0400892 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
893 !CBS_get_u16(&server_hello, &cipher_suite) ||
894 !CBS_get_u8(&server_hello, &compression_method))
David Benjamina03d95d2014-07-12 19:49:07 -0400895 {
896 al = SSL_AD_DECODE_ERROR;
897 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
898 goto f_err;
899 }
900
Adam Langley95c29f32014-06-20 12:00:00 -0700901 if (s->method->version == DTLS_ANY_VERSION)
902 {
903 /* Work out correct protocol version to use */
Adam Langley95c29f32014-06-20 12:00:00 -0700904 int options = s->options;
David Benjamina03d95d2014-07-12 19:49:07 -0400905 if (server_version == DTLS1_2_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700906 && !(options & SSL_OP_NO_DTLSv1_2))
907 s->method = DTLSv1_2_client_method();
908 else if (tls1_suiteb(s))
909 {
910 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
David Benjamina03d95d2014-07-12 19:49:07 -0400911 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700912 al = SSL_AD_PROTOCOL_VERSION;
913 goto f_err;
914 }
David Benjamina03d95d2014-07-12 19:49:07 -0400915 else if (server_version == DTLS1_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700916 && !(options & SSL_OP_NO_DTLSv1))
917 s->method = DTLSv1_client_method();
918 else
919 {
920 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400921 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700922 al = SSL_AD_PROTOCOL_VERSION;
923 goto f_err;
924 }
925 s->version = s->client_version = s->method->version;
926 }
927
David Benjamina03d95d2014-07-12 19:49:07 -0400928 if (server_version != s->version)
Adam Langley95c29f32014-06-20 12:00:00 -0700929 {
930 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400931 s->version = (s->version & 0xff00) | (server_version & 0xff);
932 al = SSL_AD_PROTOCOL_VERSION;
Adam Langley95c29f32014-06-20 12:00:00 -0700933 goto f_err;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935
David Benjamina03d95d2014-07-12 19:49:07 -0400936 /* Copy over the server random. */
937 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700938
David Benjamin9eaeef82014-07-21 22:22:02 -0400939 s->hit = 0;
940
Adam Langley95c29f32014-06-20 12:00:00 -0700941 /* check if we want to resume the session based on external pre-shared secret */
942 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
943 {
944 SSL_CIPHER *pref_cipher=NULL;
945 s->session->master_key_length=sizeof(s->session->master_key);
946 if (s->tls_session_secret_cb(s, s->session->master_key,
947 &s->session->master_key_length,
948 NULL, &pref_cipher,
949 s->tls_session_secret_cb_arg))
950 {
951 s->session->cipher = pref_cipher ?
David Benjamina03d95d2014-07-12 19:49:07 -0400952 pref_cipher :
David Benjamin39482a12014-07-20 13:30:15 -0400953 ssl3_get_cipher_by_value(cipher_suite);
David Benjamin9eaeef82014-07-21 22:22:02 -0400954 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700955 }
956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
David Benjamin9eaeef82014-07-21 22:22:02 -0400958 if (!s->hit && CBS_len(&session_id) != 0 &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400959 CBS_mem_equal(&session_id,
960 s->session->session_id, s->session->session_id_length))
Adam Langley95c29f32014-06-20 12:00:00 -0700961 {
David Benjamina03d95d2014-07-12 19:49:07 -0400962 if(s->sid_ctx_length != s->session->sid_ctx_length
963 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
964 {
965 /* actually a client application bug */
966 al = SSL_AD_ILLEGAL_PARAMETER;
967 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
968 goto f_err;
969 }
David Benjamina03d95d2014-07-12 19:49:07 -0400970 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700971 }
David Benjamin9eaeef82014-07-21 22:22:02 -0400972
973 /* a miss or crap from the other end */
974 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -0700975 {
976 /* If we were trying for session-id reuse, make a new
977 * SSL_SESSION so we don't stuff up other people */
Adam Langley95c29f32014-06-20 12:00:00 -0700978 if (s->session->session_id_length > 0)
979 {
980 if (!ssl_get_new_session(s,0))
981 {
982 goto f_err;
983 }
984 }
David Benjamina03d95d2014-07-12 19:49:07 -0400985 /* Note: session_id could be empty. */
986 s->session->session_id_length = CBS_len(&session_id);
987 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
Adam Langley95c29f32014-06-20 12:00:00 -0700988 }
David Benjamina03d95d2014-07-12 19:49:07 -0400989
David Benjamin39482a12014-07-20 13:30:15 -0400990 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -0700991 if (c == NULL)
992 {
993 /* unknown cipher */
David Benjamina03d95d2014-07-12 19:49:07 -0400994 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
996 goto f_err;
997 }
998 /* If it is a disabled cipher we didn't send it in client hello,
999 * so return an error.
1000 */
1001 if (c->algorithm_ssl & ct->mask_ssl ||
1002 c->algorithm_mkey & ct->mask_k ||
1003 c->algorithm_auth & ct->mask_a)
1004 {
1005 al=SSL_AD_ILLEGAL_PARAMETER;
1006 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1007 goto f_err;
1008 }
Adam Langley95c29f32014-06-20 12:00:00 -07001009
1010 sk=ssl_get_ciphers_by_id(s);
1011 if (!sk_SSL_CIPHER_find(sk, NULL, c))
1012 {
1013 /* we did not say we would use this cipher */
1014 al=SSL_AD_ILLEGAL_PARAMETER;
1015 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1016 goto f_err;
1017 }
1018
1019 /* Depending on the session caching (internal/external), the cipher
1020 and/or cipher_id values may not be set. Make sure that
1021 cipher_id is set and use it for comparison. */
1022 if (s->session->cipher)
1023 s->session->cipher_id = s->session->cipher->id;
1024 if (s->hit && (s->session->cipher_id != c->id))
1025 {
David Benjamina03d95d2014-07-12 19:49:07 -04001026 al = SSL_AD_ILLEGAL_PARAMETER;
1027 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1028 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001029 }
1030 s->s3->tmp.new_cipher=c;
1031 /* Don't digest cached records if no sigalgs: we may need them for
1032 * client authentication.
1033 */
1034 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1035 goto f_err;
David Benjamina03d95d2014-07-12 19:49:07 -04001036
David Benjamina03d95d2014-07-12 19:49:07 -04001037 /* Only the NULL compression algorithm is supported. */
1038 if (compression_method != 0)
1039 {
1040 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001041 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1042 goto f_err;
1043 }
Adam Langley95c29f32014-06-20 12:00:00 -07001044
David Benjamina03d95d2014-07-12 19:49:07 -04001045 /* TLS extensions */
1046 if (!ssl_parse_serverhello_tlsext(s, &server_hello))
Adam Langley95c29f32014-06-20 12:00:00 -07001047 {
1048 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
1049 goto err;
1050 }
Adam Langley95c29f32014-06-20 12:00:00 -07001051
David Benjamin03973092014-06-24 23:27:17 -04001052 /* There should be nothing left over in the record. */
David Benjamina03d95d2014-07-12 19:49:07 -04001053 if (CBS_len(&server_hello) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001054 {
1055 /* wrong packet length */
1056 al=SSL_AD_DECODE_ERROR;
1057 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
1058 goto f_err;
1059 }
1060
1061 return(1);
1062f_err:
1063 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1064err:
1065 return(-1);
1066 }
1067
1068int ssl3_get_server_certificate(SSL *s)
1069 {
1070 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001071 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001072 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001073 STACK_OF(X509) *sk=NULL;
1074 SESS_CERT *sc;
1075 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001076 CBS cbs, certificate_list;
1077 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001078
1079 n=s->method->ssl_get_message(s,
1080 SSL3_ST_CR_CERT_A,
1081 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001082 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001083 s->max_cert_list,
1084 &ok);
1085
1086 if (!ok) return((int)n);
1087
David Benjamin51b1f742014-07-12 16:31:12 -04001088 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001089
1090 if ((sk=sk_X509_new_null()) == NULL)
1091 {
1092 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1093 goto err;
1094 }
1095
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001096 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1097 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001098 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001099 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001100 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1101 goto f_err;
1102 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001103
1104 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001106 CBS certificate;
1107 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001108 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001109 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001110 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1111 goto f_err;
1112 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001113 data = CBS_data(&certificate);
1114 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001115 if (x == NULL)
1116 {
1117 al=SSL_AD_BAD_CERTIFICATE;
1118 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1119 goto f_err;
1120 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001121 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001122 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001123 al = SSL_AD_INTERNAL_ERROR;
1124 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1125 goto f_err;
1126 }
1127 if (CBS_len(&certificate) != 0)
1128 {
1129 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001130 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1131 goto f_err;
1132 }
1133 if (!sk_X509_push(sk,x))
1134 {
1135 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1136 goto err;
1137 }
1138 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001139 }
1140
1141 i=ssl_verify_cert_chain(s,sk);
1142 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1143 )
1144 {
1145 al=ssl_verify_alarm_type(s->verify_result);
1146 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1147 goto f_err;
1148 }
1149 ERR_clear_error(); /* but we keep s->verify_result */
1150
1151 sc=ssl_sess_cert_new();
1152 if (sc == NULL) goto err;
1153
1154 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1155 s->session->sess_cert=sc;
1156
1157 sc->cert_chain=sk;
1158 /* Inconsistency alert: cert_chain does include the peer's
1159 * certificate, which we don't include in s3_srvr.c */
1160 x=sk_X509_value(sk,0);
1161 sk=NULL;
1162 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1163
1164 pkey=X509_get_pubkey(x);
1165
David Benjamind26aea62014-07-12 00:13:56 -04001166 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001167 {
1168 x=NULL;
1169 al=SSL3_AL_FATAL;
1170 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1171 goto f_err;
1172 }
1173
1174 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001175 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001176 {
1177 x=NULL;
1178 al=SSL3_AL_FATAL;
1179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1180 goto f_err;
1181 }
1182
David Benjamind26aea62014-07-12 00:13:56 -04001183 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1184 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001185 {
David Benjamind26aea62014-07-12 00:13:56 -04001186 x=NULL;
1187 al=SSL_AD_ILLEGAL_PARAMETER;
1188 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1189 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
David Benjamind26aea62014-07-12 00:13:56 -04001191 sc->peer_cert_type=i;
1192 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1193 /* Why would the following ever happen?
1194 * We just created sc a couple of lines ago. */
1195 if (sc->peer_pkeys[i].x509 != NULL)
1196 X509_free(sc->peer_pkeys[i].x509);
1197 sc->peer_pkeys[i].x509=x;
1198 sc->peer_key= &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001199
David Benjamind26aea62014-07-12 00:13:56 -04001200 if (s->session->peer != NULL)
1201 X509_free(s->session->peer);
1202 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1203 s->session->peer=x;
1204
Adam Langley95c29f32014-06-20 12:00:00 -07001205 s->session->verify_result = s->verify_result;
1206
1207 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001208 ret=1;
1209 if (0)
1210 {
1211f_err:
1212 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1213 }
1214err:
1215 EVP_PKEY_free(pkey);
1216 X509_free(x);
1217 sk_X509_pop_free(sk,X509_free);
1218 return(ret);
1219 }
1220
David Benjamin8f8040d2014-07-14 19:14:46 -04001221int ssl3_get_server_key_exchange(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07001222 {
Adam Langley95c29f32014-06-20 12:00:00 -07001223 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001224 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001225 long n,alg_k,alg_a;
1226 EVP_PKEY *pkey=NULL;
1227 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001228 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001229#ifndef OPENSSL_NO_DH
1230 DH *dh=NULL;
1231#endif
1232#ifndef OPENSSL_NO_ECDH
1233 EC_KEY *ecdh = NULL;
1234 BN_CTX *bn_ctx = NULL;
1235 EC_POINT *srvr_ecpoint = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001236#endif
David Benjamined439582014-07-14 19:13:02 -04001237 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001238
1239 /* use same message size as in ssl3_get_certificate_request()
1240 * as ServerKeyExchange message may be skipped */
1241 n=s->method->ssl_get_message(s,
1242 SSL3_ST_CR_KEY_EXCH_A,
1243 SSL3_ST_CR_KEY_EXCH_B,
1244 -1,
1245 s->max_cert_list,
1246 &ok);
1247 if (!ok) return((int)n);
1248
1249 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1250 {
David Benjamin9c651c92014-07-12 13:27:45 -04001251 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1252 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001253 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin9c651c92014-07-12 13:27:45 -04001254 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1255 return -1;
1256 }
1257
Adam Langley95c29f32014-06-20 12:00:00 -07001258 /* In plain PSK ciphersuite, ServerKeyExchange can be
1259 omitted if no identity hint is sent. Set
1260 session->sess_cert anyway to avoid problems
1261 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001262 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001263 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001264 /* PSK ciphersuites that also send a
1265 * Certificate would have already initialized
1266 * |sess_cert|. */
1267 if (s->session->sess_cert == NULL)
1268 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001269 if (s->session->psk_identity_hint)
1270 {
1271 OPENSSL_free(s->session->psk_identity_hint);
1272 s->session->psk_identity_hint = NULL;
1273 }
Adam Langley95c29f32014-06-20 12:00:00 -07001274 }
Adam Langley95c29f32014-06-20 12:00:00 -07001275 s->s3->tmp.reuse_message=1;
1276 return(1);
1277 }
1278
David Benjamined439582014-07-14 19:13:02 -04001279 /* Retain a copy of the original CBS to compute the signature
1280 * over. */
1281 CBS_init(&server_key_exchange, s->init_msg, n);
1282 server_key_exchange_orig = server_key_exchange;
1283
Adam Langley95c29f32014-06-20 12:00:00 -07001284 if (s->session->sess_cert != NULL)
1285 {
Adam Langley95c29f32014-06-20 12:00:00 -07001286 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1287 {
1288 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1289 s->session->sess_cert->peer_rsa_tmp=NULL;
1290 }
Adam Langley95c29f32014-06-20 12:00:00 -07001291#ifndef OPENSSL_NO_DH
1292 if (s->session->sess_cert->peer_dh_tmp)
1293 {
1294 DH_free(s->session->sess_cert->peer_dh_tmp);
1295 s->session->sess_cert->peer_dh_tmp=NULL;
1296 }
1297#endif
1298#ifndef OPENSSL_NO_ECDH
1299 if (s->session->sess_cert->peer_ecdh_tmp)
1300 {
1301 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1302 s->session->sess_cert->peer_ecdh_tmp=NULL;
1303 }
1304#endif
1305 }
1306 else
1307 {
1308 s->session->sess_cert=ssl_sess_cert_new();
1309 }
1310
Adam Langley95c29f32014-06-20 12:00:00 -07001311 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1312 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1313 EVP_MD_CTX_init(&md_ctx);
1314
Adam Langleyc26c8022014-06-20 12:00:00 -07001315 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001316 {
David Benjamined439582014-07-14 19:13:02 -04001317 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001318
David Benjamined439582014-07-14 19:13:02 -04001319 /* Each of the PSK key exchanges begins with a
1320 * psk_identity_hint. */
1321 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001322 {
David Benjamined439582014-07-14 19:13:02 -04001323 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001324 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001325 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001326 }
Adam Langley95c29f32014-06-20 12:00:00 -07001327
David Benjamined439582014-07-14 19:13:02 -04001328 /* Store PSK identity hint for later use, hint is used in
1329 * ssl3_send_client_key_exchange. Assume that the maximum
1330 * length of a PSK identity hint can be as long as the maximum
1331 * length of a PSK identity. Also do not allow NULL
1332 * characters; identities are saved as C strings.
1333 *
1334 * TODO(davidben): Should invalid hints be ignored? It's a hint
1335 * rather than a specific identity. */
1336 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1337 CBS_contains_zero_byte(&psk_identity_hint))
1338 {
1339 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001340 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamined439582014-07-14 19:13:02 -04001341 goto f_err;
1342 }
1343
1344 /* Save the identity hint as a C string. */
1345 if (!CBS_strdup(&psk_identity_hint, &s->session->psk_identity_hint))
1346 {
1347 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001348 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001349 goto f_err;
1350 }
Adam Langley95c29f32014-06-20 12:00:00 -07001351 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001352
1353 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001354 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001355 {
David Benjamined439582014-07-14 19:13:02 -04001356 CBS rsa_modulus, rsa_exponent;
1357
1358 /* TODO(davidben): This was originally for export
1359 * reasons. Do we still need to support it? */
1360
1361 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_modulus) ||
1362 CBS_len(&rsa_modulus) == 0 ||
1363 !CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_exponent) ||
1364 CBS_len(&rsa_exponent) == 0)
1365 {
1366 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001367 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001368 goto f_err;
1369 }
1370
Adam Langley95c29f32014-06-20 12:00:00 -07001371 if ((rsa=RSA_new()) == NULL)
1372 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001373 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001374 goto err;
1375 }
Adam Langley95c29f32014-06-20 12:00:00 -07001376
David Benjamined439582014-07-14 19:13:02 -04001377 if (!(rsa->n = BN_bin2bn(CBS_data(&rsa_modulus),
1378 CBS_len(&rsa_modulus), rsa->n)))
Adam Langley95c29f32014-06-20 12:00:00 -07001379 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001380 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001381 goto err;
1382 }
David Benjamined439582014-07-14 19:13:02 -04001383
1384 if (!(rsa->e = BN_bin2bn(CBS_data(&rsa_exponent),
1385 CBS_len(&rsa_exponent), rsa->e)))
1386 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001387 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001388 goto err;
1389 }
Adam Langley95c29f32014-06-20 12:00:00 -07001390
1391 /* this should be because we are using an export cipher */
1392 if (alg_a & SSL_aRSA)
1393 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1394 else
1395 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001396 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001397 goto err;
1398 }
1399 s->session->sess_cert->peer_rsa_tmp=rsa;
1400 rsa=NULL;
1401 }
Adam Langley95c29f32014-06-20 12:00:00 -07001402#ifndef OPENSSL_NO_DH
1403 else if (alg_k & SSL_kEDH)
1404 {
David Benjamined439582014-07-14 19:13:02 -04001405 CBS dh_p, dh_g, dh_Ys;
1406
1407 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1408 CBS_len(&dh_p) == 0 ||
1409 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1410 CBS_len(&dh_g) == 0 ||
1411 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1412 CBS_len(&dh_Ys) == 0)
1413 {
1414 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001415 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001416 goto f_err;
1417 }
1418
Adam Langley95c29f32014-06-20 12:00:00 -07001419 if ((dh=DH_new()) == NULL)
1420 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001421 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001422 goto err;
1423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
David Benjamined439582014-07-14 19:13:02 -04001425 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001426 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001427 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001428 goto err;
1429 }
David Benjamined439582014-07-14 19:13:02 -04001430 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001431 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001432 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001433 goto err;
1434 }
David Benjamined439582014-07-14 19:13:02 -04001435 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1436 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001437 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001438 goto err;
1439 }
Adam Langley95c29f32014-06-20 12:00:00 -07001440
Adam Langley95c29f32014-06-20 12:00:00 -07001441 if (alg_a & SSL_aRSA)
1442 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001443#ifndef OPENSSL_NO_DSA
1444 else if (alg_a & SSL_aDSS)
1445 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1446#endif
1447 /* else anonymous DH, so no certificate or pkey. */
1448
1449 s->session->sess_cert->peer_dh_tmp=dh;
1450 dh=NULL;
1451 }
1452 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1453 {
1454 al=SSL_AD_ILLEGAL_PARAMETER;
David Benjamin8f8040d2014-07-14 19:14:46 -04001455 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
Adam Langley95c29f32014-06-20 12:00:00 -07001456 goto f_err;
1457 }
1458#endif /* !OPENSSL_NO_DH */
1459
1460#ifndef OPENSSL_NO_ECDH
1461 else if (alg_k & SSL_kEECDH)
1462 {
David Benjamined439582014-07-14 19:13:02 -04001463 uint16_t curve_id;
1464 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001465 EC_GROUP *ngroup;
1466 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001467 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001468
David Benjamined439582014-07-14 19:13:02 -04001469 /* Extract elliptic curve parameters and the server's
1470 * ephemeral ECDH public key. Check curve is one of
1471 * our preferences, if not server has sent an invalid
1472 * curve.
1473 */
1474 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001475 {
David Benjamined439582014-07-14 19:13:02 -04001476 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001477 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
Adam Langley95c29f32014-06-20 12:00:00 -07001478 goto f_err;
1479 }
1480
David Benjamined439582014-07-14 19:13:02 -04001481 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001482 {
1483 al=SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001484 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley95c29f32014-06-20 12:00:00 -07001485 goto f_err;
1486 }
1487
David Benjamined439582014-07-14 19:13:02 -04001488 if ((ecdh=EC_KEY_new()) == NULL)
1489 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001490 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001491 goto err;
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1494 if (ngroup == NULL)
1495 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001496 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001497 goto err;
1498 }
1499 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1500 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001501 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001502 goto err;
1503 }
1504 EC_GROUP_free(ngroup);
1505
1506 group = EC_KEY_get0_group(ecdh);
1507
Adam Langley95c29f32014-06-20 12:00:00 -07001508 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001509 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1510 {
1511 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001512 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001513 goto f_err;
1514 }
1515
Adam Langley95c29f32014-06-20 12:00:00 -07001516 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1517 ((bn_ctx = BN_CTX_new()) == NULL))
1518 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001519 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001520 goto err;
1521 }
1522
David Benjamined439582014-07-14 19:13:02 -04001523 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1524 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001525 {
David Benjamined439582014-07-14 19:13:02 -04001526 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001527 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
Adam Langley95c29f32014-06-20 12:00:00 -07001528 goto f_err;
1529 }
1530
Adam Langley95c29f32014-06-20 12:00:00 -07001531 /* The ECC/TLS specification does not mention
1532 * the use of DSA to sign ECParameters in the server
1533 * key exchange message. We do support RSA and ECDSA.
1534 */
1535 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001536 else if (alg_a & SSL_aRSA)
1537 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001538#ifndef OPENSSL_NO_ECDSA
1539 else if (alg_a & SSL_aECDSA)
1540 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1541#endif
1542 /* else anonymous ECDH, so no certificate or pkey. */
1543 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1544 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1545 ecdh=NULL;
1546 BN_CTX_free(bn_ctx);
1547 bn_ctx = NULL;
1548 EC_POINT_free(srvr_ecpoint);
1549 srvr_ecpoint = NULL;
1550 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001551#endif /* !OPENSSL_NO_ECDH */
1552
1553 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001554 {
1555 al=SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001556 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001557 goto f_err;
1558 }
Adam Langley95c29f32014-06-20 12:00:00 -07001559
David Benjamined439582014-07-14 19:13:02 -04001560 /* At this point, |server_key_exchange| contains the
1561 * signature, if any, while |server_key_exchange_orig|
1562 * contains the entire message. From that, derive a CBS
1563 * containing just the parameter. */
1564 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1565 CBS_len(&server_key_exchange_orig) -
1566 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001567
1568 /* if it was signed, check the signature */
1569 if (pkey != NULL)
1570 {
David Benjamined439582014-07-14 19:13:02 -04001571 CBS signature;
1572
Adam Langley95c29f32014-06-20 12:00:00 -07001573 if (SSL_USE_SIGALGS(s))
1574 {
David Benjamin05da6e12014-07-12 20:42:55 -04001575 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
David Benjamined439582014-07-14 19:13:02 -04001576 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001577 }
1578 else
1579 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001580
David Benjamined439582014-07-14 19:13:02 -04001581 /* The last field in |server_key_exchange| is the
1582 * signature. */
1583 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1584 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001585 {
David Benjamined439582014-07-14 19:13:02 -04001586 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001587 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001588 goto f_err;
1589 }
1590
Adam Langley95c29f32014-06-20 12:00:00 -07001591 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1592 {
1593 int num;
David Benjamined439582014-07-14 19:13:02 -04001594 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1595 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langley95c29f32014-06-20 12:00:00 -07001597 q=md_buf;
1598 for (num=2; num > 0; num--)
1599 {
David Benjamined439582014-07-14 19:13:02 -04001600 unsigned int digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001601 EVP_DigestInit_ex(&md_ctx,(num == 2)
1602 ?s->ctx->md5:s->ctx->sha1, NULL);
1603 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1604 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001605 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1606 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1607 q += digest_len;
1608 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001609 }
David Benjamined439582014-07-14 19:13:02 -04001610 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1611 CBS_data(&signature), CBS_len(&signature),
1612 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001613 {
David Benjamined439582014-07-14 19:13:02 -04001614 al = SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001615 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001616 goto f_err;
1617 }
1618 }
1619 else
Adam Langley95c29f32014-06-20 12:00:00 -07001620 {
1621 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1622 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1623 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001624 EVP_VerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1625 if (EVP_VerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature), pkey) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001626 {
1627 /* bad signature */
1628 al=SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001629 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001630 goto f_err;
1631 }
1632 }
1633 }
1634 else
1635 {
David Benjamine8f3d662014-07-12 01:10:19 -04001636 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001637 {
1638 /* Might be wrong key type, check it */
1639 if (ssl3_check_cert_and_algorithm(s))
1640 /* Otherwise this shouldn't happen */
David Benjamin8f8040d2014-07-14 19:14:46 -04001641 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001642 goto err;
1643 }
1644 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001645 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001646 {
1647 al=SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001648 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001649 goto f_err;
1650 }
1651 }
1652 EVP_PKEY_free(pkey);
1653 EVP_MD_CTX_cleanup(&md_ctx);
1654 return(1);
1655f_err:
1656 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1657err:
1658 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001659 if (rsa != NULL)
1660 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001661#ifndef OPENSSL_NO_DH
1662 if (dh != NULL)
1663 DH_free(dh);
1664#endif
1665#ifndef OPENSSL_NO_ECDH
1666 BN_CTX_free(bn_ctx);
1667 EC_POINT_free(srvr_ecpoint);
1668 if (ecdh != NULL)
1669 EC_KEY_free(ecdh);
1670#endif
1671 EVP_MD_CTX_cleanup(&md_ctx);
1672 return(-1);
1673 }
1674
1675static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1676 {
1677 return(X509_NAME_cmp(*a,*b));
1678 }
1679
1680int ssl3_get_certificate_request(SSL *s)
1681 {
1682 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001683 unsigned long n;
1684 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001685 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001686 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001687 CBS cbs;
1688 CBS certificate_types;
1689 CBS certificate_authorities;
1690 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001691
1692 n=s->method->ssl_get_message(s,
1693 SSL3_ST_CR_CERT_REQ_A,
1694 SSL3_ST_CR_CERT_REQ_B,
1695 -1,
1696 s->max_cert_list,
1697 &ok);
1698
1699 if (!ok) return((int)n);
1700
1701 s->s3->tmp.cert_req=0;
1702
1703 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1704 {
1705 s->s3->tmp.reuse_message=1;
1706 /* If we get here we don't need any cached handshake records
1707 * as we wont be doing client auth.
1708 */
1709 if (s->s3->handshake_buffer)
1710 {
1711 if (!ssl3_digest_cached_records(s))
1712 goto err;
1713 }
1714 return(1);
1715 }
1716
1717 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1718 {
1719 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1720 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1721 goto err;
1722 }
1723
1724 /* TLS does not like anon-DH with client cert */
1725 if (s->version > SSL3_VERSION)
1726 {
1727 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1728 {
1729 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1730 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1731 goto err;
1732 }
1733 }
1734
David Benjamin51b1f742014-07-12 16:31:12 -04001735 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001736
1737 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1738 if (ca_sk == NULL)
1739 {
1740 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1741 goto err;
1742 }
1743
1744 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001745 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001746 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001747 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1748 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1749 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001750 }
David Benjamin676d1e72014-07-08 14:34:10 -04001751 if (!CBS_stow(&certificate_types,
1752 &s->s3->tmp.certificate_types,
1753 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001754 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001755 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1756 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001757 }
Adam Langley95c29f32014-06-20 12:00:00 -07001758 if (SSL_USE_SIGALGS(s))
1759 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001760 CBS supported_signature_algorithms;
1761 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001762 {
1763 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001764 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001765 goto err;
1766 }
1767 /* Clear certificate digests and validity flags */
1768 for (i = 0; i < SSL_PKEY_NUM; i++)
1769 {
1770 s->cert->pkeys[i].digest = NULL;
1771 s->cert->pkeys[i].valid_flags = 0;
1772 }
David Benjamincd996942014-07-20 16:23:51 -04001773 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001774 {
1775 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1776 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1777 goto err;
1778 }
Adam Langley95c29f32014-06-20 12:00:00 -07001779 }
1780
1781 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001782 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001783 {
1784 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1785 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1786 goto err;
1787 }
1788
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001789 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001790 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001791 CBS distinguished_name;
1792 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001793 {
Adam Langley95c29f32014-06-20 12:00:00 -07001794 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1795 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1796 goto err;
1797 }
1798
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001799 data = CBS_data(&distinguished_name);
1800 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001801 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001802 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1803 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1804 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001805 }
1806
David Benjamin407a10c2014-07-16 12:58:59 -04001807 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001808 {
1809 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1810 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1811 goto err;
1812 }
1813 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001814 {
1815 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1816 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1817 goto err;
1818 }
1819 if (!sk_X509_NAME_push(ca_sk,xn))
1820 {
1821 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1822 goto err;
1823 }
Adam Langley95c29f32014-06-20 12:00:00 -07001824 }
1825
Adam Langley95c29f32014-06-20 12:00:00 -07001826 /* we should setup a certificate to return.... */
1827 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001828 if (s->s3->tmp.ca_names != NULL)
1829 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1830 s->s3->tmp.ca_names=ca_sk;
1831 ca_sk=NULL;
1832
1833 ret=1;
1834err:
1835 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1836 return(ret);
1837 }
1838
Adam Langley95c29f32014-06-20 12:00:00 -07001839int ssl3_get_new_session_ticket(SSL *s)
1840 {
David Benjamine044c3d2014-07-14 19:14:12 -04001841 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001842 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001843 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001844
1845 n=s->method->ssl_get_message(s,
1846 SSL3_ST_CR_SESSION_TICKET_A,
1847 SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin64442872014-07-21 17:43:45 -04001848 SSL3_MT_NEWSESSION_TICKET,
Adam Langley95c29f32014-06-20 12:00:00 -07001849 16384,
1850 &ok);
1851
1852 if (!ok)
1853 return((int)n);
1854
David Benjamine044c3d2014-07-14 19:14:12 -04001855 CBS_init(&new_session_ticket, s->init_msg, n);
1856
1857 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1858 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1859 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001860 {
Adam Langley95c29f32014-06-20 12:00:00 -07001861 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001862 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001863 goto f_err;
1864 }
1865
David Benjamine044c3d2014-07-14 19:14:12 -04001866 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001867 {
1868 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1869 goto err;
1870 }
David Benjamine044c3d2014-07-14 19:14:12 -04001871
Adam Langley95c29f32014-06-20 12:00:00 -07001872 /* There are two ways to detect a resumed ticket sesion.
1873 * One is to set an appropriate session ID and then the server
1874 * must return a match in ServerHello. This allows the normal
1875 * client session ID matching to work and we know much
1876 * earlier that the ticket has been accepted.
1877 *
1878 * The other way is to set zero length session ID when the
1879 * ticket is presented and rely on the handshake to determine
1880 * session resumption.
1881 *
1882 * We choose the former approach because this fits in with
1883 * assumptions elsewhere in OpenSSL. The session ID is set
1884 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1885 * ticket.
1886 */
David Benjamine044c3d2014-07-14 19:14:12 -04001887 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001888 s->session->session_id, &s->session->session_id_length,
1889#ifndef OPENSSL_NO_SHA256
1890 EVP_sha256(), NULL);
1891#else
1892 EVP_sha1(), NULL);
1893#endif
1894 ret=1;
1895 return(ret);
1896f_err:
1897 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1898err:
1899 return(-1);
1900 }
1901
1902int ssl3_get_cert_status(SSL *s)
1903 {
1904 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001905 long n;
1906 CBS certificate_status, ocsp_response;
1907 uint8_t status_type;
1908 size_t resplen;
Adam Langley95c29f32014-06-20 12:00:00 -07001909
1910 n=s->method->ssl_get_message(s,
1911 SSL3_ST_CR_CERT_STATUS_A,
1912 SSL3_ST_CR_CERT_STATUS_B,
1913 SSL3_MT_CERTIFICATE_STATUS,
1914 16384,
1915 &ok);
1916
1917 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001918
1919 CBS_init(&certificate_status, s->init_msg, n);
1920 if (!CBS_get_u8(&certificate_status, &status_type) ||
1921 status_type != TLSEXT_STATUSTYPE_ocsp ||
1922 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1923 CBS_len(&ocsp_response) == 0 ||
1924 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001925 {
1926 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04001927 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001928 goto f_err;
1929 }
David Benjamin46062682014-07-14 19:14:32 -04001930
1931 /* TODO(davidben): Make tlsext_ocsp_resplen a
1932 * size_t. Currently it uses -1 to signal no response. The
1933 * spec does not allow ocsp_response to be zero-length, so
1934 * using 0 should be fine. */
1935 if (!CBS_stow(&ocsp_response, &s->tlsext_ocsp_resp, &resplen))
Adam Langley95c29f32014-06-20 12:00:00 -07001936 {
1937 al = SSL_AD_INTERNAL_ERROR;
1938 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1939 goto f_err;
1940 }
1941 s->tlsext_ocsp_resplen = resplen;
1942 if (s->ctx->tlsext_status_cb)
1943 {
1944 int ret;
1945 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1946 if (ret == 0)
1947 {
1948 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1949 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
1950 goto f_err;
1951 }
1952 if (ret < 0)
1953 {
1954 al = SSL_AD_INTERNAL_ERROR;
1955 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1956 goto f_err;
1957 }
1958 }
1959 return 1;
1960f_err:
1961 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1962 return(-1);
1963 }
Adam Langley95c29f32014-06-20 12:00:00 -07001964
1965int ssl3_get_server_done(SSL *s)
1966 {
1967 int ok,ret=0;
1968 long n;
1969
1970 n=s->method->ssl_get_message(s,
1971 SSL3_ST_CR_SRVR_DONE_A,
1972 SSL3_ST_CR_SRVR_DONE_B,
1973 SSL3_MT_SERVER_DONE,
1974 30, /* should be very small, like 0 :-) */
1975 &ok);
1976
1977 if (!ok) return((int)n);
1978 if (n > 0)
1979 {
1980 /* should contain no data */
1981 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1982 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1983 return -1;
1984 }
1985 ret=1;
1986 return(ret);
1987 }
1988
1989
1990int ssl3_send_client_key_exchange(SSL *s)
1991 {
1992 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07001993 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001994 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001995 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001996 unsigned char *q;
1997 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001998#ifndef OPENSSL_NO_ECDH
1999 EC_KEY *clnt_ecdh = NULL;
2000 const EC_POINT *srvr_ecpoint = NULL;
2001 EVP_PKEY *srvr_pub_pkey = NULL;
2002 unsigned char *encodedPoint = NULL;
2003 int encoded_pt_len = 0;
2004 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002005 unsigned int psk_len = 0;
2006 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langleyc26c8022014-06-20 12:00:00 -07002007#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002008
2009 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2010 {
2011 p = ssl_handshake_start(s);
2012
2013 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002014 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2015
Adam Langleyc26c8022014-06-20 12:00:00 -07002016 if (alg_a & SSL_aPSK)
2017 {
Adam Langley01797e32014-06-20 12:00:00 -07002018 char identity[PSK_MAX_IDENTITY_LEN + 1];
2019 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002020 unsigned char *t = NULL;
2021 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2022 unsigned int pre_ms_len = 0;
2023 int psk_err = 1;
2024
2025 n = 0;
2026 if (s->psk_client_callback == NULL)
2027 {
2028 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2029 goto err;
2030 }
2031
Adam Langley01797e32014-06-20 12:00:00 -07002032 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002033 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002034 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002035 if (psk_len > PSK_MAX_PSK_LEN)
2036 {
2037 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2038 goto psk_err;
2039 }
2040 else if (psk_len == 0)
2041 {
2042 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2043 goto psk_err;
2044 }
Adam Langley01797e32014-06-20 12:00:00 -07002045 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2046 if (identity_len > PSK_MAX_IDENTITY_LEN)
2047 {
2048 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2049 goto psk_err;
2050 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002051
2052 if (!(alg_k & SSL_kEECDH))
2053 {
David Benjamin14c83e72014-07-13 04:54:57 -04002054 /* Create the shared secret now if we're not using ECDHE-PSK.
2055 * TODO(davidben): Refactor this logic similarly
2056 * to ssl3_get_client_key_exchange. */
Adam Langleyc26c8022014-06-20 12:00:00 -07002057 pre_ms_len = 2+psk_len+2+psk_len;
2058 t = pre_ms;
2059 s2n(psk_len, t);
2060 memset(t, 0, psk_len);
2061 t+=psk_len;
2062 s2n(psk_len, t);
2063 memcpy(t, psk, psk_len);
2064
2065 s->session->master_key_length =
2066 s->method->ssl3_enc->generate_master_secret(s,
2067 s->session->master_key,
2068 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002069 s2n(identity_len, p);
2070 memcpy(p, identity, identity_len);
2071 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002072 }
2073
Adam Langleyc26c8022014-06-20 12:00:00 -07002074 if (s->session->psk_identity != NULL)
2075 OPENSSL_free(s->session->psk_identity);
2076 s->session->psk_identity = BUF_strdup(identity);
2077 if (s->session->psk_identity == NULL)
2078 {
2079 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2080 goto psk_err;
2081 }
2082 psk_err = 0;
2083 psk_err:
2084 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2085 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2086 if (psk_err != 0)
2087 {
2088 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2089 goto err;
2090 }
2091 }
Adam Langley95c29f32014-06-20 12:00:00 -07002092
2093 /* Fool emacs indentation */
2094 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002095 else if (alg_k & SSL_kRSA)
2096 {
2097 RSA *rsa;
2098 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2099
2100 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2101 rsa=s->session->sess_cert->peer_rsa_tmp;
2102 else
2103 {
2104 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2105 if ((pkey == NULL) ||
2106 (pkey->type != EVP_PKEY_RSA) ||
2107 (pkey->pkey.rsa == NULL))
2108 {
2109 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2110 goto err;
2111 }
2112 rsa=pkey->pkey.rsa;
2113 EVP_PKEY_free(pkey);
2114 }
2115
2116 tmp_buf[0]=s->client_version>>8;
2117 tmp_buf[1]=s->client_version&0xff;
2118 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2119 goto err;
2120
2121 s->session->master_key_length=sizeof tmp_buf;
2122
2123 q=p;
2124 /* Fix buf for TLS and beyond */
2125 if (s->version > SSL3_VERSION)
2126 p+=2;
2127 n=RSA_public_encrypt(sizeof tmp_buf,
2128 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2129#ifdef PKCS1_CHECK
2130 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2131 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2132#endif
2133 if (n <= 0)
2134 {
2135 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2136 goto err;
2137 }
2138
2139 /* Fix buf for TLS and beyond */
2140 if (s->version > SSL3_VERSION)
2141 {
2142 s2n(n,q);
2143 n+=2;
2144 }
2145
2146 s->session->master_key_length=
2147 s->method->ssl3_enc->generate_master_secret(s,
2148 s->session->master_key,
2149 tmp_buf,sizeof tmp_buf);
2150 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2151 }
Adam Langley95c29f32014-06-20 12:00:00 -07002152#ifndef OPENSSL_NO_DH
2153 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2154 {
2155 DH *dh_srvr,*dh_clnt;
2156 SESS_CERT *scert = s->session->sess_cert;
2157
2158 if (scert == NULL)
2159 {
2160 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2161 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2162 goto err;
2163 }
2164
2165 if (scert->peer_dh_tmp != NULL)
2166 dh_srvr=scert->peer_dh_tmp;
2167 else
2168 {
2169 /* we get them from the cert */
2170 int idx = scert->peer_cert_type;
2171 EVP_PKEY *spkey = NULL;
2172 dh_srvr = NULL;
2173 if (idx >= 0)
2174 spkey = X509_get_pubkey(
2175 scert->peer_pkeys[idx].x509);
2176 if (spkey)
2177 {
2178 dh_srvr = EVP_PKEY_get1_DH(spkey);
2179 EVP_PKEY_free(spkey);
2180 }
2181 if (dh_srvr == NULL)
2182 {
2183 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2184 goto err;
2185 }
2186 }
2187 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2188 {
2189 /* Use client certificate key */
2190 EVP_PKEY *clkey = s->cert->key->privatekey;
2191 dh_clnt = NULL;
2192 if (clkey)
2193 dh_clnt = EVP_PKEY_get1_DH(clkey);
2194 if (dh_clnt == NULL)
2195 {
2196 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2197 goto err;
2198 }
2199 }
2200 else
2201 {
2202 /* generate a new random key */
2203 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2204 {
2205 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2206 goto err;
2207 }
2208 if (!DH_generate_key(dh_clnt))
2209 {
2210 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2211 DH_free(dh_clnt);
2212 goto err;
2213 }
2214 }
2215
2216 /* use the 'p' output buffer for the DH key, but
2217 * make sure to clear it out afterwards */
2218
2219 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2220 if (scert->peer_dh_tmp == NULL)
2221 DH_free(dh_srvr);
2222
2223 if (n <= 0)
2224 {
2225 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2226 DH_free(dh_clnt);
2227 goto err;
2228 }
2229
2230 /* generate master key from the result */
2231 s->session->master_key_length=
2232 s->method->ssl3_enc->generate_master_secret(s,
2233 s->session->master_key,p,n);
2234 /* clean up */
2235 memset(p,0,n);
2236
2237 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2238 n = 0;
2239 else
2240 {
2241 /* send off the data */
2242 n=BN_num_bytes(dh_clnt->pub_key);
2243 s2n(n,p);
2244 BN_bn2bin(dh_clnt->pub_key,p);
2245 n+=2;
2246 }
2247
2248 DH_free(dh_clnt);
2249
2250 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2251 }
2252#endif
2253
Adam Langleyc26c8022014-06-20 12:00:00 -07002254#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002255 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2256 {
2257 const EC_GROUP *srvr_group = NULL;
2258 EC_KEY *tkey;
2259 int ecdh_clnt_cert = 0;
2260 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002261 unsigned char *pre_ms;
2262 unsigned char *t;
2263 unsigned int pre_ms_len;
2264 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyd06eddd2014-06-20 12:00:00 -07002266 if (s->session->sess_cert == NULL)
2267 {
2268 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2269 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2270 goto err;
2271 }
2272
Adam Langley95c29f32014-06-20 12:00:00 -07002273 /* Did we send out the client's
2274 * ECDH share for use in premaster
2275 * computation as part of client certificate?
2276 * If so, set ecdh_clnt_cert to 1.
2277 */
2278 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2279 {
2280 /* XXX: For now, we do not support client
2281 * authentication using ECDH certificates.
2282 * To add such support, one needs to add
2283 * code that checks for appropriate
2284 * conditions and sets ecdh_clnt_cert to 1.
2285 * For example, the cert have an ECC
2286 * key on the same curve as the server's
2287 * and the key should be authorized for
2288 * key agreement.
2289 *
2290 * One also needs to add code in ssl3_connect
2291 * to skip sending the certificate verify
2292 * message.
2293 *
2294 * if ((s->cert->key->privatekey != NULL) &&
2295 * (s->cert->key->privatekey->type ==
2296 * EVP_PKEY_EC) && ...)
2297 * ecdh_clnt_cert = 1;
2298 */
2299 }
2300
2301 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2302 {
2303 tkey = s->session->sess_cert->peer_ecdh_tmp;
2304 }
2305 else
2306 {
2307 /* Get the Server Public Key from Cert */
2308 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2309 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2310 if ((srvr_pub_pkey == NULL) ||
2311 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2312 (srvr_pub_pkey->pkey.ec == NULL))
2313 {
2314 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2315 goto err;
2316 }
2317
2318 tkey = srvr_pub_pkey->pkey.ec;
2319 }
2320
2321 srvr_group = EC_KEY_get0_group(tkey);
2322 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2323
2324 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2325 {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2327 goto err;
2328 }
2329
2330 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2331 {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2333 goto err;
2334 }
2335
2336 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2337 {
2338 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2339 goto err;
2340 }
2341 if (ecdh_clnt_cert)
2342 {
2343 /* Reuse key info from our certificate
2344 * We only need our private key to perform
2345 * the ECDH computation.
2346 */
2347 const BIGNUM *priv_key;
2348 tkey = s->cert->key->privatekey->pkey.ec;
2349 priv_key = EC_KEY_get0_private_key(tkey);
2350 if (priv_key == NULL)
2351 {
2352 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2353 goto err;
2354 }
2355 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2356 {
2357 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2358 goto err;
2359 }
2360 }
2361 else
2362 {
2363 /* Generate a new ECDH key pair */
2364 if (!(EC_KEY_generate_key(clnt_ecdh)))
2365 {
2366 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2367 goto err;
2368 }
2369 }
2370
2371 /* use the 'p' output buffer for the ECDH key, but
2372 * make sure to clear it out afterwards
2373 */
2374
2375 field_size = EC_GROUP_get_degree(srvr_group);
2376 if (field_size <= 0)
2377 {
2378 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2379 goto err;
2380 }
2381 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2382 if (n <= 0)
2383 {
2384 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2385 goto err;
2386 }
2387
Adam Langleyc26c8022014-06-20 12:00:00 -07002388 /* ECDHE PSK ciphersuites from RFC 5489 */
2389 if ((alg_a & SSL_aPSK) && psk_len != 0)
2390 {
2391 pre_ms_len = 2+psk_len+2+n;
2392 pre_ms = OPENSSL_malloc(pre_ms_len);
2393 if (pre_ms == NULL)
2394 {
2395 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2396 goto err;
2397 }
2398 memset(pre_ms, 0, pre_ms_len);
2399 t = pre_ms;
2400 s2n(psk_len, t);
2401 memcpy(t, psk, psk_len);
2402 t += psk_len;
2403 s2n(n, t);
2404 memcpy(t, p, n);
2405 s->session->master_key_length = s->method->ssl3_enc \
2406 -> generate_master_secret(s,
2407 s->session->master_key, pre_ms, pre_ms_len);
2408 OPENSSL_cleanse(pre_ms, pre_ms_len);
2409 OPENSSL_free(pre_ms);
2410 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002411 if (!(alg_a & SSL_aPSK))
2412 {
2413 /* generate master key from the result */
2414 s->session->master_key_length = s->method->ssl3_enc \
2415 -> generate_master_secret(s,
2416 s->session->master_key, p, n);
2417 }
Adam Langley95c29f32014-06-20 12:00:00 -07002418 memset(p, 0, n); /* clean up */
2419
Adam Langleyc26c8022014-06-20 12:00:00 -07002420 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002421 {
2422 /* Send empty client key exch message */
2423 n = 0;
2424 }
2425 else
2426 {
2427 /* First check the size of encoding and
2428 * allocate memory accordingly.
2429 */
2430 encoded_pt_len =
2431 EC_POINT_point2oct(srvr_group,
2432 EC_KEY_get0_public_key(clnt_ecdh),
2433 POINT_CONVERSION_UNCOMPRESSED,
2434 NULL, 0, NULL);
2435
2436 encodedPoint = (unsigned char *)
2437 OPENSSL_malloc(encoded_pt_len *
2438 sizeof(unsigned char));
2439 bn_ctx = BN_CTX_new();
2440 if ((encodedPoint == NULL) ||
2441 (bn_ctx == NULL))
2442 {
2443 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2444 goto err;
2445 }
2446
2447 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002448 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2449 EC_KEY_get0_public_key(clnt_ecdh),
2450 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002451 encodedPoint, encoded_pt_len, bn_ctx);
2452
Adam Langleyc26c8022014-06-20 12:00:00 -07002453 n = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002454 if ((alg_a & SSL_aPSK) && psk_len != 0)
2455 {
2456 i = strlen(s->session->psk_identity);
2457 s2n(i, p);
2458 memcpy(p, s->session->psk_identity, i);
2459 p += i;
2460 n = i + 2;
2461 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002462
2463 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002464 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002465 p += 1;
2466 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002467 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002468 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002469 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002470 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002471 }
2472
2473 /* Free allocated memory */
2474 BN_CTX_free(bn_ctx);
2475 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002476 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002477 EC_KEY_free(clnt_ecdh);
2478 EVP_PKEY_free(srvr_pub_pkey);
2479 }
2480#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002481 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002482 {
2483 ssl3_send_alert(s, SSL3_AL_FATAL,
2484 SSL_AD_HANDSHAKE_FAILURE);
2485 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2486 goto err;
2487 }
2488
2489 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2490 s->state=SSL3_ST_CW_KEY_EXCH_B;
2491 }
2492
2493 /* SSL3_ST_CW_KEY_EXCH_B */
2494 return ssl_do_write(s);
2495err:
2496#ifndef OPENSSL_NO_ECDH
2497 BN_CTX_free(bn_ctx);
2498 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2499 if (clnt_ecdh != NULL)
2500 EC_KEY_free(clnt_ecdh);
2501 EVP_PKEY_free(srvr_pub_pkey);
2502#endif
2503 return(-1);
2504 }
2505
2506int ssl3_send_client_verify(SSL *s)
2507 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002508 unsigned char *buf, *p;
2509 const EVP_MD *md;
2510 uint8_t digest[EVP_MAX_MD_SIZE];
2511 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002512 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002513 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002514 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002515 size_t signature_length = 0;
2516 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002517
2518 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002519 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002520
2521 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2522 {
2523 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002524 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002525 /* For TLS v1.2 send signature algorithm and signature using
2526 * agreed digest and cached handshake records. Otherwise, use
2527 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002528 */
2529 if (SSL_USE_SIGALGS(s))
2530 {
Adam Langley37a623c2014-07-18 09:28:40 -07002531 const uint8_t *hdata;
2532 size_t hdatalen;
David Benjamin0e2908a2014-07-09 15:22:43 -04002533 md = s->cert->key->digest;
Adam Langley37a623c2014-07-18 09:28:40 -07002534 if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen) ||
2535 !tls12_get_sigandhash(p, pkey, md))
Adam Langley95c29f32014-06-20 12:00:00 -07002536 {
2537 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2538 goto err;
2539 }
2540 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002541 n += 2;
2542 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2543 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2544 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002545 {
2546 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2547 goto err;
2548 }
Adam Langley95c29f32014-06-20 12:00:00 -07002549 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002550 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002551 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002552 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002553 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002554 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2555 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2556 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2557 * a raw RSA signature, rather than wrapping in a
2558 * DigestInfo. */
2559 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002560 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002561 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002562 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002563 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2564 digest_length = SHA_DIGEST_LENGTH;
2565 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002566 }
2567 else
Adam Langleyaed23062014-06-20 12:00:00 -07002568 {
Adam Langley95c29f32014-06-20 12:00:00 -07002569 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2570 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002571 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002572
2573 /* Sign the digest. */
2574 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2575 if (pctx == NULL)
2576 goto err;
2577
2578 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2579 if (EVP_PKEY_sign_init(pctx) != 1 ||
2580 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2581 EVP_PKEY_sign(pctx, NULL, &signature_length,
2582 digest, digest_length) != 1)
2583 {
2584 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2585 goto err;
2586 }
2587
2588 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2589 {
2590 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2591 goto err;
2592 }
2593
2594 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2595 digest, digest_length) != 1)
2596 {
2597 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2598 goto err;
2599 }
2600
2601 s2n(signature_length, p);
2602 n += signature_length + 2;
2603
2604 /* Now that client auth is completed, we no longer need cached
2605 * handshake records and can digest them. */
2606 if (SSL_USE_SIGALGS(s))
2607 {
2608 if (!ssl3_digest_cached_records(s))
2609 goto err;
2610 }
2611
Adam Langley95c29f32014-06-20 12:00:00 -07002612 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2613 s->state=SSL3_ST_CW_CERT_VRFY_B;
2614 }
2615 EVP_MD_CTX_cleanup(&mctx);
2616 EVP_PKEY_CTX_free(pctx);
2617 return ssl_do_write(s);
2618err:
2619 EVP_MD_CTX_cleanup(&mctx);
2620 EVP_PKEY_CTX_free(pctx);
2621 return(-1);
2622 }
2623
2624/* Check a certificate can be used for client authentication. Currently
2625 * check cert exists, if we have a suitable digest for TLS 1.2 if
2626 * static DH client certificates can be used and optionally checks
2627 * suitability for Suite B.
2628 */
2629static int ssl3_check_client_certificate(SSL *s)
2630 {
2631 unsigned long alg_k;
2632 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2633 return 0;
2634 /* If no suitable signature algorithm can't use certificate */
2635 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2636 return 0;
2637 /* If strict mode check suitability of chain before using it.
2638 * This also adjusts suite B digest if necessary.
2639 */
2640 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2641 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2642 return 0;
2643 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2644 /* See if we can use client certificate for fixed DH */
2645 if (alg_k & (SSL_kDHr|SSL_kDHd))
2646 {
2647 SESS_CERT *scert = s->session->sess_cert;
2648 int i = scert->peer_cert_type;
2649 EVP_PKEY *clkey = NULL, *spkey = NULL;
2650 clkey = s->cert->key->privatekey;
2651 /* If client key not DH assume it can be used */
2652 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2653 return 1;
2654 if (i >= 0)
2655 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2656 if (spkey)
2657 {
2658 /* Compare server and client parameters */
2659 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2660 EVP_PKEY_free(spkey);
2661 if (i != 1)
2662 return 0;
2663 }
2664 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2665 }
2666 return 1;
2667 }
2668
2669int ssl3_send_client_certificate(SSL *s)
2670 {
2671 X509 *x509=NULL;
2672 EVP_PKEY *pkey=NULL;
2673 int i;
2674
2675 if (s->state == SSL3_ST_CW_CERT_A)
2676 {
2677 /* Let cert callback update client certificates if required */
2678 if (s->cert->cert_cb)
2679 {
2680 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2681 if (i < 0)
2682 {
2683 s->rwstate=SSL_X509_LOOKUP;
2684 return -1;
2685 }
2686 if (i == 0)
2687 {
2688 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2689 return 0;
2690 }
2691 s->rwstate=SSL_NOTHING;
2692 }
2693 if (ssl3_check_client_certificate(s))
2694 s->state=SSL3_ST_CW_CERT_C;
2695 else
2696 s->state=SSL3_ST_CW_CERT_B;
2697 }
2698
2699 /* We need to get a client cert */
2700 if (s->state == SSL3_ST_CW_CERT_B)
2701 {
2702 /* If we get an error, we need to
2703 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2704 * We then get retied later */
2705 i=0;
2706 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2707 if (i < 0)
2708 {
2709 s->rwstate=SSL_X509_LOOKUP;
2710 return(-1);
2711 }
2712 s->rwstate=SSL_NOTHING;
2713 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2714 {
2715 s->state=SSL3_ST_CW_CERT_B;
2716 if ( !SSL_use_certificate(s,x509) ||
2717 !SSL_use_PrivateKey(s,pkey))
2718 i=0;
2719 }
2720 else if (i == 1)
2721 {
2722 i=0;
2723 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2724 }
2725
2726 if (x509 != NULL) X509_free(x509);
2727 if (pkey != NULL) EVP_PKEY_free(pkey);
2728 if (i && !ssl3_check_client_certificate(s))
2729 i = 0;
2730 if (i == 0)
2731 {
2732 if (s->version == SSL3_VERSION)
2733 {
2734 s->s3->tmp.cert_req=0;
2735 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2736 return(1);
2737 }
2738 else
2739 {
2740 s->s3->tmp.cert_req=2;
2741 }
2742 }
2743
2744 /* Ok, we have a cert */
2745 s->state=SSL3_ST_CW_CERT_C;
2746 }
2747
2748 if (s->state == SSL3_ST_CW_CERT_C)
2749 {
2750 s->state=SSL3_ST_CW_CERT_D;
2751 ssl3_output_cert_chain(s,
2752 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2753 }
2754 /* SSL3_ST_CW_CERT_D */
2755 return ssl_do_write(s);
2756 }
2757
2758#define has_bits(i,m) (((i)&(m)) == (m))
2759
2760int ssl3_check_cert_and_algorithm(SSL *s)
2761 {
2762 int i,idx;
2763 long alg_k,alg_a;
2764 EVP_PKEY *pkey=NULL;
2765 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002766 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002767#ifndef OPENSSL_NO_DH
2768 DH *dh;
2769#endif
2770
David Benjamine8f3d662014-07-12 01:10:19 -04002771 /* we don't have a certificate */
2772 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2773 return 1;
2774
Adam Langley95c29f32014-06-20 12:00:00 -07002775 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2776 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2777
Adam Langley95c29f32014-06-20 12:00:00 -07002778 sc=s->session->sess_cert;
2779 if (sc == NULL)
2780 {
2781 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2782 goto err;
2783 }
2784
Adam Langley95c29f32014-06-20 12:00:00 -07002785 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002786#ifndef OPENSSL_NO_DH
2787 dh=s->session->sess_cert->peer_dh_tmp;
2788#endif
2789
2790 /* This is the passed certificate */
2791
2792 idx=sc->peer_cert_type;
2793#ifndef OPENSSL_NO_ECDH
2794 if (idx == SSL_PKEY_ECC)
2795 {
2796 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2797 s) == 0)
2798 { /* check failed */
2799 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
2800 goto f_err;
2801 }
2802 else
2803 {
2804 return 1;
2805 }
2806 }
2807 else if (alg_a & SSL_aECDSA)
2808 {
2809 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
2810 goto f_err;
2811 }
2812 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2813 {
2814 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
2815 goto f_err;
2816 }
2817#endif
2818 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2819 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2820 EVP_PKEY_free(pkey);
2821
2822
2823 /* Check that we have a certificate if we require one */
2824 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2825 {
2826 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
2827 goto f_err;
2828 }
2829#ifndef OPENSSL_NO_DSA
2830 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2831 {
2832 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
2833 goto f_err;
2834 }
2835#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002836 if ((alg_k & SSL_kRSA) &&
2837 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2838 {
2839 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2840 goto f_err;
2841 }
Adam Langley95c29f32014-06-20 12:00:00 -07002842#ifndef OPENSSL_NO_DH
2843 if ((alg_k & SSL_kEDH) &&
2844 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2845 {
2846 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
2847 goto f_err;
2848 }
2849 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2850 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2851 {
2852 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
2853 goto f_err;
2854 }
2855#ifndef OPENSSL_NO_DSA
2856 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2857 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2858 {
2859 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
2860 goto f_err;
2861 }
2862#endif
2863#endif
2864
Adam Langley95c29f32014-06-20 12:00:00 -07002865 return(1);
2866f_err:
2867 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2868err:
2869 return(0);
2870 }
2871
Adam Langley1258b6a2014-06-20 12:00:00 -07002872# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07002873int ssl3_send_next_proto(SSL *s)
2874 {
2875 unsigned int len, padding_len;
2876 unsigned char *d;
2877
2878 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
2879 {
2880 len = s->next_proto_negotiated_len;
2881 padding_len = 32 - ((len + 2) % 32);
2882 d = (unsigned char *)s->init_buf->data;
2883 d[4] = len;
2884 memcpy(d + 5, s->next_proto_negotiated, len);
2885 d[5 + len] = padding_len;
2886 memset(d + 6 + len, 0, padding_len);
2887 *(d++)=SSL3_MT_NEXT_PROTO;
2888 l2n3(2 + len + padding_len, d);
2889 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2890 s->init_num = 4 + 2 + len + padding_len;
2891 s->init_off = 0;
2892 }
2893
2894 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2895}
Adam Langley1258b6a2014-06-20 12:00:00 -07002896
2897# endif /* !OPENSSL_NO_NEXTPROTONEG */
2898
2899int ssl3_send_channel_id(SSL *s)
2900 {
2901 unsigned char *d;
2902 int ret = -1, public_key_len;
2903 EVP_MD_CTX md_ctx;
2904 size_t sig_len;
2905 ECDSA_SIG *sig = NULL;
2906 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
2907
2908 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
2909 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2910
2911 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
2912 {
2913 EVP_PKEY *key = NULL;
2914 s->ctx->channel_id_cb(s, &key);
2915 if (key != NULL)
2916 {
2917 s->tlsext_channel_id_private = key;
2918 }
2919 }
2920 if (!s->tlsext_channel_id_private)
2921 {
2922 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
2923 return (-1);
2924 }
2925 s->rwstate=SSL_NOTHING;
2926
2927 d = (unsigned char *)s->init_buf->data;
2928 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
2929 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
2930 if (s->s3->tlsext_channel_id_new)
2931 s2n(TLSEXT_TYPE_channel_id_new, d);
2932 else
2933 s2n(TLSEXT_TYPE_channel_id, d);
2934 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2935
2936 EVP_MD_CTX_init(&md_ctx);
2937
2938 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2939 if (public_key_len <= 0)
2940 {
2941 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2942 goto err;
2943 }
2944 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2945 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2946 * field elements as 32-byte, big-endian numbers. */
2947 if (public_key_len != 65)
2948 {
2949 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2950 goto err;
2951 }
2952 public_key = OPENSSL_malloc(public_key_len);
2953 if (!public_key)
2954 {
2955 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2956 goto err;
2957 }
2958
2959 derp = public_key;
2960 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2961
2962 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2963 s->tlsext_channel_id_private) != 1)
2964 {
2965 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2966 goto err;
2967 }
2968
2969 if (!tls1_channel_id_hash(&md_ctx, s))
2970 goto err;
2971
2972 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
2973 {
2974 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2975 goto err;
2976 }
2977
2978 der_sig = OPENSSL_malloc(sig_len);
2979 if (!der_sig)
2980 {
2981 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2982 goto err;
2983 }
2984
2985 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
2986 {
2987 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2988 goto err;
2989 }
2990
2991 derp = der_sig;
2992 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
2993 if (sig == NULL)
2994 {
2995 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2996 goto err;
2997 }
2998
2999 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3000 memcpy(d, public_key + 1, 64);
3001 d += 64;
3002 memset(d, 0, 2 * 32);
3003 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3004 d += 32;
3005 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3006 d += 32;
3007
3008 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3009 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3010 s->init_off = 0;
3011
3012 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3013
3014err:
3015 EVP_MD_CTX_cleanup(&md_ctx);
3016 if (public_key)
3017 OPENSSL_free(public_key);
3018 if (der_sig)
3019 OPENSSL_free(der_sig);
3020 if (sig)
3021 ECDSA_SIG_free(sig);
3022
3023 return ret;
3024 }
Adam Langley95c29f32014-06-20 12:00:00 -07003025
Adam Langley95c29f32014-06-20 12:00:00 -07003026int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3027 {
3028 int i = 0;
3029 /* TODO(fork): remove */
3030#if 0
3031#ifndef OPENSSL_NO_ENGINE
3032 if (s->ctx->client_cert_engine)
3033 {
3034 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3035 SSL_get_client_CA_list(s),
3036 px509, ppkey, NULL, NULL, NULL);
3037 if (i != 0)
3038 return i;
3039 }
3040#endif
3041#endif
3042 if (s->ctx->client_cert_cb)
3043 i = s->ctx->client_cert_cb(s,px509,ppkey);
3044 return i;
3045 }