source: EcnlProtoTool/trunk/openssl-1.1.0e/ssl/statem/statem_clnt.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 90.4 KB
Line 
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 *
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15 *
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
18 *
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
21 *
22 */
23/* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
25 *
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
28 * license.
29 *
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
33 *
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
37 *
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
47 * OTHERWISE.
48 */
49
50#include <stdio.h>
51#include "../ssl_locl.h"
52#include "statem_locl.h"
53#include <openssl/buffer.h>
54#include <openssl/rand.h>
55#include <openssl/objects.h>
56#include <openssl/evp.h>
57#include <openssl/md5.h>
58#include <openssl/dh.h>
59#include <openssl/bn.h>
60#include <openssl/engine.h>
61
62static ossl_inline int cert_req_allowed(SSL *s);
63static int key_exchange_expected(SSL *s);
64static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
65static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
66 unsigned char *p);
67
68/*
69 * Is a CertificateRequest message allowed at the moment or not?
70 *
71 * Return values are:
72 * 1: Yes
73 * 0: No
74 */
75static ossl_inline int cert_req_allowed(SSL *s)
76{
77 /* TLS does not like anon-DH with client cert */
78 if ((s->version > SSL3_VERSION
79 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
80 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
81 return 0;
82
83 return 1;
84}
85
86/*
87 * Should we expect the ServerKeyExchange message or not?
88 *
89 * Return values are:
90 * 1: Yes
91 * 0: No
92 */
93static int key_exchange_expected(SSL *s)
94{
95 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
96
97 /*
98 * Can't skip server key exchange if this is an ephemeral
99 * ciphersuite or for SRP
100 */
101 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
102 | SSL_kSRP)) {
103 return 1;
104 }
105
106 return 0;
107}
108
109/*
110 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
111 * handshake state transitions when the client is reading messages from the
112 * server. The message type that the server has sent is provided in |mt|. The
113 * current state is in |s->statem.hand_state|.
114 *
115 * Return values are:
116 * 1: Success (transition allowed)
117 * 0: Error (transition not allowed)
118 */
119int ossl_statem_client_read_transition(SSL *s, int mt)
120{
121 OSSL_STATEM *st = &s->statem;
122 int ske_expected;
123
124 switch (st->hand_state) {
125 case TLS_ST_CW_CLNT_HELLO:
126 if (mt == SSL3_MT_SERVER_HELLO) {
127 st->hand_state = TLS_ST_CR_SRVR_HELLO;
128 return 1;
129 }
130
131 if (SSL_IS_DTLS(s)) {
132 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
133 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
134 return 1;
135 }
136 }
137 break;
138
139 case TLS_ST_CR_SRVR_HELLO:
140 if (s->hit) {
141 if (s->tlsext_ticket_expected) {
142 if (mt == SSL3_MT_NEWSESSION_TICKET) {
143 st->hand_state = TLS_ST_CR_SESSION_TICKET;
144 return 1;
145 }
146 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
147 st->hand_state = TLS_ST_CR_CHANGE;
148 return 1;
149 }
150 } else {
151 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
152 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
153 return 1;
154 } else if (s->version >= TLS1_VERSION
155 && s->tls_session_secret_cb != NULL
156 && s->session->tlsext_tick != NULL
157 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
158 /*
159 * Normally, we can tell if the server is resuming the session
160 * from the session ID. EAP-FAST (RFC 4851), however, relies on
161 * the next server message after the ServerHello to determine if
162 * the server is resuming.
163 */
164 s->hit = 1;
165 st->hand_state = TLS_ST_CR_CHANGE;
166 return 1;
167 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
168 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
169 if (mt == SSL3_MT_CERTIFICATE) {
170 st->hand_state = TLS_ST_CR_CERT;
171 return 1;
172 }
173 } else {
174 ske_expected = key_exchange_expected(s);
175 /* SKE is optional for some PSK ciphersuites */
176 if (ske_expected
177 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
178 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
179 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
180 st->hand_state = TLS_ST_CR_KEY_EXCH;
181 return 1;
182 }
183 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
184 && cert_req_allowed(s)) {
185 st->hand_state = TLS_ST_CR_CERT_REQ;
186 return 1;
187 } else if (mt == SSL3_MT_SERVER_DONE) {
188 st->hand_state = TLS_ST_CR_SRVR_DONE;
189 return 1;
190 }
191 }
192 }
193 break;
194
195 case TLS_ST_CR_CERT:
196 /*
197 * The CertificateStatus message is optional even if
198 * |tlsext_status_expected| is set
199 */
200 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
201 st->hand_state = TLS_ST_CR_CERT_STATUS;
202 return 1;
203 }
204 /* Fall through */
205
206 case TLS_ST_CR_CERT_STATUS:
207 ske_expected = key_exchange_expected(s);
208 /* SKE is optional for some PSK ciphersuites */
209 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
210 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
211 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
212 st->hand_state = TLS_ST_CR_KEY_EXCH;
213 return 1;
214 }
215 goto err;
216 }
217 /* Fall through */
218
219 case TLS_ST_CR_KEY_EXCH:
220 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
221 if (cert_req_allowed(s)) {
222 st->hand_state = TLS_ST_CR_CERT_REQ;
223 return 1;
224 }
225 goto err;
226 }
227 /* Fall through */
228
229 case TLS_ST_CR_CERT_REQ:
230 if (mt == SSL3_MT_SERVER_DONE) {
231 st->hand_state = TLS_ST_CR_SRVR_DONE;
232 return 1;
233 }
234 break;
235
236 case TLS_ST_CW_FINISHED:
237 if (s->tlsext_ticket_expected) {
238 if (mt == SSL3_MT_NEWSESSION_TICKET) {
239 st->hand_state = TLS_ST_CR_SESSION_TICKET;
240 return 1;
241 }
242 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
243 st->hand_state = TLS_ST_CR_CHANGE;
244 return 1;
245 }
246 break;
247
248 case TLS_ST_CR_SESSION_TICKET:
249 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
250 st->hand_state = TLS_ST_CR_CHANGE;
251 return 1;
252 }
253 break;
254
255 case TLS_ST_CR_CHANGE:
256 if (mt == SSL3_MT_FINISHED) {
257 st->hand_state = TLS_ST_CR_FINISHED;
258 return 1;
259 }
260 break;
261
262 default:
263 break;
264 }
265
266 err:
267 /* No valid transition found */
268 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
269 SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
270 return 0;
271}
272
273/*
274 * client_write_transition() works out what handshake state to move to next
275 * when the client is writing messages to be sent to the server.
276 */
277WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
278{
279 OSSL_STATEM *st = &s->statem;
280
281 switch (st->hand_state) {
282 case TLS_ST_OK:
283 /* Renegotiation - fall through */
284 case TLS_ST_BEFORE:
285 st->hand_state = TLS_ST_CW_CLNT_HELLO;
286 return WRITE_TRAN_CONTINUE;
287
288 case TLS_ST_CW_CLNT_HELLO:
289 /*
290 * No transition at the end of writing because we don't know what
291 * we will be sent
292 */
293 return WRITE_TRAN_FINISHED;
294
295 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
296 st->hand_state = TLS_ST_CW_CLNT_HELLO;
297 return WRITE_TRAN_CONTINUE;
298
299 case TLS_ST_CR_SRVR_DONE:
300 if (s->s3->tmp.cert_req)
301 st->hand_state = TLS_ST_CW_CERT;
302 else
303 st->hand_state = TLS_ST_CW_KEY_EXCH;
304 return WRITE_TRAN_CONTINUE;
305
306 case TLS_ST_CW_CERT:
307 st->hand_state = TLS_ST_CW_KEY_EXCH;
308 return WRITE_TRAN_CONTINUE;
309
310 case TLS_ST_CW_KEY_EXCH:
311 /*
312 * For TLS, cert_req is set to 2, so a cert chain of nothing is
313 * sent, but no verify packet is sent
314 */
315 /*
316 * XXX: For now, we do not support client authentication in ECDH
317 * cipher suites with ECDH (rather than ECDSA) certificates. We
318 * need to skip the certificate verify message when client's
319 * ECDH public key is sent inside the client certificate.
320 */
321 if (s->s3->tmp.cert_req == 1) {
322 st->hand_state = TLS_ST_CW_CERT_VRFY;
323 } else {
324 st->hand_state = TLS_ST_CW_CHANGE;
325 }
326 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
327 st->hand_state = TLS_ST_CW_CHANGE;
328 }
329 return WRITE_TRAN_CONTINUE;
330
331 case TLS_ST_CW_CERT_VRFY:
332 st->hand_state = TLS_ST_CW_CHANGE;
333 return WRITE_TRAN_CONTINUE;
334
335 case TLS_ST_CW_CHANGE:
336#if defined(OPENSSL_NO_NEXTPROTONEG)
337 st->hand_state = TLS_ST_CW_FINISHED;
338#else
339 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
340 st->hand_state = TLS_ST_CW_NEXT_PROTO;
341 else
342 st->hand_state = TLS_ST_CW_FINISHED;
343#endif
344 return WRITE_TRAN_CONTINUE;
345
346#if !defined(OPENSSL_NO_NEXTPROTONEG)
347 case TLS_ST_CW_NEXT_PROTO:
348 st->hand_state = TLS_ST_CW_FINISHED;
349 return WRITE_TRAN_CONTINUE;
350#endif
351
352 case TLS_ST_CW_FINISHED:
353 if (s->hit) {
354 st->hand_state = TLS_ST_OK;
355 ossl_statem_set_in_init(s, 0);
356 return WRITE_TRAN_CONTINUE;
357 } else {
358 return WRITE_TRAN_FINISHED;
359 }
360
361 case TLS_ST_CR_FINISHED:
362 if (s->hit) {
363 st->hand_state = TLS_ST_CW_CHANGE;
364 return WRITE_TRAN_CONTINUE;
365 } else {
366 st->hand_state = TLS_ST_OK;
367 ossl_statem_set_in_init(s, 0);
368 return WRITE_TRAN_CONTINUE;
369 }
370
371 default:
372 /* Shouldn't happen */
373 return WRITE_TRAN_ERROR;
374 }
375}
376
377/*
378 * Perform any pre work that needs to be done prior to sending a message from
379 * the client to the server.
380 */
381WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
382{
383 OSSL_STATEM *st = &s->statem;
384
385 switch (st->hand_state) {
386 case TLS_ST_CW_CLNT_HELLO:
387 s->shutdown = 0;
388 if (SSL_IS_DTLS(s)) {
389 /* every DTLS ClientHello resets Finished MAC */
390 if (!ssl3_init_finished_mac(s)) {
391 ossl_statem_set_error(s);
392 return WORK_ERROR;
393 }
394 }
395 break;
396
397 case TLS_ST_CW_CHANGE:
398 if (SSL_IS_DTLS(s)) {
399 if (s->hit) {
400 /*
401 * We're into the last flight so we don't retransmit these
402 * messages unless we need to.
403 */
404 st->use_timer = 0;
405 }
406#ifndef OPENSSL_NO_SCTP
407 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
408 return dtls_wait_for_dry(s);
409#endif
410 }
411 return WORK_FINISHED_CONTINUE;
412
413 case TLS_ST_OK:
414 return tls_finish_handshake(s, wst);
415
416 default:
417 /* No pre work to be done */
418 break;
419 }
420
421 return WORK_FINISHED_CONTINUE;
422}
423
424/*
425 * Perform any work that needs to be done after sending a message from the
426 * client to the server.
427 */
428WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
429{
430 OSSL_STATEM *st = &s->statem;
431
432 s->init_num = 0;
433
434 switch (st->hand_state) {
435 case TLS_ST_CW_CLNT_HELLO:
436 if (wst == WORK_MORE_A && statem_flush(s) != 1)
437 return WORK_MORE_A;
438
439 if (SSL_IS_DTLS(s)) {
440 /* Treat the next message as the first packet */
441 s->first_packet = 1;
442 }
443 break;
444
445 case TLS_ST_CW_KEY_EXCH:
446 if (tls_client_key_exchange_post_work(s) == 0)
447 return WORK_ERROR;
448 break;
449
450 case TLS_ST_CW_CHANGE:
451 s->session->cipher = s->s3->tmp.new_cipher;
452#ifdef OPENSSL_NO_COMP
453 s->session->compress_meth = 0;
454#else
455 if (s->s3->tmp.new_compression == NULL)
456 s->session->compress_meth = 0;
457 else
458 s->session->compress_meth = s->s3->tmp.new_compression->id;
459#endif
460 if (!s->method->ssl3_enc->setup_key_block(s))
461 return WORK_ERROR;
462
463 if (!s->method->ssl3_enc->change_cipher_state(s,
464 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
465 return WORK_ERROR;
466
467 if (SSL_IS_DTLS(s)) {
468#ifndef OPENSSL_NO_SCTP
469 if (s->hit) {
470 /*
471 * Change to new shared key of SCTP-Auth, will be ignored if
472 * no SCTP used.
473 */
474 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
475 0, NULL);
476 }
477#endif
478
479 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
480 }
481 break;
482
483 case TLS_ST_CW_FINISHED:
484#ifndef OPENSSL_NO_SCTP
485 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
486 /*
487 * Change to new shared key of SCTP-Auth, will be ignored if
488 * no SCTP used.
489 */
490 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
491 0, NULL);
492 }
493#endif
494 if (statem_flush(s) != 1)
495 return WORK_MORE_B;
496 break;
497
498 default:
499 /* No post work to be done */
500 break;
501 }
502
503 return WORK_FINISHED_CONTINUE;
504}
505
506/*
507 * Construct a message to be sent from the client to the server.
508 *
509 * Valid return values are:
510 * 1: Success
511 * 0: Error
512 */
513int ossl_statem_client_construct_message(SSL *s)
514{
515 OSSL_STATEM *st = &s->statem;
516
517 switch (st->hand_state) {
518 case TLS_ST_CW_CLNT_HELLO:
519 return tls_construct_client_hello(s);
520
521 case TLS_ST_CW_CERT:
522 return tls_construct_client_certificate(s);
523
524 case TLS_ST_CW_KEY_EXCH:
525 return tls_construct_client_key_exchange(s);
526
527 case TLS_ST_CW_CERT_VRFY:
528 return tls_construct_client_verify(s);
529
530 case TLS_ST_CW_CHANGE:
531 if (SSL_IS_DTLS(s))
532 return dtls_construct_change_cipher_spec(s);
533 else
534 return tls_construct_change_cipher_spec(s);
535
536#if !defined(OPENSSL_NO_NEXTPROTONEG)
537 case TLS_ST_CW_NEXT_PROTO:
538 return tls_construct_next_proto(s);
539#endif
540 case TLS_ST_CW_FINISHED:
541 return tls_construct_finished(s,
542 s->method->
543 ssl3_enc->client_finished_label,
544 s->method->
545 ssl3_enc->client_finished_label_len);
546
547 default:
548 /* Shouldn't happen */
549 break;
550 }
551
552 return 0;
553}
554
555/*
556 * Returns the maximum allowed length for the current message that we are
557 * reading. Excludes the message header.
558 */
559unsigned long ossl_statem_client_max_message_size(SSL *s)
560{
561 OSSL_STATEM *st = &s->statem;
562
563 switch (st->hand_state) {
564 case TLS_ST_CR_SRVR_HELLO:
565 return SERVER_HELLO_MAX_LENGTH;
566
567 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
568 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
569
570 case TLS_ST_CR_CERT:
571 return s->max_cert_list;
572
573 case TLS_ST_CR_CERT_STATUS:
574 return SSL3_RT_MAX_PLAIN_LENGTH;
575
576 case TLS_ST_CR_KEY_EXCH:
577 return SERVER_KEY_EXCH_MAX_LENGTH;
578
579 case TLS_ST_CR_CERT_REQ:
580 /*
581 * Set to s->max_cert_list for compatibility with previous releases. In
582 * practice these messages can get quite long if servers are configured
583 * to provide a long list of acceptable CAs
584 */
585 return s->max_cert_list;
586
587 case TLS_ST_CR_SRVR_DONE:
588 return SERVER_HELLO_DONE_MAX_LENGTH;
589
590 case TLS_ST_CR_CHANGE:
591 if (s->version == DTLS1_BAD_VER)
592 return 3;
593 return CCS_MAX_LENGTH;
594
595 case TLS_ST_CR_SESSION_TICKET:
596 return SSL3_RT_MAX_PLAIN_LENGTH;
597
598 case TLS_ST_CR_FINISHED:
599 return FINISHED_MAX_LENGTH;
600
601 default:
602 /* Shouldn't happen */
603 break;
604 }
605
606 return 0;
607}
608
609/*
610 * Process a message that the client has been received from the server.
611 */
612MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
613{
614 OSSL_STATEM *st = &s->statem;
615
616 switch (st->hand_state) {
617 case TLS_ST_CR_SRVR_HELLO:
618 return tls_process_server_hello(s, pkt);
619
620 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
621 return dtls_process_hello_verify(s, pkt);
622
623 case TLS_ST_CR_CERT:
624 return tls_process_server_certificate(s, pkt);
625
626 case TLS_ST_CR_CERT_STATUS:
627 return tls_process_cert_status(s, pkt);
628
629 case TLS_ST_CR_KEY_EXCH:
630 return tls_process_key_exchange(s, pkt);
631
632 case TLS_ST_CR_CERT_REQ:
633 return tls_process_certificate_request(s, pkt);
634
635 case TLS_ST_CR_SRVR_DONE:
636 return tls_process_server_done(s, pkt);
637
638 case TLS_ST_CR_CHANGE:
639 return tls_process_change_cipher_spec(s, pkt);
640
641 case TLS_ST_CR_SESSION_TICKET:
642 return tls_process_new_session_ticket(s, pkt);
643
644 case TLS_ST_CR_FINISHED:
645 return tls_process_finished(s, pkt);
646
647 default:
648 /* Shouldn't happen */
649 break;
650 }
651
652 return MSG_PROCESS_ERROR;
653}
654
655/*
656 * Perform any further processing required following the receipt of a message
657 * from the server
658 */
659WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
660{
661 OSSL_STATEM *st = &s->statem;
662
663 switch (st->hand_state) {
664 case TLS_ST_CR_CERT_REQ:
665 return tls_prepare_client_certificate(s, wst);
666
667#ifndef OPENSSL_NO_SCTP
668 case TLS_ST_CR_SRVR_DONE:
669 /* We only get here if we are using SCTP and we are renegotiating */
670 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
671 s->s3->in_read_app_data = 2;
672 s->rwstate = SSL_READING;
673 BIO_clear_retry_flags(SSL_get_rbio(s));
674 BIO_set_retry_read(SSL_get_rbio(s));
675 ossl_statem_set_sctp_read_sock(s, 1);
676 return WORK_MORE_A;
677 }
678 ossl_statem_set_sctp_read_sock(s, 0);
679 return WORK_FINISHED_STOP;
680#endif
681
682 default:
683 break;
684 }
685
686 /* Shouldn't happen */
687 return WORK_ERROR;
688}
689
690int tls_construct_client_hello(SSL *s)
691{
692 unsigned char *buf;
693 unsigned char *p, *d;
694 int i;
695 int protverr;
696 unsigned long l;
697 int al = 0;
698#ifndef OPENSSL_NO_COMP
699 int j;
700 SSL_COMP *comp;
701#endif
702 SSL_SESSION *sess = s->session;
703
704 buf = (unsigned char *)s->init_buf->data;
705
706 /* Work out what SSL/TLS/DTLS version to use */
707 protverr = ssl_set_client_hello_version(s);
708 if (protverr != 0) {
709 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
710 goto err;
711 }
712
713 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
714 /*
715 * In the case of EAP-FAST, we can have a pre-shared
716 * "ticket" without a session ID.
717 */
718 (!sess->session_id_length && !sess->tlsext_tick) ||
719 (sess->not_resumable)) {
720 if (!ssl_get_new_session(s, 0))
721 goto err;
722 }
723 /* else use the pre-loaded session */
724
725 p = s->s3->client_random;
726
727 /*
728 * for DTLS if client_random is initialized, reuse it, we are
729 * required to use same upon reply to HelloVerify
730 */
731 if (SSL_IS_DTLS(s)) {
732 size_t idx;
733 i = 1;
734 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
735 if (p[idx]) {
736 i = 0;
737 break;
738 }
739 }
740 } else
741 i = 1;
742
743 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
744 goto err;
745
746 /* Do the message type and length last */
747 d = p = ssl_handshake_start(s);
748
749 /*-
750 * version indicates the negotiated version: for example from
751 * an SSLv2/v3 compatible client hello). The client_version
752 * field is the maximum version we permit and it is also
753 * used in RSA encrypted premaster secrets. Some servers can
754 * choke if we initially report a higher version then
755 * renegotiate to a lower one in the premaster secret. This
756 * didn't happen with TLS 1.0 as most servers supported it
757 * but it can with TLS 1.1 or later if the server only supports
758 * 1.0.
759 *
760 * Possible scenario with previous logic:
761 * 1. Client hello indicates TLS 1.2
762 * 2. Server hello says TLS 1.0
763 * 3. RSA encrypted premaster secret uses 1.2.
764 * 4. Handshake proceeds using TLS 1.0.
765 * 5. Server sends hello request to renegotiate.
766 * 6. Client hello indicates TLS v1.0 as we now
767 * know that is maximum server supports.
768 * 7. Server chokes on RSA encrypted premaster secret
769 * containing version 1.0.
770 *
771 * For interoperability it should be OK to always use the
772 * maximum version we support in client hello and then rely
773 * on the checking of version to ensure the servers isn't
774 * being inconsistent: for example initially negotiating with
775 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
776 * client_version in client hello and not resetting it to
777 * the negotiated version.
778 */
779 *(p++) = s->client_version >> 8;
780 *(p++) = s->client_version & 0xff;
781
782 /* Random stuff */
783 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
784 p += SSL3_RANDOM_SIZE;
785
786 /* Session ID */
787 if (s->new_session)
788 i = 0;
789 else
790 i = s->session->session_id_length;
791 *(p++) = i;
792 if (i != 0) {
793 if (i > (int)sizeof(s->session->session_id)) {
794 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
795 goto err;
796 }
797 memcpy(p, s->session->session_id, i);
798 p += i;
799 }
800
801 /* cookie stuff for DTLS */
802 if (SSL_IS_DTLS(s)) {
803 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
805 goto err;
806 }
807 *(p++) = s->d1->cookie_len;
808 memcpy(p, s->d1->cookie, s->d1->cookie_len);
809 p += s->d1->cookie_len;
810 }
811
812 /* Ciphers supported */
813 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
814 if (i == 0) {
815 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
816 goto err;
817 }
818#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
819 /*
820 * Some servers hang if client hello > 256 bytes as hack workaround
821 * chop number of supported ciphers to keep it well below this if we
822 * use TLS v1.2
823 */
824 if (TLS1_get_version(s) >= TLS1_2_VERSION
825 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
826 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
827#endif
828 s2n(i, p);
829 p += i;
830
831 /* COMPRESSION */
832#ifdef OPENSSL_NO_COMP
833 *(p++) = 1;
834#else
835
836 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
837 j = 0;
838 else
839 j = sk_SSL_COMP_num(s->ctx->comp_methods);
840 *(p++) = 1 + j;
841 for (i = 0; i < j; i++) {
842 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
843 *(p++) = comp->id;
844 }
845#endif
846 *(p++) = 0; /* Add the NULL method */
847
848 /* TLS extensions */
849 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
850 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
851 goto err;
852 }
853 if ((p =
854 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
855 &al)) == NULL) {
856 ssl3_send_alert(s, SSL3_AL_FATAL, al);
857 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
858 goto err;
859 }
860
861 l = p - d;
862 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
863 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
864 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
865 goto err;
866 }
867
868 return 1;
869 err:
870 ossl_statem_set_error(s);
871 return 0;
872}
873
874MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
875{
876 int al;
877 unsigned int cookie_len;
878 PACKET cookiepkt;
879
880 if (!PACKET_forward(pkt, 2)
881 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
882 al = SSL_AD_DECODE_ERROR;
883 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
884 goto f_err;
885 }
886
887 cookie_len = PACKET_remaining(&cookiepkt);
888 if (cookie_len > sizeof(s->d1->cookie)) {
889 al = SSL_AD_ILLEGAL_PARAMETER;
890 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
891 goto f_err;
892 }
893
894 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
895 al = SSL_AD_DECODE_ERROR;
896 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
897 goto f_err;
898 }
899 s->d1->cookie_len = cookie_len;
900
901 return MSG_PROCESS_FINISHED_READING;
902 f_err:
903 ssl3_send_alert(s, SSL3_AL_FATAL, al);
904 ossl_statem_set_error(s);
905 return MSG_PROCESS_ERROR;
906}
907
908MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
909{
910 STACK_OF(SSL_CIPHER) *sk;
911 const SSL_CIPHER *c;
912 PACKET session_id;
913 size_t session_id_len;
914 const unsigned char *cipherchars;
915 int i, al = SSL_AD_INTERNAL_ERROR;
916 unsigned int compression;
917 unsigned int sversion;
918 int protverr;
919#ifndef OPENSSL_NO_COMP
920 SSL_COMP *comp;
921#endif
922
923 if (!PACKET_get_net_2(pkt, &sversion)) {
924 al = SSL_AD_DECODE_ERROR;
925 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
926 goto f_err;
927 }
928
929 protverr = ssl_choose_client_version(s, sversion);
930 if (protverr != 0) {
931 al = SSL_AD_PROTOCOL_VERSION;
932 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
933 goto f_err;
934 }
935
936 /* load the server hello data */
937 /* load the server random */
938 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
939 al = SSL_AD_DECODE_ERROR;
940 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
941 goto f_err;
942 }
943
944 s->hit = 0;
945
946 /* Get the session-id. */
947 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
948 al = SSL_AD_DECODE_ERROR;
949 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
950 goto f_err;
951 }
952 session_id_len = PACKET_remaining(&session_id);
953 if (session_id_len > sizeof s->session->session_id
954 || session_id_len > SSL3_SESSION_ID_SIZE) {
955 al = SSL_AD_ILLEGAL_PARAMETER;
956 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
957 goto f_err;
958 }
959
960 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
961 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
962 al = SSL_AD_DECODE_ERROR;
963 goto f_err;
964 }
965
966 /*
967 * Check if we can resume the session based on external pre-shared secret.
968 * EAP-FAST (RFC 4851) supports two types of session resumption.
969 * Resumption based on server-side state works with session IDs.
970 * Resumption based on pre-shared Protected Access Credentials (PACs)
971 * works by overriding the SessionTicket extension at the application
972 * layer, and does not send a session ID. (We do not know whether EAP-FAST
973 * servers would honour the session ID.) Therefore, the session ID alone
974 * is not a reliable indicator of session resumption, so we first check if
975 * we can resume, and later peek at the next handshake message to see if the
976 * server wants to resume.
977 */
978 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
979 s->session->tlsext_tick) {
980 const SSL_CIPHER *pref_cipher = NULL;
981 s->session->master_key_length = sizeof(s->session->master_key);
982 if (s->tls_session_secret_cb(s, s->session->master_key,
983 &s->session->master_key_length,
984 NULL, &pref_cipher,
985 s->tls_session_secret_cb_arg)) {
986 s->session->cipher = pref_cipher ?
987 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
988 } else {
989 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
990 al = SSL_AD_INTERNAL_ERROR;
991 goto f_err;
992 }
993 }
994
995 if (session_id_len != 0 && session_id_len == s->session->session_id_length
996 && memcmp(PACKET_data(&session_id), s->session->session_id,
997 session_id_len) == 0) {
998 if (s->sid_ctx_length != s->session->sid_ctx_length
999 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1000 /* actually a client application bug */
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1003 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1004 goto f_err;
1005 }
1006 s->hit = 1;
1007 } else {
1008 /*
1009 * If we were trying for session-id reuse but the server
1010 * didn't echo the ID, make a new SSL_SESSION.
1011 * In the case of EAP-FAST and PAC, we do not send a session ID,
1012 * so the PAC-based session secret is always preserved. It'll be
1013 * overwritten if the server refuses resumption.
1014 */
1015 if (s->session->session_id_length > 0) {
1016 s->ctx->stats.sess_miss++;
1017 if (!ssl_get_new_session(s, 0)) {
1018 goto f_err;
1019 }
1020 }
1021
1022 s->session->ssl_version = s->version;
1023 s->session->session_id_length = session_id_len;
1024 /* session_id_len could be 0 */
1025 if (session_id_len > 0)
1026 memcpy(s->session->session_id, PACKET_data(&session_id),
1027 session_id_len);
1028 }
1029
1030 /* Session version and negotiated protocol version should match */
1031 if (s->version != s->session->ssl_version) {
1032 al = SSL_AD_PROTOCOL_VERSION;
1033
1034 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1035 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1036 goto f_err;
1037 }
1038
1039 c = ssl_get_cipher_by_char(s, cipherchars);
1040 if (c == NULL) {
1041 /* unknown cipher */
1042 al = SSL_AD_ILLEGAL_PARAMETER;
1043 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1044 goto f_err;
1045 }
1046 /*
1047 * Now that we know the version, update the check to see if it's an allowed
1048 * version.
1049 */
1050 s->s3->tmp.min_ver = s->version;
1051 s->s3->tmp.max_ver = s->version;
1052 /*
1053 * If it is a disabled cipher we either didn't send it in client hello,
1054 * or it's not allowed for the selected protocol. So we return an error.
1055 */
1056 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1057 al = SSL_AD_ILLEGAL_PARAMETER;
1058 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1059 goto f_err;
1060 }
1061
1062 sk = ssl_get_ciphers_by_id(s);
1063 i = sk_SSL_CIPHER_find(sk, c);
1064 if (i < 0) {
1065 /* we did not say we would use this cipher */
1066 al = SSL_AD_ILLEGAL_PARAMETER;
1067 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1068 goto f_err;
1069 }
1070
1071 /*
1072 * Depending on the session caching (internal/external), the cipher
1073 * and/or cipher_id values may not be set. Make sure that cipher_id is
1074 * set and use it for comparison.
1075 */
1076 if (s->session->cipher)
1077 s->session->cipher_id = s->session->cipher->id;
1078 if (s->hit && (s->session->cipher_id != c->id)) {
1079 al = SSL_AD_ILLEGAL_PARAMETER;
1080 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1081 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1082 goto f_err;
1083 }
1084 s->s3->tmp.new_cipher = c;
1085 /* lets get the compression algorithm */
1086 /* COMPRESSION */
1087 if (!PACKET_get_1(pkt, &compression)) {
1088 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1089 al = SSL_AD_DECODE_ERROR;
1090 goto f_err;
1091 }
1092#ifdef OPENSSL_NO_COMP
1093 if (compression != 0) {
1094 al = SSL_AD_ILLEGAL_PARAMETER;
1095 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1096 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1097 goto f_err;
1098 }
1099 /*
1100 * If compression is disabled we'd better not try to resume a session
1101 * using compression.
1102 */
1103 if (s->session->compress_meth != 0) {
1104 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1105 goto f_err;
1106 }
1107#else
1108 if (s->hit && compression != s->session->compress_meth) {
1109 al = SSL_AD_ILLEGAL_PARAMETER;
1110 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1111 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1112 goto f_err;
1113 }
1114 if (compression == 0)
1115 comp = NULL;
1116 else if (!ssl_allow_compression(s)) {
1117 al = SSL_AD_ILLEGAL_PARAMETER;
1118 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1119 goto f_err;
1120 } else {
1121 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1122 }
1123
1124 if (compression != 0 && comp == NULL) {
1125 al = SSL_AD_ILLEGAL_PARAMETER;
1126 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1127 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1128 goto f_err;
1129 } else {
1130 s->s3->tmp.new_compression = comp;
1131 }
1132#endif
1133
1134 /* TLS extensions */
1135 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1136 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1137 goto err;
1138 }
1139
1140 if (PACKET_remaining(pkt) != 0) {
1141 /* wrong packet length */
1142 al = SSL_AD_DECODE_ERROR;
1143 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1144 goto f_err;
1145 }
1146#ifndef OPENSSL_NO_SCTP
1147 if (SSL_IS_DTLS(s) && s->hit) {
1148 unsigned char sctpauthkey[64];
1149 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1150
1151 /*
1152 * Add new shared key for SCTP-Auth, will be ignored if
1153 * no SCTP used.
1154 */
1155 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1156 sizeof(DTLS1_SCTP_AUTH_LABEL));
1157
1158 if (SSL_export_keying_material(s, sctpauthkey,
1159 sizeof(sctpauthkey),
1160 labelbuffer,
1161 sizeof(labelbuffer), NULL, 0, 0) <= 0)
1162 goto err;
1163
1164 BIO_ctrl(SSL_get_wbio(s),
1165 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1166 sizeof(sctpauthkey), sctpauthkey);
1167 }
1168#endif
1169
1170 return MSG_PROCESS_CONTINUE_READING;
1171 f_err:
1172 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1173 err:
1174 ossl_statem_set_error(s);
1175 return MSG_PROCESS_ERROR;
1176}
1177
1178MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1179{
1180 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1181 unsigned long cert_list_len, cert_len;
1182 X509 *x = NULL;
1183 const unsigned char *certstart, *certbytes;
1184 STACK_OF(X509) *sk = NULL;
1185 EVP_PKEY *pkey = NULL;
1186
1187 if ((sk = sk_X509_new_null()) == NULL) {
1188 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1189 goto err;
1190 }
1191
1192 if (!PACKET_get_net_3(pkt, &cert_list_len)
1193 || PACKET_remaining(pkt) != cert_list_len) {
1194 al = SSL_AD_DECODE_ERROR;
1195 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1196 goto f_err;
1197 }
1198 while (PACKET_remaining(pkt)) {
1199 if (!PACKET_get_net_3(pkt, &cert_len)
1200 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1201 al = SSL_AD_DECODE_ERROR;
1202 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1203 SSL_R_CERT_LENGTH_MISMATCH);
1204 goto f_err;
1205 }
1206
1207 certstart = certbytes;
1208 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1209 if (x == NULL) {
1210 al = SSL_AD_BAD_CERTIFICATE;
1211 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1212 goto f_err;
1213 }
1214 if (certbytes != (certstart + cert_len)) {
1215 al = SSL_AD_DECODE_ERROR;
1216 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1217 SSL_R_CERT_LENGTH_MISMATCH);
1218 goto f_err;
1219 }
1220 if (!sk_X509_push(sk, x)) {
1221 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1222 goto err;
1223 }
1224 x = NULL;
1225 }
1226
1227 i = ssl_verify_cert_chain(s, sk);
1228 /*
1229 * The documented interface is that SSL_VERIFY_PEER should be set in order
1230 * for client side verification of the server certificate to take place.
1231 * However, historically the code has only checked that *any* flag is set
1232 * to cause server verification to take place. Use of the other flags makes
1233 * no sense in client mode. An attempt to clean up the semantics was
1234 * reverted because at least one application *only* set
1235 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1236 * server verification to take place, after the clean up it silently did
1237 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1238 * sent to them because they are void functions. Therefore, we now use the
1239 * (less clean) historic behaviour of performing validation if any flag is
1240 * set. The *documented* interface remains the same.
1241 */
1242 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1243 al = ssl_verify_alarm_type(s->verify_result);
1244 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1245 SSL_R_CERTIFICATE_VERIFY_FAILED);
1246 goto f_err;
1247 }
1248 ERR_clear_error(); /* but we keep s->verify_result */
1249 if (i > 1) {
1250 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1251 al = SSL_AD_HANDSHAKE_FAILURE;
1252 goto f_err;
1253 }
1254
1255 s->session->peer_chain = sk;
1256 /*
1257 * Inconsistency alert: cert_chain does include the peer's certificate,
1258 * which we don't include in statem_srvr.c
1259 */
1260 x = sk_X509_value(sk, 0);
1261 sk = NULL;
1262 /*
1263 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1264 */
1265
1266 pkey = X509_get0_pubkey(x);
1267
1268 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1269 x = NULL;
1270 al = SSL3_AL_FATAL;
1271 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1272 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1273 goto f_err;
1274 }
1275
1276 i = ssl_cert_type(x, pkey);
1277 if (i < 0) {
1278 x = NULL;
1279 al = SSL3_AL_FATAL;
1280 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1281 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1282 goto f_err;
1283 }
1284
1285 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1286 if (exp_idx >= 0 && i != exp_idx
1287 && (exp_idx != SSL_PKEY_GOST_EC ||
1288 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1289 && i != SSL_PKEY_GOST01))) {
1290 x = NULL;
1291 al = SSL_AD_ILLEGAL_PARAMETER;
1292 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1293 SSL_R_WRONG_CERTIFICATE_TYPE);
1294 goto f_err;
1295 }
1296 s->session->peer_type = i;
1297
1298 X509_free(s->session->peer);
1299 X509_up_ref(x);
1300 s->session->peer = x;
1301 s->session->verify_result = s->verify_result;
1302
1303 x = NULL;
1304 ret = MSG_PROCESS_CONTINUE_READING;
1305 goto done;
1306
1307 f_err:
1308 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1309 err:
1310 ossl_statem_set_error(s);
1311 done:
1312 X509_free(x);
1313 sk_X509_pop_free(sk, X509_free);
1314 return ret;
1315}
1316
1317static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
1318{
1319#ifndef OPENSSL_NO_PSK
1320 PACKET psk_identity_hint;
1321
1322 /* PSK ciphersuites are preceded by an identity hint */
1323
1324 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1325 *al = SSL_AD_DECODE_ERROR;
1326 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
1327 return 0;
1328 }
1329
1330 /*
1331 * Store PSK identity hint for later use, hint is used in
1332 * tls_construct_client_key_exchange. Assume that the maximum length of
1333 * a PSK identity hint can be as long as the maximum length of a PSK
1334 * identity.
1335 */
1336 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1337 *al = SSL_AD_HANDSHAKE_FAILURE;
1338 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
1339 return 0;
1340 }
1341
1342 if (PACKET_remaining(&psk_identity_hint) == 0) {
1343 OPENSSL_free(s->session->psk_identity_hint);
1344 s->session->psk_identity_hint = NULL;
1345 } else if (!PACKET_strndup(&psk_identity_hint,
1346 &s->session->psk_identity_hint)) {
1347 *al = SSL_AD_INTERNAL_ERROR;
1348 return 0;
1349 }
1350
1351 return 1;
1352#else
1353 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
1354 *al = SSL_AD_INTERNAL_ERROR;
1355 return 0;
1356#endif
1357}
1358
1359static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1360{
1361#ifndef OPENSSL_NO_SRP
1362 PACKET prime, generator, salt, server_pub;
1363
1364 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1365 || !PACKET_get_length_prefixed_2(pkt, &generator)
1366 || !PACKET_get_length_prefixed_1(pkt, &salt)
1367 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1368 *al = SSL_AD_DECODE_ERROR;
1369 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
1370 return 0;
1371 }
1372
1373 if ((s->srp_ctx.N =
1374 BN_bin2bn(PACKET_data(&prime),
1375 PACKET_remaining(&prime), NULL)) == NULL
1376 || (s->srp_ctx.g =
1377 BN_bin2bn(PACKET_data(&generator),
1378 PACKET_remaining(&generator), NULL)) == NULL
1379 || (s->srp_ctx.s =
1380 BN_bin2bn(PACKET_data(&salt),
1381 PACKET_remaining(&salt), NULL)) == NULL
1382 || (s->srp_ctx.B =
1383 BN_bin2bn(PACKET_data(&server_pub),
1384 PACKET_remaining(&server_pub), NULL)) == NULL) {
1385 *al = SSL_AD_INTERNAL_ERROR;
1386 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
1387 return 0;
1388 }
1389
1390 if (!srp_verify_server_param(s, al)) {
1391 *al = SSL_AD_DECODE_ERROR;
1392 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
1393 return 0;
1394 }
1395
1396 /* We must check if there is a certificate */
1397 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1398 *pkey = X509_get0_pubkey(s->session->peer);
1399
1400 return 1;
1401#else
1402 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
1403 *al = SSL_AD_INTERNAL_ERROR;
1404 return 0;
1405#endif
1406}
1407
1408static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1409{
1410#ifndef OPENSSL_NO_DH
1411 PACKET prime, generator, pub_key;
1412 EVP_PKEY *peer_tmp = NULL;
1413
1414 DH *dh = NULL;
1415 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1416
1417 int check_bits = 0;
1418
1419 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1420 || !PACKET_get_length_prefixed_2(pkt, &generator)
1421 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1422 *al = SSL_AD_DECODE_ERROR;
1423 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
1424 return 0;
1425 }
1426
1427 peer_tmp = EVP_PKEY_new();
1428 dh = DH_new();
1429
1430 if (peer_tmp == NULL || dh == NULL) {
1431 *al = SSL_AD_INTERNAL_ERROR;
1432 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
1433 goto err;
1434 }
1435
1436 p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
1437 g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator), NULL);
1438 bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
1439 NULL);
1440 if (p == NULL || g == NULL || bnpub_key == NULL) {
1441 *al = SSL_AD_INTERNAL_ERROR;
1442 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1443 goto err;
1444 }
1445
1446 /* test non-zero pupkey */
1447 if (BN_is_zero(bnpub_key)) {
1448 *al = SSL_AD_DECODE_ERROR;
1449 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1450 goto err;
1451 }
1452
1453 if (!DH_set0_pqg(dh, p, NULL, g)) {
1454 *al = SSL_AD_INTERNAL_ERROR;
1455 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1456 goto err;
1457 }
1458 p = g = NULL;
1459
1460 if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) {
1461 *al = SSL_AD_DECODE_ERROR;
1462 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1463 goto err;
1464 }
1465
1466 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1467 *al = SSL_AD_INTERNAL_ERROR;
1468 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1469 goto err;
1470 }
1471 bnpub_key = NULL;
1472
1473 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1474 *al = SSL_AD_HANDSHAKE_FAILURE;
1475 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
1476 goto err;
1477 }
1478
1479 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1480 *al = SSL_AD_INTERNAL_ERROR;
1481 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
1482 goto err;
1483 }
1484
1485 s->s3->peer_tmp = peer_tmp;
1486
1487 /*
1488 * FIXME: This makes assumptions about which ciphersuites come with
1489 * public keys. We should have a less ad-hoc way of doing this
1490 */
1491 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1492 *pkey = X509_get0_pubkey(s->session->peer);
1493 /* else anonymous DH, so no certificate or pkey. */
1494
1495 return 1;
1496
1497 err:
1498 BN_free(p);
1499 BN_free(g);
1500 BN_free(bnpub_key);
1501 DH_free(dh);
1502 EVP_PKEY_free(peer_tmp);
1503
1504 return 0;
1505#else
1506 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
1507 *al = SSL_AD_INTERNAL_ERROR;
1508 return 0;
1509#endif
1510}
1511
1512static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1513{
1514#ifndef OPENSSL_NO_EC
1515 PACKET encoded_pt;
1516 const unsigned char *ecparams;
1517 int curve_nid;
1518 unsigned int curve_flags;
1519 EVP_PKEY_CTX *pctx = NULL;
1520
1521 /*
1522 * Extract elliptic curve parameters and the server's ephemeral ECDH
1523 * public key. For now we only support named (not generic) curves and
1524 * ECParameters in this case is just three bytes.
1525 */
1526 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1527 *al = SSL_AD_DECODE_ERROR;
1528 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
1529 return 0;
1530 }
1531 /*
1532 * Check curve is one of our preferences, if not server has sent an
1533 * invalid curve. ECParameters is 3 bytes.
1534 */
1535 if (!tls1_check_curve(s, ecparams, 3)) {
1536 *al = SSL_AD_DECODE_ERROR;
1537 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
1538 return 0;
1539 }
1540
1541 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1542
1543 if (curve_nid == 0) {
1544 *al = SSL_AD_INTERNAL_ERROR;
1545 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
1546 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1547 return 0;
1548 }
1549
1550 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1551 EVP_PKEY *key = EVP_PKEY_new();
1552
1553 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1554 *al = SSL_AD_INTERNAL_ERROR;
1555 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1556 EVP_PKEY_free(key);
1557 return 0;
1558 }
1559 s->s3->peer_tmp = key;
1560 } else {
1561 /* Set up EVP_PKEY with named curve as parameters */
1562 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1563 if (pctx == NULL
1564 || EVP_PKEY_paramgen_init(pctx) <= 0
1565 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1566 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1567 *al = SSL_AD_INTERNAL_ERROR;
1568 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1569 EVP_PKEY_CTX_free(pctx);
1570 return 0;
1571 }
1572 EVP_PKEY_CTX_free(pctx);
1573 pctx = NULL;
1574 }
1575
1576 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1577 *al = SSL_AD_DECODE_ERROR;
1578 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
1579 return 0;
1580 }
1581
1582 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1583 PACKET_data(&encoded_pt),
1584 PACKET_remaining(&encoded_pt))) {
1585 *al = SSL_AD_DECODE_ERROR;
1586 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
1587 return 0;
1588 }
1589
1590 /*
1591 * The ECC/TLS specification does not mention the use of DSA to sign
1592 * ECParameters in the server key exchange message. We do support RSA
1593 * and ECDSA.
1594 */
1595 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1596 *pkey = X509_get0_pubkey(s->session->peer);
1597 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1598 *pkey = X509_get0_pubkey(s->session->peer);
1599 /* else anonymous ECDH, so no certificate or pkey. */
1600
1601 return 1;
1602#else
1603 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
1604 *al = SSL_AD_INTERNAL_ERROR;
1605 return 0;
1606#endif
1607}
1608
1609MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1610{
1611 int al = -1;
1612 long alg_k;
1613 EVP_PKEY *pkey = NULL;
1614 PACKET save_param_start, signature;
1615
1616 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1617
1618 save_param_start = *pkt;
1619
1620#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1621 EVP_PKEY_free(s->s3->peer_tmp);
1622 s->s3->peer_tmp = NULL;
1623#endif
1624
1625 if (alg_k & SSL_PSK) {
1626 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1627 goto err;
1628 }
1629
1630 /* Nothing else to do for plain PSK or RSAPSK */
1631 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1632 } else if (alg_k & SSL_kSRP) {
1633 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
1634 goto err;
1635 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1636 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1637 goto err;
1638 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1639 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1640 goto err;
1641 } else if (alg_k) {
1642 al = SSL_AD_UNEXPECTED_MESSAGE;
1643 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1644 goto err;
1645 }
1646
1647 /* if it was signed, check the signature */
1648 if (pkey != NULL) {
1649 PACKET params;
1650 int maxsig;
1651 const EVP_MD *md = NULL;
1652 EVP_MD_CTX *md_ctx;
1653
1654 /*
1655 * |pkt| now points to the beginning of the signature, so the difference
1656 * equals the length of the parameters.
1657 */
1658 if (!PACKET_get_sub_packet(&save_param_start, &params,
1659 PACKET_remaining(&save_param_start) -
1660 PACKET_remaining(pkt))) {
1661 al = SSL_AD_INTERNAL_ERROR;
1662 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1663 goto err;
1664 }
1665
1666 if (SSL_USE_SIGALGS(s)) {
1667 const unsigned char *sigalgs;
1668 int rv;
1669 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1670 al = SSL_AD_DECODE_ERROR;
1671 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1672 goto err;
1673 }
1674 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1675 if (rv == -1) {
1676 al = SSL_AD_INTERNAL_ERROR;
1677 goto err;
1678 } else if (rv == 0) {
1679 al = SSL_AD_DECODE_ERROR;
1680 goto err;
1681 }
1682#ifdef SSL_DEBUG
1683 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1684#endif
1685 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1686 md = EVP_md5_sha1();
1687 } else {
1688 md = EVP_sha1();
1689 }
1690
1691 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1692 || PACKET_remaining(pkt) != 0) {
1693 al = SSL_AD_DECODE_ERROR;
1694 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1695 goto err;
1696 }
1697 maxsig = EVP_PKEY_size(pkey);
1698 if (maxsig < 0) {
1699 al = SSL_AD_INTERNAL_ERROR;
1700 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1701 goto err;
1702 }
1703
1704 /*
1705 * Check signature length
1706 */
1707 if (PACKET_remaining(&signature) > (size_t)maxsig) {
1708 /* wrong packet length */
1709 al = SSL_AD_DECODE_ERROR;
1710 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1711 SSL_R_WRONG_SIGNATURE_LENGTH);
1712 goto err;
1713 }
1714
1715 md_ctx = EVP_MD_CTX_new();
1716 if (md_ctx == NULL) {
1717 al = SSL_AD_INTERNAL_ERROR;
1718 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1719 goto err;
1720 }
1721
1722 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1723 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1724 SSL3_RANDOM_SIZE) <= 0
1725 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1726 SSL3_RANDOM_SIZE) <= 0
1727 || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
1728 PACKET_remaining(&params)) <= 0) {
1729 EVP_MD_CTX_free(md_ctx);
1730 al = SSL_AD_INTERNAL_ERROR;
1731 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1732 goto err;
1733 }
1734 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1735 PACKET_remaining(&signature), pkey) <= 0) {
1736 /* bad signature */
1737 EVP_MD_CTX_free(md_ctx);
1738 al = SSL_AD_DECRYPT_ERROR;
1739 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1740 goto err;
1741 }
1742 EVP_MD_CTX_free(md_ctx);
1743 } else {
1744 /* aNULL, aSRP or PSK do not need public keys */
1745 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1746 && !(alg_k & SSL_PSK)) {
1747 /* Might be wrong key type, check it */
1748 if (ssl3_check_cert_and_algorithm(s)) {
1749 /* Otherwise this shouldn't happen */
1750 al = SSL_AD_INTERNAL_ERROR;
1751 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1752 } else {
1753 al = SSL_AD_DECODE_ERROR;
1754 }
1755 goto err;
1756 }
1757 /* still data left over */
1758 if (PACKET_remaining(pkt) != 0) {
1759 al = SSL_AD_DECODE_ERROR;
1760 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1761 goto err;
1762 }
1763 }
1764
1765 return MSG_PROCESS_CONTINUE_READING;
1766 err:
1767 if (al != -1)
1768 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1769 ossl_statem_set_error(s);
1770 return MSG_PROCESS_ERROR;
1771}
1772
1773MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1774{
1775 int ret = MSG_PROCESS_ERROR;
1776 unsigned int list_len, ctype_num, i, name_len;
1777 X509_NAME *xn = NULL;
1778 const unsigned char *data;
1779 const unsigned char *namestart, *namebytes;
1780 STACK_OF(X509_NAME) *ca_sk = NULL;
1781
1782 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1783 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1784 goto err;
1785 }
1786
1787 /* get the certificate types */
1788 if (!PACKET_get_1(pkt, &ctype_num)
1789 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1790 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1791 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1792 goto err;
1793 }
1794 OPENSSL_free(s->cert->ctypes);
1795 s->cert->ctypes = NULL;
1796 if (ctype_num > SSL3_CT_NUMBER) {
1797 /* If we exceed static buffer copy all to cert structure */
1798 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1799 if (s->cert->ctypes == NULL) {
1800 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1801 goto err;
1802 }
1803 memcpy(s->cert->ctypes, data, ctype_num);
1804 s->cert->ctype_num = (size_t)ctype_num;
1805 ctype_num = SSL3_CT_NUMBER;
1806 }
1807 for (i = 0; i < ctype_num; i++)
1808 s->s3->tmp.ctype[i] = data[i];
1809
1810 if (SSL_USE_SIGALGS(s)) {
1811 if (!PACKET_get_net_2(pkt, &list_len)
1812 || !PACKET_get_bytes(pkt, &data, list_len)) {
1813 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1814 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1815 SSL_R_LENGTH_MISMATCH);
1816 goto err;
1817 }
1818
1819 /* Clear certificate digests and validity flags */
1820 for (i = 0; i < SSL_PKEY_NUM; i++) {
1821 s->s3->tmp.md[i] = NULL;
1822 s->s3->tmp.valid_flags[i] = 0;
1823 }
1824 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1825 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1826 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1827 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1828 goto err;
1829 }
1830 if (!tls1_process_sigalgs(s)) {
1831 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1832 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1833 goto err;
1834 }
1835 } else {
1836 ssl_set_default_md(s);
1837 }
1838
1839 /* get the CA RDNs */
1840 if (!PACKET_get_net_2(pkt, &list_len)
1841 || PACKET_remaining(pkt) != list_len) {
1842 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1843 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1844 goto err;
1845 }
1846
1847 while (PACKET_remaining(pkt)) {
1848 if (!PACKET_get_net_2(pkt, &name_len)
1849 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1850 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1851 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1852 SSL_R_LENGTH_MISMATCH);
1853 goto err;
1854 }
1855
1856 namestart = namebytes;
1857
1858 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1859 name_len)) == NULL) {
1860 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1861 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1862 goto err;
1863 }
1864
1865 if (namebytes != (namestart + name_len)) {
1866 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1867 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1868 SSL_R_CA_DN_LENGTH_MISMATCH);
1869 goto err;
1870 }
1871 if (!sk_X509_NAME_push(ca_sk, xn)) {
1872 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1873 goto err;
1874 }
1875 xn = NULL;
1876 }
1877
1878 /* we should setup a certificate to return.... */
1879 s->s3->tmp.cert_req = 1;
1880 s->s3->tmp.ctype_num = ctype_num;
1881 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1882 s->s3->tmp.ca_names = ca_sk;
1883 ca_sk = NULL;
1884
1885 ret = MSG_PROCESS_CONTINUE_PROCESSING;
1886 goto done;
1887 err:
1888 ossl_statem_set_error(s);
1889 done:
1890 X509_NAME_free(xn);
1891 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1892 return ret;
1893}
1894
1895static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1896{
1897 return (X509_NAME_cmp(*a, *b));
1898}
1899
1900MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1901{
1902 int al;
1903 unsigned int ticklen;
1904 unsigned long ticket_lifetime_hint;
1905
1906 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1907 || !PACKET_get_net_2(pkt, &ticklen)
1908 || PACKET_remaining(pkt) != ticklen) {
1909 al = SSL_AD_DECODE_ERROR;
1910 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1911 goto f_err;
1912 }
1913
1914 /* Server is allowed to change its mind and send an empty ticket. */
1915 if (ticklen == 0)
1916 return MSG_PROCESS_CONTINUE_READING;
1917
1918 if (s->session->session_id_length > 0) {
1919 int i = s->session_ctx->session_cache_mode;
1920 SSL_SESSION *new_sess;
1921 /*
1922 * We reused an existing session, so we need to replace it with a new
1923 * one
1924 */
1925 if (i & SSL_SESS_CACHE_CLIENT) {
1926 /*
1927 * Remove the old session from the cache. We carry on if this fails
1928 */
1929 SSL_CTX_remove_session(s->session_ctx, s->session);
1930 }
1931
1932 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1933 al = SSL_AD_INTERNAL_ERROR;
1934 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1935 goto f_err;
1936 }
1937
1938 SSL_SESSION_free(s->session);
1939 s->session = new_sess;
1940 }
1941
1942 OPENSSL_free(s->session->tlsext_tick);
1943 s->session->tlsext_ticklen = 0;
1944
1945 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1946 if (s->session->tlsext_tick == NULL) {
1947 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1948 goto err;
1949 }
1950 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1951 al = SSL_AD_DECODE_ERROR;
1952 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1953 goto f_err;
1954 }
1955
1956 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1957 s->session->tlsext_ticklen = ticklen;
1958 /*
1959 * There are two ways to detect a resumed ticket session. One is to set
1960 * an appropriate session ID and then the server must return a match in
1961 * ServerHello. This allows the normal client session ID matching to work
1962 * and we know much earlier that the ticket has been accepted. The
1963 * other way is to set zero length session ID when the ticket is
1964 * presented and rely on the handshake to determine session resumption.
1965 * We choose the former approach because this fits in with assumptions
1966 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1967 * SHA256 is disabled) hash of the ticket.
1968 */
1969 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
1970 s->session->session_id, &s->session->session_id_length,
1971 EVP_sha256(), NULL)) {
1972 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
1973 goto err;
1974 }
1975 return MSG_PROCESS_CONTINUE_READING;
1976 f_err:
1977 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1978 err:
1979 ossl_statem_set_error(s);
1980 return MSG_PROCESS_ERROR;
1981}
1982
1983MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1984{
1985 int al;
1986 unsigned long resplen;
1987 unsigned int type;
1988
1989 if (!PACKET_get_1(pkt, &type)
1990 || type != TLSEXT_STATUSTYPE_ocsp) {
1991 al = SSL_AD_DECODE_ERROR;
1992 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1993 goto f_err;
1994 }
1995 if (!PACKET_get_net_3(pkt, &resplen)
1996 || PACKET_remaining(pkt) != resplen) {
1997 al = SSL_AD_DECODE_ERROR;
1998 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1999 goto f_err;
2000 }
2001 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2002 if (s->tlsext_ocsp_resp == NULL) {
2003 al = SSL_AD_INTERNAL_ERROR;
2004 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2005 goto f_err;
2006 }
2007 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2008 al = SSL_AD_DECODE_ERROR;
2009 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2010 goto f_err;
2011 }
2012 s->tlsext_ocsp_resplen = resplen;
2013 return MSG_PROCESS_CONTINUE_READING;
2014 f_err:
2015 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2016 ossl_statem_set_error(s);
2017 return MSG_PROCESS_ERROR;
2018}
2019
2020MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2021{
2022 if (PACKET_remaining(pkt) > 0) {
2023 /* should contain no data */
2024 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2025 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2026 ossl_statem_set_error(s);
2027 return MSG_PROCESS_ERROR;
2028 }
2029#ifndef OPENSSL_NO_SRP
2030 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2031 if (SRP_Calc_A_param(s) <= 0) {
2032 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2034 ossl_statem_set_error(s);
2035 return MSG_PROCESS_ERROR;
2036 }
2037 }
2038#endif
2039
2040 /*
2041 * at this point we check that we have the required stuff from
2042 * the server
2043 */
2044 if (!ssl3_check_cert_and_algorithm(s)) {
2045 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2046 ossl_statem_set_error(s);
2047 return MSG_PROCESS_ERROR;
2048 }
2049
2050 /*
2051 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2052 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2053 * message, or NULL and -1 otherwise
2054 */
2055 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
2056 int ret;
2057 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2058 if (ret == 0) {
2059 ssl3_send_alert(s, SSL3_AL_FATAL,
2060 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
2061 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
2062 SSL_R_INVALID_STATUS_RESPONSE);
2063 return MSG_PROCESS_ERROR;
2064 }
2065 if (ret < 0) {
2066 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2067 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
2068 return MSG_PROCESS_ERROR;
2069 }
2070 }
2071#ifndef OPENSSL_NO_CT
2072 if (s->ct_validation_callback != NULL) {
2073 /* Note we validate the SCTs whether or not we abort on error */
2074 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2075 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2076 return MSG_PROCESS_ERROR;
2077 }
2078 }
2079#endif
2080
2081#ifndef OPENSSL_NO_SCTP
2082 /* Only applies to renegotiation */
2083 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2084 && s->renegotiate != 0)
2085 return MSG_PROCESS_CONTINUE_PROCESSING;
2086 else
2087#endif
2088 return MSG_PROCESS_FINISHED_READING;
2089}
2090
2091static int tls_construct_cke_psk_preamble(SSL *s, unsigned char **p,
2092 size_t *pskhdrlen, int *al)
2093{
2094#ifndef OPENSSL_NO_PSK
2095 int ret = 0;
2096 /*
2097 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2098 * \0-terminated identity. The last byte is for us for simulating
2099 * strnlen.
2100 */
2101 char identity[PSK_MAX_IDENTITY_LEN + 1];
2102 size_t identitylen = 0;
2103 unsigned char psk[PSK_MAX_PSK_LEN];
2104 unsigned char *tmppsk = NULL;
2105 char *tmpidentity = NULL;
2106 size_t psklen = 0;
2107
2108 if (s->psk_client_callback == NULL) {
2109 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
2110 *al = SSL_AD_INTERNAL_ERROR;
2111 goto err;
2112 }
2113
2114 memset(identity, 0, sizeof(identity));
2115
2116 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2117 identity, sizeof(identity) - 1,
2118 psk, sizeof(psk));
2119
2120 if (psklen > PSK_MAX_PSK_LEN) {
2121 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2122 *al = SSL_AD_HANDSHAKE_FAILURE;
2123 goto err;
2124 } else if (psklen == 0) {
2125 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
2126 SSL_R_PSK_IDENTITY_NOT_FOUND);
2127 *al = SSL_AD_HANDSHAKE_FAILURE;
2128 goto err;
2129 }
2130
2131 identitylen = strlen(identity);
2132 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2133 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2134 *al = SSL_AD_HANDSHAKE_FAILURE;
2135 goto err;
2136 }
2137
2138 tmppsk = OPENSSL_memdup(psk, psklen);
2139 tmpidentity = OPENSSL_strdup(identity);
2140 if (tmppsk == NULL || tmpidentity == NULL) {
2141 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2142 *al = SSL_AD_INTERNAL_ERROR;
2143 goto err;
2144 }
2145
2146 OPENSSL_free(s->s3->tmp.psk);
2147 s->s3->tmp.psk = tmppsk;
2148 s->s3->tmp.psklen = psklen;
2149 tmppsk = NULL;
2150 OPENSSL_free(s->session->psk_identity);
2151 s->session->psk_identity = tmpidentity;
2152 tmpidentity = NULL;
2153 s2n(identitylen, *p);
2154 memcpy(*p, identity, identitylen);
2155 *pskhdrlen = 2 + identitylen;
2156 *p += identitylen;
2157
2158 ret = 1;
2159
2160 err:
2161 OPENSSL_cleanse(psk, psklen);
2162 OPENSSL_cleanse(identity, sizeof(identity));
2163 OPENSSL_clear_free(tmppsk, psklen);
2164 OPENSSL_clear_free(tmpidentity, identitylen);
2165
2166 return ret;
2167#else
2168 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2169 *al = SSL_AD_INTERNAL_ERROR;
2170 return 0;
2171#endif
2172}
2173
2174static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)
2175{
2176#ifndef OPENSSL_NO_RSA
2177 unsigned char *q;
2178 EVP_PKEY *pkey = NULL;
2179 EVP_PKEY_CTX *pctx = NULL;
2180 size_t enclen;
2181 unsigned char *pms = NULL;
2182 size_t pmslen = 0;
2183
2184 if (s->session->peer == NULL) {
2185 /*
2186 * We should always have a server certificate with SSL_kRSA.
2187 */
2188 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2189 return 0;
2190 }
2191
2192 pkey = X509_get0_pubkey(s->session->peer);
2193 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2194 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2195 return 0;
2196 }
2197
2198 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2199 pms = OPENSSL_malloc(pmslen);
2200 if (pms == NULL) {
2201 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
2202 *al = SSL_AD_INTERNAL_ERROR;
2203 return 0;
2204 }
2205
2206 pms[0] = s->client_version >> 8;
2207 pms[1] = s->client_version & 0xff;
2208 if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {
2209 goto err;
2210 }
2211
2212 q = *p;
2213 /* Fix buf for TLS and beyond */
2214 if (s->version > SSL3_VERSION)
2215 *p += 2;
2216 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2217 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2218 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2219 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
2220 goto err;
2221 }
2222 if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {
2223 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
2224 goto err;
2225 }
2226 *len = enclen;
2227 EVP_PKEY_CTX_free(pctx);
2228 pctx = NULL;
2229# ifdef PKCS1_CHECK
2230 if (s->options & SSL_OP_PKCS1_CHECK_1)
2231 (*p)[1]++;
2232 if (s->options & SSL_OP_PKCS1_CHECK_2)
2233 tmp_buf[0] = 0x70;
2234# endif
2235
2236 /* Fix buf for TLS and beyond */
2237 if (s->version > SSL3_VERSION) {
2238 s2n(*len, q);
2239 *len += 2;
2240 }
2241
2242 s->s3->tmp.pms = pms;
2243 s->s3->tmp.pmslen = pmslen;
2244
2245 return 1;
2246 err:
2247 OPENSSL_clear_free(pms, pmslen);
2248 EVP_PKEY_CTX_free(pctx);
2249
2250 return 0;
2251#else
2252 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2253 *al = SSL_AD_INTERNAL_ERROR;
2254 return 0;
2255#endif
2256}
2257
2258static int tls_construct_cke_dhe(SSL *s, unsigned char **p, int *len, int *al)
2259{
2260#ifndef OPENSSL_NO_DH
2261 DH *dh_clnt = NULL;
2262 const BIGNUM *pub_key;
2263 EVP_PKEY *ckey = NULL, *skey = NULL;
2264
2265 skey = s->s3->peer_tmp;
2266 if (skey == NULL) {
2267 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2268 return 0;
2269 }
2270 ckey = ssl_generate_pkey(skey);
2271 if (ckey == NULL) {
2272 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2273 return 0;
2274 }
2275
2276 dh_clnt = EVP_PKEY_get0_DH(ckey);
2277
2278 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2279 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2280 EVP_PKEY_free(ckey);
2281 return 0;
2282 }
2283
2284 /* send off the data */
2285 DH_get0_key(dh_clnt, &pub_key, NULL);
2286 *len = BN_num_bytes(pub_key);
2287 s2n(*len, *p);
2288 BN_bn2bin(pub_key, *p);
2289 *len += 2;
2290 EVP_PKEY_free(ckey);
2291
2292 return 1;
2293#else
2294 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2295 *al = SSL_AD_INTERNAL_ERROR;
2296 return 0;
2297#endif
2298}
2299
2300static int tls_construct_cke_ecdhe(SSL *s, unsigned char **p, int *len, int *al)
2301{
2302#ifndef OPENSSL_NO_EC
2303 unsigned char *encodedPoint = NULL;
2304 int encoded_pt_len = 0;
2305 EVP_PKEY *ckey = NULL, *skey = NULL;
2306
2307 skey = s->s3->peer_tmp;
2308 if (skey == NULL) {
2309 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2310 return 0;
2311 }
2312
2313 ckey = ssl_generate_pkey(skey);
2314 if (ckey == NULL) {
2315 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2316 goto err;
2317 }
2318
2319 if (ssl_derive(s, ckey, skey) == 0) {
2320 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
2321 goto err;
2322 }
2323
2324 /* Generate encoding of client key */
2325 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
2326
2327 if (encoded_pt_len == 0) {
2328 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
2329 goto err;
2330 }
2331
2332 EVP_PKEY_free(ckey);
2333 ckey = NULL;
2334
2335 *len = encoded_pt_len;
2336
2337 /* length of encoded point */
2338 **p = *len;
2339 *p += 1;
2340 /* copy the point */
2341 memcpy(*p, encodedPoint, *len);
2342 /* increment len to account for length field */
2343 *len += 1;
2344
2345 OPENSSL_free(encodedPoint);
2346
2347 return 1;
2348 err:
2349 EVP_PKEY_free(ckey);
2350 return 0;
2351#else
2352 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2353 *al = SSL_AD_INTERNAL_ERROR;
2354 return 0;
2355#endif
2356}
2357
2358static int tls_construct_cke_gost(SSL *s, unsigned char **p, int *len, int *al)
2359{
2360#ifndef OPENSSL_NO_GOST
2361 /* GOST key exchange message creation */
2362 EVP_PKEY_CTX *pkey_ctx = NULL;
2363 X509 *peer_cert;
2364 size_t msglen;
2365 unsigned int md_len;
2366 unsigned char shared_ukm[32], tmp[256];
2367 EVP_MD_CTX *ukm_hash = NULL;
2368 int dgst_nid = NID_id_GostR3411_94;
2369 unsigned char *pms = NULL;
2370 size_t pmslen = 0;
2371
2372 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2373 dgst_nid = NID_id_GostR3411_2012_256;
2374
2375 /*
2376 * Get server sertificate PKEY and create ctx from it
2377 */
2378 peer_cert = s->session->peer;
2379 if (!peer_cert) {
2380 *al = SSL_AD_HANDSHAKE_FAILURE;
2381 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
2382 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2383 return 0;
2384 }
2385
2386 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2387 if (pkey_ctx == NULL) {
2388 *al = SSL_AD_INTERNAL_ERROR;
2389 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2390 return 0;
2391 }
2392 /*
2393 * If we have send a certificate, and certificate key
2394 * parameters match those of server certificate, use
2395 * certificate key for key exchange
2396 */
2397
2398 /* Otherwise, generate ephemeral key pair */
2399 pmslen = 32;
2400 pms = OPENSSL_malloc(pmslen);
2401 if (pms == NULL) {
2402 *al = SSL_AD_INTERNAL_ERROR;
2403 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2404 goto err;
2405 }
2406
2407 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2408 /* Generate session key */
2409 || RAND_bytes(pms, pmslen) <= 0) {
2410 *al = SSL_AD_INTERNAL_ERROR;
2411 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2412 goto err;
2413 };
2414 /*
2415 * Compute shared IV and store it in algorithm-specific context
2416 * data
2417 */
2418 ukm_hash = EVP_MD_CTX_new();
2419 if (ukm_hash == NULL
2420 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2421 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2422 SSL3_RANDOM_SIZE) <= 0
2423 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2424 SSL3_RANDOM_SIZE) <= 0
2425 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2426 *al = SSL_AD_INTERNAL_ERROR;
2427 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2428 goto err;
2429 }
2430 EVP_MD_CTX_free(ukm_hash);
2431 ukm_hash = NULL;
2432 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2433 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2434 *al = SSL_AD_INTERNAL_ERROR;
2435 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2436 goto err;
2437 }
2438 /* Make GOST keytransport blob message */
2439 /*
2440 * Encapsulate it into sequence
2441 */
2442 *((*p)++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2443 msglen = 255;
2444 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2445 *al = SSL_AD_INTERNAL_ERROR;
2446 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2447 goto err;
2448 }
2449 if (msglen >= 0x80) {
2450 *((*p)++) = 0x81;
2451 *((*p)++) = msglen & 0xff;
2452 *len = msglen + 3;
2453 } else {
2454 *((*p)++) = msglen & 0xff;
2455 *len = msglen + 2;
2456 }
2457 memcpy(*p, tmp, msglen);
2458
2459 EVP_PKEY_CTX_free(pkey_ctx);
2460 s->s3->tmp.pms = pms;
2461 s->s3->tmp.pmslen = pmslen;
2462
2463 return 1;
2464 err:
2465 EVP_PKEY_CTX_free(pkey_ctx);
2466 OPENSSL_clear_free(pms, pmslen);
2467 EVP_MD_CTX_free(ukm_hash);
2468 return 0;
2469#else
2470 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2471 *al = SSL_AD_INTERNAL_ERROR;
2472 return 0;
2473#endif
2474}
2475
2476static int tls_construct_cke_srp(SSL *s, unsigned char **p, int *len, int *al)
2477{
2478#ifndef OPENSSL_NO_SRP
2479 if (s->srp_ctx.A != NULL) {
2480 /* send off the data */
2481 *len = BN_num_bytes(s->srp_ctx.A);
2482 s2n(*len, *p);
2483 BN_bn2bin(s->srp_ctx.A, *p);
2484 *len += 2;
2485 } else {
2486 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2487 return 0;
2488 }
2489 OPENSSL_free(s->session->srp_username);
2490 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2491 if (s->session->srp_username == NULL) {
2492 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
2493 return 0;
2494 }
2495
2496 return 1;
2497#else
2498 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2499 *al = SSL_AD_INTERNAL_ERROR;
2500 return 0;
2501#endif
2502}
2503
2504int tls_construct_client_key_exchange(SSL *s)
2505{
2506 unsigned char *p;
2507 int len;
2508 size_t pskhdrlen = 0;
2509 unsigned long alg_k;
2510 int al = -1;
2511
2512 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2513
2514 p = ssl_handshake_start(s);
2515
2516 if ((alg_k & SSL_PSK)
2517 && !tls_construct_cke_psk_preamble(s, &p, &pskhdrlen, &al))
2518 goto err;
2519
2520 if (alg_k & SSL_kPSK) {
2521 len = 0;
2522 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2523 if (!tls_construct_cke_rsa(s, &p, &len, &al))
2524 goto err;
2525 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2526 if (!tls_construct_cke_dhe(s, &p, &len, &al))
2527 goto err;
2528 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2529 if (!tls_construct_cke_ecdhe(s, &p, &len, &al))
2530 goto err;
2531 } else if (alg_k & SSL_kGOST) {
2532 if (!tls_construct_cke_gost(s, &p, &len, &al))
2533 goto err;
2534 } else if (alg_k & SSL_kSRP) {
2535 if (!tls_construct_cke_srp(s, &p, &len, &al))
2536 goto err;
2537 } else {
2538 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2539 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2540 goto err;
2541 }
2542
2543 len += pskhdrlen;
2544
2545 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, len)) {
2546 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2547 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2548 goto err;
2549 }
2550
2551 return 1;
2552 err:
2553 if (al != -1)
2554 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2555 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2556 s->s3->tmp.pms = NULL;
2557#ifndef OPENSSL_NO_PSK
2558 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2559 s->s3->tmp.psk = NULL;
2560#endif
2561 ossl_statem_set_error(s);
2562 return 0;
2563}
2564
2565int tls_client_key_exchange_post_work(SSL *s)
2566{
2567 unsigned char *pms = NULL;
2568 size_t pmslen = 0;
2569
2570 pms = s->s3->tmp.pms;
2571 pmslen = s->s3->tmp.pmslen;
2572
2573#ifndef OPENSSL_NO_SRP
2574 /* Check for SRP */
2575 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2576 if (!srp_generate_client_master_secret(s)) {
2577 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2578 ERR_R_INTERNAL_ERROR);
2579 goto err;
2580 }
2581 return 1;
2582 }
2583#endif
2584
2585 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2586 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2587 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2588 goto err;
2589 }
2590 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2591 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2592 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2593 /* ssl_generate_master_secret frees the pms even on error */
2594 pms = NULL;
2595 pmslen = 0;
2596 goto err;
2597 }
2598 pms = NULL;
2599 pmslen = 0;
2600
2601#ifndef OPENSSL_NO_SCTP
2602 if (SSL_IS_DTLS(s)) {
2603 unsigned char sctpauthkey[64];
2604 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2605
2606 /*
2607 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2608 * used.
2609 */
2610 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2611 sizeof(DTLS1_SCTP_AUTH_LABEL));
2612
2613 if (SSL_export_keying_material(s, sctpauthkey,
2614 sizeof(sctpauthkey), labelbuffer,
2615 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2616 goto err;
2617
2618 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2619 sizeof(sctpauthkey), sctpauthkey);
2620 }
2621#endif
2622
2623 return 1;
2624 err:
2625 OPENSSL_clear_free(pms, pmslen);
2626 s->s3->tmp.pms = NULL;
2627 return 0;
2628}
2629
2630int tls_construct_client_verify(SSL *s)
2631{
2632 unsigned char *p;
2633 EVP_PKEY *pkey;
2634 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2635 EVP_MD_CTX *mctx;
2636 unsigned u = 0;
2637 unsigned long n = 0;
2638 long hdatalen = 0;
2639 void *hdata;
2640
2641 mctx = EVP_MD_CTX_new();
2642 if (mctx == NULL) {
2643 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2644 goto err;
2645 }
2646
2647 p = ssl_handshake_start(s);
2648 pkey = s->cert->key->privatekey;
2649
2650 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2651 if (hdatalen <= 0) {
2652 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2653 goto err;
2654 }
2655 if (SSL_USE_SIGALGS(s)) {
2656 if (!tls12_get_sigandhash(p, pkey, md)) {
2657 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2658 goto err;
2659 }
2660 p += 2;
2661 n = 2;
2662 }
2663#ifdef SSL_DEBUG
2664 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2665#endif
2666 if (!EVP_SignInit_ex(mctx, md, NULL)
2667 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2668 || (s->version == SSL3_VERSION
2669 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2670 s->session->master_key_length,
2671 s->session->master_key))
2672 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
2673 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2674 goto err;
2675 }
2676#ifndef OPENSSL_NO_GOST
2677 {
2678 int pktype = EVP_PKEY_id(pkey);
2679 if (pktype == NID_id_GostR3410_2001
2680 || pktype == NID_id_GostR3410_2012_256
2681 || pktype == NID_id_GostR3410_2012_512)
2682 BUF_reverse(p + 2, NULL, u);
2683 }
2684#endif
2685
2686 s2n(u, p);
2687 n += u + 2;
2688 /* Digest cached records and discard handshake buffer */
2689 if (!ssl3_digest_cached_records(s, 0))
2690 goto err;
2691 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2692 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2693 goto err;
2694 }
2695
2696 EVP_MD_CTX_free(mctx);
2697 return 1;
2698 err:
2699 EVP_MD_CTX_free(mctx);
2700 return 0;
2701}
2702
2703/*
2704 * Check a certificate can be used for client authentication. Currently check
2705 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2706 * certificates can be used and optionally checks suitability for Suite B.
2707 */
2708static int ssl3_check_client_certificate(SSL *s)
2709{
2710 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2711 return 0;
2712 /* If no suitable signature algorithm can't use certificate */
2713 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2714 return 0;
2715 /*
2716 * If strict mode check suitability of chain before using it. This also
2717 * adjusts suite B digest if necessary.
2718 */
2719 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2720 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2721 return 0;
2722 return 1;
2723}
2724
2725WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2726{
2727 X509 *x509 = NULL;
2728 EVP_PKEY *pkey = NULL;
2729 int i;
2730
2731 if (wst == WORK_MORE_A) {
2732 /* Let cert callback update client certificates if required */
2733 if (s->cert->cert_cb) {
2734 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2735 if (i < 0) {
2736 s->rwstate = SSL_X509_LOOKUP;
2737 return WORK_MORE_A;
2738 }
2739 if (i == 0) {
2740 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2741 ossl_statem_set_error(s);
2742 return 0;
2743 }
2744 s->rwstate = SSL_NOTHING;
2745 }
2746 if (ssl3_check_client_certificate(s))
2747 return WORK_FINISHED_CONTINUE;
2748
2749 /* Fall through to WORK_MORE_B */
2750 wst = WORK_MORE_B;
2751 }
2752
2753 /* We need to get a client cert */
2754 if (wst == WORK_MORE_B) {
2755 /*
2756 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2757 * return(-1); We then get retied later
2758 */
2759 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2760 if (i < 0) {
2761 s->rwstate = SSL_X509_LOOKUP;
2762 return WORK_MORE_B;
2763 }
2764 s->rwstate = SSL_NOTHING;
2765 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2766 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2767 i = 0;
2768 } else if (i == 1) {
2769 i = 0;
2770 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2771 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2772 }
2773
2774 X509_free(x509);
2775 EVP_PKEY_free(pkey);
2776 if (i && !ssl3_check_client_certificate(s))
2777 i = 0;
2778 if (i == 0) {
2779 if (s->version == SSL3_VERSION) {
2780 s->s3->tmp.cert_req = 0;
2781 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2782 return WORK_FINISHED_CONTINUE;
2783 } else {
2784 s->s3->tmp.cert_req = 2;
2785 if (!ssl3_digest_cached_records(s, 0)) {
2786 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2787 ossl_statem_set_error(s);
2788 return 0;
2789 }
2790 }
2791 }
2792
2793 return WORK_FINISHED_CONTINUE;
2794 }
2795
2796 /* Shouldn't ever get here */
2797 return WORK_ERROR;
2798}
2799
2800int tls_construct_client_certificate(SSL *s)
2801{
2802 if (!ssl3_output_cert_chain(s,
2803 (s->s3->tmp.cert_req ==
2804 2) ? NULL : s->cert->key)) {
2805 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2806 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2807 ossl_statem_set_error(s);
2808 return 0;
2809 }
2810
2811 return 1;
2812}
2813
2814#define has_bits(i,m) (((i)&(m)) == (m))
2815
2816int ssl3_check_cert_and_algorithm(SSL *s)
2817{
2818 int i;
2819#ifndef OPENSSL_NO_EC
2820 int idx;
2821#endif
2822 long alg_k, alg_a;
2823 EVP_PKEY *pkey = NULL;
2824 int al = SSL_AD_HANDSHAKE_FAILURE;
2825
2826 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2827 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2828
2829 /* we don't have a certificate */
2830 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2831 return (1);
2832
2833 /* This is the passed certificate */
2834
2835#ifndef OPENSSL_NO_EC
2836 idx = s->session->peer_type;
2837 if (idx == SSL_PKEY_ECC) {
2838 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2839 /* check failed */
2840 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2841 goto f_err;
2842 } else {
2843 return 1;
2844 }
2845 } else if (alg_a & SSL_aECDSA) {
2846 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2847 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2848 goto f_err;
2849 }
2850#endif
2851 pkey = X509_get0_pubkey(s->session->peer);
2852 i = X509_certificate_type(s->session->peer, pkey);
2853
2854 /* Check that we have a certificate if we require one */
2855 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2856 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2857 SSL_R_MISSING_RSA_SIGNING_CERT);
2858 goto f_err;
2859 }
2860#ifndef OPENSSL_NO_DSA
2861 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2862 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2863 SSL_R_MISSING_DSA_SIGNING_CERT);
2864 goto f_err;
2865 }
2866#endif
2867#ifndef OPENSSL_NO_RSA
2868 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2869 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2870 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2871 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2872 goto f_err;
2873 }
2874#endif
2875#ifndef OPENSSL_NO_DH
2876 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
2877 al = SSL_AD_INTERNAL_ERROR;
2878 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2879 goto f_err;
2880 }
2881#endif
2882
2883 return (1);
2884 f_err:
2885 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2886 return (0);
2887}
2888
2889#ifndef OPENSSL_NO_NEXTPROTONEG
2890int tls_construct_next_proto(SSL *s)
2891{
2892 unsigned int len, padding_len;
2893 unsigned char *d;
2894
2895 len = s->next_proto_negotiated_len;
2896 padding_len = 32 - ((len + 2) % 32);
2897 d = (unsigned char *)s->init_buf->data;
2898 d[4] = len;
2899 memcpy(d + 5, s->next_proto_negotiated, len);
2900 d[5 + len] = padding_len;
2901 memset(d + 6 + len, 0, padding_len);
2902 *(d++) = SSL3_MT_NEXT_PROTO;
2903 l2n3(2 + len + padding_len, d);
2904 s->init_num = 4 + 2 + len + padding_len;
2905 s->init_off = 0;
2906
2907 return 1;
2908}
2909#endif
2910
2911int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2912{
2913 int i = 0;
2914#ifndef OPENSSL_NO_ENGINE
2915 if (s->ctx->client_cert_engine) {
2916 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2917 SSL_get_client_CA_list(s),
2918 px509, ppkey, NULL, NULL, NULL);
2919 if (i != 0)
2920 return i;
2921 }
2922#endif
2923 if (s->ctx->client_cert_cb)
2924 i = s->ctx->client_cert_cb(s, px509, ppkey);
2925 return i;
2926}
2927
2928int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, unsigned char *p)
2929{
2930 int i, j = 0;
2931 const SSL_CIPHER *c;
2932 unsigned char *q;
2933 int empty_reneg_info_scsv = !s->renegotiate;
2934 /* Set disabled masks for this session */
2935 ssl_set_client_disabled(s);
2936
2937 if (sk == NULL)
2938 return (0);
2939 q = p;
2940
2941 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2942 c = sk_SSL_CIPHER_value(sk, i);
2943 /* Skip disabled ciphers */
2944 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2945 continue;
2946 j = s->method->put_cipher_by_char(c, p);
2947 p += j;
2948 }
2949 /*
2950 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2951 * applicable SCSVs.
2952 */
2953 if (p != q) {
2954 if (empty_reneg_info_scsv) {
2955 static SSL_CIPHER scsv = {
2956 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2957 };
2958 j = s->method->put_cipher_by_char(&scsv, p);
2959 p += j;
2960 }
2961 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2962 static SSL_CIPHER scsv = {
2963 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2964 };
2965 j = s->method->put_cipher_by_char(&scsv, p);
2966 p += j;
2967 }
2968 }
2969
2970 return (p - q);
2971}
Note: See TracBrowser for help on using the repository browser.