2 * Copyright (C) 2020-2021 Pascal Knecht
3 * HSR Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include "tls_server.h"
23 #include <utils/debug.h>
24 #include <credentials/certificates/x509.h>
25 #include <collections/array.h>
27 typedef struct private_tls_server_t private_tls_server_t
;
30 * Size of a session ID
32 #define SESSION_ID_SIZE 16
39 STATE_KEY_EXCHANGE_SENT
,
43 STATE_KEY_EXCHANGE_RECEIVED
,
44 STATE_CERT_VERIFY_RECEIVED
,
45 STATE_CIPHERSPEC_CHANGED_IN
,
46 STATE_FINISHED_RECEIVED
,
47 STATE_CIPHERSPEC_CHANGED_OUT
,
49 /* new states in TLS 1.3 */
50 STATE_ENCRYPTED_EXTENSIONS_SENT
,
51 STATE_CERT_VERIFY_SENT
,
52 STATE_KEY_UPDATE_REQUESTED
,
53 STATE_KEY_UPDATE_SENT
,
54 STATE_FINISHED_SENT_KEY_SWITCHED
,
58 * Private data of an tls_server_t object.
60 struct private_tls_server_t
{
63 * Public tls_server_t interface.
85 identification_t
*server
;
88 * Peer identity, NULL for no client authentication
90 identification_t
*peer
;
98 * Hello random data selected by client
100 char client_random
[32];
103 * Hello random data selected by server
105 char server_random
[32];
108 * Auth helper for peer authentication
110 auth_cfg_t
*peer_auth
;
113 * Auth helper for server authentication
115 auth_cfg_t
*server_auth
;
120 private_key_t
*private;
125 diffie_hellman_t
*dh
;
130 tls_named_group_t requested_curve
;
133 * Selected TLS cipher suite
135 tls_cipher_suite_t suite
;
138 * Offered TLS version of the client
140 tls_version_t client_version
;
143 * TLS session identifier
148 * Do we resume a session?
153 * Hash and signature algorithms supported by peer
158 * Elliptic curves supported by peer
163 * Did we receive the curves from the client?
165 bool curves_received
;
168 * Whether to include CAs in CertificateRequest messages
170 bool send_certreq_authorities
;
174 * Find a trusted public key to encrypt/verify key exchange data
176 public_key_t
*tls_find_public_key(auth_cfg_t
*peer_auth
)
178 public_key_t
*public = NULL
, *current
;
179 certificate_t
*cert
, *found
;
180 enumerator_t
*enumerator
;
183 cert
= peer_auth
->get(peer_auth
, AUTH_HELPER_SUBJECT_CERT
);
186 enumerator
= lib
->credmgr
->create_public_enumerator(lib
->credmgr
,
187 KEY_ANY
, cert
->get_subject(cert
),
189 while (enumerator
->enumerate(enumerator
, ¤t
, &auth
))
191 found
= auth
->get(auth
, AUTH_RULE_SUBJECT_CERT
);
192 if (found
&& cert
->equals(cert
, found
))
194 public = current
->get_ref(current
);
195 peer_auth
->merge(peer_auth
, auth
, FALSE
);
199 enumerator
->destroy(enumerator
);
205 * Find a cipher suite and a server key
207 static bool select_suite_and_key(private_tls_server_t
*this,
208 tls_cipher_suite_t
*suites
, int count
)
210 tls_version_t version_min
, version_max
;
213 enumerator_t
*enumerator
;
215 version_min
= this->tls
->get_version_min(this->tls
);
216 version_max
= this->tls
->get_version_max(this->tls
);
217 enumerator
= tls_create_private_key_enumerator(version_min
, version_max
,
218 this->hashsig
, this->server
);
221 DBG1(DBG_TLS
, "no common signature algorithms found");
224 if (!enumerator
->enumerate(enumerator
, &key
, &auth
))
226 DBG1(DBG_TLS
, "no usable TLS server certificate found for '%Y'",
228 enumerator
->destroy(enumerator
);
232 if (version_max
>= TLS_1_3
)
234 this->suite
= this->crypto
->select_cipher_suite(this->crypto
, suites
,
239 this->suite
= this->crypto
->select_cipher_suite(this->crypto
, suites
,
240 count
, key
->get_type(key
));
241 while (!this->suite
&&
242 enumerator
->enumerate(enumerator
, &key
, &auth
))
243 { /* find a key and cipher suite for one of the remaining key types */
244 this->suite
= this->crypto
->select_cipher_suite(this->crypto
,
251 DBG1(DBG_TLS
, "received cipher suites or signature schemes unacceptable");
252 enumerator
->destroy(enumerator
);
255 DBG1(DBG_TLS
, "using key of type %N", key_type_names
, key
->get_type(key
));
256 this->private = key
->get_ref(key
);
257 this->server_auth
->merge(this->server_auth
, auth
, FALSE
);
258 enumerator
->destroy(enumerator
);
263 * Check if the peer supports a given TLS curve
265 static bool peer_supports_curve(private_tls_server_t
*this,
266 tls_named_group_t curve
)
268 bio_reader_t
*reader
;
271 if (!this->curves_received
)
272 { /* none received, assume yes */
275 reader
= bio_reader_create(this->curves
);
276 while (reader
->remaining(reader
) && reader
->read_uint16(reader
, ¤t
))
278 if (current
== curve
)
280 reader
->destroy(reader
);
284 reader
->destroy(reader
);
289 * TLS 1.3 key exchange key share
297 * Check if peer sent a key share of a given TLS named DH group
299 static bool peer_offered_curve(array_t
*key_shares
, tls_named_group_t curve
,
305 for (i
= 0; i
< array_count(key_shares
); i
++)
307 array_get(key_shares
, i
, &peer
);
308 if (curve
== peer
.curve
)
321 * Check if client is currently retrying to connect to the server.
323 static bool retrying(private_tls_server_t
*this)
325 return this->state
== STATE_INIT
&& this->requested_curve
;
329 * Process client hello message
331 static status_t
process_client_hello(private_tls_server_t
*this,
332 bio_reader_t
*reader
)
334 uint16_t legacy_version
= 0, version
= 0, extension_type
= 0;
335 chunk_t random
, session
, ciphers
, versions
= chunk_empty
, compression
;
336 chunk_t ext
= chunk_empty
, key_shares
= chunk_empty
;
337 key_share_t peer
= {0};
338 chunk_t extension_data
= chunk_empty
;
339 bio_reader_t
*extensions
, *extension
;
340 tls_cipher_suite_t
*suites
;
341 tls_version_t original_version_max
;
345 this->crypto
->append_handshake(this->crypto
,
346 TLS_CLIENT_HELLO
, reader
->peek(reader
));
348 if (!reader
->read_uint16(reader
, &legacy_version
) ||
349 !reader
->read_data(reader
, sizeof(this->client_random
), &random
) ||
350 !reader
->read_data8(reader
, &session
) ||
351 !reader
->read_data16(reader
, &ciphers
) ||
352 !reader
->read_data8(reader
, &compression
) ||
353 (reader
->remaining(reader
) && !reader
->read_data16(reader
, &ext
)))
355 DBG1(DBG_TLS
, "received invalid ClientHello");
356 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
360 /* before we do anything version-related, determine our supported suites
361 * as that might change the min./max. versions */
362 this->crypto
->get_cipher_suites(this->crypto
, NULL
);
364 extensions
= bio_reader_create(ext
);
365 while (extensions
->remaining(extensions
))
367 if (!extensions
->read_uint16(extensions
, &extension_type
) ||
368 !extensions
->read_data16(extensions
, &extension_data
))
370 DBG1(DBG_TLS
, "received invalid ClientHello Extensions");
371 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
372 extensions
->destroy(extensions
);
375 extension
= bio_reader_create(extension_data
);
376 DBG2(DBG_TLS
, "received TLS '%N' extension",
377 tls_extension_names
, extension_type
);
378 DBG3(DBG_TLS
, "%B", &extension_data
);
379 switch (extension_type
)
381 case TLS_EXT_SIGNATURE_ALGORITHMS
:
382 if (!extension
->read_data16(extension
, &extension_data
))
384 DBG1(DBG_TLS
, "invalid %N extension",
385 tls_extension_names
, extension_type
);
386 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
387 extensions
->destroy(extensions
);
388 extension
->destroy(extension
);
391 chunk_free(&this->hashsig
);
392 this->hashsig
= chunk_clone(extension_data
);
394 case TLS_EXT_SUPPORTED_GROUPS
:
395 if (!extension
->read_data16(extension
, &extension_data
))
397 DBG1(DBG_TLS
, "invalid %N extension",
398 tls_extension_names
, extension_type
);
399 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
400 extensions
->destroy(extensions
);
401 extension
->destroy(extension
);
404 chunk_free(&this->curves
);
405 this->curves_received
= TRUE
;
406 this->curves
= chunk_clone(extension_data
);
408 case TLS_EXT_SUPPORTED_VERSIONS
:
409 if (!extension
->read_data8(extension
, &versions
))
411 DBG1(DBG_TLS
, "invalid %N extension",
412 tls_extension_names
, extension_type
);
413 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
414 extensions
->destroy(extensions
);
415 extension
->destroy(extension
);
419 case TLS_EXT_KEY_SHARE
:
420 if (!extension
->read_data16(extension
, &key_shares
))
422 DBG1(DBG_TLS
, "invalid %N extension",
423 tls_extension_names
, extension_type
);
424 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
425 extensions
->destroy(extensions
);
426 extension
->destroy(extension
);
433 extension
->destroy(extension
);
435 extensions
->destroy(extensions
);
437 if (this->tls
->get_version_max(this->tls
) >= TLS_1_3
&& !this->hashsig
.len
)
439 DBG1(DBG_TLS
, "no %N extension received", tls_extension_names
,
440 TLS_MISSING_EXTENSION
);
441 this->alert
->add(this->alert
, TLS_FATAL
, TLS_MISSING_EXTENSION
);
445 memcpy(this->client_random
, random
.ptr
, sizeof(this->client_random
));
447 htoun32(&this->server_random
, time(NULL
));
448 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
450 !rng
->get_bytes(rng
, sizeof(this->server_random
) - 4,
451 this->server_random
+ 4))
453 DBG1(DBG_TLS
, "failed to generate server random");
454 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
460 original_version_max
= this->tls
->get_version_max(this->tls
);
464 bio_reader_t
*client_versions
;
466 client_versions
= bio_reader_create(versions
);
467 while (client_versions
->remaining(client_versions
))
469 if (client_versions
->read_uint16(client_versions
, &version
))
471 if (this->tls
->set_version(this->tls
, version
, version
))
473 this->client_version
= version
;
478 client_versions
->destroy(client_versions
);
482 version
= legacy_version
;
483 if (this->tls
->set_version(this->tls
, version
, version
))
485 this->client_version
= version
;
489 /* downgrade protection (see RFC 8446, section 4.1.3) */
490 if ((original_version_max
== TLS_1_3
&& version
< TLS_1_3
) ||
491 (original_version_max
== TLS_1_2
&& version
< TLS_1_2
))
493 chunk_t downgrade_protection
= tls_downgrade_protection_tls11
;
495 if (version
== TLS_1_2
)
497 downgrade_protection
= tls_downgrade_protection_tls12
;
499 memcpy(&this->server_random
[24], downgrade_protection
.ptr
,
500 downgrade_protection
.len
);
503 if (!this->client_version
)
505 DBG1(DBG_TLS
, "proposed version %N not supported", tls_version_names
,
507 this->alert
->add(this->alert
, TLS_FATAL
, TLS_PROTOCOL_VERSION
);
511 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
513 this->suite
= this->crypto
->resume_session(this->crypto
, session
,
515 chunk_from_thing(this->client_random
),
516 chunk_from_thing(this->server_random
));
521 this->session
= chunk_clone(session
);
523 DBG1(DBG_TLS
, "resumed %N using suite %N",
524 tls_version_names
, this->tls
->get_version_max(this->tls
),
525 tls_cipher_suite_names
, this->suite
);
529 count
= ciphers
.len
/ sizeof(uint16_t);
530 suites
= alloca(count
* sizeof(tls_cipher_suite_t
));
531 DBG2(DBG_TLS
, "received %d TLS cipher suites:", count
);
532 for (i
= 0; i
< count
; i
++)
534 suites
[i
] = untoh16(&ciphers
.ptr
[i
* sizeof(uint16_t)]);
535 DBG2(DBG_TLS
, " %N", tls_cipher_suite_names
, suites
[i
]);
537 if (!select_suite_and_key(this, suites
, count
))
539 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
542 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
544 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
546 !rng
->allocate_bytes(rng
, SESSION_ID_SIZE
, &this->session
))
548 DBG1(DBG_TLS
, "generating TLS session identifier failed, skipped");
554 this->session
= chunk_clone(session
);
556 DBG1(DBG_TLS
, "negotiated %N using suite %N",
557 tls_version_names
, this->tls
->get_version_max(this->tls
),
558 tls_cipher_suite_names
, this->suite
);
561 if (this->tls
->get_version_max(this->tls
) >= TLS_1_3
)
563 diffie_hellman_group_t group
;
564 tls_named_group_t curve
, requesting_curve
= 0;
565 enumerator_t
*enumerator
;
566 array_t
*peer_key_shares
;
568 peer_key_shares
= array_create(sizeof(key_share_t
), 1);
569 extension
= bio_reader_create(key_shares
);
570 while (extension
->remaining(extension
))
572 if (!extension
->read_uint16(extension
, &peer
.curve
) ||
573 !extension
->read_data16(extension
, &peer
.key_share
) ||
576 DBG1(DBG_TLS
, "invalid %N extension",
577 tls_extension_names
, extension_type
);
578 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
579 extension
->destroy(extension
);
580 array_destroy(peer_key_shares
);
583 array_insert(peer_key_shares
, ARRAY_TAIL
, &peer
);
585 extension
->destroy(extension
);
587 enumerator
= this->crypto
->create_ec_enumerator(this->crypto
);
588 while (enumerator
->enumerate(enumerator
, &group
, &curve
))
590 if (!requesting_curve
&&
591 peer_supports_curve(this, curve
) &&
592 !peer_offered_curve(peer_key_shares
, curve
, NULL
))
594 requesting_curve
= curve
;
596 if (peer_supports_curve(this, curve
) &&
597 peer_offered_curve(peer_key_shares
, curve
, &peer
))
599 DBG1(DBG_TLS
, "using key exchange %N",
600 tls_named_group_names
, curve
);
601 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, group
);
605 enumerator
->destroy(enumerator
);
606 array_destroy(peer_key_shares
);
612 DBG1(DBG_TLS
, "already replied with a hello retry request");
613 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
617 if (!requesting_curve
)
619 DBG1(DBG_TLS
, "no mutual supported group in client hello");
620 this->alert
->add(this->alert
, TLS_FATAL
, TLS_ILLEGAL_PARAMETER
);
623 this->requested_curve
= requesting_curve
;
625 if (!this->crypto
->hash_handshake(this->crypto
, NULL
))
627 DBG1(DBG_TLS
, "failed to hash handshake messages");
628 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
634 if (peer
.key_share
.len
&&
635 peer
.curve
!= TLS_CURVE25519
&&
636 peer
.curve
!= TLS_CURVE448
)
637 { /* classic format (see RFC 8446, section 4.2.8.2) */
638 if (peer
.key_share
.ptr
[0] != TLS_ANSI_UNCOMPRESSED
)
640 DBG1(DBG_TLS
, "DH point format '%N' not supported",
641 tls_ansi_point_format_names
, peer
.key_share
.ptr
[0]);
642 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
645 peer
.key_share
= chunk_skip(peer
.key_share
, 1);
647 if (!peer
.key_share
.len
||
648 !this->dh
->set_other_public_value(this->dh
, peer
.key_share
))
650 DBG1(DBG_TLS
, "DH key derivation failed");
651 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
654 this->requested_curve
= 0;
658 this->state
= STATE_HELLO_RECEIVED
;
663 * Process certificate
665 static status_t
process_certificate(private_tls_server_t
*this,
666 bio_reader_t
*reader
)
673 this->crypto
->append_handshake(this->crypto
,
674 TLS_CERTIFICATE
, reader
->peek(reader
));
676 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
678 if (!reader
->read_data8(reader
, &data
))
680 DBG1(DBG_TLS
, "certificate request context invalid");
681 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
686 if (!reader
->read_data24(reader
, &data
))
688 DBG1(DBG_TLS
, "certificate message header invalid");
689 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
692 certs
= bio_reader_create(data
);
693 if (!certs
->remaining(certs
))
695 DBG1(DBG_TLS
, "no certificate sent by peer");
696 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
699 while (certs
->remaining(certs
))
701 if (!certs
->read_data24(certs
, &data
))
703 DBG1(DBG_TLS
, "certificate message invalid");
704 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
705 certs
->destroy(certs
);
708 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
709 BUILD_BLOB_ASN1_DER
, data
, BUILD_END
);
714 this->peer_auth
->add(this->peer_auth
,
715 AUTH_HELPER_SUBJECT_CERT
, cert
);
716 DBG1(DBG_TLS
, "received TLS peer certificate '%Y'",
717 cert
->get_subject(cert
));
722 DBG1(DBG_TLS
, "received TLS intermediate certificate '%Y'",
723 cert
->get_subject(cert
));
724 this->peer_auth
->add(this->peer_auth
, AUTH_HELPER_IM_CERT
, cert
);
729 DBG1(DBG_TLS
, "parsing TLS certificate failed, skipped");
730 this->alert
->add(this->alert
, TLS_WARNING
, TLS_BAD_CERTIFICATE
);
732 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
734 if (!certs
->read_data16(certs
, &data
))
736 DBG1(DBG_TLS
, "failed to read extensions of CertificateEntry");
737 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
742 certs
->destroy(certs
);
743 this->state
= STATE_CERT_RECEIVED
;
748 * Process Client Key Exchange, using premaster encryption
750 static status_t
process_key_exchange_encrypted(private_tls_server_t
*this,
751 bio_reader_t
*reader
)
753 chunk_t encrypted
, decrypted
;
757 this->crypto
->append_handshake(this->crypto
,
758 TLS_CLIENT_KEY_EXCHANGE
, reader
->peek(reader
));
760 if (!reader
->read_data16(reader
, &encrypted
))
762 DBG1(DBG_TLS
, "received invalid Client Key Exchange");
763 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
767 htoun16(premaster
, this->client_version
);
768 /* pre-randomize premaster for failure cases */
769 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
770 if (!rng
|| !rng
->get_bytes(rng
, sizeof(premaster
) - 2, premaster
+ 2))
772 DBG1(DBG_TLS
, "failed to generate premaster secret");
773 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
780 this->private->decrypt(this->private,
781 ENCRYPT_RSA_PKCS1
, encrypted
, &decrypted
))
783 if (decrypted
.len
== sizeof(premaster
) &&
784 untoh16(decrypted
.ptr
) == this->client_version
)
786 memcpy(premaster
+ 2, decrypted
.ptr
+ 2, sizeof(premaster
) - 2);
790 DBG1(DBG_TLS
, "decrypted premaster has invalid length/version");
792 chunk_clear(&decrypted
);
796 DBG1(DBG_TLS
, "decrypting Client Key Exchange failed");
799 if (!this->crypto
->derive_secrets(this->crypto
, chunk_from_thing(premaster
),
800 this->session
, this->peer
,
801 chunk_from_thing(this->client_random
),
802 chunk_from_thing(this->server_random
)))
804 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
808 this->state
= STATE_KEY_EXCHANGE_RECEIVED
;
813 * Process client key exchange, using DHE exchange
815 static status_t
process_key_exchange_dhe(private_tls_server_t
*this,
816 bio_reader_t
*reader
)
818 chunk_t premaster
, pub
;
821 this->crypto
->append_handshake(this->crypto
,
822 TLS_CLIENT_KEY_EXCHANGE
, reader
->peek(reader
));
824 ec
= diffie_hellman_group_is_ec(this->dh
->get_dh_group(this->dh
));
825 if ((ec
&& !reader
->read_data8(reader
, &pub
)) ||
826 (!ec
&& (!reader
->read_data16(reader
, &pub
) || pub
.len
== 0)))
828 DBG1(DBG_TLS
, "received invalid Client Key Exchange");
829 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
835 if (pub
.ptr
[0] != TLS_ANSI_UNCOMPRESSED
)
837 DBG1(DBG_TLS
, "DH point format '%N' not supported",
838 tls_ansi_point_format_names
, pub
.ptr
[0]);
839 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
842 pub
= chunk_skip(pub
, 1);
844 if (!this->dh
->set_other_public_value(this->dh
, pub
))
846 DBG1(DBG_TLS
, "applying DH public value failed");
847 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
850 if (!this->dh
->get_shared_secret(this->dh
, &premaster
))
852 DBG1(DBG_TLS
, "calculating premaster from DH failed");
853 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
857 if (!this->crypto
->derive_secrets(this->crypto
, premaster
,
858 this->session
, this->peer
,
859 chunk_from_thing(this->client_random
),
860 chunk_from_thing(this->server_random
)))
862 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
863 chunk_clear(&premaster
);
866 chunk_clear(&premaster
);
868 this->state
= STATE_KEY_EXCHANGE_RECEIVED
;
873 * Process Client Key Exchange
875 static status_t
process_key_exchange(private_tls_server_t
*this,
876 bio_reader_t
*reader
)
880 return process_key_exchange_dhe(this, reader
);
882 return process_key_exchange_encrypted(this, reader
);
886 * Process Certificate verify
888 static status_t
process_cert_verify(private_tls_server_t
*this,
889 bio_reader_t
*reader
)
891 public_key_t
*public;
894 public = tls_find_public_key(this->peer_auth
);
897 DBG1(DBG_TLS
, "no trusted certificate found for '%Y' to verify TLS peer",
899 this->alert
->add(this->alert
, TLS_FATAL
, TLS_CERTIFICATE_UNKNOWN
);
903 msg
= reader
->peek(reader
);
904 if (!this->crypto
->verify_handshake(this->crypto
, public, reader
))
906 public->destroy(public);
907 DBG1(DBG_TLS
, "signature verification failed");
908 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECRYPT_ERROR
);
911 public->destroy(public);
912 this->state
= STATE_CERT_VERIFY_RECEIVED
;
913 this->crypto
->append_handshake(this->crypto
, TLS_CERTIFICATE_VERIFY
, msg
);
918 * Process finished message
920 static status_t
process_finished(private_tls_server_t
*this,
921 bio_reader_t
*reader
)
923 chunk_t received
, verify_data
;
926 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
928 if (!reader
->read_data(reader
, sizeof(buf
), &received
))
930 DBG1(DBG_TLS
, "received client finished too short");
931 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
934 if (!this->crypto
->calculate_finished_legacy(this->crypto
,
935 "client finished", buf
))
937 DBG1(DBG_TLS
, "calculating client finished failed");
938 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
941 verify_data
= chunk_from_thing(buf
);
945 received
= reader
->peek(reader
);
946 if (!this->crypto
->calculate_finished(this->crypto
, FALSE
, &verify_data
))
948 DBG1(DBG_TLS
, "calculating client finished failed");
949 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
952 this->crypto
->change_cipher(this->crypto
, TRUE
);
955 if (!chunk_equals_const(received
, verify_data
))
957 DBG1(DBG_TLS
, "received client finished invalid");
958 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECRYPT_ERROR
);
962 if (verify_data
.ptr
!= buf
)
964 chunk_free(&verify_data
);
967 this->crypto
->append_handshake(this->crypto
, TLS_FINISHED
, received
);
968 this->state
= STATE_FINISHED_RECEIVED
;
973 * Process KeyUpdate message
975 static status_t
process_key_update(private_tls_server_t
*this,
976 bio_reader_t
*reader
)
978 uint8_t update_requested
;
980 if (!reader
->read_uint8(reader
, &update_requested
) ||
981 update_requested
> 1)
983 DBG1(DBG_TLS
, "received invalid KeyUpdate");
984 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
988 if (!this->crypto
->update_app_keys(this->crypto
, TRUE
))
990 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
993 this->crypto
->change_cipher(this->crypto
, TRUE
);
995 if (update_requested
)
997 DBG1(DBG_TLS
, "client requested KeyUpdate");
998 this->state
= STATE_KEY_UPDATE_REQUESTED
;
1003 METHOD(tls_handshake_t
, process
, status_t
,
1004 private_tls_server_t
*this, tls_handshake_type_t type
, bio_reader_t
*reader
)
1006 tls_handshake_type_t expected
;
1008 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1010 switch (this->state
)
1013 if (type
== TLS_CLIENT_HELLO
)
1015 return process_client_hello(this, reader
);
1017 expected
= TLS_CLIENT_HELLO
;
1019 case STATE_HELLO_DONE
:
1020 if (type
== TLS_CERTIFICATE
)
1022 return process_certificate(this, reader
);
1026 expected
= TLS_CERTIFICATE
;
1029 /* otherwise fall through to next state */
1030 case STATE_CERT_RECEIVED
:
1031 if (type
== TLS_CLIENT_KEY_EXCHANGE
)
1033 return process_key_exchange(this, reader
);
1035 expected
= TLS_CLIENT_KEY_EXCHANGE
;
1037 case STATE_KEY_EXCHANGE_RECEIVED
:
1038 if (type
== TLS_CERTIFICATE_VERIFY
)
1040 return process_cert_verify(this, reader
);
1044 expected
= TLS_CERTIFICATE_VERIFY
;
1047 return INVALID_STATE
;
1048 case STATE_CIPHERSPEC_CHANGED_IN
:
1049 if (type
== TLS_FINISHED
)
1051 return process_finished(this, reader
);
1053 expected
= TLS_FINISHED
;
1056 DBG1(DBG_TLS
, "TLS %N not expected in current state",
1057 tls_handshake_type_names
, type
);
1058 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1064 switch (this->state
)
1067 if (type
== TLS_CLIENT_HELLO
)
1069 return process_client_hello(this, reader
);
1071 expected
= TLS_CLIENT_HELLO
;
1073 case STATE_CIPHERSPEC_CHANGED_IN
:
1074 case STATE_FINISHED_SENT
:
1075 case STATE_FINISHED_SENT_KEY_SWITCHED
:
1076 if (type
== TLS_CERTIFICATE
)
1078 return process_certificate(this, reader
);
1082 expected
= TLS_CERTIFICATE
;
1085 /* otherwise fall through to next state */
1086 case STATE_CERT_RECEIVED
:
1087 if (type
== TLS_CERTIFICATE_VERIFY
)
1089 return process_cert_verify(this, reader
);
1093 expected
= TLS_CERTIFICATE_VERIFY
;
1096 /* otherwise fall through to next state */
1097 case STATE_CERT_VERIFY_RECEIVED
:
1098 if (type
== TLS_FINISHED
)
1100 return process_finished(this, reader
);
1103 case STATE_FINISHED_RECEIVED
:
1104 if (type
== TLS_KEY_UPDATE
)
1106 return process_key_update(this, reader
);
1108 return INVALID_STATE
;
1110 DBG1(DBG_TLS
, "TLS %N not expected in current state",
1111 tls_handshake_type_names
, type
);
1112 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1116 DBG1(DBG_TLS
, "TLS %N expected, but received %N",
1117 tls_handshake_type_names
, expected
, tls_handshake_type_names
, type
);
1118 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1123 * Write public key into key share extension
1125 bool tls_write_key_share(bio_writer_t
**key_share
, diffie_hellman_t
*dh
)
1127 bio_writer_t
*writer
;
1128 tls_named_group_t curve
;
1135 curve
= tls_ec_group_to_curve(dh
->get_dh_group(dh
));
1136 if (!curve
|| !dh
->get_my_public_value(dh
, &pub
))
1140 *key_share
= writer
= bio_writer_create(pub
.len
+ 7);
1141 writer
->write_uint16(writer
, curve
);
1142 if (curve
== TLS_CURVE25519
||
1143 curve
== TLS_CURVE448
)
1145 writer
->write_data16(writer
, pub
);
1148 { /* classic format (see RFC 8446, section 4.2.8.2) */
1149 writer
->write_uint16(writer
, pub
.len
+ 1);
1150 writer
->write_uint8(writer
, TLS_ANSI_UNCOMPRESSED
);
1151 writer
->write_data(writer
, pub
);
1158 * Send ServerHello message
1160 static status_t
send_server_hello(private_tls_server_t
*this,
1161 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1163 bio_writer_t
*key_share
, *extensions
;
1164 tls_version_t version
;
1166 version
= this->tls
->get_version_max(this->tls
);
1168 /* cap legacy version at TLS 1.2 for middlebox compatibility */
1169 writer
->write_uint16(writer
, min(TLS_1_2
, version
));
1171 if (this->requested_curve
)
1173 writer
->write_data(writer
, tls_hello_retry_request_magic
);
1177 writer
->write_data(writer
, chunk_from_thing(this->server_random
));
1180 /* session identifier if we have one */
1181 writer
->write_data8(writer
, this->session
);
1183 /* add selected TLS cipher suite */
1184 writer
->write_uint16(writer
, this->suite
);
1186 /* NULL compression only */
1187 writer
->write_uint8(writer
, 0);
1189 if (version
>= TLS_1_3
)
1191 extensions
= bio_writer_create(32);
1193 DBG2(DBG_TLS
, "sending extension: %N",
1194 tls_extension_names
, TLS_EXT_SUPPORTED_VERSIONS
);
1195 extensions
->write_uint16(extensions
, TLS_EXT_SUPPORTED_VERSIONS
);
1196 extensions
->write_uint16(extensions
, 2);
1197 extensions
->write_uint16(extensions
, version
);
1199 DBG2(DBG_TLS
, "sending extension: %N",
1200 tls_extension_names
, TLS_EXT_KEY_SHARE
);
1201 extensions
->write_uint16(extensions
, TLS_EXT_KEY_SHARE
);
1202 if (this->requested_curve
)
1204 DBG1(DBG_TLS
, "requesting key exchange with %N",
1205 tls_named_group_names
, this->requested_curve
);
1206 extensions
->write_uint16(extensions
, 2);
1207 extensions
->write_uint16(extensions
, this->requested_curve
);
1211 if (!tls_write_key_share(&key_share
, this->dh
))
1213 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1214 extensions
->destroy(extensions
);
1217 extensions
->write_data16(extensions
, key_share
->get_buf(key_share
));
1218 key_share
->destroy(key_share
);
1221 writer
->write_data16(writer
, extensions
->get_buf(extensions
));
1222 extensions
->destroy(extensions
);
1225 *type
= TLS_SERVER_HELLO
;
1226 this->state
= this->requested_curve ? STATE_INIT
: STATE_HELLO_SENT
;
1227 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1232 * Send encrypted extensions message
1234 static status_t
send_encrypted_extensions(private_tls_server_t
*this,
1235 tls_handshake_type_t
*type
,
1236 bio_writer_t
*writer
)
1238 chunk_t shared_secret
= chunk_empty
;
1240 if (!this->dh
->get_shared_secret(this->dh
, &shared_secret
) ||
1241 !this->crypto
->derive_handshake_keys(this->crypto
, shared_secret
))
1243 DBG1(DBG_TLS
, "DH key derivation failed");
1244 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
1245 chunk_clear(&shared_secret
);
1248 chunk_clear(&shared_secret
);
1250 this->crypto
->change_cipher(this->crypto
, TRUE
);
1251 this->crypto
->change_cipher(this->crypto
, FALSE
);
1253 /* currently no extensions are supported */
1254 writer
->write_uint16(writer
, 0);
1256 *type
= TLS_ENCRYPTED_EXTENSIONS
;
1257 this->state
= STATE_ENCRYPTED_EXTENSIONS_SENT
;
1258 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1265 static status_t
send_certificate(private_tls_server_t
*this,
1266 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1268 enumerator_t
*enumerator
;
1269 certificate_t
*cert
;
1271 bio_writer_t
*certs
;
1274 /* certificate request context as described in RFC 8446, section 4.4.2 */
1275 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
1277 writer
->write_uint8(writer
, 0);
1280 /* generate certificate payload */
1281 certs
= bio_writer_create(256);
1282 cert
= this->server_auth
->get(this->server_auth
, AUTH_RULE_SUBJECT_CERT
);
1285 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &data
))
1287 DBG1(DBG_TLS
, "sending TLS server certificate '%Y'",
1288 cert
->get_subject(cert
));
1289 certs
->write_data24(certs
, data
);
1292 /* extensions see RFC 8446, section 4.4.2 */
1293 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
1295 certs
->write_uint16(certs
, 0);
1298 enumerator
= this->server_auth
->create_enumerator(this->server_auth
);
1299 while (enumerator
->enumerate(enumerator
, &rule
, &cert
))
1301 if (rule
== AUTH_RULE_IM_CERT
)
1303 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &data
))
1305 DBG1(DBG_TLS
, "sending TLS intermediate certificate '%Y'",
1306 cert
->get_subject(cert
));
1307 certs
->write_data24(certs
, data
);
1312 enumerator
->destroy(enumerator
);
1314 writer
->write_data24(writer
, certs
->get_buf(certs
));
1315 certs
->destroy(certs
);
1317 *type
= TLS_CERTIFICATE
;
1318 this->state
= STATE_CERT_SENT
;
1319 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1324 * Send Certificate Verify
1326 static status_t
send_certificate_verify(private_tls_server_t
*this,
1327 tls_handshake_type_t
*type
,
1328 bio_writer_t
*writer
)
1330 if (!this->crypto
->sign_handshake(this->crypto
, this->private, writer
,
1333 DBG1(DBG_TLS
, "signature generation failed");
1334 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1338 *type
= TLS_CERTIFICATE_VERIFY
;
1339 this->state
= STATE_CERT_VERIFY_SENT
;
1340 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1345 * Write all available certificate authorities to output writer
1347 static void write_certificate_authorities(bio_writer_t
*writer
)
1349 bio_writer_t
*authorities
;
1350 enumerator_t
*enumerator
;
1351 certificate_t
*cert
;
1353 identification_t
*id
;
1355 authorities
= bio_writer_create(64);
1356 enumerator
= lib
->credmgr
->create_cert_enumerator(lib
->credmgr
, CERT_X509
,
1357 KEY_RSA
, NULL
, TRUE
);
1358 while (enumerator
->enumerate(enumerator
, &cert
))
1360 x509
= (x509_t
*)cert
;
1361 if (x509
->get_flags(x509
) & X509_CA
)
1363 id
= cert
->get_subject(cert
);
1364 DBG1(DBG_TLS
, "sending TLS cert request for '%Y'", id
);
1365 authorities
->write_data16(authorities
, id
->get_encoding(id
));
1368 enumerator
->destroy(enumerator
);
1369 writer
->write_data16(writer
, authorities
->get_buf(authorities
));
1370 authorities
->destroy(authorities
);
1374 * Send Certificate Request
1376 static status_t
send_certificate_request(private_tls_server_t
*this,
1377 tls_handshake_type_t
*type
,
1378 bio_writer_t
*writer
)
1380 bio_writer_t
*authorities
, *supported
, *extensions
;
1382 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1384 supported
= bio_writer_create(4);
1385 /* we propose both RSA and ECDSA */
1386 supported
->write_uint8(supported
, TLS_RSA_SIGN
);
1387 supported
->write_uint8(supported
, TLS_ECDSA_SIGN
);
1388 writer
->write_data8(writer
, supported
->get_buf(supported
));
1389 supported
->destroy(supported
);
1390 if (this->tls
->get_version_max(this->tls
) >= TLS_1_2
)
1392 this->crypto
->get_signature_algorithms(this->crypto
, writer
, TRUE
);
1395 if (this->send_certreq_authorities
)
1397 write_certificate_authorities(writer
);
1401 writer
->write_data16(writer
, chunk_empty
);
1406 /* certificate request context as described in RFC 8446, section 4.3.2 */
1407 writer
->write_uint8(writer
, 0);
1409 extensions
= bio_writer_create(32);
1411 if (this->send_certreq_authorities
)
1413 DBG2(DBG_TLS
, "sending extension: %N",
1414 tls_extension_names
, TLS_EXT_CERTIFICATE_AUTHORITIES
);
1415 authorities
= bio_writer_create(64);
1416 write_certificate_authorities(authorities
);
1417 extensions
->write_uint16(extensions
, TLS_EXT_CERTIFICATE_AUTHORITIES
);
1418 extensions
->write_data16(extensions
, authorities
->get_buf(authorities
));
1419 authorities
->destroy(authorities
);
1422 DBG2(DBG_TLS
, "sending extension: %N",
1423 tls_extension_names
, TLS_EXT_SIGNATURE_ALGORITHMS
);
1424 extensions
->write_uint16(extensions
, TLS_EXT_SIGNATURE_ALGORITHMS
);
1425 supported
= bio_writer_create(32);
1426 this->crypto
->get_signature_algorithms(this->crypto
, supported
, TRUE
);
1427 extensions
->write_data16(extensions
, supported
->get_buf(supported
));
1428 supported
->destroy(supported
);
1429 writer
->write_data16(writer
, extensions
->get_buf(extensions
));
1430 extensions
->destroy(extensions
);
1433 *type
= TLS_CERTIFICATE_REQUEST
;
1434 this->state
= STATE_CERTREQ_SENT
;
1435 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1440 * Try to find a curve supported by both, client and server
1442 static bool find_supported_curve(private_tls_server_t
*this,
1443 tls_named_group_t
*curve
)
1445 tls_named_group_t current
;
1446 enumerator_t
*enumerator
;
1448 enumerator
= this->crypto
->create_ec_enumerator(this->crypto
);
1449 while (enumerator
->enumerate(enumerator
, NULL
, ¤t
))
1451 if (peer_supports_curve(this, current
))
1454 enumerator
->destroy(enumerator
);
1458 enumerator
->destroy(enumerator
);
1463 * Send Server key Exchange
1465 static status_t
send_server_key_exchange(private_tls_server_t
*this,
1466 tls_handshake_type_t
*type
, bio_writer_t
*writer
,
1467 diffie_hellman_group_t group
)
1469 diffie_hellman_params_t
*params
= NULL
;
1470 tls_named_group_t curve
;
1473 if (diffie_hellman_group_is_ec(group
))
1475 curve
= tls_ec_group_to_curve(group
);
1476 if (!curve
|| (!peer_supports_curve(this, curve
) &&
1477 !find_supported_curve(this, &curve
)))
1479 DBG1(DBG_TLS
, "no EC group supported by client and server");
1480 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
1483 DBG2(DBG_TLS
, "selected ECDH group %N", tls_named_group_names
, curve
);
1484 writer
->write_uint8(writer
, TLS_ECC_NAMED_CURVE
);
1485 writer
->write_uint16(writer
, curve
);
1489 params
= diffie_hellman_get_params(group
);
1492 DBG1(DBG_TLS
, "no parameters found for DH group %N",
1493 diffie_hellman_group_names
, group
);
1494 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1497 DBG2(DBG_TLS
, "selected DH group %N", diffie_hellman_group_names
, group
);
1498 writer
->write_data16(writer
, params
->prime
);
1499 writer
->write_data16(writer
, params
->generator
);
1501 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, group
);
1504 DBG1(DBG_TLS
, "DH group %N not supported",
1505 diffie_hellman_group_names
, group
);
1506 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1509 if (!this->dh
->get_my_public_value(this->dh
, &chunk
))
1511 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1516 writer
->write_data16(writer
, chunk
);
1519 { /* ECP uses 8bit length header only, but a point format */
1520 writer
->write_uint8(writer
, chunk
.len
+ 1);
1521 writer
->write_uint8(writer
, TLS_ANSI_UNCOMPRESSED
);
1522 writer
->write_data(writer
, chunk
);
1526 chunk
= chunk_cat("ccc", chunk_from_thing(this->client_random
),
1527 chunk_from_thing(this->server_random
), writer
->get_buf(writer
));
1528 if (!this->private || !this->crypto
->sign(this->crypto
, this->private,
1529 writer
, chunk
, this->hashsig
))
1531 DBG1(DBG_TLS
, "signing DH parameters failed");
1532 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1537 *type
= TLS_SERVER_KEY_EXCHANGE
;
1538 this->state
= STATE_KEY_EXCHANGE_SENT
;
1539 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1546 static status_t
send_hello_done(private_tls_server_t
*this,
1547 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1549 *type
= TLS_SERVER_HELLO_DONE
;
1550 this->state
= STATE_HELLO_DONE
;
1551 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1558 static status_t
send_finished(private_tls_server_t
*this,
1559 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1561 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1565 if (!this->crypto
->calculate_finished_legacy(this->crypto
,
1566 "server finished", buf
))
1568 DBG1(DBG_TLS
, "calculating server finished data failed");
1569 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1573 writer
->write_data(writer
, chunk_from_thing(buf
));
1577 chunk_t verify_data
;
1579 if (!this->crypto
->calculate_finished(this->crypto
, TRUE
, &verify_data
))
1581 DBG1(DBG_TLS
, "calculating server finished data failed");
1582 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1586 writer
->write_data(writer
, verify_data
);
1587 chunk_free(&verify_data
);
1590 *type
= TLS_FINISHED
;
1591 this->state
= STATE_FINISHED_SENT
;
1592 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1598 * Send KeyUpdate message
1600 static status_t
send_key_update(private_tls_server_t
*this,
1601 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1603 *type
= TLS_KEY_UPDATE
;
1605 /* we currently only send this as reply, so we never request an update */
1606 writer
->write_uint8(writer
, 0);
1608 this->state
= STATE_KEY_UPDATE_SENT
;
1612 METHOD(tls_handshake_t
, build
, status_t
,
1613 private_tls_server_t
*this, tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1615 diffie_hellman_group_t group
;
1617 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1619 switch (this->state
)
1621 case STATE_HELLO_RECEIVED
:
1622 return send_server_hello(this, type
, writer
);
1623 case STATE_HELLO_SENT
:
1624 return send_certificate(this, type
, writer
);
1625 case STATE_CERT_SENT
:
1626 group
= this->crypto
->get_dh_group(this->crypto
);
1629 return send_server_key_exchange(this, type
, writer
, group
);
1631 /* otherwise fall through to next state */
1632 case STATE_KEY_EXCHANGE_SENT
:
1635 return send_certificate_request(this, type
, writer
);
1637 /* otherwise fall through to next state */
1638 case STATE_CERTREQ_SENT
:
1639 return send_hello_done(this, type
, writer
);
1640 case STATE_CIPHERSPEC_CHANGED_OUT
:
1641 return send_finished(this, type
, writer
);
1642 case STATE_FINISHED_SENT
:
1643 return INVALID_STATE
;
1645 return INVALID_STATE
;
1650 switch (this->state
)
1652 case STATE_HELLO_RECEIVED
:
1653 return send_server_hello(this, type
, writer
);
1654 case STATE_HELLO_SENT
:
1655 case STATE_CIPHERSPEC_CHANGED_OUT
:
1656 return send_encrypted_extensions(this, type
, writer
);
1657 case STATE_ENCRYPTED_EXTENSIONS_SENT
:
1660 return send_certificate_request(this, type
, writer
);
1662 /* otherwise fall through to next state */
1663 case STATE_CERTREQ_SENT
:
1664 return send_certificate(this, type
, writer
);
1665 case STATE_CERT_SENT
:
1666 return send_certificate_verify(this, type
, writer
);
1667 case STATE_CERT_VERIFY_SENT
:
1668 return send_finished(this, type
, writer
);
1669 case STATE_FINISHED_SENT
:
1670 if (!this->crypto
->derive_app_keys(this->crypto
))
1672 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1675 /* inbound key switches after process client finished message */
1676 this->crypto
->change_cipher(this->crypto
, FALSE
);
1677 this->state
= STATE_FINISHED_SENT_KEY_SWITCHED
;
1678 return INVALID_STATE
;
1679 case STATE_KEY_UPDATE_REQUESTED
:
1680 return send_key_update(this, type
, writer
);
1681 case STATE_KEY_UPDATE_SENT
:
1682 if (!this->crypto
->update_app_keys(this->crypto
, FALSE
))
1684 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1687 this->crypto
->change_cipher(this->crypto
, FALSE
);
1688 this->state
= STATE_FINISHED_RECEIVED
;
1690 return INVALID_STATE
;
1695 METHOD(tls_handshake_t
, cipherspec_changed
, bool,
1696 private_tls_server_t
*this, bool inbound
)
1698 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1704 return this->state
== STATE_FINISHED_SENT
;
1708 return this->state
== STATE_CERT_VERIFY_RECEIVED
;
1710 return this->state
== STATE_KEY_EXCHANGE_RECEIVED
;
1716 return this->state
== STATE_HELLO_SENT
;
1718 return this->state
== STATE_FINISHED_RECEIVED
;
1725 { /* accept ChangeCipherSpec after ServerFinish or HelloRetryRequest */
1726 return this->state
== STATE_FINISHED_SENT
||
1727 this->state
== STATE_FINISHED_SENT_KEY_SWITCHED
||
1732 return this->state
== STATE_HELLO_SENT
;
1737 METHOD(tls_handshake_t
, change_cipherspec
, void,
1738 private_tls_server_t
*this, bool inbound
)
1740 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1742 this->crypto
->change_cipher(this->crypto
, inbound
);
1746 { /* client might send a ChangeCipherSpec after a HelloRetryRequest and
1747 * before a new ClientHello which should not cause any state changes */
1753 this->state
= STATE_CIPHERSPEC_CHANGED_IN
;
1757 this->state
= STATE_CIPHERSPEC_CHANGED_OUT
;
1761 METHOD(tls_handshake_t
, finished
, bool,
1762 private_tls_server_t
*this)
1764 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1768 return this->state
== STATE_FINISHED_RECEIVED
;
1770 return this->state
== STATE_FINISHED_SENT
;
1774 return this->state
== STATE_FINISHED_RECEIVED
;
1778 METHOD(tls_handshake_t
, get_peer_id
, identification_t
*,
1779 private_tls_server_t
*this)
1784 METHOD(tls_handshake_t
, get_server_id
, identification_t
*,
1785 private_tls_server_t
*this)
1787 return this->server
;
1790 METHOD(tls_handshake_t
, get_auth
, auth_cfg_t
*,
1791 private_tls_server_t
*this)
1793 return this->peer_auth
;
1796 METHOD(tls_handshake_t
, destroy
, void,
1797 private_tls_server_t
*this)
1799 DESTROY_IF(this->private);
1800 DESTROY_IF(this->dh
);
1801 DESTROY_IF(this->peer
);
1802 this->server
->destroy(this->server
);
1803 this->peer_auth
->destroy(this->peer_auth
);
1804 this->server_auth
->destroy(this->server_auth
);
1805 free(this->hashsig
.ptr
);
1806 free(this->curves
.ptr
);
1807 free(this->session
.ptr
);
1814 tls_server_t
*tls_server_create(tls_t
*tls
,
1815 tls_crypto_t
*crypto
, tls_alert_t
*alert
,
1816 identification_t
*server
, identification_t
*peer
)
1818 private_tls_server_t
*this;
1823 .process
= _process
,
1825 .cipherspec_changed
= _cipherspec_changed
,
1826 .change_cipherspec
= _change_cipherspec
,
1827 .finished
= _finished
,
1828 .get_peer_id
= _get_peer_id
,
1829 .get_server_id
= _get_server_id
,
1830 .get_auth
= _get_auth
,
1831 .destroy
= _destroy
,
1837 .server
= server
->clone(server
),
1838 .peer
= peer ? peer
->clone(peer
) : NULL
,
1839 .state
= STATE_INIT
,
1840 .peer_auth
= auth_cfg_create(),
1841 .server_auth
= auth_cfg_create(),
1842 .send_certreq_authorities
= lib
->settings
->get_bool(lib
->settings
,
1843 "%s.tls.send_certreq_authorities",
1847 return &this->public;