]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/s3_srvr.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / ssl / s3_srvr.c
1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
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 #define REUSE_CIPHER_BUG
152 #define NETSCAPE_HANG_BUG
153
154 #include <stdio.h>
155 #include "ssl_locl.h"
156 #include "kssl_lcl.h"
157 #include "../crypto/constant_time_locl.h"
158 #include <openssl/buffer.h>
159 #include <openssl/rand.h>
160 #include <openssl/objects.h>
161 #include <openssl/evp.h>
162 #include <openssl/hmac.h>
163 #include <openssl/x509.h>
164 #ifndef OPENSSL_NO_DH
165 # include <openssl/dh.h>
166 #endif
167 #include <openssl/bn.h>
168 #ifndef OPENSSL_NO_KRB5
169 # include <openssl/krb5_asn.h>
170 #endif
171 #include <openssl/md5.h>
172
173 #ifndef OPENSSL_NO_SSL3_METHOD
174 static const SSL_METHOD *ssl3_get_server_method(int ver);
175
176 static const SSL_METHOD *ssl3_get_server_method(int ver)
177 {
178 if (ver == SSL3_VERSION)
179 return (SSLv3_server_method());
180 else
181 return (NULL);
182 }
183
184 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
185 ssl3_accept,
186 ssl_undefined_function, ssl3_get_server_method)
187 #endif
188 #ifndef OPENSSL_NO_SRP
189 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
190 {
191 int ret = SSL_ERROR_NONE;
192
193 *al = SSL_AD_UNRECOGNIZED_NAME;
194
195 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
196 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
197 if (s->srp_ctx.login == NULL) {
198 /*
199 * RFC 5054 says SHOULD reject, we do so if There is no srp
200 * login name
201 */
202 ret = SSL3_AL_FATAL;
203 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
204 } else {
205 ret = SSL_srp_server_param_with_username(s, al);
206 }
207 }
208 return ret;
209 }
210 #endif
211
212 int ssl3_accept(SSL *s)
213 {
214 BUF_MEM *buf;
215 unsigned long alg_k, Time = (unsigned long)time(NULL);
216 void (*cb) (const SSL *ssl, int type, int val) = NULL;
217 int ret = -1;
218 int new_state, state, skip = 0;
219
220 RAND_add(&Time, sizeof(Time), 0);
221 ERR_clear_error();
222 clear_sys_error();
223
224 if (s->info_callback != NULL)
225 cb = s->info_callback;
226 else if (s->ctx->info_callback != NULL)
227 cb = s->ctx->info_callback;
228
229 /* init things to blank */
230 s->in_handshake++;
231 if (!SSL_in_init(s) || SSL_in_before(s))
232 SSL_clear(s);
233
234 if (s->cert == NULL) {
235 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
236 return (-1);
237 }
238 #ifndef OPENSSL_NO_HEARTBEATS
239 /*
240 * If we're awaiting a HeartbeatResponse, pretend we already got and
241 * don't await it anymore, because Heartbeats don't make sense during
242 * handshakes anyway.
243 */
244 if (s->tlsext_hb_pending) {
245 s->tlsext_hb_pending = 0;
246 s->tlsext_hb_seq++;
247 }
248 #endif
249
250 for (;;) {
251 state = s->state;
252
253 switch (s->state) {
254 case SSL_ST_RENEGOTIATE:
255 s->renegotiate = 1;
256 /* s->state=SSL_ST_ACCEPT; */
257
258 case SSL_ST_BEFORE:
259 case SSL_ST_ACCEPT:
260 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
261 case SSL_ST_OK | SSL_ST_ACCEPT:
262
263 s->server = 1;
264 if (cb != NULL)
265 cb(s, SSL_CB_HANDSHAKE_START, 1);
266
267 if ((s->version >> 8) != 3) {
268 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
269 return -1;
270 }
271 s->type = SSL_ST_ACCEPT;
272
273 if (s->init_buf == NULL) {
274 if ((buf = BUF_MEM_new()) == NULL) {
275 ret = -1;
276 goto end;
277 }
278 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
279 BUF_MEM_free(buf);
280 ret = -1;
281 goto end;
282 }
283 s->init_buf = buf;
284 }
285
286 if (!ssl3_setup_buffers(s)) {
287 ret = -1;
288 goto end;
289 }
290
291 s->init_num = 0;
292 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
293 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
294 /*
295 * Should have been reset by ssl3_get_finished, too.
296 */
297 s->s3->change_cipher_spec = 0;
298
299 if (s->state != SSL_ST_RENEGOTIATE) {
300 /*
301 * Ok, we now need to push on a buffering BIO so that the
302 * output is sent in a way that TCP likes :-)
303 */
304 if (!ssl_init_wbio_buffer(s, 1)) {
305 ret = -1;
306 goto end;
307 }
308
309 ssl3_init_finished_mac(s);
310 s->state = SSL3_ST_SR_CLNT_HELLO_A;
311 s->ctx->stats.sess_accept++;
312 } else if (!s->s3->send_connection_binding &&
313 !(s->options &
314 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
315 /*
316 * Server attempting to renegotiate with client that doesn't
317 * support secure renegotiation.
318 */
319 SSLerr(SSL_F_SSL3_ACCEPT,
320 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
321 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
322 ret = -1;
323 goto end;
324 } else {
325 /*
326 * s->state == SSL_ST_RENEGOTIATE, we will just send a
327 * HelloRequest
328 */
329 s->ctx->stats.sess_accept_renegotiate++;
330 s->state = SSL3_ST_SW_HELLO_REQ_A;
331 }
332 break;
333
334 case SSL3_ST_SW_HELLO_REQ_A:
335 case SSL3_ST_SW_HELLO_REQ_B:
336
337 s->shutdown = 0;
338 ret = ssl3_send_hello_request(s);
339 if (ret <= 0)
340 goto end;
341 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
342 s->state = SSL3_ST_SW_FLUSH;
343 s->init_num = 0;
344
345 ssl3_init_finished_mac(s);
346 break;
347
348 case SSL3_ST_SW_HELLO_REQ_C:
349 s->state = SSL_ST_OK;
350 break;
351
352 case SSL3_ST_SR_CLNT_HELLO_A:
353 case SSL3_ST_SR_CLNT_HELLO_B:
354 case SSL3_ST_SR_CLNT_HELLO_C:
355
356 s->shutdown = 0;
357 ret = ssl3_get_client_hello(s);
358 if (ret <= 0)
359 goto end;
360 #ifndef OPENSSL_NO_SRP
361 s->state = SSL3_ST_SR_CLNT_HELLO_D;
362 case SSL3_ST_SR_CLNT_HELLO_D:
363 {
364 int al;
365 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
366 /*
367 * callback indicates firther work to be done
368 */
369 s->rwstate = SSL_X509_LOOKUP;
370 goto end;
371 }
372 if (ret != SSL_ERROR_NONE) {
373 ssl3_send_alert(s, SSL3_AL_FATAL, al);
374 /*
375 * This is not really an error but the only means to for
376 * a client to detect whether srp is supported.
377 */
378 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
379 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
380 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
381 ret = -1;
382 goto end;
383 }
384 }
385 #endif
386
387 s->renegotiate = 2;
388 s->state = SSL3_ST_SW_SRVR_HELLO_A;
389 s->init_num = 0;
390 break;
391
392 case SSL3_ST_SW_SRVR_HELLO_A:
393 case SSL3_ST_SW_SRVR_HELLO_B:
394 ret = ssl3_send_server_hello(s);
395 if (ret <= 0)
396 goto end;
397 #ifndef OPENSSL_NO_TLSEXT
398 if (s->hit) {
399 if (s->tlsext_ticket_expected)
400 s->state = SSL3_ST_SW_SESSION_TICKET_A;
401 else
402 s->state = SSL3_ST_SW_CHANGE_A;
403 }
404 #else
405 if (s->hit)
406 s->state = SSL3_ST_SW_CHANGE_A;
407 #endif
408 else
409 s->state = SSL3_ST_SW_CERT_A;
410 s->init_num = 0;
411 break;
412
413 case SSL3_ST_SW_CERT_A:
414 case SSL3_ST_SW_CERT_B:
415 /* Check if it is anon DH or anon ECDH, */
416 /* normal PSK or KRB5 or SRP */
417 if (!
418 (s->s3->tmp.
419 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aKRB5 |
420 SSL_aSRP))
421 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
422 ret = ssl3_send_server_certificate(s);
423 if (ret <= 0)
424 goto end;
425 #ifndef OPENSSL_NO_TLSEXT
426 if (s->tlsext_status_expected)
427 s->state = SSL3_ST_SW_CERT_STATUS_A;
428 else
429 s->state = SSL3_ST_SW_KEY_EXCH_A;
430 } else {
431 skip = 1;
432 s->state = SSL3_ST_SW_KEY_EXCH_A;
433 }
434 #else
435 } else
436 skip = 1;
437
438 s->state = SSL3_ST_SW_KEY_EXCH_A;
439 #endif
440 s->init_num = 0;
441 break;
442
443 case SSL3_ST_SW_KEY_EXCH_A:
444 case SSL3_ST_SW_KEY_EXCH_B:
445 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
446
447 /*
448 * clear this, it may get reset by
449 * send_server_key_exchange
450 */
451 s->s3->tmp.use_rsa_tmp = 0;
452
453 /*
454 * only send if a DH key exchange, fortezza or RSA but we have a
455 * sign only certificate PSK: may send PSK identity hints For
456 * ECC ciphersuites, we send a serverKeyExchange message only if
457 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
458 * the server certificate contains the server's public key for
459 * key exchange.
460 */
461 if (0
462 /*
463 * PSK: send ServerKeyExchange if PSK identity hint if
464 * provided
465 */
466 #ifndef OPENSSL_NO_PSK
467 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
468 #endif
469 #ifndef OPENSSL_NO_SRP
470 /* SRP: send ServerKeyExchange */
471 || (alg_k & SSL_kSRP)
472 #endif
473 || (alg_k & SSL_kEDH)
474 || (alg_k & SSL_kEECDH)
475 || ((alg_k & SSL_kRSA)
476 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
477 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
478 && EVP_PKEY_size(s->cert->
479 pkeys
480 [SSL_PKEY_RSA_ENC].privatekey) *
481 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
482 )
483 )
484 )
485 ) {
486 ret = ssl3_send_server_key_exchange(s);
487 if (ret <= 0)
488 goto end;
489 } else
490 skip = 1;
491
492 s->state = SSL3_ST_SW_CERT_REQ_A;
493 s->init_num = 0;
494 break;
495
496 case SSL3_ST_SW_CERT_REQ_A:
497 case SSL3_ST_SW_CERT_REQ_B:
498 if ( /* don't request cert unless asked for it: */
499 !(s->verify_mode & SSL_VERIFY_PEER) ||
500 /*
501 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
502 * during re-negotiation:
503 */
504 ((s->session->peer != NULL) &&
505 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
506 /*
507 * never request cert in anonymous ciphersuites (see
508 * section "Certificate request" in SSL 3 drafts and in
509 * RFC 2246):
510 */
511 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
512 /*
513 * ... except when the application insists on
514 * verification (against the specs, but s3_clnt.c accepts
515 * this for SSL 3)
516 */
517 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
518 /*
519 * never request cert in Kerberos ciphersuites
520 */
521 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) ||
522 /* don't request certificate for SRP auth */
523 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
524 /*
525 * With normal PSK Certificates and Certificate Requests
526 * are omitted
527 */
528 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
529 /* no cert request */
530 skip = 1;
531 s->s3->tmp.cert_request = 0;
532 s->state = SSL3_ST_SW_SRVR_DONE_A;
533 if (s->s3->handshake_buffer)
534 if (!ssl3_digest_cached_records(s))
535 return -1;
536 } else {
537 s->s3->tmp.cert_request = 1;
538 ret = ssl3_send_certificate_request(s);
539 if (ret <= 0)
540 goto end;
541 #ifndef NETSCAPE_HANG_BUG
542 s->state = SSL3_ST_SW_SRVR_DONE_A;
543 #else
544 s->state = SSL3_ST_SW_FLUSH;
545 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
546 #endif
547 s->init_num = 0;
548 }
549 break;
550
551 case SSL3_ST_SW_SRVR_DONE_A:
552 case SSL3_ST_SW_SRVR_DONE_B:
553 ret = ssl3_send_server_done(s);
554 if (ret <= 0)
555 goto end;
556 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
557 s->state = SSL3_ST_SW_FLUSH;
558 s->init_num = 0;
559 break;
560
561 case SSL3_ST_SW_FLUSH:
562
563 /*
564 * This code originally checked to see if any data was pending
565 * using BIO_CTRL_INFO and then flushed. This caused problems as
566 * documented in PR#1939. The proposed fix doesn't completely
567 * resolve this issue as buggy implementations of
568 * BIO_CTRL_PENDING still exist. So instead we just flush
569 * unconditionally.
570 */
571
572 s->rwstate = SSL_WRITING;
573 if (BIO_flush(s->wbio) <= 0) {
574 ret = -1;
575 goto end;
576 }
577 s->rwstate = SSL_NOTHING;
578
579 s->state = s->s3->tmp.next_state;
580 break;
581
582 case SSL3_ST_SR_CERT_A:
583 case SSL3_ST_SR_CERT_B:
584 if (s->s3->tmp.cert_request) {
585 ret = ssl3_get_client_certificate(s);
586 if (ret <= 0)
587 goto end;
588 }
589 s->init_num = 0;
590 s->state = SSL3_ST_SR_KEY_EXCH_A;
591 break;
592
593 case SSL3_ST_SR_KEY_EXCH_A:
594 case SSL3_ST_SR_KEY_EXCH_B:
595 ret = ssl3_get_client_key_exchange(s);
596 if (ret <= 0)
597 goto end;
598 if (ret == 2) {
599 /*
600 * For the ECDH ciphersuites when the client sends its ECDH
601 * pub key in a certificate, the CertificateVerify message is
602 * not sent. Also for GOST ciphersuites when the client uses
603 * its key from the certificate for key exchange.
604 */
605 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
606 s->state = SSL3_ST_SR_FINISHED_A;
607 #else
608 if (s->s3->next_proto_neg_seen)
609 s->state = SSL3_ST_SR_NEXT_PROTO_A;
610 else
611 s->state = SSL3_ST_SR_FINISHED_A;
612 #endif
613 s->init_num = 0;
614 } else if (SSL_USE_SIGALGS(s)) {
615 s->state = SSL3_ST_SR_CERT_VRFY_A;
616 s->init_num = 0;
617 if (!s->session->peer)
618 break;
619 /*
620 * For sigalgs freeze the handshake buffer at this point and
621 * digest cached records.
622 */
623 if (!s->s3->handshake_buffer) {
624 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
625 return -1;
626 }
627 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
628 if (!ssl3_digest_cached_records(s))
629 return -1;
630 } else {
631 int offset = 0;
632 int dgst_num;
633
634 s->state = SSL3_ST_SR_CERT_VRFY_A;
635 s->init_num = 0;
636
637 /*
638 * We need to get hashes here so if there is a client cert,
639 * it can be verified FIXME - digest processing for
640 * CertificateVerify should be generalized. But it is next
641 * step
642 */
643 if (s->s3->handshake_buffer)
644 if (!ssl3_digest_cached_records(s))
645 return -1;
646 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
647 if (s->s3->handshake_dgst[dgst_num]) {
648 int dgst_size;
649
650 s->method->ssl3_enc->cert_verify_mac(s,
651 EVP_MD_CTX_type
652 (s->
653 s3->handshake_dgst
654 [dgst_num]),
655 &(s->s3->
656 tmp.cert_verify_md
657 [offset]));
658 dgst_size =
659 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
660 if (dgst_size < 0) {
661 ret = -1;
662 goto end;
663 }
664 offset += dgst_size;
665 }
666 }
667 break;
668
669 case SSL3_ST_SR_CERT_VRFY_A:
670 case SSL3_ST_SR_CERT_VRFY_B:
671 /*
672 * This *should* be the first time we enable CCS, but be
673 * extra careful about surrounding code changes. We need
674 * to set this here because we don't know if we're
675 * expecting a CertificateVerify or not.
676 */
677 if (!s->s3->change_cipher_spec)
678 s->s3->flags |= SSL3_FLAGS_CCS_OK;
679 /* we should decide if we expected this one */
680 ret = ssl3_get_cert_verify(s);
681 if (ret <= 0)
682 goto end;
683
684 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
685 s->state = SSL3_ST_SR_FINISHED_A;
686 #else
687 if (s->s3->next_proto_neg_seen)
688 s->state = SSL3_ST_SR_NEXT_PROTO_A;
689 else
690 s->state = SSL3_ST_SR_FINISHED_A;
691 #endif
692 s->init_num = 0;
693 break;
694
695 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
696 case SSL3_ST_SR_NEXT_PROTO_A:
697 case SSL3_ST_SR_NEXT_PROTO_B:
698 /*
699 * Enable CCS for resumed handshakes with NPN.
700 * In a full handshake with NPN, we end up here through
701 * SSL3_ST_SR_CERT_VRFY_B, where SSL3_FLAGS_CCS_OK was
702 * already set. Receiving a CCS clears the flag, so make
703 * sure not to re-enable it to ban duplicates.
704 * s->s3->change_cipher_spec is set when a CCS is
705 * processed in s3_pkt.c, and remains set until
706 * the client's Finished message is read.
707 */
708 if (!s->s3->change_cipher_spec)
709 s->s3->flags |= SSL3_FLAGS_CCS_OK;
710
711 ret = ssl3_get_next_proto(s);
712 if (ret <= 0)
713 goto end;
714 s->init_num = 0;
715 s->state = SSL3_ST_SR_FINISHED_A;
716 break;
717 #endif
718
719 case SSL3_ST_SR_FINISHED_A:
720 case SSL3_ST_SR_FINISHED_B:
721 /*
722 * Enable CCS for resumed handshakes without NPN.
723 * In a full handshake, we end up here through
724 * SSL3_ST_SR_CERT_VRFY_B, where SSL3_FLAGS_CCS_OK was
725 * already set. Receiving a CCS clears the flag, so make
726 * sure not to re-enable it to ban duplicates.
727 * s->s3->change_cipher_spec is set when a CCS is
728 * processed in s3_pkt.c, and remains set until
729 * the client's Finished message is read.
730 */
731 if (!s->s3->change_cipher_spec)
732 s->s3->flags |= SSL3_FLAGS_CCS_OK;
733 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
734 SSL3_ST_SR_FINISHED_B);
735 if (ret <= 0)
736 goto end;
737 if (s->hit)
738 s->state = SSL_ST_OK;
739 #ifndef OPENSSL_NO_TLSEXT
740 else if (s->tlsext_ticket_expected)
741 s->state = SSL3_ST_SW_SESSION_TICKET_A;
742 #endif
743 else
744 s->state = SSL3_ST_SW_CHANGE_A;
745 s->init_num = 0;
746 break;
747
748 #ifndef OPENSSL_NO_TLSEXT
749 case SSL3_ST_SW_SESSION_TICKET_A:
750 case SSL3_ST_SW_SESSION_TICKET_B:
751 ret = ssl3_send_newsession_ticket(s);
752 if (ret <= 0)
753 goto end;
754 s->state = SSL3_ST_SW_CHANGE_A;
755 s->init_num = 0;
756 break;
757
758 case SSL3_ST_SW_CERT_STATUS_A:
759 case SSL3_ST_SW_CERT_STATUS_B:
760 ret = ssl3_send_cert_status(s);
761 if (ret <= 0)
762 goto end;
763 s->state = SSL3_ST_SW_KEY_EXCH_A;
764 s->init_num = 0;
765 break;
766
767 #endif
768
769 case SSL3_ST_SW_CHANGE_A:
770 case SSL3_ST_SW_CHANGE_B:
771
772 s->session->cipher = s->s3->tmp.new_cipher;
773 if (!s->method->ssl3_enc->setup_key_block(s)) {
774 ret = -1;
775 goto end;
776 }
777
778 ret = ssl3_send_change_cipher_spec(s,
779 SSL3_ST_SW_CHANGE_A,
780 SSL3_ST_SW_CHANGE_B);
781
782 if (ret <= 0)
783 goto end;
784 s->state = SSL3_ST_SW_FINISHED_A;
785 s->init_num = 0;
786
787 if (!s->method->ssl3_enc->change_cipher_state(s,
788 SSL3_CHANGE_CIPHER_SERVER_WRITE))
789 {
790 ret = -1;
791 goto end;
792 }
793
794 break;
795
796 case SSL3_ST_SW_FINISHED_A:
797 case SSL3_ST_SW_FINISHED_B:
798 ret = ssl3_send_finished(s,
799 SSL3_ST_SW_FINISHED_A,
800 SSL3_ST_SW_FINISHED_B,
801 s->method->
802 ssl3_enc->server_finished_label,
803 s->method->
804 ssl3_enc->server_finished_label_len);
805 if (ret <= 0)
806 goto end;
807 s->state = SSL3_ST_SW_FLUSH;
808 if (s->hit) {
809 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
810 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
811 #else
812 if (s->s3->next_proto_neg_seen) {
813 s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
814 } else
815 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
816 #endif
817 } else
818 s->s3->tmp.next_state = SSL_ST_OK;
819 s->init_num = 0;
820 break;
821
822 case SSL_ST_OK:
823 /* clean a few things up */
824 ssl3_cleanup_key_block(s);
825
826 BUF_MEM_free(s->init_buf);
827 s->init_buf = NULL;
828
829 /* remove buffering on output */
830 ssl_free_wbio_buffer(s);
831
832 s->init_num = 0;
833
834 if (s->renegotiate == 2) { /* skipped if we just sent a
835 * HelloRequest */
836 s->renegotiate = 0;
837 s->new_session = 0;
838
839 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
840
841 s->ctx->stats.sess_accept_good++;
842 /* s->server=1; */
843 s->handshake_func = ssl3_accept;
844
845 if (cb != NULL)
846 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
847 }
848
849 ret = 1;
850 goto end;
851 /* break; */
852
853 default:
854 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
855 ret = -1;
856 goto end;
857 /* break; */
858 }
859
860 if (!s->s3->tmp.reuse_message && !skip) {
861 if (s->debug) {
862 if ((ret = BIO_flush(s->wbio)) <= 0)
863 goto end;
864 }
865
866 if ((cb != NULL) && (s->state != state)) {
867 new_state = s->state;
868 s->state = state;
869 cb(s, SSL_CB_ACCEPT_LOOP, 1);
870 s->state = new_state;
871 }
872 }
873 skip = 0;
874 }
875 end:
876 /* BIO_flush(s->wbio); */
877
878 s->in_handshake--;
879 if (cb != NULL)
880 cb(s, SSL_CB_ACCEPT_EXIT, ret);
881 return (ret);
882 }
883
884 int ssl3_send_hello_request(SSL *s)
885 {
886
887 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
888 ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
889 s->state = SSL3_ST_SW_HELLO_REQ_B;
890 }
891
892 /* SSL3_ST_SW_HELLO_REQ_B */
893 return ssl_do_write(s);
894 }
895
896 int ssl3_get_client_hello(SSL *s)
897 {
898 int i, j, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
899 unsigned int cookie_len;
900 long n;
901 unsigned long id;
902 unsigned char *p, *d;
903 SSL_CIPHER *c;
904 #ifndef OPENSSL_NO_COMP
905 unsigned char *q;
906 SSL_COMP *comp = NULL;
907 #endif
908 STACK_OF(SSL_CIPHER) *ciphers = NULL;
909
910 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
911 goto retry_cert;
912
913 /*
914 * We do this so that we will respond with our native type. If we are
915 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
916 * switching should be handled by a different method. If we are SSLv3, we
917 * will respond with SSLv3, even if prompted with TLSv1.
918 */
919 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
920 s->state = SSL3_ST_SR_CLNT_HELLO_B;
921 }
922 s->first_packet = 1;
923 n = s->method->ssl_get_message(s,
924 SSL3_ST_SR_CLNT_HELLO_B,
925 SSL3_ST_SR_CLNT_HELLO_C,
926 SSL3_MT_CLIENT_HELLO,
927 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
928
929 if (!ok)
930 return ((int)n);
931 s->first_packet = 0;
932 d = p = (unsigned char *)s->init_msg;
933
934 /*
935 * use version from inside client hello, not from record header (may
936 * differ: see RFC 2246, Appendix E, second paragraph)
937 */
938 s->client_version = (((int)p[0]) << 8) | (int)p[1];
939 p += 2;
940
941 if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
942 s->method->version != DTLS_ANY_VERSION)
943 : (s->client_version < s->version)) {
944 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
945 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
946 !s->enc_write_ctx && !s->write_hash) {
947 /*
948 * similar to ssl3_get_record, send alert using remote version
949 * number
950 */
951 s->version = s->client_version;
952 }
953 al = SSL_AD_PROTOCOL_VERSION;
954 goto f_err;
955 }
956
957 /*
958 * If we require cookies and this ClientHello doesn't contain one, just
959 * return since we do not want to allocate any memory yet. So check
960 * cookie length...
961 */
962 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
963 unsigned int session_length, cookie_length;
964
965 session_length = *(p + SSL3_RANDOM_SIZE);
966 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
967
968 if (cookie_length == 0)
969 return 1;
970 }
971
972 /* load the client random */
973 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
974 p += SSL3_RANDOM_SIZE;
975
976 /* get the session-id */
977 j = *(p++);
978
979 s->hit = 0;
980 /*
981 * Versions before 0.9.7 always allow clients to resume sessions in
982 * renegotiation. 0.9.7 and later allow this by default, but optionally
983 * ignore resumption requests with flag
984 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
985 * than a change to default behavior so that applications relying on this
986 * for security won't even compile against older library versions).
987 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
988 * request renegotiation but not a new session (s->new_session remains
989 * unset): for servers, this essentially just means that the
990 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored.
991 */
992 if ((s->new_session
993 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
994 if (!ssl_get_new_session(s, 1))
995 goto err;
996 } else {
997 i = ssl_get_prev_session(s, p, j, d + n);
998 /*
999 * Only resume if the session's version matches the negotiated
1000 * version.
1001 * RFC 5246 does not provide much useful advice on resumption
1002 * with a different protocol version. It doesn't forbid it but
1003 * the sanity of such behaviour would be questionable.
1004 * In practice, clients do not accept a version mismatch and
1005 * will abort the handshake with an error.
1006 */
1007 if (i == 1 && s->version == s->session->ssl_version) { /* previous
1008 * session */
1009 s->hit = 1;
1010 } else if (i == -1)
1011 goto err;
1012 else { /* i == 0 */
1013
1014 if (!ssl_get_new_session(s, 1))
1015 goto err;
1016 }
1017 }
1018
1019 p += j;
1020
1021 if (SSL_IS_DTLS(s)) {
1022 /* cookie stuff */
1023 cookie_len = *(p++);
1024
1025 /*
1026 * The ClientHello may contain a cookie even if the
1027 * HelloVerify message has not been sent--make sure that it
1028 * does not cause an overflow.
1029 */
1030 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1031 /* too much data */
1032 al = SSL_AD_DECODE_ERROR;
1033 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1034 goto f_err;
1035 }
1036
1037 /* verify the cookie if appropriate option is set. */
1038 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
1039 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1040
1041 if (s->ctx->app_verify_cookie_cb != NULL) {
1042 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1043 cookie_len) == 0) {
1044 al = SSL_AD_HANDSHAKE_FAILURE;
1045 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1046 SSL_R_COOKIE_MISMATCH);
1047 goto f_err;
1048 }
1049 /* else cookie verification succeeded */
1050 }
1051 /* default verification */
1052 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1053 s->d1->cookie_len) != 0) {
1054 al = SSL_AD_HANDSHAKE_FAILURE;
1055 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1056 goto f_err;
1057 }
1058 /* Set to -2 so if successful we return 2 */
1059 ret = -2;
1060 }
1061
1062 p += cookie_len;
1063 if (s->method->version == DTLS_ANY_VERSION) {
1064 /* Select version to use */
1065 if (s->client_version <= DTLS1_2_VERSION &&
1066 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1067 s->version = DTLS1_2_VERSION;
1068 s->method = DTLSv1_2_server_method();
1069 } else if (tls1_suiteb(s)) {
1070 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1071 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1072 s->version = s->client_version;
1073 al = SSL_AD_PROTOCOL_VERSION;
1074 goto f_err;
1075 } else if (s->client_version <= DTLS1_VERSION &&
1076 !(s->options & SSL_OP_NO_DTLSv1)) {
1077 s->version = DTLS1_VERSION;
1078 s->method = DTLSv1_server_method();
1079 } else {
1080 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1081 SSL_R_WRONG_VERSION_NUMBER);
1082 s->version = s->client_version;
1083 al = SSL_AD_PROTOCOL_VERSION;
1084 goto f_err;
1085 }
1086 s->session->ssl_version = s->version;
1087 }
1088 }
1089
1090 n2s(p, i);
1091 if ((i == 0) && (j != 0)) {
1092 /* we need a cipher if we are not resuming a session */
1093 al = SSL_AD_ILLEGAL_PARAMETER;
1094 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1095 goto f_err;
1096 }
1097 if ((p + i) >= (d + n)) {
1098 /* not enough data */
1099 al = SSL_AD_DECODE_ERROR;
1100 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1101 goto f_err;
1102 }
1103 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
1104 == NULL)) {
1105 goto err;
1106 }
1107 p += i;
1108
1109 /* If it is a hit, check that the cipher is in the list */
1110 if ((s->hit) && (i > 0)) {
1111 j = 0;
1112 id = s->session->cipher->id;
1113
1114 #ifdef CIPHER_DEBUG
1115 fprintf(stderr, "client sent %d ciphers\n",
1116 sk_SSL_CIPHER_num(ciphers));
1117 #endif
1118 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1119 c = sk_SSL_CIPHER_value(ciphers, i);
1120 #ifdef CIPHER_DEBUG
1121 fprintf(stderr, "client [%2d of %2d]:%s\n",
1122 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1123 #endif
1124 if (c->id == id) {
1125 j = 1;
1126 break;
1127 }
1128 }
1129 /*
1130 * Disabled because it can be used in a ciphersuite downgrade attack:
1131 * CVE-2010-4180.
1132 */
1133 #if 0
1134 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1135 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1136 /*
1137 * Special case as client bug workaround: the previously used
1138 * cipher may not be in the current list, the client instead
1139 * might be trying to continue using a cipher that before wasn't
1140 * chosen due to server preferences. We'll have to reject the
1141 * connection if the cipher is not enabled, though.
1142 */
1143 c = sk_SSL_CIPHER_value(ciphers, 0);
1144 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1145 s->session->cipher = c;
1146 j = 1;
1147 }
1148 }
1149 #endif
1150 if (j == 0) {
1151 /*
1152 * we need to have the cipher in the cipher list if we are asked
1153 * to reuse it
1154 */
1155 al = SSL_AD_ILLEGAL_PARAMETER;
1156 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1157 SSL_R_REQUIRED_CIPHER_MISSING);
1158 goto f_err;
1159 }
1160 }
1161
1162 /* compression */
1163 i = *(p++);
1164 if ((p + i) > (d + n)) {
1165 /* not enough data */
1166 al = SSL_AD_DECODE_ERROR;
1167 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1168 goto f_err;
1169 }
1170 #ifndef OPENSSL_NO_COMP
1171 q = p;
1172 #endif
1173 for (j = 0; j < i; j++) {
1174 if (p[j] == 0)
1175 break;
1176 }
1177
1178 p += i;
1179 if (j >= i) {
1180 /* no compress */
1181 al = SSL_AD_DECODE_ERROR;
1182 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1183 goto f_err;
1184 }
1185 #ifndef OPENSSL_NO_TLSEXT
1186 /* TLS extensions */
1187 if (s->version >= SSL3_VERSION) {
1188 if (!ssl_parse_clienthello_tlsext(s, &p, d, n)) {
1189 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1190 goto err;
1191 }
1192 }
1193
1194 /*
1195 * Check if we want to use external pre-shared secret for this handshake
1196 * for not reused session only. We need to generate server_random before
1197 * calling tls_session_secret_cb in order to allow SessionTicket
1198 * processing to use it in key derivation.
1199 */
1200 {
1201 unsigned char *pos;
1202 pos = s->s3->server_random;
1203 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1204 goto f_err;
1205 }
1206 }
1207
1208 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1209 SSL_CIPHER *pref_cipher = NULL;
1210
1211 s->session->master_key_length = sizeof(s->session->master_key);
1212 if (s->tls_session_secret_cb(s, s->session->master_key,
1213 &s->session->master_key_length, ciphers,
1214 &pref_cipher,
1215 s->tls_session_secret_cb_arg)) {
1216 s->hit = 1;
1217 s->session->ciphers = ciphers;
1218 s->session->verify_result = X509_V_OK;
1219
1220 ciphers = NULL;
1221
1222 /* check if some cipher was preferred by call back */
1223 pref_cipher =
1224 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1225 s->
1226 session->ciphers,
1227 SSL_get_ciphers
1228 (s));
1229 if (pref_cipher == NULL) {
1230 al = SSL_AD_HANDSHAKE_FAILURE;
1231 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1232 goto f_err;
1233 }
1234
1235 s->session->cipher = pref_cipher;
1236
1237 if (s->cipher_list)
1238 sk_SSL_CIPHER_free(s->cipher_list);
1239
1240 if (s->cipher_list_by_id)
1241 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1242
1243 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1244 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1245 }
1246 }
1247 #endif
1248
1249 /*
1250 * Worst case, we will use the NULL compression, but if we have other
1251 * options, we will now look for them. We have i-1 compression
1252 * algorithms from the client, starting at q.
1253 */
1254 s->s3->tmp.new_compression = NULL;
1255 #ifndef OPENSSL_NO_COMP
1256 /* This only happens if we have a cache hit */
1257 if (s->session->compress_meth != 0) {
1258 int m, comp_id = s->session->compress_meth;
1259 /* Perform sanity checks on resumed compression algorithm */
1260 /* Can't disable compression */
1261 if (s->options & SSL_OP_NO_COMPRESSION) {
1262 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1263 SSL_R_INCONSISTENT_COMPRESSION);
1264 goto f_err;
1265 }
1266 /* Look for resumed compression method */
1267 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1268 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1269 if (comp_id == comp->id) {
1270 s->s3->tmp.new_compression = comp;
1271 break;
1272 }
1273 }
1274 if (s->s3->tmp.new_compression == NULL) {
1275 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1276 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1277 goto f_err;
1278 }
1279 /* Look for resumed method in compression list */
1280 for (m = 0; m < i; m++) {
1281 if (q[m] == comp_id)
1282 break;
1283 }
1284 if (m >= i) {
1285 al = SSL_AD_ILLEGAL_PARAMETER;
1286 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1287 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1288 goto f_err;
1289 }
1290 } else if (s->hit)
1291 comp = NULL;
1292 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) {
1293 /* See if we have a match */
1294 int m, nn, o, v, done = 0;
1295
1296 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1297 for (m = 0; m < nn; m++) {
1298 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1299 v = comp->id;
1300 for (o = 0; o < i; o++) {
1301 if (v == q[o]) {
1302 done = 1;
1303 break;
1304 }
1305 }
1306 if (done)
1307 break;
1308 }
1309 if (done)
1310 s->s3->tmp.new_compression = comp;
1311 else
1312 comp = NULL;
1313 }
1314 #else
1315 /*
1316 * If compression is disabled we'd better not try to resume a session
1317 * using compression.
1318 */
1319 if (s->session->compress_meth != 0) {
1320 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1321 goto f_err;
1322 }
1323 #endif
1324
1325 /*
1326 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1327 */
1328
1329 if (!s->hit) {
1330 #ifdef OPENSSL_NO_COMP
1331 s->session->compress_meth = 0;
1332 #else
1333 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1334 #endif
1335 if (s->session->ciphers != NULL)
1336 sk_SSL_CIPHER_free(s->session->ciphers);
1337 s->session->ciphers = ciphers;
1338 if (ciphers == NULL) {
1339 al = SSL_AD_ILLEGAL_PARAMETER;
1340 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1341 goto f_err;
1342 }
1343 ciphers = NULL;
1344 if (!tls1_set_server_sigalgs(s)) {
1345 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1346 goto err;
1347 }
1348 /* Let cert callback update server certificates if required */
1349 retry_cert:
1350 if (s->cert->cert_cb) {
1351 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1352 if (rv == 0) {
1353 al = SSL_AD_INTERNAL_ERROR;
1354 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1355 goto f_err;
1356 }
1357 if (rv < 0) {
1358 s->rwstate = SSL_X509_LOOKUP;
1359 return -1;
1360 }
1361 s->rwstate = SSL_NOTHING;
1362 }
1363 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1364
1365 if (c == NULL) {
1366 al = SSL_AD_HANDSHAKE_FAILURE;
1367 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1368 goto f_err;
1369 }
1370 s->s3->tmp.new_cipher = c;
1371 } else {
1372 /* Session-id reuse */
1373 #ifdef REUSE_CIPHER_BUG
1374 STACK_OF(SSL_CIPHER) *sk;
1375 SSL_CIPHER *nc = NULL;
1376 SSL_CIPHER *ec = NULL;
1377
1378 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1379 sk = s->session->ciphers;
1380 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1381 c = sk_SSL_CIPHER_value(sk, i);
1382 if (c->algorithm_enc & SSL_eNULL)
1383 nc = c;
1384 if (SSL_C_IS_EXPORT(c))
1385 ec = c;
1386 }
1387 if (nc != NULL)
1388 s->s3->tmp.new_cipher = nc;
1389 else if (ec != NULL)
1390 s->s3->tmp.new_cipher = ec;
1391 else
1392 s->s3->tmp.new_cipher = s->session->cipher;
1393 } else
1394 #endif
1395 s->s3->tmp.new_cipher = s->session->cipher;
1396 }
1397
1398 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1399 if (!ssl3_digest_cached_records(s))
1400 goto f_err;
1401 }
1402
1403 /*-
1404 * we now have the following setup.
1405 * client_random
1406 * cipher_list - our prefered list of ciphers
1407 * ciphers - the clients prefered list of ciphers
1408 * compression - basically ignored right now
1409 * ssl version is set - sslv3
1410 * s->session - The ssl session has been setup.
1411 * s->hit - session reuse flag
1412 * s->tmp.new_cipher - the new cipher to use.
1413 */
1414
1415 /* Handles TLS extensions that we couldn't check earlier */
1416 if (s->version >= SSL3_VERSION) {
1417 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1418 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1419 goto err;
1420 }
1421 }
1422
1423 if (ret < 0)
1424 ret = -ret;
1425 if (0) {
1426 f_err:
1427 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1428 }
1429 err:
1430 if (ciphers != NULL)
1431 sk_SSL_CIPHER_free(ciphers);
1432 return ret < 0 ? -1 : ret;
1433 }
1434
1435 int ssl3_send_server_hello(SSL *s)
1436 {
1437 unsigned char *buf;
1438 unsigned char *p, *d;
1439 int i, sl;
1440 int al = 0;
1441 unsigned long l;
1442
1443 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1444 buf = (unsigned char *)s->init_buf->data;
1445 #ifdef OPENSSL_NO_TLSEXT
1446 p = s->s3->server_random;
1447 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0)
1448 return -1;
1449 #endif
1450 /* Do the message type and length last */
1451 d = p = ssl_handshake_start(s);
1452
1453 *(p++) = s->version >> 8;
1454 *(p++) = s->version & 0xff;
1455
1456 /* Random stuff */
1457 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1458 p += SSL3_RANDOM_SIZE;
1459
1460 /*-
1461 * There are several cases for the session ID to send
1462 * back in the server hello:
1463 * - For session reuse from the session cache,
1464 * we send back the old session ID.
1465 * - If stateless session reuse (using a session ticket)
1466 * is successful, we send back the client's "session ID"
1467 * (which doesn't actually identify the session).
1468 * - If it is a new session, we send back the new
1469 * session ID.
1470 * - However, if we want the new session to be single-use,
1471 * we send back a 0-length session ID.
1472 * s->hit is non-zero in either case of session reuse,
1473 * so the following won't overwrite an ID that we're supposed
1474 * to send back.
1475 */
1476 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1477 && !s->hit)
1478 s->session->session_id_length = 0;
1479
1480 sl = s->session->session_id_length;
1481 if (sl > (int)sizeof(s->session->session_id)) {
1482 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1483 return -1;
1484 }
1485 *(p++) = sl;
1486 memcpy(p, s->session->session_id, sl);
1487 p += sl;
1488
1489 /* put the cipher */
1490 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1491 p += i;
1492
1493 /* put the compression method */
1494 #ifdef OPENSSL_NO_COMP
1495 *(p++) = 0;
1496 #else
1497 if (s->s3->tmp.new_compression == NULL)
1498 *(p++) = 0;
1499 else
1500 *(p++) = s->s3->tmp.new_compression->id;
1501 #endif
1502 #ifndef OPENSSL_NO_TLSEXT
1503 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1504 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1505 return -1;
1506 }
1507 if ((p =
1508 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1509 &al)) == NULL) {
1510 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1511 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1512 return -1;
1513 }
1514 #endif
1515 /* do the header */
1516 l = (p - d);
1517 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1518 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1519 }
1520
1521 /* SSL3_ST_SW_SRVR_HELLO_B */
1522 return ssl_do_write(s);
1523 }
1524
1525 int ssl3_send_server_done(SSL *s)
1526 {
1527
1528 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1529 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1530 s->state = SSL3_ST_SW_SRVR_DONE_B;
1531 }
1532
1533 /* SSL3_ST_SW_SRVR_DONE_B */
1534 return ssl_do_write(s);
1535 }
1536
1537 int ssl3_send_server_key_exchange(SSL *s)
1538 {
1539 #ifndef OPENSSL_NO_RSA
1540 unsigned char *q;
1541 int j, num;
1542 RSA *rsa;
1543 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1544 unsigned int u;
1545 #endif
1546 #ifndef OPENSSL_NO_DH
1547 DH *dh = NULL, *dhp;
1548 #endif
1549 #ifndef OPENSSL_NO_ECDH
1550 EC_KEY *ecdh = NULL, *ecdhp;
1551 unsigned char *encodedPoint = NULL;
1552 int encodedlen = 0;
1553 int curve_id = 0;
1554 BN_CTX *bn_ctx = NULL;
1555 #endif
1556 EVP_PKEY *pkey;
1557 const EVP_MD *md = NULL;
1558 unsigned char *p, *d;
1559 int al, i;
1560 unsigned long type;
1561 int n;
1562 CERT *cert;
1563 BIGNUM *r[4];
1564 int nr[4], kn;
1565 BUF_MEM *buf;
1566 EVP_MD_CTX md_ctx;
1567
1568 EVP_MD_CTX_init(&md_ctx);
1569 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1570 type = s->s3->tmp.new_cipher->algorithm_mkey;
1571 cert = s->cert;
1572
1573 buf = s->init_buf;
1574
1575 r[0] = r[1] = r[2] = r[3] = NULL;
1576 n = 0;
1577 #ifndef OPENSSL_NO_RSA
1578 if (type & SSL_kRSA) {
1579 rsa = cert->rsa_tmp;
1580 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1581 rsa = s->cert->rsa_tmp_cb(s,
1582 SSL_C_IS_EXPORT(s->s3->
1583 tmp.new_cipher),
1584 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1585 tmp.new_cipher));
1586 if (rsa == NULL) {
1587 al = SSL_AD_HANDSHAKE_FAILURE;
1588 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1589 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1590 goto f_err;
1591 }
1592 RSA_up_ref(rsa);
1593 cert->rsa_tmp = rsa;
1594 }
1595 if (rsa == NULL) {
1596 al = SSL_AD_HANDSHAKE_FAILURE;
1597 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1598 SSL_R_MISSING_TMP_RSA_KEY);
1599 goto f_err;
1600 }
1601 r[0] = rsa->n;
1602 r[1] = rsa->e;
1603 s->s3->tmp.use_rsa_tmp = 1;
1604 } else
1605 #endif
1606 #ifndef OPENSSL_NO_DH
1607 if (type & SSL_kEDH) {
1608 dhp = cert->dh_tmp;
1609 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1610 dhp = s->cert->dh_tmp_cb(s,
1611 SSL_C_IS_EXPORT(s->s3->
1612 tmp.new_cipher),
1613 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1614 tmp.new_cipher));
1615 if (dhp == NULL) {
1616 al = SSL_AD_HANDSHAKE_FAILURE;
1617 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1618 SSL_R_MISSING_TMP_DH_KEY);
1619 goto f_err;
1620 }
1621
1622 if (s->s3->tmp.dh != NULL) {
1623 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1624 ERR_R_INTERNAL_ERROR);
1625 goto err;
1626 }
1627
1628 if ((dh = DHparams_dup(dhp)) == NULL) {
1629 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1630 goto err;
1631 }
1632
1633 s->s3->tmp.dh = dh;
1634 if ((dhp->pub_key == NULL ||
1635 dhp->priv_key == NULL ||
1636 (s->options & SSL_OP_SINGLE_DH_USE))) {
1637 if (!DH_generate_key(dh)) {
1638 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1639 goto err;
1640 }
1641 } else {
1642 dh->pub_key = BN_dup(dhp->pub_key);
1643 dh->priv_key = BN_dup(dhp->priv_key);
1644 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1645 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1646 goto err;
1647 }
1648 }
1649 r[0] = dh->p;
1650 r[1] = dh->g;
1651 r[2] = dh->pub_key;
1652 } else
1653 #endif
1654 #ifndef OPENSSL_NO_ECDH
1655 if (type & SSL_kEECDH) {
1656 const EC_GROUP *group;
1657
1658 ecdhp = cert->ecdh_tmp;
1659 if (s->cert->ecdh_tmp_auto) {
1660 /* Get NID of appropriate shared curve */
1661 int nid = tls1_shared_curve(s, -2);
1662 if (nid != NID_undef)
1663 ecdhp = EC_KEY_new_by_curve_name(nid);
1664 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1665 ecdhp = s->cert->ecdh_tmp_cb(s,
1666 SSL_C_IS_EXPORT(s->s3->
1667 tmp.new_cipher),
1668 SSL_C_EXPORT_PKEYLENGTH(s->
1669 s3->tmp.new_cipher));
1670 }
1671 if (ecdhp == NULL) {
1672 al = SSL_AD_HANDSHAKE_FAILURE;
1673 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1674 SSL_R_MISSING_TMP_ECDH_KEY);
1675 goto f_err;
1676 }
1677
1678 if (s->s3->tmp.ecdh != NULL) {
1679 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1680 ERR_R_INTERNAL_ERROR);
1681 goto err;
1682 }
1683
1684 /* Duplicate the ECDH structure. */
1685 if (ecdhp == NULL) {
1686 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1687 goto err;
1688 }
1689 if (s->cert->ecdh_tmp_auto)
1690 ecdh = ecdhp;
1691 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1692 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1693 goto err;
1694 }
1695
1696 s->s3->tmp.ecdh = ecdh;
1697 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1698 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1699 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1700 if (!EC_KEY_generate_key(ecdh)) {
1701 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1702 ERR_R_ECDH_LIB);
1703 goto err;
1704 }
1705 }
1706
1707 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1708 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1709 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1710 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1711 goto err;
1712 }
1713
1714 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1715 (EC_GROUP_get_degree(group) > 163)) {
1716 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1717 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1718 goto err;
1719 }
1720
1721 /*
1722 * XXX: For now, we only support ephemeral ECDH keys over named
1723 * (not generic) curves. For supported named curves, curve_id is
1724 * non-zero.
1725 */
1726 if ((curve_id =
1727 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1728 == 0) {
1729 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1730 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1731 goto err;
1732 }
1733
1734 /*
1735 * Encode the public key. First check the size of encoding and
1736 * allocate memory accordingly.
1737 */
1738 encodedlen = EC_POINT_point2oct(group,
1739 EC_KEY_get0_public_key(ecdh),
1740 POINT_CONVERSION_UNCOMPRESSED,
1741 NULL, 0, NULL);
1742
1743 encodedPoint = (unsigned char *)
1744 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1745 bn_ctx = BN_CTX_new();
1746 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1747 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1748 ERR_R_MALLOC_FAILURE);
1749 goto err;
1750 }
1751
1752 encodedlen = EC_POINT_point2oct(group,
1753 EC_KEY_get0_public_key(ecdh),
1754 POINT_CONVERSION_UNCOMPRESSED,
1755 encodedPoint, encodedlen, bn_ctx);
1756
1757 if (encodedlen == 0) {
1758 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1759 goto err;
1760 }
1761
1762 BN_CTX_free(bn_ctx);
1763 bn_ctx = NULL;
1764
1765 /*
1766 * XXX: For now, we only support named (not generic) curves in
1767 * ECDH ephemeral key exchanges. In this situation, we need four
1768 * additional bytes to encode the entire ServerECDHParams
1769 * structure.
1770 */
1771 n = 4 + encodedlen;
1772
1773 /*
1774 * We'll generate the serverKeyExchange message explicitly so we
1775 * can set these to NULLs
1776 */
1777 r[0] = NULL;
1778 r[1] = NULL;
1779 r[2] = NULL;
1780 r[3] = NULL;
1781 } else
1782 #endif /* !OPENSSL_NO_ECDH */
1783 #ifndef OPENSSL_NO_PSK
1784 if (type & SSL_kPSK) {
1785 /*
1786 * reserve size for record length and PSK identity hint
1787 */
1788 n += 2 + strlen(s->ctx->psk_identity_hint);
1789 } else
1790 #endif /* !OPENSSL_NO_PSK */
1791 #ifndef OPENSSL_NO_SRP
1792 if (type & SSL_kSRP) {
1793 if ((s->srp_ctx.N == NULL) ||
1794 (s->srp_ctx.g == NULL) ||
1795 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1796 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1797 SSL_R_MISSING_SRP_PARAM);
1798 goto err;
1799 }
1800 r[0] = s->srp_ctx.N;
1801 r[1] = s->srp_ctx.g;
1802 r[2] = s->srp_ctx.s;
1803 r[3] = s->srp_ctx.B;
1804 } else
1805 #endif
1806 {
1807 al = SSL_AD_HANDSHAKE_FAILURE;
1808 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1809 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1810 goto f_err;
1811 }
1812 for (i = 0; i < 4 && r[i] != NULL; i++) {
1813 nr[i] = BN_num_bytes(r[i]);
1814 #ifndef OPENSSL_NO_SRP
1815 if ((i == 2) && (type & SSL_kSRP))
1816 n += 1 + nr[i];
1817 else
1818 #endif
1819 n += 2 + nr[i];
1820 }
1821
1822 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1823 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1824 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1825 == NULL) {
1826 al = SSL_AD_DECODE_ERROR;
1827 goto f_err;
1828 }
1829 kn = EVP_PKEY_size(pkey);
1830 } else {
1831 pkey = NULL;
1832 kn = 0;
1833 }
1834
1835 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1836 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1837 goto err;
1838 }
1839 d = p = ssl_handshake_start(s);
1840
1841 for (i = 0; i < 4 && r[i] != NULL; i++) {
1842 #ifndef OPENSSL_NO_SRP
1843 if ((i == 2) && (type & SSL_kSRP)) {
1844 *p = nr[i];
1845 p++;
1846 } else
1847 #endif
1848 s2n(nr[i], p);
1849 BN_bn2bin(r[i], p);
1850 p += nr[i];
1851 }
1852
1853 #ifndef OPENSSL_NO_ECDH
1854 if (type & SSL_kEECDH) {
1855 /*
1856 * XXX: For now, we only support named (not generic) curves. In
1857 * this situation, the serverKeyExchange message has: [1 byte
1858 * CurveType], [2 byte CurveName] [1 byte length of encoded
1859 * point], followed by the actual encoded point itself
1860 */
1861 *p = NAMED_CURVE_TYPE;
1862 p += 1;
1863 *p = 0;
1864 p += 1;
1865 *p = curve_id;
1866 p += 1;
1867 *p = encodedlen;
1868 p += 1;
1869 memcpy((unsigned char *)p,
1870 (unsigned char *)encodedPoint, encodedlen);
1871 OPENSSL_free(encodedPoint);
1872 encodedPoint = NULL;
1873 p += encodedlen;
1874 }
1875 #endif
1876
1877 #ifndef OPENSSL_NO_PSK
1878 if (type & SSL_kPSK) {
1879 /* copy PSK identity hint */
1880 s2n(strlen(s->ctx->psk_identity_hint), p);
1881 strncpy((char *)p, s->ctx->psk_identity_hint,
1882 strlen(s->ctx->psk_identity_hint));
1883 p += strlen(s->ctx->psk_identity_hint);
1884 }
1885 #endif
1886
1887 /* not anonymous */
1888 if (pkey != NULL) {
1889 /*
1890 * n is the length of the params, they start at &(d[4]) and p
1891 * points to the space at the end.
1892 */
1893 #ifndef OPENSSL_NO_RSA
1894 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1895 q = md_buf;
1896 j = 0;
1897 for (num = 2; num > 0; num--) {
1898 EVP_MD_CTX_set_flags(&md_ctx,
1899 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1900 EVP_DigestInit_ex(&md_ctx, (num == 2)
1901 ? s->ctx->md5 : s->ctx->sha1, NULL);
1902 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1903 SSL3_RANDOM_SIZE);
1904 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1905 SSL3_RANDOM_SIZE);
1906 EVP_DigestUpdate(&md_ctx, d, n);
1907 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1908 q += i;
1909 j += i;
1910 }
1911 if (RSA_sign(NID_md5_sha1, md_buf, j,
1912 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1913 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1914 goto err;
1915 }
1916 s2n(u, p);
1917 n += u + 2;
1918 } else
1919 #endif
1920 if (md) {
1921 /* send signature algorithm */
1922 if (SSL_USE_SIGALGS(s)) {
1923 if (!tls12_get_sigandhash(p, pkey, md)) {
1924 /* Should never happen */
1925 al = SSL_AD_INTERNAL_ERROR;
1926 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1927 ERR_R_INTERNAL_ERROR);
1928 goto f_err;
1929 }
1930 p += 2;
1931 }
1932 #ifdef SSL_DEBUG
1933 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
1934 #endif
1935 EVP_SignInit_ex(&md_ctx, md, NULL);
1936 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1937 SSL3_RANDOM_SIZE);
1938 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1939 SSL3_RANDOM_SIZE);
1940 EVP_SignUpdate(&md_ctx, d, n);
1941 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1942 (unsigned int *)&i, pkey)) {
1943 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
1944 goto err;
1945 }
1946 s2n(i, p);
1947 n += i + 2;
1948 if (SSL_USE_SIGALGS(s))
1949 n += 2;
1950 } else {
1951 /* Is this error check actually needed? */
1952 al = SSL_AD_HANDSHAKE_FAILURE;
1953 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1954 SSL_R_UNKNOWN_PKEY_TYPE);
1955 goto f_err;
1956 }
1957 }
1958
1959 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
1960 }
1961
1962 s->state = SSL3_ST_SW_KEY_EXCH_B;
1963 EVP_MD_CTX_cleanup(&md_ctx);
1964 return ssl_do_write(s);
1965 f_err:
1966 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1967 err:
1968 #ifndef OPENSSL_NO_ECDH
1969 if (encodedPoint != NULL)
1970 OPENSSL_free(encodedPoint);
1971 BN_CTX_free(bn_ctx);
1972 #endif
1973 EVP_MD_CTX_cleanup(&md_ctx);
1974 return (-1);
1975 }
1976
1977 int ssl3_send_certificate_request(SSL *s)
1978 {
1979 unsigned char *p, *d;
1980 int i, j, nl, off, n;
1981 STACK_OF(X509_NAME) *sk = NULL;
1982 X509_NAME *name;
1983 BUF_MEM *buf;
1984
1985 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1986 buf = s->init_buf;
1987
1988 d = p = ssl_handshake_start(s);
1989
1990 /* get the list of acceptable cert types */
1991 p++;
1992 n = ssl3_get_req_cert_type(s, p);
1993 d[0] = n;
1994 p += n;
1995 n++;
1996
1997 if (SSL_USE_SIGALGS(s)) {
1998 const unsigned char *psigs;
1999 nl = tls12_get_psigalgs(s, &psigs);
2000 s2n(nl, p);
2001 memcpy(p, psigs, nl);
2002 p += nl;
2003 n += nl + 2;
2004 }
2005
2006 off = n;
2007 p += 2;
2008 n += 2;
2009
2010 sk = SSL_get_client_CA_list(s);
2011 nl = 0;
2012 if (sk != NULL) {
2013 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2014 name = sk_X509_NAME_value(sk, i);
2015 j = i2d_X509_NAME(name, NULL);
2016 if (!BUF_MEM_grow_clean
2017 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2018 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2019 ERR_R_BUF_LIB);
2020 goto err;
2021 }
2022 p = ssl_handshake_start(s) + n;
2023 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
2024 s2n(j, p);
2025 i2d_X509_NAME(name, &p);
2026 n += 2 + j;
2027 nl += 2 + j;
2028 } else {
2029 d = p;
2030 i2d_X509_NAME(name, &p);
2031 j -= 2;
2032 s2n(j, d);
2033 j += 2;
2034 n += j;
2035 nl += j;
2036 }
2037 }
2038 }
2039 /* else no CA names */
2040 p = ssl_handshake_start(s) + off;
2041 s2n(nl, p);
2042
2043 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
2044
2045 #ifdef NETSCAPE_HANG_BUG
2046 if (!SSL_IS_DTLS(s)) {
2047 if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
2048 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
2049 goto err;
2050 }
2051 p = (unsigned char *)s->init_buf->data + s->init_num;
2052 /* do the header */
2053 *(p++) = SSL3_MT_SERVER_DONE;
2054 *(p++) = 0;
2055 *(p++) = 0;
2056 *(p++) = 0;
2057 s->init_num += 4;
2058 }
2059 #endif
2060
2061 s->state = SSL3_ST_SW_CERT_REQ_B;
2062 }
2063
2064 /* SSL3_ST_SW_CERT_REQ_B */
2065 return ssl_do_write(s);
2066 err:
2067 return (-1);
2068 }
2069
2070 int ssl3_get_client_key_exchange(SSL *s)
2071 {
2072 int i, al, ok;
2073 long n;
2074 unsigned long alg_k;
2075 unsigned char *p;
2076 #ifndef OPENSSL_NO_RSA
2077 RSA *rsa = NULL;
2078 EVP_PKEY *pkey = NULL;
2079 #endif
2080 #ifndef OPENSSL_NO_DH
2081 BIGNUM *pub = NULL;
2082 DH *dh_srvr, *dh_clnt = NULL;
2083 #endif
2084 #ifndef OPENSSL_NO_KRB5
2085 KSSL_ERR kssl_err;
2086 #endif /* OPENSSL_NO_KRB5 */
2087
2088 #ifndef OPENSSL_NO_ECDH
2089 EC_KEY *srvr_ecdh = NULL;
2090 EVP_PKEY *clnt_pub_pkey = NULL;
2091 EC_POINT *clnt_ecpoint = NULL;
2092 BN_CTX *bn_ctx = NULL;
2093 #endif
2094
2095 n = s->method->ssl_get_message(s,
2096 SSL3_ST_SR_KEY_EXCH_A,
2097 SSL3_ST_SR_KEY_EXCH_B,
2098 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2099
2100 if (!ok)
2101 return ((int)n);
2102 p = (unsigned char *)s->init_msg;
2103
2104 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2105
2106 #ifndef OPENSSL_NO_RSA
2107 if (alg_k & SSL_kRSA) {
2108 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2109 int decrypt_len;
2110 unsigned char decrypt_good, version_good;
2111 size_t j;
2112
2113 /* FIX THIS UP EAY EAY EAY EAY */
2114 if (s->s3->tmp.use_rsa_tmp) {
2115 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2116 rsa = s->cert->rsa_tmp;
2117 /*
2118 * Don't do a callback because rsa_tmp should be sent already
2119 */
2120 if (rsa == NULL) {
2121 al = SSL_AD_HANDSHAKE_FAILURE;
2122 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2123 SSL_R_MISSING_TMP_RSA_PKEY);
2124 goto f_err;
2125
2126 }
2127 } else {
2128 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2129 if ((pkey == NULL) ||
2130 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2131 al = SSL_AD_HANDSHAKE_FAILURE;
2132 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2133 SSL_R_MISSING_RSA_CERTIFICATE);
2134 goto f_err;
2135 }
2136 rsa = pkey->pkey.rsa;
2137 }
2138
2139 /* TLS and [incidentally] DTLS{0xFEFF} */
2140 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2141 n2s(p, i);
2142 if (n != i + 2) {
2143 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2144 al = SSL_AD_DECODE_ERROR;
2145 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2146 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2147 goto f_err;
2148 } else
2149 p -= 2;
2150 } else
2151 n = i;
2152 }
2153
2154 /*
2155 * Reject overly short RSA ciphertext because we want to be sure
2156 * that the buffer size makes it safe to iterate over the entire
2157 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2158 * actual expected size is larger due to RSA padding, but the
2159 * bound is sufficient to be safe.
2160 */
2161 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2162 al = SSL_AD_DECRYPT_ERROR;
2163 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2164 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2165 goto f_err;
2166 }
2167
2168 /*
2169 * We must not leak whether a decryption failure occurs because of
2170 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2171 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2172 * generates a random premaster secret for the case that the decrypt
2173 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2174 */
2175
2176 /*
2177 * should be RAND_bytes, but we cannot work around a failure.
2178 */
2179 if (RAND_pseudo_bytes(rand_premaster_secret,
2180 sizeof(rand_premaster_secret)) <= 0)
2181 goto err;
2182 decrypt_len =
2183 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2184 ERR_clear_error();
2185
2186 /*
2187 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2188 * be 0xff if so and zero otherwise.
2189 */
2190 decrypt_good =
2191 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2192
2193 /*
2194 * If the version in the decrypted pre-master secret is correct then
2195 * version_good will be 0xff, otherwise it'll be zero. The
2196 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2197 * (http://eprint.iacr.org/2003/052/) exploits the version number
2198 * check as a "bad version oracle". Thus version checks are done in
2199 * constant time and are treated like any other decryption error.
2200 */
2201 version_good =
2202 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2203 version_good &=
2204 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2205
2206 /*
2207 * The premaster secret must contain the same version number as the
2208 * ClientHello to detect version rollback attacks (strangely, the
2209 * protocol does not offer such protection for DH ciphersuites).
2210 * However, buggy clients exist that send the negotiated protocol
2211 * version instead if the server does not support the requested
2212 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2213 * clients.
2214 */
2215 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2216 unsigned char workaround_good;
2217 workaround_good =
2218 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2219 workaround_good &=
2220 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2221 version_good |= workaround_good;
2222 }
2223
2224 /*
2225 * Both decryption and version must be good for decrypt_good to
2226 * remain non-zero (0xff).
2227 */
2228 decrypt_good &= version_good;
2229
2230 /*
2231 * Now copy rand_premaster_secret over from p using
2232 * decrypt_good_mask. If decryption failed, then p does not
2233 * contain valid plaintext, however, a check above guarantees
2234 * it is still sufficiently large to read from.
2235 */
2236 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2237 p[j] = constant_time_select_8(decrypt_good, p[j],
2238 rand_premaster_secret[j]);
2239 }
2240
2241 s->session->master_key_length =
2242 s->method->ssl3_enc->generate_master_secret(s,
2243 s->
2244 session->master_key,
2245 p,
2246 sizeof
2247 (rand_premaster_secret));
2248 OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2249 } else
2250 #endif
2251 #ifndef OPENSSL_NO_DH
2252 if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2253 int idx = -1;
2254 EVP_PKEY *skey = NULL;
2255 if (n)
2256 n2s(p, i);
2257 else
2258 i = 0;
2259 if (n && n != i + 2) {
2260 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2261 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2262 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2263 goto err;
2264 } else {
2265 p -= 2;
2266 i = (int)n;
2267 }
2268 }
2269 if (alg_k & SSL_kDHr)
2270 idx = SSL_PKEY_DH_RSA;
2271 else if (alg_k & SSL_kDHd)
2272 idx = SSL_PKEY_DH_DSA;
2273 if (idx >= 0) {
2274 skey = s->cert->pkeys[idx].privatekey;
2275 if ((skey == NULL) ||
2276 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2277 al = SSL_AD_HANDSHAKE_FAILURE;
2278 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2279 SSL_R_MISSING_RSA_CERTIFICATE);
2280 goto f_err;
2281 }
2282 dh_srvr = skey->pkey.dh;
2283 } else if (s->s3->tmp.dh == NULL) {
2284 al = SSL_AD_HANDSHAKE_FAILURE;
2285 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2286 SSL_R_MISSING_TMP_DH_KEY);
2287 goto f_err;
2288 } else
2289 dh_srvr = s->s3->tmp.dh;
2290
2291 if (n == 0L) {
2292 /* Get pubkey from cert */
2293 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2294 if (clkey) {
2295 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2296 dh_clnt = EVP_PKEY_get1_DH(clkey);
2297 }
2298 if (dh_clnt == NULL) {
2299 al = SSL_AD_HANDSHAKE_FAILURE;
2300 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2301 SSL_R_MISSING_TMP_DH_KEY);
2302 goto f_err;
2303 }
2304 EVP_PKEY_free(clkey);
2305 pub = dh_clnt->pub_key;
2306 } else
2307 pub = BN_bin2bn(p, i, NULL);
2308 if (pub == NULL) {
2309 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2310 goto err;
2311 }
2312
2313 i = DH_compute_key(p, pub, dh_srvr);
2314
2315 if (i <= 0) {
2316 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2317 BN_clear_free(pub);
2318 goto err;
2319 }
2320
2321 DH_free(s->s3->tmp.dh);
2322 s->s3->tmp.dh = NULL;
2323 if (dh_clnt)
2324 DH_free(dh_clnt);
2325 else
2326 BN_clear_free(pub);
2327 pub = NULL;
2328 s->session->master_key_length =
2329 s->method->ssl3_enc->generate_master_secret(s,
2330 s->
2331 session->master_key,
2332 p, i);
2333 OPENSSL_cleanse(p, i);
2334 if (dh_clnt)
2335 return 2;
2336 } else
2337 #endif
2338 #ifndef OPENSSL_NO_KRB5
2339 if (alg_k & SSL_kKRB5) {
2340 krb5_error_code krb5rc;
2341 krb5_data enc_ticket;
2342 krb5_data authenticator;
2343 krb5_data enc_pms;
2344 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2345 EVP_CIPHER_CTX ciph_ctx;
2346 const EVP_CIPHER *enc = NULL;
2347 unsigned char iv[EVP_MAX_IV_LENGTH];
2348 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2349 int padl, outl;
2350 krb5_timestamp authtime = 0;
2351 krb5_ticket_times ttimes;
2352
2353 EVP_CIPHER_CTX_init(&ciph_ctx);
2354
2355 if (!kssl_ctx)
2356 kssl_ctx = kssl_ctx_new();
2357
2358 n2s(p, i);
2359 enc_ticket.length = i;
2360
2361 if (n < (long)(enc_ticket.length + 6)) {
2362 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2363 SSL_R_DATA_LENGTH_TOO_LONG);
2364 goto err;
2365 }
2366
2367 enc_ticket.data = (char *)p;
2368 p += enc_ticket.length;
2369
2370 n2s(p, i);
2371 authenticator.length = i;
2372
2373 if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
2374 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2375 SSL_R_DATA_LENGTH_TOO_LONG);
2376 goto err;
2377 }
2378
2379 authenticator.data = (char *)p;
2380 p += authenticator.length;
2381
2382 n2s(p, i);
2383 enc_pms.length = i;
2384 enc_pms.data = (char *)p;
2385 p += enc_pms.length;
2386
2387 /*
2388 * Note that the length is checked again below, ** after decryption
2389 */
2390 if (enc_pms.length > sizeof pms) {
2391 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2392 SSL_R_DATA_LENGTH_TOO_LONG);
2393 goto err;
2394 }
2395
2396 if (n != (long)(enc_ticket.length + authenticator.length +
2397 enc_pms.length + 6)) {
2398 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2399 SSL_R_DATA_LENGTH_TOO_LONG);
2400 goto err;
2401 }
2402
2403 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2404 &kssl_err)) != 0) {
2405 # ifdef KSSL_DEBUG
2406 fprintf(stderr, "kssl_sget_tkt rtn %d [%d]\n",
2407 krb5rc, kssl_err.reason);
2408 if (kssl_err.text)
2409 fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2410 # endif /* KSSL_DEBUG */
2411 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2412 goto err;
2413 }
2414
2415 /*
2416 * Note: no authenticator is not considered an error, ** but will
2417 * return authtime == 0.
2418 */
2419 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2420 &authtime, &kssl_err)) != 0) {
2421 # ifdef KSSL_DEBUG
2422 fprintf(stderr, "kssl_check_authent rtn %d [%d]\n",
2423 krb5rc, kssl_err.reason);
2424 if (kssl_err.text)
2425 fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2426 # endif /* KSSL_DEBUG */
2427 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2428 goto err;
2429 }
2430
2431 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2432 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2433 goto err;
2434 }
2435 # ifdef KSSL_DEBUG
2436 kssl_ctx_show(kssl_ctx);
2437 # endif /* KSSL_DEBUG */
2438
2439 enc = kssl_map_enc(kssl_ctx->enctype);
2440 if (enc == NULL)
2441 goto err;
2442
2443 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2444
2445 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2446 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2447 SSL_R_DECRYPTION_FAILED);
2448 goto err;
2449 }
2450 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2451 (unsigned char *)enc_pms.data, enc_pms.length))
2452 {
2453 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2454 SSL_R_DECRYPTION_FAILED);
2455 goto err;
2456 }
2457 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2458 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2459 SSL_R_DATA_LENGTH_TOO_LONG);
2460 goto err;
2461 }
2462 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2463 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2464 SSL_R_DECRYPTION_FAILED);
2465 goto err;
2466 }
2467 outl += padl;
2468 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2469 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2470 SSL_R_DATA_LENGTH_TOO_LONG);
2471 goto err;
2472 }
2473 if (!((pms[0] == (s->client_version >> 8))
2474 && (pms[1] == (s->client_version & 0xff)))) {
2475 /*
2476 * The premaster secret must contain the same version number as
2477 * the ClientHello to detect version rollback attacks (strangely,
2478 * the protocol does not offer such protection for DH
2479 * ciphersuites). However, buggy clients exist that send random
2480 * bytes instead of the protocol version. If
2481 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2482 * (Perhaps we should have a separate BUG value for the Kerberos
2483 * cipher)
2484 */
2485 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2486 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2487 SSL_AD_DECODE_ERROR);
2488 goto err;
2489 }
2490 }
2491
2492 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2493
2494 s->session->master_key_length =
2495 s->method->ssl3_enc->generate_master_secret(s,
2496 s->
2497 session->master_key,
2498 pms, outl);
2499
2500 if (kssl_ctx->client_princ) {
2501 size_t len = strlen(kssl_ctx->client_princ);
2502 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2503 s->session->krb5_client_princ_len = len;
2504 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2505 len);
2506 }
2507 }
2508
2509 /*- Was doing kssl_ctx_free() here,
2510 * but it caused problems for apache.
2511 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2512 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2513 */
2514 } else
2515 #endif /* OPENSSL_NO_KRB5 */
2516
2517 #ifndef OPENSSL_NO_ECDH
2518 if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2519 int ret = 1;
2520 int field_size = 0;
2521 const EC_KEY *tkey;
2522 const EC_GROUP *group;
2523 const BIGNUM *priv_key;
2524
2525 /* initialize structures for server's ECDH key pair */
2526 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2527 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2528 goto err;
2529 }
2530
2531 /* Let's get server private key and group information */
2532 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2533 /* use the certificate */
2534 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2535 } else {
2536 /*
2537 * use the ephermeral values we saved when generating the
2538 * ServerKeyExchange msg.
2539 */
2540 tkey = s->s3->tmp.ecdh;
2541 }
2542
2543 group = EC_KEY_get0_group(tkey);
2544 priv_key = EC_KEY_get0_private_key(tkey);
2545
2546 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2547 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2548 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2549 goto err;
2550 }
2551
2552 /* Let's get client's public key */
2553 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2554 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2555 goto err;
2556 }
2557
2558 if (n == 0L) {
2559 /* Client Publickey was in Client Certificate */
2560
2561 if (alg_k & SSL_kEECDH) {
2562 al = SSL_AD_HANDSHAKE_FAILURE;
2563 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2564 SSL_R_MISSING_TMP_ECDH_KEY);
2565 goto f_err;
2566 }
2567 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2568 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2569 /*
2570 * XXX: For now, we do not support client authentication
2571 * using ECDH certificates so this branch (n == 0L) of the
2572 * code is never executed. When that support is added, we
2573 * ought to ensure the key received in the certificate is
2574 * authorized for key agreement. ECDH_compute_key implicitly
2575 * checks that the two ECDH shares are for the same group.
2576 */
2577 al = SSL_AD_HANDSHAKE_FAILURE;
2578 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2579 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2580 goto f_err;
2581 }
2582
2583 if (EC_POINT_copy(clnt_ecpoint,
2584 EC_KEY_get0_public_key(clnt_pub_pkey->
2585 pkey.ec)) == 0) {
2586 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2587 goto err;
2588 }
2589 ret = 2; /* Skip certificate verify processing */
2590 } else {
2591 /*
2592 * Get client's public key from encoded point in the
2593 * ClientKeyExchange message.
2594 */
2595 if ((bn_ctx = BN_CTX_new()) == NULL) {
2596 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2597 ERR_R_MALLOC_FAILURE);
2598 goto err;
2599 }
2600
2601 /* Get encoded point length */
2602 i = *p;
2603 p += 1;
2604 if (n != 1 + i) {
2605 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2606 goto err;
2607 }
2608 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2609 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2610 goto err;
2611 }
2612 /*
2613 * p is pointing to somewhere in the buffer currently, so set it
2614 * to the start
2615 */
2616 p = (unsigned char *)s->init_buf->data;
2617 }
2618
2619 /* Compute the shared pre-master secret */
2620 field_size = EC_GROUP_get_degree(group);
2621 if (field_size <= 0) {
2622 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2623 goto err;
2624 }
2625 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2626 NULL);
2627 if (i <= 0) {
2628 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2629 goto err;
2630 }
2631
2632 EVP_PKEY_free(clnt_pub_pkey);
2633 EC_POINT_free(clnt_ecpoint);
2634 EC_KEY_free(srvr_ecdh);
2635 BN_CTX_free(bn_ctx);
2636 EC_KEY_free(s->s3->tmp.ecdh);
2637 s->s3->tmp.ecdh = NULL;
2638
2639 /* Compute the master secret */
2640 s->session->master_key_length =
2641 s->method->ssl3_enc->generate_master_secret(s,
2642 s->
2643 session->master_key,
2644 p, i);
2645
2646 OPENSSL_cleanse(p, i);
2647 return (ret);
2648 } else
2649 #endif
2650 #ifndef OPENSSL_NO_PSK
2651 if (alg_k & SSL_kPSK) {
2652 unsigned char *t = NULL;
2653 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2654 unsigned int pre_ms_len = 0, psk_len = 0;
2655 int psk_err = 1;
2656 char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2657
2658 al = SSL_AD_HANDSHAKE_FAILURE;
2659
2660 n2s(p, i);
2661 if (n != i + 2) {
2662 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2663 goto psk_err;
2664 }
2665 if (i > PSK_MAX_IDENTITY_LEN) {
2666 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2667 SSL_R_DATA_LENGTH_TOO_LONG);
2668 goto psk_err;
2669 }
2670 if (s->psk_server_callback == NULL) {
2671 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2672 SSL_R_PSK_NO_SERVER_CB);
2673 goto psk_err;
2674 }
2675
2676 /*
2677 * Create guaranteed NULL-terminated identity string for the callback
2678 */
2679 memcpy(tmp_id, p, i);
2680 memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2681 psk_len = s->psk_server_callback(s, tmp_id,
2682 psk_or_pre_ms,
2683 sizeof(psk_or_pre_ms));
2684 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2685
2686 if (psk_len > PSK_MAX_PSK_LEN) {
2687 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2688 goto psk_err;
2689 } else if (psk_len == 0) {
2690 /*
2691 * PSK related to the given identity not found
2692 */
2693 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2694 SSL_R_PSK_IDENTITY_NOT_FOUND);
2695 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2696 goto psk_err;
2697 }
2698
2699 /* create PSK pre_master_secret */
2700 pre_ms_len = 2 + psk_len + 2 + psk_len;
2701 t = psk_or_pre_ms;
2702 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2703 s2n(psk_len, t);
2704 memset(t, 0, psk_len);
2705 t += psk_len;
2706 s2n(psk_len, t);
2707
2708 if (s->session->psk_identity != NULL)
2709 OPENSSL_free(s->session->psk_identity);
2710 s->session->psk_identity = BUF_strdup((char *)p);
2711 if (s->session->psk_identity == NULL) {
2712 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2713 goto psk_err;
2714 }
2715
2716 if (s->session->psk_identity_hint != NULL)
2717 OPENSSL_free(s->session->psk_identity_hint);
2718 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2719 if (s->ctx->psk_identity_hint != NULL &&
2720 s->session->psk_identity_hint == NULL) {
2721 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2722 goto psk_err;
2723 }
2724
2725 s->session->master_key_length =
2726 s->method->ssl3_enc->generate_master_secret(s,
2727 s->
2728 session->master_key,
2729 psk_or_pre_ms,
2730 pre_ms_len);
2731 psk_err = 0;
2732 psk_err:
2733 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2734 if (psk_err != 0)
2735 goto f_err;
2736 } else
2737 #endif
2738 #ifndef OPENSSL_NO_SRP
2739 if (alg_k & SSL_kSRP) {
2740 int param_len;
2741
2742 n2s(p, i);
2743 param_len = i + 2;
2744 if (param_len > n) {
2745 al = SSL_AD_DECODE_ERROR;
2746 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2747 SSL_R_BAD_SRP_A_LENGTH);
2748 goto f_err;
2749 }
2750 if (!(s->srp_ctx.A = BN_bin2bn(p, i, NULL))) {
2751 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2752 goto err;
2753 }
2754 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2755 || BN_is_zero(s->srp_ctx.A)) {
2756 al = SSL_AD_ILLEGAL_PARAMETER;
2757 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2758 SSL_R_BAD_SRP_PARAMETERS);
2759 goto f_err;
2760 }
2761 if (s->session->srp_username != NULL)
2762 OPENSSL_free(s->session->srp_username);
2763 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2764 if (s->session->srp_username == NULL) {
2765 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2766 goto err;
2767 }
2768
2769 if ((s->session->master_key_length =
2770 SRP_generate_server_master_secret(s,
2771 s->session->master_key)) < 0) {
2772 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2773 goto err;
2774 }
2775
2776 p += i;
2777 } else
2778 #endif /* OPENSSL_NO_SRP */
2779 if (alg_k & SSL_kGOST) {
2780 int ret = 0;
2781 EVP_PKEY_CTX *pkey_ctx;
2782 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2783 unsigned char premaster_secret[32], *start;
2784 size_t outlen = 32, inlen;
2785 unsigned long alg_a;
2786 int Ttag, Tclass;
2787 long Tlen;
2788
2789 /* Get our certificate private key */
2790 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2791 if (alg_a & SSL_aGOST94)
2792 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2793 else if (alg_a & SSL_aGOST01)
2794 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2795
2796 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2797 EVP_PKEY_decrypt_init(pkey_ctx);
2798 /*
2799 * If client certificate is present and is of the same type, maybe
2800 * use it for key exchange. Don't mind errors from
2801 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2802 * client certificate for authorization only.
2803 */
2804 client_pub_pkey = X509_get_pubkey(s->session->peer);
2805 if (client_pub_pkey) {
2806 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2807 ERR_clear_error();
2808 }
2809 /* Decrypt session key */
2810 if (ASN1_get_object
2811 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2812 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2813 || Tclass != V_ASN1_UNIVERSAL) {
2814 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2815 SSL_R_DECRYPTION_FAILED);
2816 goto gerr;
2817 }
2818 start = p;
2819 inlen = Tlen;
2820 if (EVP_PKEY_decrypt
2821 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2822 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2823 SSL_R_DECRYPTION_FAILED);
2824 goto gerr;
2825 }
2826 /* Generate master secret */
2827 s->session->master_key_length =
2828 s->method->ssl3_enc->generate_master_secret(s,
2829 s->
2830 session->master_key,
2831 premaster_secret, 32);
2832 /* Check if pubkey from client certificate was used */
2833 if (EVP_PKEY_CTX_ctrl
2834 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2835 ret = 2;
2836 else
2837 ret = 1;
2838 gerr:
2839 EVP_PKEY_free(client_pub_pkey);
2840 EVP_PKEY_CTX_free(pkey_ctx);
2841 if (ret)
2842 return ret;
2843 else
2844 goto err;
2845 } else {
2846 al = SSL_AD_HANDSHAKE_FAILURE;
2847 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2848 goto f_err;
2849 }
2850
2851 return (1);
2852 f_err:
2853 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2854 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
2855 err:
2856 #endif
2857 #ifndef OPENSSL_NO_ECDH
2858 EVP_PKEY_free(clnt_pub_pkey);
2859 EC_POINT_free(clnt_ecpoint);
2860 if (srvr_ecdh != NULL)
2861 EC_KEY_free(srvr_ecdh);
2862 BN_CTX_free(bn_ctx);
2863 #endif
2864 return (-1);
2865 }
2866
2867 int ssl3_get_cert_verify(SSL *s)
2868 {
2869 EVP_PKEY *pkey = NULL;
2870 unsigned char *p;
2871 int al, ok, ret = 0;
2872 long n;
2873 int type = 0, i, j;
2874 X509 *peer;
2875 const EVP_MD *md = NULL;
2876 EVP_MD_CTX mctx;
2877 EVP_MD_CTX_init(&mctx);
2878
2879 n = s->method->ssl_get_message(s,
2880 SSL3_ST_SR_CERT_VRFY_A,
2881 SSL3_ST_SR_CERT_VRFY_B,
2882 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2883
2884 if (!ok)
2885 return ((int)n);
2886
2887 if (s->session->peer != NULL) {
2888 peer = s->session->peer;
2889 pkey = X509_get_pubkey(peer);
2890 type = X509_certificate_type(peer, pkey);
2891 } else {
2892 peer = NULL;
2893 pkey = NULL;
2894 }
2895
2896 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2897 s->s3->tmp.reuse_message = 1;
2898 if (peer != NULL) {
2899 al = SSL_AD_UNEXPECTED_MESSAGE;
2900 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2901 goto f_err;
2902 }
2903 ret = 1;
2904 goto end;
2905 }
2906
2907 if (peer == NULL) {
2908 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2909 al = SSL_AD_UNEXPECTED_MESSAGE;
2910 goto f_err;
2911 }
2912
2913 if (!(type & EVP_PKT_SIGN)) {
2914 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2915 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2916 al = SSL_AD_ILLEGAL_PARAMETER;
2917 goto f_err;
2918 }
2919
2920 if (s->s3->change_cipher_spec) {
2921 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2922 al = SSL_AD_UNEXPECTED_MESSAGE;
2923 goto f_err;
2924 }
2925
2926 /* we now have a signature that we need to verify */
2927 p = (unsigned char *)s->init_msg;
2928 /* Check for broken implementations of GOST ciphersuites */
2929 /*
2930 * If key is GOST and n is exactly 64, it is bare signature without
2931 * length field
2932 */
2933 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2934 pkey->type == NID_id_GostR3410_2001)) {
2935 i = 64;
2936 } else {
2937 if (SSL_USE_SIGALGS(s)) {
2938 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
2939 if (rv == -1) {
2940 al = SSL_AD_INTERNAL_ERROR;
2941 goto f_err;
2942 } else if (rv == 0) {
2943 al = SSL_AD_DECODE_ERROR;
2944 goto f_err;
2945 }
2946 #ifdef SSL_DEBUG
2947 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2948 #endif
2949 p += 2;
2950 n -= 2;
2951 }
2952 n2s(p, i);
2953 n -= 2;
2954 if (i > n) {
2955 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2956 al = SSL_AD_DECODE_ERROR;
2957 goto f_err;
2958 }
2959 }
2960 j = EVP_PKEY_size(pkey);
2961 if ((i > j) || (n > j) || (n <= 0)) {
2962 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2963 al = SSL_AD_DECODE_ERROR;
2964 goto f_err;
2965 }
2966
2967 if (SSL_USE_SIGALGS(s)) {
2968 long hdatalen = 0;
2969 void *hdata;
2970 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2971 if (hdatalen <= 0) {
2972 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2973 al = SSL_AD_INTERNAL_ERROR;
2974 goto f_err;
2975 }
2976 #ifdef SSL_DEBUG
2977 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
2978 EVP_MD_name(md));
2979 #endif
2980 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
2981 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
2982 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
2983 al = SSL_AD_INTERNAL_ERROR;
2984 goto f_err;
2985 }
2986
2987 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
2988 al = SSL_AD_DECRYPT_ERROR;
2989 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
2990 goto f_err;
2991 }
2992 } else
2993 #ifndef OPENSSL_NO_RSA
2994 if (pkey->type == EVP_PKEY_RSA) {
2995 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2996 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2997 pkey->pkey.rsa);
2998 if (i < 0) {
2999 al = SSL_AD_DECRYPT_ERROR;
3000 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3001 goto f_err;
3002 }
3003 if (i == 0) {
3004 al = SSL_AD_DECRYPT_ERROR;
3005 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3006 goto f_err;
3007 }
3008 } else
3009 #endif
3010 #ifndef OPENSSL_NO_DSA
3011 if (pkey->type == EVP_PKEY_DSA) {
3012 j = DSA_verify(pkey->save_type,
3013 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3014 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3015 if (j <= 0) {
3016 /* bad signature */
3017 al = SSL_AD_DECRYPT_ERROR;
3018 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3019 goto f_err;
3020 }
3021 } else
3022 #endif
3023 #ifndef OPENSSL_NO_ECDSA
3024 if (pkey->type == EVP_PKEY_EC) {
3025 j = ECDSA_verify(pkey->save_type,
3026 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3027 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3028 if (j <= 0) {
3029 /* bad signature */
3030 al = SSL_AD_DECRYPT_ERROR;
3031 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3032 goto f_err;
3033 }
3034 } else
3035 #endif
3036 if (pkey->type == NID_id_GostR3410_94
3037 || pkey->type == NID_id_GostR3410_2001) {
3038 unsigned char signature[64];
3039 int idx;
3040 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3041 EVP_PKEY_verify_init(pctx);
3042 if (i != 64) {
3043 fprintf(stderr, "GOST signature length is %d", i);
3044 }
3045 for (idx = 0; idx < 64; idx++) {
3046 signature[63 - idx] = p[idx];
3047 }
3048 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3049 32);
3050 EVP_PKEY_CTX_free(pctx);
3051 if (j <= 0) {
3052 al = SSL_AD_DECRYPT_ERROR;
3053 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3054 goto f_err;
3055 }
3056 } else {
3057 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3058 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3059 goto f_err;
3060 }
3061
3062 ret = 1;
3063 if (0) {
3064 f_err:
3065 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3066 }
3067 end:
3068 if (s->s3->handshake_buffer) {
3069 BIO_free(s->s3->handshake_buffer);
3070 s->s3->handshake_buffer = NULL;
3071 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3072 }
3073 EVP_MD_CTX_cleanup(&mctx);
3074 EVP_PKEY_free(pkey);
3075 return (ret);
3076 }
3077
3078 int ssl3_get_client_certificate(SSL *s)
3079 {
3080 int i, ok, al, ret = -1;
3081 X509 *x = NULL;
3082 unsigned long l, nc, llen, n;
3083 const unsigned char *p, *q;
3084 unsigned char *d;
3085 STACK_OF(X509) *sk = NULL;
3086
3087 n = s->method->ssl_get_message(s,
3088 SSL3_ST_SR_CERT_A,
3089 SSL3_ST_SR_CERT_B,
3090 -1, s->max_cert_list, &ok);
3091
3092 if (!ok)
3093 return ((int)n);
3094
3095 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3096 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3097 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3098 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3099 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3100 al = SSL_AD_HANDSHAKE_FAILURE;
3101 goto f_err;
3102 }
3103 /*
3104 * If tls asked for a client cert, the client must return a 0 list
3105 */
3106 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3107 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3108 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3109 al = SSL_AD_UNEXPECTED_MESSAGE;
3110 goto f_err;
3111 }
3112 s->s3->tmp.reuse_message = 1;
3113 return (1);
3114 }
3115
3116 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3117 al = SSL_AD_UNEXPECTED_MESSAGE;
3118 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3119 goto f_err;
3120 }
3121 p = d = (unsigned char *)s->init_msg;
3122
3123 if ((sk = sk_X509_new_null()) == NULL) {
3124 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3125 goto err;
3126 }
3127
3128 n2l3(p, llen);
3129 if (llen + 3 != n) {
3130 al = SSL_AD_DECODE_ERROR;
3131 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3132 goto f_err;
3133 }
3134 for (nc = 0; nc < llen;) {
3135 n2l3(p, l);
3136 if ((l + nc + 3) > llen) {
3137 al = SSL_AD_DECODE_ERROR;
3138 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3139 SSL_R_CERT_LENGTH_MISMATCH);
3140 goto f_err;
3141 }
3142
3143 q = p;
3144 x = d2i_X509(NULL, &p, l);
3145 if (x == NULL) {
3146 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3147 goto err;
3148 }
3149 if (p != (q + l)) {
3150 al = SSL_AD_DECODE_ERROR;
3151 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3152 SSL_R_CERT_LENGTH_MISMATCH);
3153 goto f_err;
3154 }
3155 if (!sk_X509_push(sk, x)) {
3156 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3157 goto err;
3158 }
3159 x = NULL;
3160 nc += l + 3;
3161 }
3162
3163 if (sk_X509_num(sk) <= 0) {
3164 /* TLS does not mind 0 certs returned */
3165 if (s->version == SSL3_VERSION) {
3166 al = SSL_AD_HANDSHAKE_FAILURE;
3167 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3168 SSL_R_NO_CERTIFICATES_RETURNED);
3169 goto f_err;
3170 }
3171 /* Fail for TLS only if we required a certificate */
3172 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3173 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3174 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3175 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3176 al = SSL_AD_HANDSHAKE_FAILURE;
3177 goto f_err;
3178 }
3179 /* No client certificate so digest cached records */
3180 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3181 al = SSL_AD_INTERNAL_ERROR;
3182 goto f_err;
3183 }
3184 } else {
3185 i = ssl_verify_cert_chain(s, sk);
3186 if (i <= 0) {
3187 al = ssl_verify_alarm_type(s->verify_result);
3188 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3189 SSL_R_CERTIFICATE_VERIFY_FAILED);
3190 goto f_err;
3191 }
3192 }
3193
3194 if (s->session->peer != NULL) /* This should not be needed */
3195 X509_free(s->session->peer);
3196 s->session->peer = sk_X509_shift(sk);
3197 s->session->verify_result = s->verify_result;
3198
3199 /*
3200 * With the current implementation, sess_cert will always be NULL when we
3201 * arrive here.
3202 */
3203 if (s->session->sess_cert == NULL) {
3204 s->session->sess_cert = ssl_sess_cert_new();
3205 if (s->session->sess_cert == NULL) {
3206 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3207 goto err;
3208 }
3209 }
3210 if (s->session->sess_cert->cert_chain != NULL)
3211 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3212 s->session->sess_cert->cert_chain = sk;
3213 /*
3214 * Inconsistency alert: cert_chain does *not* include the peer's own
3215 * certificate, while we do include it in s3_clnt.c
3216 */
3217
3218 sk = NULL;
3219
3220 ret = 1;
3221 if (0) {
3222 f_err:
3223 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3224 }
3225 err:
3226 if (x != NULL)
3227 X509_free(x);
3228 if (sk != NULL)
3229 sk_X509_pop_free(sk, X509_free);
3230 return (ret);
3231 }
3232
3233 int ssl3_send_server_certificate(SSL *s)
3234 {
3235 CERT_PKEY *cpk;
3236
3237 if (s->state == SSL3_ST_SW_CERT_A) {
3238 cpk = ssl_get_server_send_pkey(s);
3239 if (cpk == NULL) {
3240 /* VRS: allow null cert if auth == KRB5 */
3241 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3242 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
3243 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
3244 ERR_R_INTERNAL_ERROR);
3245 return (0);
3246 }
3247 }
3248
3249 if (!ssl3_output_cert_chain(s, cpk)) {
3250 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3251 return (0);
3252 }
3253 s->state = SSL3_ST_SW_CERT_B;
3254 }
3255
3256 /* SSL3_ST_SW_CERT_B */
3257 return ssl_do_write(s);
3258 }
3259
3260 #ifndef OPENSSL_NO_TLSEXT
3261 /* send a new session ticket (not necessarily for a new session) */
3262 int ssl3_send_newsession_ticket(SSL *s)
3263 {
3264 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3265 unsigned char *p, *senc, *macstart;
3266 const unsigned char *const_p;
3267 int len, slen_full, slen;
3268 SSL_SESSION *sess;
3269 unsigned int hlen;
3270 EVP_CIPHER_CTX ctx;
3271 HMAC_CTX hctx;
3272 SSL_CTX *tctx = s->initial_ctx;
3273 unsigned char iv[EVP_MAX_IV_LENGTH];
3274 unsigned char key_name[16];
3275
3276 /* get session encoding length */
3277 slen_full = i2d_SSL_SESSION(s->session, NULL);
3278 /*
3279 * Some length values are 16 bits, so forget it if session is too
3280 * long
3281 */
3282 if (slen_full > 0xFF00)
3283 return -1;
3284 senc = OPENSSL_malloc(slen_full);
3285 if (!senc)
3286 return -1;
3287 p = senc;
3288 i2d_SSL_SESSION(s->session, &p);
3289
3290 /*
3291 * create a fresh copy (not shared with other threads) to clean up
3292 */
3293 const_p = senc;
3294 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3295 if (sess == NULL) {
3296 OPENSSL_free(senc);
3297 return -1;
3298 }
3299 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3300
3301 slen = i2d_SSL_SESSION(sess, NULL);
3302 if (slen > slen_full) { /* shouldn't ever happen */
3303 OPENSSL_free(senc);
3304 return -1;
3305 }
3306 p = senc;
3307 i2d_SSL_SESSION(sess, &p);
3308 SSL_SESSION_free(sess);
3309
3310 /*-
3311 * Grow buffer if need be: the length calculation is as
3312 * follows handshake_header_length +
3313 * 4 (ticket lifetime hint) + 2 (ticket length) +
3314 * 16 (key name) + max_iv_len (iv length) +
3315 * session_length + max_enc_block_size (max encrypted session
3316 * length) + max_md_size (HMAC).
3317 */
3318 if (!BUF_MEM_grow(s->init_buf,
3319 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3320 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3321 return -1;
3322 p = ssl_handshake_start(s);
3323 EVP_CIPHER_CTX_init(&ctx);
3324 HMAC_CTX_init(&hctx);
3325 /*
3326 * Initialize HMAC and cipher contexts. If callback present it does
3327 * all the work otherwise use generated values from parent ctx.
3328 */
3329 if (tctx->tlsext_ticket_key_cb) {
3330 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3331 &hctx, 1) < 0) {
3332 OPENSSL_free(senc);
3333 return -1;
3334 }
3335 } else {
3336 RAND_pseudo_bytes(iv, 16);
3337 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3338 tctx->tlsext_tick_aes_key, iv);
3339 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3340 tlsext_tick_md(), NULL);
3341 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3342 }
3343
3344 /*
3345 * Ticket lifetime hint (advisory only): We leave this unspecified
3346 * for resumed session (for simplicity), and guess that tickets for
3347 * new sessions will live as long as their sessions.
3348 */
3349 l2n(s->hit ? 0 : s->session->timeout, p);
3350
3351 /* Skip ticket length for now */
3352 p += 2;
3353 /* Output key name */
3354 macstart = p;
3355 memcpy(p, key_name, 16);
3356 p += 16;
3357 /* output IV */
3358 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3359 p += EVP_CIPHER_CTX_iv_length(&ctx);
3360 /* Encrypt session data */
3361 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
3362 p += len;
3363 EVP_EncryptFinal(&ctx, p, &len);
3364 p += len;
3365 EVP_CIPHER_CTX_cleanup(&ctx);
3366
3367 HMAC_Update(&hctx, macstart, p - macstart);
3368 HMAC_Final(&hctx, p, &hlen);
3369 HMAC_CTX_cleanup(&hctx);
3370
3371 p += hlen;
3372 /* Now write out lengths: p points to end of data written */
3373 /* Total length */
3374 len = p - ssl_handshake_start(s);
3375 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
3376 /* Skip ticket lifetime hint */
3377 p = ssl_handshake_start(s) + 4;
3378 s2n(len - 6, p);
3379 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3380 OPENSSL_free(senc);
3381 }
3382
3383 /* SSL3_ST_SW_SESSION_TICKET_B */
3384 return ssl_do_write(s);
3385 }
3386
3387 int ssl3_send_cert_status(SSL *s)
3388 {
3389 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3390 unsigned char *p;
3391 /*-
3392 * Grow buffer if need be: the length calculation is as
3393 * follows 1 (message type) + 3 (message length) +
3394 * 1 (ocsp response type) + 3 (ocsp response length)
3395 * + (ocsp response)
3396 */
3397 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
3398 return -1;
3399
3400 p = (unsigned char *)s->init_buf->data;
3401
3402 /* do the header */
3403 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3404 /* message length */
3405 l2n3(s->tlsext_ocsp_resplen + 4, p);
3406 /* status type */
3407 *(p++) = s->tlsext_status_type;
3408 /* length of OCSP response */
3409 l2n3(s->tlsext_ocsp_resplen, p);
3410 /* actual response */
3411 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3412 /* number of bytes to write */
3413 s->init_num = 8 + s->tlsext_ocsp_resplen;
3414 s->state = SSL3_ST_SW_CERT_STATUS_B;
3415 s->init_off = 0;
3416 }
3417
3418 /* SSL3_ST_SW_CERT_STATUS_B */
3419 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3420 }
3421
3422 # ifndef OPENSSL_NO_NEXTPROTONEG
3423 /*
3424 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3425 * It sets the next_proto member in s if found
3426 */
3427 int ssl3_get_next_proto(SSL *s)
3428 {
3429 int ok;
3430 int proto_len, padding_len;
3431 long n;
3432 const unsigned char *p;
3433
3434 /*
3435 * Clients cannot send a NextProtocol message if we didn't see the
3436 * extension in their ClientHello
3437 */
3438 if (!s->s3->next_proto_neg_seen) {
3439 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3440 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3441 return -1;
3442 }
3443
3444 /* See the payload format below */
3445 n = s->method->ssl_get_message(s,
3446 SSL3_ST_SR_NEXT_PROTO_A,
3447 SSL3_ST_SR_NEXT_PROTO_B,
3448 SSL3_MT_NEXT_PROTO, 514, &ok);
3449
3450 if (!ok)
3451 return ((int)n);
3452
3453 /*
3454 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3455 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3456 * ssl3_get_finished).
3457 */
3458 if (!s->s3->change_cipher_spec) {
3459 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3460 return -1;
3461 }
3462
3463 if (n < 2)
3464 return 0; /* The body must be > 1 bytes long */
3465
3466 p = (unsigned char *)s->init_msg;
3467
3468 /*-
3469 * The payload looks like:
3470 * uint8 proto_len;
3471 * uint8 proto[proto_len];
3472 * uint8 padding_len;
3473 * uint8 padding[padding_len];
3474 */
3475 proto_len = p[0];
3476 if (proto_len + 2 > s->init_num)
3477 return 0;
3478 padding_len = p[proto_len + 1];
3479 if (proto_len + padding_len + 2 != s->init_num)
3480 return 0;
3481
3482 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3483 if (!s->next_proto_negotiated) {
3484 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3485 return 0;
3486 }
3487 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3488 s->next_proto_negotiated_len = proto_len;
3489
3490 return 1;
3491 }
3492 # endif
3493
3494 #endif