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