Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: openssl/ssl/s3_clnt.c

Issue 2072073002: Delete bundled copy of OpenSSL and replace with README. (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/openssl@master
Patch Set: Delete bundled copy of OpenSSL and replace with README. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « openssl/ssl/s3_cbc.c ('k') | openssl/ssl/s3_enc.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* 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 #include "ssl_locl.h"
153 #include "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
159 #ifdef OPENSSL_FIPS
160 #include <openssl/fips.h>
161 #endif
162 #ifndef OPENSSL_NO_DH
163 #include <openssl/dh.h>
164 #endif
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_ENGINE
167 #include <openssl/engine.h>
168 #endif
169
170 static const SSL_METHOD *ssl3_get_client_method(int ver);
171 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
172
173 static const SSL_METHOD *ssl3_get_client_method(int ver)
174 {
175 if (ver == SSL3_VERSION)
176 return(SSLv3_client_method());
177 else
178 return(NULL);
179 }
180
181 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
182 ssl_undefined_function,
183 ssl3_connect,
184 ssl3_get_client_method)
185
186 int ssl3_connect(SSL *s)
187 {
188 BUF_MEM *buf=NULL;
189 unsigned long Time=(unsigned long)time(NULL);
190 void (*cb)(const SSL *ssl,int type,int val)=NULL;
191 int ret= -1;
192 int new_state,state,skip=0;
193
194 RAND_add(&Time,sizeof(Time),0);
195 ERR_clear_error();
196 clear_sys_error();
197
198 if (s->info_callback != NULL)
199 cb=s->info_callback;
200 else if (s->ctx->info_callback != NULL)
201 cb=s->ctx->info_callback;
202
203 s->in_handshake++;
204 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
205
206 #ifndef OPENSSL_NO_HEARTBEATS
207 /* If we're awaiting a HeartbeatResponse, pretend we
208 * already got and don't await it anymore, because
209 * Heartbeats don't make sense during handshakes anyway.
210 */
211 if (s->tlsext_hb_pending)
212 {
213 s->tlsext_hb_pending = 0;
214 s->tlsext_hb_seq++;
215 }
216 #endif
217
218 // BEGIN android-added
219 #if 0
220 /* Send app data in separate packet, otherwise, some particular site
221 * (only one site so far) closes the socket. http://b/2511073
222 * Note: there is a very small chance that two TCP packets
223 * could be arriving at server combined into a single TCP packet,
224 * then trigger that site to break. We haven't encounter that though.
225 */
226 // END android-added
227 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
228 {
229 /* Send app data along with CCS/Finished */
230 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED;
231 }
232
233 // BEGIN android-added
234 #endif
235 // END android-added
236 for (;;)
237 {
238 state=s->state;
239
240 switch(s->state)
241 {
242 case SSL_ST_RENEGOTIATE:
243 s->renegotiate=1;
244 s->state=SSL_ST_CONNECT;
245 s->ctx->stats.sess_connect_renegotiate++;
246 /* break */
247 case SSL_ST_BEFORE:
248 case SSL_ST_CONNECT:
249 case SSL_ST_BEFORE|SSL_ST_CONNECT:
250 case SSL_ST_OK|SSL_ST_CONNECT:
251
252 s->server=0;
253 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
254
255 if ((s->version & 0xff00 ) != 0x0300)
256 {
257 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR) ;
258 ret = -1;
259 goto end;
260 }
261
262 /* s->version=SSL3_VERSION; */
263 s->type=SSL_ST_CONNECT;
264
265 if (s->init_buf == NULL)
266 {
267 if ((buf=BUF_MEM_new()) == NULL)
268 {
269 ret= -1;
270 goto end;
271 }
272 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
273 {
274 ret= -1;
275 goto end;
276 }
277 s->init_buf=buf;
278 buf=NULL;
279 }
280
281 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
282
283 /* setup buffing BIO */
284 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
285
286 /* don't push the buffering BIO quite yet */
287
288 ssl3_init_finished_mac(s);
289
290 s->state=SSL3_ST_CW_CLNT_HELLO_A;
291 s->ctx->stats.sess_connect++;
292 s->init_num=0;
293 break;
294
295 case SSL3_ST_CW_CLNT_HELLO_A:
296 case SSL3_ST_CW_CLNT_HELLO_B:
297
298 s->shutdown=0;
299 ret=ssl3_client_hello(s);
300 if (ret <= 0) goto end;
301 s->state=SSL3_ST_CR_SRVR_HELLO_A;
302 s->init_num=0;
303
304 /* turn on buffering for the next lot of output */
305 if (s->bbio != s->wbio)
306 s->wbio=BIO_push(s->bbio,s->wbio);
307
308 break;
309
310 case SSL3_ST_CR_SRVR_HELLO_A:
311 case SSL3_ST_CR_SRVR_HELLO_B:
312 ret=ssl3_get_server_hello(s);
313 if (ret <= 0) goto end;
314
315 if (s->hit)
316 {
317 s->state=SSL3_ST_CR_FINISHED_A;
318 #ifndef OPENSSL_NO_TLSEXT
319 if (s->tlsext_ticket_expected)
320 {
321 /* receive renewed session ticket */
322 s->state=SSL3_ST_CR_SESSION_TICKET_A;
323 }
324 #endif
325 }
326 else
327 s->state=SSL3_ST_CR_CERT_A;
328 s->init_num=0;
329 break;
330
331 case SSL3_ST_CR_CERT_A:
332 case SSL3_ST_CR_CERT_B:
333 #ifndef OPENSSL_NO_TLSEXT
334 ret=ssl3_check_finished(s);
335 if (ret <= 0) goto end;
336 if (ret == 2)
337 {
338 s->hit = 1;
339 if (s->tlsext_ticket_expected)
340 s->state=SSL3_ST_CR_SESSION_TICKET_A;
341 else
342 s->state=SSL3_ST_CR_FINISHED_A;
343 s->init_num=0;
344 break;
345 }
346 #endif
347 /* Check if it is anon DH/ECDH */
348 /* or PSK */
349 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
350 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
351 {
352 ret=ssl3_get_server_certificate(s);
353 if (ret <= 0) goto end;
354 #ifndef OPENSSL_NO_TLSEXT
355 if (s->tlsext_status_expected)
356 s->state=SSL3_ST_CR_CERT_STATUS_A;
357 else
358 s->state=SSL3_ST_CR_KEY_EXCH_A;
359 }
360 else
361 {
362 skip = 1;
363 s->state=SSL3_ST_CR_KEY_EXCH_A;
364 }
365 #else
366 }
367 else
368 skip=1;
369
370 s->state=SSL3_ST_CR_KEY_EXCH_A;
371 #endif
372 s->init_num=0;
373 break;
374
375 case SSL3_ST_CR_KEY_EXCH_A:
376 case SSL3_ST_CR_KEY_EXCH_B:
377 ret=ssl3_get_key_exchange(s);
378 if (ret <= 0) goto end;
379 s->state=SSL3_ST_CR_CERT_REQ_A;
380 s->init_num=0;
381
382 /* at this point we check that we have the
383 * required stuff from the server */
384 if (!ssl3_check_cert_and_algorithm(s))
385 {
386 ret= -1;
387 goto end;
388 }
389 break;
390
391 case SSL3_ST_CR_CERT_REQ_A:
392 case SSL3_ST_CR_CERT_REQ_B:
393 ret=ssl3_get_certificate_request(s);
394 if (ret <= 0) goto end;
395 s->state=SSL3_ST_CR_SRVR_DONE_A;
396 s->init_num=0;
397 break;
398
399 case SSL3_ST_CR_SRVR_DONE_A:
400 case SSL3_ST_CR_SRVR_DONE_B:
401 ret=ssl3_get_server_done(s);
402 if (ret <= 0) goto end;
403 #ifndef OPENSSL_NO_SRP
404 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP)
405 {
406 if ((ret = SRP_Calc_A_param(s))<=0)
407 {
408 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_SRP_A_CA LC);
409 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_I NTERNAL_ERROR);
410 goto end;
411 }
412 }
413 #endif
414 if (s->s3->tmp.cert_req)
415 s->state=SSL3_ST_CW_CERT_A;
416 else
417 s->state=SSL3_ST_CW_KEY_EXCH_A;
418 s->init_num=0;
419
420 break;
421
422 case SSL3_ST_CW_CERT_A:
423 case SSL3_ST_CW_CERT_B:
424 case SSL3_ST_CW_CERT_C:
425 case SSL3_ST_CW_CERT_D:
426 ret=ssl3_send_client_certificate(s);
427 if (ret <= 0) goto end;
428 s->state=SSL3_ST_CW_KEY_EXCH_A;
429 s->init_num=0;
430 break;
431
432 case SSL3_ST_CW_KEY_EXCH_A:
433 case SSL3_ST_CW_KEY_EXCH_B:
434 ret=ssl3_send_client_key_exchange(s);
435 if (ret <= 0) goto end;
436 /* EAY EAY EAY need to check for DH fix cert
437 * sent back */
438 /* For TLS, cert_req is set to 2, so a cert chain
439 * of nothing is sent, but no verify packet is sent */
440 /* XXX: For now, we do not support client
441 * authentication in ECDH cipher suites with
442 * ECDH (rather than ECDSA) certificates.
443 * We need to skip the certificate verify
444 * message when client's ECDH public key is sent
445 * inside the client certificate.
446 */
447 if (s->s3->tmp.cert_req == 1)
448 {
449 s->state=SSL3_ST_CW_CERT_VRFY_A;
450 }
451 else
452 {
453 s->state=SSL3_ST_CW_CHANGE_A;
454 s->s3->change_cipher_spec=0;
455 }
456 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
457 {
458 s->state=SSL3_ST_CW_CHANGE_A;
459 s->s3->change_cipher_spec=0;
460 }
461
462 s->init_num=0;
463 break;
464
465 case SSL3_ST_CW_CERT_VRFY_A:
466 case SSL3_ST_CW_CERT_VRFY_B:
467 ret=ssl3_send_client_verify(s);
468 if (ret <= 0) goto end;
469 s->state=SSL3_ST_CW_CHANGE_A;
470 s->init_num=0;
471 s->s3->change_cipher_spec=0;
472 break;
473
474 case SSL3_ST_CW_CHANGE_A:
475 case SSL3_ST_CW_CHANGE_B:
476 ret=ssl3_send_change_cipher_spec(s,
477 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
478 if (ret <= 0) goto end;
479
480 s->state=SSL3_ST_CW_FINISHED_A;
481 #if !defined(OPENSSL_NO_TLSEXT)
482 if (s->s3->tlsext_channel_id_valid)
483 s->state=SSL3_ST_CW_CHANNEL_ID_A;
484 # if !defined(OPENSSL_NO_NEXTPROTONEG)
485 if (s->s3->next_proto_neg_seen)
486 s->state=SSL3_ST_CW_NEXT_PROTO_A;
487 # endif
488 #endif
489 s->init_num=0;
490
491 s->session->cipher=s->s3->tmp.new_cipher;
492 #ifdef OPENSSL_NO_COMP
493 s->session->compress_meth=0;
494 #else
495 if (s->s3->tmp.new_compression == NULL)
496 s->session->compress_meth=0;
497 else
498 s->session->compress_meth=
499 s->s3->tmp.new_compression->id;
500 #endif
501 if (!s->method->ssl3_enc->setup_key_block(s))
502 {
503 ret= -1;
504 goto end;
505 }
506
507 if (!s->method->ssl3_enc->change_cipher_state(s,
508 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
509 {
510 ret= -1;
511 goto end;
512 }
513
514 break;
515
516 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
517 case SSL3_ST_CW_NEXT_PROTO_A:
518 case SSL3_ST_CW_NEXT_PROTO_B:
519 ret=ssl3_send_next_proto(s);
520 if (ret <= 0) goto end;
521 if (s->s3->tlsext_channel_id_valid)
522 s->state=SSL3_ST_CW_CHANNEL_ID_A;
523 else
524 s->state=SSL3_ST_CW_FINISHED_A;
525 break;
526 #endif
527
528 #if !defined(OPENSSL_NO_TLSEXT)
529 case SSL3_ST_CW_CHANNEL_ID_A:
530 case SSL3_ST_CW_CHANNEL_ID_B:
531 ret=ssl3_send_channel_id(s);
532 if (ret <= 0) goto end;
533 s->state=SSL3_ST_CW_FINISHED_A;
534 break;
535 #endif
536
537 case SSL3_ST_CW_FINISHED_A:
538 case SSL3_ST_CW_FINISHED_B:
539 ret=ssl3_send_finished(s,
540 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
541 s->method->ssl3_enc->client_finished_label,
542 s->method->ssl3_enc->client_finished_label_len);
543 if (ret <= 0) goto end;
544 s->state=SSL3_ST_CW_FLUSH;
545
546 /* clear flags */
547 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
548 if (s->hit)
549 {
550 s->s3->tmp.next_state=SSL_ST_OK;
551 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINIS HED)
552 {
553 s->state=SSL_ST_OK;
554 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
555 s->s3->delay_buf_pop_ret=0;
556 }
557 }
558 else
559 {
560 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHR OUGH)
561 && ssl3_can_cutthrough(s)
562 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
563 )
564 {
565 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIE NT_FINISHED)
566 {
567 s->state=SSL3_ST_CUTTHROUGH_COMP LETE;
568 s->s3->flags|=SSL3_FLAGS_POP_BUF FER;
569 s->s3->delay_buf_pop_ret=0;
570 }
571 else
572 {
573 s->s3->tmp.next_state=SSL3_ST_CU TTHROUGH_COMPLETE;
574 }
575 }
576 else
577 {
578 #ifndef OPENSSL_NO_TLSEXT
579 /* Allow NewSessionTicket if ticket expe cted */
580 if (s->tlsext_ticket_expected)
581 s->s3->tmp.next_state=SSL3_ST_CR _SESSION_TICKET_A;
582 else
583 #endif
584 s->s3->tmp.next_state=SSL3_ST_CR _FINISHED_A;
585 }
586 if (s->s3->tlsext_channel_id_valid)
587 {
588 /* This is a non-resumption handshake. I f it
589 * involves ChannelID, then record the
590 * handshake hashes at this point in the
591 * session so that any resumption of thi s
592 * session with ChannelID can sign those
593 * hashes. */
594 ret = tls1_record_handshake_hashes_for_c hannel_id(s);
595 if (ret <= 0)
596 goto end;
597 }
598 }
599 s->init_num=0;
600 break;
601
602 #ifndef OPENSSL_NO_TLSEXT
603 case SSL3_ST_CR_SESSION_TICKET_A:
604 case SSL3_ST_CR_SESSION_TICKET_B:
605 ret=ssl3_get_new_session_ticket(s);
606 if (ret <= 0) goto end;
607 s->state=SSL3_ST_CR_FINISHED_A;
608 s->init_num=0;
609 break;
610
611 case SSL3_ST_CR_CERT_STATUS_A:
612 case SSL3_ST_CR_CERT_STATUS_B:
613 ret=ssl3_get_cert_status(s);
614 if (ret <= 0) goto end;
615 s->state=SSL3_ST_CR_KEY_EXCH_A;
616 s->init_num=0;
617 break;
618 #endif
619
620 case SSL3_ST_CR_FINISHED_A:
621 case SSL3_ST_CR_FINISHED_B:
622
623 s->s3->flags |= SSL3_FLAGS_CCS_OK;
624 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
625 SSL3_ST_CR_FINISHED_B);
626 if (ret <= 0) goto end;
627
628 if (s->hit)
629 s->state=SSL3_ST_CW_CHANGE_A;
630 else
631 s->state=SSL_ST_OK;
632 s->init_num=0;
633 break;
634
635 case SSL3_ST_CW_FLUSH:
636 s->rwstate=SSL_WRITING;
637 if (BIO_flush(s->wbio) <= 0)
638 {
639 ret= -1;
640 goto end;
641 }
642 s->rwstate=SSL_NOTHING;
643 s->state=s->s3->tmp.next_state;
644 break;
645
646 case SSL3_ST_CUTTHROUGH_COMPLETE:
647 #ifndef OPENSSL_NO_TLSEXT
648 /* Allow NewSessionTicket if ticket expected */
649 if (s->tlsext_ticket_expected)
650 s->state=SSL3_ST_CR_SESSION_TICKET_A;
651 else
652 #endif
653 s->state=SSL3_ST_CR_FINISHED_A;
654
655 /* SSL_write() will take care of flushing buffered data if
656 * DELAY_CLIENT_FINISHED is set.
657 */
658 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED))
659 ssl_free_wbio_buffer(s);
660 ret = 1;
661 goto end;
662 /* break; */
663
664 case SSL_ST_OK:
665 /* clean a few things up */
666 ssl3_cleanup_key_block(s);
667
668 if (s->init_buf != NULL)
669 {
670 BUF_MEM_free(s->init_buf);
671 s->init_buf=NULL;
672 }
673
674 /* If we are not 'joining' the last two packets,
675 * remove the buffering now */
676 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
677 ssl_free_wbio_buffer(s);
678 /* else do it later in ssl3_write */
679
680 s->init_num=0;
681 s->renegotiate=0;
682 s->new_session=0;
683
684 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
685 if (s->hit) s->ctx->stats.sess_hit++;
686
687 ret=1;
688 /* s->server=0; */
689 s->handshake_func=ssl3_connect;
690 s->ctx->stats.sess_connect_good++;
691
692 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
693
694 goto end;
695 /* break; */
696
697 default:
698 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
699 ret= -1;
700 goto end;
701 /* break; */
702 }
703
704 /* did we do anything */
705 if (!s->s3->tmp.reuse_message && !skip)
706 {
707 if (s->debug)
708 {
709 if ((ret=BIO_flush(s->wbio)) <= 0)
710 goto end;
711 }
712
713 if ((cb != NULL) && (s->state != state))
714 {
715 new_state=s->state;
716 s->state=state;
717 cb(s,SSL_CB_CONNECT_LOOP,1);
718 s->state=new_state;
719 }
720 }
721 skip=0;
722 }
723 end:
724 s->in_handshake--;
725 if (buf != NULL)
726 BUF_MEM_free(buf);
727 if (cb != NULL)
728 cb(s,SSL_CB_CONNECT_EXIT,ret);
729 return(ret);
730 }
731
732
733 int ssl3_client_hello(SSL *s)
734 {
735 unsigned char *buf;
736 unsigned char *p,*d;
737 int i;
738 unsigned long Time,l;
739 #ifndef OPENSSL_NO_COMP
740 int j;
741 SSL_COMP *comp;
742 #endif
743
744 buf=(unsigned char *)s->init_buf->data;
745 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
746 {
747 SSL_SESSION *sess = s->session;
748 if ((sess == NULL) ||
749 (sess->ssl_version != s->version) ||
750 #ifdef OPENSSL_NO_TLSEXT
751 !sess->session_id_length ||
752 #else
753 (!sess->session_id_length && !sess->tlsext_tick) ||
754 #endif
755 (sess->not_resumable))
756 {
757 if (!s->session_creation_enabled)
758 {
759 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE _FAILURE);
760 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_SESSION_MAY _NOT_BE_CREATED);
761 goto err;
762 }
763 if (!ssl_get_new_session(s,0))
764 goto err;
765 }
766 /* else use the pre-loaded session */
767
768 p=s->s3->client_random;
769 Time=(unsigned long)time(NULL); /* Time */
770 l2n(Time,p);
771 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
772 goto err;
773
774 /* Do the message type and length last.
775 * Note: the final argument to ssl_add_clienthello_tlsext below
776 * depends on the size of this prefix. */
777 d=p= &(buf[4]);
778
779 /* version indicates the negotiated version: for example from
780 * an SSLv2/v3 compatible client hello). The client_version
781 * field is the maximum version we permit and it is also
782 * used in RSA encrypted premaster secrets. Some servers can
783 * choke if we initially report a higher version then
784 * renegotiate to a lower one in the premaster secret. This
785 * didn't happen with TLS 1.0 as most servers supported it
786 * but it can with TLS 1.1 or later if the server only supports
787 * 1.0.
788 *
789 * Possible scenario with previous logic:
790 * 1. Client hello indicates TLS 1.2
791 * 2. Server hello says TLS 1.0
792 * 3. RSA encrypted premaster secret uses 1.2.
793 * 4. Handhaked proceeds using TLS 1.0.
794 * 5. Server sends hello request to renegotiate.
795 * 6. Client hello indicates TLS v1.0 as we now
796 * know that is maximum server supports.
797 * 7. Server chokes on RSA encrypted premaster secret
798 * containing version 1.0.
799 *
800 * For interoperability it should be OK to always use the
801 * maximum version we support in client hello and then rely
802 * on the checking of version to ensure the servers isn't
803 * being inconsistent: for example initially negotiating with
804 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
805 * client_version in client hello and not resetting it to
806 * the negotiated version.
807 */
808 #if 0
809 *(p++)=s->version>>8;
810 *(p++)=s->version&0xff;
811 s->client_version=s->version;
812 #else
813 *(p++)=s->client_version>>8;
814 *(p++)=s->client_version&0xff;
815 #endif
816
817 /* Random stuff */
818 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
819 p+=SSL3_RANDOM_SIZE;
820
821 /* Session ID */
822 if (s->new_session)
823 i=0;
824 else
825 i=s->session->session_id_length;
826 *(p++)=i;
827 if (i != 0)
828 {
829 if (i > (int)sizeof(s->session->session_id))
830 {
831 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_E RROR);
832 goto err;
833 }
834 memcpy(p,s->session->session_id,i);
835 p+=i;
836 }
837
838 /* Ciphers supported */
839 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
840 if (i == 0)
841 {
842 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABL E);
843 goto err;
844 }
845 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
846 /* Some servers hang if client hello > 256 bytes
847 * as hack workaround chop number of supported ciphers
848 * to keep it well below this if we use TLS v1.2
849 */
850 if (TLS1_get_version(s) >= TLS1_2_VERSION
851 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
852 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
853 #endif
854 s2n(i,p);
855 p+=i;
856
857 /* COMPRESSION */
858 #ifdef OPENSSL_NO_COMP
859 *(p++)=1;
860 #else
861
862 if ((s->options & SSL_OP_NO_COMPRESSION)
863 || !s->ctx->comp_methods)
864 j=0;
865 else
866 j=sk_SSL_COMP_num(s->ctx->comp_methods);
867 *(p++)=1+j;
868 for (i=0; i<j; i++)
869 {
870 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
871 *(p++)=comp->id;
872 }
873 #endif
874 *(p++)=0; /* Add the NULL method */
875
876 #ifndef OPENSSL_NO_TLSEXT
877 /* TLS extensions*/
878 if (ssl_prepare_clienthello_tlsext(s) <= 0)
879 {
880 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT) ;
881 goto err;
882 }
883 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_ LENGTH, p-buf)) == NULL)
884 {
885 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
886 goto err;
887 }
888 #endif
889
890 l=(p-d);
891 d=buf;
892 *(d++)=SSL3_MT_CLIENT_HELLO;
893 l2n3(l,d);
894
895 s->state=SSL3_ST_CW_CLNT_HELLO_B;
896 /* number of bytes to write */
897 s->init_num=p-buf;
898 s->init_off=0;
899 }
900
901 /* SSL3_ST_CW_CLNT_HELLO_B */
902 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
903 err:
904 return(-1);
905 }
906
907 int ssl3_get_server_hello(SSL *s)
908 {
909 STACK_OF(SSL_CIPHER) *sk;
910 const SSL_CIPHER *c;
911 unsigned char *p,*d;
912 int i,al,ok;
913 unsigned int j;
914 long n;
915 #ifndef OPENSSL_NO_COMP
916 SSL_COMP *comp;
917 #endif
918
919 n=s->method->ssl_get_message(s,
920 SSL3_ST_CR_SRVR_HELLO_A,
921 SSL3_ST_CR_SRVR_HELLO_B,
922 -1,
923 20000, /* ?? */
924 &ok);
925
926 if (!ok) return((int)n);
927
928 if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
929 {
930 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
931 {
932 if ( s->d1->send_cookie == 0)
933 {
934 s->s3->tmp.reuse_message = 1;
935 return 1;
936 }
937 else /* already sent a cookie */
938 {
939 al=SSL_AD_UNEXPECTED_MESSAGE;
940 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MES SAGE_TYPE);
941 goto f_err;
942 }
943 }
944 }
945
946 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
947 {
948 al=SSL_AD_UNEXPECTED_MESSAGE;
949 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
950 goto f_err;
951 }
952
953 d=p=(unsigned char *)s->init_msg;
954
955 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
956 {
957 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
958 s->version=(s->version&0xff00)|p[1];
959 al=SSL_AD_PROTOCOL_VERSION;
960 goto f_err;
961 }
962 p+=2;
963
964 /* load the server hello data */
965 /* load the server random */
966 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
967 p+=SSL3_RANDOM_SIZE;
968
969 /* get the session-id */
970 j= *(p++);
971
972 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
973 {
974 al=SSL_AD_ILLEGAL_PARAMETER;
975 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LON G);
976 goto f_err;
977 }
978
979 #ifndef OPENSSL_NO_TLSEXT
980 /* check if we want to resume the session based on external pre-shared s ecret */
981 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
982 {
983 SSL_CIPHER *pref_cipher=NULL;
984 s->session->master_key_length=sizeof(s->session->master_key);
985 if (s->tls_session_secret_cb(s, s->session->master_key,
986 &s->session->master_key_length,
987 NULL, &pref_cipher,
988 s->tls_session_secret_cb_arg))
989 {
990 s->session->cipher = pref_cipher ?
991 pref_cipher : ssl_get_cipher_by_char(s, p+j);
992 }
993 }
994 #endif /* OPENSSL_NO_TLSEXT */
995
996 if (j != 0 && j == s->session->session_id_length
997 && memcmp(p,s->session->session_id,j) == 0)
998 {
999 if(s->sid_ctx_length != s->session->sid_ctx_length
1000 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
1001 {
1002 /* actually a client application bug */
1003 al=SSL_AD_ILLEGAL_PARAMETER;
1004 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSIO N_IN_DIFFERENT_CONTEXT);
1005 goto f_err;
1006 }
1007 s->s3->flags |= SSL3_FLAGS_CCS_OK;
1008 s->hit=1;
1009 }
1010 else /* a miss or crap from the other end */
1011 {
1012 /* If we were trying for session-id reuse, make a new
1013 * SSL_SESSION so we don't stuff up other people */
1014 s->hit=0;
1015 if (s->session->session_id_length > 0)
1016 {
1017 if (!s->session_creation_enabled)
1018 {
1019 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE _FAILURE);
1020 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SESSION _MAY_NOT_BE_CREATED);
1021 goto err;
1022 }
1023 if (!ssl_get_new_session(s,0))
1024 {
1025 al=SSL_AD_INTERNAL_ERROR;
1026 goto f_err;
1027 }
1028 }
1029 s->session->session_id_length=j;
1030 memcpy(s->session->session_id,p,j); /* j could be 0 */
1031 }
1032 p+=j;
1033 c=ssl_get_cipher_by_char(s,p);
1034 if (c == NULL)
1035 {
1036 /* unknown cipher */
1037 al=SSL_AD_ILLEGAL_PARAMETER;
1038 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED );
1039 goto f_err;
1040 }
1041 /* TLS v1.2 only ciphersuites require v1.2 or later */
1042 if ((c->algorithm_ssl & SSL_TLSV1_2) &&
1043 (TLS1_get_version(s) < TLS1_2_VERSION))
1044 {
1045 al=SSL_AD_ILLEGAL_PARAMETER;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
1047 goto f_err;
1048 }
1049 p+=ssl_put_cipher_by_char(s,NULL,NULL);
1050
1051 sk=ssl_get_ciphers_by_id(s);
1052 i=sk_SSL_CIPHER_find(sk,c);
1053 if (i < 0)
1054 {
1055 /* we did not say we would use this cipher */
1056 al=SSL_AD_ILLEGAL_PARAMETER;
1057 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
1058 goto f_err;
1059 }
1060
1061 /* Depending on the session caching (internal/external), the cipher
1062 and/or cipher_id values may not be set. Make sure that
1063 cipher_id is set and use it for comparison. */
1064 if (s->session->cipher)
1065 s->session->cipher_id = s->session->cipher->id;
1066 if (s->hit && (s->session->cipher_id != c->id))
1067 {
1068 /* Workaround is now obsolete */
1069 #if 0
1070 if (!(s->options &
1071 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1072 #endif
1073 {
1074 al=SSL_AD_ILLEGAL_PARAMETER;
1075 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIP HER_NOT_RETURNED);
1076 goto f_err;
1077 }
1078 }
1079 s->s3->tmp.new_cipher=c;
1080 /* Don't digest cached records if TLS v1.2: we may need them for
1081 * client authentication.
1082 */
1083 if (TLS1_get_version(s) < TLS1_2_VERSION && !ssl3_digest_cached_records( s))
1084 {
1085 al = SSL_AD_INTERNAL_ERROR;
1086 goto f_err;
1087 }
1088 /* lets get the compression algorithm */
1089 /* COMPRESSION */
1090 #ifdef OPENSSL_NO_COMP
1091 if (*(p++) != 0)
1092 {
1093 al=SSL_AD_ILLEGAL_PARAMETER;
1094 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION _ALGORITHM);
1095 goto f_err;
1096 }
1097 /* If compression is disabled we'd better not try to resume a session
1098 * using compression.
1099 */
1100 if (s->session->compress_meth != 0)
1101 {
1102 al=SSL_AD_INTERNAL_ERROR;
1103 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSIO N);
1104 goto f_err;
1105 }
1106 #else
1107 j= *(p++);
1108 if (s->hit && j != s->session->compress_meth)
1109 {
1110 al=SSL_AD_ILLEGAL_PARAMETER;
1111 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION _ALGORITHM_NOT_RETURNED);
1112 goto f_err;
1113 }
1114 if (j == 0)
1115 comp=NULL;
1116 else if (s->options & SSL_OP_NO_COMPRESSION)
1117 {
1118 al=SSL_AD_ILLEGAL_PARAMETER;
1119 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
1120 goto f_err;
1121 }
1122 else
1123 comp=ssl3_comp_find(s->ctx->comp_methods,j);
1124
1125 if ((j != 0) && (comp == NULL))
1126 {
1127 al=SSL_AD_ILLEGAL_PARAMETER;
1128 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION _ALGORITHM);
1129 goto f_err;
1130 }
1131 else
1132 {
1133 s->s3->tmp.new_compression=comp;
1134 }
1135 #endif
1136
1137 #ifndef OPENSSL_NO_TLSEXT
1138 /* TLS extensions*/
1139 if (s->version >= SSL3_VERSION)
1140 {
1141 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
1142 {
1143 /* 'al' set by ssl_parse_serverhello_tlsext */
1144 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
1145 goto f_err;
1146 }
1147 if (ssl_check_serverhello_tlsext(s) <= 0)
1148 {
1149 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLS EXT);
1150 goto err;
1151 }
1152 }
1153 #endif
1154
1155 if (p != (d+n))
1156 {
1157 /* wrong packet length */
1158 al=SSL_AD_DECODE_ERROR;
1159 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
1160 goto f_err;
1161 }
1162
1163 return(1);
1164 f_err:
1165 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1166 err:
1167 return(-1);
1168 }
1169
1170 int ssl3_get_server_certificate(SSL *s)
1171 {
1172 int al,i,ok,ret= -1;
1173 unsigned long n,nc,llen,l;
1174 X509 *x=NULL;
1175 const unsigned char *q,*p;
1176 unsigned char *d;
1177 STACK_OF(X509) *sk=NULL;
1178 SESS_CERT *sc;
1179 EVP_PKEY *pkey=NULL;
1180 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
1181
1182 n=s->method->ssl_get_message(s,
1183 SSL3_ST_CR_CERT_A,
1184 SSL3_ST_CR_CERT_B,
1185 -1,
1186 s->max_cert_list,
1187 &ok);
1188
1189 if (!ok) return((int)n);
1190
1191 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1192 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1193 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
1194 {
1195 s->s3->tmp.reuse_message=1;
1196 return(1);
1197 }
1198
1199 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1200 {
1201 al=SSL_AD_UNEXPECTED_MESSAGE;
1202 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE) ;
1203 goto f_err;
1204 }
1205 p=d=(unsigned char *)s->init_msg;
1206
1207 if ((sk=sk_X509_new_null()) == NULL)
1208 {
1209 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1210 goto err;
1211 }
1212
1213 n2l3(p,llen);
1214 if (llen+3 != n)
1215 {
1216 al=SSL_AD_DECODE_ERROR;
1217 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1218 goto f_err;
1219 }
1220 for (nc=0; nc<llen; )
1221 {
1222 n2l3(p,l);
1223 if ((l+nc+3) > llen)
1224 {
1225 al=SSL_AD_DECODE_ERROR;
1226 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENG TH_MISMATCH);
1227 goto f_err;
1228 }
1229
1230 q=p;
1231 x=d2i_X509(NULL,&q,l);
1232 if (x == NULL)
1233 {
1234 al=SSL_AD_BAD_CERTIFICATE;
1235 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB) ;
1236 goto f_err;
1237 }
1238 if (q != (p+l))
1239 {
1240 al=SSL_AD_DECODE_ERROR;
1241 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENG TH_MISMATCH);
1242 goto f_err;
1243 }
1244 if (!sk_X509_push(sk,x))
1245 {
1246 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FA ILURE);
1247 goto err;
1248 }
1249 x=NULL;
1250 nc+=l+3;
1251 p=q;
1252 }
1253
1254 i=ssl_verify_cert_chain(s,sk);
1255 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1256 #ifndef OPENSSL_NO_KRB5
1257 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1258 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1259 #endif /* OPENSSL_NO_KRB5 */
1260 )
1261 {
1262 al=ssl_verify_alarm_type(s->verify_result);
1263 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIF Y_FAILED);
1264 goto f_err;
1265 }
1266 ERR_clear_error(); /* but we keep s->verify_result */
1267
1268 sc=ssl_sess_cert_new();
1269 if (sc == NULL) goto err;
1270
1271 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1272 s->session->sess_cert=sc;
1273
1274 sc->cert_chain=sk;
1275 /* Inconsistency alert: cert_chain does include the peer's
1276 * certificate, which we don't include in s3_srvr.c */
1277 x=sk_X509_value(sk,0);
1278 sk=NULL;
1279 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1280
1281 pkey=X509_get_pubkey(x);
1282
1283 /* VRS: allow null cert if auth == KRB5 */
1284 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1285 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1286 ? 0 : 1;
1287
1288 #ifdef KSSL_DEBUG
1289 printf("pkey,x = %p, %p\n", pkey,x);
1290 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1291 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->na me,
1292 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->al gorithm_auth, need_cert);
1293 #endif /* KSSL_DEBUG */
1294
1295 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1296 {
1297 x=NULL;
1298 al=SSL3_AL_FATAL;
1299 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1300 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1301 goto f_err;
1302 }
1303
1304 i=ssl_cert_type(x,pkey);
1305 if (need_cert && i < 0)
1306 {
1307 x=NULL;
1308 al=SSL3_AL_FATAL;
1309 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1310 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1311 goto f_err;
1312 }
1313
1314 if (need_cert)
1315 {
1316 sc->peer_cert_type=i;
1317 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1318 /* Why would the following ever happen?
1319 * We just created sc a couple of lines ago. */
1320 if (sc->peer_pkeys[i].x509 != NULL)
1321 X509_free(sc->peer_pkeys[i].x509);
1322 sc->peer_pkeys[i].x509=x;
1323 sc->peer_key= &(sc->peer_pkeys[i]);
1324
1325 if (s->session->peer != NULL)
1326 X509_free(s->session->peer);
1327 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1328 s->session->peer=x;
1329 }
1330 else
1331 {
1332 sc->peer_cert_type=i;
1333 sc->peer_key= NULL;
1334
1335 if (s->session->peer != NULL)
1336 X509_free(s->session->peer);
1337 s->session->peer=NULL;
1338 }
1339 s->session->verify_result = s->verify_result;
1340
1341 x=NULL;
1342 ret=1;
1343
1344 if (0)
1345 {
1346 f_err:
1347 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1348 }
1349 err:
1350 EVP_PKEY_free(pkey);
1351 X509_free(x);
1352 sk_X509_pop_free(sk,X509_free);
1353 return(ret);
1354 }
1355
1356 int ssl3_get_key_exchange(SSL *s)
1357 {
1358 #ifndef OPENSSL_NO_RSA
1359 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1360 #endif
1361 EVP_MD_CTX md_ctx;
1362 unsigned char *param,*p;
1363 int al,i,j,param_len,ok;
1364 long n,alg_k,alg_a;
1365 EVP_PKEY *pkey=NULL;
1366 const EVP_MD *md = NULL;
1367 #ifndef OPENSSL_NO_RSA
1368 RSA *rsa=NULL;
1369 #endif
1370 #ifndef OPENSSL_NO_DH
1371 DH *dh=NULL;
1372 #endif
1373 #ifndef OPENSSL_NO_ECDH
1374 EC_KEY *ecdh = NULL;
1375 BN_CTX *bn_ctx = NULL;
1376 EC_POINT *srvr_ecpoint = NULL;
1377 int curve_nid = 0;
1378 int encoded_pt_len = 0;
1379 #endif
1380
1381 /* use same message size as in ssl3_get_certificate_request()
1382 * as ServerKeyExchange message may be skipped */
1383 n=s->method->ssl_get_message(s,
1384 SSL3_ST_CR_KEY_EXCH_A,
1385 SSL3_ST_CR_KEY_EXCH_B,
1386 -1,
1387 s->max_cert_list,
1388 &ok);
1389 if (!ok) return((int)n);
1390
1391 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1392 {
1393 #ifndef OPENSSL_NO_PSK
1394 /* In plain PSK ciphersuite, ServerKeyExchange can be
1395 omitted if no identity hint is sent. Set
1396 session->sess_cert anyway to avoid problems
1397 later.*/
1398 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1399 {
1400 s->session->sess_cert=ssl_sess_cert_new();
1401 if (s->ctx->psk_identity_hint)
1402 OPENSSL_free(s->ctx->psk_identity_hint);
1403 s->ctx->psk_identity_hint = NULL;
1404 }
1405 #endif
1406 s->s3->tmp.reuse_message=1;
1407 return(1);
1408 }
1409
1410 param=p=(unsigned char *)s->init_msg;
1411 if (s->session->sess_cert != NULL)
1412 {
1413 #ifndef OPENSSL_NO_RSA
1414 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1415 {
1416 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1417 s->session->sess_cert->peer_rsa_tmp=NULL;
1418 }
1419 #endif
1420 #ifndef OPENSSL_NO_DH
1421 if (s->session->sess_cert->peer_dh_tmp)
1422 {
1423 DH_free(s->session->sess_cert->peer_dh_tmp);
1424 s->session->sess_cert->peer_dh_tmp=NULL;
1425 }
1426 #endif
1427 #ifndef OPENSSL_NO_ECDH
1428 if (s->session->sess_cert->peer_ecdh_tmp)
1429 {
1430 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1431 s->session->sess_cert->peer_ecdh_tmp=NULL;
1432 }
1433 #endif
1434 }
1435 else
1436 {
1437 s->session->sess_cert=ssl_sess_cert_new();
1438 }
1439
1440 param_len=0;
1441 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1442 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1443 EVP_MD_CTX_init(&md_ctx);
1444
1445 #ifndef OPENSSL_NO_PSK
1446 if (alg_k & SSL_kPSK)
1447 {
1448 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1449
1450 al=SSL_AD_HANDSHAKE_FAILURE;
1451 n2s(p,i);
1452 param_len=i+2;
1453 /* Store PSK identity hint for later use, hint is used
1454 * in ssl3_send_client_key_exchange. Assume that the
1455 * maximum length of a PSK identity hint can be as
1456 * long as the maximum length of a PSK identity. */
1457 if (i > PSK_MAX_IDENTITY_LEN)
1458 {
1459 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1460 SSL_R_DATA_LENGTH_TOO_LONG);
1461 goto f_err;
1462 }
1463 if (param_len > n)
1464 {
1465 al=SSL_AD_DECODE_ERROR;
1466 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1467 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1468 goto f_err;
1469 }
1470 /* If received PSK identity hint contains NULL
1471 * characters, the hint is truncated from the first
1472 * NULL. p may not be ending with NULL, so create a
1473 * NULL-terminated string. */
1474 memcpy(tmp_id_hint, p, i);
1475 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1476 if (s->ctx->psk_identity_hint != NULL)
1477 OPENSSL_free(s->ctx->psk_identity_hint);
1478 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1479 if (s->ctx->psk_identity_hint == NULL)
1480 {
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE );
1482 goto f_err;
1483 }
1484
1485 p+=i;
1486 n-=param_len;
1487 }
1488 else
1489 #endif /* !OPENSSL_NO_PSK */
1490 #ifndef OPENSSL_NO_SRP
1491 if (alg_k & SSL_kSRP)
1492 {
1493 n2s(p,i);
1494 param_len=i+2;
1495 if (param_len > n)
1496 {
1497 al=SSL_AD_DECODE_ERROR;
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_N_LENGT H);
1499 goto f_err;
1500 }
1501 if (!(s->srp_ctx.N=BN_bin2bn(p,i,NULL)))
1502 {
1503 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1504 goto err;
1505 }
1506 p+=i;
1507
1508 n2s(p,i);
1509 param_len+=i+2;
1510 if (param_len > n)
1511 {
1512 al=SSL_AD_DECODE_ERROR;
1513 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_G_LENGT H);
1514 goto f_err;
1515 }
1516 if (!(s->srp_ctx.g=BN_bin2bn(p,i,NULL)))
1517 {
1518 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1519 goto err;
1520 }
1521 p+=i;
1522
1523 i = (unsigned int)(p[0]);
1524 p++;
1525 param_len+=i+1;
1526 if (param_len > n)
1527 {
1528 al=SSL_AD_DECODE_ERROR;
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_S_LENGT H);
1530 goto f_err;
1531 }
1532 if (!(s->srp_ctx.s=BN_bin2bn(p,i,NULL)))
1533 {
1534 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1535 goto err;
1536 }
1537 p+=i;
1538
1539 n2s(p,i);
1540 param_len+=i+2;
1541 if (param_len > n)
1542 {
1543 al=SSL_AD_DECODE_ERROR;
1544 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_B_LENGT H);
1545 goto f_err;
1546 }
1547 if (!(s->srp_ctx.B=BN_bin2bn(p,i,NULL)))
1548 {
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1550 goto err;
1551 }
1552 p+=i;
1553 n-=param_len;
1554
1555 /* We must check if there is a certificate */
1556 #ifndef OPENSSL_NO_RSA
1557 if (alg_a & SSL_aRSA)
1558 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_RSA_ENC].x509);
1559 #else
1560 if (0)
1561 ;
1562 #endif
1563 #ifndef OPENSSL_NO_DSA
1564 else if (alg_a & SSL_aDSS)
1565 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_DSA_SIGN].x509);
1566 #endif
1567 }
1568 else
1569 #endif /* !OPENSSL_NO_SRP */
1570 #ifndef OPENSSL_NO_RSA
1571 if (alg_k & SSL_kRSA)
1572 {
1573 if ((rsa=RSA_new()) == NULL)
1574 {
1575 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE) ;
1576 goto err;
1577 }
1578 n2s(p,i);
1579 param_len=i+2;
1580 if (param_len > n)
1581 {
1582 al=SSL_AD_DECODE_ERROR;
1583 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS _LENGTH);
1584 goto f_err;
1585 }
1586 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1587 {
1588 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1589 goto err;
1590 }
1591 p+=i;
1592
1593 n2s(p,i);
1594 param_len+=i+2;
1595 if (param_len > n)
1596 {
1597 al=SSL_AD_DECODE_ERROR;
1598 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGT H);
1599 goto f_err;
1600 }
1601 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1602 {
1603 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1604 goto err;
1605 }
1606 p+=i;
1607 n-=param_len;
1608
1609 /* this should be because we are using an export cipher */
1610 if (alg_a & SSL_aRSA)
1611 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_RSA_ENC].x509);
1612 else
1613 {
1614 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR) ;
1615 goto err;
1616 }
1617 s->session->sess_cert->peer_rsa_tmp=rsa;
1618 rsa=NULL;
1619 }
1620 #else /* OPENSSL_NO_RSA */
1621 if (0)
1622 ;
1623 #endif
1624 #ifndef OPENSSL_NO_DH
1625 else if (alg_k & SSL_kEDH)
1626 {
1627 if ((dh=DH_new()) == NULL)
1628 {
1629 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1630 goto err;
1631 }
1632 n2s(p,i);
1633 param_len=i+2;
1634 if (param_len > n)
1635 {
1636 al=SSL_AD_DECODE_ERROR;
1637 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH );
1638 goto f_err;
1639 }
1640 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1641 {
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1643 goto err;
1644 }
1645 p+=i;
1646
1647 n2s(p,i);
1648 param_len+=i+2;
1649 if (param_len > n)
1650 {
1651 al=SSL_AD_DECODE_ERROR;
1652 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH );
1653 goto f_err;
1654 }
1655 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1656 {
1657 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1658 goto err;
1659 }
1660 p+=i;
1661
1662 n2s(p,i);
1663 param_len+=i+2;
1664 if (param_len > n)
1665 {
1666 al=SSL_AD_DECODE_ERROR;
1667 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_ LENGTH);
1668 goto f_err;
1669 }
1670 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1671 {
1672 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1673 goto err;
1674 }
1675 p+=i;
1676 n-=param_len;
1677
1678 #ifndef OPENSSL_NO_RSA
1679 if (alg_a & SSL_aRSA)
1680 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_RSA_ENC].x509);
1681 #else
1682 if (0)
1683 ;
1684 #endif
1685 #ifndef OPENSSL_NO_DSA
1686 else if (alg_a & SSL_aDSS)
1687 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_DSA_SIGN].x509);
1688 #endif
1689 /* else anonymous DH, so no certificate or pkey. */
1690
1691 s->session->sess_cert->peer_dh_tmp=dh;
1692 dh=NULL;
1693 }
1694 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1695 {
1696 al=SSL_AD_ILLEGAL_PARAMETER;
1697 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTE D_CIPHER);
1698 goto f_err;
1699 }
1700 #endif /* !OPENSSL_NO_DH */
1701
1702 #ifndef OPENSSL_NO_ECDH
1703 else if (alg_k & SSL_kEECDH)
1704 {
1705 EC_GROUP *ngroup;
1706 const EC_GROUP *group;
1707
1708 if ((ecdh=EC_KEY_new()) == NULL)
1709 {
1710 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE) ;
1711 goto err;
1712 }
1713
1714 /* Extract elliptic curve parameters and the
1715 * server's ephemeral ECDH public key.
1716 * Keep accumulating lengths of various components in
1717 * param_len and make sure it never exceeds n.
1718 */
1719
1720 /* XXX: For now we only support named (not generic) curves
1721 * and the ECParameters in this case is just three bytes.
1722 */
1723 param_len=3;
1724 if ((param_len > n) ||
1725 (*p != NAMED_CURVE_TYPE) ||
1726 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
1727 {
1728 al=SSL_AD_INTERNAL_ERROR;
1729 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ ECDH_PARAMETERS);
1730 goto f_err;
1731 }
1732
1733 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1734 if (ngroup == NULL)
1735 {
1736 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1737 goto err;
1738 }
1739 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1740 {
1741 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1742 goto err;
1743 }
1744 EC_GROUP_free(ngroup);
1745
1746 group = EC_KEY_get0_group(ecdh);
1747
1748 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1749 (EC_GROUP_get_degree(group) > 163))
1750 {
1751 al=SSL_AD_EXPORT_RESTRICTION;
1752 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LAR GE_FOR_CIPHER);
1753 goto f_err;
1754 }
1755
1756 p+=3;
1757
1758 /* Next, get the encoded ECPoint */
1759 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1760 ((bn_ctx = BN_CTX_new()) == NULL))
1761 {
1762 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE) ;
1763 goto err;
1764 }
1765
1766 encoded_pt_len = *p; /* length of encoded point */
1767 p+=1;
1768 param_len += (1 + encoded_pt_len);
1769 if ((param_len > n) ||
1770 (EC_POINT_oct2point(group, srvr_ecpoint,
1771 p, encoded_pt_len, bn_ctx) == 0))
1772 {
1773 al=SSL_AD_DECODE_ERROR;
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1775 goto f_err;
1776 }
1777
1778 n-=param_len;
1779 p+=encoded_pt_len;
1780
1781 /* The ECC/TLS specification does not mention
1782 * the use of DSA to sign ECParameters in the server
1783 * key exchange message. We do support RSA and ECDSA.
1784 */
1785 if (0) ;
1786 #ifndef OPENSSL_NO_RSA
1787 else if (alg_a & SSL_aRSA)
1788 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_RSA_ENC].x509);
1789 #endif
1790 #ifndef OPENSSL_NO_ECDSA
1791 else if (alg_a & SSL_aECDSA)
1792 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S SL_PKEY_ECC].x509);
1793 #endif
1794 /* else anonymous ECDH, so no certificate or pkey. */
1795 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1796 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1797 ecdh=NULL;
1798 BN_CTX_free(bn_ctx);
1799 bn_ctx = NULL;
1800 EC_POINT_free(srvr_ecpoint);
1801 srvr_ecpoint = NULL;
1802 }
1803 else if (alg_k)
1804 {
1805 al=SSL_AD_UNEXPECTED_MESSAGE;
1806 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1807 goto f_err;
1808 }
1809 #endif /* !OPENSSL_NO_ECDH */
1810
1811
1812 /* p points to the next byte, there are 'n' bytes left */
1813
1814 /* if it was signed, check the signature */
1815 if (pkey != NULL)
1816 {
1817 if (TLS1_get_version(s) >= TLS1_2_VERSION)
1818 {
1819 int sigalg = tls12_get_sigid(pkey);
1820 /* Should never happen */
1821 if (sigalg == -1)
1822 {
1823 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNA L_ERROR);
1824 goto err;
1825 }
1826 /* Check key type is consistent with signature */
1827 if (sigalg != (int)p[1])
1828 {
1829 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_S IGNATURE_TYPE);
1830 al=SSL_AD_DECODE_ERROR;
1831 goto f_err;
1832 }
1833 md = tls12_get_hash(p[0]);
1834 if (md == NULL)
1835 {
1836 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNKNOWN _DIGEST);
1837 al=SSL_AD_DECODE_ERROR;
1838 goto f_err;
1839 }
1840 #ifdef SSL_DEBUG
1841 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1842 #endif
1843 p += 2;
1844 n -= 2;
1845 }
1846 else
1847 md = EVP_sha1();
1848
1849 n2s(p,i);
1850 n-=2;
1851 j=EVP_PKEY_size(pkey);
1852
1853 if ((i != n) || (n > j) || (n <= 0))
1854 {
1855 /* wrong packet length */
1856 al=SSL_AD_DECODE_ERROR;
1857 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE _LENGTH);
1858 goto f_err;
1859 }
1860
1861 #ifndef OPENSSL_NO_RSA
1862 if (pkey->type == EVP_PKEY_RSA && TLS1_get_version(s) < TLS1_2_V ERSION)
1863 {
1864 int num;
1865
1866 j=0;
1867 q=md_buf;
1868 for (num=2; num > 0; num--)
1869 {
1870 EVP_MD_CTX_set_flags(&md_ctx,
1871 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1872 EVP_DigestInit_ex(&md_ctx,(num == 2)
1873 ?s->ctx->md5:s->ctx->sha1, NULL);
1874 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[ 0]),SSL3_RANDOM_SIZE);
1875 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[ 0]),SSL3_RANDOM_SIZE);
1876 EVP_DigestUpdate(&md_ctx,param,param_len);
1877 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i) ;
1878 q+=i;
1879 j+=i;
1880 }
1881 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1882 pkey->pkey.rsa);
1883 if (i < 0)
1884 {
1885 al=SSL_AD_DECRYPT_ERROR;
1886 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA _DECRYPT);
1887 goto f_err;
1888 }
1889 if (i == 0)
1890 {
1891 /* bad signature */
1892 al=SSL_AD_DECRYPT_ERROR;
1893 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIG NATURE);
1894 goto f_err;
1895 }
1896 }
1897 else
1898 #endif
1899 {
1900 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1901 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3 _RANDOM_SIZE);
1902 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3 _RANDOM_SIZE);
1903 EVP_VerifyUpdate(&md_ctx,param,param_len);
1904 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1905 {
1906 /* bad signature */
1907 al=SSL_AD_DECRYPT_ERROR;
1908 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIG NATURE);
1909 goto f_err;
1910 }
1911 }
1912 }
1913 else
1914 {
1915 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1916 /* aNULL or kPSK do not need public keys */
1917 {
1918 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR) ;
1919 goto err;
1920 }
1921 /* still data left over */
1922 if (n != 0)
1923 {
1924 al=SSL_AD_DECODE_ERROR;
1925 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_M ESSAGE);
1926 goto f_err;
1927 }
1928 }
1929 EVP_PKEY_free(pkey);
1930 EVP_MD_CTX_cleanup(&md_ctx);
1931 return(1);
1932 f_err:
1933 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1934 err:
1935 EVP_PKEY_free(pkey);
1936 #ifndef OPENSSL_NO_RSA
1937 if (rsa != NULL)
1938 RSA_free(rsa);
1939 #endif
1940 #ifndef OPENSSL_NO_DH
1941 if (dh != NULL)
1942 DH_free(dh);
1943 #endif
1944 #ifndef OPENSSL_NO_ECDH
1945 BN_CTX_free(bn_ctx);
1946 EC_POINT_free(srvr_ecpoint);
1947 if (ecdh != NULL)
1948 EC_KEY_free(ecdh);
1949 #endif
1950 EVP_MD_CTX_cleanup(&md_ctx);
1951 return(-1);
1952 }
1953
1954 int ssl3_get_certificate_request(SSL *s)
1955 {
1956 int ok,ret=0;
1957 unsigned long n,nc,l;
1958 unsigned int llen, ctype_num,i;
1959 X509_NAME *xn=NULL;
1960 const unsigned char *p,*q;
1961 unsigned char *d;
1962 STACK_OF(X509_NAME) *ca_sk=NULL;
1963
1964 n=s->method->ssl_get_message(s,
1965 SSL3_ST_CR_CERT_REQ_A,
1966 SSL3_ST_CR_CERT_REQ_B,
1967 -1,
1968 s->max_cert_list,
1969 &ok);
1970
1971 if (!ok) return((int)n);
1972
1973 s->s3->tmp.cert_req=0;
1974
1975 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1976 {
1977 s->s3->tmp.reuse_message=1;
1978 /* If we get here we don't need any cached handshake records
1979 * as we wont be doing client auth.
1980 */
1981 if (s->s3->handshake_buffer)
1982 {
1983 if (!ssl3_digest_cached_records(s))
1984 goto err;
1985 }
1986 return(1);
1987 }
1988
1989 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1990 {
1991 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1992 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TY PE);
1993 goto err;
1994 }
1995
1996 /* TLS does not like anon-DH with client cert */
1997 if (s->version > SSL3_VERSION)
1998 {
1999 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
2000 {
2001 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAG E);
2002 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIE NT_CERT_REQ_WITH_ANON_CIPHER);
2003 goto err;
2004 }
2005 }
2006
2007 p=d=(unsigned char *)s->init_msg;
2008
2009 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
2010 {
2011 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
2012 goto err;
2013 }
2014
2015 /* get the certificate types */
2016 ctype_num= *(p++);
2017 if (ctype_num > SSL3_CT_NUMBER)
2018 ctype_num=SSL3_CT_NUMBER;
2019 for (i=0; i<ctype_num; i++)
2020 s->s3->tmp.ctype[i]= p[i];
2021 p+=ctype_num;
2022 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2023 {
2024 n2s(p, llen);
2025 /* Check we have enough room for signature algorithms and
2026 * following length value.
2027 */
2028 if ((unsigned long)(p - d + llen + 2) > n)
2029 {
2030 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2031 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_DATA_LEN GTH_TOO_LONG);
2032 goto err;
2033 }
2034 if ((llen & 1) || !tls1_process_sigalgs(s, p, llen))
2035 {
2036 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2037 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_SIGNATUR E_ALGORITHMS_ERROR);
2038 goto err;
2039 }
2040 p += llen;
2041 }
2042
2043 /* get the CA RDNs */
2044 n2s(p,llen);
2045 #if 0
2046 {
2047 FILE *out;
2048 out=fopen("/tmp/vsign.der","w");
2049 fwrite(p,1,llen,out);
2050 fclose(out);
2051 }
2052 #endif
2053
2054 if ((unsigned long)(p - d + llen) != n)
2055 {
2056 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2057 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH) ;
2058 goto err;
2059 }
2060
2061 for (nc=0; nc<llen; )
2062 {
2063 n2s(p,l);
2064 if ((l+nc+2) > llen)
2065 {
2066 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2067 goto cont; /* netscape bugs */
2068 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2069 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TO O_LONG);
2070 goto err;
2071 }
2072
2073 q=p;
2074
2075 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
2076 {
2077 /* If netscape tolerance is on, ignore errors */
2078 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2079 goto cont;
2080 else
2081 {
2082 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ER ROR);
2083 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ ASN1_LIB);
2084 goto err;
2085 }
2086 }
2087
2088 if (q != (p+l))
2089 {
2090 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2091 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LE NGTH_MISMATCH);
2092 goto err;
2093 }
2094 if (!sk_X509_NAME_push(ca_sk,xn))
2095 {
2096 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_F AILURE);
2097 goto err;
2098 }
2099
2100 p+=l;
2101 nc+=l+2;
2102 }
2103
2104 if (0)
2105 {
2106 cont:
2107 ERR_clear_error();
2108 }
2109
2110 /* we should setup a certificate to return.... */
2111 s->s3->tmp.cert_req=1;
2112 s->s3->tmp.ctype_num=ctype_num;
2113 if (s->s3->tmp.ca_names != NULL)
2114 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
2115 s->s3->tmp.ca_names=ca_sk;
2116 ca_sk=NULL;
2117
2118 ret=1;
2119 err:
2120 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
2121 return(ret);
2122 }
2123
2124 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
2125 {
2126 return(X509_NAME_cmp(*a,*b));
2127 }
2128 #ifndef OPENSSL_NO_TLSEXT
2129 int ssl3_get_new_session_ticket(SSL *s)
2130 {
2131 int ok,al,ret=0, ticklen;
2132 long n;
2133 const unsigned char *p;
2134 unsigned char *d;
2135
2136 n=s->method->ssl_get_message(s,
2137 SSL3_ST_CR_SESSION_TICKET_A,
2138 SSL3_ST_CR_SESSION_TICKET_B,
2139 -1,
2140 16384,
2141 &ok);
2142
2143 if (!ok)
2144 return((int)n);
2145
2146 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
2147 {
2148 s->s3->tmp.reuse_message=1;
2149 return(1);
2150 }
2151 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
2152 {
2153 al=SSL_AD_UNEXPECTED_MESSAGE;
2154 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE) ;
2155 goto f_err;
2156 }
2157 if (n < 6)
2158 {
2159 /* need at least ticket_lifetime_hint + ticket length */
2160 al = SSL_AD_DECODE_ERROR;
2161 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
2162 goto f_err;
2163 }
2164
2165 p=d=(unsigned char *)s->init_msg;
2166 n2l(p, s->session->tlsext_tick_lifetime_hint);
2167 n2s(p, ticklen);
2168 /* ticket_lifetime_hint + ticket_length + ticket */
2169 if (ticklen + 6 != n)
2170 {
2171 al = SSL_AD_DECODE_ERROR;
2172 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
2173 goto f_err;
2174 }
2175 if (s->session->tlsext_tick)
2176 {
2177 OPENSSL_free(s->session->tlsext_tick);
2178 s->session->tlsext_ticklen = 0;
2179 }
2180 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2181 if (!s->session->tlsext_tick)
2182 {
2183 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
2184 goto err;
2185 }
2186 memcpy(s->session->tlsext_tick, p, ticklen);
2187 s->session->tlsext_ticklen = ticklen;
2188 /* There are two ways to detect a resumed ticket sesion.
2189 * One is to set an appropriate session ID and then the server
2190 * must return a match in ServerHello. This allows the normal
2191 * client session ID matching to work and we know much
2192 * earlier that the ticket has been accepted.
2193 *
2194 * The other way is to set zero length session ID when the
2195 * ticket is presented and rely on the handshake to determine
2196 * session resumption.
2197 *
2198 * We choose the former approach because this fits in with
2199 * assumptions elsewhere in OpenSSL. The session ID is set
2200 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2201 * ticket.
2202 */
2203 EVP_Digest(p, ticklen,
2204 s->session->session_id, &s->session->session_id_length,
2205 #ifndef OPENSSL_NO_SHA256
2206 EVP_sha256(), NULL);
2207 #else
2208 EVP_sha1(), NULL);
2209 #endif
2210 ret=1;
2211 return(ret);
2212 f_err:
2213 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2214 err:
2215 return(-1);
2216 }
2217
2218 int ssl3_get_cert_status(SSL *s)
2219 {
2220 int ok, al;
2221 unsigned long resplen,n;
2222 const unsigned char *p;
2223
2224 n=s->method->ssl_get_message(s,
2225 SSL3_ST_CR_CERT_STATUS_A,
2226 SSL3_ST_CR_CERT_STATUS_B,
2227 SSL3_MT_CERTIFICATE_STATUS,
2228 16384,
2229 &ok);
2230
2231 if (!ok) return((int)n);
2232 if (n < 4)
2233 {
2234 /* need at least status type + length */
2235 al = SSL_AD_DECODE_ERROR;
2236 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
2237 goto f_err;
2238 }
2239 p = (unsigned char *)s->init_msg;
2240 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2241 {
2242 al = SSL_AD_DECODE_ERROR;
2243 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE) ;
2244 goto f_err;
2245 }
2246 n2l3(p, resplen);
2247 if (resplen + 4 != n)
2248 {
2249 al = SSL_AD_DECODE_ERROR;
2250 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
2251 goto f_err;
2252 }
2253 if (s->tlsext_ocsp_resp)
2254 OPENSSL_free(s->tlsext_ocsp_resp);
2255 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2256 if (!s->tlsext_ocsp_resp)
2257 {
2258 al = SSL_AD_INTERNAL_ERROR;
2259 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
2260 goto f_err;
2261 }
2262 s->tlsext_ocsp_resplen = resplen;
2263 if (s->ctx->tlsext_status_cb)
2264 {
2265 int ret;
2266 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2267 if (ret == 0)
2268 {
2269 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2270 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_R ESPONSE);
2271 goto f_err;
2272 }
2273 if (ret < 0)
2274 {
2275 al = SSL_AD_INTERNAL_ERROR;
2276 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
2277 goto f_err;
2278 }
2279 }
2280 return 1;
2281 f_err:
2282 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2283 return(-1);
2284 }
2285 #endif
2286
2287 int ssl3_get_server_done(SSL *s)
2288 {
2289 int ok,ret=0;
2290 long n;
2291
2292 n=s->method->ssl_get_message(s,
2293 SSL3_ST_CR_SRVR_DONE_A,
2294 SSL3_ST_CR_SRVR_DONE_B,
2295 SSL3_MT_SERVER_DONE,
2296 30, /* should be very small, like 0 :-) */
2297 &ok);
2298
2299 if (!ok) return((int)n);
2300 if (n > 0)
2301 {
2302 /* should contain no data */
2303 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2304 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
2305 return -1;
2306 }
2307 ret=1;
2308 return(ret);
2309 }
2310
2311
2312 int ssl3_send_client_key_exchange(SSL *s)
2313 {
2314 unsigned char *p,*d;
2315 int n;
2316 unsigned long alg_k;
2317 #ifndef OPENSSL_NO_RSA
2318 unsigned char *q;
2319 EVP_PKEY *pkey=NULL;
2320 #endif
2321 #ifndef OPENSSL_NO_KRB5
2322 KSSL_ERR kssl_err;
2323 #endif /* OPENSSL_NO_KRB5 */
2324 #ifndef OPENSSL_NO_ECDH
2325 EC_KEY *clnt_ecdh = NULL;
2326 const EC_POINT *srvr_ecpoint = NULL;
2327 EVP_PKEY *srvr_pub_pkey = NULL;
2328 unsigned char *encodedPoint = NULL;
2329 int encoded_pt_len = 0;
2330 BN_CTX * bn_ctx = NULL;
2331 #endif
2332
2333 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2334 {
2335 d=(unsigned char *)s->init_buf->data;
2336 p= &(d[4]);
2337
2338 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2339
2340 /* Fool emacs indentation */
2341 if (0) {}
2342 #ifndef OPENSSL_NO_RSA
2343 else if (alg_k & SSL_kRSA)
2344 {
2345 RSA *rsa;
2346 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2347
2348 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2349 rsa=s->session->sess_cert->peer_rsa_tmp;
2350 else
2351 {
2352 pkey=X509_get_pubkey(s->session->sess_cert->peer _pkeys[SSL_PKEY_RSA_ENC].x509);
2353 if ((pkey == NULL) ||
2354 (pkey->type != EVP_PKEY_RSA) ||
2355 (pkey->pkey.rsa == NULL))
2356 {
2357 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,ERR_R_INTERNAL_ERROR);
2358 goto err;
2359 }
2360 rsa=pkey->pkey.rsa;
2361 EVP_PKEY_free(pkey);
2362 }
2363
2364 tmp_buf[0]=s->client_version>>8;
2365 tmp_buf[1]=s->client_version&0xff;
2366 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2367 goto err;
2368
2369 s->session->master_key_length=sizeof tmp_buf;
2370
2371 q=p;
2372 /* Fix buf for TLS and beyond */
2373 if (s->version > SSL3_VERSION)
2374 p+=2;
2375 n=RSA_public_encrypt(sizeof tmp_buf,
2376 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2377 #ifdef PKCS1_CHECK
2378 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2379 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2380 #endif
2381 if (n <= 0)
2382 {
2383 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R _BAD_RSA_ENCRYPT);
2384 goto err;
2385 }
2386
2387 /* Fix buf for TLS and beyond */
2388 if (s->version > SSL3_VERSION)
2389 {
2390 s2n(n,q);
2391 n+=2;
2392 }
2393
2394 s->session->master_key_length=
2395 s->method->ssl3_enc->generate_master_secret(s,
2396 s->session->master_key,
2397 tmp_buf,sizeof tmp_buf);
2398 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2399 }
2400 #endif
2401 #ifndef OPENSSL_NO_KRB5
2402 else if (alg_k & SSL_kKRB5)
2403 {
2404 krb5_error_code krb5rc;
2405 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2406 /* krb5_data krb5_ap_req; */
2407 krb5_data *enc_ticket;
2408 krb5_data authenticator, *authp = NULL;
2409 EVP_CIPHER_CTX ciph_ctx;
2410 const EVP_CIPHER *enc = NULL;
2411 unsigned char iv[EVP_MAX_IV_LENGTH];
2412 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2413 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2414 + EVP_MAX_IV_LENGTH];
2415 int padl, outl = sizeof(epms);
2416
2417 EVP_CIPHER_CTX_init(&ciph_ctx);
2418
2419 #ifdef KSSL_DEBUG
2420 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2421 alg_k, SSL_kKRB5);
2422 #endif /* KSSL_DEBUG */
2423
2424 authp = NULL;
2425 #ifdef KRB5SENDAUTH
2426 if (KRB5SENDAUTH) authp = &authenticator;
2427 #endif /* KRB5SENDAUTH */
2428
2429 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2430 &kssl_err);
2431 enc = kssl_map_enc(kssl_ctx->enctype);
2432 if (enc == NULL)
2433 goto err;
2434 #ifdef KSSL_DEBUG
2435 {
2436 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2437 if (krb5rc && kssl_err.text)
2438 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2439 }
2440 #endif /* KSSL_DEBUG */
2441
2442 if (krb5rc)
2443 {
2444 ssl3_send_alert(s,SSL3_AL_FATAL,
2445 SSL_AD_HANDSHAKE_FAILURE);
2446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2447 kssl_err.reason);
2448 goto err;
2449 }
2450
2451 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2452 ** in place of RFC 2712 KerberosWrapper, as in:
2453 **
2454 ** Send ticket (copy to *p, set n = length)
2455 ** n = krb5_ap_req.length;
2456 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2457 ** if (krb5_ap_req.data)
2458 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2459 **
2460 ** Now using real RFC 2712 KerberosWrapper
2461 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2462 ** Note: 2712 "opaque" types are here replaced
2463 ** with a 2-byte length followed by the value.
2464 ** Example:
2465 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2466 ** Where "xx xx" = length bytes. Shown here with
2467 ** optional authenticator omitted.
2468 */
2469
2470 /* KerberosWrapper.Ticket */
2471 s2n(enc_ticket->length,p);
2472 memcpy(p, enc_ticket->data, enc_ticket->length);
2473 p+= enc_ticket->length;
2474 n = enc_ticket->length + 2;
2475
2476 /* KerberosWrapper.Authenticator */
2477 if (authp && authp->length)
2478 {
2479 s2n(authp->length,p);
2480 memcpy(p, authp->data, authp->length);
2481 p+= authp->length;
2482 n+= authp->length + 2;
2483
2484 free(authp->data);
2485 authp->data = NULL;
2486 authp->length = 0;
2487 }
2488 else
2489 {
2490 s2n(0,p);/* null authenticator length */
2491 n+=2;
2492 }
2493
2494 tmp_buf[0]=s->client_version>>8;
2495 tmp_buf[1]=s->client_version&0xff;
2496 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2497 goto err;
2498
2499 /* 20010420 VRS. Tried it this way; failed.
2500 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2501 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2502 ** kssl_ctx->length);
2503 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2504 */
2505
2506 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2507 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2508 kssl_ctx->key,iv);
2509 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2510 sizeof tmp_buf);
2511 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2512 outl += padl;
2513 if (outl > (int)sizeof epms)
2514 {
2515 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_ R_INTERNAL_ERROR);
2516 goto err;
2517 }
2518 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2519
2520 /* KerberosWrapper.EncryptedPreMasterSecret */
2521 s2n(outl,p);
2522 memcpy(p, epms, outl);
2523 p+=outl;
2524 n+=outl + 2;
2525
2526 s->session->master_key_length=
2527 s->method->ssl3_enc->generate_master_secret(s,
2528 s->session->master_key,
2529 tmp_buf, sizeof tmp_buf);
2530
2531 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2532 OPENSSL_cleanse(epms, outl);
2533 }
2534 #endif
2535 #ifndef OPENSSL_NO_DH
2536 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2537 {
2538 DH *dh_srvr,*dh_clnt;
2539
2540 if (s->session->sess_cert == NULL)
2541 {
2542 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTE D_MESSAGE);
2543 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R _UNEXPECTED_MESSAGE);
2544 goto err;
2545 }
2546
2547 if (s->session->sess_cert->peer_dh_tmp != NULL)
2548 dh_srvr=s->session->sess_cert->peer_dh_tmp;
2549 else
2550 {
2551 /* we get them from the cert */
2552 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE _FAILURE);
2553 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R _UNABLE_TO_FIND_DH_PARAMETERS);
2554 goto err;
2555 }
2556
2557 /* generate a new random key */
2558 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2559 {
2560 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _DH_LIB);
2561 goto err;
2562 }
2563 if (!DH_generate_key(dh_clnt))
2564 {
2565 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _DH_LIB);
2566 DH_free(dh_clnt);
2567 goto err;
2568 }
2569
2570 /* use the 'p' output buffer for the DH key, but
2571 * make sure to clear it out afterwards */
2572
2573 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2574
2575 if (n <= 0)
2576 {
2577 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _DH_LIB);
2578 DH_free(dh_clnt);
2579 goto err;
2580 }
2581
2582 /* generate master key from the result */
2583 s->session->master_key_length=
2584 s->method->ssl3_enc->generate_master_secret(s,
2585 s->session->master_key,p,n);
2586 /* clean up */
2587 memset(p,0,n);
2588
2589 /* send off the data */
2590 n=BN_num_bytes(dh_clnt->pub_key);
2591 s2n(n,p);
2592 BN_bn2bin(dh_clnt->pub_key,p);
2593 n+=2;
2594
2595 DH_free(dh_clnt);
2596
2597 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2598 }
2599 #endif
2600
2601 #ifndef OPENSSL_NO_ECDH
2602 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2603 {
2604 const EC_GROUP *srvr_group = NULL;
2605 EC_KEY *tkey;
2606 int ecdh_clnt_cert = 0;
2607 int field_size = 0;
2608
2609 if (s->session->sess_cert == NULL)
2610 {
2611 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTE D_MESSAGE);
2612 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R _UNEXPECTED_MESSAGE);
2613 goto err;
2614 }
2615
2616 /* Did we send out the client's
2617 * ECDH share for use in premaster
2618 * computation as part of client certificate?
2619 * If so, set ecdh_clnt_cert to 1.
2620 */
2621 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NUL L))
2622 {
2623 /* XXX: For now, we do not support client
2624 * authentication using ECDH certificates.
2625 * To add such support, one needs to add
2626 * code that checks for appropriate
2627 * conditions and sets ecdh_clnt_cert to 1.
2628 * For example, the cert have an ECC
2629 * key on the same curve as the server's
2630 * and the key should be authorized for
2631 * key agreement.
2632 *
2633 * One also needs to add code in ssl3_connect
2634 * to skip sending the certificate verify
2635 * message.
2636 *
2637 * if ((s->cert->key->privatekey != NULL) &&
2638 * (s->cert->key->privatekey->type ==
2639 * EVP_PKEY_EC) && ...)
2640 * ecdh_clnt_cert = 1;
2641 */
2642 }
2643
2644 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2645 {
2646 tkey = s->session->sess_cert->peer_ecdh_tmp;
2647 }
2648 else
2649 {
2650 /* Get the Server Public Key from Cert */
2651 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2652 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2653 if ((srvr_pub_pkey == NULL) ||
2654 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2655 (srvr_pub_pkey->pkey.ec == NULL))
2656 {
2657 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,
2658 ERR_R_INTERNAL_ERROR);
2659 goto err;
2660 }
2661
2662 tkey = srvr_pub_pkey->pkey.ec;
2663 }
2664
2665 srvr_group = EC_KEY_get0_group(tkey);
2666 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2667
2668 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2669 {
2670 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2671 ERR_R_INTERNAL_ERROR);
2672 goto err;
2673 }
2674
2675 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2676 {
2677 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _MALLOC_FAILURE);
2678 goto err;
2679 }
2680
2681 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2682 {
2683 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _EC_LIB);
2684 goto err;
2685 }
2686 if (ecdh_clnt_cert)
2687 {
2688 /* Reuse key info from our certificate
2689 * We only need our private key to perform
2690 * the ECDH computation.
2691 */
2692 const BIGNUM *priv_key;
2693 tkey = s->cert->key->privatekey->pkey.ec;
2694 priv_key = EC_KEY_get0_private_key(tkey);
2695 if (priv_key == NULL)
2696 {
2697 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,ERR_R_MALLOC_FAILURE);
2698 goto err;
2699 }
2700 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key) )
2701 {
2702 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,ERR_R_EC_LIB);
2703 goto err;
2704 }
2705 }
2706 else
2707 {
2708 /* Generate a new ECDH key pair */
2709 if (!(EC_KEY_generate_key(clnt_ecdh)))
2710 {
2711 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE, ERR_R_ECDH_LIB);
2712 goto err;
2713 }
2714 }
2715
2716 /* use the 'p' output buffer for the ECDH key, but
2717 * make sure to clear it out afterwards
2718 */
2719
2720 field_size = EC_GROUP_get_degree(srvr_group);
2721 if (field_size <= 0)
2722 {
2723 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2724 ERR_R_ECDH_LIB);
2725 goto err;
2726 }
2727 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, cl nt_ecdh, NULL);
2728 if (n <= 0)
2729 {
2730 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2731 ERR_R_ECDH_LIB);
2732 goto err;
2733 }
2734
2735 /* generate master key from the result */
2736 s->session->master_key_length = s->method->ssl3_enc \
2737 -> generate_master_secret(s,
2738 s->session->master_key,
2739 p, n);
2740
2741 memset(p, 0, n); /* clean up */
2742
2743 if (ecdh_clnt_cert)
2744 {
2745 /* Send empty client key exch message */
2746 n = 0;
2747 }
2748 else
2749 {
2750 /* First check the size of encoding and
2751 * allocate memory accordingly.
2752 */
2753 encoded_pt_len =
2754 EC_POINT_point2oct(srvr_group,
2755 EC_KEY_get0_public_key(clnt_ecdh),
2756 POINT_CONVERSION_UNCOMPRESSED,
2757 NULL, 0, NULL);
2758
2759 encodedPoint = (unsigned char *)
2760 OPENSSL_malloc(encoded_pt_len *
2761 sizeof(unsigned char));
2762 bn_ctx = BN_CTX_new();
2763 if ((encodedPoint == NULL) ||
2764 (bn_ctx == NULL))
2765 {
2766 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,ERR_R_MALLOC_FAILURE);
2767 goto err;
2768 }
2769
2770 /* Encode the public key */
2771 n = EC_POINT_point2oct(srvr_group,
2772 EC_KEY_get0_public_key(clnt_ecdh),
2773 POINT_CONVERSION_UNCOMPRESSED,
2774 encodedPoint, encoded_pt_len, bn_ctx);
2775
2776 *p = n; /* length of encoded point */
2777 /* Encoded point will be copied here */
2778 p += 1;
2779 /* copy the point */
2780 memcpy((unsigned char *)p, encodedPoint, n);
2781 /* increment n to account for length field */
2782 n += 1;
2783 }
2784
2785 /* Free allocated memory */
2786 BN_CTX_free(bn_ctx);
2787 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2788 if (clnt_ecdh != NULL)
2789 EC_KEY_free(clnt_ecdh);
2790 EVP_PKEY_free(srvr_pub_pkey);
2791 }
2792 #endif /* !OPENSSL_NO_ECDH */
2793 else if (alg_k & SSL_kGOST)
2794 {
2795 /* GOST key exchange message creation */
2796 EVP_PKEY_CTX *pkey_ctx;
2797 X509 *peer_cert;
2798 size_t msglen;
2799 unsigned int md_len;
2800 int keytype;
2801 unsigned char premaster_secret[32],shared_ukm[32], tmp[2 56];
2802 EVP_MD_CTX *ukm_hash;
2803 EVP_PKEY *pub_key;
2804
2805 /* Get server sertificate PKEY and create ctx from it */
2806 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL _PKEY_GOST01)].x509;
2807 if (!peer_cert)
2808 peer_cert=s->session->sess_cert->peer_pkeys[(key type=SSL_PKEY_GOST94)].x509;
2809 if (!peer_cert) {
2810 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2811 goto err;
2812 }
2813
2814 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_c ert),NULL);
2815 /* If we have send a certificate, and certificate key
2816
2817 * parameters match those of server certificate, use
2818 * certificate key for key exchange
2819 */
2820
2821 /* Otherwise, generate ephemeral key pair */
2822
2823 EVP_PKEY_encrypt_init(pkey_ctx);
2824 /* Generate session key */
2825 RAND_bytes(premaster_secret,32);
2826 /* If we have client certificate, use its secret as peer key */
2827 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2828 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->k ey->privatekey) <=0) {
2829 /* If there was an error - just ignore i t. Ephemeral key
2830 * would be used
2831 */
2832 ERR_clear_error();
2833 }
2834 }
2835 /* Compute shared IV and store it in algorithm-specific
2836 * context data */
2837 ukm_hash = EVP_MD_CTX_create();
2838 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR 3411_94));
2839 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RAND OM_SIZE);
2840 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RAND OM_SIZE);
2841 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2842 EVP_MD_CTX_destroy(ukm_hash);
2843 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EV P_PKEY_CTRL_SET_IV,
2844 8,shared_ukm)<0) {
2845 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN GE,
2846 SSL_R_LIBRARY_BUG);
2847 goto err;
2848 }
2849 /* Make GOST keytransport blob message */
2850 /*Encapsulate it into sequence */
2851 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2852 msglen=255;
2853 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secr et,32)<0) {
2854 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2855 SSL_R_LIBRARY_BUG);
2856 goto err;
2857 }
2858 if (msglen >= 0x80)
2859 {
2860 *(p++)=0x81;
2861 *(p++)= msglen & 0xff;
2862 n=msglen+3;
2863 }
2864 else
2865 {
2866 *(p++)= msglen & 0xff;
2867 n=msglen+2;
2868 }
2869 memcpy(p, tmp, msglen);
2870 /* Check if pubkey from client certificate was used */
2871 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PE ER_KEY, 2, NULL) > 0)
2872 {
2873 /* Set flag "skip certificate verify" */
2874 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2875 }
2876 EVP_PKEY_CTX_free(pkey_ctx);
2877 s->session->master_key_length=
2878 s->method->ssl3_enc->generate_master_secret(s,
2879 s->session->master_key,premaster_secret, 32);
2880 EVP_PKEY_free(pub_key);
2881
2882 }
2883 #ifndef OPENSSL_NO_SRP
2884 else if (alg_k & SSL_kSRP)
2885 {
2886 if (s->srp_ctx.A != NULL)
2887 {
2888 /* send off the data */
2889 n=BN_num_bytes(s->srp_ctx.A);
2890 s2n(n,p);
2891 BN_bn2bin(s->srp_ctx.A,p);
2892 n+=2;
2893 }
2894 else
2895 {
2896 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _INTERNAL_ERROR);
2897 goto err;
2898 }
2899 if (s->session->srp_username != NULL)
2900 OPENSSL_free(s->session->srp_username);
2901 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2902 if (s->session->srp_username == NULL)
2903 {
2904 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2905 ERR_R_MALLOC_FAILURE);
2906 goto err;
2907 }
2908
2909 if ((s->session->master_key_length = SRP_generate_client _master_secret(s,s->session->master_key))<0)
2910 {
2911 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R _INTERNAL_ERROR);
2912 goto err;
2913 }
2914 }
2915 #endif
2916 #ifndef OPENSSL_NO_PSK
2917 else if (alg_k & SSL_kPSK)
2918 {
2919 char identity[PSK_MAX_IDENTITY_LEN];
2920 unsigned char *t = NULL;
2921 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2922 unsigned int pre_ms_len = 0, psk_len = 0;
2923 int psk_err = 1;
2924
2925 n = 0;
2926 if (s->psk_client_callback == NULL)
2927 {
2928 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2929 SSL_R_PSK_NO_CLIENT_CB);
2930 goto err;
2931 }
2932
2933 psk_len = s->psk_client_callback(s, s->ctx->psk_identity _hint,
2934 identity, PSK_MAX_IDENTITY_LEN,
2935 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2936 if (psk_len > PSK_MAX_PSK_LEN)
2937 {
2938 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2939 ERR_R_INTERNAL_ERROR);
2940 goto psk_err;
2941 }
2942 else if (psk_len == 0)
2943 {
2944 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2945 SSL_R_PSK_IDENTITY_NOT_FOUND);
2946 goto psk_err;
2947 }
2948
2949 /* create PSK pre_master_secret */
2950 pre_ms_len = 2+psk_len+2+psk_len;
2951 t = psk_or_pre_ms;
2952 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len) ;
2953 s2n(psk_len, t);
2954 memset(t, 0, psk_len);
2955 t+=psk_len;
2956 s2n(psk_len, t);
2957
2958 if (s->session->psk_identity_hint != NULL)
2959 OPENSSL_free(s->session->psk_identity_hint);
2960 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_i dentity_hint);
2961 if (s->ctx->psk_identity_hint != NULL &&
2962 s->session->psk_identity_hint == NULL)
2963 {
2964 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2965 ERR_R_MALLOC_FAILURE);
2966 goto psk_err;
2967 }
2968
2969 if (s->session->psk_identity != NULL)
2970 OPENSSL_free(s->session->psk_identity);
2971 s->session->psk_identity = BUF_strdup(identity);
2972 if (s->session->psk_identity == NULL)
2973 {
2974 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2975 ERR_R_MALLOC_FAILURE);
2976 goto psk_err;
2977 }
2978
2979 s->session->master_key_length =
2980 s->method->ssl3_enc->generate_master_secret(s,
2981 s->session->master_key,
2982 psk_or_pre_ms, pre_ms_len);
2983 n = strlen(identity);
2984 s2n(n, p);
2985 memcpy(p, identity, n);
2986 n+=2;
2987 psk_err = 0;
2988 psk_err:
2989 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2990 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2991 if (psk_err != 0)
2992 {
2993 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHA KE_FAILURE);
2994 goto err;
2995 }
2996 }
2997 #endif
2998 else
2999 {
3000 ssl3_send_alert(s, SSL3_AL_FATAL,
3001 SSL_AD_HANDSHAKE_FAILURE);
3002 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3003 ERR_R_INTERNAL_ERROR);
3004 goto err;
3005 }
3006
3007 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
3008 l2n3(n,d);
3009
3010 s->state=SSL3_ST_CW_KEY_EXCH_B;
3011 /* number of bytes to write */
3012 s->init_num=n+4;
3013 s->init_off=0;
3014 }
3015
3016 /* SSL3_ST_CW_KEY_EXCH_B */
3017 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
3018 err:
3019 #ifndef OPENSSL_NO_ECDH
3020 BN_CTX_free(bn_ctx);
3021 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
3022 if (clnt_ecdh != NULL)
3023 EC_KEY_free(clnt_ecdh);
3024 EVP_PKEY_free(srvr_pub_pkey);
3025 #endif
3026 return(-1);
3027 }
3028
3029 int ssl3_send_client_verify(SSL *s)
3030 {
3031 unsigned char *p,*d;
3032 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
3033 EVP_PKEY *pkey;
3034 EVP_PKEY_CTX *pctx = NULL;
3035 EVP_MD_CTX mctx;
3036 unsigned signature_length = 0;
3037 unsigned long n;
3038
3039 EVP_MD_CTX_init(&mctx);
3040
3041 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
3042 {
3043 d = (unsigned char *)s->init_buf->data;
3044 p = &(d[4]);
3045 pkey = s->cert->key->privatekey;
3046 /* For TLS v1.2 send signature algorithm and signature
3047 * using agreed digest and cached handshake records.
3048 */
3049 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3050 {
3051 long hdatalen = 0;
3052 void *hdata;
3053 const EVP_MD *md = s->cert->key->digest;
3054 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
3055 &hdata);
3056 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
3057 {
3058 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3059 ERR_R_INTERNAL_ERROR);
3060 goto err;
3061 }
3062 p += 2;
3063 #ifdef SSL_DEBUG
3064 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3065 EVP_MD_name(md));
3066 #endif
3067 if (!EVP_SignInit_ex(&mctx, md, NULL)
3068 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3069 || !EVP_SignFinal(&mctx, p + 2,
3070 &signature_length, pkey))
3071 {
3072 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3073 ERR_R_EVP_LIB);
3074 goto err;
3075 }
3076 s2n(signature_length, p);
3077 n = signature_length + 4;
3078 if (!ssl3_digest_cached_records(s))
3079 goto err;
3080 }
3081 else
3082 #ifndef OPENSSL_NO_RSA
3083 if (pkey->type == EVP_PKEY_RSA)
3084 {
3085 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, data);
3086 s->method->ssl3_enc->cert_verify_mac(s,
3087 NID_sha1, &(data[MD5_DIGEST_LENGTH]));
3088 if (RSA_sign(NID_md5_sha1, data,
3089 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3090 &(p[2]), &signature_length, pkey->pkey.r sa) <= 0)
3091 {
3092 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_ LIB);
3093 goto err;
3094 }
3095 s2n(signature_length, p);
3096 n = signature_length + 2;
3097 }
3098 else
3099 #endif
3100 #ifndef OPENSSL_NO_DSA
3101 if (pkey->type == EVP_PKEY_DSA)
3102 {
3103 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, data);
3104 if (!DSA_sign(pkey->save_type, data,
3105 SHA_DIGEST_LENGTH, &(p[2]),
3106 &signature_length, pkey->pkey.dsa))
3107 {
3108 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_ LIB);
3109 goto err;
3110 }
3111 s2n(signature_length, p);
3112 n = signature_length + 2;
3113 }
3114 else
3115 #endif
3116 #ifndef OPENSSL_NO_ECDSA
3117 if (pkey->type == EVP_PKEY_EC)
3118 {
3119 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, data);
3120 if (!ECDSA_sign(pkey->save_type, data,
3121 SHA_DIGEST_LENGTH, &(p[2]),
3122 &signature_length, pkey->pkey.ec))
3123 {
3124 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDS A_LIB);
3125 goto err;
3126 }
3127 s2n(signature_length, p);
3128 n = signature_length + 2;
3129 }
3130 else
3131 #endif
3132 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_Go stR3410_2001)
3133 {
3134 unsigned char signbuf[64];
3135 int i, j;
3136 size_t sigsize=64;
3137
3138 s->method->ssl3_enc->cert_verify_mac(s,
3139 NID_id_GostR3411_94,
3140 data);
3141 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3142 EVP_PKEY_sign_init(pctx);
3143 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3144 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3145 ERR_R_INTERNAL_ERROR);
3146 goto err;
3147 }
3148 for (i=63,j=0; i>=0; j++, i--) {
3149 p[2+j]=signbuf[i];
3150 }
3151 s2n(j,p);
3152 n=j+2;
3153 }
3154 else
3155 {
3156 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERRO R);
3157 goto err;
3158 }
3159 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
3160 l2n3(n,d);
3161
3162 s->state=SSL3_ST_CW_CERT_VRFY_B;
3163 s->init_num=(int)n+4;
3164 s->init_off=0;
3165 }
3166 EVP_MD_CTX_cleanup(&mctx);
3167 EVP_PKEY_CTX_free(pctx);
3168 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
3169 err:
3170 EVP_MD_CTX_cleanup(&mctx);
3171 EVP_PKEY_CTX_free(pctx);
3172 return(-1);
3173 }
3174
3175 int ssl3_send_client_certificate(SSL *s)
3176 {
3177 X509 *x509=NULL;
3178 EVP_PKEY *pkey=NULL;
3179 int i;
3180 unsigned long l;
3181
3182 if (s->state == SSL3_ST_CW_CERT_A)
3183 {
3184 if ((s->cert == NULL) ||
3185 (s->cert->key->x509 == NULL) ||
3186 (s->cert->key->privatekey == NULL))
3187 s->state=SSL3_ST_CW_CERT_B;
3188 else
3189 s->state=SSL3_ST_CW_CERT_C;
3190 }
3191
3192 /* We need to get a client cert */
3193 if (s->state == SSL3_ST_CW_CERT_B)
3194 {
3195 /* If we get an error, we need to
3196 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
3197 * We then get retied later */
3198 i=0;
3199 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3200 if (i < 0)
3201 {
3202 s->rwstate=SSL_X509_LOOKUP;
3203 return(-1);
3204 }
3205 s->rwstate=SSL_NOTHING;
3206 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
3207 {
3208 s->state=SSL3_ST_CW_CERT_B;
3209 if ( !SSL_use_certificate(s,x509) ||
3210 !SSL_use_PrivateKey(s,pkey))
3211 i=0;
3212 }
3213 else if (i == 1)
3214 {
3215 i=0;
3216 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA _RETURNED_BY_CALLBACK);
3217 }
3218
3219 if (x509 != NULL) X509_free(x509);
3220 if (pkey != NULL) EVP_PKEY_free(pkey);
3221 if (i == 0)
3222 {
3223 if (s->version == SSL3_VERSION)
3224 {
3225 s->s3->tmp.cert_req=0;
3226 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERT IFICATE);
3227 return(1);
3228 }
3229 else
3230 {
3231 s->s3->tmp.cert_req=2;
3232 }
3233 }
3234
3235 /* Ok, we have a cert */
3236 s->state=SSL3_ST_CW_CERT_C;
3237 }
3238
3239 if (s->state == SSL3_ST_CW_CERT_C)
3240 {
3241 s->state=SSL3_ST_CW_CERT_D;
3242 l=ssl3_output_cert_chain(s,
3243 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
3244 s->init_num=(int)l;
3245 s->init_off=0;
3246 }
3247 /* SSL3_ST_CW_CERT_D */
3248 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
3249 }
3250
3251 #define has_bits(i,m) (((i)&(m)) == (m))
3252
3253 int ssl3_check_cert_and_algorithm(SSL *s)
3254 {
3255 int i,idx;
3256 long alg_k,alg_a;
3257 EVP_PKEY *pkey=NULL;
3258 SESS_CERT *sc;
3259 #ifndef OPENSSL_NO_RSA
3260 RSA *rsa;
3261 #endif
3262 #ifndef OPENSSL_NO_DH
3263 DH *dh;
3264 #endif
3265
3266 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
3267 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
3268
3269 /* we don't have a certificate */
3270 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
3271 return(1);
3272
3273 sc=s->session->sess_cert;
3274 if (sc == NULL)
3275 {
3276 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR) ;
3277 goto err;
3278 }
3279
3280 #ifndef OPENSSL_NO_RSA
3281 rsa=s->session->sess_cert->peer_rsa_tmp;
3282 #endif
3283 #ifndef OPENSSL_NO_DH
3284 dh=s->session->sess_cert->peer_dh_tmp;
3285 #endif
3286
3287 /* This is the passed certificate */
3288
3289 idx=sc->peer_cert_type;
3290 #ifndef OPENSSL_NO_ECDH
3291 if (idx == SSL_PKEY_ECC)
3292 {
3293 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
3294 s) == 0)
3295 { /* check failed */
3296 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC _CERT);
3297 goto f_err;
3298 }
3299 else
3300 {
3301 return 1;
3302 }
3303 }
3304 #endif
3305 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
3306 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
3307 EVP_PKEY_free(pkey);
3308
3309
3310 /* Check that we have a certificate if we require one */
3311 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
3312 {
3313 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIG NING_CERT);
3314 goto f_err;
3315 }
3316 #ifndef OPENSSL_NO_DSA
3317 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
3318 {
3319 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIG NING_CERT);
3320 goto f_err;
3321 }
3322 #endif
3323 #ifndef OPENSSL_NO_RSA
3324 if ((alg_k & SSL_kRSA) &&
3325 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
3326 {
3327 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENC RYPTING_CERT);
3328 goto f_err;
3329 }
3330 #endif
3331 #ifndef OPENSSL_NO_DH
3332 if ((alg_k & SSL_kEDH) &&
3333 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
3334 {
3335 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY) ;
3336 goto f_err;
3337 }
3338 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
3339 {
3340 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_ CERT);
3341 goto f_err;
3342 }
3343 #ifndef OPENSSL_NO_DSA
3344 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
3345 {
3346 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_ CERT);
3347 goto f_err;
3348 }
3349 #endif
3350 #endif
3351
3352 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
3353 {
3354 #ifndef OPENSSL_NO_RSA
3355 if (alg_k & SSL_kRSA)
3356 {
3357 if (rsa == NULL
3358 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3-> tmp.new_cipher))
3359 {
3360 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R _MISSING_EXPORT_TMP_RSA_KEY);
3361 goto f_err;
3362 }
3363 }
3364 else
3365 #endif
3366 #ifndef OPENSSL_NO_DH
3367 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
3368 {
3369 if (dh == NULL
3370 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3 ->tmp.new_cipher))
3371 {
3372 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R _MISSING_EXPORT_TMP_DH_KEY);
3373 goto f_err;
3374 }
3375 }
3376 else
3377 #endif
3378 {
3379 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN _KEY_EXCHANGE_TYPE);
3380 goto f_err;
3381 }
3382 }
3383 return(1);
3384 f_err:
3385 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3386 err:
3387 return(0);
3388 }
3389
3390 #if !defined(OPENSSL_NO_TLSEXT)
3391 # if !defined(OPENSSL_NO_NEXTPROTONEG)
3392 int ssl3_send_next_proto(SSL *s)
3393 {
3394 unsigned int len, padding_len;
3395 unsigned char *d;
3396
3397 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3398 {
3399 len = s->next_proto_negotiated_len;
3400 padding_len = 32 - ((len + 2) % 32);
3401 d = (unsigned char *)s->init_buf->data;
3402 d[4] = len;
3403 memcpy(d + 5, s->next_proto_negotiated, len);
3404 d[5 + len] = padding_len;
3405 memset(d + 6 + len, 0, padding_len);
3406 *(d++)=SSL3_MT_NEXT_PROTO;
3407 l2n3(2 + len + padding_len, d);
3408 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3409 s->init_num = 4 + 2 + len + padding_len;
3410 s->init_off = 0;
3411 }
3412
3413 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3414 }
3415 # endif /* !OPENSSL_NO_NEXTPROTONEG */
3416
3417 int ssl3_send_channel_id(SSL *s)
3418 {
3419 unsigned char *d;
3420 int ret = -1, public_key_len;
3421 EVP_MD_CTX md_ctx;
3422 size_t sig_len;
3423 ECDSA_SIG *sig = NULL;
3424 unsigned char *public_key = NULL, *derp, *der_sig = NULL;
3425
3426 if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
3427 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3428
3429 if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
3430 {
3431 EVP_PKEY *key = NULL;
3432 s->ctx->channel_id_cb(s, &key);
3433 if (key != NULL)
3434 {
3435 s->tlsext_channel_id_private = key;
3436 }
3437 }
3438 if (!s->tlsext_channel_id_private)
3439 {
3440 s->rwstate=SSL_CHANNEL_ID_LOOKUP;
3441 return (-1);
3442 }
3443 s->rwstate=SSL_NOTHING;
3444
3445 d = (unsigned char *)s->init_buf->data;
3446 *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
3447 l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
3448 s2n(TLSEXT_TYPE_channel_id, d);
3449 s2n(TLSEXT_CHANNEL_ID_SIZE, d);
3450
3451 EVP_MD_CTX_init(&md_ctx);
3452
3453 public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
3454 if (public_key_len <= 0)
3455 {
3456 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_CANNOT_SERIALIZE_PUBLIC_ KEY);
3457 goto err;
3458 }
3459 // i2d_PublicKey will produce an ANSI X9.62 public key which, for a
3460 // P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
3461 // field elements as 32-byte, big-endian numbers.
3462 if (public_key_len != 65)
3463 {
3464 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_CHANNEL_ID_NOT_P256);
3465 goto err;
3466 }
3467 public_key = OPENSSL_malloc(public_key_len);
3468 if (!public_key)
3469 {
3470 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,ERR_R_MALLOC_FAILURE);
3471 goto err;
3472 }
3473
3474 derp = public_key;
3475 i2d_PublicKey(s->tlsext_channel_id_private, &derp);
3476
3477 if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
3478 s->tlsext_channel_id_private) != 1)
3479 {
3480 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_EVP_DIGESTSIGNINIT_FAILE D);
3481 goto err;
3482 }
3483
3484 if (!tls1_channel_id_hash(&md_ctx, s))
3485 goto err;
3486
3487 if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
3488 {
3489 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_EVP_DIGESTSIGNFINAL_FAIL ED);
3490 goto err;
3491 }
3492
3493 der_sig = OPENSSL_malloc(sig_len);
3494 if (!der_sig)
3495 {
3496 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,ERR_R_MALLOC_FAILURE);
3497 goto err;
3498 }
3499
3500 if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
3501 {
3502 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_EVP_DIGESTSIGNFINAL_FAIL ED);
3503 goto err;
3504 }
3505
3506 derp = der_sig;
3507 sig = d2i_ECDSA_SIG(NULL, (const unsigned char**)&derp, sig_len);
3508 if (sig == NULL)
3509 {
3510 SSLerr(SSL_F_SSL3_SEND_CHANNEL_ID,SSL_R_D2I_ECDSA_SIG);
3511 goto err;
3512 }
3513
3514 // The first byte of public_key will be 0x4, denoting an uncompressed ke y.
3515 memcpy(d, public_key + 1, 64);
3516 d += 64;
3517 memset(d, 0, 2 * 32);
3518 BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
3519 d += 32;
3520 BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
3521 d += 32;
3522
3523 s->state = SSL3_ST_CW_CHANNEL_ID_B;
3524 s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
3525 s->init_off = 0;
3526
3527 ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3528
3529 err:
3530 EVP_MD_CTX_cleanup(&md_ctx);
3531 if (public_key)
3532 OPENSSL_free(public_key);
3533 if (der_sig)
3534 OPENSSL_free(der_sig);
3535 if (sig)
3536 ECDSA_SIG_free(sig);
3537
3538 return ret;
3539 }
3540 #endif /* !OPENSSL_NO_TLSEXT */
3541
3542 /* Check to see if handshake is full or resumed. Usually this is just a
3543 * case of checking to see if a cache hit has occurred. In the case of
3544 * session tickets we have to check the next message to be sure.
3545 */
3546
3547 #ifndef OPENSSL_NO_TLSEXT
3548 int ssl3_check_finished(SSL *s)
3549 {
3550 int ok;
3551 long n;
3552 /* If we have no ticket it cannot be a resumed session. */
3553 if (!s->session->tlsext_tick)
3554 return 1;
3555 /* this function is called when we really expect a Certificate
3556 * message, so permit appropriate message length */
3557 n=s->method->ssl_get_message(s,
3558 SSL3_ST_CR_CERT_A,
3559 SSL3_ST_CR_CERT_B,
3560 -1,
3561 s->max_cert_list,
3562 &ok);
3563 if (!ok) return((int)n);
3564 s->s3->tmp.reuse_message = 1;
3565 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3566 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3567 return 2;
3568
3569 return 1;
3570 }
3571 #endif
3572
3573 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3574 {
3575 int i = 0;
3576 #ifndef OPENSSL_NO_ENGINE
3577 if (s->ctx->client_cert_engine)
3578 {
3579 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3580 SSL_get_client_CA_list(s),
3581 px509, ppkey, NULL, NULL, NULL);
3582 if (i != 0)
3583 return i;
3584 }
3585 #endif
3586 if (s->ctx->client_cert_cb)
3587 i = s->ctx->client_cert_cb(s,px509,ppkey);
3588 return i;
3589 }
OLDNEW
« no previous file with comments | « openssl/ssl/s3_cbc.c ('k') | openssl/ssl/s3_enc.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698