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