2 * Copyright (C) 2020 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
;
169 * Find a trusted public key to encrypt/verify key exchange data
171 public_key_t
*tls_find_public_key(auth_cfg_t
*peer_auth
)
173 public_key_t
*public = NULL
, *current
;
174 certificate_t
*cert
, *found
;
175 enumerator_t
*enumerator
;
178 cert
= peer_auth
->get(peer_auth
, AUTH_HELPER_SUBJECT_CERT
);
181 enumerator
= lib
->credmgr
->create_public_enumerator(lib
->credmgr
,
182 KEY_ANY
, cert
->get_subject(cert
),
184 while (enumerator
->enumerate(enumerator
, ¤t
, &auth
))
186 found
= auth
->get(auth
, AUTH_RULE_SUBJECT_CERT
);
187 if (found
&& cert
->equals(cert
, found
))
189 public = current
->get_ref(current
);
190 peer_auth
->merge(peer_auth
, auth
, FALSE
);
194 enumerator
->destroy(enumerator
);
200 * Find a cipher suite and a server key
202 static bool select_suite_and_key(private_tls_server_t
*this,
203 tls_cipher_suite_t
*suites
, int count
)
205 tls_version_t version_min
, version_max
;
208 enumerator_t
*enumerator
;
210 version_min
= this->tls
->get_version_min(this->tls
);
211 version_max
= this->tls
->get_version_max(this->tls
);
212 enumerator
= tls_create_private_key_enumerator(version_min
, version_max
,
213 this->hashsig
, this->server
);
216 DBG1(DBG_TLS
, "no common signature algorithms found");
219 if (!enumerator
->enumerate(enumerator
, &key
, &auth
))
221 DBG1(DBG_TLS
, "no usable TLS server certificate found for '%Y'",
223 enumerator
->destroy(enumerator
);
227 if (version_max
>= TLS_1_3
)
229 this->suite
= this->crypto
->select_cipher_suite(this->crypto
, suites
,
234 this->suite
= this->crypto
->select_cipher_suite(this->crypto
, suites
,
235 count
, key
->get_type(key
));
236 while (!this->suite
&&
237 enumerator
->enumerate(enumerator
, &key
, &auth
))
238 { /* find a key and cipher suite for one of the remaining key types */
239 this->suite
= this->crypto
->select_cipher_suite(this->crypto
,
246 DBG1(DBG_TLS
, "received cipher suites or signature schemes unacceptable");
247 enumerator
->destroy(enumerator
);
250 DBG1(DBG_TLS
, "using key of type %N", key_type_names
, key
->get_type(key
));
251 this->private = key
->get_ref(key
);
252 this->server_auth
->merge(this->server_auth
, auth
, FALSE
);
253 enumerator
->destroy(enumerator
);
258 * Check if the peer supports a given TLS curve
260 static bool peer_supports_curve(private_tls_server_t
*this,
261 tls_named_group_t curve
)
263 bio_reader_t
*reader
;
266 if (!this->curves_received
)
267 { /* none received, assume yes */
270 reader
= bio_reader_create(this->curves
);
271 while (reader
->remaining(reader
) && reader
->read_uint16(reader
, ¤t
))
273 if (current
== curve
)
275 reader
->destroy(reader
);
279 reader
->destroy(reader
);
284 * TLS 1.3 key exchange key share
292 * Check if peer sent a key share of a given TLS named DH group
294 static bool peer_offered_curve(array_t
*key_shares
, tls_named_group_t curve
,
300 for (i
= 0; i
< array_count(key_shares
); i
++)
302 array_get(key_shares
, i
, &peer
);
303 if (curve
== peer
.curve
)
316 * Check if client is currently retrying to connect to the server.
318 static bool retrying(private_tls_server_t
*this)
320 return this->state
== STATE_INIT
&& this->requested_curve
;
324 * Process client hello message
326 static status_t
process_client_hello(private_tls_server_t
*this,
327 bio_reader_t
*reader
)
329 uint16_t legacy_version
= 0, version
= 0, extension_type
= 0;
330 chunk_t random
, session
, ciphers
, versions
= chunk_empty
, compression
;
331 chunk_t ext
= chunk_empty
, key_shares
= chunk_empty
;
332 key_share_t peer
= {0};
333 chunk_t extension_data
= chunk_empty
;
334 bio_reader_t
*extensions
, *extension
;
335 tls_cipher_suite_t
*suites
;
339 this->crypto
->append_handshake(this->crypto
,
340 TLS_CLIENT_HELLO
, reader
->peek(reader
));
342 if (!reader
->read_uint16(reader
, &legacy_version
) ||
343 !reader
->read_data(reader
, sizeof(this->client_random
), &random
) ||
344 !reader
->read_data8(reader
, &session
) ||
345 !reader
->read_data16(reader
, &ciphers
) ||
346 !reader
->read_data8(reader
, &compression
) ||
347 (reader
->remaining(reader
) && !reader
->read_data16(reader
, &ext
)))
349 DBG1(DBG_TLS
, "received invalid ClientHello");
350 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
354 /* before we do anything version-related, determine our supported suites
355 * as that might change the min./max. versions */
356 this->crypto
->get_cipher_suites(this->crypto
, NULL
);
358 extensions
= bio_reader_create(ext
);
359 while (extensions
->remaining(extensions
))
361 if (!extensions
->read_uint16(extensions
, &extension_type
) ||
362 !extensions
->read_data16(extensions
, &extension_data
))
364 DBG1(DBG_TLS
, "received invalid ClientHello Extensions");
365 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
366 extensions
->destroy(extensions
);
369 extension
= bio_reader_create(extension_data
);
370 DBG2(DBG_TLS
, "received TLS '%N' extension",
371 tls_extension_names
, extension_type
);
372 DBG3(DBG_TLS
, "%B", &extension_data
);
373 switch (extension_type
)
375 case TLS_EXT_SIGNATURE_ALGORITHMS
:
376 if (!extension
->read_data16(extension
, &extension_data
))
378 DBG1(DBG_TLS
, "invalid %N extension",
379 tls_extension_names
, extension_type
);
380 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
381 extensions
->destroy(extensions
);
382 extension
->destroy(extension
);
385 chunk_free(&this->hashsig
);
386 this->hashsig
= chunk_clone(extension_data
);
388 case TLS_EXT_SUPPORTED_GROUPS
:
389 if (!extension
->read_data16(extension
, &extension_data
))
391 DBG1(DBG_TLS
, "invalid %N extension",
392 tls_extension_names
, extension_type
);
393 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
394 extensions
->destroy(extensions
);
395 extension
->destroy(extension
);
398 chunk_free(&this->curves
);
399 this->curves_received
= TRUE
;
400 this->curves
= chunk_clone(extension_data
);
402 case TLS_EXT_SUPPORTED_VERSIONS
:
403 if (!extension
->read_data8(extension
, &versions
))
405 DBG1(DBG_TLS
, "invalid %N extension",
406 tls_extension_names
, extension_type
);
407 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
408 extensions
->destroy(extensions
);
409 extension
->destroy(extension
);
413 case TLS_EXT_KEY_SHARE
:
414 if (!extension
->read_data16(extension
, &key_shares
))
416 DBG1(DBG_TLS
, "invalid %N extension",
417 tls_extension_names
, extension_type
);
418 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
419 extensions
->destroy(extensions
);
420 extension
->destroy(extension
);
427 extension
->destroy(extension
);
429 extensions
->destroy(extensions
);
431 if (this->tls
->get_version_max(this->tls
) >= TLS_1_3
&& !this->hashsig
.len
)
433 DBG1(DBG_TLS
, "no %N extension received", tls_extension_names
,
434 TLS_MISSING_EXTENSION
);
435 this->alert
->add(this->alert
, TLS_FATAL
, TLS_MISSING_EXTENSION
);
439 memcpy(this->client_random
, random
.ptr
, sizeof(this->client_random
));
441 htoun32(&this->server_random
, time(NULL
));
442 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
444 !rng
->get_bytes(rng
, sizeof(this->server_random
) - 4,
445 this->server_random
+ 4))
447 DBG1(DBG_TLS
, "failed to generate server random");
448 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
456 bio_reader_t
*client_versions
;
458 client_versions
= bio_reader_create(versions
);
459 while (client_versions
->remaining(client_versions
))
461 if (client_versions
->read_uint16(client_versions
, &version
))
463 if (this->tls
->set_version(this->tls
, version
, version
))
465 this->client_version
= version
;
470 client_versions
->destroy(client_versions
);
474 version
= legacy_version
;
475 if (this->tls
->set_version(this->tls
, version
, version
))
477 this->client_version
= version
;
480 if (!this->client_version
)
482 DBG1(DBG_TLS
, "proposed version %N not supported", tls_version_names
,
484 this->alert
->add(this->alert
, TLS_FATAL
, TLS_PROTOCOL_VERSION
);
488 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
490 this->suite
= this->crypto
->resume_session(this->crypto
, session
,
492 chunk_from_thing(this->client_random
),
493 chunk_from_thing(this->server_random
));
498 this->session
= chunk_clone(session
);
500 DBG1(DBG_TLS
, "resumed %N using suite %N",
501 tls_version_names
, this->tls
->get_version_max(this->tls
),
502 tls_cipher_suite_names
, this->suite
);
506 count
= ciphers
.len
/ sizeof(uint16_t);
507 suites
= alloca(count
* sizeof(tls_cipher_suite_t
));
508 DBG2(DBG_TLS
, "received %d TLS cipher suites:", count
);
509 for (i
= 0; i
< count
; i
++)
511 suites
[i
] = untoh16(&ciphers
.ptr
[i
* sizeof(uint16_t)]);
512 DBG2(DBG_TLS
, " %N", tls_cipher_suite_names
, suites
[i
]);
514 if (!select_suite_and_key(this, suites
, count
))
516 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
519 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
521 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
523 !rng
->allocate_bytes(rng
, SESSION_ID_SIZE
, &this->session
))
525 DBG1(DBG_TLS
, "generating TLS session identifier failed, skipped");
531 this->session
= chunk_clone(session
);
533 DBG1(DBG_TLS
, "negotiated %N using suite %N",
534 tls_version_names
, this->tls
->get_version_max(this->tls
),
535 tls_cipher_suite_names
, this->suite
);
538 if (this->tls
->get_version_max(this->tls
) >= TLS_1_3
)
540 diffie_hellman_group_t group
;
541 tls_named_group_t curve
, requesting_curve
= 0;
542 enumerator_t
*enumerator
;
543 chunk_t shared_secret
= chunk_empty
;
544 array_t
*peer_key_shares
;
546 peer_key_shares
= array_create(sizeof(key_share_t
), 1);
547 extension
= bio_reader_create(key_shares
);
548 while (extension
->remaining(extension
))
550 if (!extension
->read_uint16(extension
, &peer
.curve
) ||
551 !extension
->read_data16(extension
, &peer
.key_share
) ||
554 DBG1(DBG_TLS
, "invalid %N extension",
555 tls_extension_names
, extension_type
);
556 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
557 extension
->destroy(extension
);
558 array_destroy(peer_key_shares
);
561 array_insert(peer_key_shares
, ARRAY_TAIL
, &peer
);
563 extension
->destroy(extension
);
565 enumerator
= this->crypto
->create_ec_enumerator(this->crypto
);
566 while (enumerator
->enumerate(enumerator
, &group
, &curve
))
568 if (!requesting_curve
&&
569 peer_supports_curve(this, curve
) &&
570 !peer_offered_curve(peer_key_shares
, curve
, NULL
))
572 requesting_curve
= curve
;
574 if (peer_supports_curve(this, curve
) &&
575 peer_offered_curve(peer_key_shares
, curve
, &peer
))
577 DBG1(DBG_TLS
, "using key exchange %N",
578 tls_named_group_names
, curve
);
579 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, group
);
583 enumerator
->destroy(enumerator
);
584 array_destroy(peer_key_shares
);
590 DBG1(DBG_TLS
, "already replied with a hello retry request");
591 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
595 if (!requesting_curve
)
597 DBG1(DBG_TLS
, "no mutual supported group in client hello");
598 this->alert
->add(this->alert
, TLS_FATAL
, TLS_ILLEGAL_PARAMETER
);
601 this->requested_curve
= requesting_curve
;
603 if (!this->crypto
->hash_handshake(this->crypto
, NULL
))
605 DBG1(DBG_TLS
, "failed to hash handshake messages");
606 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
612 if (peer
.key_share
.len
&&
613 peer
.curve
!= TLS_CURVE25519
&&
614 peer
.curve
!= TLS_CURVE448
)
615 { /* classic format (see RFC 8446, section 4.2.8.2) */
616 if (peer
.key_share
.ptr
[0] != TLS_ANSI_UNCOMPRESSED
)
618 DBG1(DBG_TLS
, "DH point format '%N' not supported",
619 tls_ansi_point_format_names
, peer
.key_share
.ptr
[0]);
620 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
623 peer
.key_share
= chunk_skip(peer
.key_share
, 1);
625 if (!peer
.key_share
.len
||
626 !this->dh
->set_other_public_value(this->dh
, peer
.key_share
))
628 DBG1(DBG_TLS
, "DH key derivation failed");
629 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
630 chunk_clear(&shared_secret
);
633 chunk_clear(&shared_secret
);
634 this->requested_curve
= 0;
638 this->state
= STATE_HELLO_RECEIVED
;
643 * Process certificate
645 static status_t
process_certificate(private_tls_server_t
*this,
646 bio_reader_t
*reader
)
653 this->crypto
->append_handshake(this->crypto
,
654 TLS_CERTIFICATE
, reader
->peek(reader
));
656 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
658 if (!reader
->read_data8(reader
, &data
))
660 DBG1(DBG_TLS
, "certificate request context invalid");
661 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
666 if (!reader
->read_data24(reader
, &data
))
668 DBG1(DBG_TLS
, "certificate message header invalid");
669 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
672 certs
= bio_reader_create(data
);
673 if (!certs
->remaining(certs
))
675 DBG1(DBG_TLS
, "no certificate sent by peer");
676 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
679 while (certs
->remaining(certs
))
681 if (!certs
->read_data24(certs
, &data
))
683 DBG1(DBG_TLS
, "certificate message invalid");
684 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
685 certs
->destroy(certs
);
688 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
689 BUILD_BLOB_ASN1_DER
, data
, BUILD_END
);
694 this->peer_auth
->add(this->peer_auth
,
695 AUTH_HELPER_SUBJECT_CERT
, cert
);
696 DBG1(DBG_TLS
, "received TLS peer certificate '%Y'",
697 cert
->get_subject(cert
));
702 DBG1(DBG_TLS
, "received TLS intermediate certificate '%Y'",
703 cert
->get_subject(cert
));
704 this->peer_auth
->add(this->peer_auth
, AUTH_HELPER_IM_CERT
, cert
);
709 DBG1(DBG_TLS
, "parsing TLS certificate failed, skipped");
710 this->alert
->add(this->alert
, TLS_WARNING
, TLS_BAD_CERTIFICATE
);
712 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
714 if (!certs
->read_data16(certs
, &data
))
716 DBG1(DBG_TLS
, "failed to read extensions of CertificateEntry");
717 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
722 certs
->destroy(certs
);
723 this->state
= STATE_CERT_RECEIVED
;
728 * Process Client Key Exchange, using premaster encryption
730 static status_t
process_key_exchange_encrypted(private_tls_server_t
*this,
731 bio_reader_t
*reader
)
733 chunk_t encrypted
, decrypted
;
737 this->crypto
->append_handshake(this->crypto
,
738 TLS_CLIENT_KEY_EXCHANGE
, reader
->peek(reader
));
740 if (!reader
->read_data16(reader
, &encrypted
))
742 DBG1(DBG_TLS
, "received invalid Client Key Exchange");
743 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
747 htoun16(premaster
, this->client_version
);
748 /* pre-randomize premaster for failure cases */
749 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
750 if (!rng
|| !rng
->get_bytes(rng
, sizeof(premaster
) - 2, premaster
+ 2))
752 DBG1(DBG_TLS
, "failed to generate premaster secret");
753 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
760 this->private->decrypt(this->private,
761 ENCRYPT_RSA_PKCS1
, encrypted
, &decrypted
))
763 if (decrypted
.len
== sizeof(premaster
) &&
764 untoh16(decrypted
.ptr
) == this->client_version
)
766 memcpy(premaster
+ 2, decrypted
.ptr
+ 2, sizeof(premaster
) - 2);
770 DBG1(DBG_TLS
, "decrypted premaster has invalid length/version");
772 chunk_clear(&decrypted
);
776 DBG1(DBG_TLS
, "decrypting Client Key Exchange failed");
779 if (!this->crypto
->derive_secrets(this->crypto
, chunk_from_thing(premaster
),
780 this->session
, this->peer
,
781 chunk_from_thing(this->client_random
),
782 chunk_from_thing(this->server_random
)))
784 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
788 this->state
= STATE_KEY_EXCHANGE_RECEIVED
;
793 * Process client key exchange, using DHE exchange
795 static status_t
process_key_exchange_dhe(private_tls_server_t
*this,
796 bio_reader_t
*reader
)
798 chunk_t premaster
, pub
;
801 this->crypto
->append_handshake(this->crypto
,
802 TLS_CLIENT_KEY_EXCHANGE
, reader
->peek(reader
));
804 ec
= diffie_hellman_group_is_ec(this->dh
->get_dh_group(this->dh
));
805 if ((ec
&& !reader
->read_data8(reader
, &pub
)) ||
806 (!ec
&& (!reader
->read_data16(reader
, &pub
) || pub
.len
== 0)))
808 DBG1(DBG_TLS
, "received invalid Client Key Exchange");
809 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
815 if (pub
.ptr
[0] != TLS_ANSI_UNCOMPRESSED
)
817 DBG1(DBG_TLS
, "DH point format '%N' not supported",
818 tls_ansi_point_format_names
, pub
.ptr
[0]);
819 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
822 pub
= chunk_skip(pub
, 1);
824 if (!this->dh
->set_other_public_value(this->dh
, pub
))
826 DBG1(DBG_TLS
, "applying DH public value failed");
827 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
830 if (!this->dh
->get_shared_secret(this->dh
, &premaster
))
832 DBG1(DBG_TLS
, "calculating premaster from DH failed");
833 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
837 if (!this->crypto
->derive_secrets(this->crypto
, premaster
,
838 this->session
, this->peer
,
839 chunk_from_thing(this->client_random
),
840 chunk_from_thing(this->server_random
)))
842 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
843 chunk_clear(&premaster
);
846 chunk_clear(&premaster
);
848 this->state
= STATE_KEY_EXCHANGE_RECEIVED
;
853 * Process Client Key Exchange
855 static status_t
process_key_exchange(private_tls_server_t
*this,
856 bio_reader_t
*reader
)
860 return process_key_exchange_dhe(this, reader
);
862 return process_key_exchange_encrypted(this, reader
);
866 * Process Certificate verify
868 static status_t
process_cert_verify(private_tls_server_t
*this,
869 bio_reader_t
*reader
)
871 public_key_t
*public;
874 public = tls_find_public_key(this->peer_auth
);
877 DBG1(DBG_TLS
, "no trusted certificate found for '%Y' to verify TLS peer",
879 this->alert
->add(this->alert
, TLS_FATAL
, TLS_CERTIFICATE_UNKNOWN
);
883 msg
= reader
->peek(reader
);
884 if (!this->crypto
->verify_handshake(this->crypto
, public, reader
))
886 public->destroy(public);
887 DBG1(DBG_TLS
, "signature verification failed");
888 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECRYPT_ERROR
);
891 public->destroy(public);
892 this->state
= STATE_CERT_VERIFY_RECEIVED
;
893 this->crypto
->append_handshake(this->crypto
, TLS_CERTIFICATE_VERIFY
, msg
);
898 * Process finished message
900 static status_t
process_finished(private_tls_server_t
*this,
901 bio_reader_t
*reader
)
903 chunk_t received
, verify_data
;
906 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
908 if (!reader
->read_data(reader
, sizeof(buf
), &received
))
910 DBG1(DBG_TLS
, "received client finished too short");
911 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
914 if (!this->crypto
->calculate_finished_legacy(this->crypto
,
915 "client finished", buf
))
917 DBG1(DBG_TLS
, "calculating client finished failed");
918 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
921 verify_data
= chunk_from_thing(buf
);
925 received
= reader
->peek(reader
);
926 if (!this->crypto
->calculate_finished(this->crypto
, FALSE
, &verify_data
))
928 DBG1(DBG_TLS
, "calculating client finished failed");
929 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
932 this->crypto
->change_cipher(this->crypto
, TRUE
);
935 if (!chunk_equals_const(received
, verify_data
))
937 DBG1(DBG_TLS
, "received client finished invalid");
938 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECRYPT_ERROR
);
942 if (verify_data
.ptr
!= buf
)
944 chunk_free(&verify_data
);
947 this->crypto
->append_handshake(this->crypto
, TLS_FINISHED
, received
);
948 this->state
= STATE_FINISHED_RECEIVED
;
953 * Process KeyUpdate message
955 static status_t
process_key_update(private_tls_server_t
*this,
956 bio_reader_t
*reader
)
958 uint8_t update_requested
;
960 if (!reader
->read_uint8(reader
, &update_requested
) ||
961 update_requested
> 1)
963 DBG1(DBG_TLS
, "received invalid KeyUpdate");
964 this->alert
->add(this->alert
, TLS_FATAL
, TLS_DECODE_ERROR
);
968 if (!this->crypto
->update_app_keys(this->crypto
, TRUE
))
970 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
973 this->crypto
->change_cipher(this->crypto
, TRUE
);
975 if (update_requested
)
977 DBG1(DBG_TLS
, "client requested KeyUpdate");
978 this->state
= STATE_KEY_UPDATE_REQUESTED
;
983 METHOD(tls_handshake_t
, process
, status_t
,
984 private_tls_server_t
*this, tls_handshake_type_t type
, bio_reader_t
*reader
)
986 tls_handshake_type_t expected
;
988 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
993 if (type
== TLS_CLIENT_HELLO
)
995 return process_client_hello(this, reader
);
997 expected
= TLS_CLIENT_HELLO
;
999 case STATE_HELLO_DONE
:
1000 if (type
== TLS_CERTIFICATE
)
1002 return process_certificate(this, reader
);
1006 expected
= TLS_CERTIFICATE
;
1009 /* otherwise fall through to next state */
1010 case STATE_CERT_RECEIVED
:
1011 if (type
== TLS_CLIENT_KEY_EXCHANGE
)
1013 return process_key_exchange(this, reader
);
1015 expected
= TLS_CLIENT_KEY_EXCHANGE
;
1017 case STATE_KEY_EXCHANGE_RECEIVED
:
1018 if (type
== TLS_CERTIFICATE_VERIFY
)
1020 return process_cert_verify(this, reader
);
1024 expected
= TLS_CERTIFICATE_VERIFY
;
1027 return INVALID_STATE
;
1028 case STATE_CIPHERSPEC_CHANGED_IN
:
1029 if (type
== TLS_FINISHED
)
1031 return process_finished(this, reader
);
1033 expected
= TLS_FINISHED
;
1036 DBG1(DBG_TLS
, "TLS %N not expected in current state",
1037 tls_handshake_type_names
, type
);
1038 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1044 switch (this->state
)
1047 if (type
== TLS_CLIENT_HELLO
)
1049 return process_client_hello(this, reader
);
1051 expected
= TLS_CLIENT_HELLO
;
1053 case STATE_CIPHERSPEC_CHANGED_IN
:
1054 case STATE_FINISHED_SENT
:
1055 case STATE_FINISHED_SENT_KEY_SWITCHED
:
1056 if (type
== TLS_CERTIFICATE
)
1058 return process_certificate(this, reader
);
1062 expected
= TLS_CERTIFICATE
;
1065 /* otherwise fall through to next state */
1066 case STATE_CERT_RECEIVED
:
1067 if (type
== TLS_CERTIFICATE_VERIFY
)
1069 return process_cert_verify(this, reader
);
1073 expected
= TLS_CERTIFICATE_VERIFY
;
1076 /* otherwise fall through to next state */
1077 case STATE_CERT_VERIFY_RECEIVED
:
1078 if (type
== TLS_FINISHED
)
1080 return process_finished(this, reader
);
1083 case STATE_FINISHED_RECEIVED
:
1084 if (type
== TLS_KEY_UPDATE
)
1086 return process_key_update(this, reader
);
1088 return INVALID_STATE
;
1090 DBG1(DBG_TLS
, "TLS %N not expected in current state",
1091 tls_handshake_type_names
, type
);
1092 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1096 DBG1(DBG_TLS
, "TLS %N expected, but received %N",
1097 tls_handshake_type_names
, expected
, tls_handshake_type_names
, type
);
1098 this->alert
->add(this->alert
, TLS_FATAL
, TLS_UNEXPECTED_MESSAGE
);
1103 * Write public key into key share extension
1105 bool tls_write_key_share(bio_writer_t
**key_share
, diffie_hellman_t
*dh
)
1107 bio_writer_t
*writer
;
1108 tls_named_group_t curve
;
1115 curve
= tls_ec_group_to_curve(dh
->get_dh_group(dh
));
1116 if (!curve
|| !dh
->get_my_public_value(dh
, &pub
))
1120 *key_share
= writer
= bio_writer_create(pub
.len
+ 7);
1121 writer
->write_uint16(writer
, curve
);
1122 if (curve
== TLS_CURVE25519
||
1123 curve
== TLS_CURVE448
)
1125 writer
->write_data16(writer
, pub
);
1128 { /* classic format (see RFC 8446, section 4.2.8.2) */
1129 writer
->write_uint16(writer
, pub
.len
+ 1);
1130 writer
->write_uint8(writer
, TLS_ANSI_UNCOMPRESSED
);
1131 writer
->write_data(writer
, pub
);
1138 * Send ServerHello message
1140 static status_t
send_server_hello(private_tls_server_t
*this,
1141 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1143 bio_writer_t
*key_share
, *extensions
;
1144 tls_version_t version
;
1146 version
= this->tls
->get_version_max(this->tls
);
1148 /* cap legacy version at TLS 1.2 for middlebox compatibility */
1149 writer
->write_uint16(writer
, min(TLS_1_2
, version
));
1151 if (this->requested_curve
)
1153 writer
->write_data(writer
, tls_hello_retry_request_magic
);
1157 writer
->write_data(writer
, chunk_from_thing(this->server_random
));
1160 /* session identifier if we have one */
1161 writer
->write_data8(writer
, this->session
);
1163 /* add selected TLS cipher suite */
1164 writer
->write_uint16(writer
, this->suite
);
1166 /* NULL compression only */
1167 writer
->write_uint8(writer
, 0);
1169 if (version
>= TLS_1_3
)
1171 extensions
= bio_writer_create(32);
1173 DBG2(DBG_TLS
, "sending extension: %N",
1174 tls_extension_names
, TLS_EXT_SUPPORTED_VERSIONS
);
1175 extensions
->write_uint16(extensions
, TLS_EXT_SUPPORTED_VERSIONS
);
1176 extensions
->write_uint16(extensions
, 2);
1177 extensions
->write_uint16(extensions
, version
);
1179 DBG2(DBG_TLS
, "sending extension: %N",
1180 tls_extension_names
, TLS_EXT_KEY_SHARE
);
1181 extensions
->write_uint16(extensions
, TLS_EXT_KEY_SHARE
);
1182 if (this->requested_curve
)
1184 DBG1(DBG_TLS
, "requesting key exchange with %N",
1185 tls_named_group_names
, this->requested_curve
);
1186 extensions
->write_uint16(extensions
, 2);
1187 extensions
->write_uint16(extensions
, this->requested_curve
);
1191 if (!tls_write_key_share(&key_share
, this->dh
))
1193 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1194 extensions
->destroy(extensions
);
1197 extensions
->write_data16(extensions
, key_share
->get_buf(key_share
));
1198 key_share
->destroy(key_share
);
1201 writer
->write_data16(writer
, extensions
->get_buf(extensions
));
1202 extensions
->destroy(extensions
);
1205 *type
= TLS_SERVER_HELLO
;
1206 this->state
= this->requested_curve ? STATE_INIT
: STATE_HELLO_SENT
;
1207 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1212 * Send encrypted extensions message
1214 static status_t
send_encrypted_extensions(private_tls_server_t
*this,
1215 tls_handshake_type_t
*type
,
1216 bio_writer_t
*writer
)
1218 chunk_t shared_secret
= chunk_empty
;
1220 if (!this->dh
->get_shared_secret(this->dh
, &shared_secret
) ||
1221 !this->crypto
->derive_handshake_keys(this->crypto
, shared_secret
))
1223 DBG1(DBG_TLS
, "DH key derivation failed");
1224 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
1225 chunk_clear(&shared_secret
);
1228 chunk_clear(&shared_secret
);
1230 this->crypto
->change_cipher(this->crypto
, TRUE
);
1231 this->crypto
->change_cipher(this->crypto
, FALSE
);
1233 /* currently no extensions are supported */
1234 writer
->write_uint16(writer
, 0);
1236 *type
= TLS_ENCRYPTED_EXTENSIONS
;
1237 this->state
= STATE_ENCRYPTED_EXTENSIONS_SENT
;
1238 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1245 static status_t
send_certificate(private_tls_server_t
*this,
1246 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1248 enumerator_t
*enumerator
;
1249 certificate_t
*cert
;
1251 bio_writer_t
*certs
;
1254 /* certificate request context as described in RFC 8446, section 4.4.2 */
1255 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
1257 writer
->write_uint8(writer
, 0);
1260 /* generate certificate payload */
1261 certs
= bio_writer_create(256);
1262 cert
= this->server_auth
->get(this->server_auth
, AUTH_RULE_SUBJECT_CERT
);
1265 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &data
))
1267 DBG1(DBG_TLS
, "sending TLS server certificate '%Y'",
1268 cert
->get_subject(cert
));
1269 certs
->write_data24(certs
, data
);
1272 /* extensions see RFC 8446, section 4.4.2 */
1273 if (this->tls
->get_version_max(this->tls
) > TLS_1_2
)
1275 certs
->write_uint16(certs
, 0);
1278 enumerator
= this->server_auth
->create_enumerator(this->server_auth
);
1279 while (enumerator
->enumerate(enumerator
, &rule
, &cert
))
1281 if (rule
== AUTH_RULE_IM_CERT
)
1283 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &data
))
1285 DBG1(DBG_TLS
, "sending TLS intermediate certificate '%Y'",
1286 cert
->get_subject(cert
));
1287 certs
->write_data24(certs
, data
);
1292 enumerator
->destroy(enumerator
);
1294 writer
->write_data24(writer
, certs
->get_buf(certs
));
1295 certs
->destroy(certs
);
1297 *type
= TLS_CERTIFICATE
;
1298 this->state
= STATE_CERT_SENT
;
1299 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1304 * Send Certificate Verify
1306 static status_t
send_certificate_verify(private_tls_server_t
*this,
1307 tls_handshake_type_t
*type
,
1308 bio_writer_t
*writer
)
1310 if (!this->crypto
->sign_handshake(this->crypto
, this->private, writer
,
1313 DBG1(DBG_TLS
, "signature generation failed");
1314 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1318 *type
= TLS_CERTIFICATE_VERIFY
;
1319 this->state
= STATE_CERT_VERIFY_SENT
;
1320 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1325 * Write all available certificate authorities to output writer
1327 static void write_certificate_authorities(bio_writer_t
*writer
)
1329 bio_writer_t
*authorities
;
1330 enumerator_t
*enumerator
;
1331 certificate_t
*cert
;
1333 identification_t
*id
;
1335 authorities
= bio_writer_create(64);
1336 enumerator
= lib
->credmgr
->create_cert_enumerator(lib
->credmgr
, CERT_X509
,
1337 KEY_RSA
, NULL
, TRUE
);
1338 while (enumerator
->enumerate(enumerator
, &cert
))
1340 x509
= (x509_t
*)cert
;
1341 if (x509
->get_flags(x509
) & X509_CA
)
1343 id
= cert
->get_subject(cert
);
1344 DBG1(DBG_TLS
, "sending TLS cert request for '%Y'", id
);
1345 authorities
->write_data16(authorities
, id
->get_encoding(id
));
1348 enumerator
->destroy(enumerator
);
1349 writer
->write_data16(writer
, authorities
->get_buf(authorities
));
1350 authorities
->destroy(authorities
);
1354 * Send Certificate Request
1356 static status_t
send_certificate_request(private_tls_server_t
*this,
1357 tls_handshake_type_t
*type
,
1358 bio_writer_t
*writer
)
1360 bio_writer_t
*authorities
, *supported
, *extensions
;
1362 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1364 supported
= bio_writer_create(4);
1365 /* we propose both RSA and ECDSA */
1366 supported
->write_uint8(supported
, TLS_RSA_SIGN
);
1367 supported
->write_uint8(supported
, TLS_ECDSA_SIGN
);
1368 writer
->write_data8(writer
, supported
->get_buf(supported
));
1369 supported
->destroy(supported
);
1370 if (this->tls
->get_version_max(this->tls
) >= TLS_1_2
)
1372 this->crypto
->get_signature_algorithms(this->crypto
, writer
, TRUE
);
1375 write_certificate_authorities(writer
);
1379 /* certificate request context as described in RFC 8446, section 4.3.2 */
1380 writer
->write_uint8(writer
, 0);
1382 extensions
= bio_writer_create(32);
1383 DBG2(DBG_TLS
, "sending extension: %N",
1384 tls_extension_names
, TLS_EXT_CERTIFICATE_AUTHORITIES
);
1385 authorities
= bio_writer_create(64);
1386 write_certificate_authorities(authorities
);
1387 extensions
->write_uint16(extensions
, TLS_EXT_CERTIFICATE_AUTHORITIES
);
1388 extensions
->write_data16(extensions
, authorities
->get_buf(authorities
));
1389 authorities
->destroy(authorities
);
1391 DBG2(DBG_TLS
, "sending extension: %N",
1392 tls_extension_names
, TLS_EXT_SIGNATURE_ALGORITHMS
);
1393 extensions
->write_uint16(extensions
, TLS_EXT_SIGNATURE_ALGORITHMS
);
1394 supported
= bio_writer_create(32);
1395 this->crypto
->get_signature_algorithms(this->crypto
, supported
, TRUE
);
1396 extensions
->write_data16(extensions
, supported
->get_buf(supported
));
1397 supported
->destroy(supported
);
1398 writer
->write_data16(writer
, extensions
->get_buf(extensions
));
1399 extensions
->destroy(extensions
);
1402 *type
= TLS_CERTIFICATE_REQUEST
;
1403 this->state
= STATE_CERTREQ_SENT
;
1404 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1409 * Try to find a curve supported by both, client and server
1411 static bool find_supported_curve(private_tls_server_t
*this,
1412 tls_named_group_t
*curve
)
1414 tls_named_group_t current
;
1415 enumerator_t
*enumerator
;
1417 enumerator
= this->crypto
->create_ec_enumerator(this->crypto
);
1418 while (enumerator
->enumerate(enumerator
, NULL
, ¤t
))
1420 if (peer_supports_curve(this, current
))
1423 enumerator
->destroy(enumerator
);
1427 enumerator
->destroy(enumerator
);
1432 * Send Server key Exchange
1434 static status_t
send_server_key_exchange(private_tls_server_t
*this,
1435 tls_handshake_type_t
*type
, bio_writer_t
*writer
,
1436 diffie_hellman_group_t group
)
1438 diffie_hellman_params_t
*params
= NULL
;
1439 tls_named_group_t curve
;
1442 if (diffie_hellman_group_is_ec(group
))
1444 curve
= tls_ec_group_to_curve(group
);
1445 if (!curve
|| (!peer_supports_curve(this, curve
) &&
1446 !find_supported_curve(this, &curve
)))
1448 DBG1(DBG_TLS
, "no EC group supported by client and server");
1449 this->alert
->add(this->alert
, TLS_FATAL
, TLS_HANDSHAKE_FAILURE
);
1452 DBG2(DBG_TLS
, "selected ECDH group %N", tls_named_group_names
, curve
);
1453 writer
->write_uint8(writer
, TLS_ECC_NAMED_CURVE
);
1454 writer
->write_uint16(writer
, curve
);
1458 params
= diffie_hellman_get_params(group
);
1461 DBG1(DBG_TLS
, "no parameters found for DH group %N",
1462 diffie_hellman_group_names
, group
);
1463 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1466 DBG2(DBG_TLS
, "selected DH group %N", diffie_hellman_group_names
, group
);
1467 writer
->write_data16(writer
, params
->prime
);
1468 writer
->write_data16(writer
, params
->generator
);
1470 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, group
);
1473 DBG1(DBG_TLS
, "DH group %N not supported",
1474 diffie_hellman_group_names
, group
);
1475 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1478 if (!this->dh
->get_my_public_value(this->dh
, &chunk
))
1480 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1485 writer
->write_data16(writer
, chunk
);
1488 { /* ECP uses 8bit length header only, but a point format */
1489 writer
->write_uint8(writer
, chunk
.len
+ 1);
1490 writer
->write_uint8(writer
, TLS_ANSI_UNCOMPRESSED
);
1491 writer
->write_data(writer
, chunk
);
1495 chunk
= chunk_cat("ccc", chunk_from_thing(this->client_random
),
1496 chunk_from_thing(this->server_random
), writer
->get_buf(writer
));
1497 if (!this->private || !this->crypto
->sign(this->crypto
, this->private,
1498 writer
, chunk
, this->hashsig
))
1500 DBG1(DBG_TLS
, "signing DH parameters failed");
1501 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1506 *type
= TLS_SERVER_KEY_EXCHANGE
;
1507 this->state
= STATE_KEY_EXCHANGE_SENT
;
1508 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1515 static status_t
send_hello_done(private_tls_server_t
*this,
1516 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1518 *type
= TLS_SERVER_HELLO_DONE
;
1519 this->state
= STATE_HELLO_DONE
;
1520 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1527 static status_t
send_finished(private_tls_server_t
*this,
1528 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1530 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1534 if (!this->crypto
->calculate_finished_legacy(this->crypto
,
1535 "server finished", buf
))
1537 DBG1(DBG_TLS
, "calculating server finished data failed");
1538 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1542 writer
->write_data(writer
, chunk_from_thing(buf
));
1546 chunk_t verify_data
;
1548 if (!this->crypto
->calculate_finished(this->crypto
, TRUE
, &verify_data
))
1550 DBG1(DBG_TLS
, "calculating server finished data failed");
1551 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1555 writer
->write_data(writer
, verify_data
);
1556 chunk_free(&verify_data
);
1559 *type
= TLS_FINISHED
;
1560 this->state
= STATE_FINISHED_SENT
;
1561 this->crypto
->append_handshake(this->crypto
, *type
, writer
->get_buf(writer
));
1567 * Send KeyUpdate message
1569 static status_t
send_key_update(private_tls_server_t
*this,
1570 tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1572 *type
= TLS_KEY_UPDATE
;
1574 /* we currently only send this as reply, so we never request an update */
1575 writer
->write_uint8(writer
, 0);
1577 this->state
= STATE_KEY_UPDATE_SENT
;
1581 METHOD(tls_handshake_t
, build
, status_t
,
1582 private_tls_server_t
*this, tls_handshake_type_t
*type
, bio_writer_t
*writer
)
1584 diffie_hellman_group_t group
;
1586 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1588 switch (this->state
)
1590 case STATE_HELLO_RECEIVED
:
1591 return send_server_hello(this, type
, writer
);
1592 case STATE_HELLO_SENT
:
1593 return send_certificate(this, type
, writer
);
1594 case STATE_CERT_SENT
:
1595 group
= this->crypto
->get_dh_group(this->crypto
);
1598 return send_server_key_exchange(this, type
, writer
, group
);
1600 /* otherwise fall through to next state */
1601 case STATE_KEY_EXCHANGE_SENT
:
1604 return send_certificate_request(this, type
, writer
);
1606 /* otherwise fall through to next state */
1607 case STATE_CERTREQ_SENT
:
1608 return send_hello_done(this, type
, writer
);
1609 case STATE_CIPHERSPEC_CHANGED_OUT
:
1610 return send_finished(this, type
, writer
);
1611 case STATE_FINISHED_SENT
:
1612 return INVALID_STATE
;
1614 return INVALID_STATE
;
1619 switch (this->state
)
1621 case STATE_HELLO_RECEIVED
:
1622 return send_server_hello(this, type
, writer
);
1623 case STATE_HELLO_SENT
:
1624 case STATE_CIPHERSPEC_CHANGED_OUT
:
1625 return send_encrypted_extensions(this, type
, writer
);
1626 case STATE_ENCRYPTED_EXTENSIONS_SENT
:
1629 return send_certificate_request(this, type
, writer
);
1631 /* otherwise fall through to next state */
1632 case STATE_CERTREQ_SENT
:
1633 return send_certificate(this, type
, writer
);
1634 case STATE_CERT_SENT
:
1635 return send_certificate_verify(this, type
, writer
);
1636 case STATE_CERT_VERIFY_SENT
:
1637 return send_finished(this, type
, writer
);
1638 case STATE_FINISHED_SENT
:
1639 if (!this->crypto
->derive_app_keys(this->crypto
))
1641 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1644 /* inbound key switches after process client finished message */
1645 this->crypto
->change_cipher(this->crypto
, FALSE
);
1646 this->state
= STATE_FINISHED_SENT_KEY_SWITCHED
;
1647 return INVALID_STATE
;
1648 case STATE_KEY_UPDATE_REQUESTED
:
1649 return send_key_update(this, type
, writer
);
1650 case STATE_KEY_UPDATE_SENT
:
1651 if (!this->crypto
->update_app_keys(this->crypto
, FALSE
))
1653 this->alert
->add(this->alert
, TLS_FATAL
, TLS_INTERNAL_ERROR
);
1656 this->crypto
->change_cipher(this->crypto
, FALSE
);
1657 this->state
= STATE_FINISHED_RECEIVED
;
1659 return INVALID_STATE
;
1664 METHOD(tls_handshake_t
, cipherspec_changed
, bool,
1665 private_tls_server_t
*this, bool inbound
)
1667 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1673 return this->state
== STATE_FINISHED_SENT
;
1677 return this->state
== STATE_CERT_VERIFY_RECEIVED
;
1679 return this->state
== STATE_KEY_EXCHANGE_RECEIVED
;
1685 return this->state
== STATE_HELLO_SENT
;
1687 return this->state
== STATE_FINISHED_RECEIVED
;
1694 { /* accept ChangeCipherSpec after ServerFinish or HelloRetryRequest */
1695 return this->state
== STATE_FINISHED_SENT
||
1696 this->state
== STATE_FINISHED_SENT_KEY_SWITCHED
||
1701 return this->state
== STATE_HELLO_SENT
;
1706 METHOD(tls_handshake_t
, change_cipherspec
, void,
1707 private_tls_server_t
*this, bool inbound
)
1709 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1711 this->crypto
->change_cipher(this->crypto
, inbound
);
1715 { /* client might send a ChangeCipherSpec after a HelloRetryRequest and
1716 * before a new ClientHello which should not cause any state changes */
1722 this->state
= STATE_CIPHERSPEC_CHANGED_IN
;
1726 this->state
= STATE_CIPHERSPEC_CHANGED_OUT
;
1730 METHOD(tls_handshake_t
, finished
, bool,
1731 private_tls_server_t
*this)
1733 if (this->tls
->get_version_max(this->tls
) < TLS_1_3
)
1737 return this->state
== STATE_FINISHED_RECEIVED
;
1739 return this->state
== STATE_FINISHED_SENT
;
1743 return this->state
== STATE_FINISHED_RECEIVED
;
1747 METHOD(tls_handshake_t
, get_peer_id
, identification_t
*,
1748 private_tls_server_t
*this)
1753 METHOD(tls_handshake_t
, get_server_id
, identification_t
*,
1754 private_tls_server_t
*this)
1756 return this->server
;
1759 METHOD(tls_handshake_t
, get_auth
, auth_cfg_t
*,
1760 private_tls_server_t
*this)
1762 return this->peer_auth
;
1765 METHOD(tls_handshake_t
, destroy
, void,
1766 private_tls_server_t
*this)
1768 DESTROY_IF(this->private);
1769 DESTROY_IF(this->dh
);
1770 DESTROY_IF(this->peer
);
1771 this->server
->destroy(this->server
);
1772 this->peer_auth
->destroy(this->peer_auth
);
1773 this->server_auth
->destroy(this->server_auth
);
1774 free(this->hashsig
.ptr
);
1775 free(this->curves
.ptr
);
1776 free(this->session
.ptr
);
1783 tls_server_t
*tls_server_create(tls_t
*tls
,
1784 tls_crypto_t
*crypto
, tls_alert_t
*alert
,
1785 identification_t
*server
, identification_t
*peer
)
1787 private_tls_server_t
*this;
1792 .process
= _process
,
1794 .cipherspec_changed
= _cipherspec_changed
,
1795 .change_cipherspec
= _change_cipherspec
,
1796 .finished
= _finished
,
1797 .get_peer_id
= _get_peer_id
,
1798 .get_server_id
= _get_server_id
,
1799 .get_auth
= _get_auth
,
1800 .destroy
= _destroy
,
1806 .server
= server
->clone(server
),
1807 .peer
= peer ? peer
->clone(peer
) : NULL
,
1808 .state
= STATE_INIT
,
1809 .peer_auth
= auth_cfg_create(),
1810 .server_auth
= auth_cfg_create(),
1813 return &this->public;