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