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