blob: 7219984344565d3e5e04927ff8a1e969d8bbabf6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
134 *
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
138 *
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
143 *
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
149 */
150
151#include <stdio.h>
152
153#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400154#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155#include <openssl/rand.h>
156#include <openssl/obj.h>
157#include <openssl/evp.h>
158#include <openssl/mem.h>
159#include <openssl/md5.h>
160#include <openssl/dh.h>
161#include <openssl/bn.h>
162#include <openssl/engine.h>
163#include <openssl/x509.h>
164
165#include "ssl_locl.h"
166#include "../crypto/dh/internal.h"
167
168static const SSL_METHOD *ssl3_get_client_method(int ver);
169
170static const SSL_METHOD *ssl3_get_client_method(int ver)
171 {
172 if (ver == SSL3_VERSION)
173 return(SSLv3_client_method());
174 else
175 return(NULL);
176 }
177
178IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
179 ssl_undefined_function,
180 ssl3_connect,
181 ssl3_get_client_method)
182
183int ssl3_connect(SSL *s)
184 {
185 BUF_MEM *buf=NULL;
186 void (*cb)(const SSL *ssl,int type,int val)=NULL;
187 int ret= -1;
188 int new_state,state,skip=0;
189
190 ERR_clear_error();
191 ERR_clear_system_error();
192
193 if (s->info_callback != NULL)
194 cb=s->info_callback;
195 else if (s->ctx->info_callback != NULL)
196 cb=s->ctx->info_callback;
197
198 s->in_handshake++;
199 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
200
Adam Langley95c29f32014-06-20 12:00:00 -0700201 for (;;)
202 {
203 state=s->state;
204
205 switch(s->state)
206 {
207 case SSL_ST_RENEGOTIATE:
208 s->renegotiate=1;
209 s->state=SSL_ST_CONNECT;
210 s->ctx->stats.sess_connect_renegotiate++;
211 /* break */
212 case SSL_ST_BEFORE:
213 case SSL_ST_CONNECT:
214 case SSL_ST_BEFORE|SSL_ST_CONNECT:
215 case SSL_ST_OK|SSL_ST_CONNECT:
216
217 s->server=0;
218 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
219
220 if ((s->version & 0xff00 ) != 0x0300)
221 {
222 OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
223 ret = -1;
224 goto end;
225 }
226
227 /* s->version=SSL3_VERSION; */
228 s->type=SSL_ST_CONNECT;
229
230 if (s->init_buf == NULL)
231 {
232 if ((buf=BUF_MEM_new()) == NULL)
233 {
234 ret= -1;
235 goto end;
236 }
237 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
238 {
239 ret= -1;
240 goto end;
241 }
242 s->init_buf=buf;
243 buf=NULL;
244 }
245
246 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
247
248 /* setup buffing BIO */
249 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
250
251 /* don't push the buffering BIO quite yet */
252
253 ssl3_init_finished_mac(s);
254
255 s->state=SSL3_ST_CW_CLNT_HELLO_A;
256 s->ctx->stats.sess_connect++;
257 s->init_num=0;
258 break;
259
260 case SSL3_ST_CW_CLNT_HELLO_A:
261 case SSL3_ST_CW_CLNT_HELLO_B:
262
263 s->shutdown=0;
264 ret=ssl3_client_hello(s);
265 if (ret <= 0) goto end;
266 s->state=SSL3_ST_CR_SRVR_HELLO_A;
267 s->init_num=0;
268
269 /* turn on buffering for the next lot of output */
270 if (s->bbio != s->wbio)
271 s->wbio=BIO_push(s->bbio,s->wbio);
272
273 break;
274
275 case SSL3_ST_CR_SRVR_HELLO_A:
276 case SSL3_ST_CR_SRVR_HELLO_B:
277 ret=ssl3_get_server_hello(s);
278 if (ret <= 0) goto end;
279
280 if (s->hit)
281 {
David Benjamincb5abad2014-07-25 12:14:28 -0400282 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700283 if (s->tlsext_ticket_expected)
284 {
285 /* receive renewed session ticket */
286 s->state=SSL3_ST_CR_SESSION_TICKET_A;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288 }
289 else
290 {
David Benjamin2b0aeec2014-07-01 00:39:02 -0400291 s->state=SSL3_ST_CR_CERT_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700292 }
293 s->init_num=0;
294 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400295
Adam Langley95c29f32014-06-20 12:00:00 -0700296 case SSL3_ST_CR_CERT_A:
297 case SSL3_ST_CR_CERT_B:
David Benjamine8f3d662014-07-12 01:10:19 -0400298 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -0700299 {
300 ret=ssl3_get_server_certificate(s);
301 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700302 if (s->tlsext_status_expected)
303 s->state=SSL3_ST_CR_CERT_STATUS_A;
304 else
305 s->state=SSL3_ST_CR_KEY_EXCH_A;
306 }
307 else
308 {
309 skip = 1;
310 s->state=SSL3_ST_CR_KEY_EXCH_A;
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312 s->init_num=0;
313 break;
314
315 case SSL3_ST_CR_KEY_EXCH_A:
316 case SSL3_ST_CR_KEY_EXCH_B:
David Benjamin8f8040d2014-07-14 19:14:46 -0400317 ret=ssl3_get_server_key_exchange(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (ret <= 0) goto end;
319 s->state=SSL3_ST_CR_CERT_REQ_A;
320 s->init_num=0;
321
322 /* at this point we check that we have the
323 * required stuff from the server */
324 if (!ssl3_check_cert_and_algorithm(s))
325 {
326 ret= -1;
327 goto end;
328 }
329 break;
330
331 case SSL3_ST_CR_CERT_REQ_A:
332 case SSL3_ST_CR_CERT_REQ_B:
333 ret=ssl3_get_certificate_request(s);
334 if (ret <= 0) goto end;
335 s->state=SSL3_ST_CR_SRVR_DONE_A;
336 s->init_num=0;
337 break;
338
339 case SSL3_ST_CR_SRVR_DONE_A:
340 case SSL3_ST_CR_SRVR_DONE_B:
341 ret=ssl3_get_server_done(s);
342 if (ret <= 0) goto end;
343 if (s->s3->tmp.cert_req)
344 s->state=SSL3_ST_CW_CERT_A;
345 else
346 s->state=SSL3_ST_CW_KEY_EXCH_A;
347 s->init_num=0;
348
349 break;
350
351 case SSL3_ST_CW_CERT_A:
352 case SSL3_ST_CW_CERT_B:
353 case SSL3_ST_CW_CERT_C:
354 case SSL3_ST_CW_CERT_D:
355 ret=ssl3_send_client_certificate(s);
356 if (ret <= 0) goto end;
357 s->state=SSL3_ST_CW_KEY_EXCH_A;
358 s->init_num=0;
359 break;
360
361 case SSL3_ST_CW_KEY_EXCH_A:
362 case SSL3_ST_CW_KEY_EXCH_B:
363 ret=ssl3_send_client_key_exchange(s);
364 if (ret <= 0) goto end;
365 /* EAY EAY EAY need to check for DH fix cert
366 * sent back */
367 /* For TLS, cert_req is set to 2, so a cert chain
368 * of nothing is sent, but no verify packet is sent */
369 /* XXX: For now, we do not support client
370 * authentication in ECDH cipher suites with
371 * ECDH (rather than ECDSA) certificates.
372 * We need to skip the certificate verify
373 * message when client's ECDH public key is sent
374 * inside the client certificate.
375 */
376 if (s->s3->tmp.cert_req == 1)
377 {
378 s->state=SSL3_ST_CW_CERT_VRFY_A;
379 }
380 else
381 {
382 s->state=SSL3_ST_CW_CHANGE_A;
383 s->s3->change_cipher_spec=0;
384 }
385 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
386 {
387 s->state=SSL3_ST_CW_CHANGE_A;
388 s->s3->change_cipher_spec=0;
389 }
390
391 s->init_num=0;
392 break;
393
394 case SSL3_ST_CW_CERT_VRFY_A:
395 case SSL3_ST_CW_CERT_VRFY_B:
396 ret=ssl3_send_client_verify(s);
397 if (ret <= 0) goto end;
398 s->state=SSL3_ST_CW_CHANGE_A;
399 s->init_num=0;
400 s->s3->change_cipher_spec=0;
401 break;
402
403 case SSL3_ST_CW_CHANGE_A:
404 case SSL3_ST_CW_CHANGE_B:
405 ret=ssl3_send_change_cipher_spec(s,
406 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
407 if (ret <= 0) goto end;
408
Adam Langley1258b6a2014-06-20 12:00:00 -0700409 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700410 if (s->s3->tlsext_channel_id_valid)
411 s->state=SSL3_ST_CW_CHANNEL_ID_A;
412# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700413 if (s->s3->next_proto_neg_seen)
414 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700415# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700416 s->init_num=0;
417
418 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700419 if (!s->method->ssl3_enc->setup_key_block(s))
420 {
421 ret= -1;
422 goto end;
423 }
424
425 if (!s->method->ssl3_enc->change_cipher_state(s,
426 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
427 {
428 ret= -1;
429 goto end;
430 }
431
432 break;
433
David Benjamin6dbd73d2014-07-03 15:59:49 -0400434#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700435 case SSL3_ST_CW_NEXT_PROTO_A:
436 case SSL3_ST_CW_NEXT_PROTO_B:
437 ret=ssl3_send_next_proto(s);
438 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700439 if (s->s3->tlsext_channel_id_valid)
440 s->state=SSL3_ST_CW_CHANNEL_ID_A;
441 else
442 s->state=SSL3_ST_CW_FINISHED_A;
443 break;
444#endif
445
Adam Langley1258b6a2014-06-20 12:00:00 -0700446 case SSL3_ST_CW_CHANNEL_ID_A:
447 case SSL3_ST_CW_CHANNEL_ID_B:
448 ret=ssl3_send_channel_id(s);
449 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700450 s->state=SSL3_ST_CW_FINISHED_A;
451 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
453 case SSL3_ST_CW_FINISHED_A:
454 case SSL3_ST_CW_FINISHED_B:
455 ret=ssl3_send_finished(s,
456 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
457 s->method->ssl3_enc->client_finished_label,
458 s->method->ssl3_enc->client_finished_label_len);
459 if (ret <= 0) goto end;
460 s->state=SSL3_ST_CW_FLUSH;
461
462 /* clear flags */
463 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
464 if (s->hit)
465 {
466 s->s3->tmp.next_state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700467 }
468 else
469 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700470 /* This is a non-resumption handshake. If it
471 * involves ChannelID, then record the
472 * handshake hashes at this point in the
473 * session so that any resumption of this
474 * session with ChannelID can sign those
475 * hashes. */
476 if (s->s3->tlsext_channel_id_new)
477 {
478 ret = tls1_record_handshake_hashes_for_channel_id(s);
479 if (ret <= 0)
480 goto end;
481 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700482 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
483 && ssl3_can_cutthrough(s)
484 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
485 )
486 {
Adam Langley045cc552014-07-16 10:45:57 -0700487 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700490 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700491 /* Allow NewSessionTicket if ticket expected */
492 if (s->tlsext_ticket_expected)
493 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
494 else
David Benjamincb5abad2014-07-25 12:14:28 -0400495 s->s3->tmp.next_state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497 }
498 s->init_num=0;
499 break;
500
Adam Langley95c29f32014-06-20 12:00:00 -0700501 case SSL3_ST_CR_SESSION_TICKET_A:
502 case SSL3_ST_CR_SESSION_TICKET_B:
503 ret=ssl3_get_new_session_ticket(s);
504 if (ret <= 0) goto end;
David Benjamincb5abad2014-07-25 12:14:28 -0400505 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700506 s->init_num=0;
507 break;
508
509 case SSL3_ST_CR_CERT_STATUS_A:
510 case SSL3_ST_CR_CERT_STATUS_B:
511 ret=ssl3_get_cert_status(s);
512 if (ret <= 0) goto end;
513 s->state=SSL3_ST_CR_KEY_EXCH_A;
514 s->init_num=0;
515 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamincb5abad2014-07-25 12:14:28 -0400517 case SSL3_ST_CR_CHANGE:
518 /* At this point, the next message must be entirely
519 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400520 if (!ssl3_expect_change_cipher_spec(s))
521 {
522 ret = -1;
523 goto end;
524 }
David Benjamincb5abad2014-07-25 12:14:28 -0400525 s->state = SSL3_ST_CR_FINISHED_A;
526 break;
527
Adam Langley95c29f32014-06-20 12:00:00 -0700528 case SSL3_ST_CR_FINISHED_A:
529 case SSL3_ST_CR_FINISHED_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700530 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
531 SSL3_ST_CR_FINISHED_B);
532 if (ret <= 0) goto end;
533
534 if (s->hit)
535 s->state=SSL3_ST_CW_CHANGE_A;
536 else
537 s->state=SSL_ST_OK;
538 s->init_num=0;
539 break;
540
541 case SSL3_ST_CW_FLUSH:
542 s->rwstate=SSL_WRITING;
543 if (BIO_flush(s->wbio) <= 0)
544 {
545 ret= -1;
546 goto end;
547 }
548 s->rwstate=SSL_NOTHING;
549 s->state=s->s3->tmp.next_state;
550 break;
551
Adam Langleyadb739e2014-06-20 12:00:00 -0700552 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700553 /* Allow NewSessionTicket if ticket expected */
554 if (s->tlsext_ticket_expected)
555 s->state=SSL3_ST_CR_SESSION_TICKET_A;
556 else
David Benjamin7e3305e2014-07-28 14:52:32 -0400557 s->state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700558
Adam Langley045cc552014-07-16 10:45:57 -0700559 ssl_free_wbio_buffer(s);
Adam Langleyadb739e2014-06-20 12:00:00 -0700560 ret = 1;
561 goto end;
562 /* break; */
563
Adam Langley95c29f32014-06-20 12:00:00 -0700564 case SSL_ST_OK:
565 /* clean a few things up */
566 ssl3_cleanup_key_block(s);
567
568 if (s->init_buf != NULL)
569 {
570 BUF_MEM_free(s->init_buf);
571 s->init_buf=NULL;
572 }
573
574 /* If we are not 'joining' the last two packets,
575 * remove the buffering now */
576 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
577 ssl_free_wbio_buffer(s);
578 /* else do it later in ssl3_write */
579
580 s->init_num=0;
581 s->renegotiate=0;
582 s->new_session=0;
583
584 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
585 if (s->hit) s->ctx->stats.sess_hit++;
586
587 ret=1;
588 /* s->server=0; */
589 s->handshake_func=ssl3_connect;
590 s->ctx->stats.sess_connect_good++;
591
592 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
593
594 goto end;
595 /* break; */
596
597 default:
598 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
599 ret= -1;
600 goto end;
601 /* break; */
602 }
603
604 /* did we do anything */
605 if (!s->s3->tmp.reuse_message && !skip)
606 {
607 if (s->debug)
608 {
609 if ((ret=BIO_flush(s->wbio)) <= 0)
610 goto end;
611 }
612
613 if ((cb != NULL) && (s->state != state))
614 {
615 new_state=s->state;
616 s->state=state;
617 cb(s,SSL_CB_CONNECT_LOOP,1);
618 s->state=new_state;
619 }
620 }
621 skip=0;
622 }
623end:
624 s->in_handshake--;
625 if (buf != NULL)
626 BUF_MEM_free(buf);
627 if (cb != NULL)
628 cb(s,SSL_CB_CONNECT_EXIT,ret);
629 return(ret);
630 }
631
632
633int ssl3_client_hello(SSL *s)
634 {
635 unsigned char *buf;
636 unsigned char *p,*d;
637 int i;
638 unsigned long l;
639
640 buf=(unsigned char *)s->init_buf->data;
641 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
642 {
643 SSL_SESSION *sess = s->session;
David Benjamin407886f2014-07-21 22:23:50 -0400644 if (sess == NULL ||
645 sess->ssl_version != s->version ||
646 !sess->session_id_length ||
647 sess->not_resumable)
Adam Langley95c29f32014-06-20 12:00:00 -0700648 {
649 if (!ssl_get_new_session(s,0))
650 goto err;
651 }
652 if (s->method->version == DTLS_ANY_VERSION)
653 {
654 /* Determine which DTLS version to use */
655 int options = s->options;
656 /* If DTLS 1.2 disabled correct the version number */
657 if (options & SSL_OP_NO_DTLSv1_2)
658 {
659 if (tls1_suiteb(s))
660 {
661 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
662 goto err;
663 }
664 /* Disabling all versions is silly: return an
665 * error.
666 */
667 if (options & SSL_OP_NO_DTLSv1)
668 {
669 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
670 goto err;
671 }
672 /* Update method so we don't use any DTLS 1.2
673 * features.
674 */
675 s->method = DTLSv1_client_method();
676 s->version = DTLS1_VERSION;
677 }
678 else
679 {
680 /* We only support one version: update method */
681 if (options & SSL_OP_NO_DTLSv1)
682 s->method = DTLSv1_2_client_method();
683 s->version = DTLS1_2_VERSION;
684 }
685 s->client_version = s->version;
686 }
687 /* else use the pre-loaded session */
688
689 p=s->s3->client_random;
690
691 /* for DTLS if client_random is initialized, reuse it, we are
692 * required to use same upon reply to HelloVerify */
693 if (SSL_IS_DTLS(s))
694 {
695 size_t idx;
696 i = 1;
697 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
698 {
699 if (p[idx])
700 {
701 i = 0;
702 break;
703 }
704 }
705 }
706 else
707 i = 1;
708
709 if (i)
710 ssl_fill_hello_random(s, 0, p,
711 sizeof(s->s3->client_random));
712
Adam Langleyb0c235e2014-06-20 12:00:00 -0700713 /* Do the message type and length last.
714 * Note: the final argument to ssl_add_clienthello_tlsext below
715 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700716 d=p= ssl_handshake_start(s);
717
718 /* version indicates the negotiated version: for example from
719 * an SSLv2/v3 compatible client hello). The client_version
720 * field is the maximum version we permit and it is also
721 * used in RSA encrypted premaster secrets. Some servers can
722 * choke if we initially report a higher version then
723 * renegotiate to a lower one in the premaster secret. This
724 * didn't happen with TLS 1.0 as most servers supported it
725 * but it can with TLS 1.1 or later if the server only supports
726 * 1.0.
727 *
728 * Possible scenario with previous logic:
729 * 1. Client hello indicates TLS 1.2
730 * 2. Server hello says TLS 1.0
731 * 3. RSA encrypted premaster secret uses 1.2.
732 * 4. Handhaked proceeds using TLS 1.0.
733 * 5. Server sends hello request to renegotiate.
734 * 6. Client hello indicates TLS v1.0 as we now
735 * know that is maximum server supports.
736 * 7. Server chokes on RSA encrypted premaster secret
737 * containing version 1.0.
738 *
739 * For interoperability it should be OK to always use the
740 * maximum version we support in client hello and then rely
741 * on the checking of version to ensure the servers isn't
742 * being inconsistent: for example initially negotiating with
743 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
744 * client_version in client hello and not resetting it to
745 * the negotiated version.
746 */
747#if 0
748 *(p++)=s->version>>8;
749 *(p++)=s->version&0xff;
750 s->client_version=s->version;
751#else
752 *(p++)=s->client_version>>8;
753 *(p++)=s->client_version&0xff;
754#endif
755
756 /* Random stuff */
757 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
758 p+=SSL3_RANDOM_SIZE;
759
760 /* Session ID */
761 if (s->new_session)
762 i=0;
763 else
764 i=s->session->session_id_length;
765 *(p++)=i;
766 if (i != 0)
767 {
768 if (i > (int)sizeof(s->session->session_id))
769 {
770 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
771 goto err;
772 }
773 memcpy(p,s->session->session_id,i);
774 p+=i;
775 }
776
777 /* cookie stuff for DTLS */
778 if (SSL_IS_DTLS(s))
779 {
780 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
781 {
782 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
783 goto err;
784 }
785 *(p++) = s->d1->cookie_len;
786 memcpy(p, s->d1->cookie, s->d1->cookie_len);
787 p += s->d1->cookie_len;
788 }
789
790 /* Ciphers supported */
David Benjamin39482a12014-07-20 13:30:15 -0400791 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
Adam Langley95c29f32014-06-20 12:00:00 -0700792 if (i == 0)
793 {
794 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
795 goto err;
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797 s2n(i,p);
798 p+=i;
799
800 /* COMPRESSION */
801 *(p++)=1;
802 *(p++)=0; /* Add the NULL method */
803
Adam Langley95c29f32014-06-20 12:00:00 -0700804 /* TLS extensions*/
805 if (ssl_prepare_clienthello_tlsext(s) <= 0)
806 {
807 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
808 goto err;
809 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700810 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700811 {
812 OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
813 goto err;
814 }
Adam Langley95c29f32014-06-20 12:00:00 -0700815
816 l= p-d;
817 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
818 s->state=SSL3_ST_CW_CLNT_HELLO_B;
819 }
820
821 /* SSL3_ST_CW_CLNT_HELLO_B */
822 return ssl_do_write(s);
823err:
824 return(-1);
825 }
826
827int ssl3_get_server_hello(SSL *s)
828 {
829 STACK_OF(SSL_CIPHER) *sk;
830 const SSL_CIPHER *c;
831 CERT *ct = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -0700832 int al=SSL_AD_INTERNAL_ERROR,ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700833 long n;
David Benjamina03d95d2014-07-12 19:49:07 -0400834 CBS server_hello, server_random, session_id;
David Benjamin39482a12014-07-20 13:30:15 -0400835 uint16_t server_version, cipher_suite;
David Benjamina03d95d2014-07-12 19:49:07 -0400836 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700837 /* Hello verify request and/or server hello version may not
838 * match so set first packet if we're negotiating version.
839 */
840 if (SSL_IS_DTLS(s))
841 s->first_packet = 1;
842
843 n=s->method->ssl_get_message(s,
844 SSL3_ST_CR_SRVR_HELLO_A,
845 SSL3_ST_CR_SRVR_HELLO_B,
846 -1,
847 20000, /* ?? */
848 &ok);
849
850 if (!ok) return((int)n);
851
852 if (SSL_IS_DTLS(s))
853 {
854 s->first_packet = 0;
855 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
856 {
857 if ( s->d1->send_cookie == 0)
858 {
859 s->s3->tmp.reuse_message = 1;
860 return 1;
861 }
862 else /* already sent a cookie */
863 {
864 al=SSL_AD_UNEXPECTED_MESSAGE;
865 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
866 goto f_err;
867 }
868 }
869 }
870
871 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
872 {
873 al=SSL_AD_UNEXPECTED_MESSAGE;
874 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
875 goto f_err;
876 }
877
David Benjamina03d95d2014-07-12 19:49:07 -0400878 CBS_init(&server_hello, s->init_msg, n);
879
880 if (!CBS_get_u16(&server_hello, &server_version) ||
881 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
882 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
David Benjamin39482a12014-07-20 13:30:15 -0400883 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
884 !CBS_get_u16(&server_hello, &cipher_suite) ||
885 !CBS_get_u8(&server_hello, &compression_method))
David Benjamina03d95d2014-07-12 19:49:07 -0400886 {
887 al = SSL_AD_DECODE_ERROR;
888 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
889 goto f_err;
890 }
891
Adam Langley95c29f32014-06-20 12:00:00 -0700892 if (s->method->version == DTLS_ANY_VERSION)
893 {
894 /* Work out correct protocol version to use */
Adam Langley95c29f32014-06-20 12:00:00 -0700895 int options = s->options;
David Benjamina03d95d2014-07-12 19:49:07 -0400896 if (server_version == DTLS1_2_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700897 && !(options & SSL_OP_NO_DTLSv1_2))
898 s->method = DTLSv1_2_client_method();
899 else if (tls1_suiteb(s))
900 {
901 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
David Benjamina03d95d2014-07-12 19:49:07 -0400902 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700903 al = SSL_AD_PROTOCOL_VERSION;
904 goto f_err;
905 }
David Benjamina03d95d2014-07-12 19:49:07 -0400906 else if (server_version == DTLS1_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700907 && !(options & SSL_OP_NO_DTLSv1))
908 s->method = DTLSv1_client_method();
909 else
910 {
911 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400912 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700913 al = SSL_AD_PROTOCOL_VERSION;
914 goto f_err;
915 }
916 s->version = s->client_version = s->method->version;
917 }
918
David Benjamina03d95d2014-07-12 19:49:07 -0400919 if (server_version != s->version)
Adam Langley95c29f32014-06-20 12:00:00 -0700920 {
921 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400922 s->version = (s->version & 0xff00) | (server_version & 0xff);
923 al = SSL_AD_PROTOCOL_VERSION;
Adam Langley95c29f32014-06-20 12:00:00 -0700924 goto f_err;
925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
David Benjamina03d95d2014-07-12 19:49:07 -0400927 /* Copy over the server random. */
928 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700929
David Benjamin9eaeef82014-07-21 22:22:02 -0400930 s->hit = 0;
931
Adam Langley95c29f32014-06-20 12:00:00 -0700932 /* check if we want to resume the session based on external pre-shared secret */
933 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
934 {
935 SSL_CIPHER *pref_cipher=NULL;
936 s->session->master_key_length=sizeof(s->session->master_key);
937 if (s->tls_session_secret_cb(s, s->session->master_key,
938 &s->session->master_key_length,
939 NULL, &pref_cipher,
940 s->tls_session_secret_cb_arg))
941 {
942 s->session->cipher = pref_cipher ?
David Benjamina03d95d2014-07-12 19:49:07 -0400943 pref_cipher :
David Benjamin39482a12014-07-20 13:30:15 -0400944 ssl3_get_cipher_by_value(cipher_suite);
David Benjamin9eaeef82014-07-21 22:22:02 -0400945 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700946 }
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
David Benjamin9eaeef82014-07-21 22:22:02 -0400949 if (!s->hit && CBS_len(&session_id) != 0 &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400950 CBS_mem_equal(&session_id,
951 s->session->session_id, s->session->session_id_length))
Adam Langley95c29f32014-06-20 12:00:00 -0700952 {
David Benjamina03d95d2014-07-12 19:49:07 -0400953 if(s->sid_ctx_length != s->session->sid_ctx_length
954 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
955 {
956 /* actually a client application bug */
957 al = SSL_AD_ILLEGAL_PARAMETER;
958 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
959 goto f_err;
960 }
David Benjamina03d95d2014-07-12 19:49:07 -0400961 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700962 }
David Benjamin9eaeef82014-07-21 22:22:02 -0400963
964 /* a miss or crap from the other end */
965 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -0700966 {
967 /* If we were trying for session-id reuse, make a new
968 * SSL_SESSION so we don't stuff up other people */
Adam Langley95c29f32014-06-20 12:00:00 -0700969 if (s->session->session_id_length > 0)
970 {
971 if (!ssl_get_new_session(s,0))
972 {
973 goto f_err;
974 }
975 }
David Benjamina03d95d2014-07-12 19:49:07 -0400976 /* Note: session_id could be empty. */
977 s->session->session_id_length = CBS_len(&session_id);
978 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
Adam Langley95c29f32014-06-20 12:00:00 -0700979 }
David Benjamina03d95d2014-07-12 19:49:07 -0400980
David Benjamin39482a12014-07-20 13:30:15 -0400981 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -0700982 if (c == NULL)
983 {
984 /* unknown cipher */
David Benjamina03d95d2014-07-12 19:49:07 -0400985 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700986 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
987 goto f_err;
988 }
989 /* If it is a disabled cipher we didn't send it in client hello,
990 * so return an error.
991 */
992 if (c->algorithm_ssl & ct->mask_ssl ||
993 c->algorithm_mkey & ct->mask_k ||
994 c->algorithm_auth & ct->mask_a)
995 {
996 al=SSL_AD_ILLEGAL_PARAMETER;
997 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
998 goto f_err;
999 }
Adam Langley95c29f32014-06-20 12:00:00 -07001000
1001 sk=ssl_get_ciphers_by_id(s);
1002 if (!sk_SSL_CIPHER_find(sk, NULL, c))
1003 {
1004 /* we did not say we would use this cipher */
1005 al=SSL_AD_ILLEGAL_PARAMETER;
1006 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
1007 goto f_err;
1008 }
1009
1010 /* Depending on the session caching (internal/external), the cipher
1011 and/or cipher_id values may not be set. Make sure that
1012 cipher_id is set and use it for comparison. */
1013 if (s->session->cipher)
1014 s->session->cipher_id = s->session->cipher->id;
1015 if (s->hit && (s->session->cipher_id != c->id))
1016 {
David Benjamina03d95d2014-07-12 19:49:07 -04001017 al = SSL_AD_ILLEGAL_PARAMETER;
1018 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1019 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001020 }
1021 s->s3->tmp.new_cipher=c;
1022 /* Don't digest cached records if no sigalgs: we may need them for
1023 * client authentication.
1024 */
1025 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1026 goto f_err;
David Benjamina03d95d2014-07-12 19:49:07 -04001027
David Benjamina03d95d2014-07-12 19:49:07 -04001028 /* Only the NULL compression algorithm is supported. */
1029 if (compression_method != 0)
1030 {
1031 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001032 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1033 goto f_err;
1034 }
Adam Langley95c29f32014-06-20 12:00:00 -07001035
David Benjamina03d95d2014-07-12 19:49:07 -04001036 /* TLS extensions */
1037 if (!ssl_parse_serverhello_tlsext(s, &server_hello))
Adam Langley95c29f32014-06-20 12:00:00 -07001038 {
1039 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
1040 goto err;
1041 }
Adam Langley95c29f32014-06-20 12:00:00 -07001042
David Benjamin03973092014-06-24 23:27:17 -04001043 /* There should be nothing left over in the record. */
David Benjamina03d95d2014-07-12 19:49:07 -04001044 if (CBS_len(&server_hello) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001045 {
1046 /* wrong packet length */
1047 al=SSL_AD_DECODE_ERROR;
1048 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
1049 goto f_err;
1050 }
1051
1052 return(1);
1053f_err:
1054 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1055err:
1056 return(-1);
1057 }
1058
1059int ssl3_get_server_certificate(SSL *s)
1060 {
1061 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001062 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -07001063 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001064 STACK_OF(X509) *sk=NULL;
1065 SESS_CERT *sc;
1066 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001067 CBS cbs, certificate_list;
1068 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001069
1070 n=s->method->ssl_get_message(s,
1071 SSL3_ST_CR_CERT_A,
1072 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001073 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001074 s->max_cert_list,
1075 &ok);
1076
1077 if (!ok) return((int)n);
1078
David Benjamin51b1f742014-07-12 16:31:12 -04001079 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001080
1081 if ((sk=sk_X509_new_null()) == NULL)
1082 {
1083 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1084 goto err;
1085 }
1086
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001087 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1088 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001089 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001090 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001091 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1092 goto f_err;
1093 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001094
1095 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001096 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001097 CBS certificate;
1098 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001099 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001100 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001101 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1102 goto f_err;
1103 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001104 data = CBS_data(&certificate);
1105 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001106 if (x == NULL)
1107 {
1108 al=SSL_AD_BAD_CERTIFICATE;
1109 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1110 goto f_err;
1111 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001112 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001113 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001114 al = SSL_AD_INTERNAL_ERROR;
1115 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1116 goto f_err;
1117 }
1118 if (CBS_len(&certificate) != 0)
1119 {
1120 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001121 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1122 goto f_err;
1123 }
1124 if (!sk_X509_push(sk,x))
1125 {
1126 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1127 goto err;
1128 }
1129 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001130 }
1131
1132 i=ssl_verify_cert_chain(s,sk);
1133 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1134 )
1135 {
1136 al=ssl_verify_alarm_type(s->verify_result);
1137 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1138 goto f_err;
1139 }
1140 ERR_clear_error(); /* but we keep s->verify_result */
1141
1142 sc=ssl_sess_cert_new();
1143 if (sc == NULL) goto err;
1144
1145 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1146 s->session->sess_cert=sc;
1147
1148 sc->cert_chain=sk;
1149 /* Inconsistency alert: cert_chain does include the peer's
1150 * certificate, which we don't include in s3_srvr.c */
1151 x=sk_X509_value(sk,0);
1152 sk=NULL;
1153 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1154
1155 pkey=X509_get_pubkey(x);
1156
David Benjamind26aea62014-07-12 00:13:56 -04001157 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001158 {
1159 x=NULL;
1160 al=SSL3_AL_FATAL;
1161 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1162 goto f_err;
1163 }
1164
1165 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001166 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001167 {
1168 x=NULL;
1169 al=SSL3_AL_FATAL;
1170 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1171 goto f_err;
1172 }
1173
David Benjamind26aea62014-07-12 00:13:56 -04001174 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1175 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001176 {
David Benjamind26aea62014-07-12 00:13:56 -04001177 x=NULL;
1178 al=SSL_AD_ILLEGAL_PARAMETER;
1179 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1180 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001181 }
David Benjamind26aea62014-07-12 00:13:56 -04001182 sc->peer_cert_type=i;
David Benjamind26aea62014-07-12 00:13:56 -04001183 /* Why would the following ever happen?
1184 * We just created sc a couple of lines ago. */
1185 if (sc->peer_pkeys[i].x509 != NULL)
1186 X509_free(sc->peer_pkeys[i].x509);
David Benjamin150c6172014-08-05 22:22:49 -04001187 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1188 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001189
David Benjamind26aea62014-07-12 00:13:56 -04001190 if (s->session->peer != NULL)
1191 X509_free(s->session->peer);
David Benjamin150c6172014-08-05 22:22:49 -04001192 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001193
Adam Langley95c29f32014-06-20 12:00:00 -07001194 s->session->verify_result = s->verify_result;
1195
1196 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001197 ret=1;
1198 if (0)
1199 {
1200f_err:
1201 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1202 }
1203err:
1204 EVP_PKEY_free(pkey);
1205 X509_free(x);
1206 sk_X509_pop_free(sk,X509_free);
1207 return(ret);
1208 }
1209
David Benjamin8f8040d2014-07-14 19:14:46 -04001210int ssl3_get_server_key_exchange(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07001211 {
Adam Langley95c29f32014-06-20 12:00:00 -07001212 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001213 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001214 long n,alg_k,alg_a;
1215 EVP_PKEY *pkey=NULL;
1216 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001217 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001218#ifndef OPENSSL_NO_DH
1219 DH *dh=NULL;
1220#endif
1221#ifndef OPENSSL_NO_ECDH
1222 EC_KEY *ecdh = NULL;
1223 BN_CTX *bn_ctx = NULL;
1224 EC_POINT *srvr_ecpoint = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001225#endif
David Benjamined439582014-07-14 19:13:02 -04001226 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001227
1228 /* use same message size as in ssl3_get_certificate_request()
1229 * as ServerKeyExchange message may be skipped */
1230 n=s->method->ssl_get_message(s,
1231 SSL3_ST_CR_KEY_EXCH_A,
1232 SSL3_ST_CR_KEY_EXCH_B,
1233 -1,
1234 s->max_cert_list,
1235 &ok);
1236 if (!ok) return((int)n);
1237
1238 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1239 {
David Benjamin9c651c92014-07-12 13:27:45 -04001240 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1241 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001242 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin9c651c92014-07-12 13:27:45 -04001243 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1244 return -1;
1245 }
1246
Adam Langley95c29f32014-06-20 12:00:00 -07001247 /* In plain PSK ciphersuite, ServerKeyExchange can be
1248 omitted if no identity hint is sent. Set
1249 session->sess_cert anyway to avoid problems
1250 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001251 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001252 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001253 /* PSK ciphersuites that also send a
1254 * Certificate would have already initialized
1255 * |sess_cert|. */
1256 if (s->session->sess_cert == NULL)
1257 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001258 if (s->session->psk_identity_hint)
1259 {
1260 OPENSSL_free(s->session->psk_identity_hint);
1261 s->session->psk_identity_hint = NULL;
1262 }
Adam Langley95c29f32014-06-20 12:00:00 -07001263 }
Adam Langley95c29f32014-06-20 12:00:00 -07001264 s->s3->tmp.reuse_message=1;
1265 return(1);
1266 }
1267
David Benjamined439582014-07-14 19:13:02 -04001268 /* Retain a copy of the original CBS to compute the signature
1269 * over. */
1270 CBS_init(&server_key_exchange, s->init_msg, n);
1271 server_key_exchange_orig = server_key_exchange;
1272
Adam Langley95c29f32014-06-20 12:00:00 -07001273 if (s->session->sess_cert != NULL)
1274 {
Adam Langley95c29f32014-06-20 12:00:00 -07001275 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1276 {
1277 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1278 s->session->sess_cert->peer_rsa_tmp=NULL;
1279 }
Adam Langley95c29f32014-06-20 12:00:00 -07001280#ifndef OPENSSL_NO_DH
1281 if (s->session->sess_cert->peer_dh_tmp)
1282 {
1283 DH_free(s->session->sess_cert->peer_dh_tmp);
1284 s->session->sess_cert->peer_dh_tmp=NULL;
1285 }
1286#endif
1287#ifndef OPENSSL_NO_ECDH
1288 if (s->session->sess_cert->peer_ecdh_tmp)
1289 {
1290 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1291 s->session->sess_cert->peer_ecdh_tmp=NULL;
1292 }
1293#endif
1294 }
1295 else
1296 {
1297 s->session->sess_cert=ssl_sess_cert_new();
1298 }
1299
Adam Langley95c29f32014-06-20 12:00:00 -07001300 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1301 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1302 EVP_MD_CTX_init(&md_ctx);
1303
Adam Langleyc26c8022014-06-20 12:00:00 -07001304 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001305 {
David Benjamined439582014-07-14 19:13:02 -04001306 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001307
David Benjamined439582014-07-14 19:13:02 -04001308 /* Each of the PSK key exchanges begins with a
1309 * psk_identity_hint. */
1310 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001311 {
David Benjamined439582014-07-14 19:13:02 -04001312 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001313 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001314 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001315 }
Adam Langley95c29f32014-06-20 12:00:00 -07001316
David Benjamined439582014-07-14 19:13:02 -04001317 /* Store PSK identity hint for later use, hint is used in
1318 * ssl3_send_client_key_exchange. Assume that the maximum
1319 * length of a PSK identity hint can be as long as the maximum
1320 * length of a PSK identity. Also do not allow NULL
1321 * characters; identities are saved as C strings.
1322 *
1323 * TODO(davidben): Should invalid hints be ignored? It's a hint
1324 * rather than a specific identity. */
1325 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1326 CBS_contains_zero_byte(&psk_identity_hint))
1327 {
1328 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001329 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamined439582014-07-14 19:13:02 -04001330 goto f_err;
1331 }
1332
1333 /* Save the identity hint as a C string. */
1334 if (!CBS_strdup(&psk_identity_hint, &s->session->psk_identity_hint))
1335 {
1336 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001337 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001338 goto f_err;
1339 }
Adam Langley95c29f32014-06-20 12:00:00 -07001340 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001341
1342 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001343 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001344 {
David Benjamined439582014-07-14 19:13:02 -04001345 CBS rsa_modulus, rsa_exponent;
1346
1347 /* TODO(davidben): This was originally for export
1348 * reasons. Do we still need to support it? */
1349
1350 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_modulus) ||
1351 CBS_len(&rsa_modulus) == 0 ||
1352 !CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_exponent) ||
1353 CBS_len(&rsa_exponent) == 0)
1354 {
1355 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001356 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001357 goto f_err;
1358 }
1359
Adam Langley95c29f32014-06-20 12:00:00 -07001360 if ((rsa=RSA_new()) == NULL)
1361 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001362 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001363 goto err;
1364 }
Adam Langley95c29f32014-06-20 12:00:00 -07001365
David Benjamined439582014-07-14 19:13:02 -04001366 if (!(rsa->n = BN_bin2bn(CBS_data(&rsa_modulus),
1367 CBS_len(&rsa_modulus), rsa->n)))
Adam Langley95c29f32014-06-20 12:00:00 -07001368 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001369 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001370 goto err;
1371 }
David Benjamined439582014-07-14 19:13:02 -04001372
1373 if (!(rsa->e = BN_bin2bn(CBS_data(&rsa_exponent),
1374 CBS_len(&rsa_exponent), rsa->e)))
1375 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001376 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001377 goto err;
1378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
1380 /* this should be because we are using an export cipher */
1381 if (alg_a & SSL_aRSA)
1382 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1383 else
1384 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001385 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001386 goto err;
1387 }
1388 s->session->sess_cert->peer_rsa_tmp=rsa;
1389 rsa=NULL;
1390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391#ifndef OPENSSL_NO_DH
1392 else if (alg_k & SSL_kEDH)
1393 {
David Benjamined439582014-07-14 19:13:02 -04001394 CBS dh_p, dh_g, dh_Ys;
1395
1396 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1397 CBS_len(&dh_p) == 0 ||
1398 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1399 CBS_len(&dh_g) == 0 ||
1400 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1401 CBS_len(&dh_Ys) == 0)
1402 {
1403 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001404 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001405 goto f_err;
1406 }
1407
Adam Langley95c29f32014-06-20 12:00:00 -07001408 if ((dh=DH_new()) == NULL)
1409 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001410 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001411 goto err;
1412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
David Benjamined439582014-07-14 19:13:02 -04001414 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001415 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001416 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001417 goto err;
1418 }
David Benjamined439582014-07-14 19:13:02 -04001419 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001420 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001421 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001422 goto err;
1423 }
David Benjamined439582014-07-14 19:13:02 -04001424 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1425 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001426 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001427 goto err;
1428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001430 if (DH_size(dh) < 512/8)
1431 {
1432 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1433 goto err;
1434 }
1435
Adam Langley95c29f32014-06-20 12:00:00 -07001436 if (alg_a & SSL_aRSA)
1437 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001438#ifndef OPENSSL_NO_DSA
1439 else if (alg_a & SSL_aDSS)
1440 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1441#endif
1442 /* else anonymous DH, so no certificate or pkey. */
1443
1444 s->session->sess_cert->peer_dh_tmp=dh;
1445 dh=NULL;
1446 }
1447 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1448 {
1449 al=SSL_AD_ILLEGAL_PARAMETER;
David Benjamin8f8040d2014-07-14 19:14:46 -04001450 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
Adam Langley95c29f32014-06-20 12:00:00 -07001451 goto f_err;
1452 }
1453#endif /* !OPENSSL_NO_DH */
1454
1455#ifndef OPENSSL_NO_ECDH
1456 else if (alg_k & SSL_kEECDH)
1457 {
David Benjamined439582014-07-14 19:13:02 -04001458 uint16_t curve_id;
1459 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001460 EC_GROUP *ngroup;
1461 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001462 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001463
David Benjamined439582014-07-14 19:13:02 -04001464 /* Extract elliptic curve parameters and the server's
1465 * ephemeral ECDH public key. Check curve is one of
1466 * our preferences, if not server has sent an invalid
1467 * curve.
1468 */
1469 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001470 {
David Benjamined439582014-07-14 19:13:02 -04001471 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001472 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
Adam Langley95c29f32014-06-20 12:00:00 -07001473 goto f_err;
1474 }
1475
David Benjamined439582014-07-14 19:13:02 -04001476 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001477 {
1478 al=SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001479 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley95c29f32014-06-20 12:00:00 -07001480 goto f_err;
1481 }
1482
David Benjamined439582014-07-14 19:13:02 -04001483 if ((ecdh=EC_KEY_new()) == NULL)
1484 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001485 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001486 goto err;
1487 }
Adam Langley95c29f32014-06-20 12:00:00 -07001488 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1489 if (ngroup == NULL)
1490 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001491 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001492 goto err;
1493 }
1494 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1495 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001496 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001497 goto err;
1498 }
1499 EC_GROUP_free(ngroup);
1500
1501 group = EC_KEY_get0_group(ecdh);
1502
Adam Langley95c29f32014-06-20 12:00:00 -07001503 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001504 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1505 {
1506 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001507 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001508 goto f_err;
1509 }
1510
Adam Langley95c29f32014-06-20 12:00:00 -07001511 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1512 ((bn_ctx = BN_CTX_new()) == NULL))
1513 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001514 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001515 goto err;
1516 }
1517
David Benjamined439582014-07-14 19:13:02 -04001518 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1519 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001520 {
David Benjamined439582014-07-14 19:13:02 -04001521 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001522 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
Adam Langley95c29f32014-06-20 12:00:00 -07001523 goto f_err;
1524 }
1525
Adam Langley95c29f32014-06-20 12:00:00 -07001526 /* The ECC/TLS specification does not mention
1527 * the use of DSA to sign ECParameters in the server
1528 * key exchange message. We do support RSA and ECDSA.
1529 */
1530 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001531 else if (alg_a & SSL_aRSA)
1532 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001533#ifndef OPENSSL_NO_ECDSA
1534 else if (alg_a & SSL_aECDSA)
1535 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1536#endif
1537 /* else anonymous ECDH, so no certificate or pkey. */
1538 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1539 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1540 ecdh=NULL;
1541 BN_CTX_free(bn_ctx);
1542 bn_ctx = NULL;
1543 EC_POINT_free(srvr_ecpoint);
1544 srvr_ecpoint = NULL;
1545 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001546#endif /* !OPENSSL_NO_ECDH */
1547
1548 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001549 {
1550 al=SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001551 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001552 goto f_err;
1553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554
David Benjamined439582014-07-14 19:13:02 -04001555 /* At this point, |server_key_exchange| contains the
1556 * signature, if any, while |server_key_exchange_orig|
1557 * contains the entire message. From that, derive a CBS
1558 * containing just the parameter. */
1559 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1560 CBS_len(&server_key_exchange_orig) -
1561 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001562
1563 /* if it was signed, check the signature */
1564 if (pkey != NULL)
1565 {
David Benjamined439582014-07-14 19:13:02 -04001566 CBS signature;
1567
Adam Langley95c29f32014-06-20 12:00:00 -07001568 if (SSL_USE_SIGALGS(s))
1569 {
David Benjamin05da6e12014-07-12 20:42:55 -04001570 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
David Benjamined439582014-07-14 19:13:02 -04001571 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001572 }
1573 else
1574 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001575
David Benjamined439582014-07-14 19:13:02 -04001576 /* The last field in |server_key_exchange| is the
1577 * signature. */
1578 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1579 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001580 {
David Benjamined439582014-07-14 19:13:02 -04001581 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001582 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001583 goto f_err;
1584 }
1585
Adam Langley95c29f32014-06-20 12:00:00 -07001586 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1587 {
1588 int num;
David Benjamined439582014-07-14 19:13:02 -04001589 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1590 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langley95c29f32014-06-20 12:00:00 -07001592 q=md_buf;
1593 for (num=2; num > 0; num--)
1594 {
David Benjamined439582014-07-14 19:13:02 -04001595 unsigned int digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001596 EVP_DigestInit_ex(&md_ctx,(num == 2)
1597 ?s->ctx->md5:s->ctx->sha1, NULL);
1598 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1599 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001600 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1601 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1602 q += digest_len;
1603 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001604 }
David Benjamined439582014-07-14 19:13:02 -04001605 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1606 CBS_data(&signature), CBS_len(&signature),
1607 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001608 {
David Benjamined439582014-07-14 19:13:02 -04001609 al = SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001610 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001611 goto f_err;
1612 }
1613 }
1614 else
Adam Langley95c29f32014-06-20 12:00:00 -07001615 {
1616 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1617 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1618 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001619 EVP_VerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1620 if (EVP_VerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature), pkey) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001621 {
1622 /* bad signature */
1623 al=SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001624 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001625 goto f_err;
1626 }
1627 }
1628 }
1629 else
1630 {
David Benjamine8f3d662014-07-12 01:10:19 -04001631 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001632 {
1633 /* Might be wrong key type, check it */
1634 if (ssl3_check_cert_and_algorithm(s))
1635 /* Otherwise this shouldn't happen */
David Benjamin8f8040d2014-07-14 19:14:46 -04001636 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001637 goto err;
1638 }
1639 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001640 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001641 {
1642 al=SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001643 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001644 goto f_err;
1645 }
1646 }
1647 EVP_PKEY_free(pkey);
1648 EVP_MD_CTX_cleanup(&md_ctx);
1649 return(1);
1650f_err:
1651 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1652err:
1653 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001654 if (rsa != NULL)
1655 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001656#ifndef OPENSSL_NO_DH
1657 if (dh != NULL)
1658 DH_free(dh);
1659#endif
1660#ifndef OPENSSL_NO_ECDH
1661 BN_CTX_free(bn_ctx);
1662 EC_POINT_free(srvr_ecpoint);
1663 if (ecdh != NULL)
1664 EC_KEY_free(ecdh);
1665#endif
1666 EVP_MD_CTX_cleanup(&md_ctx);
1667 return(-1);
1668 }
1669
1670static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1671 {
1672 return(X509_NAME_cmp(*a,*b));
1673 }
1674
1675int ssl3_get_certificate_request(SSL *s)
1676 {
1677 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001678 unsigned long n;
1679 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001680 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001681 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001682 CBS cbs;
1683 CBS certificate_types;
1684 CBS certificate_authorities;
1685 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001686
1687 n=s->method->ssl_get_message(s,
1688 SSL3_ST_CR_CERT_REQ_A,
1689 SSL3_ST_CR_CERT_REQ_B,
1690 -1,
1691 s->max_cert_list,
1692 &ok);
1693
1694 if (!ok) return((int)n);
1695
1696 s->s3->tmp.cert_req=0;
1697
1698 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1699 {
1700 s->s3->tmp.reuse_message=1;
1701 /* If we get here we don't need any cached handshake records
1702 * as we wont be doing client auth.
1703 */
1704 if (s->s3->handshake_buffer)
1705 {
1706 if (!ssl3_digest_cached_records(s))
1707 goto err;
1708 }
1709 return(1);
1710 }
1711
1712 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1713 {
1714 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1715 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1716 goto err;
1717 }
1718
1719 /* TLS does not like anon-DH with client cert */
1720 if (s->version > SSL3_VERSION)
1721 {
1722 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1723 {
1724 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1725 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1726 goto err;
1727 }
1728 }
1729
David Benjamin51b1f742014-07-12 16:31:12 -04001730 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001731
1732 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1733 if (ca_sk == NULL)
1734 {
1735 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1736 goto err;
1737 }
1738
1739 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001740 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001741 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001742 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1743 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1744 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001745 }
David Benjamin676d1e72014-07-08 14:34:10 -04001746 if (!CBS_stow(&certificate_types,
1747 &s->s3->tmp.certificate_types,
1748 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001749 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001750 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1751 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001752 }
Adam Langley95c29f32014-06-20 12:00:00 -07001753 if (SSL_USE_SIGALGS(s))
1754 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001755 CBS supported_signature_algorithms;
1756 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001757 {
1758 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001759 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001760 goto err;
1761 }
1762 /* Clear certificate digests and validity flags */
1763 for (i = 0; i < SSL_PKEY_NUM; i++)
1764 {
1765 s->cert->pkeys[i].digest = NULL;
1766 s->cert->pkeys[i].valid_flags = 0;
1767 }
David Benjamincd996942014-07-20 16:23:51 -04001768 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001769 {
1770 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1771 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1772 goto err;
1773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774 }
1775
1776 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001777 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001778 {
1779 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1780 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1781 goto err;
1782 }
1783
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001784 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001785 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001786 CBS distinguished_name;
1787 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001788 {
Adam Langley95c29f32014-06-20 12:00:00 -07001789 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1790 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1791 goto err;
1792 }
1793
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001794 data = CBS_data(&distinguished_name);
1795 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001796 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001797 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1798 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1799 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001800 }
1801
David Benjamin407a10c2014-07-16 12:58:59 -04001802 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001803 {
1804 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1805 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1806 goto err;
1807 }
1808 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001809 {
1810 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1811 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1812 goto err;
1813 }
1814 if (!sk_X509_NAME_push(ca_sk,xn))
1815 {
1816 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1817 goto err;
1818 }
Adam Langley95c29f32014-06-20 12:00:00 -07001819 }
1820
Adam Langley95c29f32014-06-20 12:00:00 -07001821 /* we should setup a certificate to return.... */
1822 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001823 if (s->s3->tmp.ca_names != NULL)
1824 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1825 s->s3->tmp.ca_names=ca_sk;
1826 ca_sk=NULL;
1827
1828 ret=1;
1829err:
1830 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1831 return(ret);
1832 }
1833
Adam Langley95c29f32014-06-20 12:00:00 -07001834int ssl3_get_new_session_ticket(SSL *s)
1835 {
David Benjamine044c3d2014-07-14 19:14:12 -04001836 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001838 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
1840 n=s->method->ssl_get_message(s,
1841 SSL3_ST_CR_SESSION_TICKET_A,
1842 SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin64442872014-07-21 17:43:45 -04001843 SSL3_MT_NEWSESSION_TICKET,
Adam Langley95c29f32014-06-20 12:00:00 -07001844 16384,
1845 &ok);
1846
1847 if (!ok)
1848 return((int)n);
1849
David Benjamine044c3d2014-07-14 19:14:12 -04001850 CBS_init(&new_session_ticket, s->init_msg, n);
1851
1852 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1853 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1854 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001855 {
Adam Langley95c29f32014-06-20 12:00:00 -07001856 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001857 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001858 goto f_err;
1859 }
1860
David Benjamine044c3d2014-07-14 19:14:12 -04001861 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001862 {
1863 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1864 goto err;
1865 }
David Benjamine044c3d2014-07-14 19:14:12 -04001866
Adam Langley95c29f32014-06-20 12:00:00 -07001867 /* There are two ways to detect a resumed ticket sesion.
1868 * One is to set an appropriate session ID and then the server
1869 * must return a match in ServerHello. This allows the normal
1870 * client session ID matching to work and we know much
1871 * earlier that the ticket has been accepted.
1872 *
1873 * The other way is to set zero length session ID when the
1874 * ticket is presented and rely on the handshake to determine
1875 * session resumption.
1876 *
1877 * We choose the former approach because this fits in with
1878 * assumptions elsewhere in OpenSSL. The session ID is set
1879 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1880 * ticket.
1881 */
David Benjamine044c3d2014-07-14 19:14:12 -04001882 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001883 s->session->session_id, &s->session->session_id_length,
Adam Langley95c29f32014-06-20 12:00:00 -07001884 EVP_sha256(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001885 ret=1;
1886 return(ret);
1887f_err:
1888 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1889err:
1890 return(-1);
1891 }
1892
1893int ssl3_get_cert_status(SSL *s)
1894 {
1895 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001896 long n;
1897 CBS certificate_status, ocsp_response;
1898 uint8_t status_type;
1899 size_t resplen;
Adam Langley95c29f32014-06-20 12:00:00 -07001900
1901 n=s->method->ssl_get_message(s,
1902 SSL3_ST_CR_CERT_STATUS_A,
1903 SSL3_ST_CR_CERT_STATUS_B,
1904 SSL3_MT_CERTIFICATE_STATUS,
1905 16384,
1906 &ok);
1907
1908 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001909
1910 CBS_init(&certificate_status, s->init_msg, n);
1911 if (!CBS_get_u8(&certificate_status, &status_type) ||
1912 status_type != TLSEXT_STATUSTYPE_ocsp ||
1913 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1914 CBS_len(&ocsp_response) == 0 ||
1915 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001916 {
1917 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04001918 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001919 goto f_err;
1920 }
David Benjamin46062682014-07-14 19:14:32 -04001921
1922 /* TODO(davidben): Make tlsext_ocsp_resplen a
1923 * size_t. Currently it uses -1 to signal no response. The
1924 * spec does not allow ocsp_response to be zero-length, so
1925 * using 0 should be fine. */
1926 if (!CBS_stow(&ocsp_response, &s->tlsext_ocsp_resp, &resplen))
Adam Langley95c29f32014-06-20 12:00:00 -07001927 {
1928 al = SSL_AD_INTERNAL_ERROR;
1929 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1930 goto f_err;
1931 }
1932 s->tlsext_ocsp_resplen = resplen;
1933 if (s->ctx->tlsext_status_cb)
1934 {
1935 int ret;
1936 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1937 if (ret == 0)
1938 {
1939 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1940 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
1941 goto f_err;
1942 }
1943 if (ret < 0)
1944 {
1945 al = SSL_AD_INTERNAL_ERROR;
1946 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1947 goto f_err;
1948 }
1949 }
1950 return 1;
1951f_err:
1952 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1953 return(-1);
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
1956int ssl3_get_server_done(SSL *s)
1957 {
1958 int ok,ret=0;
1959 long n;
1960
1961 n=s->method->ssl_get_message(s,
1962 SSL3_ST_CR_SRVR_DONE_A,
1963 SSL3_ST_CR_SRVR_DONE_B,
1964 SSL3_MT_SERVER_DONE,
1965 30, /* should be very small, like 0 :-) */
1966 &ok);
1967
1968 if (!ok) return((int)n);
1969 if (n > 0)
1970 {
1971 /* should contain no data */
1972 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1973 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1974 return -1;
1975 }
1976 ret=1;
1977 return(ret);
1978 }
1979
1980
1981int ssl3_send_client_key_exchange(SSL *s)
1982 {
1983 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07001984 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001985 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001986 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001987 unsigned char *q;
1988 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001989#ifndef OPENSSL_NO_ECDH
1990 EC_KEY *clnt_ecdh = NULL;
1991 const EC_POINT *srvr_ecpoint = NULL;
1992 EVP_PKEY *srvr_pub_pkey = NULL;
1993 unsigned char *encodedPoint = NULL;
1994 int encoded_pt_len = 0;
1995 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001996 unsigned int psk_len = 0;
1997 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langleyc26c8022014-06-20 12:00:00 -07001998#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07001999
2000 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2001 {
2002 p = ssl_handshake_start(s);
2003
2004 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07002005 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2006
Adam Langleyc26c8022014-06-20 12:00:00 -07002007 if (alg_a & SSL_aPSK)
2008 {
Adam Langley01797e32014-06-20 12:00:00 -07002009 char identity[PSK_MAX_IDENTITY_LEN + 1];
2010 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002011 unsigned char *t = NULL;
2012 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
2013 unsigned int pre_ms_len = 0;
2014 int psk_err = 1;
2015
2016 n = 0;
2017 if (s->psk_client_callback == NULL)
2018 {
2019 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
2020 goto err;
2021 }
2022
Adam Langley01797e32014-06-20 12:00:00 -07002023 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07002024 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07002025 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07002026 if (psk_len > PSK_MAX_PSK_LEN)
2027 {
2028 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2029 goto psk_err;
2030 }
2031 else if (psk_len == 0)
2032 {
2033 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
2034 goto psk_err;
2035 }
Adam Langley01797e32014-06-20 12:00:00 -07002036 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
2037 if (identity_len > PSK_MAX_IDENTITY_LEN)
2038 {
2039 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2040 goto psk_err;
2041 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002042
2043 if (!(alg_k & SSL_kEECDH))
2044 {
David Benjamin14c83e72014-07-13 04:54:57 -04002045 /* Create the shared secret now if we're not using ECDHE-PSK.
2046 * TODO(davidben): Refactor this logic similarly
2047 * to ssl3_get_client_key_exchange. */
Adam Langleyc26c8022014-06-20 12:00:00 -07002048 pre_ms_len = 2+psk_len+2+psk_len;
2049 t = pre_ms;
2050 s2n(psk_len, t);
2051 memset(t, 0, psk_len);
2052 t+=psk_len;
2053 s2n(psk_len, t);
2054 memcpy(t, psk, psk_len);
2055
2056 s->session->master_key_length =
2057 s->method->ssl3_enc->generate_master_secret(s,
2058 s->session->master_key,
2059 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07002060 s2n(identity_len, p);
2061 memcpy(p, identity, identity_len);
2062 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07002063 }
2064
Adam Langleyc26c8022014-06-20 12:00:00 -07002065 if (s->session->psk_identity != NULL)
2066 OPENSSL_free(s->session->psk_identity);
2067 s->session->psk_identity = BUF_strdup(identity);
2068 if (s->session->psk_identity == NULL)
2069 {
2070 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2071 goto psk_err;
2072 }
2073 psk_err = 0;
2074 psk_err:
2075 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2076 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2077 if (psk_err != 0)
2078 {
2079 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2080 goto err;
2081 }
2082 }
Adam Langley95c29f32014-06-20 12:00:00 -07002083
2084 /* Fool emacs indentation */
2085 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002086 else if (alg_k & SSL_kRSA)
2087 {
2088 RSA *rsa;
2089 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2090
Emilia Käsperc3d79602014-07-24 22:15:29 +02002091 if (s->session->sess_cert == NULL)
2092 {
2093 /* We should always have a server certificate with SSL_kRSA. */
2094 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2095 goto err;
2096 }
2097
Adam Langley95c29f32014-06-20 12:00:00 -07002098 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2099 rsa=s->session->sess_cert->peer_rsa_tmp;
2100 else
2101 {
2102 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2103 if ((pkey == NULL) ||
2104 (pkey->type != EVP_PKEY_RSA) ||
2105 (pkey->pkey.rsa == NULL))
2106 {
2107 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2108 goto err;
2109 }
2110 rsa=pkey->pkey.rsa;
2111 EVP_PKEY_free(pkey);
2112 }
2113
2114 tmp_buf[0]=s->client_version>>8;
2115 tmp_buf[1]=s->client_version&0xff;
2116 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2117 goto err;
2118
2119 s->session->master_key_length=sizeof tmp_buf;
2120
2121 q=p;
2122 /* Fix buf for TLS and beyond */
2123 if (s->version > SSL3_VERSION)
2124 p+=2;
2125 n=RSA_public_encrypt(sizeof tmp_buf,
2126 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
Adam Langley95c29f32014-06-20 12:00:00 -07002127 if (n <= 0)
2128 {
2129 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2130 goto err;
2131 }
2132
2133 /* Fix buf for TLS and beyond */
2134 if (s->version > SSL3_VERSION)
2135 {
2136 s2n(n,q);
2137 n+=2;
2138 }
2139
2140 s->session->master_key_length=
2141 s->method->ssl3_enc->generate_master_secret(s,
2142 s->session->master_key,
2143 tmp_buf,sizeof tmp_buf);
2144 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2145 }
Adam Langley95c29f32014-06-20 12:00:00 -07002146#ifndef OPENSSL_NO_DH
2147 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2148 {
2149 DH *dh_srvr,*dh_clnt;
2150 SESS_CERT *scert = s->session->sess_cert;
2151
2152 if (scert == NULL)
2153 {
2154 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2155 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2156 goto err;
2157 }
2158
2159 if (scert->peer_dh_tmp != NULL)
2160 dh_srvr=scert->peer_dh_tmp;
2161 else
2162 {
2163 /* we get them from the cert */
2164 int idx = scert->peer_cert_type;
2165 EVP_PKEY *spkey = NULL;
2166 dh_srvr = NULL;
2167 if (idx >= 0)
2168 spkey = X509_get_pubkey(
2169 scert->peer_pkeys[idx].x509);
2170 if (spkey)
2171 {
2172 dh_srvr = EVP_PKEY_get1_DH(spkey);
2173 EVP_PKEY_free(spkey);
2174 }
2175 if (dh_srvr == NULL)
2176 {
2177 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2178 goto err;
2179 }
2180 }
2181 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2182 {
2183 /* Use client certificate key */
2184 EVP_PKEY *clkey = s->cert->key->privatekey;
2185 dh_clnt = NULL;
2186 if (clkey)
2187 dh_clnt = EVP_PKEY_get1_DH(clkey);
2188 if (dh_clnt == NULL)
2189 {
2190 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2191 goto err;
2192 }
2193 }
2194 else
2195 {
2196 /* generate a new random key */
2197 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2198 {
2199 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2200 goto err;
2201 }
2202 if (!DH_generate_key(dh_clnt))
2203 {
2204 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2205 DH_free(dh_clnt);
2206 goto err;
2207 }
2208 }
2209
2210 /* use the 'p' output buffer for the DH key, but
2211 * make sure to clear it out afterwards */
2212
2213 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2214 if (scert->peer_dh_tmp == NULL)
2215 DH_free(dh_srvr);
2216
2217 if (n <= 0)
2218 {
2219 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2220 DH_free(dh_clnt);
2221 goto err;
2222 }
2223
2224 /* generate master key from the result */
2225 s->session->master_key_length=
2226 s->method->ssl3_enc->generate_master_secret(s,
2227 s->session->master_key,p,n);
2228 /* clean up */
2229 memset(p,0,n);
2230
2231 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2232 n = 0;
2233 else
2234 {
2235 /* send off the data */
2236 n=BN_num_bytes(dh_clnt->pub_key);
2237 s2n(n,p);
2238 BN_bn2bin(dh_clnt->pub_key,p);
2239 n+=2;
2240 }
2241
2242 DH_free(dh_clnt);
2243
2244 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2245 }
2246#endif
2247
Adam Langleyc26c8022014-06-20 12:00:00 -07002248#ifndef OPENSSL_NO_ECDH
Adam Langley95c29f32014-06-20 12:00:00 -07002249 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2250 {
2251 const EC_GROUP *srvr_group = NULL;
2252 EC_KEY *tkey;
2253 int ecdh_clnt_cert = 0;
2254 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002255 unsigned char *pre_ms;
2256 unsigned char *t;
2257 unsigned int pre_ms_len;
2258 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyd06eddd2014-06-20 12:00:00 -07002260 if (s->session->sess_cert == NULL)
2261 {
2262 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2263 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2264 goto err;
2265 }
2266
Adam Langley95c29f32014-06-20 12:00:00 -07002267 /* Did we send out the client's
2268 * ECDH share for use in premaster
2269 * computation as part of client certificate?
2270 * If so, set ecdh_clnt_cert to 1.
2271 */
2272 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2273 {
2274 /* XXX: For now, we do not support client
2275 * authentication using ECDH certificates.
2276 * To add such support, one needs to add
2277 * code that checks for appropriate
2278 * conditions and sets ecdh_clnt_cert to 1.
2279 * For example, the cert have an ECC
2280 * key on the same curve as the server's
2281 * and the key should be authorized for
2282 * key agreement.
2283 *
2284 * One also needs to add code in ssl3_connect
2285 * to skip sending the certificate verify
2286 * message.
2287 *
2288 * if ((s->cert->key->privatekey != NULL) &&
2289 * (s->cert->key->privatekey->type ==
2290 * EVP_PKEY_EC) && ...)
2291 * ecdh_clnt_cert = 1;
2292 */
2293 }
2294
2295 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2296 {
2297 tkey = s->session->sess_cert->peer_ecdh_tmp;
2298 }
2299 else
2300 {
2301 /* Get the Server Public Key from Cert */
2302 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2303 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2304 if ((srvr_pub_pkey == NULL) ||
2305 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2306 (srvr_pub_pkey->pkey.ec == NULL))
2307 {
2308 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2309 goto err;
2310 }
2311
2312 tkey = srvr_pub_pkey->pkey.ec;
2313 }
2314
2315 srvr_group = EC_KEY_get0_group(tkey);
2316 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2317
2318 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2319 {
2320 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2321 goto err;
2322 }
2323
2324 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2325 {
2326 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2327 goto err;
2328 }
2329
2330 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2331 {
2332 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2333 goto err;
2334 }
2335 if (ecdh_clnt_cert)
2336 {
2337 /* Reuse key info from our certificate
2338 * We only need our private key to perform
2339 * the ECDH computation.
2340 */
2341 const BIGNUM *priv_key;
2342 tkey = s->cert->key->privatekey->pkey.ec;
2343 priv_key = EC_KEY_get0_private_key(tkey);
2344 if (priv_key == NULL)
2345 {
2346 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2347 goto err;
2348 }
2349 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2350 {
2351 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2352 goto err;
2353 }
2354 }
2355 else
2356 {
2357 /* Generate a new ECDH key pair */
2358 if (!(EC_KEY_generate_key(clnt_ecdh)))
2359 {
2360 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2361 goto err;
2362 }
2363 }
2364
2365 /* use the 'p' output buffer for the ECDH key, but
2366 * make sure to clear it out afterwards
2367 */
2368
2369 field_size = EC_GROUP_get_degree(srvr_group);
2370 if (field_size <= 0)
2371 {
2372 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2373 goto err;
2374 }
2375 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2376 if (n <= 0)
2377 {
2378 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2379 goto err;
2380 }
2381
Adam Langleyc26c8022014-06-20 12:00:00 -07002382 /* ECDHE PSK ciphersuites from RFC 5489 */
2383 if ((alg_a & SSL_aPSK) && psk_len != 0)
2384 {
2385 pre_ms_len = 2+psk_len+2+n;
2386 pre_ms = OPENSSL_malloc(pre_ms_len);
2387 if (pre_ms == NULL)
2388 {
2389 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2390 goto err;
2391 }
2392 memset(pre_ms, 0, pre_ms_len);
2393 t = pre_ms;
2394 s2n(psk_len, t);
2395 memcpy(t, psk, psk_len);
2396 t += psk_len;
2397 s2n(n, t);
2398 memcpy(t, p, n);
2399 s->session->master_key_length = s->method->ssl3_enc \
2400 -> generate_master_secret(s,
2401 s->session->master_key, pre_ms, pre_ms_len);
2402 OPENSSL_cleanse(pre_ms, pre_ms_len);
2403 OPENSSL_free(pre_ms);
2404 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002405 if (!(alg_a & SSL_aPSK))
2406 {
2407 /* generate master key from the result */
2408 s->session->master_key_length = s->method->ssl3_enc \
2409 -> generate_master_secret(s,
2410 s->session->master_key, p, n);
2411 }
Adam Langley95c29f32014-06-20 12:00:00 -07002412 memset(p, 0, n); /* clean up */
2413
Adam Langleyc26c8022014-06-20 12:00:00 -07002414 if (ecdh_clnt_cert)
Adam Langley95c29f32014-06-20 12:00:00 -07002415 {
2416 /* Send empty client key exch message */
2417 n = 0;
2418 }
2419 else
2420 {
2421 /* First check the size of encoding and
2422 * allocate memory accordingly.
2423 */
2424 encoded_pt_len =
2425 EC_POINT_point2oct(srvr_group,
2426 EC_KEY_get0_public_key(clnt_ecdh),
2427 POINT_CONVERSION_UNCOMPRESSED,
2428 NULL, 0, NULL);
2429
2430 encodedPoint = (unsigned char *)
2431 OPENSSL_malloc(encoded_pt_len *
2432 sizeof(unsigned char));
2433 bn_ctx = BN_CTX_new();
2434 if ((encodedPoint == NULL) ||
2435 (bn_ctx == NULL))
2436 {
2437 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2438 goto err;
2439 }
2440
2441 /* Encode the public key */
Adam Langleyc26c8022014-06-20 12:00:00 -07002442 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2443 EC_KEY_get0_public_key(clnt_ecdh),
2444 POINT_CONVERSION_UNCOMPRESSED,
Adam Langley95c29f32014-06-20 12:00:00 -07002445 encodedPoint, encoded_pt_len, bn_ctx);
2446
Adam Langleyc26c8022014-06-20 12:00:00 -07002447 n = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002448 if ((alg_a & SSL_aPSK) && psk_len != 0)
2449 {
2450 i = strlen(s->session->psk_identity);
2451 s2n(i, p);
2452 memcpy(p, s->session->psk_identity, i);
2453 p += i;
2454 n = i + 2;
2455 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002456
2457 *p = encoded_pt_len; /* length of encoded point */
Adam Langley95c29f32014-06-20 12:00:00 -07002458 /* Encoded point will be copied here */
Adam Langleyc26c8022014-06-20 12:00:00 -07002459 p += 1;
2460 n += 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002461 /* copy the point */
Adam Langleyc26c8022014-06-20 12:00:00 -07002462 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
Adam Langley95c29f32014-06-20 12:00:00 -07002463 /* increment n to account for length field */
Adam Langleyc26c8022014-06-20 12:00:00 -07002464 n += encoded_pt_len;
Adam Langley95c29f32014-06-20 12:00:00 -07002465 }
2466
2467 /* Free allocated memory */
2468 BN_CTX_free(bn_ctx);
2469 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002470 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002471 EC_KEY_free(clnt_ecdh);
2472 EVP_PKEY_free(srvr_pub_pkey);
2473 }
2474#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002475 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002476 {
2477 ssl3_send_alert(s, SSL3_AL_FATAL,
2478 SSL_AD_HANDSHAKE_FAILURE);
2479 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2480 goto err;
2481 }
2482
2483 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2484 s->state=SSL3_ST_CW_KEY_EXCH_B;
2485 }
2486
2487 /* SSL3_ST_CW_KEY_EXCH_B */
2488 return ssl_do_write(s);
2489err:
2490#ifndef OPENSSL_NO_ECDH
2491 BN_CTX_free(bn_ctx);
2492 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2493 if (clnt_ecdh != NULL)
2494 EC_KEY_free(clnt_ecdh);
2495 EVP_PKEY_free(srvr_pub_pkey);
2496#endif
2497 return(-1);
2498 }
2499
2500int ssl3_send_client_verify(SSL *s)
2501 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002502 unsigned char *buf, *p;
2503 const EVP_MD *md;
2504 uint8_t digest[EVP_MAX_MD_SIZE];
2505 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002506 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002507 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002508 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002509 size_t signature_length = 0;
2510 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002511
2512 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002513 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002514
2515 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2516 {
2517 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002518 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002519 /* For TLS v1.2 send signature algorithm and signature using
2520 * agreed digest and cached handshake records. Otherwise, use
2521 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002522 */
2523 if (SSL_USE_SIGALGS(s))
2524 {
Adam Langley37a623c2014-07-18 09:28:40 -07002525 const uint8_t *hdata;
2526 size_t hdatalen;
David Benjamin0e2908a2014-07-09 15:22:43 -04002527 md = s->cert->key->digest;
Adam Langley37a623c2014-07-18 09:28:40 -07002528 if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen) ||
2529 !tls12_get_sigandhash(p, pkey, md))
Adam Langley95c29f32014-06-20 12:00:00 -07002530 {
2531 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2532 goto err;
2533 }
2534 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002535 n += 2;
2536 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2537 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2538 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002539 {
2540 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2541 goto err;
2542 }
Adam Langley95c29f32014-06-20 12:00:00 -07002543 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002544 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002545 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002546 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002547 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002548 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2549 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2550 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2551 * a raw RSA signature, rather than wrapping in a
2552 * DigestInfo. */
2553 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002554 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002555 else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002556 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002557 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2558 digest_length = SHA_DIGEST_LENGTH;
2559 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002560 }
2561 else
Adam Langleyaed23062014-06-20 12:00:00 -07002562 {
Adam Langley95c29f32014-06-20 12:00:00 -07002563 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
2564 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002565 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002566
2567 /* Sign the digest. */
2568 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2569 if (pctx == NULL)
2570 goto err;
2571
2572 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2573 if (EVP_PKEY_sign_init(pctx) != 1 ||
2574 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2575 EVP_PKEY_sign(pctx, NULL, &signature_length,
2576 digest, digest_length) != 1)
2577 {
2578 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2579 goto err;
2580 }
2581
2582 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2583 {
2584 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
2585 goto err;
2586 }
2587
2588 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2589 digest, digest_length) != 1)
2590 {
2591 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
2592 goto err;
2593 }
2594
2595 s2n(signature_length, p);
2596 n += signature_length + 2;
2597
2598 /* Now that client auth is completed, we no longer need cached
2599 * handshake records and can digest them. */
2600 if (SSL_USE_SIGALGS(s))
2601 {
2602 if (!ssl3_digest_cached_records(s))
2603 goto err;
2604 }
2605
Adam Langley95c29f32014-06-20 12:00:00 -07002606 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2607 s->state=SSL3_ST_CW_CERT_VRFY_B;
2608 }
2609 EVP_MD_CTX_cleanup(&mctx);
2610 EVP_PKEY_CTX_free(pctx);
2611 return ssl_do_write(s);
2612err:
2613 EVP_MD_CTX_cleanup(&mctx);
2614 EVP_PKEY_CTX_free(pctx);
2615 return(-1);
2616 }
2617
2618/* Check a certificate can be used for client authentication. Currently
2619 * check cert exists, if we have a suitable digest for TLS 1.2 if
2620 * static DH client certificates can be used and optionally checks
2621 * suitability for Suite B.
2622 */
2623static int ssl3_check_client_certificate(SSL *s)
2624 {
2625 unsigned long alg_k;
2626 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2627 return 0;
2628 /* If no suitable signature algorithm can't use certificate */
2629 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2630 return 0;
2631 /* If strict mode check suitability of chain before using it.
2632 * This also adjusts suite B digest if necessary.
2633 */
2634 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2635 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2636 return 0;
2637 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2638 /* See if we can use client certificate for fixed DH */
2639 if (alg_k & (SSL_kDHr|SSL_kDHd))
2640 {
2641 SESS_CERT *scert = s->session->sess_cert;
2642 int i = scert->peer_cert_type;
2643 EVP_PKEY *clkey = NULL, *spkey = NULL;
2644 clkey = s->cert->key->privatekey;
2645 /* If client key not DH assume it can be used */
2646 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2647 return 1;
2648 if (i >= 0)
2649 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
2650 if (spkey)
2651 {
2652 /* Compare server and client parameters */
2653 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2654 EVP_PKEY_free(spkey);
2655 if (i != 1)
2656 return 0;
2657 }
2658 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2659 }
2660 return 1;
2661 }
2662
2663int ssl3_send_client_certificate(SSL *s)
2664 {
2665 X509 *x509=NULL;
2666 EVP_PKEY *pkey=NULL;
2667 int i;
2668
2669 if (s->state == SSL3_ST_CW_CERT_A)
2670 {
2671 /* Let cert callback update client certificates if required */
2672 if (s->cert->cert_cb)
2673 {
2674 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2675 if (i < 0)
2676 {
2677 s->rwstate=SSL_X509_LOOKUP;
2678 return -1;
2679 }
2680 if (i == 0)
2681 {
2682 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2683 return 0;
2684 }
2685 s->rwstate=SSL_NOTHING;
2686 }
2687 if (ssl3_check_client_certificate(s))
2688 s->state=SSL3_ST_CW_CERT_C;
2689 else
2690 s->state=SSL3_ST_CW_CERT_B;
2691 }
2692
2693 /* We need to get a client cert */
2694 if (s->state == SSL3_ST_CW_CERT_B)
2695 {
2696 /* If we get an error, we need to
2697 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
David Benjamin7bbeead2014-07-25 03:56:20 -04002698 * We then get retried later */
Adam Langley95c29f32014-06-20 12:00:00 -07002699 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2700 if (i < 0)
2701 {
2702 s->rwstate=SSL_X509_LOOKUP;
2703 return(-1);
2704 }
2705 s->rwstate=SSL_NOTHING;
2706 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2707 {
2708 s->state=SSL3_ST_CW_CERT_B;
2709 if ( !SSL_use_certificate(s,x509) ||
2710 !SSL_use_PrivateKey(s,pkey))
2711 i=0;
2712 }
2713 else if (i == 1)
2714 {
2715 i=0;
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002716 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
Adam Langley95c29f32014-06-20 12:00:00 -07002717 }
2718
2719 if (x509 != NULL) X509_free(x509);
2720 if (pkey != NULL) EVP_PKEY_free(pkey);
2721 if (i && !ssl3_check_client_certificate(s))
2722 i = 0;
2723 if (i == 0)
2724 {
2725 if (s->version == SSL3_VERSION)
2726 {
2727 s->s3->tmp.cert_req=0;
2728 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2729 return(1);
2730 }
2731 else
2732 {
2733 s->s3->tmp.cert_req=2;
2734 }
2735 }
2736
2737 /* Ok, we have a cert */
2738 s->state=SSL3_ST_CW_CERT_C;
2739 }
2740
2741 if (s->state == SSL3_ST_CW_CERT_C)
2742 {
2743 s->state=SSL3_ST_CW_CERT_D;
2744 ssl3_output_cert_chain(s,
2745 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2746 }
2747 /* SSL3_ST_CW_CERT_D */
2748 return ssl_do_write(s);
2749 }
2750
2751#define has_bits(i,m) (((i)&(m)) == (m))
2752
2753int ssl3_check_cert_and_algorithm(SSL *s)
2754 {
2755 int i,idx;
2756 long alg_k,alg_a;
2757 EVP_PKEY *pkey=NULL;
2758 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002759 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002760#ifndef OPENSSL_NO_DH
2761 DH *dh;
2762#endif
2763
David Benjamine8f3d662014-07-12 01:10:19 -04002764 /* we don't have a certificate */
2765 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2766 return 1;
2767
Adam Langley95c29f32014-06-20 12:00:00 -07002768 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2769 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2770
Adam Langley95c29f32014-06-20 12:00:00 -07002771 sc=s->session->sess_cert;
2772 if (sc == NULL)
2773 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002774 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002775 goto err;
2776 }
2777
Adam Langley95c29f32014-06-20 12:00:00 -07002778 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002779#ifndef OPENSSL_NO_DH
2780 dh=s->session->sess_cert->peer_dh_tmp;
2781#endif
2782
2783 /* This is the passed certificate */
2784
2785 idx=sc->peer_cert_type;
2786#ifndef OPENSSL_NO_ECDH
2787 if (idx == SSL_PKEY_ECC)
2788 {
2789 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2790 s) == 0)
2791 { /* check failed */
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002792 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002793 goto f_err;
2794 }
2795 else
2796 {
2797 return 1;
2798 }
2799 }
2800 else if (alg_a & SSL_aECDSA)
2801 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002802 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002803 goto f_err;
2804 }
2805 else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2806 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002807 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDH_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002808 goto f_err;
2809 }
2810#endif
2811 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2812 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2813 EVP_PKEY_free(pkey);
2814
2815
2816 /* Check that we have a certificate if we require one */
2817 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2818 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002819 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002820 goto f_err;
2821 }
2822#ifndef OPENSSL_NO_DSA
2823 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2824 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002825 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002826 goto f_err;
2827 }
2828#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002829 if ((alg_k & SSL_kRSA) &&
2830 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2831 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002832 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002833 goto f_err;
2834 }
Adam Langley95c29f32014-06-20 12:00:00 -07002835#ifndef OPENSSL_NO_DH
2836 if ((alg_k & SSL_kEDH) &&
2837 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2838 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002839 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -07002840 goto f_err;
2841 }
2842 else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2843 !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2844 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002845 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_RSA_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002846 goto f_err;
2847 }
2848#ifndef OPENSSL_NO_DSA
2849 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2850 !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2851 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002852 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_DSA_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002853 goto f_err;
2854 }
2855#endif
2856#endif
2857
Adam Langley95c29f32014-06-20 12:00:00 -07002858 return(1);
2859f_err:
2860 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2861err:
2862 return(0);
2863 }
2864
Adam Langley1258b6a2014-06-20 12:00:00 -07002865# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07002866int ssl3_send_next_proto(SSL *s)
2867 {
2868 unsigned int len, padding_len;
2869 unsigned char *d;
2870
2871 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
2872 {
2873 len = s->next_proto_negotiated_len;
2874 padding_len = 32 - ((len + 2) % 32);
2875 d = (unsigned char *)s->init_buf->data;
2876 d[4] = len;
2877 memcpy(d + 5, s->next_proto_negotiated, len);
2878 d[5 + len] = padding_len;
2879 memset(d + 6 + len, 0, padding_len);
2880 *(d++)=SSL3_MT_NEXT_PROTO;
2881 l2n3(2 + len + padding_len, d);
2882 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2883 s->init_num = 4 + 2 + len + padding_len;
2884 s->init_off = 0;
2885 }
2886
2887 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2888}
Adam Langley1258b6a2014-06-20 12:00:00 -07002889
2890# endif /* !OPENSSL_NO_NEXTPROTONEG */
2891
2892int ssl3_send_channel_id(SSL *s)
2893 {
2894 unsigned char *d;
2895 int ret = -1, public_key_len;
2896 EVP_MD_CTX md_ctx;
2897 size_t sig_len;
2898 ECDSA_SIG *sig = NULL;
2899 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
2900
2901 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
2902 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2903
2904 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
2905 {
2906 EVP_PKEY *key = NULL;
2907 s->ctx->channel_id_cb(s, &key);
2908 if (key != NULL)
2909 {
2910 s->tlsext_channel_id_private = key;
2911 }
2912 }
2913 if (!s->tlsext_channel_id_private)
2914 {
2915 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
2916 return (-1);
2917 }
2918 s->rwstate=SSL_NOTHING;
2919
2920 d = (unsigned char *)s->init_buf->data;
2921 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
2922 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
2923 if (s->s3->tlsext_channel_id_new)
2924 s2n(TLSEXT_TYPE_channel_id_new, d);
2925 else
2926 s2n(TLSEXT_TYPE_channel_id, d);
2927 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2928
2929 EVP_MD_CTX_init(&md_ctx);
2930
2931 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2932 if (public_key_len <= 0)
2933 {
2934 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2935 goto err;
2936 }
2937 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2938 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2939 * field elements as 32-byte, big-endian numbers. */
2940 if (public_key_len != 65)
2941 {
2942 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2943 goto err;
2944 }
2945 public_key = OPENSSL_malloc(public_key_len);
2946 if (!public_key)
2947 {
2948 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2949 goto err;
2950 }
2951
2952 derp = public_key;
2953 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2954
2955 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2956 s->tlsext_channel_id_private) != 1)
2957 {
2958 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2959 goto err;
2960 }
2961
2962 if (!tls1_channel_id_hash(&md_ctx, s))
2963 goto err;
2964
2965 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
2966 {
2967 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2968 goto err;
2969 }
2970
2971 der_sig = OPENSSL_malloc(sig_len);
2972 if (!der_sig)
2973 {
2974 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2975 goto err;
2976 }
2977
2978 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
2979 {
2980 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2981 goto err;
2982 }
2983
2984 derp = der_sig;
2985 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
2986 if (sig == NULL)
2987 {
2988 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2989 goto err;
2990 }
2991
2992 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2993 memcpy(d, public_key + 1, 64);
2994 d += 64;
2995 memset(d, 0, 2 * 32);
2996 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
2997 d += 32;
2998 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
2999 d += 32;
3000
3001 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3002 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3003 s->init_off = 0;
3004
3005 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3006
3007err:
3008 EVP_MD_CTX_cleanup(&md_ctx);
3009 if (public_key)
3010 OPENSSL_free(public_key);
3011 if (der_sig)
3012 OPENSSL_free(der_sig);
3013 if (sig)
3014 ECDSA_SIG_free(sig);
3015
3016 return ret;
3017 }
Adam Langley95c29f32014-06-20 12:00:00 -07003018
Adam Langley95c29f32014-06-20 12:00:00 -07003019int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3020 {
3021 int i = 0;
3022 /* TODO(fork): remove */
3023#if 0
3024#ifndef OPENSSL_NO_ENGINE
3025 if (s->ctx->client_cert_engine)
3026 {
3027 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3028 SSL_get_client_CA_list(s),
3029 px509, ppkey, NULL, NULL, NULL);
3030 if (i != 0)
3031 return i;
3032 }
3033#endif
3034#endif
3035 if (s->ctx->client_cert_cb)
3036 i = s->ctx->client_cert_cb(s,px509,ppkey);
3037 return i;
3038 }