blob: 35c399ff93c6b7b41a25f92c5dece19417a2c8c3 [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 Langleyadb739e2014-06-20 12:00:00 -0700201 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
202 {
203 /* Send app data along with CCS/Finished */
204 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED;
205 }
206
Adam Langley95c29f32014-06-20 12:00:00 -0700207 for (;;)
208 {
209 state=s->state;
210
211 switch(s->state)
212 {
213 case SSL_ST_RENEGOTIATE:
214 s->renegotiate=1;
215 s->state=SSL_ST_CONNECT;
216 s->ctx->stats.sess_connect_renegotiate++;
217 /* break */
218 case SSL_ST_BEFORE:
219 case SSL_ST_CONNECT:
220 case SSL_ST_BEFORE|SSL_ST_CONNECT:
221 case SSL_ST_OK|SSL_ST_CONNECT:
222
223 s->server=0;
224 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
225
226 if ((s->version & 0xff00 ) != 0x0300)
227 {
228 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
229 ret = -1;
230 goto end;
231 }
232
233 /* s->version=SSL3_VERSION; */
234 s->type=SSL_ST_CONNECT;
235
236 if (s->init_buf == NULL)
237 {
238 if ((buf=BUF_MEM_new()) == NULL)
239 {
240 ret= -1;
241 goto end;
242 }
243 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
244 {
245 ret= -1;
246 goto end;
247 }
248 s->init_buf=buf;
249 buf=NULL;
250 }
251
252 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
253
254 /* setup buffing BIO */
255 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
256
257 /* don't push the buffering BIO quite yet */
258
259 ssl3_init_finished_mac(s);
260
261 s->state=SSL3_ST_CW_CLNT_HELLO_A;
262 s->ctx->stats.sess_connect++;
263 s->init_num=0;
264 break;
265
266 case SSL3_ST_CW_CLNT_HELLO_A:
267 case SSL3_ST_CW_CLNT_HELLO_B:
268
269 s->shutdown=0;
270 ret=ssl3_client_hello(s);
271 if (ret <= 0) goto end;
272 s->state=SSL3_ST_CR_SRVR_HELLO_A;
273 s->init_num=0;
274
275 /* turn on buffering for the next lot of output */
276 if (s->bbio != s->wbio)
277 s->wbio=BIO_push(s->bbio,s->wbio);
278
279 break;
280
281 case SSL3_ST_CR_SRVR_HELLO_A:
282 case SSL3_ST_CR_SRVR_HELLO_B:
283 ret=ssl3_get_server_hello(s);
284 if (ret <= 0) goto end;
285
286 if (s->hit)
287 {
288 s->state=SSL3_ST_CR_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700289 if (s->tlsext_ticket_expected)
290 {
291 /* receive renewed session ticket */
292 s->state=SSL3_ST_CR_SESSION_TICKET_A;
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294 }
295 else
296 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400297 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700298 }
299 s->init_num=0;
300 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400301
Adam Langley95c29f32014-06-20 12:00:00 -0700302 case SSL3_ST_CR_CERT_A:
303 case SSL3_ST_CR_CERT_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700304 ret=ssl3_check_finished(s);
305 if (ret <= 0) goto end;
306 if (ret == 2)
307 {
308 s->hit = 1;
309 if (s->tlsext_ticket_expected)
310 s->state=SSL3_ST_CR_SESSION_TICKET_A;
311 else
312 s->state=SSL3_ST_CR_FINISHED_A;
313 s->init_num=0;
314 break;
315 }
Adam Langley95c29f32014-06-20 12:00:00 -0700316 /* Check if it is anon DH/ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -0700317 /* or non-RSA PSK */
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
Adam Langleyc26c8022014-06-20 12:00:00 -0700319 !((s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) &&
320 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -0700321 {
322 ret=ssl3_get_server_certificate(s);
323 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700324 if (s->tlsext_status_expected)
325 s->state=SSL3_ST_CR_CERT_STATUS_A;
326 else
327 s->state=SSL3_ST_CR_KEY_EXCH_A;
328 }
329 else
330 {
331 skip = 1;
332 s->state=SSL3_ST_CR_KEY_EXCH_A;
333 }
Adam Langley95c29f32014-06-20 12:00:00 -0700334 s->init_num=0;
335 break;
336
337 case SSL3_ST_CR_KEY_EXCH_A:
338 case SSL3_ST_CR_KEY_EXCH_B:
339 ret=ssl3_get_key_exchange(s);
340 if (ret <= 0) goto end;
341 s->state=SSL3_ST_CR_CERT_REQ_A;
342 s->init_num=0;
343
344 /* at this point we check that we have the
345 * required stuff from the server */
346 if (!ssl3_check_cert_and_algorithm(s))
347 {
348 ret= -1;
349 goto end;
350 }
351 break;
352
353 case SSL3_ST_CR_CERT_REQ_A:
354 case SSL3_ST_CR_CERT_REQ_B:
355 ret=ssl3_get_certificate_request(s);
356 if (ret <= 0) goto end;
357 s->state=SSL3_ST_CR_SRVR_DONE_A;
358 s->init_num=0;
359 break;
360
361 case SSL3_ST_CR_SRVR_DONE_A:
362 case SSL3_ST_CR_SRVR_DONE_B:
363 ret=ssl3_get_server_done(s);
364 if (ret <= 0) goto end;
365 if (s->s3->tmp.cert_req)
366 s->state=SSL3_ST_CW_CERT_A;
367 else
368 s->state=SSL3_ST_CW_KEY_EXCH_A;
369 s->init_num=0;
370
371 break;
372
373 case SSL3_ST_CW_CERT_A:
374 case SSL3_ST_CW_CERT_B:
375 case SSL3_ST_CW_CERT_C:
376 case SSL3_ST_CW_CERT_D:
377 ret=ssl3_send_client_certificate(s);
378 if (ret <= 0) goto end;
379 s->state=SSL3_ST_CW_KEY_EXCH_A;
380 s->init_num=0;
381 break;
382
383 case SSL3_ST_CW_KEY_EXCH_A:
384 case SSL3_ST_CW_KEY_EXCH_B:
385 ret=ssl3_send_client_key_exchange(s);
386 if (ret <= 0) goto end;
387 /* EAY EAY EAY need to check for DH fix cert
388 * sent back */
389 /* For TLS, cert_req is set to 2, so a cert chain
390 * of nothing is sent, but no verify packet is sent */
391 /* XXX: For now, we do not support client
392 * authentication in ECDH cipher suites with
393 * ECDH (rather than ECDSA) certificates.
394 * We need to skip the certificate verify
395 * message when client's ECDH public key is sent
396 * inside the client certificate.
397 */
398 if (s->s3->tmp.cert_req == 1)
399 {
400 s->state=SSL3_ST_CW_CERT_VRFY_A;
401 }
402 else
403 {
404 s->state=SSL3_ST_CW_CHANGE_A;
405 s->s3->change_cipher_spec=0;
406 }
407 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
408 {
409 s->state=SSL3_ST_CW_CHANGE_A;
410 s->s3->change_cipher_spec=0;
411 }
412
413 s->init_num=0;
414 break;
415
416 case SSL3_ST_CW_CERT_VRFY_A:
417 case SSL3_ST_CW_CERT_VRFY_B:
418 ret=ssl3_send_client_verify(s);
419 if (ret <= 0) goto end;
420 s->state=SSL3_ST_CW_CHANGE_A;
421 s->init_num=0;
422 s->s3->change_cipher_spec=0;
423 break;
424
425 case SSL3_ST_CW_CHANGE_A:
426 case SSL3_ST_CW_CHANGE_B:
427 ret=ssl3_send_change_cipher_spec(s,
428 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
429 if (ret <= 0) goto end;
430
Adam Langley1258b6a2014-06-20 12:00:00 -0700431 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700432 if (s->s3->tlsext_channel_id_valid)
433 s->state=SSL3_ST_CW_CHANNEL_ID_A;
434# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700435 if (s->s3->next_proto_neg_seen)
436 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700437# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700438 s->init_num=0;
439
440 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700441 if (!s->method->ssl3_enc->setup_key_block(s))
442 {
443 ret= -1;
444 goto end;
445 }
446
447 if (!s->method->ssl3_enc->change_cipher_state(s,
448 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
449 {
450 ret= -1;
451 goto end;
452 }
453
454 break;
455
David Benjamin6dbd73d2014-07-03 15:59:49 -0400456#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700457 case SSL3_ST_CW_NEXT_PROTO_A:
458 case SSL3_ST_CW_NEXT_PROTO_B:
459 ret=ssl3_send_next_proto(s);
460 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700461 if (s->s3->tlsext_channel_id_valid)
462 s->state=SSL3_ST_CW_CHANNEL_ID_A;
463 else
464 s->state=SSL3_ST_CW_FINISHED_A;
465 break;
466#endif
467
Adam Langley1258b6a2014-06-20 12:00:00 -0700468 case SSL3_ST_CW_CHANNEL_ID_A:
469 case SSL3_ST_CW_CHANNEL_ID_B:
470 ret=ssl3_send_channel_id(s);
471 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700472 s->state=SSL3_ST_CW_FINISHED_A;
473 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700474
475 case SSL3_ST_CW_FINISHED_A:
476 case SSL3_ST_CW_FINISHED_B:
477 ret=ssl3_send_finished(s,
478 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
479 s->method->ssl3_enc->client_finished_label,
480 s->method->ssl3_enc->client_finished_label_len);
481 if (ret <= 0) goto end;
Adam Langleya433cbd2014-06-20 12:00:00 -0700482 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700483 s->state=SSL3_ST_CW_FLUSH;
484
485 /* clear flags */
486 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
487 if (s->hit)
488 {
489 s->s3->tmp.next_state=SSL_ST_OK;
490 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
491 {
492 s->state=SSL_ST_OK;
493 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
494 s->s3->delay_buf_pop_ret=0;
495 }
496 }
497 else
498 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700499 /* This is a non-resumption handshake. If it
500 * involves ChannelID, then record the
501 * handshake hashes at this point in the
502 * session so that any resumption of this
503 * session with ChannelID can sign those
504 * hashes. */
505 if (s->s3->tlsext_channel_id_new)
506 {
507 ret = tls1_record_handshake_hashes_for_channel_id(s);
508 if (ret <= 0)
509 goto end;
510 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700511 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
512 && ssl3_can_cutthrough(s)
513 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
514 )
515 {
516 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
517 {
518 s->state=SSL3_ST_CUTTHROUGH_COMPLETE;
519 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
520 s->s3->delay_buf_pop_ret=0;
521 }
522 else
523 {
524 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
525 }
526 }
Adam Langley95c29f32014-06-20 12:00:00 -0700527 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700528 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700529 /* Allow NewSessionTicket if ticket expected */
530 if (s->tlsext_ticket_expected)
531 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
532 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700533 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535 }
536 s->init_num=0;
537 break;
538
Adam Langley95c29f32014-06-20 12:00:00 -0700539 case SSL3_ST_CR_SESSION_TICKET_A:
540 case SSL3_ST_CR_SESSION_TICKET_B:
541 ret=ssl3_get_new_session_ticket(s);
542 if (ret <= 0) goto end;
543 s->state=SSL3_ST_CR_FINISHED_A;
544 s->init_num=0;
545 break;
546
547 case SSL3_ST_CR_CERT_STATUS_A:
548 case SSL3_ST_CR_CERT_STATUS_B:
549 ret=ssl3_get_cert_status(s);
550 if (ret <= 0) goto end;
551 s->state=SSL3_ST_CR_KEY_EXCH_A;
552 s->init_num=0;
553 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700554
555 case SSL3_ST_CR_FINISHED_A:
556 case SSL3_ST_CR_FINISHED_B:
557
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700558 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700559 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
560 SSL3_ST_CR_FINISHED_B);
561 if (ret <= 0) goto end;
562
563 if (s->hit)
564 s->state=SSL3_ST_CW_CHANGE_A;
565 else
566 s->state=SSL_ST_OK;
567 s->init_num=0;
568 break;
569
570 case SSL3_ST_CW_FLUSH:
571 s->rwstate=SSL_WRITING;
572 if (BIO_flush(s->wbio) <= 0)
573 {
574 ret= -1;
575 goto end;
576 }
577 s->rwstate=SSL_NOTHING;
578 s->state=s->s3->tmp.next_state;
579 break;
580
Adam Langleyadb739e2014-06-20 12:00:00 -0700581 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700582 /* Allow NewSessionTicket if ticket expected */
583 if (s->tlsext_ticket_expected)
584 s->state=SSL3_ST_CR_SESSION_TICKET_A;
585 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700586 s->state=SSL3_ST_CR_FINISHED_A;
587
588 /* SSL_write() will take care of flushing buffered data if
589 * DELAY_CLIENT_FINISHED is set.
590 */
591 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED))
592 ssl_free_wbio_buffer(s);
593 ret = 1;
594 goto end;
595 /* break; */
596
Adam Langley95c29f32014-06-20 12:00:00 -0700597 case SSL_ST_OK:
598 /* clean a few things up */
599 ssl3_cleanup_key_block(s);
600
601 if (s->init_buf != NULL)
602 {
603 BUF_MEM_free(s->init_buf);
604 s->init_buf=NULL;
605 }
606
607 /* If we are not 'joining' the last two packets,
608 * remove the buffering now */
609 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
610 ssl_free_wbio_buffer(s);
611 /* else do it later in ssl3_write */
612
613 s->init_num=0;
614 s->renegotiate=0;
615 s->new_session=0;
616
617 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
618 if (s->hit) s->ctx->stats.sess_hit++;
619
620 ret=1;
621 /* s->server=0; */
622 s->handshake_func=ssl3_connect;
623 s->ctx->stats.sess_connect_good++;
624
625 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
626
627 goto end;
628 /* break; */
629
630 default:
631 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
632 ret= -1;
633 goto end;
634 /* break; */
635 }
636
637 /* did we do anything */
638 if (!s->s3->tmp.reuse_message && !skip)
639 {
640 if (s->debug)
641 {
642 if ((ret=BIO_flush(s->wbio)) <= 0)
643 goto end;
644 }
645
646 if ((cb != NULL) && (s->state != state))
647 {
648 new_state=s->state;
649 s->state=state;
650 cb(s,SSL_CB_CONNECT_LOOP,1);
651 s->state=new_state;
652 }
653 }
654 skip=0;
655 }
656end:
657 s->in_handshake--;
658 if (buf != NULL)
659 BUF_MEM_free(buf);
660 if (cb != NULL)
661 cb(s,SSL_CB_CONNECT_EXIT,ret);
662 return(ret);
663 }
664
665
666int ssl3_client_hello(SSL *s)
667 {
668 unsigned char *buf;
669 unsigned char *p,*d;
670 int i;
671 unsigned long l;
672
673 buf=(unsigned char *)s->init_buf->data;
674 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
675 {
676 SSL_SESSION *sess = s->session;
677 if ((sess == NULL) ||
678 (sess->ssl_version != s->version) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700679 (!sess->session_id_length && !sess->tlsext_tick) ||
Adam Langley95c29f32014-06-20 12:00:00 -0700680 (sess->not_resumable))
681 {
682 if (!ssl_get_new_session(s,0))
683 goto err;
684 }
685 if (s->method->version == DTLS_ANY_VERSION)
686 {
687 /* Determine which DTLS version to use */
688 int options = s->options;
689 /* If DTLS 1.2 disabled correct the version number */
690 if (options & SSL_OP_NO_DTLSv1_2)
691 {
692 if (tls1_suiteb(s))
693 {
694 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
695 goto err;
696 }
697 /* Disabling all versions is silly: return an
698 * error.
699 */
700 if (options & SSL_OP_NO_DTLSv1)
701 {
702 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
703 goto err;
704 }
705 /* Update method so we don't use any DTLS 1.2
706 * features.
707 */
708 s->method = DTLSv1_client_method();
709 s->version = DTLS1_VERSION;
710 }
711 else
712 {
713 /* We only support one version: update method */
714 if (options & SSL_OP_NO_DTLSv1)
715 s->method = DTLSv1_2_client_method();
716 s->version = DTLS1_2_VERSION;
717 }
718 s->client_version = s->version;
719 }
720 /* else use the pre-loaded session */
721
722 p=s->s3->client_random;
723
724 /* for DTLS if client_random is initialized, reuse it, we are
725 * required to use same upon reply to HelloVerify */
726 if (SSL_IS_DTLS(s))
727 {
728 size_t idx;
729 i = 1;
730 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
731 {
732 if (p[idx])
733 {
734 i = 0;
735 break;
736 }
737 }
738 }
739 else
740 i = 1;
741
742 if (i)
743 ssl_fill_hello_random(s, 0, p,
744 sizeof(s->s3->client_random));
745
Adam Langleyb0c235e2014-06-20 12:00:00 -0700746 /* Do the message type and length last.
747 * Note: the final argument to ssl_add_clienthello_tlsext below
748 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700749 d=p= ssl_handshake_start(s);
750
751 /* version indicates the negotiated version: for example from
752 * an SSLv2/v3 compatible client hello). The client_version
753 * field is the maximum version we permit and it is also
754 * used in RSA encrypted premaster secrets. Some servers can
755 * choke if we initially report a higher version then
756 * renegotiate to a lower one in the premaster secret. This
757 * didn't happen with TLS 1.0 as most servers supported it
758 * but it can with TLS 1.1 or later if the server only supports
759 * 1.0.
760 *
761 * Possible scenario with previous logic:
762 * 1. Client hello indicates TLS 1.2
763 * 2. Server hello says TLS 1.0
764 * 3. RSA encrypted premaster secret uses 1.2.
765 * 4. Handhaked proceeds using TLS 1.0.
766 * 5. Server sends hello request to renegotiate.
767 * 6. Client hello indicates TLS v1.0 as we now
768 * know that is maximum server supports.
769 * 7. Server chokes on RSA encrypted premaster secret
770 * containing version 1.0.
771 *
772 * For interoperability it should be OK to always use the
773 * maximum version we support in client hello and then rely
774 * on the checking of version to ensure the servers isn't
775 * being inconsistent: for example initially negotiating with
776 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
777 * client_version in client hello and not resetting it to
778 * the negotiated version.
779 */
780#if 0
781 *(p++)=s->version>>8;
782 *(p++)=s->version&0xff;
783 s->client_version=s->version;
784#else
785 *(p++)=s->client_version>>8;
786 *(p++)=s->client_version&0xff;
787#endif
788
789 /* Random stuff */
790 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
791 p+=SSL3_RANDOM_SIZE;
792
793 /* Session ID */
794 if (s->new_session)
795 i=0;
796 else
797 i=s->session->session_id_length;
798 *(p++)=i;
799 if (i != 0)
800 {
801 if (i > (int)sizeof(s->session->session_id))
802 {
803 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
804 goto err;
805 }
806 memcpy(p,s->session->session_id,i);
807 p+=i;
808 }
809
810 /* cookie stuff for DTLS */
811 if (SSL_IS_DTLS(s))
812 {
813 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
814 {
815 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
816 goto err;
817 }
818 *(p++) = s->d1->cookie_len;
819 memcpy(p, s->d1->cookie, s->d1->cookie_len);
820 p += s->d1->cookie_len;
821 }
822
823 /* Ciphers supported */
824 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
825 if (i == 0)
826 {
827 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
828 goto err;
829 }
830#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
831 /* Some servers hang if client hello > 256 bytes
832 * as hack workaround chop number of supported ciphers
833 * to keep it well below this if we use TLS v1.2
834 */
835 if (TLS1_get_version(s) >= TLS1_2_VERSION
836 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
837 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
838#endif
839 s2n(i,p);
840 p+=i;
841
842 /* COMPRESSION */
843 *(p++)=1;
844 *(p++)=0; /* Add the NULL method */
845
Adam Langley95c29f32014-06-20 12:00:00 -0700846 /* TLS extensions*/
847 if (ssl_prepare_clienthello_tlsext(s) <= 0)
848 {
849 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
850 goto err;
851 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700852 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700853 {
854 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
855 goto err;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
858 l= p-d;
859 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
860 s->state=SSL3_ST_CW_CLNT_HELLO_B;
861 }
862
863 /* SSL3_ST_CW_CLNT_HELLO_B */
864 return ssl_do_write(s);
865err:
866 return(-1);
867 }
868
869int ssl3_get_server_hello(SSL *s)
870 {
871 STACK_OF(SSL_CIPHER) *sk;
872 const SSL_CIPHER *c;
873 CERT *ct = s->cert;
874 unsigned char *p,*d;
875 int al=SSL_AD_INTERNAL_ERROR,ok;
876 unsigned int j;
877 long n;
David Benjamin03973092014-06-24 23:27:17 -0400878 CBS cbs;
Adam Langley95c29f32014-06-20 12:00:00 -0700879 /* Hello verify request and/or server hello version may not
880 * match so set first packet if we're negotiating version.
881 */
882 if (SSL_IS_DTLS(s))
883 s->first_packet = 1;
884
885 n=s->method->ssl_get_message(s,
886 SSL3_ST_CR_SRVR_HELLO_A,
887 SSL3_ST_CR_SRVR_HELLO_B,
888 -1,
889 20000, /* ?? */
890 &ok);
891
892 if (!ok) return((int)n);
893
894 if (SSL_IS_DTLS(s))
895 {
896 s->first_packet = 0;
897 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
898 {
899 if ( s->d1->send_cookie == 0)
900 {
901 s->s3->tmp.reuse_message = 1;
902 return 1;
903 }
904 else /* already sent a cookie */
905 {
906 al=SSL_AD_UNEXPECTED_MESSAGE;
907 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
908 goto f_err;
909 }
910 }
911 }
912
913 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
914 {
915 al=SSL_AD_UNEXPECTED_MESSAGE;
916 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
917 goto f_err;
918 }
919
920 d=p=(unsigned char *)s->init_msg;
921 if (s->method->version == DTLS_ANY_VERSION)
922 {
923 /* Work out correct protocol version to use */
924 int hversion = (p[0] << 8)|p[1];
925 int options = s->options;
926 if (hversion == DTLS1_2_VERSION
927 && !(options & SSL_OP_NO_DTLSv1_2))
928 s->method = DTLSv1_2_client_method();
929 else if (tls1_suiteb(s))
930 {
931 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
932 s->version = hversion;
933 al = SSL_AD_PROTOCOL_VERSION;
934 goto f_err;
935 }
936 else if (hversion == DTLS1_VERSION
937 && !(options & SSL_OP_NO_DTLSv1))
938 s->method = DTLSv1_client_method();
939 else
940 {
941 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
942 s->version = hversion;
943 al = SSL_AD_PROTOCOL_VERSION;
944 goto f_err;
945 }
946 s->version = s->client_version = s->method->version;
947 }
948
949 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
950 {
951 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
952 s->version=(s->version&0xff00)|p[1];
953 al=SSL_AD_PROTOCOL_VERSION;
954 goto f_err;
955 }
956 p+=2;
957
958 /* load the server hello data */
959 /* load the server random */
960 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
961 p+=SSL3_RANDOM_SIZE;
962
963 /* get the session-id */
964 j= *(p++);
965
966 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
967 {
968 al=SSL_AD_ILLEGAL_PARAMETER;
969 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_SSL3_SESSION_ID_TOO_LONG);
970 goto f_err;
971 }
972
Adam Langley95c29f32014-06-20 12:00:00 -0700973 /* check if we want to resume the session based on external pre-shared secret */
974 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
975 {
976 SSL_CIPHER *pref_cipher=NULL;
977 s->session->master_key_length=sizeof(s->session->master_key);
978 if (s->tls_session_secret_cb(s, s->session->master_key,
979 &s->session->master_key_length,
980 NULL, &pref_cipher,
981 s->tls_session_secret_cb_arg))
982 {
983 s->session->cipher = pref_cipher ?
984 pref_cipher : ssl_get_cipher_by_char(s, p+j);
Adam Langley6acf4762014-06-20 12:00:00 -0700985 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700986 }
987 }
Adam Langley95c29f32014-06-20 12:00:00 -0700988
989 if (j != 0 && j == s->session->session_id_length
990 && memcmp(p,s->session->session_id,j) == 0)
991 {
992 if(s->sid_ctx_length != s->session->sid_ctx_length
993 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
994 {
995 /* actually a client application bug */
996 al=SSL_AD_ILLEGAL_PARAMETER;
997 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
998 goto f_err;
999 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001000 s->s3->flags |= SSL3_FLAGS_CCS_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07001001 s->hit=1;
1002 }
1003 else /* a miss or crap from the other end */
1004 {
1005 /* If we were trying for session-id reuse, make a new
1006 * SSL_SESSION so we don't stuff up other people */
1007 s->hit=0;
1008 if (s->session->session_id_length > 0)
1009 {
1010 if (!ssl_get_new_session(s,0))
1011 {
1012 goto f_err;
1013 }
1014 }
1015 s->session->session_id_length=j;
1016 memcpy(s->session->session_id,p,j); /* j could be 0 */
1017 }
1018 p+=j;
1019 c=ssl_get_cipher_by_char(s,p);
1020 if (c == NULL)
1021 {
1022 /* unknown cipher */
1023 al=SSL_AD_ILLEGAL_PARAMETER;
1024 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
1025 goto f_err;
1026 }
1027 /* If it is a disabled cipher we didn't send it in client hello,
1028 * so return an error.
1029 */
1030 if (c->algorithm_ssl & ct->mask_ssl ||
1031 c->algorithm_mkey & ct->mask_k ||
1032 c->algorithm_auth & ct->mask_a)
1033 {
1034 al=SSL_AD_ILLEGAL_PARAMETER;
1035 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1036 goto f_err;
1037 }
1038 p+=ssl_put_cipher_by_char(s,NULL,NULL);
1039
1040 sk=ssl_get_ciphers_by_id(s);
1041 if (!sk_SSL_CIPHER_find(sk, NULL, c))
1042 {
1043 /* we did not say we would use this cipher */
1044 al=SSL_AD_ILLEGAL_PARAMETER;
1045 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1046 goto f_err;
1047 }
1048
1049 /* Depending on the session caching (internal/external), the cipher
1050 and/or cipher_id values may not be set. Make sure that
1051 cipher_id is set and use it for comparison. */
1052 if (s->session->cipher)
1053 s->session->cipher_id = s->session->cipher->id;
1054 if (s->hit && (s->session->cipher_id != c->id))
1055 {
1056/* Workaround is now obsolete */
1057#if 0
1058 if (!(s->options &
1059 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1060#endif
1061 {
1062 al=SSL_AD_ILLEGAL_PARAMETER;
1063 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1064 goto f_err;
1065 }
1066 }
1067 s->s3->tmp.new_cipher=c;
1068 /* Don't digest cached records if no sigalgs: we may need them for
1069 * client authentication.
1070 */
1071 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1072 goto f_err;
1073 /* lets get the compression algorithm */
1074 /* COMPRESSION */
1075 if (*(p++) != 0)
1076 {
1077 al=SSL_AD_ILLEGAL_PARAMETER;
1078 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1079 goto f_err;
1080 }
Adam Langley95c29f32014-06-20 12:00:00 -07001081
David Benjamin03973092014-06-24 23:27:17 -04001082 /* TODO(fork): Port the rest of this function to CBS. */
1083 CBS_init(&cbs, p, d + n - p);
Adam Langley95c29f32014-06-20 12:00:00 -07001084 /* TLS extensions*/
David Benjamin03973092014-06-24 23:27:17 -04001085 if (!ssl_parse_serverhello_tlsext(s, &cbs))
Adam Langley95c29f32014-06-20 12:00:00 -07001086 {
1087 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
1088 goto err;
1089 }
Adam Langley95c29f32014-06-20 12:00:00 -07001090
David Benjamin03973092014-06-24 23:27:17 -04001091 /* There should be nothing left over in the record. */
1092 if (CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001093 {
1094 /* wrong packet length */
1095 al=SSL_AD_DECODE_ERROR;
1096 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
1097 goto f_err;
1098 }
1099
1100 return(1);
1101f_err:
1102 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1103err:
1104 return(-1);
1105 }
1106
1107int ssl3_get_server_certificate(SSL *s)
1108 {
1109 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001110 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001111 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001112 STACK_OF(X509) *sk=NULL;
1113 SESS_CERT *sc;
1114 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001115 CBS cbs, certificate_list;
1116 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001117
1118 n=s->method->ssl_get_message(s,
1119 SSL3_ST_CR_CERT_A,
1120 SSL3_ST_CR_CERT_B,
1121 -1,
1122 s->max_cert_list,
1123 &ok);
1124
1125 if (!ok) return((int)n);
1126
David Benjamind26aea62014-07-12 00:13:56 -04001127 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
Adam Langley95c29f32014-06-20 12:00:00 -07001128 {
1129 s->s3->tmp.reuse_message=1;
1130 return(1);
1131 }
1132
1133 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1134 {
1135 al=SSL_AD_UNEXPECTED_MESSAGE;
1136 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_BAD_MESSAGE_TYPE);
1137 goto f_err;
1138 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001139 CBS_init(&cbs, (uint8_t *)s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001140
1141 if ((sk=sk_X509_new_null()) == NULL)
1142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1144 goto err;
1145 }
1146
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001147 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1148 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001149 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001150 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001151 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1152 goto f_err;
1153 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001154
1155 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001156 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001157 CBS certificate;
1158 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001159 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001160 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001161 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1162 goto f_err;
1163 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001164 data = CBS_data(&certificate);
1165 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001166 if (x == NULL)
1167 {
1168 al=SSL_AD_BAD_CERTIFICATE;
1169 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1170 goto f_err;
1171 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001172 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001173 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001174 al = SSL_AD_INTERNAL_ERROR;
1175 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1176 goto f_err;
1177 }
1178 if (CBS_len(&certificate) != 0)
1179 {
1180 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001181 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1182 goto f_err;
1183 }
1184 if (!sk_X509_push(sk,x))
1185 {
1186 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1187 goto err;
1188 }
1189 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
1191
1192 i=ssl_verify_cert_chain(s,sk);
1193 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1194 )
1195 {
1196 al=ssl_verify_alarm_type(s->verify_result);
1197 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1198 goto f_err;
1199 }
1200 ERR_clear_error(); /* but we keep s->verify_result */
1201
1202 sc=ssl_sess_cert_new();
1203 if (sc == NULL) goto err;
1204
1205 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1206 s->session->sess_cert=sc;
1207
1208 sc->cert_chain=sk;
1209 /* Inconsistency alert: cert_chain does include the peer's
1210 * certificate, which we don't include in s3_srvr.c */
1211 x=sk_X509_value(sk,0);
1212 sk=NULL;
1213 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1214
1215 pkey=X509_get_pubkey(x);
1216
David Benjamind26aea62014-07-12 00:13:56 -04001217 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001218 {
1219 x=NULL;
1220 al=SSL3_AL_FATAL;
1221 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1222 goto f_err;
1223 }
1224
1225 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001226 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001227 {
1228 x=NULL;
1229 al=SSL3_AL_FATAL;
1230 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1231 goto f_err;
1232 }
1233
David Benjamind26aea62014-07-12 00:13:56 -04001234 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1235 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001236 {
David Benjamind26aea62014-07-12 00:13:56 -04001237 x=NULL;
1238 al=SSL_AD_ILLEGAL_PARAMETER;
1239 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1240 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001241 }
David Benjamind26aea62014-07-12 00:13:56 -04001242 sc->peer_cert_type=i;
1243 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1244 /* Why would the following ever happen?
1245 * We just created sc a couple of lines ago. */
1246 if (sc->peer_pkeys[i].x509 != NULL)
1247 X509_free(sc->peer_pkeys[i].x509);
1248 sc->peer_pkeys[i].x509=x;
1249 sc->peer_key= &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001250
David Benjamind26aea62014-07-12 00:13:56 -04001251 if (s->session->peer != NULL)
1252 X509_free(s->session->peer);
1253 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1254 s->session->peer=x;
1255
Adam Langley95c29f32014-06-20 12:00:00 -07001256 s->session->verify_result = s->verify_result;
1257
1258 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001259 ret=1;
1260 if (0)
1261 {
1262f_err:
1263 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1264 }
1265err:
1266 EVP_PKEY_free(pkey);
1267 X509_free(x);
1268 sk_X509_pop_free(sk,X509_free);
1269 return(ret);
1270 }
1271
1272int ssl3_get_key_exchange(SSL *s)
1273 {
Adam Langley95c29f32014-06-20 12:00:00 -07001274 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
Adam Langley95c29f32014-06-20 12:00:00 -07001275 EVP_MD_CTX md_ctx;
1276 unsigned char *param,*p;
1277 int al,i,j,param_len,ok;
1278 long n,alg_k,alg_a;
1279 EVP_PKEY *pkey=NULL;
1280 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001281 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001282#ifndef OPENSSL_NO_DH
1283 DH *dh=NULL;
1284#endif
1285#ifndef OPENSSL_NO_ECDH
1286 EC_KEY *ecdh = NULL;
1287 BN_CTX *bn_ctx = NULL;
1288 EC_POINT *srvr_ecpoint = NULL;
1289 int curve_nid = 0;
1290 int encoded_pt_len = 0;
1291#endif
1292
1293 /* use same message size as in ssl3_get_certificate_request()
1294 * as ServerKeyExchange message may be skipped */
1295 n=s->method->ssl_get_message(s,
1296 SSL3_ST_CR_KEY_EXCH_A,
1297 SSL3_ST_CR_KEY_EXCH_B,
1298 -1,
1299 s->max_cert_list,
1300 &ok);
1301 if (!ok) return((int)n);
1302
1303 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1304 {
1305#ifndef OPENSSL_NO_PSK
1306 /* In plain PSK ciphersuite, ServerKeyExchange can be
1307 omitted if no identity hint is sent. Set
1308 session->sess_cert anyway to avoid problems
1309 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001310 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001311 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001312 /* PSK ciphersuites that also send a
1313 * Certificate would have already initialized
1314 * |sess_cert|. */
1315 if (s->session->sess_cert == NULL)
1316 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001317 if (s->session->psk_identity_hint)
1318 {
1319 OPENSSL_free(s->session->psk_identity_hint);
1320 s->session->psk_identity_hint = NULL;
1321 }
Adam Langley95c29f32014-06-20 12:00:00 -07001322 }
1323#endif
1324 s->s3->tmp.reuse_message=1;
1325 return(1);
1326 }
1327
1328 param=p=(unsigned char *)s->init_msg;
1329 if (s->session->sess_cert != NULL)
1330 {
Adam Langley95c29f32014-06-20 12:00:00 -07001331 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1332 {
1333 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1334 s->session->sess_cert->peer_rsa_tmp=NULL;
1335 }
Adam Langley95c29f32014-06-20 12:00:00 -07001336#ifndef OPENSSL_NO_DH
1337 if (s->session->sess_cert->peer_dh_tmp)
1338 {
1339 DH_free(s->session->sess_cert->peer_dh_tmp);
1340 s->session->sess_cert->peer_dh_tmp=NULL;
1341 }
1342#endif
1343#ifndef OPENSSL_NO_ECDH
1344 if (s->session->sess_cert->peer_ecdh_tmp)
1345 {
1346 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1347 s->session->sess_cert->peer_ecdh_tmp=NULL;
1348 }
1349#endif
1350 }
1351 else
1352 {
1353 s->session->sess_cert=ssl_sess_cert_new();
1354 }
1355
1356 param_len=0;
1357 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1358 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1359 EVP_MD_CTX_init(&md_ctx);
1360
1361#ifndef OPENSSL_NO_PSK
Adam Langleyc26c8022014-06-20 12:00:00 -07001362 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001363 {
1364 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1365
1366 al=SSL_AD_HANDSHAKE_FAILURE;
1367 n2s(p,i);
1368 param_len=i+2;
Adam Langley0289c732014-06-20 12:00:00 -07001369 if (s->session->psk_identity_hint)
1370 {
1371 OPENSSL_free(s->session->psk_identity_hint);
1372 s->session->psk_identity_hint = NULL;
1373 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001374 if (i != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001375 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001376 /* Store PSK identity hint for later use, hint is used
1377 * in ssl3_send_client_key_exchange. Assume that the
1378 * maximum length of a PSK identity hint can be as
1379 * long as the maximum length of a PSK identity. */
1380 if (i > PSK_MAX_IDENTITY_LEN)
1381 {
1382 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
1383 goto f_err;
1384 }
1385 if (param_len > n)
1386 {
1387 al=SSL_AD_DECODE_ERROR;
1388 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1389 goto f_err;
1390 }
1391 /* If received PSK identity hint contains NULL
1392 * characters, the hint is truncated from the first
1393 * NULL. p may not be ending with NULL, so create a
1394 * NULL-terminated string. */
1395 memcpy(tmp_id_hint, p, i);
1396 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
Adam Langley0289c732014-06-20 12:00:00 -07001397 s->session->psk_identity_hint = BUF_strdup(tmp_id_hint);
1398 if (s->session->psk_identity_hint == NULL)
Adam Langleyc26c8022014-06-20 12:00:00 -07001399 {
1400 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1401 goto f_err;
1402 }
1403 }
Adam Langley95c29f32014-06-20 12:00:00 -07001404
1405 p+=i;
1406 n-=param_len;
1407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408#endif /* !OPENSSL_NO_PSK */
Adam Langleyc26c8022014-06-20 12:00:00 -07001409
1410 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001411 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001412 {
1413 if ((rsa=RSA_new()) == NULL)
1414 {
1415 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1416 goto err;
1417 }
1418 n2s(p,i);
1419 param_len=i+2;
1420 if (param_len > n)
1421 {
1422 al=SSL_AD_DECODE_ERROR;
1423 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_MODULUS_LENGTH);
1424 goto f_err;
1425 }
1426 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1427 {
1428 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1429 goto err;
1430 }
1431 p+=i;
1432
1433 n2s(p,i);
1434 param_len+=i+2;
1435 if (param_len > n)
1436 {
1437 al=SSL_AD_DECODE_ERROR;
1438 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_E_LENGTH);
1439 goto f_err;
1440 }
1441 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1442 {
1443 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1444 goto err;
1445 }
1446 p+=i;
1447 n-=param_len;
1448
1449 /* this should be because we are using an export cipher */
1450 if (alg_a & SSL_aRSA)
1451 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1452 else
1453 {
1454 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1455 goto err;
1456 }
1457 s->session->sess_cert->peer_rsa_tmp=rsa;
1458 rsa=NULL;
1459 }
Adam Langley95c29f32014-06-20 12:00:00 -07001460#ifndef OPENSSL_NO_DH
1461 else if (alg_k & SSL_kEDH)
1462 {
1463 if ((dh=DH_new()) == NULL)
1464 {
1465 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_DH_LIB);
1466 goto err;
1467 }
1468 n2s(p,i);
1469 param_len=i+2;
1470 if (param_len > n)
1471 {
1472 al=SSL_AD_DECODE_ERROR;
1473 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1474 goto f_err;
1475 }
1476 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1477 {
1478 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1479 goto err;
1480 }
1481 p+=i;
1482
1483 n2s(p,i);
1484 param_len+=i+2;
1485 if (param_len > n)
1486 {
1487 al=SSL_AD_DECODE_ERROR;
1488 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_G_LENGTH);
1489 goto f_err;
1490 }
1491 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1492 {
1493 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1494 goto err;
1495 }
1496 p+=i;
1497
1498 n2s(p,i);
1499 param_len+=i+2;
1500 if (param_len > n)
1501 {
1502 al=SSL_AD_DECODE_ERROR;
1503 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1504 goto f_err;
1505 }
1506 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1507 {
1508 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB);
1509 goto err;
1510 }
1511 p+=i;
1512 n-=param_len;
1513
Adam Langley95c29f32014-06-20 12:00:00 -07001514 if (alg_a & SSL_aRSA)
1515 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001516#ifndef OPENSSL_NO_DSA
1517 else if (alg_a & SSL_aDSS)
1518 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1519#endif
1520 /* else anonymous DH, so no certificate or pkey. */
1521
1522 s->session->sess_cert->peer_dh_tmp=dh;
1523 dh=NULL;
1524 }
1525 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1526 {
1527 al=SSL_AD_ILLEGAL_PARAMETER;
1528 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1529 goto f_err;
1530 }
1531#endif /* !OPENSSL_NO_DH */
1532
1533#ifndef OPENSSL_NO_ECDH
1534 else if (alg_k & SSL_kEECDH)
1535 {
1536 EC_GROUP *ngroup;
1537 const EC_GROUP *group;
1538
1539 if ((ecdh=EC_KEY_new()) == NULL)
1540 {
1541 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1542 goto err;
1543 }
1544
1545 /* Extract elliptic curve parameters and the
1546 * server's ephemeral ECDH public key.
1547 * Keep accumulating lengths of various components in
1548 * param_len and make sure it never exceeds n.
1549 */
1550
1551 /* XXX: For now we only support named (not generic) curves
1552 * and the ECParameters in this case is just three bytes.
1553 */
1554 param_len=3;
1555 /* Check curve is one of our prefrences, if not server has
1556 * sent an invalid curve.
1557 */
1558 if (!tls1_check_curve(s, p, param_len))
1559 {
1560 al=SSL_AD_DECODE_ERROR;
1561 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_CURVE);
1562 goto f_err;
1563 }
1564
1565 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)
1566 {
1567 al=SSL_AD_INTERNAL_ERROR;
1568 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1569 goto f_err;
1570 }
1571
1572 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1573 if (ngroup == NULL)
1574 {
1575 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1576 goto err;
1577 }
1578 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1579 {
1580 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB);
1581 goto err;
1582 }
1583 EC_GROUP_free(ngroup);
1584
1585 group = EC_KEY_get0_group(ecdh);
1586
1587 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1588 (EC_GROUP_get_degree(group) > 163))
1589 {
1590 al=SSL_AD_EXPORT_RESTRICTION;
1591 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1592 goto f_err;
1593 }
1594
1595 p+=3;
1596
1597 /* Next, get the encoded ECPoint */
1598 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1599 ((bn_ctx = BN_CTX_new()) == NULL))
1600 {
1601 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE);
1602 goto err;
1603 }
1604
1605 encoded_pt_len = *p; /* length of encoded point */
1606 p+=1;
1607 param_len += (1 + encoded_pt_len);
1608 if ((param_len > n) ||
1609 (EC_POINT_oct2point(group, srvr_ecpoint,
1610 p, encoded_pt_len, bn_ctx) == 0))
1611 {
1612 al=SSL_AD_DECODE_ERROR;
1613 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_ECPOINT);
1614 goto f_err;
1615 }
1616
1617 n-=param_len;
1618 p+=encoded_pt_len;
1619
1620 /* The ECC/TLS specification does not mention
1621 * the use of DSA to sign ECParameters in the server
1622 * key exchange message. We do support RSA and ECDSA.
1623 */
1624 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001625 else if (alg_a & SSL_aRSA)
1626 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001627#ifndef OPENSSL_NO_ECDSA
1628 else if (alg_a & SSL_aECDSA)
1629 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1630#endif
1631 /* else anonymous ECDH, so no certificate or pkey. */
1632 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1633 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1634 ecdh=NULL;
1635 BN_CTX_free(bn_ctx);
1636 bn_ctx = NULL;
1637 EC_POINT_free(srvr_ecpoint);
1638 srvr_ecpoint = NULL;
1639 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001640#endif /* !OPENSSL_NO_ECDH */
1641
1642 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001643 {
1644 al=SSL_AD_UNEXPECTED_MESSAGE;
1645 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
1646 goto f_err;
1647 }
Adam Langley95c29f32014-06-20 12:00:00 -07001648
1649 /* p points to the next byte, there are 'n' bytes left */
1650
1651 /* if it was signed, check the signature */
1652 if (pkey != NULL)
1653 {
1654 if (SSL_USE_SIGALGS(s))
1655 {
1656 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1657 if (rv == -1)
1658 goto err;
1659 else if (rv == 0)
1660 {
1661 al = SSL_AD_DECODE_ERROR;
1662 goto f_err;
1663 }
1664#ifdef SSL_DEBUG
1665fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1666#endif
1667 p += 2;
1668 n -= 2;
1669 }
1670 else
1671 md = EVP_sha1();
1672
1673 n2s(p,i);
1674 n-=2;
1675 j=EVP_PKEY_size(pkey);
1676
1677 if ((i != n) || (n > j) || (n <= 0))
1678 {
1679 /* wrong packet length */
1680 al=SSL_AD_DECODE_ERROR;
1681 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_SIGNATURE_LENGTH);
1682 goto f_err;
1683 }
1684
Adam Langley95c29f32014-06-20 12:00:00 -07001685 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1686 {
1687 int num;
1688
1689 j=0;
1690 q=md_buf;
1691 for (num=2; num > 0; num--)
1692 {
1693 EVP_DigestInit_ex(&md_ctx,(num == 2)
1694 ?s->ctx->md5:s->ctx->sha1, NULL);
1695 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1696 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1697 EVP_DigestUpdate(&md_ctx,param,param_len);
1698 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1699 q+=i;
1700 j+=i;
1701 }
1702 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1703 pkey->pkey.rsa);
1704 if (i < 0)
1705 {
1706 al=SSL_AD_DECRYPT_ERROR;
1707 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_DECRYPT);
1708 goto f_err;
1709 }
1710 if (i == 0)
1711 {
1712 /* bad signature */
1713 al=SSL_AD_DECRYPT_ERROR;
1714 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1715 goto f_err;
1716 }
1717 }
1718 else
Adam Langley95c29f32014-06-20 12:00:00 -07001719 {
1720 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1721 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1722 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1723 EVP_VerifyUpdate(&md_ctx,param,param_len);
1724 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1725 {
1726 /* bad signature */
1727 al=SSL_AD_DECRYPT_ERROR;
1728 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE);
1729 goto f_err;
1730 }
1731 }
1732 }
1733 else
1734 {
Adam Langleyc26c8022014-06-20 12:00:00 -07001735 if (!(alg_a & SSL_aNULL) &&
1736 /* Among PSK ciphers only RSA_PSK needs a public key */
1737 !((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07001738 {
1739 /* Might be wrong key type, check it */
1740 if (ssl3_check_cert_and_algorithm(s))
1741 /* Otherwise this shouldn't happen */
1742 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR);
1743 goto err;
1744 }
1745 /* still data left over */
1746 if (n != 0)
1747 {
1748 al=SSL_AD_DECODE_ERROR;
1749 OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
1750 goto f_err;
1751 }
1752 }
1753 EVP_PKEY_free(pkey);
1754 EVP_MD_CTX_cleanup(&md_ctx);
1755 return(1);
1756f_err:
1757 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1758err:
1759 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001760 if (rsa != NULL)
1761 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001762#ifndef OPENSSL_NO_DH
1763 if (dh != NULL)
1764 DH_free(dh);
1765#endif
1766#ifndef OPENSSL_NO_ECDH
1767 BN_CTX_free(bn_ctx);
1768 EC_POINT_free(srvr_ecpoint);
1769 if (ecdh != NULL)
1770 EC_KEY_free(ecdh);
1771#endif
1772 EVP_MD_CTX_cleanup(&md_ctx);
1773 return(-1);
1774 }
1775
1776static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1777 {
1778 return(X509_NAME_cmp(*a,*b));
1779 }
1780
1781int ssl3_get_certificate_request(SSL *s)
1782 {
1783 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001784 unsigned long n;
1785 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001786 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001787 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001788 CBS cbs;
1789 CBS certificate_types;
1790 CBS certificate_authorities;
1791 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001792
1793 n=s->method->ssl_get_message(s,
1794 SSL3_ST_CR_CERT_REQ_A,
1795 SSL3_ST_CR_CERT_REQ_B,
1796 -1,
1797 s->max_cert_list,
1798 &ok);
1799
1800 if (!ok) return((int)n);
1801
1802 s->s3->tmp.cert_req=0;
1803
1804 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1805 {
1806 s->s3->tmp.reuse_message=1;
1807 /* If we get here we don't need any cached handshake records
1808 * as we wont be doing client auth.
1809 */
1810 if (s->s3->handshake_buffer)
1811 {
1812 if (!ssl3_digest_cached_records(s))
1813 goto err;
1814 }
1815 return(1);
1816 }
1817
1818 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1819 {
1820 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1821 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1822 goto err;
1823 }
1824
1825 /* TLS does not like anon-DH with client cert */
1826 if (s->version > SSL3_VERSION)
1827 {
1828 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1829 {
1830 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1831 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1832 goto err;
1833 }
1834 }
1835
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001836 CBS_init(&cbs, (uint8_t *)s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001837
1838 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1839 if (ca_sk == NULL)
1840 {
1841 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1842 goto err;
1843 }
1844
1845 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001846 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001847 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001848 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1849 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1850 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001851 }
David Benjamin676d1e72014-07-08 14:34:10 -04001852 if (!CBS_stow(&certificate_types,
1853 &s->s3->tmp.certificate_types,
1854 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001855 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001856 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1857 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001858 }
Adam Langley95c29f32014-06-20 12:00:00 -07001859 if (SSL_USE_SIGALGS(s))
1860 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001861 CBS supported_signature_algorithms;
1862 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001863 {
1864 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001865 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001866 goto err;
1867 }
1868 /* Clear certificate digests and validity flags */
1869 for (i = 0; i < SSL_PKEY_NUM; i++)
1870 {
1871 s->cert->pkeys[i].digest = NULL;
1872 s->cert->pkeys[i].valid_flags = 0;
1873 }
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001874 if (!tls1_process_sigalgs(s,
1875 CBS_data(&supported_signature_algorithms),
1876 CBS_len(&supported_signature_algorithms)))
Adam Langley95c29f32014-06-20 12:00:00 -07001877 {
1878 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1879 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1880 goto err;
1881 }
Adam Langley95c29f32014-06-20 12:00:00 -07001882 }
1883
1884 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001885 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001886 {
1887 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1888 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1889 goto err;
1890 }
1891
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001892 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001893 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001894 CBS distinguished_name;
1895 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001896 {
Adam Langley95c29f32014-06-20 12:00:00 -07001897 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1898 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1899 goto err;
1900 }
1901
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001902 data = CBS_data(&distinguished_name);
1903 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001904 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001905 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1906 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1907 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001908 }
1909
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001910 if (CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
1911 {
1912 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1913 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1914 goto err;
1915 }
1916 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001917 {
1918 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1919 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1920 goto err;
1921 }
1922 if (!sk_X509_NAME_push(ca_sk,xn))
1923 {
1924 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1925 goto err;
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927 }
1928
Adam Langley95c29f32014-06-20 12:00:00 -07001929 /* we should setup a certificate to return.... */
1930 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001931 if (s->s3->tmp.ca_names != NULL)
1932 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1933 s->s3->tmp.ca_names=ca_sk;
1934 ca_sk=NULL;
1935
1936 ret=1;
1937err:
1938 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1939 return(ret);
1940 }
1941
Adam Langley95c29f32014-06-20 12:00:00 -07001942int ssl3_get_new_session_ticket(SSL *s)
1943 {
1944 int ok,al,ret=0, ticklen;
1945 long n;
1946 const unsigned char *p;
1947 unsigned char *d;
1948
1949 n=s->method->ssl_get_message(s,
1950 SSL3_ST_CR_SESSION_TICKET_A,
1951 SSL3_ST_CR_SESSION_TICKET_B,
1952 -1,
1953 16384,
1954 &ok);
1955
1956 if (!ok)
1957 return((int)n);
1958
1959 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1960 {
1961 s->s3->tmp.reuse_message=1;
1962 return(1);
1963 }
1964 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1965 {
1966 al=SSL_AD_UNEXPECTED_MESSAGE;
1967 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE);
1968 goto f_err;
1969 }
1970 if (n < 6)
1971 {
1972 /* need at least ticket_lifetime_hint + ticket length */
1973 al = SSL_AD_DECODE_ERROR;
1974 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1975 goto f_err;
1976 }
1977
1978 p=d=(unsigned char *)s->init_msg;
1979 n2l(p, s->session->tlsext_tick_lifetime_hint);
1980 n2s(p, ticklen);
1981 /* ticket_lifetime_hint + ticket_length + ticket */
1982 if (ticklen + 6 != n)
1983 {
1984 al = SSL_AD_DECODE_ERROR;
1985 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH);
1986 goto f_err;
1987 }
1988 if (s->session->tlsext_tick)
1989 {
1990 OPENSSL_free(s->session->tlsext_tick);
1991 s->session->tlsext_ticklen = 0;
1992 }
1993 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1994 if (!s->session->tlsext_tick)
1995 {
1996 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1997 goto err;
1998 }
1999 memcpy(s->session->tlsext_tick, p, ticklen);
2000 s->session->tlsext_ticklen = ticklen;
2001 /* There are two ways to detect a resumed ticket sesion.
2002 * One is to set an appropriate session ID and then the server
2003 * must return a match in ServerHello. This allows the normal
2004 * client session ID matching to work and we know much
2005 * earlier that the ticket has been accepted.
2006 *
2007 * The other way is to set zero length session ID when the
2008 * ticket is presented and rely on the handshake to determine
2009 * session resumption.
2010 *
2011 * We choose the former approach because this fits in with
2012 * assumptions elsewhere in OpenSSL. The session ID is set
2013 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2014 * ticket.
2015 */
2016 EVP_Digest(p, ticklen,
2017 s->session->session_id, &s->session->session_id_length,
2018#ifndef OPENSSL_NO_SHA256
2019 EVP_sha256(), NULL);
2020#else
2021 EVP_sha1(), NULL);
2022#endif
2023 ret=1;
2024 return(ret);
2025f_err:
2026 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2027err:
2028 return(-1);
2029 }
2030
2031int ssl3_get_cert_status(SSL *s)
2032 {
2033 int ok, al;
2034 unsigned long resplen,n;
2035 const unsigned char *p;
2036
2037 n=s->method->ssl_get_message(s,
2038 SSL3_ST_CR_CERT_STATUS_A,
2039 SSL3_ST_CR_CERT_STATUS_B,
2040 SSL3_MT_CERTIFICATE_STATUS,
2041 16384,
2042 &ok);
2043
2044 if (!ok) return((int)n);
2045 if (n < 4)
2046 {
2047 /* need at least status type + length */
2048 al = SSL_AD_DECODE_ERROR;
2049 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2050 goto f_err;
2051 }
2052 p = (unsigned char *)s->init_msg;
2053 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2054 {
2055 al = SSL_AD_DECODE_ERROR;
2056 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_UNSUPPORTED_STATUS_TYPE);
2057 goto f_err;
2058 }
2059 n2l3(p, resplen);
2060 if (resplen + 4 != n)
2061 {
2062 al = SSL_AD_DECODE_ERROR;
2063 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH);
2064 goto f_err;
2065 }
2066 if (s->tlsext_ocsp_resp)
2067 OPENSSL_free(s->tlsext_ocsp_resp);
2068 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2069 if (!s->tlsext_ocsp_resp)
2070 {
2071 al = SSL_AD_INTERNAL_ERROR;
2072 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2073 goto f_err;
2074 }
2075 s->tlsext_ocsp_resplen = resplen;
2076 if (s->ctx->tlsext_status_cb)
2077 {
2078 int ret;
2079 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2080 if (ret == 0)
2081 {
2082 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2083 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
2084 goto f_err;
2085 }
2086 if (ret < 0)
2087 {
2088 al = SSL_AD_INTERNAL_ERROR;
2089 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
2090 goto f_err;
2091 }
2092 }
2093 return 1;
2094f_err:
2095 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2096 return(-1);
2097 }
Adam Langley95c29f32014-06-20 12:00:00 -07002098
2099int ssl3_get_server_done(SSL *s)
2100 {
2101 int ok,ret=0;
2102 long n;
2103
2104 n=s->method->ssl_get_message(s,
2105 SSL3_ST_CR_SRVR_DONE_A,
2106 SSL3_ST_CR_SRVR_DONE_B,
2107 SSL3_MT_SERVER_DONE,
2108 30, /* should be very small, like 0 :-) */
2109 &ok);
2110
2111 if (!ok) return((int)n);
2112 if (n > 0)
2113 {
2114 /* should contain no data */
2115 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2116 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
2117 return -1;
2118 }
2119 ret=1;
2120 return(ret);
2121 }
2122
2123
2124int ssl3_send_client_key_exchange(SSL *s)
2125 {
2126 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07002127 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002128 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07002129 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002130 unsigned char *q;
2131 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002132#ifndef OPENSSL_NO_ECDH
2133 EC_KEY *clnt_ecdh = NULL;
2134 const EC_POINT *srvr_ecpoint = NULL;
2135 EVP_PKEY *srvr_pub_pkey = NULL;
2136 unsigned char *encodedPoint = NULL;
2137 int encoded_pt_len = 0;
2138 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07002139#ifndef OPENSSL_NO_PSK
2140 unsigned int psk_len = 0;
2141 unsigned char psk[PSK_MAX_PSK_LEN];
2142#endif /* OPENSSL_NO_PSK */
2143#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07002144
2145 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2146 {
2147 p = ssl_handshake_start(s);
2148
2149 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002150 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2151
2152#ifndef OPENSSL_NO_PSK
2153 if (alg_a & SSL_aPSK)
2154 {
Adam Langley01797e32014-06-20 12:00:00 -07002155 char identity[PSK_MAX_IDENTITY_LEN + 1];
2156 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002157 unsigned char *t = NULL;
2158 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2159 unsigned int pre_ms_len = 0;
2160 int psk_err = 1;
2161
2162 n = 0;
2163 if (s->psk_client_callback == NULL)
2164 {
2165 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2166 goto err;
2167 }
2168
Adam Langley01797e32014-06-20 12:00:00 -07002169 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002170 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002171 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002172 if (psk_len > PSK_MAX_PSK_LEN)
2173 {
2174 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2175 goto psk_err;
2176 }
2177 else if (psk_len == 0)
2178 {
2179 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2180 goto psk_err;
2181 }
Adam Langley01797e32014-06-20 12:00:00 -07002182 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2183 if (identity_len > PSK_MAX_IDENTITY_LEN)
2184 {
2185 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2186 goto psk_err;
2187 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002188
2189 if (!(alg_k & SSL_kEECDH))
2190 {
2191 /* Create the shared secret now if we're not using ECDHE-PSK.*/
2192 pre_ms_len = 2+psk_len+2+psk_len;
2193 t = pre_ms;
2194 s2n(psk_len, t);
2195 memset(t, 0, psk_len);
2196 t+=psk_len;
2197 s2n(psk_len, t);
2198 memcpy(t, psk, psk_len);
2199
2200 s->session->master_key_length =
2201 s->method->ssl3_enc->generate_master_secret(s,
2202 s->session->master_key,
2203 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002204 s2n(identity_len, p);
2205 memcpy(p, identity, identity_len);
2206 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002207 }
2208
Adam Langleyc26c8022014-06-20 12:00:00 -07002209 if (s->session->psk_identity != NULL)
2210 OPENSSL_free(s->session->psk_identity);
2211 s->session->psk_identity = BUF_strdup(identity);
2212 if (s->session->psk_identity == NULL)
2213 {
2214 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2215 goto psk_err;
2216 }
2217 psk_err = 0;
2218 psk_err:
2219 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2220 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2221 if (psk_err != 0)
2222 {
2223 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2224 goto err;
2225 }
2226 }
2227#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002228
2229 /* Fool emacs indentation */
2230 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002231 else if (alg_k & SSL_kRSA)
2232 {
2233 RSA *rsa;
2234 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2235
2236 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2237 rsa=s->session->sess_cert->peer_rsa_tmp;
2238 else
2239 {
2240 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2241 if ((pkey == NULL) ||
2242 (pkey->type != EVP_PKEY_RSA) ||
2243 (pkey->pkey.rsa == NULL))
2244 {
2245 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2246 goto err;
2247 }
2248 rsa=pkey->pkey.rsa;
2249 EVP_PKEY_free(pkey);
2250 }
2251
2252 tmp_buf[0]=s->client_version>>8;
2253 tmp_buf[1]=s->client_version&0xff;
2254 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2255 goto err;
2256
2257 s->session->master_key_length=sizeof tmp_buf;
2258
2259 q=p;
2260 /* Fix buf for TLS and beyond */
2261 if (s->version > SSL3_VERSION)
2262 p+=2;
2263 n=RSA_public_encrypt(sizeof tmp_buf,
2264 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2265#ifdef PKCS1_CHECK
2266 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2267 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2268#endif
2269 if (n <= 0)
2270 {
2271 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2272 goto err;
2273 }
2274
2275 /* Fix buf for TLS and beyond */
2276 if (s->version > SSL3_VERSION)
2277 {
2278 s2n(n,q);
2279 n+=2;
2280 }
2281
2282 s->session->master_key_length=
2283 s->method->ssl3_enc->generate_master_secret(s,
2284 s->session->master_key,
2285 tmp_buf,sizeof tmp_buf);
2286 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2287 }
Adam Langley95c29f32014-06-20 12:00:00 -07002288#ifndef OPENSSL_NO_DH
2289 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2290 {
2291 DH *dh_srvr,*dh_clnt;
2292 SESS_CERT *scert = s->session->sess_cert;
2293
2294 if (scert == NULL)
2295 {
2296 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2297 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2298 goto err;
2299 }
2300
2301 if (scert->peer_dh_tmp != NULL)
2302 dh_srvr=scert->peer_dh_tmp;
2303 else
2304 {
2305 /* we get them from the cert */
2306 int idx = scert->peer_cert_type;
2307 EVP_PKEY *spkey = NULL;
2308 dh_srvr = NULL;
2309 if (idx >= 0)
2310 spkey = X509_get_pubkey(
2311 scert->peer_pkeys[idx].x509);
2312 if (spkey)
2313 {
2314 dh_srvr = EVP_PKEY_get1_DH(spkey);
2315 EVP_PKEY_free(spkey);
2316 }
2317 if (dh_srvr == NULL)
2318 {
2319 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2320 goto err;
2321 }
2322 }
2323 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2324 {
2325 /* Use client certificate key */
2326 EVP_PKEY *clkey = s->cert->key->privatekey;
2327 dh_clnt = NULL;
2328 if (clkey)
2329 dh_clnt = EVP_PKEY_get1_DH(clkey);
2330 if (dh_clnt == NULL)
2331 {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2333 goto err;
2334 }
2335 }
2336 else
2337 {
2338 /* generate a new random key */
2339 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2340 {
2341 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2342 goto err;
2343 }
2344 if (!DH_generate_key(dh_clnt))
2345 {
2346 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2347 DH_free(dh_clnt);
2348 goto err;
2349 }
2350 }
2351
2352 /* use the 'p' output buffer for the DH key, but
2353 * make sure to clear it out afterwards */
2354
2355 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2356 if (scert->peer_dh_tmp == NULL)
2357 DH_free(dh_srvr);
2358
2359 if (n <= 0)
2360 {
2361 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2362 DH_free(dh_clnt);
2363 goto err;
2364 }
2365
2366 /* generate master key from the result */
2367 s->session->master_key_length=
2368 s->method->ssl3_enc->generate_master_secret(s,
2369 s->session->master_key,p,n);
2370 /* clean up */
2371 memset(p,0,n);
2372
2373 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2374 n = 0;
2375 else
2376 {
2377 /* send off the data */
2378 n=BN_num_bytes(dh_clnt->pub_key);
2379 s2n(n,p);
2380 BN_bn2bin(dh_clnt->pub_key,p);
2381 n+=2;
2382 }
2383
2384 DH_free(dh_clnt);
2385
2386 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2387 }
2388#endif
2389
Adam Langleyc26c8022014-06-20 12:00:00 -07002390#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002391 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2392 {
2393 const EC_GROUP *srvr_group = NULL;
2394 EC_KEY *tkey;
2395 int ecdh_clnt_cert = 0;
2396 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002397#ifndef OPENSSL_NO_PSK
2398 unsigned char *pre_ms;
2399 unsigned char *t;
2400 unsigned int pre_ms_len;
2401 unsigned int i;
2402#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyd06eddd2014-06-20 12:00:00 -07002404 if (s->session->sess_cert == NULL)
2405 {
2406 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2407 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2408 goto err;
2409 }
2410
Adam Langley95c29f32014-06-20 12:00:00 -07002411 /* Did we send out the client's
2412 * ECDH share for use in premaster
2413 * computation as part of client certificate?
2414 * If so, set ecdh_clnt_cert to 1.
2415 */
2416 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2417 {
2418 /* XXX: For now, we do not support client
2419 * authentication using ECDH certificates.
2420 * To add such support, one needs to add
2421 * code that checks for appropriate
2422 * conditions and sets ecdh_clnt_cert to 1.
2423 * For example, the cert have an ECC
2424 * key on the same curve as the server's
2425 * and the key should be authorized for
2426 * key agreement.
2427 *
2428 * One also needs to add code in ssl3_connect
2429 * to skip sending the certificate verify
2430 * message.
2431 *
2432 * if ((s->cert->key->privatekey != NULL) &&
2433 * (s->cert->key->privatekey->type ==
2434 * EVP_PKEY_EC) && ...)
2435 * ecdh_clnt_cert = 1;
2436 */
2437 }
2438
2439 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2440 {
2441 tkey = s->session->sess_cert->peer_ecdh_tmp;
2442 }
2443 else
2444 {
2445 /* Get the Server Public Key from Cert */
2446 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2447 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2448 if ((srvr_pub_pkey == NULL) ||
2449 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2450 (srvr_pub_pkey->pkey.ec == NULL))
2451 {
2452 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2453 goto err;
2454 }
2455
2456 tkey = srvr_pub_pkey->pkey.ec;
2457 }
2458
2459 srvr_group = EC_KEY_get0_group(tkey);
2460 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2461
2462 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2463 {
2464 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2465 goto err;
2466 }
2467
2468 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2469 {
2470 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2471 goto err;
2472 }
2473
2474 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2475 {
2476 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2477 goto err;
2478 }
2479 if (ecdh_clnt_cert)
2480 {
2481 /* Reuse key info from our certificate
2482 * We only need our private key to perform
2483 * the ECDH computation.
2484 */
2485 const BIGNUM *priv_key;
2486 tkey = s->cert->key->privatekey->pkey.ec;
2487 priv_key = EC_KEY_get0_private_key(tkey);
2488 if (priv_key == NULL)
2489 {
2490 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2491 goto err;
2492 }
2493 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2494 {
2495 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2496 goto err;
2497 }
2498 }
2499 else
2500 {
2501 /* Generate a new ECDH key pair */
2502 if (!(EC_KEY_generate_key(clnt_ecdh)))
2503 {
2504 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2505 goto err;
2506 }
2507 }
2508
2509 /* use the 'p' output buffer for the ECDH key, but
2510 * make sure to clear it out afterwards
2511 */
2512
2513 field_size = EC_GROUP_get_degree(srvr_group);
2514 if (field_size <= 0)
2515 {
2516 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2517 goto err;
2518 }
2519 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2520 if (n <= 0)
2521 {
2522 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2523 goto err;
2524 }
2525
Adam Langleyc26c8022014-06-20 12:00:00 -07002526#ifndef OPENSSL_NO_PSK
2527 /* ECDHE PSK ciphersuites from RFC 5489 */
2528 if ((alg_a & SSL_aPSK) && psk_len != 0)
2529 {
2530 pre_ms_len = 2+psk_len+2+n;
2531 pre_ms = OPENSSL_malloc(pre_ms_len);
2532 if (pre_ms == NULL)
2533 {
2534 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2535 goto err;
2536 }
2537 memset(pre_ms, 0, pre_ms_len);
2538 t = pre_ms;
2539 s2n(psk_len, t);
2540 memcpy(t, psk, psk_len);
2541 t += psk_len;
2542 s2n(n, t);
2543 memcpy(t, p, n);
2544 s->session->master_key_length = s->method->ssl3_enc \
2545 -> generate_master_secret(s,
2546 s->session->master_key, pre_ms, pre_ms_len);
2547 OPENSSL_cleanse(pre_ms, pre_ms_len);
2548 OPENSSL_free(pre_ms);
2549 }
2550#endif /* OPENSSL_NO_PSK */
2551 if (!(alg_a & SSL_aPSK))
2552 {
2553 /* generate master key from the result */
2554 s->session->master_key_length = s->method->ssl3_enc \
2555 -> generate_master_secret(s,
2556 s->session->master_key, p, n);
2557 }
Adam Langley95c29f32014-06-20 12:00:00 -07002558 memset(p, 0, n); /* clean up */
2559
Adam Langleyc26c8022014-06-20 12:00:00 -07002560 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002561 {
2562 /* Send empty client key exch message */
2563 n = 0;
2564 }
2565 else
2566 {
2567 /* First check the size of encoding and
2568 * allocate memory accordingly.
2569 */
2570 encoded_pt_len =
2571 EC_POINT_point2oct(srvr_group,
2572 EC_KEY_get0_public_key(clnt_ecdh),
2573 POINT_CONVERSION_UNCOMPRESSED,
2574 NULL, 0, NULL);
2575
2576 encodedPoint = (unsigned char *)
2577 OPENSSL_malloc(encoded_pt_len *
2578 sizeof(unsigned char));
2579 bn_ctx = BN_CTX_new();
2580 if ((encodedPoint == NULL) ||
2581 (bn_ctx == NULL))
2582 {
2583 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2584 goto err;
2585 }
2586
2587 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002588 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2589 EC_KEY_get0_public_key(clnt_ecdh),
2590 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002591 encodedPoint, encoded_pt_len, bn_ctx);
2592
Adam Langleyc26c8022014-06-20 12:00:00 -07002593 n = 0;
2594#ifndef OPENSSL_NO_PSK
2595 if ((alg_a & SSL_aPSK) && psk_len != 0)
2596 {
2597 i = strlen(s->session->psk_identity);
2598 s2n(i, p);
2599 memcpy(p, s->session->psk_identity, i);
2600 p += i;
2601 n = i + 2;
2602 }
2603#endif
2604
2605 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002606 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002607 p += 1;
2608 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002609 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002610 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002611 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002612 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002613 }
2614
2615 /* Free allocated memory */
2616 BN_CTX_free(bn_ctx);
2617 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002618 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002619 EC_KEY_free(clnt_ecdh);
2620 EVP_PKEY_free(srvr_pub_pkey);
2621 }
2622#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002623 else if (alg_k & SSL_kGOST)
Adam Langley95c29f32014-06-20 12:00:00 -07002624 {
2625 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED);
2626 goto err;
2627 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002628 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002629 {
2630 ssl3_send_alert(s, SSL3_AL_FATAL,
2631 SSL_AD_HANDSHAKE_FAILURE);
2632 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2633 goto err;
2634 }
2635
2636 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2637 s->state=SSL3_ST_CW_KEY_EXCH_B;
2638 }
2639
2640 /* SSL3_ST_CW_KEY_EXCH_B */
2641 return ssl_do_write(s);
2642err:
2643#ifndef OPENSSL_NO_ECDH
2644 BN_CTX_free(bn_ctx);
2645 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2646 if (clnt_ecdh != NULL)
2647 EC_KEY_free(clnt_ecdh);
2648 EVP_PKEY_free(srvr_pub_pkey);
2649#endif
2650 return(-1);
2651 }
2652
2653int ssl3_send_client_verify(SSL *s)
2654 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002655 unsigned char *buf, *p;
2656 const EVP_MD *md;
2657 uint8_t digest[EVP_MAX_MD_SIZE];
2658 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002659 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002660 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002661 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002662 size_t signature_length = 0;
2663 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002664
2665 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002666 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002667
2668 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2669 {
2670 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002671 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002672 /* For TLS v1.2 send signature algorithm and signature using
2673 * agreed digest and cached handshake records. Otherwise, use
2674 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002675 */
2676 if (SSL_USE_SIGALGS(s))
2677 {
2678 long hdatalen = 0;
2679 char *hdata;
David Benjamin0e2908a2014-07-09 15:22:43 -04002680 md = s->cert->key->digest;
Adam Langley95c29f32014-06-20 12:00:00 -07002681 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2682 &hdata);
2683 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
2684 {
2685 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2686 goto err;
2687 }
2688 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002689 n += 2;
2690 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2691 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2692 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002693 {
2694 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2695 goto err;
2696 }
Adam Langley95c29f32014-06-20 12:00:00 -07002697 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002698 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002699 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002700 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002701 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002702 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2703 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2704 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2705 * a raw RSA signature, rather than wrapping in a
2706 * DigestInfo. */
2707 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002708 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002709 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002710 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002711 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2712 digest_length = SHA_DIGEST_LENGTH;
2713 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002714 }
2715 else
Adam Langleyaed23062014-06-20 12:00:00 -07002716 {
Adam Langley95c29f32014-06-20 12:00:00 -07002717 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2718 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002719 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002720
2721 /* Sign the digest. */
2722 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2723 if (pctx == NULL)
2724 goto err;
2725
2726 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2727 if (EVP_PKEY_sign_init(pctx) != 1 ||
2728 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2729 EVP_PKEY_sign(pctx, NULL, &signature_length,
2730 digest, digest_length) != 1)
2731 {
2732 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2733 goto err;
2734 }
2735
2736 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2737 {
2738 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2739 goto err;
2740 }
2741
2742 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2743 digest, digest_length) != 1)
2744 {
2745 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2746 goto err;
2747 }
2748
2749 s2n(signature_length, p);
2750 n += signature_length + 2;
2751
2752 /* Now that client auth is completed, we no longer need cached
2753 * handshake records and can digest them. */
2754 if (SSL_USE_SIGALGS(s))
2755 {
2756 if (!ssl3_digest_cached_records(s))
2757 goto err;
2758 }
2759
Adam Langley95c29f32014-06-20 12:00:00 -07002760 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2761 s->state=SSL3_ST_CW_CERT_VRFY_B;
2762 }
2763 EVP_MD_CTX_cleanup(&mctx);
2764 EVP_PKEY_CTX_free(pctx);
2765 return ssl_do_write(s);
2766err:
2767 EVP_MD_CTX_cleanup(&mctx);
2768 EVP_PKEY_CTX_free(pctx);
2769 return(-1);
2770 }
2771
2772/* Check a certificate can be used for client authentication. Currently
2773 * check cert exists, if we have a suitable digest for TLS 1.2 if
2774 * static DH client certificates can be used and optionally checks
2775 * suitability for Suite B.
2776 */
2777static int ssl3_check_client_certificate(SSL *s)
2778 {
2779 unsigned long alg_k;
2780 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2781 return 0;
2782 /* If no suitable signature algorithm can't use certificate */
2783 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2784 return 0;
2785 /* If strict mode check suitability of chain before using it.
2786 * This also adjusts suite B digest if necessary.
2787 */
2788 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2789 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2790 return 0;
2791 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2792 /* See if we can use client certificate for fixed DH */
2793 if (alg_k & (SSL_kDHr|SSL_kDHd))
2794 {
2795 SESS_CERT *scert = s->session->sess_cert;
2796 int i = scert->peer_cert_type;
2797 EVP_PKEY *clkey = NULL, *spkey = NULL;
2798 clkey = s->cert->key->privatekey;
2799 /* If client key not DH assume it can be used */
2800 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2801 return 1;
2802 if (i >= 0)
2803 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2804 if (spkey)
2805 {
2806 /* Compare server and client parameters */
2807 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2808 EVP_PKEY_free(spkey);
2809 if (i != 1)
2810 return 0;
2811 }
2812 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2813 }
2814 return 1;
2815 }
2816
2817int ssl3_send_client_certificate(SSL *s)
2818 {
2819 X509 *x509=NULL;
2820 EVP_PKEY *pkey=NULL;
2821 int i;
2822
2823 if (s->state == SSL3_ST_CW_CERT_A)
2824 {
2825 /* Let cert callback update client certificates if required */
2826 if (s->cert->cert_cb)
2827 {
2828 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2829 if (i < 0)
2830 {
2831 s->rwstate=SSL_X509_LOOKUP;
2832 return -1;
2833 }
2834 if (i == 0)
2835 {
2836 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2837 return 0;
2838 }
2839 s->rwstate=SSL_NOTHING;
2840 }
2841 if (ssl3_check_client_certificate(s))
2842 s->state=SSL3_ST_CW_CERT_C;
2843 else
2844 s->state=SSL3_ST_CW_CERT_B;
2845 }
2846
2847 /* We need to get a client cert */
2848 if (s->state == SSL3_ST_CW_CERT_B)
2849 {
2850 /* If we get an error, we need to
2851 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2852 * We then get retied later */
2853 i=0;
2854 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2855 if (i < 0)
2856 {
2857 s->rwstate=SSL_X509_LOOKUP;
2858 return(-1);
2859 }
2860 s->rwstate=SSL_NOTHING;
2861 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2862 {
2863 s->state=SSL3_ST_CW_CERT_B;
2864 if ( !SSL_use_certificate(s,x509) ||
2865 !SSL_use_PrivateKey(s,pkey))
2866 i=0;
2867 }
2868 else if (i == 1)
2869 {
2870 i=0;
2871 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2872 }
2873
2874 if (x509 != NULL) X509_free(x509);
2875 if (pkey != NULL) EVP_PKEY_free(pkey);
2876 if (i && !ssl3_check_client_certificate(s))
2877 i = 0;
2878 if (i == 0)
2879 {
2880 if (s->version == SSL3_VERSION)
2881 {
2882 s->s3->tmp.cert_req=0;
2883 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2884 return(1);
2885 }
2886 else
2887 {
2888 s->s3->tmp.cert_req=2;
2889 }
2890 }
2891
2892 /* Ok, we have a cert */
2893 s->state=SSL3_ST_CW_CERT_C;
2894 }
2895
2896 if (s->state == SSL3_ST_CW_CERT_C)
2897 {
2898 s->state=SSL3_ST_CW_CERT_D;
2899 ssl3_output_cert_chain(s,
2900 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2901 }
2902 /* SSL3_ST_CW_CERT_D */
2903 return ssl_do_write(s);
2904 }
2905
2906#define has_bits(i,m) (((i)&(m)) == (m))
2907
2908int ssl3_check_cert_and_algorithm(SSL *s)
2909 {
2910 int i,idx;
2911 long alg_k,alg_a;
2912 EVP_PKEY *pkey=NULL;
2913 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002914 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002915#ifndef OPENSSL_NO_DH
2916 DH *dh;
2917#endif
2918
2919 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2920 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2921
2922 /* we don't have a certificate */
David Benjamind26aea62014-07-12 00:13:56 -04002923 if ((alg_a & SSL_aNULL) || ((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA)))
Adam Langley95c29f32014-06-20 12:00:00 -07002924 return(1);
2925
2926 sc=s->session->sess_cert;
2927 if (sc == NULL)
2928 {
2929 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2930 goto err;
2931 }
2932
Adam Langley95c29f32014-06-20 12:00:00 -07002933 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002934#ifndef OPENSSL_NO_DH
2935 dh=s->session->sess_cert->peer_dh_tmp;
2936#endif
2937
2938 /* This is the passed certificate */
2939
2940 idx=sc->peer_cert_type;
2941#ifndef OPENSSL_NO_ECDH
2942 if (idx == SSL_PKEY_ECC)
2943 {
2944 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2945 s) == 0)
2946 { /* check failed */
2947 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
2948 goto f_err;
2949 }
2950 else
2951 {
2952 return 1;
2953 }
2954 }
2955 else if (alg_a & SSL_aECDSA)
2956 {
2957 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
2958 goto f_err;
2959 }
2960 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2961 {
2962 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
2963 goto f_err;
2964 }
2965#endif
2966 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2967 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2968 EVP_PKEY_free(pkey);
2969
2970
2971 /* Check that we have a certificate if we require one */
2972 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2973 {
2974 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
2975 goto f_err;
2976 }
2977#ifndef OPENSSL_NO_DSA
2978 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2979 {
2980 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
2981 goto f_err;
2982 }
2983#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002984 if ((alg_k & SSL_kRSA) &&
2985 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2986 {
2987 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2988 goto f_err;
2989 }
Adam Langley95c29f32014-06-20 12:00:00 -07002990#ifndef OPENSSL_NO_DH
2991 if ((alg_k & SSL_kEDH) &&
2992 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2993 {
2994 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
2995 goto f_err;
2996 }
2997 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2998 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2999 {
3000 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
3001 goto f_err;
3002 }
3003#ifndef OPENSSL_NO_DSA
3004 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3005 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
3006 {
3007 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
3008 goto f_err;
3009 }
3010#endif
3011#endif
3012
3013 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
3014 {
Adam Langley95c29f32014-06-20 12:00:00 -07003015 if (alg_k & SSL_kRSA)
3016 {
3017 if (rsa == NULL
3018 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3019 {
3020 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3021 goto f_err;
3022 }
3023 }
3024 else
Adam Langley95c29f32014-06-20 12:00:00 -07003025#ifndef OPENSSL_NO_DH
3026 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
3027 {
3028 if (dh == NULL
3029 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3030 {
3031 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3032 goto f_err;
3033 }
3034 }
3035 else
3036#endif
3037 {
3038 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3039 goto f_err;
3040 }
3041 }
3042 return(1);
3043f_err:
3044 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3045err:
3046 return(0);
3047 }
3048
Adam Langley1258b6a2014-06-20 12:00:00 -07003049# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07003050int ssl3_send_next_proto(SSL *s)
3051 {
3052 unsigned int len, padding_len;
3053 unsigned char *d;
3054
3055 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3056 {
3057 len = s->next_proto_negotiated_len;
3058 padding_len = 32 - ((len + 2) % 32);
3059 d = (unsigned char *)s->init_buf->data;
3060 d[4] = len;
3061 memcpy(d + 5, s->next_proto_negotiated, len);
3062 d[5 + len] = padding_len;
3063 memset(d + 6 + len, 0, padding_len);
3064 *(d++)=SSL3_MT_NEXT_PROTO;
3065 l2n3(2 + len + padding_len, d);
3066 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3067 s->init_num = 4 + 2 + len + padding_len;
3068 s->init_off = 0;
3069 }
3070
3071 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3072}
Adam Langley1258b6a2014-06-20 12:00:00 -07003073
3074# endif /* !OPENSSL_NO_NEXTPROTONEG */
3075
3076int ssl3_send_channel_id(SSL *s)
3077 {
3078 unsigned char *d;
3079 int ret = -1, public_key_len;
3080 EVP_MD_CTX md_ctx;
3081 size_t sig_len;
3082 ECDSA_SIG *sig = NULL;
3083 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3084
3085 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3086 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3087
3088 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3089 {
3090 EVP_PKEY *key = NULL;
3091 s->ctx->channel_id_cb(s, &key);
3092 if (key != NULL)
3093 {
3094 s->tlsext_channel_id_private = key;
3095 }
3096 }
3097 if (!s->tlsext_channel_id_private)
3098 {
3099 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3100 return (-1);
3101 }
3102 s->rwstate=SSL_NOTHING;
3103
3104 d = (unsigned char *)s->init_buf->data;
3105 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3106 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3107 if (s->s3->tlsext_channel_id_new)
3108 s2n(TLSEXT_TYPE_channel_id_new, d);
3109 else
3110 s2n(TLSEXT_TYPE_channel_id, d);
3111 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3112
3113 EVP_MD_CTX_init(&md_ctx);
3114
3115 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3116 if (public_key_len <= 0)
3117 {
3118 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
3119 goto err;
3120 }
3121 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3122 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3123 * field elements as 32-byte, big-endian numbers. */
3124 if (public_key_len != 65)
3125 {
3126 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
3127 goto err;
3128 }
3129 public_key = OPENSSL_malloc(public_key_len);
3130 if (!public_key)
3131 {
3132 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3133 goto err;
3134 }
3135
3136 derp = public_key;
3137 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3138
3139 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3140 s->tlsext_channel_id_private) != 1)
3141 {
3142 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
3143 goto err;
3144 }
3145
3146 if (!tls1_channel_id_hash(&md_ctx, s))
3147 goto err;
3148
3149 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3150 {
3151 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3152 goto err;
3153 }
3154
3155 der_sig = OPENSSL_malloc(sig_len);
3156 if (!der_sig)
3157 {
3158 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
3159 goto err;
3160 }
3161
3162 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3163 {
3164 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
3165 goto err;
3166 }
3167
3168 derp = der_sig;
3169 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
3170 if (sig == NULL)
3171 {
3172 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
3173 goto err;
3174 }
3175
3176 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
3177 memcpy(d, public_key + 1, 64);
3178 d += 64;
3179 memset(d, 0, 2 * 32);
3180 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3181 d += 32;
3182 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3183 d += 32;
3184
3185 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3186 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3187 s->init_off = 0;
3188
3189 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3190
3191err:
3192 EVP_MD_CTX_cleanup(&md_ctx);
3193 if (public_key)
3194 OPENSSL_free(public_key);
3195 if (der_sig)
3196 OPENSSL_free(der_sig);
3197 if (sig)
3198 ECDSA_SIG_free(sig);
3199
3200 return ret;
3201 }
Adam Langley95c29f32014-06-20 12:00:00 -07003202
3203/* Check to see if handshake is full or resumed. Usually this is just a
3204 * case of checking to see if a cache hit has occurred. In the case of
3205 * session tickets we have to check the next message to be sure.
3206 */
3207
Adam Langley95c29f32014-06-20 12:00:00 -07003208int ssl3_check_finished(SSL *s)
3209 {
3210 int ok;
3211 long n;
3212 /* If we have no ticket it cannot be a resumed session. */
3213 if (!s->session->tlsext_tick)
3214 return 1;
3215 /* this function is called when we really expect a Certificate
3216 * message, so permit appropriate message length */
3217 n=s->method->ssl_get_message(s,
3218 SSL3_ST_CR_CERT_A,
3219 SSL3_ST_CR_CERT_B,
3220 -1,
3221 s->max_cert_list,
3222 &ok);
3223 if (!ok) return((int)n);
3224 s->s3->tmp.reuse_message = 1;
3225 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3226 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3227 return 2;
3228
3229 return 1;
3230 }
Adam Langley95c29f32014-06-20 12:00:00 -07003231
3232int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3233 {
3234 int i = 0;
3235 /* TODO(fork): remove */
3236#if 0
3237#ifndef OPENSSL_NO_ENGINE
3238 if (s->ctx->client_cert_engine)
3239 {
3240 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3241 SSL_get_client_CA_list(s),
3242 px509, ppkey, NULL, NULL, NULL);
3243 if (i != 0)
3244 return i;
3245 }
3246#endif
3247#endif
3248 if (s->ctx->client_cert_cb)
3249 i = s->ctx->client_cert_cb(s,px509,ppkey);
3250 return i;
3251 }