blob: ec992c294ce302a1f7517c7b9ab89ae3c9f23e30 [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;
David Benjamin8da99062014-08-24 12:03:09 -0400264 ret=ssl3_send_client_hello(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700265 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;
Adam Langley95c29f32014-06-20 12:00:00 -0700365 /* For TLS, cert_req is set to 2, so a cert chain
366 * of nothing is sent, but no verify packet is sent */
Adam Langley95c29f32014-06-20 12:00:00 -0700367 if (s->s3->tmp.cert_req == 1)
368 {
369 s->state=SSL3_ST_CW_CERT_VRFY_A;
370 }
371 else
372 {
373 s->state=SSL3_ST_CW_CHANGE_A;
374 s->s3->change_cipher_spec=0;
375 }
Adam Langley95c29f32014-06-20 12:00:00 -0700376
377 s->init_num=0;
378 break;
379
380 case SSL3_ST_CW_CERT_VRFY_A:
381 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamin8da99062014-08-24 12:03:09 -0400382 ret=ssl3_send_cert_verify(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700383 if (ret <= 0) goto end;
384 s->state=SSL3_ST_CW_CHANGE_A;
385 s->init_num=0;
386 s->s3->change_cipher_spec=0;
387 break;
388
389 case SSL3_ST_CW_CHANGE_A:
390 case SSL3_ST_CW_CHANGE_B:
391 ret=ssl3_send_change_cipher_spec(s,
392 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
393 if (ret <= 0) goto end;
394
Adam Langley1258b6a2014-06-20 12:00:00 -0700395 s->state=SSL3_ST_CW_FINISHED_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700396 if (s->s3->tlsext_channel_id_valid)
397 s->state=SSL3_ST_CW_CHANNEL_ID_A;
398# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700399 if (s->s3->next_proto_neg_seen)
400 s->state=SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1258b6a2014-06-20 12:00:00 -0700401# endif
Adam Langley95c29f32014-06-20 12:00:00 -0700402 s->init_num=0;
403
404 s->session->cipher=s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -0700405 if (!s->method->ssl3_enc->setup_key_block(s))
406 {
407 ret= -1;
408 goto end;
409 }
410
411 if (!s->method->ssl3_enc->change_cipher_state(s,
412 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
413 {
414 ret= -1;
415 goto end;
416 }
417
418 break;
419
David Benjamin6dbd73d2014-07-03 15:59:49 -0400420#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700421 case SSL3_ST_CW_NEXT_PROTO_A:
422 case SSL3_ST_CW_NEXT_PROTO_B:
423 ret=ssl3_send_next_proto(s);
424 if (ret <= 0) goto end;
Adam Langley1258b6a2014-06-20 12:00:00 -0700425 if (s->s3->tlsext_channel_id_valid)
426 s->state=SSL3_ST_CW_CHANNEL_ID_A;
427 else
428 s->state=SSL3_ST_CW_FINISHED_A;
429 break;
430#endif
431
Adam Langley1258b6a2014-06-20 12:00:00 -0700432 case SSL3_ST_CW_CHANNEL_ID_A:
433 case SSL3_ST_CW_CHANNEL_ID_B:
434 ret=ssl3_send_channel_id(s);
435 if (ret <= 0) goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700436 s->state=SSL3_ST_CW_FINISHED_A;
437 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
439 case SSL3_ST_CW_FINISHED_A:
440 case SSL3_ST_CW_FINISHED_B:
441 ret=ssl3_send_finished(s,
442 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
443 s->method->ssl3_enc->client_finished_label,
444 s->method->ssl3_enc->client_finished_label_len);
445 if (ret <= 0) goto end;
446 s->state=SSL3_ST_CW_FLUSH;
447
448 /* clear flags */
449 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
450 if (s->hit)
451 {
452 s->s3->tmp.next_state=SSL_ST_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700453 }
454 else
455 {
Adam Langley1258b6a2014-06-20 12:00:00 -0700456 /* This is a non-resumption handshake. If it
457 * involves ChannelID, then record the
458 * handshake hashes at this point in the
459 * session so that any resumption of this
460 * session with ChannelID can sign those
461 * hashes. */
462 if (s->s3->tlsext_channel_id_new)
463 {
464 ret = tls1_record_handshake_hashes_for_channel_id(s);
465 if (ret <= 0)
466 goto end;
467 }
Adam Langleyadb739e2014-06-20 12:00:00 -0700468 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
469 && ssl3_can_cutthrough(s)
470 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
471 )
472 {
Adam Langley045cc552014-07-16 10:45:57 -0700473 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475 else
Adam Langleyadb739e2014-06-20 12:00:00 -0700476 {
Adam Langleyadb739e2014-06-20 12:00:00 -0700477 /* Allow NewSessionTicket if ticket expected */
478 if (s->tlsext_ticket_expected)
479 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
480 else
David Benjamincb5abad2014-07-25 12:14:28 -0400481 s->s3->tmp.next_state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483 }
484 s->init_num=0;
485 break;
486
Adam Langley95c29f32014-06-20 12:00:00 -0700487 case SSL3_ST_CR_SESSION_TICKET_A:
488 case SSL3_ST_CR_SESSION_TICKET_B:
489 ret=ssl3_get_new_session_ticket(s);
490 if (ret <= 0) goto end;
David Benjamincb5abad2014-07-25 12:14:28 -0400491 s->state=SSL3_ST_CR_CHANGE;
Adam Langley95c29f32014-06-20 12:00:00 -0700492 s->init_num=0;
493 break;
494
495 case SSL3_ST_CR_CERT_STATUS_A:
496 case SSL3_ST_CR_CERT_STATUS_B:
497 ret=ssl3_get_cert_status(s);
498 if (ret <= 0) goto end;
499 s->state=SSL3_ST_CR_KEY_EXCH_A;
500 s->init_num=0;
501 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjamincb5abad2014-07-25 12:14:28 -0400503 case SSL3_ST_CR_CHANGE:
504 /* At this point, the next message must be entirely
505 * behind a ChangeCipherSpec. */
David Benjamin86271ee2014-07-21 16:14:03 -0400506 if (!ssl3_expect_change_cipher_spec(s))
507 {
508 ret = -1;
509 goto end;
510 }
David Benjamincb5abad2014-07-25 12:14:28 -0400511 s->state = SSL3_ST_CR_FINISHED_A;
512 break;
513
Adam Langley95c29f32014-06-20 12:00:00 -0700514 case SSL3_ST_CR_FINISHED_A:
515 case SSL3_ST_CR_FINISHED_B:
Adam Langley95c29f32014-06-20 12:00:00 -0700516 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
517 SSL3_ST_CR_FINISHED_B);
518 if (ret <= 0) goto end;
519
520 if (s->hit)
521 s->state=SSL3_ST_CW_CHANGE_A;
522 else
523 s->state=SSL_ST_OK;
524 s->init_num=0;
525 break;
526
527 case SSL3_ST_CW_FLUSH:
528 s->rwstate=SSL_WRITING;
529 if (BIO_flush(s->wbio) <= 0)
530 {
531 ret= -1;
532 goto end;
533 }
534 s->rwstate=SSL_NOTHING;
535 s->state=s->s3->tmp.next_state;
536 break;
537
Adam Langleyadb739e2014-06-20 12:00:00 -0700538 case SSL3_ST_CUTTHROUGH_COMPLETE:
Adam Langleyadb739e2014-06-20 12:00:00 -0700539 /* Allow NewSessionTicket if ticket expected */
540 if (s->tlsext_ticket_expected)
541 s->state=SSL3_ST_CR_SESSION_TICKET_A;
542 else
David Benjamin7e3305e2014-07-28 14:52:32 -0400543 s->state=SSL3_ST_CR_CHANGE;
Adam Langleyadb739e2014-06-20 12:00:00 -0700544
Adam Langley045cc552014-07-16 10:45:57 -0700545 ssl_free_wbio_buffer(s);
Adam Langleyadb739e2014-06-20 12:00:00 -0700546 ret = 1;
547 goto end;
548 /* break; */
549
Adam Langley95c29f32014-06-20 12:00:00 -0700550 case SSL_ST_OK:
551 /* clean a few things up */
552 ssl3_cleanup_key_block(s);
553
554 if (s->init_buf != NULL)
555 {
556 BUF_MEM_free(s->init_buf);
557 s->init_buf=NULL;
558 }
559
560 /* If we are not 'joining' the last two packets,
561 * remove the buffering now */
562 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
563 ssl_free_wbio_buffer(s);
564 /* else do it later in ssl3_write */
565
566 s->init_num=0;
567 s->renegotiate=0;
568 s->new_session=0;
569
570 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
571 if (s->hit) s->ctx->stats.sess_hit++;
572
573 ret=1;
574 /* s->server=0; */
575 s->handshake_func=ssl3_connect;
576 s->ctx->stats.sess_connect_good++;
577
578 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
579
580 goto end;
581 /* break; */
582
583 default:
584 OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
585 ret= -1;
586 goto end;
587 /* break; */
588 }
589
590 /* did we do anything */
591 if (!s->s3->tmp.reuse_message && !skip)
592 {
593 if (s->debug)
594 {
595 if ((ret=BIO_flush(s->wbio)) <= 0)
596 goto end;
597 }
598
599 if ((cb != NULL) && (s->state != state))
600 {
601 new_state=s->state;
602 s->state=state;
603 cb(s,SSL_CB_CONNECT_LOOP,1);
604 s->state=new_state;
605 }
606 }
607 skip=0;
608 }
609end:
610 s->in_handshake--;
611 if (buf != NULL)
612 BUF_MEM_free(buf);
613 if (cb != NULL)
614 cb(s,SSL_CB_CONNECT_EXIT,ret);
615 return(ret);
616 }
617
618
David Benjamin8da99062014-08-24 12:03:09 -0400619int ssl3_send_client_hello(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700620 {
621 unsigned char *buf;
622 unsigned char *p,*d;
623 int i;
624 unsigned long l;
625
626 buf=(unsigned char *)s->init_buf->data;
627 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
628 {
629 SSL_SESSION *sess = s->session;
David Benjamin407886f2014-07-21 22:23:50 -0400630 if (sess == NULL ||
631 sess->ssl_version != s->version ||
632 !sess->session_id_length ||
633 sess->not_resumable)
Adam Langley95c29f32014-06-20 12:00:00 -0700634 {
635 if (!ssl_get_new_session(s,0))
636 goto err;
637 }
638 if (s->method->version == DTLS_ANY_VERSION)
639 {
640 /* Determine which DTLS version to use */
641 int options = s->options;
642 /* If DTLS 1.2 disabled correct the version number */
643 if (options & SSL_OP_NO_DTLSv1_2)
644 {
Adam Langley95c29f32014-06-20 12:00:00 -0700645 /* Disabling all versions is silly: return an
646 * error.
647 */
648 if (options & SSL_OP_NO_DTLSv1)
649 {
David Benjamin8da99062014-08-24 12:03:09 -0400650 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
Adam Langley95c29f32014-06-20 12:00:00 -0700651 goto err;
652 }
653 /* Update method so we don't use any DTLS 1.2
654 * features.
655 */
656 s->method = DTLSv1_client_method();
657 s->version = DTLS1_VERSION;
658 }
659 else
660 {
661 /* We only support one version: update method */
662 if (options & SSL_OP_NO_DTLSv1)
663 s->method = DTLSv1_2_client_method();
664 s->version = DTLS1_2_VERSION;
665 }
666 s->client_version = s->version;
667 }
668 /* else use the pre-loaded session */
669
670 p=s->s3->client_random;
671
David Benjaminf2fedef2014-08-16 01:37:34 -0400672 /* If resending the ClientHello in DTLS after a
673 * HelloVerifyRequest, don't renegerate the client_random. The
674 * random must be reused. */
675 if (!SSL_IS_DTLS(s) || !s->d1->send_cookie)
Adam Langley95c29f32014-06-20 12:00:00 -0700676 {
Adam Langley95c29f32014-06-20 12:00:00 -0700677 ssl_fill_hello_random(s, 0, p,
678 sizeof(s->s3->client_random));
David Benjaminf2fedef2014-08-16 01:37:34 -0400679 }
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langleyb0c235e2014-06-20 12:00:00 -0700681 /* Do the message type and length last.
682 * Note: the final argument to ssl_add_clienthello_tlsext below
683 * depends on the size of this prefix. */
Adam Langley95c29f32014-06-20 12:00:00 -0700684 d=p= ssl_handshake_start(s);
685
686 /* version indicates the negotiated version: for example from
687 * an SSLv2/v3 compatible client hello). The client_version
688 * field is the maximum version we permit and it is also
689 * used in RSA encrypted premaster secrets. Some servers can
690 * choke if we initially report a higher version then
691 * renegotiate to a lower one in the premaster secret. This
692 * didn't happen with TLS 1.0 as most servers supported it
693 * but it can with TLS 1.1 or later if the server only supports
694 * 1.0.
695 *
696 * Possible scenario with previous logic:
697 * 1. Client hello indicates TLS 1.2
698 * 2. Server hello says TLS 1.0
699 * 3. RSA encrypted premaster secret uses 1.2.
700 * 4. Handhaked proceeds using TLS 1.0.
701 * 5. Server sends hello request to renegotiate.
702 * 6. Client hello indicates TLS v1.0 as we now
703 * know that is maximum server supports.
704 * 7. Server chokes on RSA encrypted premaster secret
705 * containing version 1.0.
706 *
707 * For interoperability it should be OK to always use the
708 * maximum version we support in client hello and then rely
709 * on the checking of version to ensure the servers isn't
710 * being inconsistent: for example initially negotiating with
711 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
712 * client_version in client hello and not resetting it to
713 * the negotiated version.
714 */
715#if 0
716 *(p++)=s->version>>8;
717 *(p++)=s->version&0xff;
718 s->client_version=s->version;
719#else
720 *(p++)=s->client_version>>8;
721 *(p++)=s->client_version&0xff;
722#endif
723
724 /* Random stuff */
725 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
726 p+=SSL3_RANDOM_SIZE;
727
728 /* Session ID */
729 if (s->new_session)
730 i=0;
731 else
732 i=s->session->session_id_length;
733 *(p++)=i;
734 if (i != 0)
735 {
736 if (i > (int)sizeof(s->session->session_id))
737 {
David Benjamin8da99062014-08-24 12:03:09 -0400738 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700739 goto err;
740 }
741 memcpy(p,s->session->session_id,i);
742 p+=i;
743 }
744
745 /* cookie stuff for DTLS */
746 if (SSL_IS_DTLS(s))
747 {
748 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
749 {
David Benjamin8da99062014-08-24 12:03:09 -0400750 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700751 goto err;
752 }
753 *(p++) = s->d1->cookie_len;
754 memcpy(p, s->d1->cookie, s->d1->cookie_len);
755 p += s->d1->cookie_len;
756 }
757
758 /* Ciphers supported */
David Benjamin39482a12014-07-20 13:30:15 -0400759 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
Adam Langley95c29f32014-06-20 12:00:00 -0700760 if (i == 0)
761 {
David Benjamin8da99062014-08-24 12:03:09 -0400762 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
Adam Langley95c29f32014-06-20 12:00:00 -0700763 goto err;
764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765 s2n(i,p);
766 p+=i;
767
768 /* COMPRESSION */
769 *(p++)=1;
770 *(p++)=0; /* Add the NULL method */
771
Adam Langley95c29f32014-06-20 12:00:00 -0700772 /* TLS extensions*/
773 if (ssl_prepare_clienthello_tlsext(s) <= 0)
774 {
David Benjamin8da99062014-08-24 12:03:09 -0400775 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -0700776 goto err;
777 }
Adam Langleyb0c235e2014-06-20 12:00:00 -0700778 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700779 {
David Benjamin8da99062014-08-24 12:03:09 -0400780 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -0700781 goto err;
782 }
Adam Langley95c29f32014-06-20 12:00:00 -0700783
784 l= p-d;
785 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
786 s->state=SSL3_ST_CW_CLNT_HELLO_B;
787 }
788
789 /* SSL3_ST_CW_CLNT_HELLO_B */
790 return ssl_do_write(s);
791err:
792 return(-1);
793 }
794
795int ssl3_get_server_hello(SSL *s)
796 {
797 STACK_OF(SSL_CIPHER) *sk;
798 const SSL_CIPHER *c;
799 CERT *ct = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -0700800 int al=SSL_AD_INTERNAL_ERROR,ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700801 long n;
David Benjamina03d95d2014-07-12 19:49:07 -0400802 CBS server_hello, server_random, session_id;
David Benjamin39482a12014-07-20 13:30:15 -0400803 uint16_t server_version, cipher_suite;
David Benjamina03d95d2014-07-12 19:49:07 -0400804 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700805
806 n=s->method->ssl_get_message(s,
807 SSL3_ST_CR_SRVR_HELLO_A,
808 SSL3_ST_CR_SRVR_HELLO_B,
David Benjaminf2fedef2014-08-16 01:37:34 -0400809 SSL3_MT_SERVER_HELLO,
Adam Langley95c29f32014-06-20 12:00:00 -0700810 20000, /* ?? */
David Benjamin590cbe92014-08-25 21:34:56 -0400811 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -0700812 &ok);
813
814 if (!ok) return((int)n);
815
David Benjamina03d95d2014-07-12 19:49:07 -0400816 CBS_init(&server_hello, s->init_msg, n);
817
818 if (!CBS_get_u16(&server_hello, &server_version) ||
819 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
820 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
David Benjamin39482a12014-07-20 13:30:15 -0400821 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
822 !CBS_get_u16(&server_hello, &cipher_suite) ||
823 !CBS_get_u8(&server_hello, &compression_method))
David Benjamina03d95d2014-07-12 19:49:07 -0400824 {
825 al = SSL_AD_DECODE_ERROR;
826 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
827 goto f_err;
828 }
829
Adam Langley95c29f32014-06-20 12:00:00 -0700830 if (s->method->version == DTLS_ANY_VERSION)
831 {
832 /* Work out correct protocol version to use */
Adam Langley95c29f32014-06-20 12:00:00 -0700833 int options = s->options;
David Benjamina03d95d2014-07-12 19:49:07 -0400834 if (server_version == DTLS1_2_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700835 && !(options & SSL_OP_NO_DTLSv1_2))
836 s->method = DTLSv1_2_client_method();
David Benjamina03d95d2014-07-12 19:49:07 -0400837 else if (server_version == DTLS1_VERSION
Adam Langley95c29f32014-06-20 12:00:00 -0700838 && !(options & SSL_OP_NO_DTLSv1))
839 s->method = DTLSv1_client_method();
840 else
841 {
842 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400843 s->version = server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700844 al = SSL_AD_PROTOCOL_VERSION;
845 goto f_err;
846 }
847 s->version = s->client_version = s->method->version;
848 }
849
David Benjamina03d95d2014-07-12 19:49:07 -0400850 if (server_version != s->version)
Adam Langley95c29f32014-06-20 12:00:00 -0700851 {
852 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
David Benjamina03d95d2014-07-12 19:49:07 -0400853 s->version = (s->version & 0xff00) | (server_version & 0xff);
854 al = SSL_AD_PROTOCOL_VERSION;
Adam Langley95c29f32014-06-20 12:00:00 -0700855 goto f_err;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
David Benjamina03d95d2014-07-12 19:49:07 -0400858 /* Copy over the server random. */
859 memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700860
David Benjamin9eaeef82014-07-21 22:22:02 -0400861 s->hit = 0;
862
Adam Langley95c29f32014-06-20 12:00:00 -0700863 /* check if we want to resume the session based on external pre-shared secret */
864 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
865 {
David Benjamin6f260012014-08-15 13:49:12 -0400866 const SSL_CIPHER *pref_cipher=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700867 s->session->master_key_length=sizeof(s->session->master_key);
868 if (s->tls_session_secret_cb(s, s->session->master_key,
869 &s->session->master_key_length,
870 NULL, &pref_cipher,
871 s->tls_session_secret_cb_arg))
872 {
873 s->session->cipher = pref_cipher ?
David Benjamina03d95d2014-07-12 19:49:07 -0400874 pref_cipher :
David Benjamin39482a12014-07-20 13:30:15 -0400875 ssl3_get_cipher_by_value(cipher_suite);
David Benjamin9eaeef82014-07-21 22:22:02 -0400876 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700877 }
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
David Benjamin9eaeef82014-07-21 22:22:02 -0400880 if (!s->hit && CBS_len(&session_id) != 0 &&
David Benjamin22f9bcc2014-07-13 12:29:21 -0400881 CBS_mem_equal(&session_id,
882 s->session->session_id, s->session->session_id_length))
Adam Langley95c29f32014-06-20 12:00:00 -0700883 {
David Benjamina03d95d2014-07-12 19:49:07 -0400884 if(s->sid_ctx_length != s->session->sid_ctx_length
885 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
886 {
887 /* actually a client application bug */
888 al = SSL_AD_ILLEGAL_PARAMETER;
889 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
890 goto f_err;
891 }
David Benjamina03d95d2014-07-12 19:49:07 -0400892 s->hit = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700893 }
David Benjamin9eaeef82014-07-21 22:22:02 -0400894
895 /* a miss or crap from the other end */
896 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -0700897 {
898 /* If we were trying for session-id reuse, make a new
899 * SSL_SESSION so we don't stuff up other people */
Adam Langley95c29f32014-06-20 12:00:00 -0700900 if (s->session->session_id_length > 0)
901 {
902 if (!ssl_get_new_session(s,0))
903 {
904 goto f_err;
905 }
906 }
David Benjamina03d95d2014-07-12 19:49:07 -0400907 /* Note: session_id could be empty. */
908 s->session->session_id_length = CBS_len(&session_id);
909 memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
Adam Langley95c29f32014-06-20 12:00:00 -0700910 }
David Benjamina03d95d2014-07-12 19:49:07 -0400911
David Benjamin39482a12014-07-20 13:30:15 -0400912 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -0700913 if (c == NULL)
914 {
915 /* unknown cipher */
David Benjamina03d95d2014-07-12 19:49:07 -0400916 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700917 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
918 goto f_err;
919 }
920 /* If it is a disabled cipher we didn't send it in client hello,
921 * so return an error.
922 */
923 if (c->algorithm_ssl & ct->mask_ssl ||
924 c->algorithm_mkey & ct->mask_k ||
925 c->algorithm_auth & ct->mask_a)
926 {
927 al=SSL_AD_ILLEGAL_PARAMETER;
928 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
929 goto f_err;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
932 sk=ssl_get_ciphers_by_id(s);
933 if (!sk_SSL_CIPHER_find(sk, NULL, c))
934 {
935 /* we did not say we would use this cipher */
936 al=SSL_AD_ILLEGAL_PARAMETER;
937 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
938 goto f_err;
939 }
940
941 /* Depending on the session caching (internal/external), the cipher
942 and/or cipher_id values may not be set. Make sure that
943 cipher_id is set and use it for comparison. */
944 if (s->session->cipher)
945 s->session->cipher_id = s->session->cipher->id;
946 if (s->hit && (s->session->cipher_id != c->id))
947 {
David Benjamina03d95d2014-07-12 19:49:07 -0400948 al = SSL_AD_ILLEGAL_PARAMETER;
949 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
950 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700951 }
952 s->s3->tmp.new_cipher=c;
953 /* Don't digest cached records if no sigalgs: we may need them for
954 * client authentication.
955 */
956 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
957 goto f_err;
David Benjamina03d95d2014-07-12 19:49:07 -0400958
David Benjamina03d95d2014-07-12 19:49:07 -0400959 /* Only the NULL compression algorithm is supported. */
960 if (compression_method != 0)
961 {
962 al = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700963 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
964 goto f_err;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
David Benjamina03d95d2014-07-12 19:49:07 -0400967 /* TLS extensions */
968 if (!ssl_parse_serverhello_tlsext(s, &server_hello))
Adam Langley95c29f32014-06-20 12:00:00 -0700969 {
970 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
971 goto err;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
David Benjamin03973092014-06-24 23:27:17 -0400974 /* There should be nothing left over in the record. */
David Benjamina03d95d2014-07-12 19:49:07 -0400975 if (CBS_len(&server_hello) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700976 {
977 /* wrong packet length */
978 al=SSL_AD_DECODE_ERROR;
979 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
980 goto f_err;
981 }
982
983 return(1);
984f_err:
985 ssl3_send_alert(s,SSL3_AL_FATAL,al);
986err:
987 return(-1);
988 }
989
990int ssl3_get_server_certificate(SSL *s)
991 {
992 int al,i,ok,ret= -1;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400993 unsigned long n;
Adam Langley95c29f32014-06-20 12:00:00 -0700994 X509 *x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 STACK_OF(X509) *sk=NULL;
996 SESS_CERT *sc;
997 EVP_PKEY *pkey=NULL;
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400998 CBS cbs, certificate_list;
999 const uint8_t* data;
Adam Langley95c29f32014-06-20 12:00:00 -07001000
1001 n=s->method->ssl_get_message(s,
1002 SSL3_ST_CR_CERT_A,
1003 SSL3_ST_CR_CERT_B,
David Benjamine8f3d662014-07-12 01:10:19 -04001004 SSL3_MT_CERTIFICATE,
Adam Langley95c29f32014-06-20 12:00:00 -07001005 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001006 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001007 &ok);
1008
1009 if (!ok) return((int)n);
1010
David Benjamin51b1f742014-07-12 16:31:12 -04001011 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001012
1013 if ((sk=sk_X509_new_null()) == NULL)
1014 {
1015 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1016 goto err;
1017 }
1018
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001019 if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
1020 CBS_len(&cbs) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001021 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001022 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001023 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
1024 goto f_err;
1025 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001026
1027 while (CBS_len(&certificate_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001028 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001029 CBS certificate;
1030 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
Adam Langley95c29f32014-06-20 12:00:00 -07001031 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001032 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001033 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1034 goto f_err;
1035 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001036 data = CBS_data(&certificate);
1037 x = d2i_X509(NULL, &data, CBS_len(&certificate));
Adam Langley95c29f32014-06-20 12:00:00 -07001038 if (x == NULL)
1039 {
1040 al=SSL_AD_BAD_CERTIFICATE;
1041 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
1042 goto f_err;
1043 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001044 if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
Adam Langley95c29f32014-06-20 12:00:00 -07001045 {
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001046 al = SSL_AD_INTERNAL_ERROR;
1047 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1048 goto f_err;
1049 }
1050 if (CBS_len(&certificate) != 0)
1051 {
1052 al = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001053 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
1054 goto f_err;
1055 }
1056 if (!sk_X509_push(sk,x))
1057 {
1058 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
1059 goto err;
1060 }
1061 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001062 }
1063
1064 i=ssl_verify_cert_chain(s,sk);
1065 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1066 )
1067 {
1068 al=ssl_verify_alarm_type(s->verify_result);
1069 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
1070 goto f_err;
1071 }
1072 ERR_clear_error(); /* but we keep s->verify_result */
1073
1074 sc=ssl_sess_cert_new();
1075 if (sc == NULL) goto err;
1076
1077 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1078 s->session->sess_cert=sc;
1079
1080 sc->cert_chain=sk;
1081 /* Inconsistency alert: cert_chain does include the peer's
1082 * certificate, which we don't include in s3_srvr.c */
1083 x=sk_X509_value(sk,0);
1084 sk=NULL;
1085 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1086
1087 pkey=X509_get_pubkey(x);
1088
David Benjamind26aea62014-07-12 00:13:56 -04001089 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
Adam Langley95c29f32014-06-20 12:00:00 -07001090 {
1091 x=NULL;
1092 al=SSL3_AL_FATAL;
1093 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1094 goto f_err;
1095 }
1096
1097 i=ssl_cert_type(x,pkey);
David Benjamind26aea62014-07-12 00:13:56 -04001098 if (i < 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001099 {
1100 x=NULL;
1101 al=SSL3_AL_FATAL;
1102 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1103 goto f_err;
1104 }
1105
David Benjamind26aea62014-07-12 00:13:56 -04001106 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1107 if (exp_idx >= 0 && i != exp_idx)
Adam Langley95c29f32014-06-20 12:00:00 -07001108 {
David Benjamind26aea62014-07-12 00:13:56 -04001109 x=NULL;
1110 al=SSL_AD_ILLEGAL_PARAMETER;
1111 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
1112 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001113 }
David Benjamind26aea62014-07-12 00:13:56 -04001114 sc->peer_cert_type=i;
David Benjamind26aea62014-07-12 00:13:56 -04001115 /* Why would the following ever happen?
1116 * We just created sc a couple of lines ago. */
1117 if (sc->peer_pkeys[i].x509 != NULL)
1118 X509_free(sc->peer_pkeys[i].x509);
David Benjamin150c6172014-08-05 22:22:49 -04001119 sc->peer_pkeys[i].x509 = X509_up_ref(x);
1120 sc->peer_key = &(sc->peer_pkeys[i]);
Adam Langley95c29f32014-06-20 12:00:00 -07001121
David Benjamind26aea62014-07-12 00:13:56 -04001122 if (s->session->peer != NULL)
1123 X509_free(s->session->peer);
David Benjamin150c6172014-08-05 22:22:49 -04001124 s->session->peer = X509_up_ref(x);
David Benjamind26aea62014-07-12 00:13:56 -04001125
Adam Langley95c29f32014-06-20 12:00:00 -07001126 s->session->verify_result = s->verify_result;
1127
1128 x=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001129 ret=1;
1130 if (0)
1131 {
1132f_err:
1133 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1134 }
1135err:
1136 EVP_PKEY_free(pkey);
1137 X509_free(x);
1138 sk_X509_pop_free(sk,X509_free);
1139 return(ret);
1140 }
1141
David Benjamin8f8040d2014-07-14 19:14:46 -04001142int ssl3_get_server_key_exchange(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07001143 {
Adam Langley95c29f32014-06-20 12:00:00 -07001144 EVP_MD_CTX md_ctx;
David Benjamined439582014-07-14 19:13:02 -04001145 int al,ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001146 long n,alg_k,alg_a;
1147 EVP_PKEY *pkey=NULL;
1148 const EVP_MD *md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001149 RSA *rsa=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001150#ifndef OPENSSL_NO_DH
1151 DH *dh=NULL;
1152#endif
1153#ifndef OPENSSL_NO_ECDH
1154 EC_KEY *ecdh = NULL;
1155 BN_CTX *bn_ctx = NULL;
1156 EC_POINT *srvr_ecpoint = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001157#endif
David Benjamined439582014-07-14 19:13:02 -04001158 CBS server_key_exchange, server_key_exchange_orig, parameter;
Adam Langley95c29f32014-06-20 12:00:00 -07001159
1160 /* use same message size as in ssl3_get_certificate_request()
1161 * as ServerKeyExchange message may be skipped */
1162 n=s->method->ssl_get_message(s,
1163 SSL3_ST_CR_KEY_EXCH_A,
1164 SSL3_ST_CR_KEY_EXCH_B,
1165 -1,
1166 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001167 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001168 &ok);
1169 if (!ok) return((int)n);
1170
1171 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1172 {
David Benjamin9c651c92014-07-12 13:27:45 -04001173 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
1174 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001175 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin9c651c92014-07-12 13:27:45 -04001176 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1177 return -1;
1178 }
1179
Adam Langley95c29f32014-06-20 12:00:00 -07001180 /* In plain PSK ciphersuite, ServerKeyExchange can be
1181 omitted if no identity hint is sent. Set
1182 session->sess_cert anyway to avoid problems
1183 later.*/
David Benjamin5235f742014-07-12 13:11:24 -04001184 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001185 {
David Benjamin0c49ec92014-07-12 13:16:51 -04001186 /* PSK ciphersuites that also send a
1187 * Certificate would have already initialized
1188 * |sess_cert|. */
1189 if (s->session->sess_cert == NULL)
1190 s->session->sess_cert = ssl_sess_cert_new();
Adam Langley0289c732014-06-20 12:00:00 -07001191 if (s->session->psk_identity_hint)
1192 {
1193 OPENSSL_free(s->session->psk_identity_hint);
1194 s->session->psk_identity_hint = NULL;
1195 }
Adam Langley95c29f32014-06-20 12:00:00 -07001196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197 s->s3->tmp.reuse_message=1;
1198 return(1);
1199 }
1200
David Benjamined439582014-07-14 19:13:02 -04001201 /* Retain a copy of the original CBS to compute the signature
1202 * over. */
1203 CBS_init(&server_key_exchange, s->init_msg, n);
1204 server_key_exchange_orig = server_key_exchange;
1205
Adam Langley95c29f32014-06-20 12:00:00 -07001206 if (s->session->sess_cert != NULL)
1207 {
Adam Langley95c29f32014-06-20 12:00:00 -07001208 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1209 {
1210 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1211 s->session->sess_cert->peer_rsa_tmp=NULL;
1212 }
Adam Langley95c29f32014-06-20 12:00:00 -07001213#ifndef OPENSSL_NO_DH
1214 if (s->session->sess_cert->peer_dh_tmp)
1215 {
1216 DH_free(s->session->sess_cert->peer_dh_tmp);
1217 s->session->sess_cert->peer_dh_tmp=NULL;
1218 }
1219#endif
1220#ifndef OPENSSL_NO_ECDH
1221 if (s->session->sess_cert->peer_ecdh_tmp)
1222 {
1223 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1224 s->session->sess_cert->peer_ecdh_tmp=NULL;
1225 }
1226#endif
1227 }
1228 else
1229 {
1230 s->session->sess_cert=ssl_sess_cert_new();
1231 }
1232
Adam Langley95c29f32014-06-20 12:00:00 -07001233 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1234 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1235 EVP_MD_CTX_init(&md_ctx);
1236
Adam Langleyc26c8022014-06-20 12:00:00 -07001237 if (alg_a & SSL_aPSK)
Adam Langley95c29f32014-06-20 12:00:00 -07001238 {
David Benjamined439582014-07-14 19:13:02 -04001239 CBS psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07001240
David Benjamined439582014-07-14 19:13:02 -04001241 /* Each of the PSK key exchanges begins with a
1242 * psk_identity_hint. */
1243 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
Adam Langley0289c732014-06-20 12:00:00 -07001244 {
David Benjamined439582014-07-14 19:13:02 -04001245 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001246 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001247 goto f_err;
Adam Langleyc26c8022014-06-20 12:00:00 -07001248 }
Adam Langley95c29f32014-06-20 12:00:00 -07001249
David Benjamined439582014-07-14 19:13:02 -04001250 /* Store PSK identity hint for later use, hint is used in
1251 * ssl3_send_client_key_exchange. Assume that the maximum
1252 * length of a PSK identity hint can be as long as the maximum
1253 * length of a PSK identity. Also do not allow NULL
1254 * characters; identities are saved as C strings.
1255 *
1256 * TODO(davidben): Should invalid hints be ignored? It's a hint
1257 * rather than a specific identity. */
1258 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1259 CBS_contains_zero_byte(&psk_identity_hint))
1260 {
1261 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001262 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamined439582014-07-14 19:13:02 -04001263 goto f_err;
1264 }
1265
1266 /* Save the identity hint as a C string. */
1267 if (!CBS_strdup(&psk_identity_hint, &s->session->psk_identity_hint))
1268 {
1269 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001270 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001271 goto f_err;
1272 }
Adam Langley95c29f32014-06-20 12:00:00 -07001273 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001274
1275 if (0) {}
Adam Langleyc26c8022014-06-20 12:00:00 -07001276 else if (alg_k & SSL_kRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001277 {
David Benjamined439582014-07-14 19:13:02 -04001278 CBS rsa_modulus, rsa_exponent;
1279
1280 /* TODO(davidben): This was originally for export
1281 * reasons. Do we still need to support it? */
1282
1283 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_modulus) ||
1284 CBS_len(&rsa_modulus) == 0 ||
1285 !CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_exponent) ||
1286 CBS_len(&rsa_exponent) == 0)
1287 {
1288 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001289 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001290 goto f_err;
1291 }
1292
Adam Langley95c29f32014-06-20 12:00:00 -07001293 if ((rsa=RSA_new()) == NULL)
1294 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001295 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001296 goto err;
1297 }
Adam Langley95c29f32014-06-20 12:00:00 -07001298
David Benjamined439582014-07-14 19:13:02 -04001299 if (!(rsa->n = BN_bin2bn(CBS_data(&rsa_modulus),
1300 CBS_len(&rsa_modulus), rsa->n)))
Adam Langley95c29f32014-06-20 12:00:00 -07001301 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001302 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001303 goto err;
1304 }
David Benjamined439582014-07-14 19:13:02 -04001305
1306 if (!(rsa->e = BN_bin2bn(CBS_data(&rsa_exponent),
1307 CBS_len(&rsa_exponent), rsa->e)))
1308 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001309 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001310 goto err;
1311 }
Adam Langley95c29f32014-06-20 12:00:00 -07001312
1313 /* this should be because we are using an export cipher */
1314 if (alg_a & SSL_aRSA)
1315 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1316 else
1317 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001318 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001319 goto err;
1320 }
1321 s->session->sess_cert->peer_rsa_tmp=rsa;
1322 rsa=NULL;
1323 }
Adam Langley95c29f32014-06-20 12:00:00 -07001324#ifndef OPENSSL_NO_DH
1325 else if (alg_k & SSL_kEDH)
1326 {
David Benjamined439582014-07-14 19:13:02 -04001327 CBS dh_p, dh_g, dh_Ys;
1328
1329 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1330 CBS_len(&dh_p) == 0 ||
1331 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1332 CBS_len(&dh_g) == 0 ||
1333 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1334 CBS_len(&dh_Ys) == 0)
1335 {
1336 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001337 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001338 goto f_err;
1339 }
1340
Adam Langley95c29f32014-06-20 12:00:00 -07001341 if ((dh=DH_new()) == NULL)
1342 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001343 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001344 goto err;
1345 }
Adam Langley95c29f32014-06-20 12:00:00 -07001346
David Benjamined439582014-07-14 19:13:02 -04001347 if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001348 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001349 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001350 goto err;
1351 }
David Benjamined439582014-07-14 19:13:02 -04001352 if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
Adam Langley95c29f32014-06-20 12:00:00 -07001353 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001354 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001355 goto err;
1356 }
David Benjamined439582014-07-14 19:13:02 -04001357 if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
1358 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001359 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
David Benjamined439582014-07-14 19:13:02 -04001360 goto err;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
HÃ¥vard Molland16c623b2014-08-12 11:29:57 +02001363 if (DH_size(dh) < 512/8)
1364 {
1365 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_DH_P_LENGTH);
1366 goto err;
1367 }
1368
Adam Langley95c29f32014-06-20 12:00:00 -07001369 if (alg_a & SSL_aRSA)
1370 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001371 /* else anonymous DH, so no certificate or pkey. */
1372
1373 s->session->sess_cert->peer_dh_tmp=dh;
1374 dh=NULL;
1375 }
Adam Langley95c29f32014-06-20 12:00:00 -07001376#endif /* !OPENSSL_NO_DH */
1377
1378#ifndef OPENSSL_NO_ECDH
1379 else if (alg_k & SSL_kEECDH)
1380 {
David Benjamined439582014-07-14 19:13:02 -04001381 uint16_t curve_id;
1382 int curve_nid = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001383 EC_GROUP *ngroup;
1384 const EC_GROUP *group;
David Benjamined439582014-07-14 19:13:02 -04001385 CBS point;
Adam Langley95c29f32014-06-20 12:00:00 -07001386
David Benjamined439582014-07-14 19:13:02 -04001387 /* Extract elliptic curve parameters and the server's
1388 * ephemeral ECDH public key. Check curve is one of
1389 * our preferences, if not server has sent an invalid
1390 * curve.
1391 */
1392 if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -07001393 {
David Benjamined439582014-07-14 19:13:02 -04001394 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001395 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
Adam Langley95c29f32014-06-20 12:00:00 -07001396 goto f_err;
1397 }
1398
David Benjamined439582014-07-14 19:13:02 -04001399 if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001400 {
1401 al=SSL_AD_INTERNAL_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001402 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
Adam Langley95c29f32014-06-20 12:00:00 -07001403 goto f_err;
1404 }
1405
David Benjamined439582014-07-14 19:13:02 -04001406 if ((ecdh=EC_KEY_new()) == NULL)
1407 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001408 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
David Benjamined439582014-07-14 19:13:02 -04001409 goto err;
1410 }
Adam Langley95c29f32014-06-20 12:00:00 -07001411 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1412 if (ngroup == NULL)
1413 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001414 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001415 goto err;
1416 }
1417 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1418 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001419 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07001420 goto err;
1421 }
1422 EC_GROUP_free(ngroup);
1423
1424 group = EC_KEY_get0_group(ecdh);
1425
Adam Langley95c29f32014-06-20 12:00:00 -07001426 /* Next, get the encoded ECPoint */
David Benjamined439582014-07-14 19:13:02 -04001427 if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
1428 {
1429 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001430 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
David Benjamined439582014-07-14 19:13:02 -04001431 goto f_err;
1432 }
1433
Adam Langley95c29f32014-06-20 12:00:00 -07001434 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1435 ((bn_ctx = BN_CTX_new()) == NULL))
1436 {
David Benjamin8f8040d2014-07-14 19:14:46 -04001437 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001438 goto err;
1439 }
1440
David Benjamined439582014-07-14 19:13:02 -04001441 if (!EC_POINT_oct2point(group, srvr_ecpoint,
1442 CBS_data(&point), CBS_len(&point), bn_ctx))
Adam Langley95c29f32014-06-20 12:00:00 -07001443 {
David Benjamined439582014-07-14 19:13:02 -04001444 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001445 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
Adam Langley95c29f32014-06-20 12:00:00 -07001446 goto f_err;
1447 }
1448
Adam Langley95c29f32014-06-20 12:00:00 -07001449 /* The ECC/TLS specification does not mention
1450 * the use of DSA to sign ECParameters in the server
1451 * key exchange message. We do support RSA and ECDSA.
1452 */
1453 if (0) ;
Adam Langley95c29f32014-06-20 12:00:00 -07001454 else if (alg_a & SSL_aRSA)
1455 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
Adam Langley95c29f32014-06-20 12:00:00 -07001456#ifndef OPENSSL_NO_ECDSA
1457 else if (alg_a & SSL_aECDSA)
1458 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1459#endif
1460 /* else anonymous ECDH, so no certificate or pkey. */
1461 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1462 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1463 ecdh=NULL;
1464 BN_CTX_free(bn_ctx);
1465 bn_ctx = NULL;
1466 EC_POINT_free(srvr_ecpoint);
1467 srvr_ecpoint = NULL;
1468 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001469#endif /* !OPENSSL_NO_ECDH */
1470
1471 else if (!(alg_k & SSL_kPSK))
Adam Langley95c29f32014-06-20 12:00:00 -07001472 {
1473 al=SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin8f8040d2014-07-14 19:14:46 -04001474 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001475 goto f_err;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
David Benjamined439582014-07-14 19:13:02 -04001478 /* At this point, |server_key_exchange| contains the
1479 * signature, if any, while |server_key_exchange_orig|
1480 * contains the entire message. From that, derive a CBS
1481 * containing just the parameter. */
1482 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1483 CBS_len(&server_key_exchange_orig) -
1484 CBS_len(&server_key_exchange));
Adam Langley95c29f32014-06-20 12:00:00 -07001485
1486 /* if it was signed, check the signature */
1487 if (pkey != NULL)
1488 {
David Benjamined439582014-07-14 19:13:02 -04001489 CBS signature;
1490
Adam Langley95c29f32014-06-20 12:00:00 -07001491 if (SSL_USE_SIGALGS(s))
1492 {
David Benjamin05da6e12014-07-12 20:42:55 -04001493 if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
David Benjamined439582014-07-14 19:13:02 -04001494 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -07001495 }
1496 else
1497 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07001498
David Benjamined439582014-07-14 19:13:02 -04001499 /* The last field in |server_key_exchange| is the
1500 * signature. */
1501 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1502 CBS_len(&server_key_exchange) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001503 {
David Benjamined439582014-07-14 19:13:02 -04001504 al = SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001505 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001506 goto f_err;
1507 }
1508
Adam Langley95c29f32014-06-20 12:00:00 -07001509 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1510 {
1511 int num;
David Benjamined439582014-07-14 19:13:02 -04001512 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1513 size_t md_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001514
Adam Langley95c29f32014-06-20 12:00:00 -07001515 q=md_buf;
1516 for (num=2; num > 0; num--)
1517 {
David Benjamined439582014-07-14 19:13:02 -04001518 unsigned int digest_len;
David Benjamine7127782014-08-18 23:15:29 -04001519 EVP_DigestInit_ex(&md_ctx,
1520 (num == 2) ? EVP_md5() : EVP_sha1(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001521 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1522 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001523 EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1524 EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
1525 q += digest_len;
1526 md_len += digest_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001527 }
David Benjamined439582014-07-14 19:13:02 -04001528 if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
1529 CBS_data(&signature), CBS_len(&signature),
1530 pkey->pkey.rsa))
Adam Langley95c29f32014-06-20 12:00:00 -07001531 {
David Benjamined439582014-07-14 19:13:02 -04001532 al = SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001533 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001534 goto f_err;
1535 }
1536 }
1537 else
Adam Langley95c29f32014-06-20 12:00:00 -07001538 {
1539 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1540 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1541 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
David Benjamined439582014-07-14 19:13:02 -04001542 EVP_VerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
1543 if (EVP_VerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature), pkey) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {
1545 /* bad signature */
1546 al=SSL_AD_DECRYPT_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001547 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001548 goto f_err;
1549 }
1550 }
1551 }
1552 else
1553 {
David Benjamine8f3d662014-07-12 01:10:19 -04001554 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
Adam Langley95c29f32014-06-20 12:00:00 -07001555 {
1556 /* Might be wrong key type, check it */
1557 if (ssl3_check_cert_and_algorithm(s))
1558 /* Otherwise this shouldn't happen */
David Benjamin8f8040d2014-07-14 19:14:46 -04001559 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001560 goto err;
1561 }
1562 /* still data left over */
David Benjamined439582014-07-14 19:13:02 -04001563 if (CBS_len(&server_key_exchange) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001564 {
1565 al=SSL_AD_DECODE_ERROR;
David Benjamin8f8040d2014-07-14 19:14:46 -04001566 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley95c29f32014-06-20 12:00:00 -07001567 goto f_err;
1568 }
1569 }
1570 EVP_PKEY_free(pkey);
1571 EVP_MD_CTX_cleanup(&md_ctx);
1572 return(1);
1573f_err:
1574 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1575err:
1576 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001577 if (rsa != NULL)
1578 RSA_free(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -07001579#ifndef OPENSSL_NO_DH
1580 if (dh != NULL)
1581 DH_free(dh);
1582#endif
1583#ifndef OPENSSL_NO_ECDH
1584 BN_CTX_free(bn_ctx);
1585 EC_POINT_free(srvr_ecpoint);
1586 if (ecdh != NULL)
1587 EC_KEY_free(ecdh);
1588#endif
1589 EVP_MD_CTX_cleanup(&md_ctx);
1590 return(-1);
1591 }
1592
1593static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
1594 {
1595 return(X509_NAME_cmp(*a,*b));
1596 }
1597
1598int ssl3_get_certificate_request(SSL *s)
1599 {
1600 int ok,ret=0;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001601 unsigned long n;
1602 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001603 X509_NAME *xn=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001604 STACK_OF(X509_NAME) *ca_sk=NULL;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001605 CBS cbs;
1606 CBS certificate_types;
1607 CBS certificate_authorities;
1608 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
1610 n=s->method->ssl_get_message(s,
1611 SSL3_ST_CR_CERT_REQ_A,
1612 SSL3_ST_CR_CERT_REQ_B,
1613 -1,
1614 s->max_cert_list,
David Benjamin590cbe92014-08-25 21:34:56 -04001615 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001616 &ok);
1617
1618 if (!ok) return((int)n);
1619
1620 s->s3->tmp.cert_req=0;
1621
1622 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1623 {
1624 s->s3->tmp.reuse_message=1;
1625 /* If we get here we don't need any cached handshake records
1626 * as we wont be doing client auth.
1627 */
1628 if (s->s3->handshake_buffer)
1629 {
1630 if (!ssl3_digest_cached_records(s))
1631 goto err;
1632 }
1633 return(1);
1634 }
1635
1636 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1637 {
1638 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1639 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
1640 goto err;
1641 }
1642
1643 /* TLS does not like anon-DH with client cert */
1644 if (s->version > SSL3_VERSION)
1645 {
1646 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1647 {
1648 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1649 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1650 goto err;
1651 }
1652 }
1653
David Benjamin51b1f742014-07-12 16:31:12 -04001654 CBS_init(&cbs, s->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07001655
1656 ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1657 if (ca_sk == NULL)
1658 {
1659 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1660 goto err;
1661 }
1662
1663 /* get the certificate types */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001664 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001665 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001666 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1667 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
1668 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001669 }
David Benjamin676d1e72014-07-08 14:34:10 -04001670 if (!CBS_stow(&certificate_types,
1671 &s->s3->tmp.certificate_types,
1672 &s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07001673 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001674 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1675 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001676 }
Adam Langley95c29f32014-06-20 12:00:00 -07001677 if (SSL_USE_SIGALGS(s))
1678 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001679 CBS supported_signature_algorithms;
1680 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001681 {
1682 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001683 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001684 goto err;
1685 }
1686 /* Clear certificate digests and validity flags */
1687 for (i = 0; i < SSL_PKEY_NUM; i++)
1688 {
1689 s->cert->pkeys[i].digest = NULL;
1690 s->cert->pkeys[i].valid_flags = 0;
1691 }
David Benjamincd996942014-07-20 16:23:51 -04001692 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
Adam Langley95c29f32014-06-20 12:00:00 -07001693 {
1694 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1695 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1696 goto err;
1697 }
Adam Langley95c29f32014-06-20 12:00:00 -07001698 }
1699
1700 /* get the CA RDNs */
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001701 if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
Adam Langley95c29f32014-06-20 12:00:00 -07001702 {
1703 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1704 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
1705 goto err;
1706 }
1707
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001708 while (CBS_len(&certificate_authorities) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001709 {
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001710 CBS distinguished_name;
1711 if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001712 {
Adam Langley95c29f32014-06-20 12:00:00 -07001713 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1714 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
1715 goto err;
1716 }
1717
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001718 data = CBS_data(&distinguished_name);
1719 if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001720 {
Alex Chernyakhovsky9c890d42014-07-05 00:53:11 -04001721 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1722 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
1723 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001724 }
1725
David Benjamin407a10c2014-07-16 12:58:59 -04001726 if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04001727 {
1728 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1729 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
1730 goto err;
1731 }
1732 if (CBS_len(&distinguished_name) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001733 {
1734 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1735 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
1736 goto err;
1737 }
1738 if (!sk_X509_NAME_push(ca_sk,xn))
1739 {
1740 OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
1741 goto err;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743 }
1744
Adam Langley95c29f32014-06-20 12:00:00 -07001745 /* we should setup a certificate to return.... */
1746 s->s3->tmp.cert_req=1;
Adam Langley95c29f32014-06-20 12:00:00 -07001747 if (s->s3->tmp.ca_names != NULL)
1748 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1749 s->s3->tmp.ca_names=ca_sk;
1750 ca_sk=NULL;
1751
1752 ret=1;
1753err:
1754 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1755 return(ret);
1756 }
1757
Adam Langley95c29f32014-06-20 12:00:00 -07001758int ssl3_get_new_session_ticket(SSL *s)
1759 {
David Benjamine044c3d2014-07-14 19:14:12 -04001760 int ok,al,ret=0;
Adam Langley95c29f32014-06-20 12:00:00 -07001761 long n;
David Benjamine044c3d2014-07-14 19:14:12 -04001762 CBS new_session_ticket, ticket;
Adam Langley95c29f32014-06-20 12:00:00 -07001763
1764 n=s->method->ssl_get_message(s,
1765 SSL3_ST_CR_SESSION_TICKET_A,
1766 SSL3_ST_CR_SESSION_TICKET_B,
David Benjamin64442872014-07-21 17:43:45 -04001767 SSL3_MT_NEWSESSION_TICKET,
Adam Langley95c29f32014-06-20 12:00:00 -07001768 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001769 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001770 &ok);
1771
1772 if (!ok)
1773 return((int)n);
1774
David Benjamine044c3d2014-07-14 19:14:12 -04001775 CBS_init(&new_session_ticket, s->init_msg, n);
1776
1777 if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
1778 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1779 CBS_len(&new_session_ticket) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001780 {
Adam Langley95c29f32014-06-20 12:00:00 -07001781 al = SSL_AD_DECODE_ERROR;
David Benjamine044c3d2014-07-14 19:14:12 -04001782 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001783 goto f_err;
1784 }
1785
David Benjamine044c3d2014-07-14 19:14:12 -04001786 if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
Adam Langley95c29f32014-06-20 12:00:00 -07001787 {
1788 OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
1789 goto err;
1790 }
David Benjamine044c3d2014-07-14 19:14:12 -04001791
Adam Langley95c29f32014-06-20 12:00:00 -07001792 /* There are two ways to detect a resumed ticket sesion.
1793 * One is to set an appropriate session ID and then the server
1794 * must return a match in ServerHello. This allows the normal
1795 * client session ID matching to work and we know much
1796 * earlier that the ticket has been accepted.
1797 *
1798 * The other way is to set zero length session ID when the
1799 * ticket is presented and rely on the handshake to determine
1800 * session resumption.
1801 *
1802 * We choose the former approach because this fits in with
1803 * assumptions elsewhere in OpenSSL. The session ID is set
1804 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1805 * ticket.
1806 */
David Benjamine044c3d2014-07-14 19:14:12 -04001807 EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
Adam Langley95c29f32014-06-20 12:00:00 -07001808 s->session->session_id, &s->session->session_id_length,
Adam Langley95c29f32014-06-20 12:00:00 -07001809 EVP_sha256(), NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001810 ret=1;
1811 return(ret);
1812f_err:
1813 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1814err:
1815 return(-1);
1816 }
1817
1818int ssl3_get_cert_status(SSL *s)
1819 {
1820 int ok, al;
David Benjamin46062682014-07-14 19:14:32 -04001821 long n;
1822 CBS certificate_status, ocsp_response;
1823 uint8_t status_type;
1824 size_t resplen;
Adam Langley95c29f32014-06-20 12:00:00 -07001825
1826 n=s->method->ssl_get_message(s,
1827 SSL3_ST_CR_CERT_STATUS_A,
1828 SSL3_ST_CR_CERT_STATUS_B,
1829 SSL3_MT_CERTIFICATE_STATUS,
1830 16384,
David Benjamin590cbe92014-08-25 21:34:56 -04001831 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001832 &ok);
1833
1834 if (!ok) return((int)n);
David Benjamin46062682014-07-14 19:14:32 -04001835
1836 CBS_init(&certificate_status, s->init_msg, n);
1837 if (!CBS_get_u8(&certificate_status, &status_type) ||
1838 status_type != TLSEXT_STATUSTYPE_ocsp ||
1839 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1840 CBS_len(&ocsp_response) == 0 ||
1841 CBS_len(&certificate_status) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001842 {
1843 al = SSL_AD_DECODE_ERROR;
David Benjamin46062682014-07-14 19:14:32 -04001844 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07001845 goto f_err;
1846 }
David Benjamin46062682014-07-14 19:14:32 -04001847
1848 /* TODO(davidben): Make tlsext_ocsp_resplen a
1849 * size_t. Currently it uses -1 to signal no response. The
1850 * spec does not allow ocsp_response to be zero-length, so
1851 * using 0 should be fine. */
1852 if (!CBS_stow(&ocsp_response, &s->tlsext_ocsp_resp, &resplen))
Adam Langley95c29f32014-06-20 12:00:00 -07001853 {
1854 al = SSL_AD_INTERNAL_ERROR;
1855 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1856 goto f_err;
1857 }
1858 s->tlsext_ocsp_resplen = resplen;
1859 if (s->ctx->tlsext_status_cb)
1860 {
1861 int ret;
1862 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1863 if (ret == 0)
1864 {
1865 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1866 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
1867 goto f_err;
1868 }
1869 if (ret < 0)
1870 {
1871 al = SSL_AD_INTERNAL_ERROR;
1872 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
1873 goto f_err;
1874 }
1875 }
1876 return 1;
1877f_err:
1878 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1879 return(-1);
1880 }
Adam Langley95c29f32014-06-20 12:00:00 -07001881
1882int ssl3_get_server_done(SSL *s)
1883 {
1884 int ok,ret=0;
1885 long n;
1886
1887 n=s->method->ssl_get_message(s,
1888 SSL3_ST_CR_SRVR_DONE_A,
1889 SSL3_ST_CR_SRVR_DONE_B,
1890 SSL3_MT_SERVER_DONE,
1891 30, /* should be very small, like 0 :-) */
David Benjamin590cbe92014-08-25 21:34:56 -04001892 SSL_GET_MESSAGE_HASH_MESSAGE,
Adam Langley95c29f32014-06-20 12:00:00 -07001893 &ok);
1894
1895 if (!ok) return((int)n);
1896 if (n > 0)
1897 {
1898 /* should contain no data */
1899 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1900 OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
1901 return -1;
1902 }
1903 ret=1;
1904 return(ret);
1905 }
1906
1907
1908int ssl3_send_client_key_exchange(SSL *s)
1909 {
1910 unsigned char *p;
Adam Langleyc26c8022014-06-20 12:00:00 -07001911 int n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001912 unsigned long alg_k;
Adam Langleyc26c8022014-06-20 12:00:00 -07001913 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07001914 unsigned char *q;
1915 EVP_PKEY *pkey=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001916#ifndef OPENSSL_NO_ECDH
1917 EC_KEY *clnt_ecdh = NULL;
1918 const EC_POINT *srvr_ecpoint = NULL;
1919 EVP_PKEY *srvr_pub_pkey = NULL;
1920 unsigned char *encodedPoint = NULL;
1921 int encoded_pt_len = 0;
1922 BN_CTX * bn_ctx = NULL;
Adam Langleyc26c8022014-06-20 12:00:00 -07001923 unsigned int psk_len = 0;
1924 unsigned char psk[PSK_MAX_PSK_LEN];
Adam Langleyc26c8022014-06-20 12:00:00 -07001925#endif /* OPENSSL_NO_ECDH */
Adam Langley95c29f32014-06-20 12:00:00 -07001926
1927 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1928 {
1929 p = ssl_handshake_start(s);
1930
1931 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
Adam Langleyc26c8022014-06-20 12:00:00 -07001932 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1933
Adam Langleyc26c8022014-06-20 12:00:00 -07001934 if (alg_a & SSL_aPSK)
1935 {
Adam Langley01797e32014-06-20 12:00:00 -07001936 char identity[PSK_MAX_IDENTITY_LEN + 1];
1937 size_t identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001938 unsigned char *t = NULL;
1939 unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
1940 unsigned int pre_ms_len = 0;
1941 int psk_err = 1;
1942
1943 n = 0;
1944 if (s->psk_client_callback == NULL)
1945 {
1946 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
1947 goto err;
1948 }
1949
Adam Langley01797e32014-06-20 12:00:00 -07001950 memset(identity, 0, sizeof(identity));
Adam Langley0289c732014-06-20 12:00:00 -07001951 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
Adam Langley01797e32014-06-20 12:00:00 -07001952 identity, sizeof(identity), psk, sizeof(psk));
Adam Langleyc26c8022014-06-20 12:00:00 -07001953 if (psk_len > PSK_MAX_PSK_LEN)
1954 {
1955 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1956 goto psk_err;
1957 }
1958 else if (psk_len == 0)
1959 {
1960 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
1961 goto psk_err;
1962 }
Adam Langley01797e32014-06-20 12:00:00 -07001963 identity_len = OPENSSL_strnlen(identity, sizeof(identity));
1964 if (identity_len > PSK_MAX_IDENTITY_LEN)
1965 {
1966 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
1967 goto psk_err;
1968 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001969
1970 if (!(alg_k & SSL_kEECDH))
1971 {
David Benjamin14c83e72014-07-13 04:54:57 -04001972 /* Create the shared secret now if we're not using ECDHE-PSK.
1973 * TODO(davidben): Refactor this logic similarly
1974 * to ssl3_get_client_key_exchange. */
Adam Langleyc26c8022014-06-20 12:00:00 -07001975 pre_ms_len = 2+psk_len+2+psk_len;
1976 t = pre_ms;
1977 s2n(psk_len, t);
1978 memset(t, 0, psk_len);
1979 t+=psk_len;
1980 s2n(psk_len, t);
1981 memcpy(t, psk, psk_len);
1982
1983 s->session->master_key_length =
1984 s->method->ssl3_enc->generate_master_secret(s,
1985 s->session->master_key,
1986 pre_ms, pre_ms_len);
Adam Langley01797e32014-06-20 12:00:00 -07001987 s2n(identity_len, p);
1988 memcpy(p, identity, identity_len);
1989 n = 2 + identity_len;
Adam Langleyc26c8022014-06-20 12:00:00 -07001990 }
1991
Adam Langleyc26c8022014-06-20 12:00:00 -07001992 if (s->session->psk_identity != NULL)
1993 OPENSSL_free(s->session->psk_identity);
1994 s->session->psk_identity = BUF_strdup(identity);
1995 if (s->session->psk_identity == NULL)
1996 {
1997 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
1998 goto psk_err;
1999 }
2000 psk_err = 0;
2001 psk_err:
2002 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2003 OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
2004 if (psk_err != 0)
2005 {
2006 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2007 goto err;
2008 }
2009 }
Adam Langley95c29f32014-06-20 12:00:00 -07002010
2011 /* Fool emacs indentation */
2012 if (0) {}
Adam Langley95c29f32014-06-20 12:00:00 -07002013 else if (alg_k & SSL_kRSA)
2014 {
2015 RSA *rsa;
2016 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2017
Emilia Käsperc3d79602014-07-24 22:15:29 +02002018 if (s->session->sess_cert == NULL)
2019 {
2020 /* We should always have a server certificate with SSL_kRSA. */
2021 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2022 goto err;
2023 }
2024
Adam Langley95c29f32014-06-20 12:00:00 -07002025 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2026 rsa=s->session->sess_cert->peer_rsa_tmp;
2027 else
2028 {
2029 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2030 if ((pkey == NULL) ||
2031 (pkey->type != EVP_PKEY_RSA) ||
2032 (pkey->pkey.rsa == NULL))
2033 {
2034 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2035 goto err;
2036 }
2037 rsa=pkey->pkey.rsa;
2038 EVP_PKEY_free(pkey);
2039 }
2040
2041 tmp_buf[0]=s->client_version>>8;
2042 tmp_buf[1]=s->client_version&0xff;
2043 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2044 goto err;
2045
2046 s->session->master_key_length=sizeof tmp_buf;
2047
2048 q=p;
2049 /* Fix buf for TLS and beyond */
2050 if (s->version > SSL3_VERSION)
2051 p+=2;
2052 n=RSA_public_encrypt(sizeof tmp_buf,
2053 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
Adam Langley95c29f32014-06-20 12:00:00 -07002054 if (n <= 0)
2055 {
2056 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
2057 goto err;
2058 }
2059
2060 /* Fix buf for TLS and beyond */
2061 if (s->version > SSL3_VERSION)
2062 {
2063 s2n(n,q);
2064 n+=2;
2065 }
2066
2067 s->session->master_key_length=
2068 s->method->ssl3_enc->generate_master_secret(s,
2069 s->session->master_key,
2070 tmp_buf,sizeof tmp_buf);
2071 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2072 }
Adam Langley95c29f32014-06-20 12:00:00 -07002073#ifndef OPENSSL_NO_DH
David Benjamin0da0e182014-08-19 16:20:28 -04002074 else if (alg_k & SSL_kEDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002075 {
2076 DH *dh_srvr,*dh_clnt;
2077 SESS_CERT *scert = s->session->sess_cert;
2078
2079 if (scert == NULL)
2080 {
2081 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2082 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2083 goto err;
2084 }
2085
David Benjamin0da0e182014-08-19 16:20:28 -04002086 if (scert->peer_dh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002087 {
David Benjamin0da0e182014-08-19 16:20:28 -04002088 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2089 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002090 }
David Benjamin0da0e182014-08-19 16:20:28 -04002091 dh_srvr=scert->peer_dh_tmp;
2092
David Benjaminbd30f8e2014-08-19 16:02:38 -04002093 /* generate a new random key */
2094 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002095 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002096 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2097 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002098 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002099 if (!DH_generate_key(dh_clnt))
Adam Langley95c29f32014-06-20 12:00:00 -07002100 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002101 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2102 DH_free(dh_clnt);
2103 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002104 }
2105
2106 /* use the 'p' output buffer for the DH key, but
2107 * make sure to clear it out afterwards */
2108
2109 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
Adam Langley95c29f32014-06-20 12:00:00 -07002110 if (n <= 0)
2111 {
2112 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
2113 DH_free(dh_clnt);
2114 goto err;
2115 }
2116
2117 /* generate master key from the result */
2118 s->session->master_key_length=
2119 s->method->ssl3_enc->generate_master_secret(s,
2120 s->session->master_key,p,n);
2121 /* clean up */
2122 memset(p,0,n);
2123
David Benjaminbd30f8e2014-08-19 16:02:38 -04002124 /* send off the data */
2125 n=BN_num_bytes(dh_clnt->pub_key);
2126 s2n(n,p);
2127 BN_bn2bin(dh_clnt->pub_key,p);
2128 n+=2;
Adam Langley95c29f32014-06-20 12:00:00 -07002129
2130 DH_free(dh_clnt);
2131
2132 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2133 }
2134#endif
2135
Adam Langleyc26c8022014-06-20 12:00:00 -07002136#ifndef OPENSSL_NO_ECDH
David Benjamin0da0e182014-08-19 16:20:28 -04002137 else if (alg_k & SSL_kEECDH)
Adam Langley95c29f32014-06-20 12:00:00 -07002138 {
2139 const EC_GROUP *srvr_group = NULL;
2140 EC_KEY *tkey;
Adam Langley95c29f32014-06-20 12:00:00 -07002141 int field_size = 0;
Adam Langleyc26c8022014-06-20 12:00:00 -07002142 unsigned char *pre_ms;
2143 unsigned char *t;
2144 unsigned int pre_ms_len;
2145 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyd06eddd2014-06-20 12:00:00 -07002147 if (s->session->sess_cert == NULL)
2148 {
2149 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2150 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
2151 goto err;
2152 }
2153
David Benjamin0da0e182014-08-19 16:20:28 -04002154 if (s->session->sess_cert->peer_ecdh_tmp == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002155 {
David Benjamin0da0e182014-08-19 16:20:28 -04002156 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2157 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002158 }
David Benjamin0da0e182014-08-19 16:20:28 -04002159 tkey = s->session->sess_cert->peer_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002160
2161 srvr_group = EC_KEY_get0_group(tkey);
2162 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2163
2164 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2165 {
2166 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2167 goto err;
2168 }
2169
2170 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2171 {
2172 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2173 goto err;
2174 }
2175
2176 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2177 {
2178 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
2179 goto err;
2180 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04002181 /* Generate a new ECDH key pair */
2182 if (!(EC_KEY_generate_key(clnt_ecdh)))
Adam Langley95c29f32014-06-20 12:00:00 -07002183 {
David Benjaminbd30f8e2014-08-19 16:02:38 -04002184 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2185 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002186 }
2187
2188 /* use the 'p' output buffer for the ECDH key, but
2189 * make sure to clear it out afterwards
2190 */
2191
2192 field_size = EC_GROUP_get_degree(srvr_group);
2193 if (field_size <= 0)
2194 {
2195 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2196 goto err;
2197 }
2198 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2199 if (n <= 0)
2200 {
2201 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
2202 goto err;
2203 }
2204
Adam Langleyc26c8022014-06-20 12:00:00 -07002205 /* ECDHE PSK ciphersuites from RFC 5489 */
2206 if ((alg_a & SSL_aPSK) && psk_len != 0)
2207 {
2208 pre_ms_len = 2+psk_len+2+n;
2209 pre_ms = OPENSSL_malloc(pre_ms_len);
2210 if (pre_ms == NULL)
2211 {
2212 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2213 goto err;
2214 }
2215 memset(pre_ms, 0, pre_ms_len);
2216 t = pre_ms;
2217 s2n(psk_len, t);
2218 memcpy(t, psk, psk_len);
2219 t += psk_len;
2220 s2n(n, t);
2221 memcpy(t, p, n);
2222 s->session->master_key_length = s->method->ssl3_enc \
2223 -> generate_master_secret(s,
2224 s->session->master_key, pre_ms, pre_ms_len);
2225 OPENSSL_cleanse(pre_ms, pre_ms_len);
2226 OPENSSL_free(pre_ms);
2227 }
Adam Langleyc26c8022014-06-20 12:00:00 -07002228 if (!(alg_a & SSL_aPSK))
2229 {
2230 /* generate master key from the result */
2231 s->session->master_key_length = s->method->ssl3_enc \
2232 -> generate_master_secret(s,
2233 s->session->master_key, p, n);
2234 }
Adam Langley95c29f32014-06-20 12:00:00 -07002235 memset(p, 0, n); /* clean up */
2236
David Benjaminbd30f8e2014-08-19 16:02:38 -04002237 /* First check the size of encoding and
2238 * allocate memory accordingly.
2239 */
2240 encoded_pt_len =
2241 EC_POINT_point2oct(srvr_group,
Adam Langley95c29f32014-06-20 12:00:00 -07002242 EC_KEY_get0_public_key(clnt_ecdh),
2243 POINT_CONVERSION_UNCOMPRESSED,
2244 NULL, 0, NULL);
2245
David Benjaminbd30f8e2014-08-19 16:02:38 -04002246 encodedPoint = (unsigned char *)
2247 OPENSSL_malloc(encoded_pt_len *
Adam Langley95c29f32014-06-20 12:00:00 -07002248 sizeof(unsigned char));
David Benjaminbd30f8e2014-08-19 16:02:38 -04002249 bn_ctx = BN_CTX_new();
2250 if ((encodedPoint == NULL) ||
2251 (bn_ctx == NULL))
2252 {
2253 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
2254 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07002255 }
2256
David Benjaminbd30f8e2014-08-19 16:02:38 -04002257 /* Encode the public key */
2258 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2259 EC_KEY_get0_public_key(clnt_ecdh),
2260 POINT_CONVERSION_UNCOMPRESSED,
2261 encodedPoint, encoded_pt_len, bn_ctx);
2262
2263 n = 0;
2264 if ((alg_a & SSL_aPSK) && psk_len != 0)
2265 {
2266 i = strlen(s->session->psk_identity);
2267 s2n(i, p);
2268 memcpy(p, s->session->psk_identity, i);
2269 p += i;
2270 n = i + 2;
2271 }
2272
2273 *p = encoded_pt_len; /* length of encoded point */
2274 /* Encoded point will be copied here */
2275 p += 1;
2276 n += 1;
2277 /* copy the point */
2278 memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
2279 /* increment n to account for length field */
2280 n += encoded_pt_len;
2281
Adam Langley95c29f32014-06-20 12:00:00 -07002282 /* Free allocated memory */
2283 BN_CTX_free(bn_ctx);
2284 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
Adam Langleyc26c8022014-06-20 12:00:00 -07002285 if (clnt_ecdh != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002286 EC_KEY_free(clnt_ecdh);
2287 EVP_PKEY_free(srvr_pub_pkey);
2288 }
2289#endif /* !OPENSSL_NO_ECDH */
Adam Langleyc26c8022014-06-20 12:00:00 -07002290 else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
Adam Langley95c29f32014-06-20 12:00:00 -07002291 {
2292 ssl3_send_alert(s, SSL3_AL_FATAL,
2293 SSL_AD_HANDSHAKE_FAILURE);
2294 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
2295 goto err;
2296 }
2297
2298 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
2299 s->state=SSL3_ST_CW_KEY_EXCH_B;
2300 }
2301
2302 /* SSL3_ST_CW_KEY_EXCH_B */
2303 return ssl_do_write(s);
2304err:
2305#ifndef OPENSSL_NO_ECDH
2306 BN_CTX_free(bn_ctx);
2307 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2308 if (clnt_ecdh != NULL)
2309 EC_KEY_free(clnt_ecdh);
2310 EVP_PKEY_free(srvr_pub_pkey);
2311#endif
2312 return(-1);
2313 }
2314
David Benjamin8da99062014-08-24 12:03:09 -04002315int ssl3_send_cert_verify(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002316 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002317 unsigned char *buf, *p;
2318 const EVP_MD *md;
2319 uint8_t digest[EVP_MAX_MD_SIZE];
2320 unsigned digest_length;
Adam Langley95c29f32014-06-20 12:00:00 -07002321 EVP_PKEY *pkey;
Adam Langleyaed23062014-06-20 12:00:00 -07002322 EVP_PKEY_CTX *pctx = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002323 EVP_MD_CTX mctx;
David Benjamin0e2908a2014-07-09 15:22:43 -04002324 size_t signature_length = 0;
2325 unsigned long n = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002326
2327 EVP_MD_CTX_init(&mctx);
David Benjamin0e2908a2014-07-09 15:22:43 -04002328 buf=(unsigned char *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -07002329
2330 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2331 {
2332 p= ssl_handshake_start(s);
Adam Langleyaed23062014-06-20 12:00:00 -07002333 pkey = s->cert->key->privatekey;
David Benjamin0e2908a2014-07-09 15:22:43 -04002334 /* For TLS v1.2 send signature algorithm and signature using
2335 * agreed digest and cached handshake records. Otherwise, use
2336 * SHA1 or MD5 + SHA1 depending on key type.
Adam Langley95c29f32014-06-20 12:00:00 -07002337 */
2338 if (SSL_USE_SIGALGS(s))
2339 {
Adam Langley37a623c2014-07-18 09:28:40 -07002340 const uint8_t *hdata;
2341 size_t hdatalen;
David Benjamin0e2908a2014-07-09 15:22:43 -04002342 md = s->cert->key->digest;
Adam Langley37a623c2014-07-18 09:28:40 -07002343 if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen) ||
2344 !tls12_get_sigandhash(p, pkey, md))
Adam Langley95c29f32014-06-20 12:00:00 -07002345 {
David Benjamin8da99062014-08-24 12:03:09 -04002346 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002347 goto err;
2348 }
2349 p += 2;
David Benjamin0e2908a2014-07-09 15:22:43 -04002350 n += 2;
2351 if (!EVP_DigestInit_ex(&mctx, md, NULL)
2352 || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
2353 || !EVP_DigestFinal(&mctx, digest, &digest_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002354 {
David Benjamin8da99062014-08-24 12:03:09 -04002355 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -07002356 goto err;
2357 }
Adam Langley95c29f32014-06-20 12:00:00 -07002358 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002359 else if (pkey->type == EVP_PKEY_RSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002360 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002361 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
Adam Langley95c29f32014-06-20 12:00:00 -07002362 s->method->ssl3_enc->cert_verify_mac(s,
David Benjamin0e2908a2014-07-09 15:22:43 -04002363 NID_sha1, &digest[MD5_DIGEST_LENGTH]);
2364 digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
2365 /* Using a NULL signature MD makes EVP_PKEY_sign perform
2366 * a raw RSA signature, rather than wrapping in a
2367 * DigestInfo. */
2368 md = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002369 }
David Benjamina7d13632014-08-19 20:32:41 -04002370 else if (pkey->type == EVP_PKEY_EC)
Adam Langley95c29f32014-06-20 12:00:00 -07002371 {
David Benjamin0e2908a2014-07-09 15:22:43 -04002372 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
2373 digest_length = SHA_DIGEST_LENGTH;
2374 md = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002375 }
2376 else
Adam Langleyaed23062014-06-20 12:00:00 -07002377 {
David Benjamin8da99062014-08-24 12:03:09 -04002378 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002379 goto err;
Adam Langleyaed23062014-06-20 12:00:00 -07002380 }
David Benjamin0e2908a2014-07-09 15:22:43 -04002381
2382 /* Sign the digest. */
2383 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2384 if (pctx == NULL)
2385 goto err;
2386
2387 /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
2388 if (EVP_PKEY_sign_init(pctx) != 1 ||
2389 EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
2390 EVP_PKEY_sign(pctx, NULL, &signature_length,
2391 digest, digest_length) != 1)
2392 {
David Benjamin8da99062014-08-24 12:03:09 -04002393 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002394 goto err;
2395 }
2396
2397 if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
2398 {
David Benjamin8da99062014-08-24 12:03:09 -04002399 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin0e2908a2014-07-09 15:22:43 -04002400 goto err;
2401 }
2402
2403 if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
2404 digest, digest_length) != 1)
2405 {
David Benjamin8da99062014-08-24 12:03:09 -04002406 OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
David Benjamin0e2908a2014-07-09 15:22:43 -04002407 goto err;
2408 }
2409
2410 s2n(signature_length, p);
2411 n += signature_length + 2;
2412
2413 /* Now that client auth is completed, we no longer need cached
2414 * handshake records and can digest them. */
2415 if (SSL_USE_SIGALGS(s))
2416 {
2417 if (!ssl3_digest_cached_records(s))
2418 goto err;
2419 }
2420
Adam Langley95c29f32014-06-20 12:00:00 -07002421 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
2422 s->state=SSL3_ST_CW_CERT_VRFY_B;
2423 }
2424 EVP_MD_CTX_cleanup(&mctx);
2425 EVP_PKEY_CTX_free(pctx);
2426 return ssl_do_write(s);
2427err:
2428 EVP_MD_CTX_cleanup(&mctx);
2429 EVP_PKEY_CTX_free(pctx);
2430 return(-1);
2431 }
2432
2433/* Check a certificate can be used for client authentication. Currently
David Benjaminbd30f8e2014-08-19 16:02:38 -04002434 * check the cert exists and if we have a suitable digest for TLS 1.2.
Adam Langley95c29f32014-06-20 12:00:00 -07002435 */
2436static int ssl3_check_client_certificate(SSL *s)
2437 {
Adam Langley95c29f32014-06-20 12:00:00 -07002438 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2439 return 0;
2440 /* If no suitable signature algorithm can't use certificate */
2441 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
2442 return 0;
2443 /* If strict mode check suitability of chain before using it.
Adam Langley95c29f32014-06-20 12:00:00 -07002444 */
2445 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2446 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2447 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002448 return 1;
2449 }
2450
2451int ssl3_send_client_certificate(SSL *s)
2452 {
2453 X509 *x509=NULL;
2454 EVP_PKEY *pkey=NULL;
2455 int i;
2456
2457 if (s->state == SSL3_ST_CW_CERT_A)
2458 {
2459 /* Let cert callback update client certificates if required */
2460 if (s->cert->cert_cb)
2461 {
2462 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2463 if (i < 0)
2464 {
2465 s->rwstate=SSL_X509_LOOKUP;
2466 return -1;
2467 }
2468 if (i == 0)
2469 {
2470 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
2471 return 0;
2472 }
2473 s->rwstate=SSL_NOTHING;
2474 }
2475 if (ssl3_check_client_certificate(s))
2476 s->state=SSL3_ST_CW_CERT_C;
2477 else
2478 s->state=SSL3_ST_CW_CERT_B;
2479 }
2480
2481 /* We need to get a client cert */
2482 if (s->state == SSL3_ST_CW_CERT_B)
2483 {
2484 /* If we get an error, we need to
2485 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
David Benjamin7bbeead2014-07-25 03:56:20 -04002486 * We then get retried later */
Adam Langley95c29f32014-06-20 12:00:00 -07002487 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2488 if (i < 0)
2489 {
2490 s->rwstate=SSL_X509_LOOKUP;
2491 return(-1);
2492 }
2493 s->rwstate=SSL_NOTHING;
2494 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2495 {
2496 s->state=SSL3_ST_CW_CERT_B;
2497 if ( !SSL_use_certificate(s,x509) ||
2498 !SSL_use_PrivateKey(s,pkey))
2499 i=0;
2500 }
2501 else if (i == 1)
2502 {
2503 i=0;
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002504 OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
Adam Langley95c29f32014-06-20 12:00:00 -07002505 }
2506
2507 if (x509 != NULL) X509_free(x509);
2508 if (pkey != NULL) EVP_PKEY_free(pkey);
2509 if (i && !ssl3_check_client_certificate(s))
2510 i = 0;
2511 if (i == 0)
2512 {
2513 if (s->version == SSL3_VERSION)
2514 {
2515 s->s3->tmp.cert_req=0;
2516 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2517 return(1);
2518 }
2519 else
2520 {
2521 s->s3->tmp.cert_req=2;
2522 }
2523 }
2524
2525 /* Ok, we have a cert */
2526 s->state=SSL3_ST_CW_CERT_C;
2527 }
2528
2529 if (s->state == SSL3_ST_CW_CERT_C)
2530 {
2531 s->state=SSL3_ST_CW_CERT_D;
2532 ssl3_output_cert_chain(s,
2533 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
2534 }
2535 /* SSL3_ST_CW_CERT_D */
2536 return ssl_do_write(s);
2537 }
2538
2539#define has_bits(i,m) (((i)&(m)) == (m))
2540
2541int ssl3_check_cert_and_algorithm(SSL *s)
2542 {
2543 int i,idx;
2544 long alg_k,alg_a;
2545 EVP_PKEY *pkey=NULL;
2546 SESS_CERT *sc;
Adam Langley95c29f32014-06-20 12:00:00 -07002547 RSA *rsa;
Adam Langley95c29f32014-06-20 12:00:00 -07002548#ifndef OPENSSL_NO_DH
2549 DH *dh;
2550#endif
2551
David Benjamine8f3d662014-07-12 01:10:19 -04002552 /* we don't have a certificate */
2553 if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
2554 return 1;
2555
Adam Langley95c29f32014-06-20 12:00:00 -07002556 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2557 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2558
Adam Langley95c29f32014-06-20 12:00:00 -07002559 sc=s->session->sess_cert;
2560 if (sc == NULL)
2561 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002562 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
Adam Langley95c29f32014-06-20 12:00:00 -07002563 goto err;
2564 }
2565
Adam Langley95c29f32014-06-20 12:00:00 -07002566 rsa=s->session->sess_cert->peer_rsa_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002567#ifndef OPENSSL_NO_DH
2568 dh=s->session->sess_cert->peer_dh_tmp;
2569#endif
2570
2571 /* This is the passed certificate */
2572
2573 idx=sc->peer_cert_type;
2574#ifndef OPENSSL_NO_ECDH
2575 if (idx == SSL_PKEY_ECC)
2576 {
2577 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2578 s) == 0)
2579 { /* check failed */
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002580 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002581 goto f_err;
2582 }
2583 else
2584 {
2585 return 1;
2586 }
2587 }
2588 else if (alg_a & SSL_aECDSA)
2589 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002590 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002591 goto f_err;
2592 }
Adam Langley95c29f32014-06-20 12:00:00 -07002593#endif
2594 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2595 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2596 EVP_PKEY_free(pkey);
2597
2598
2599 /* Check that we have a certificate if we require one */
2600 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2601 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002602 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_SIGNING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002603 goto f_err;
2604 }
Adam Langley95c29f32014-06-20 12:00:00 -07002605 if ((alg_k & SSL_kRSA) &&
2606 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2607 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002608 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
Adam Langley95c29f32014-06-20 12:00:00 -07002609 goto f_err;
2610 }
Adam Langley95c29f32014-06-20 12:00:00 -07002611#ifndef OPENSSL_NO_DH
2612 if ((alg_k & SSL_kEDH) &&
2613 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2614 {
HÃ¥vard Molland90974e72014-08-11 15:50:54 +02002615 OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -07002616 goto f_err;
2617 }
Adam Langley95c29f32014-06-20 12:00:00 -07002618#endif
2619
Adam Langley95c29f32014-06-20 12:00:00 -07002620 return(1);
2621f_err:
2622 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2623err:
2624 return(0);
2625 }
2626
Adam Langley1258b6a2014-06-20 12:00:00 -07002627# if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -07002628int ssl3_send_next_proto(SSL *s)
2629 {
2630 unsigned int len, padding_len;
2631 unsigned char *d;
2632
2633 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
2634 {
2635 len = s->next_proto_negotiated_len;
2636 padding_len = 32 - ((len + 2) % 32);
2637 d = (unsigned char *)s->init_buf->data;
2638 d[4] = len;
2639 memcpy(d + 5, s->next_proto_negotiated, len);
2640 d[5 + len] = padding_len;
2641 memset(d + 6 + len, 0, padding_len);
2642 *(d++)=SSL3_MT_NEXT_PROTO;
2643 l2n3(2 + len + padding_len, d);
2644 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2645 s->init_num = 4 + 2 + len + padding_len;
2646 s->init_off = 0;
2647 }
2648
2649 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2650}
Adam Langley1258b6a2014-06-20 12:00:00 -07002651
2652# endif /* !OPENSSL_NO_NEXTPROTONEG */
2653
2654int ssl3_send_channel_id(SSL *s)
2655 {
2656 unsigned char *d;
2657 int ret = -1, public_key_len;
2658 EVP_MD_CTX md_ctx;
2659 size_t sig_len;
2660 ECDSA_SIG *sig = NULL;
2661 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
2662
2663 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
2664 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2665
2666 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
2667 {
2668 EVP_PKEY *key = NULL;
2669 s->ctx->channel_id_cb(s, &key);
2670 if (key != NULL)
2671 {
2672 s->tlsext_channel_id_private = key;
2673 }
2674 }
2675 if (!s->tlsext_channel_id_private)
2676 {
2677 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
2678 return (-1);
2679 }
2680 s->rwstate=SSL_NOTHING;
2681
2682 d = (unsigned char *)s->init_buf->data;
2683 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
2684 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
2685 if (s->s3->tlsext_channel_id_new)
2686 s2n(TLSEXT_TYPE_channel_id_new, d);
2687 else
2688 s2n(TLSEXT_TYPE_channel_id, d);
2689 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
2690
2691 EVP_MD_CTX_init(&md_ctx);
2692
2693 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
2694 if (public_key_len <= 0)
2695 {
2696 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
2697 goto err;
2698 }
2699 /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
2700 * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
2701 * field elements as 32-byte, big-endian numbers. */
2702 if (public_key_len != 65)
2703 {
2704 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
2705 goto err;
2706 }
2707 public_key = OPENSSL_malloc(public_key_len);
2708 if (!public_key)
2709 {
2710 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2711 goto err;
2712 }
2713
2714 derp = public_key;
2715 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
2716
2717 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
2718 s->tlsext_channel_id_private) != 1)
2719 {
2720 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
2721 goto err;
2722 }
2723
2724 if (!tls1_channel_id_hash(&md_ctx, s))
2725 goto err;
2726
2727 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
2728 {
2729 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2730 goto err;
2731 }
2732
2733 der_sig = OPENSSL_malloc(sig_len);
2734 if (!der_sig)
2735 {
2736 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
2737 goto err;
2738 }
2739
2740 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
2741 {
2742 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
2743 goto err;
2744 }
2745
2746 derp = der_sig;
2747 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
2748 if (sig == NULL)
2749 {
2750 OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
2751 goto err;
2752 }
2753
2754 /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
2755 memcpy(d, public_key + 1, 64);
2756 d += 64;
2757 memset(d, 0, 2 * 32);
2758 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
2759 d += 32;
2760 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
2761 d += 32;
2762
2763 s->state = SSL3_ST_CW_CHANNEL_ID_B;
2764 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
2765 s->init_off = 0;
2766
2767 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
2768
2769err:
2770 EVP_MD_CTX_cleanup(&md_ctx);
2771 if (public_key)
2772 OPENSSL_free(public_key);
2773 if (der_sig)
2774 OPENSSL_free(der_sig);
2775 if (sig)
2776 ECDSA_SIG_free(sig);
2777
2778 return ret;
2779 }
Adam Langley95c29f32014-06-20 12:00:00 -07002780
Adam Langley95c29f32014-06-20 12:00:00 -07002781int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2782 {
2783 int i = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002784 if (s->ctx->client_cert_cb)
2785 i = s->ctx->client_cert_cb(s,px509,ppkey);
2786 return i;
2787 }