]> git.ipfire.org Git - people/ms/strongswan.git/blame - src/libtls/tls_server.c
libtls: Enforce client/server identity when looking for public key
[people/ms/strongswan.git] / src / libtls / tls_server.c
CommitLineData
4c0c2283 1/*
dc9f6c68 2 * Copyright (C) 2020-2021 Pascal Knecht
7fbe2e27
PK
3 * HSR Hochschule fuer Technik Rapperswil
4 *
4c0c2283
MW
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
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>.
12 *
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
16 * for more details.
17 */
18
19#include "tls_server.h"
20
400df4ca
MW
21#include <time.h>
22
f05b4272 23#include <utils/debug.h>
714d0bfd 24#include <credentials/certificates/x509.h>
3767a4a6 25#include <collections/array.h>
4c0c2283
MW
26
27typedef struct private_tls_server_t private_tls_server_t;
28
6a5c86b7
MW
29/**
30 * Size of a session ID
31 */
32#define SESSION_ID_SIZE 16
400df4ca
MW
33
34typedef enum {
35 STATE_INIT,
36 STATE_HELLO_RECEIVED,
37 STATE_HELLO_SENT,
38 STATE_CERT_SENT,
f14358a9 39 STATE_KEY_EXCHANGE_SENT,
400df4ca
MW
40 STATE_CERTREQ_SENT,
41 STATE_HELLO_DONE,
42 STATE_CERT_RECEIVED,
43 STATE_KEY_EXCHANGE_RECEIVED,
44 STATE_CERT_VERIFY_RECEIVED,
45 STATE_CIPHERSPEC_CHANGED_IN,
46 STATE_FINISHED_RECEIVED,
47 STATE_CIPHERSPEC_CHANGED_OUT,
48 STATE_FINISHED_SENT,
7fbe2e27
PK
49 /* new states in TLS 1.3 */
50 STATE_ENCRYPTED_EXTENSIONS_SENT,
51 STATE_CERT_VERIFY_SENT,
ab70f68c
PK
52 STATE_KEY_UPDATE_REQUESTED,
53 STATE_KEY_UPDATE_SENT,
d2fc9b09 54 STATE_FINISHED_SENT_KEY_SWITCHED,
400df4ca
MW
55} server_state_t;
56
4c0c2283
MW
57/**
58 * Private data of an tls_server_t object.
59 */
60struct private_tls_server_t {
61
62 /**
63 * Public tls_server_t interface.
64 */
65 tls_server_t public;
536dbc00 66
3e962b08
MW
67 /**
68 * TLS stack
69 */
70 tls_t *tls;
71
536dbc00
MW
72 /**
73 * TLS crypto context
74 */
75 tls_crypto_t *crypto;
3ddd164e 76
e6f3ef13
MW
77 /**
78 * TLS alert handler
79 */
80 tls_alert_t *alert;
81
3ddd164e
MW
82 /**
83 * Server identity
84 */
85 identification_t *server;
86
87 /**
69e8bb2e 88 * Peer identity, NULL for no client authentication
3ddd164e
MW
89 */
90 identification_t *peer;
400df4ca
MW
91
92 /**
93 * State we are in
94 */
95 server_state_t state;
96
97 /**
98 * Hello random data selected by client
99 */
100 char client_random[32];
101
102 /**
103 * Hello random data selected by server
104 */
105 char server_random[32];
106
107 /**
108 * Auth helper for peer authentication
109 */
110 auth_cfg_t *peer_auth;
111
112 /**
113 * Auth helper for server authentication
114 */
115 auth_cfg_t *server_auth;
116
117 /**
118 * Peer private key
119 */
120 private_key_t *private;
121
f14358a9
MW
122 /**
123 * DHE exchange
124 */
125 diffie_hellman_t *dh;
126
7fbe2e27
PK
127 /**
128 * Requested DH group
129 */
130 tls_named_group_t requested_curve;
131
400df4ca
MW
132 /**
133 * Selected TLS cipher suite
134 */
135 tls_cipher_suite_t suite;
60c4b3b5
MW
136
137 /**
138 * Offered TLS version of the client
139 */
140 tls_version_t client_version;
06109c47 141
6a5c86b7
MW
142 /**
143 * TLS session identifier
144 */
145 chunk_t session;
146
147 /**
148 * Do we resume a session?
149 */
150 bool resume;
151
06109c47
MW
152 /**
153 * Hash and signature algorithms supported by peer
154 */
155 chunk_t hashsig;
37a59a8f
MW
156
157 /**
158 * Elliptic curves supported by peer
159 */
160 chunk_t curves;
161
162 /**
163 * Did we receive the curves from the client?
164 */
165 bool curves_received;
a4a128bd
SL
166
167 /**
168 * Whether to include CAs in CertificateRequest messages
169 */
170 bool send_certreq_authorities;
4c0c2283
MW
171};
172
4635f348
PK
173/**
174 * Find a trusted public key to encrypt/verify key exchange data
175 */
e4b4aabc 176public_key_t *tls_find_public_key(auth_cfg_t *peer_auth, identification_t *id)
4635f348
PK
177{
178 public_key_t *public = NULL, *current;
179 certificate_t *cert, *found;
180 enumerator_t *enumerator;
181 auth_cfg_t *auth;
182
183 cert = peer_auth->get(peer_auth, AUTH_HELPER_SUBJECT_CERT);
184 if (cert)
185 {
186 enumerator = lib->credmgr->create_public_enumerator(lib->credmgr,
e4b4aabc 187 KEY_ANY, id, peer_auth, TRUE);
4635f348
PK
188 while (enumerator->enumerate(enumerator, &current, &auth))
189 {
190 found = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
191 if (found && cert->equals(cert, found))
192 {
193 public = current->get_ref(current);
194 peer_auth->merge(peer_auth, auth, FALSE);
195 break;
196 }
197 }
198 enumerator->destroy(enumerator);
199 }
200 return public;
201}
202
4cdade5a
MW
203/**
204 * Find a cipher suite and a server key
205 */
206static bool select_suite_and_key(private_tls_server_t *this,
207 tls_cipher_suite_t *suites, int count)
208{
9803fb82 209 tls_version_t version_min, version_max;
4cdade5a 210 private_key_t *key;
d8e42a3d
PK
211 auth_cfg_t *auth;
212 enumerator_t *enumerator;
4cdade5a 213
9803fb82
PK
214 version_min = this->tls->get_version_min(this->tls);
215 version_max = this->tls->get_version_max(this->tls);
d8e42a3d
PK
216 enumerator = tls_create_private_key_enumerator(version_min, version_max,
217 this->hashsig, this->server);
218 if (!enumerator)
9803fb82
PK
219 {
220 DBG1(DBG_TLS, "no common signature algorithms found");
9803fb82
PK
221 return FALSE;
222 }
d8e42a3d 223 if (!enumerator->enumerate(enumerator, &key, &auth))
4cdade5a
MW
224 {
225 DBG1(DBG_TLS, "no usable TLS server certificate found for '%Y'",
226 this->server);
d8e42a3d 227 enumerator->destroy(enumerator);
4cdade5a
MW
228 return FALSE;
229 }
4cdade5a 230
9803fb82 231 if (version_max >= TLS_1_3)
7fbe2e27 232 {
7fbe2e27
PK
233 this->suite = this->crypto->select_cipher_suite(this->crypto, suites,
234 count, KEY_ANY);
7fbe2e27
PK
235 }
236 else
237 {
238 this->suite = this->crypto->select_cipher_suite(this->crypto, suites,
d8e42a3d
PK
239 count, key->get_type(key));
240 while (!this->suite &&
241 enumerator->enumerate(enumerator, &key, &auth))
9803fb82 242 { /* find a key and cipher suite for one of the remaining key types */
d8e42a3d
PK
243 this->suite = this->crypto->select_cipher_suite(this->crypto,
244 suites, count,
245 key->get_type(key));
4cdade5a
MW
246 }
247 }
d8e42a3d 248 if (!this->suite)
9803fb82
PK
249 {
250 DBG1(DBG_TLS, "received cipher suites or signature schemes unacceptable");
d8e42a3d 251 enumerator->destroy(enumerator);
9803fb82
PK
252 return FALSE;
253 }
254 DBG1(DBG_TLS, "using key of type %N", key_type_names, key->get_type(key));
ab226b39 255 DESTROY_IF(this->private);
d8e42a3d 256 this->private = key->get_ref(key);
ab226b39 257 this->server_auth->purge(this->server_auth, FALSE);
d8e42a3d
PK
258 this->server_auth->merge(this->server_auth, auth, FALSE);
259 enumerator->destroy(enumerator);
4cdade5a
MW
260 return TRUE;
261}
262
7fbe2e27
PK
263/**
264 * Check if the peer supports a given TLS curve
265 */
266static bool peer_supports_curve(private_tls_server_t *this,
267 tls_named_group_t curve)
268{
269 bio_reader_t *reader;
270 uint16_t current;
271
272 if (!this->curves_received)
273 { /* none received, assume yes */
274 return TRUE;
275 }
276 reader = bio_reader_create(this->curves);
277 while (reader->remaining(reader) && reader->read_uint16(reader, &current))
278 {
279 if (current == curve)
280 {
281 reader->destroy(reader);
282 return TRUE;
283 }
284 }
285 reader->destroy(reader);
286 return FALSE;
287}
288
3767a4a6
PK
289/**
290 * TLS 1.3 key exchange key share
291 */
292typedef struct {
293 uint16_t curve;
294 chunk_t key_share;
295} key_share_t;
296
297/**
298 * Check if peer sent a key share of a given TLS named DH group
299 */
300static bool peer_offered_curve(array_t *key_shares, tls_named_group_t curve,
301 key_share_t *out)
302{
303 key_share_t peer;
304 int i;
305
306 for (i = 0; i < array_count(key_shares); i++)
307 {
308 array_get(key_shares, i, &peer);
309 if (curve == peer.curve)
310 {
311 if (out)
312 {
313 *out = peer;
314 }
315 return TRUE;
316 }
317 }
318 return FALSE;
319}
320
c3e71324
PK
321/**
322 * Check if client is currently retrying to connect to the server.
323 */
324static bool retrying(private_tls_server_t *this)
325{
326 return this->state == STATE_INIT && this->requested_curve;
327}
328
400df4ca
MW
329/**
330 * Process client hello message
331 */
332static status_t process_client_hello(private_tls_server_t *this,
7e432eff 333 bio_reader_t *reader)
400df4ca 334{
3767a4a6 335 uint16_t legacy_version = 0, version = 0, extension_type = 0;
7fbe2e27 336 chunk_t random, session, ciphers, versions = chunk_empty, compression;
3767a4a6
PK
337 chunk_t ext = chunk_empty, key_shares = chunk_empty;
338 key_share_t peer = {0};
7fbe2e27
PK
339 chunk_t extension_data = chunk_empty;
340 bio_reader_t *extensions, *extension;
400df4ca 341 tls_cipher_suite_t *suites;
dc9f6c68 342 tls_version_t original_version_max;
400df4ca 343 int count, i;
6a5c86b7 344 rng_t *rng;
400df4ca
MW
345
346 this->crypto->append_handshake(this->crypto,
347 TLS_CLIENT_HELLO, reader->peek(reader));
348
7fbe2e27 349 if (!reader->read_uint16(reader, &legacy_version) ||
400df4ca
MW
350 !reader->read_data(reader, sizeof(this->client_random), &random) ||
351 !reader->read_data8(reader, &session) ||
352 !reader->read_data16(reader, &ciphers) ||
353 !reader->read_data8(reader, &compression) ||
354 (reader->remaining(reader) && !reader->read_data16(reader, &ext)))
355 {
3c19b346 356 DBG1(DBG_TLS, "received invalid ClientHello");
e6f3ef13
MW
357 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
358 return NEED_MORE;
400df4ca
MW
359 }
360
06424efa
TB
361 /* before we do anything version-related, determine our supported suites
362 * as that might change the min./max. versions */
363 this->crypto->get_cipher_suites(this->crypto, NULL);
364
7fbe2e27
PK
365 extensions = bio_reader_create(ext);
366 while (extensions->remaining(extensions))
06109c47 367 {
7fbe2e27
PK
368 if (!extensions->read_uint16(extensions, &extension_type) ||
369 !extensions->read_data16(extensions, &extension_data))
06109c47 370 {
7fbe2e27
PK
371 DBG1(DBG_TLS, "received invalid ClientHello Extensions");
372 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
373 extensions->destroy(extensions);
374 return NEED_MORE;
06109c47 375 }
7fbe2e27
PK
376 extension = bio_reader_create(extension_data);
377 DBG2(DBG_TLS, "received TLS '%N' extension",
378 tls_extension_names, extension_type);
379 DBG3(DBG_TLS, "%B", &extension_data);
380 switch (extension_type)
381 {
382 case TLS_EXT_SIGNATURE_ALGORITHMS:
0dfe1590
PK
383 if (!extension->read_data16(extension, &extension_data))
384 {
385 DBG1(DBG_TLS, "invalid %N extension",
386 tls_extension_names, extension_type);
387 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
388 extensions->destroy(extensions);
389 extension->destroy(extension);
390 return NEED_MORE;
391 }
c3e71324 392 chunk_free(&this->hashsig);
7fbe2e27
PK
393 this->hashsig = chunk_clone(extension_data);
394 break;
395 case TLS_EXT_SUPPORTED_GROUPS:
0dfe1590
PK
396 if (!extension->read_data16(extension, &extension_data))
397 {
398 DBG1(DBG_TLS, "invalid %N extension",
399 tls_extension_names, extension_type);
400 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
401 extensions->destroy(extensions);
402 extension->destroy(extension);
403 return NEED_MORE;
404 }
c3e71324 405 chunk_free(&this->curves);
7fbe2e27
PK
406 this->curves_received = TRUE;
407 this->curves = chunk_clone(extension_data);
408 break;
409 case TLS_EXT_SUPPORTED_VERSIONS:
410 if (!extension->read_data8(extension, &versions))
411 {
412 DBG1(DBG_TLS, "invalid %N extension",
413 tls_extension_names, extension_type);
0dfe1590 414 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
7fbe2e27
PK
415 extensions->destroy(extensions);
416 extension->destroy(extension);
417 return NEED_MORE;
418 }
419 break;
420 case TLS_EXT_KEY_SHARE:
3767a4a6 421 if (!extension->read_data16(extension, &key_shares))
7fbe2e27
PK
422 {
423 DBG1(DBG_TLS, "invalid %N extension",
424 tls_extension_names, extension_type);
5c4cb40e 425 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
7fbe2e27
PK
426 extensions->destroy(extensions);
427 extension->destroy(extension);
428 return NEED_MORE;
429 }
430 break;
431 default:
432 break;
433 }
434 extension->destroy(extension);
06109c47 435 }
7fbe2e27 436 extensions->destroy(extensions);
06109c47 437
e7313962
PK
438 if (this->tls->get_version_max(this->tls) >= TLS_1_3 && !this->hashsig.len)
439 {
440 DBG1(DBG_TLS, "no %N extension received", tls_extension_names,
441 TLS_MISSING_EXTENSION);
442 this->alert->add(this->alert, TLS_FATAL, TLS_MISSING_EXTENSION);
443 return NEED_MORE;
444 }
445
400df4ca
MW
446 memcpy(this->client_random, random.ptr, sizeof(this->client_random));
447
6a5c86b7
MW
448 htoun32(&this->server_random, time(NULL));
449 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
126eb2af
TB
450 if (!rng ||
451 !rng->get_bytes(rng, sizeof(this->server_random) - 4,
452 this->server_random + 4))
6a5c86b7 453 {
126eb2af 454 DBG1(DBG_TLS, "failed to generate server random");
6a5c86b7 455 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
126eb2af 456 DESTROY_IF(rng);
6a5c86b7
MW
457 return NEED_MORE;
458 }
6a5c86b7
MW
459 rng->destroy(rng);
460
dc9f6c68
PK
461 original_version_max = this->tls->get_version_max(this->tls);
462
7fbe2e27
PK
463 if (versions.len)
464 {
465 bio_reader_t *client_versions;
466
467 client_versions = bio_reader_create(versions);
468 while (client_versions->remaining(client_versions))
469 {
470 if (client_versions->read_uint16(client_versions, &version))
471 {
472 if (this->tls->set_version(this->tls, version, version))
473 {
474 this->client_version = version;
475 break;
476 }
477 }
478 }
479 client_versions->destroy(client_versions);
480 }
481 else
482 {
483 version = legacy_version;
484 if (this->tls->set_version(this->tls, version, version))
485 {
486 this->client_version = version;
487 }
488 }
dc9f6c68
PK
489
490 /* downgrade protection (see RFC 8446, section 4.1.3) */
491 if ((original_version_max == TLS_1_3 && version < TLS_1_3) ||
492 (original_version_max == TLS_1_2 && version < TLS_1_2))
493 {
494 chunk_t downgrade_protection = tls_downgrade_protection_tls11;
495
496 if (version == TLS_1_2)
497 {
498 downgrade_protection = tls_downgrade_protection_tls12;
499 }
500 memcpy(&this->server_random[24], downgrade_protection.ptr,
501 downgrade_protection.len);
502 }
503
7fbe2e27 504 if (!this->client_version)
400df4ca 505 {
7fbe2e27
PK
506 DBG1(DBG_TLS, "proposed version %N not supported", tls_version_names,
507 version);
e6f3ef13
MW
508 this->alert->add(this->alert, TLS_FATAL, TLS_PROTOCOL_VERSION);
509 return NEED_MORE;
400df4ca 510 }
6a5c86b7 511
7fbe2e27
PK
512 if (this->tls->get_version_max(this->tls) < TLS_1_3)
513 {
514 this->suite = this->crypto->resume_session(this->crypto, session,
515 this->peer,
516 chunk_from_thing(this->client_random),
517 chunk_from_thing(this->server_random));
518 }
519
ab226b39 520 if (this->suite && !retrying(this))
400df4ca 521 {
6a5c86b7
MW
522 this->session = chunk_clone(session);
523 this->resume = TRUE;
524 DBG1(DBG_TLS, "resumed %N using suite %N",
7a2b0266 525 tls_version_names, this->tls->get_version_max(this->tls),
6a5c86b7 526 tls_cipher_suite_names, this->suite);
400df4ca 527 }
6a5c86b7 528 else
400df4ca 529 {
ab226b39
TB
530 tls_cipher_suite_t original_suite = this->suite;
531
b12c53ce 532 count = ciphers.len / sizeof(uint16_t);
6a5c86b7
MW
533 suites = alloca(count * sizeof(tls_cipher_suite_t));
534 DBG2(DBG_TLS, "received %d TLS cipher suites:", count);
535 for (i = 0; i < count; i++)
536 {
b12c53ce 537 suites[i] = untoh16(&ciphers.ptr[i * sizeof(uint16_t)]);
6a5c86b7
MW
538 DBG2(DBG_TLS, " %N", tls_cipher_suite_names, suites[i]);
539 }
540 if (!select_suite_and_key(this, suites, count))
541 {
542 this->alert->add(this->alert, TLS_FATAL, TLS_HANDSHAKE_FAILURE);
543 return NEED_MORE;
544 }
ab226b39
TB
545 if (retrying(this) && original_suite != this->suite)
546 {
547 DBG1(DBG_TLS, "selected %N instead of %N during retry",
548 tls_cipher_suite_names, this->suite, tls_cipher_suite_names,
549 original_suite);
550 this->alert->add(this->alert, TLS_FATAL, TLS_ILLEGAL_PARAMETER);
551 return NEED_MORE;
552 }
7fbe2e27 553 if (this->tls->get_version_max(this->tls) < TLS_1_3)
6a5c86b7 554 {
7fbe2e27
PK
555 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
556 if (!rng ||
557 !rng->allocate_bytes(rng, SESSION_ID_SIZE, &this->session))
558 {
559 DBG1(DBG_TLS, "generating TLS session identifier failed, skipped");
560 }
561 DESTROY_IF(rng);
562 }
563 else
564 {
ab226b39 565 chunk_free(&this->session);
7fbe2e27 566 this->session = chunk_clone(session);
6a5c86b7
MW
567 }
568 DBG1(DBG_TLS, "negotiated %N using suite %N",
7a2b0266 569 tls_version_names, this->tls->get_version_max(this->tls),
6a5c86b7 570 tls_cipher_suite_names, this->suite);
400df4ca 571 }
7fbe2e27
PK
572
573 if (this->tls->get_version_max(this->tls) >= TLS_1_3)
574 {
575 diffie_hellman_group_t group;
c3e71324 576 tls_named_group_t curve, requesting_curve = 0;
7fbe2e27 577 enumerator_t *enumerator;
3767a4a6
PK
578 array_t *peer_key_shares;
579
580 peer_key_shares = array_create(sizeof(key_share_t), 1);
581 extension = bio_reader_create(key_shares);
582 while (extension->remaining(extension))
583 {
584 if (!extension->read_uint16(extension, &peer.curve) ||
585 !extension->read_data16(extension, &peer.key_share) ||
586 !peer.key_share.len)
587 {
588 DBG1(DBG_TLS, "invalid %N extension",
589 tls_extension_names, extension_type);
590 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
591 extension->destroy(extension);
592 array_destroy(peer_key_shares);
593 return NEED_MORE;
594 }
595 array_insert(peer_key_shares, ARRAY_TAIL, &peer);
596 }
597 extension->destroy(extension);
7fbe2e27
PK
598
599 enumerator = this->crypto->create_ec_enumerator(this->crypto);
600 while (enumerator->enumerate(enumerator, &group, &curve))
601 {
c3e71324 602 if (!requesting_curve &&
3767a4a6
PK
603 peer_supports_curve(this, curve) &&
604 !peer_offered_curve(peer_key_shares, curve, NULL))
c3e71324
PK
605 {
606 requesting_curve = curve;
607 }
3767a4a6
PK
608 if (peer_supports_curve(this, curve) &&
609 peer_offered_curve(peer_key_shares, curve, &peer))
7fbe2e27 610 {
c3e71324
PK
611 DBG1(DBG_TLS, "using key exchange %N",
612 tls_named_group_names, curve);
7fbe2e27
PK
613 this->dh = lib->crypto->create_dh(lib->crypto, group);
614 break;
615 }
616 }
617 enumerator->destroy(enumerator);
3767a4a6 618 array_destroy(peer_key_shares);
c3e71324 619
7fbe2e27 620 if (!this->dh)
c3e71324
PK
621 {
622 if (retrying(this))
623 {
624 DBG1(DBG_TLS, "already replied with a hello retry request");
625 this->alert->add(this->alert, TLS_FATAL, TLS_UNEXPECTED_MESSAGE);
626 return NEED_MORE;
627 }
628
c3e71324
PK
629 if (!requesting_curve)
630 {
631 DBG1(DBG_TLS, "no mutual supported group in client hello");
632 this->alert->add(this->alert, TLS_FATAL, TLS_ILLEGAL_PARAMETER);
633 return NEED_MORE;
634 }
635 this->requested_curve = requesting_curve;
7fbe2e27 636
c3e71324 637 if (!this->crypto->hash_handshake(this->crypto, NULL))
7fbe2e27 638 {
c3e71324 639 DBG1(DBG_TLS, "failed to hash handshake messages");
7fbe2e27
PK
640 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
641 return NEED_MORE;
642 }
7fbe2e27 643 }
c3e71324 644 else
7fbe2e27 645 {
3767a4a6
PK
646 if (peer.key_share.len &&
647 peer.curve != TLS_CURVE25519 &&
648 peer.curve != TLS_CURVE448)
c3e71324 649 { /* classic format (see RFC 8446, section 4.2.8.2) */
3767a4a6 650 if (peer.key_share.ptr[0] != TLS_ANSI_UNCOMPRESSED)
c3e71324
PK
651 {
652 DBG1(DBG_TLS, "DH point format '%N' not supported",
3767a4a6 653 tls_ansi_point_format_names, peer.key_share.ptr[0]);
c3e71324
PK
654 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
655 return NEED_MORE;
656 }
3767a4a6 657 peer.key_share = chunk_skip(peer.key_share, 1);
c3e71324 658 }
3767a4a6
PK
659 if (!peer.key_share.len ||
660 !this->dh->set_other_public_value(this->dh, peer.key_share))
c3e71324
PK
661 {
662 DBG1(DBG_TLS, "DH key derivation failed");
663 this->alert->add(this->alert, TLS_FATAL, TLS_HANDSHAKE_FAILURE);
c3e71324
PK
664 return NEED_MORE;
665 }
c3e71324 666 this->requested_curve = 0;
7fbe2e27 667 }
7fbe2e27
PK
668 }
669
400df4ca
MW
670 this->state = STATE_HELLO_RECEIVED;
671 return NEED_MORE;
672}
673
674/**
675 * Process certificate
676 */
677static status_t process_certificate(private_tls_server_t *this,
7e432eff 678 bio_reader_t *reader)
400df4ca
MW
679{
680 certificate_t *cert;
7e432eff 681 bio_reader_t *certs;
400df4ca
MW
682 chunk_t data;
683 bool first = TRUE;
684
685 this->crypto->append_handshake(this->crypto,
686 TLS_CERTIFICATE, reader->peek(reader));
687
d2fc9b09
PK
688 if (this->tls->get_version_max(this->tls) > TLS_1_2)
689 {
690 if (!reader->read_data8(reader, &data))
691 {
692 DBG1(DBG_TLS, "certificate request context invalid");
693 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
694 return NEED_MORE;
695 }
696 }
697
400df4ca
MW
698 if (!reader->read_data24(reader, &data))
699 {
e6f3ef13
MW
700 DBG1(DBG_TLS, "certificate message header invalid");
701 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
702 return NEED_MORE;
400df4ca 703 }
7e432eff 704 certs = bio_reader_create(data);
dc49d457
PK
705 if (!certs->remaining(certs))
706 {
760f3b73
TB
707 if (this->tls->get_flags(this->tls) & TLS_FLAG_CLIENT_AUTH_OPTIONAL)
708 {
709 /* client authentication is not required so we clear the identity */
710 DESTROY_IF(this->peer);
711 this->peer = NULL;
712 }
713 else
714 {
715 DBG1(DBG_TLS, "no certificate sent by peer");
716 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
717 return NEED_MORE;
718 }
dc49d457 719 }
400df4ca
MW
720 while (certs->remaining(certs))
721 {
722 if (!certs->read_data24(certs, &data))
723 {
e6f3ef13
MW
724 DBG1(DBG_TLS, "certificate message invalid");
725 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
400df4ca 726 certs->destroy(certs);
e6f3ef13 727 return NEED_MORE;
400df4ca
MW
728 }
729 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
730 BUILD_BLOB_ASN1_DER, data, BUILD_END);
731 if (cert)
732 {
733 if (first)
734 {
735 this->peer_auth->add(this->peer_auth,
736 AUTH_HELPER_SUBJECT_CERT, cert);
3c19b346 737 DBG1(DBG_TLS, "received TLS peer certificate '%Y'",
400df4ca
MW
738 cert->get_subject(cert));
739 first = FALSE;
4b7cfb25
TB
740 if (this->peer && this->peer->get_type(this->peer) == ID_ANY)
741 {
742 this->peer->destroy(this->peer);
743 this->peer = cert->get_subject(cert);
744 this->peer = this->peer->clone(this->peer);
745 }
400df4ca
MW
746 }
747 else
748 {
3c19b346 749 DBG1(DBG_TLS, "received TLS intermediate certificate '%Y'",
400df4ca
MW
750 cert->get_subject(cert));
751 this->peer_auth->add(this->peer_auth, AUTH_HELPER_IM_CERT, cert);
752 }
753 }
754 else
755 {
3c19b346 756 DBG1(DBG_TLS, "parsing TLS certificate failed, skipped");
e6f3ef13 757 this->alert->add(this->alert, TLS_WARNING, TLS_BAD_CERTIFICATE);
400df4ca 758 }
d2fc9b09
PK
759 if (this->tls->get_version_max(this->tls) > TLS_1_2)
760 {
761 if (!certs->read_data16(certs, &data))
762 {
763 DBG1(DBG_TLS, "failed to read extensions of CertificateEntry");
764 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
765 return NEED_MORE;
766 }
767 }
400df4ca
MW
768 }
769 certs->destroy(certs);
770 this->state = STATE_CERT_RECEIVED;
771 return NEED_MORE;
772}
773
774/**
f14358a9 775 * Process Client Key Exchange, using premaster encryption
400df4ca 776 */
f14358a9 777static status_t process_key_exchange_encrypted(private_tls_server_t *this,
7e432eff 778 bio_reader_t *reader)
400df4ca 779{
60c4b3b5
MW
780 chunk_t encrypted, decrypted;
781 char premaster[48];
782 rng_t *rng;
400df4ca
MW
783
784 this->crypto->append_handshake(this->crypto,
785 TLS_CLIENT_KEY_EXCHANGE, reader->peek(reader));
786
787 if (!reader->read_data16(reader, &encrypted))
788 {
3c19b346 789 DBG1(DBG_TLS, "received invalid Client Key Exchange");
e6f3ef13
MW
790 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
791 return NEED_MORE;
400df4ca
MW
792 }
793
60c4b3b5
MW
794 htoun16(premaster, this->client_version);
795 /* pre-randomize premaster for failure cases */
796 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
126eb2af 797 if (!rng || !rng->get_bytes(rng, sizeof(premaster) - 2, premaster + 2))
400df4ca 798 {
126eb2af 799 DBG1(DBG_TLS, "failed to generate premaster secret");
60c4b3b5 800 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
126eb2af 801 DESTROY_IF(rng);
e6f3ef13 802 return NEED_MORE;
400df4ca 803 }
60c4b3b5
MW
804 rng->destroy(rng);
805
806 if (this->private &&
4abb29f6
AS
807 this->private->decrypt(this->private, ENCRYPT_RSA_PKCS1, NULL,
808 encrypted, &decrypted))
60c4b3b5
MW
809 {
810 if (decrypted.len == sizeof(premaster) &&
811 untoh16(decrypted.ptr) == this->client_version)
812 {
813 memcpy(premaster + 2, decrypted.ptr + 2, sizeof(premaster) - 2);
814 }
815 else
816 {
817 DBG1(DBG_TLS, "decrypted premaster has invalid length/version");
818 }
819 chunk_clear(&decrypted);
820 }
821 else
822 {
823 DBG1(DBG_TLS, "decrypting Client Key Exchange failed");
824 }
825
9020f7d0
MW
826 if (!this->crypto->derive_secrets(this->crypto, chunk_from_thing(premaster),
827 this->session, this->peer,
828 chunk_from_thing(this->client_random),
829 chunk_from_thing(this->server_random)))
830 {
831 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
832 return NEED_MORE;
833 }
400df4ca
MW
834
835 this->state = STATE_KEY_EXCHANGE_RECEIVED;
836 return NEED_MORE;
837}
838
f14358a9
MW
839/**
840 * Process client key exchange, using DHE exchange
841 */
842static status_t process_key_exchange_dhe(private_tls_server_t *this,
7e432eff 843 bio_reader_t *reader)
f14358a9
MW
844{
845 chunk_t premaster, pub;
966a26ea 846 diffie_hellman_group_t group;
f4c98ae6 847 bool ec;
f14358a9
MW
848
849 this->crypto->append_handshake(this->crypto,
850 TLS_CLIENT_KEY_EXCHANGE, reader->peek(reader));
851
966a26ea
TB
852 group = this->dh->get_dh_group(this->dh);
853 ec = diffie_hellman_group_is_ec(group);
141d7f7a 854 if ((ec && !reader->read_data8(reader, &pub)) ||
e6cce7ff 855 (!ec && (!reader->read_data16(reader, &pub) || pub.len == 0)))
f14358a9
MW
856 {
857 DBG1(DBG_TLS, "received invalid Client Key Exchange");
858 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
859 return NEED_MORE;
860 }
e6cce7ff 861
966a26ea
TB
862 if (ec &&
863 group != CURVE_25519 &&
864 group != CURVE_448)
e6cce7ff 865 {
7b3c0184
MW
866 if (pub.ptr[0] != TLS_ANSI_UNCOMPRESSED)
867 {
868 DBG1(DBG_TLS, "DH point format '%N' not supported",
869 tls_ansi_point_format_names, pub.ptr[0]);
870 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
871 return NEED_MORE;
872 }
873 pub = chunk_skip(pub, 1);
e6cce7ff 874 }
a777155f
MW
875 if (!this->dh->set_other_public_value(this->dh, pub))
876 {
877 DBG1(DBG_TLS, "applying DH public value failed");
878 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
879 return NEED_MORE;
880 }
bace1d64 881 if (!this->dh->get_shared_secret(this->dh, &premaster))
f14358a9
MW
882 {
883 DBG1(DBG_TLS, "calculating premaster from DH failed");
884 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
885 return NEED_MORE;
886 }
887
9020f7d0
MW
888 if (!this->crypto->derive_secrets(this->crypto, premaster,
889 this->session, this->peer,
890 chunk_from_thing(this->client_random),
891 chunk_from_thing(this->server_random)))
892 {
893 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
894 chunk_clear(&premaster);
895 return NEED_MORE;
896 }
f14358a9
MW
897 chunk_clear(&premaster);
898
899 this->state = STATE_KEY_EXCHANGE_RECEIVED;
900 return NEED_MORE;
901}
902
903/**
904 * Process Client Key Exchange
905 */
906static status_t process_key_exchange(private_tls_server_t *this,
7e432eff 907 bio_reader_t *reader)
f14358a9
MW
908{
909 if (this->dh)
910 {
911 return process_key_exchange_dhe(this, reader);
912 }
913 return process_key_exchange_encrypted(this, reader);
914}
915
400df4ca
MW
916/**
917 * Process Certificate verify
918 */
919static status_t process_cert_verify(private_tls_server_t *this,
7e432eff 920 bio_reader_t *reader)
400df4ca 921{
400df4ca 922 public_key_t *public;
d2fc9b09 923 chunk_t msg;
400df4ca 924
e4b4aabc 925 public = tls_find_public_key(this->peer_auth, this->peer);
d2fc9b09 926 if (!public)
400df4ca 927 {
3c19b346 928 DBG1(DBG_TLS, "no trusted certificate found for '%Y' to verify TLS peer",
400df4ca 929 this->peer);
d2fc9b09
PK
930 this->alert->add(this->alert, TLS_FATAL, TLS_CERTIFICATE_UNKNOWN);
931 return NEED_MORE;
1db6bf2f 932 }
d2fc9b09
PK
933
934 msg = reader->peek(reader);
935 if (!this->crypto->verify_handshake(this->crypto, public, reader))
1db6bf2f 936 {
d2fc9b09
PK
937 public->destroy(public);
938 DBG1(DBG_TLS, "signature verification failed");
939 this->alert->add(this->alert, TLS_FATAL, TLS_DECRYPT_ERROR);
940 return NEED_MORE;
400df4ca 941 }
d2fc9b09
PK
942 public->destroy(public);
943 this->state = STATE_CERT_VERIFY_RECEIVED;
944 this->crypto->append_handshake(this->crypto, TLS_CERTIFICATE_VERIFY, msg);
400df4ca
MW
945 return NEED_MORE;
946}
947
948/**
949 * Process finished message
950 */
951static status_t process_finished(private_tls_server_t *this,
7e432eff 952 bio_reader_t *reader)
400df4ca 953{
7fbe2e27
PK
954 chunk_t received, verify_data;
955 u_char buf[12];
400df4ca 956
7fbe2e27 957 if (this->tls->get_version_max(this->tls) < TLS_1_3)
400df4ca 958 {
7fbe2e27
PK
959 if (!reader->read_data(reader, sizeof(buf), &received))
960 {
961 DBG1(DBG_TLS, "received client finished too short");
962 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
963 return NEED_MORE;
964 }
965 if (!this->crypto->calculate_finished_legacy(this->crypto,
966 "client finished", buf))
967 {
968 DBG1(DBG_TLS, "calculating client finished failed");
969 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
970 return NEED_MORE;
971 }
972 verify_data = chunk_from_thing(buf);
400df4ca 973 }
7fbe2e27 974 else
400df4ca 975 {
7fbe2e27
PK
976 received = reader->peek(reader);
977 if (!this->crypto->calculate_finished(this->crypto, FALSE, &verify_data))
978 {
979 DBG1(DBG_TLS, "calculating client finished failed");
980 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
981 return NEED_MORE;
982 }
7fbe2e27 983 this->crypto->change_cipher(this->crypto, TRUE);
400df4ca 984 }
7fbe2e27
PK
985
986 if (!chunk_equals_const(received, verify_data))
400df4ca 987 {
3c19b346 988 DBG1(DBG_TLS, "received client finished invalid");
e6f3ef13
MW
989 this->alert->add(this->alert, TLS_FATAL, TLS_DECRYPT_ERROR);
990 return NEED_MORE;
400df4ca
MW
991 }
992
7fbe2e27
PK
993 if (verify_data.ptr != buf)
994 {
995 chunk_free(&verify_data);
996 }
997
400df4ca
MW
998 this->crypto->append_handshake(this->crypto, TLS_FINISHED, received);
999 this->state = STATE_FINISHED_RECEIVED;
1000 return NEED_MORE;
1001}
4c0c2283 1002
ab70f68c
PK
1003/**
1004 * Process KeyUpdate message
1005 */
1006static status_t process_key_update(private_tls_server_t *this,
1007 bio_reader_t *reader)
1008{
1009 uint8_t update_requested;
1010
1011 if (!reader->read_uint8(reader, &update_requested) ||
1012 update_requested > 1)
1013 {
1014 DBG1(DBG_TLS, "received invalid KeyUpdate");
1015 this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
1016 return NEED_MORE;
1017 }
1018
1019 if (!this->crypto->update_app_keys(this->crypto, TRUE))
1020 {
1021 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1022 return NEED_MORE;
1023 }
1024 this->crypto->change_cipher(this->crypto, TRUE);
1025
1026 if (update_requested)
1027 {
1028 DBG1(DBG_TLS, "client requested KeyUpdate");
1029 this->state = STATE_KEY_UPDATE_REQUESTED;
1030 }
1031 return NEED_MORE;
1032}
1033
4c0c2283 1034METHOD(tls_handshake_t, process, status_t,
7e432eff 1035 private_tls_server_t *this, tls_handshake_type_t type, bio_reader_t *reader)
4c0c2283 1036{
400df4ca
MW
1037 tls_handshake_type_t expected;
1038
7fbe2e27 1039 if (this->tls->get_version_max(this->tls) < TLS_1_3)
400df4ca 1040 {
7fbe2e27
PK
1041 switch (this->state)
1042 {
1043 case STATE_INIT:
1044 if (type == TLS_CLIENT_HELLO)
1045 {
1046 return process_client_hello(this, reader);
1047 }
1048 expected = TLS_CLIENT_HELLO;
b51ac45c 1049 break;
7fbe2e27
PK
1050 case STATE_HELLO_DONE:
1051 if (type == TLS_CERTIFICATE)
1052 {
1053 return process_certificate(this, reader);
1054 }
1055 if (this->peer)
1056 {
1057 expected = TLS_CERTIFICATE;
1058 break;
1059 }
1060 /* otherwise fall through to next state */
1061 case STATE_CERT_RECEIVED:
1062 if (type == TLS_CLIENT_KEY_EXCHANGE)
1063 {
1064 return process_key_exchange(this, reader);
1065 }
1066 expected = TLS_CLIENT_KEY_EXCHANGE;
b51ac45c 1067 break;
7fbe2e27
PK
1068 case STATE_KEY_EXCHANGE_RECEIVED:
1069 if (type == TLS_CERTIFICATE_VERIFY)
1070 {
1071 return process_cert_verify(this, reader);
1072 }
1073 if (this->peer)
1074 {
1075 expected = TLS_CERTIFICATE_VERIFY;
1076 break;
1077 }
1078 return INVALID_STATE;
1079 case STATE_CIPHERSPEC_CHANGED_IN:
1080 if (type == TLS_FINISHED)
1081 {
1082 return process_finished(this, reader);
1083 }
1084 expected = TLS_FINISHED;
1085 break;
1086 default:
1087 DBG1(DBG_TLS, "TLS %N not expected in current state",
1088 tls_handshake_type_names, type);
1089 this->alert->add(this->alert, TLS_FATAL, TLS_UNEXPECTED_MESSAGE);
1090 return NEED_MORE;
1091 }
1092 }
1093 else
1094 {
1095 switch (this->state)
1096 {
1097 case STATE_INIT:
1098 if (type == TLS_CLIENT_HELLO)
1099 {
1100 return process_client_hello(this, reader);
1101 }
1102 expected = TLS_CLIENT_HELLO;
1103 break;
1104 case STATE_CIPHERSPEC_CHANGED_IN:
1105 case STATE_FINISHED_SENT:
d2fc9b09
PK
1106 case STATE_FINISHED_SENT_KEY_SWITCHED:
1107 if (type == TLS_CERTIFICATE)
1108 {
1109 return process_certificate(this, reader);
1110 }
1111 if (this->peer)
1112 {
1113 expected = TLS_CERTIFICATE;
1114 break;
1115 }
1116 /* otherwise fall through to next state */
1117 case STATE_CERT_RECEIVED:
1118 if (type == TLS_CERTIFICATE_VERIFY)
1119 {
1120 return process_cert_verify(this, reader);
1121 }
1122 if (this->peer)
1123 {
1124 expected = TLS_CERTIFICATE_VERIFY;
1125 break;
1126 }
1127 /* otherwise fall through to next state */
1128 case STATE_CERT_VERIFY_RECEIVED:
7fbe2e27
PK
1129 if (type == TLS_FINISHED)
1130 {
1131 return process_finished(this, reader);
1132 }
1133 return NEED_MORE;
1134 case STATE_FINISHED_RECEIVED:
ab70f68c
PK
1135 if (type == TLS_KEY_UPDATE)
1136 {
1137 return process_key_update(this, reader);
1138 }
7fbe2e27
PK
1139 return INVALID_STATE;
1140 default:
1141 DBG1(DBG_TLS, "TLS %N not expected in current state",
1142 tls_handshake_type_names, type);
1143 this->alert->add(this->alert, TLS_FATAL, TLS_UNEXPECTED_MESSAGE);
1144 return NEED_MORE;
1145 }
400df4ca 1146 }
3c19b346 1147 DBG1(DBG_TLS, "TLS %N expected, but received %N",
400df4ca 1148 tls_handshake_type_names, expected, tls_handshake_type_names, type);
e6f3ef13
MW
1149 this->alert->add(this->alert, TLS_FATAL, TLS_UNEXPECTED_MESSAGE);
1150 return NEED_MORE;
400df4ca
MW
1151}
1152
5c4cb40e
PK
1153/**
1154 * Write public key into key share extension
1155 */
066ac880 1156bool tls_write_key_share(bio_writer_t **key_share, diffie_hellman_t *dh)
5c4cb40e
PK
1157{
1158 bio_writer_t *writer;
066ac880 1159 tls_named_group_t curve;
5c4cb40e
PK
1160 chunk_t pub;
1161
066ac880
PK
1162 if (!dh)
1163 {
1164 return FALSE;
1165 }
1166 curve = tls_ec_group_to_curve(dh->get_dh_group(dh));
1167 if (!curve || !dh->get_my_public_value(dh, &pub))
5c4cb40e
PK
1168 {
1169 return FALSE;
1170 }
1171 *key_share = writer = bio_writer_create(pub.len + 7);
066ac880
PK
1172 writer->write_uint16(writer, curve);
1173 if (curve == TLS_CURVE25519 ||
1174 curve == TLS_CURVE448)
5c4cb40e
PK
1175 {
1176 writer->write_data16(writer, pub);
1177 }
1178 else
1179 { /* classic format (see RFC 8446, section 4.2.8.2) */
1180 writer->write_uint16(writer, pub.len + 1);
1181 writer->write_uint8(writer, TLS_ANSI_UNCOMPRESSED);
1182 writer->write_data(writer, pub);
1183 }
1184 free(pub.ptr);
1185 return TRUE;
1186}
1187
400df4ca
MW
1188/**
1189 * Send ServerHello message
1190 */
1191static status_t send_server_hello(private_tls_server_t *this,
7e432eff 1192 tls_handshake_type_t *type, bio_writer_t *writer)
400df4ca 1193{
5c4cb40e
PK
1194 bio_writer_t *key_share, *extensions;
1195 tls_version_t version;
1196
1197 version = this->tls->get_version_max(this->tls);
7fbe2e27
PK
1198
1199 /* cap legacy version at TLS 1.2 for middlebox compatibility */
1200 writer->write_uint16(writer, min(TLS_1_2, version));
c3e71324
PK
1201
1202 if (this->requested_curve)
1203 {
1204 writer->write_data(writer, tls_hello_retry_request_magic);
1205 }
1206 else
1207 {
1208 writer->write_data(writer, chunk_from_thing(this->server_random));
1209 }
7ea87db0 1210
6a5c86b7
MW
1211 /* session identifier if we have one */
1212 writer->write_data8(writer, this->session);
7ea87db0
AS
1213
1214 /* add selected TLS cipher suite */
400df4ca 1215 writer->write_uint16(writer, this->suite);
7ea87db0 1216
400df4ca
MW
1217 /* NULL compression only */
1218 writer->write_uint8(writer, 0);
1219
7fbe2e27
PK
1220 if (version >= TLS_1_3)
1221 {
1222 extensions = bio_writer_create(32);
1223
1224 DBG2(DBG_TLS, "sending extension: %N",
1225 tls_extension_names, TLS_EXT_SUPPORTED_VERSIONS);
1226 extensions->write_uint16(extensions, TLS_EXT_SUPPORTED_VERSIONS);
1227 extensions->write_uint16(extensions, 2);
1228 extensions->write_uint16(extensions, version);
1229
5c4cb40e
PK
1230 DBG2(DBG_TLS, "sending extension: %N",
1231 tls_extension_names, TLS_EXT_KEY_SHARE);
1232 extensions->write_uint16(extensions, TLS_EXT_KEY_SHARE);
c3e71324 1233 if (this->requested_curve)
5c4cb40e 1234 {
c3e71324
PK
1235 DBG1(DBG_TLS, "requesting key exchange with %N",
1236 tls_named_group_names, this->requested_curve);
1237 extensions->write_uint16(extensions, 2);
1238 extensions->write_uint16(extensions, this->requested_curve);
1239 }
1240 else
1241 {
1242 if (!tls_write_key_share(&key_share, this->dh))
1243 {
1244 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1245 extensions->destroy(extensions);
1246 return NEED_MORE;
1247 }
1248 extensions->write_data16(extensions, key_share->get_buf(key_share));
1249 key_share->destroy(key_share);
7fbe2e27
PK
1250 }
1251
1252 writer->write_data16(writer, extensions->get_buf(extensions));
1253 extensions->destroy(extensions);
1254 }
1255
400df4ca 1256 *type = TLS_SERVER_HELLO;
c3e71324 1257 this->state = this->requested_curve ? STATE_INIT : STATE_HELLO_SENT;
400df4ca
MW
1258 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1259 return NEED_MORE;
1260}
1261
7fbe2e27
PK
1262/**
1263 * Send encrypted extensions message
1264 */
1265static status_t send_encrypted_extensions(private_tls_server_t *this,
1266 tls_handshake_type_t *type,
1267 bio_writer_t *writer)
1268{
1269 chunk_t shared_secret = chunk_empty;
1270
1271 if (!this->dh->get_shared_secret(this->dh, &shared_secret) ||
1272 !this->crypto->derive_handshake_keys(this->crypto, shared_secret))
1273 {
1274 DBG1(DBG_TLS, "DH key derivation failed");
1275 this->alert->add(this->alert, TLS_FATAL, TLS_HANDSHAKE_FAILURE);
1276 chunk_clear(&shared_secret);
1277 return NEED_MORE;
1278 }
1279 chunk_clear(&shared_secret);
1280
1281 this->crypto->change_cipher(this->crypto, TRUE);
1282 this->crypto->change_cipher(this->crypto, FALSE);
1283
1284 /* currently no extensions are supported */
1285 writer->write_uint16(writer, 0);
1286
1287 *type = TLS_ENCRYPTED_EXTENSIONS;
1288 this->state = STATE_ENCRYPTED_EXTENSIONS_SENT;
1289 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1290 return NEED_MORE;
1291}
1292
400df4ca
MW
1293/**
1294 * Send Certificate
1295 */
1296static status_t send_certificate(private_tls_server_t *this,
7e432eff 1297 tls_handshake_type_t *type, bio_writer_t *writer)
400df4ca
MW
1298{
1299 enumerator_t *enumerator;
1300 certificate_t *cert;
1301 auth_rule_t rule;
7e432eff 1302 bio_writer_t *certs;
400df4ca
MW
1303 chunk_t data;
1304
7fbe2e27
PK
1305 /* certificate request context as described in RFC 8446, section 4.4.2 */
1306 if (this->tls->get_version_max(this->tls) > TLS_1_2)
1307 {
1308 writer->write_uint8(writer, 0);
1309 }
1310
400df4ca 1311 /* generate certificate payload */
7e432eff 1312 certs = bio_writer_create(256);
400df4ca
MW
1313 cert = this->server_auth->get(this->server_auth, AUTH_RULE_SUBJECT_CERT);
1314 if (cert)
1315 {
0b71bc7a
MW
1316 if (cert->get_encoding(cert, CERT_ASN1_DER, &data))
1317 {
3c19b346 1318 DBG1(DBG_TLS, "sending TLS server certificate '%Y'",
0b71bc7a
MW
1319 cert->get_subject(cert));
1320 certs->write_data24(certs, data);
1321 free(data.ptr);
1322 }
7fbe2e27
PK
1323 /* extensions see RFC 8446, section 4.4.2 */
1324 if (this->tls->get_version_max(this->tls) > TLS_1_2)
1325 {
1326 certs->write_uint16(certs, 0);
1327 }
400df4ca
MW
1328 }
1329 enumerator = this->server_auth->create_enumerator(this->server_auth);
1330 while (enumerator->enumerate(enumerator, &rule, &cert))
1331 {
1332 if (rule == AUTH_RULE_IM_CERT)
1333 {
0b71bc7a
MW
1334 if (cert->get_encoding(cert, CERT_ASN1_DER, &data))
1335 {
3c19b346 1336 DBG1(DBG_TLS, "sending TLS intermediate certificate '%Y'",
0b71bc7a
MW
1337 cert->get_subject(cert));
1338 certs->write_data24(certs, data);
1339 free(data.ptr);
1340 }
400df4ca
MW
1341 }
1342 }
1343 enumerator->destroy(enumerator);
1344
1345 writer->write_data24(writer, certs->get_buf(certs));
1346 certs->destroy(certs);
1347
1348 *type = TLS_CERTIFICATE;
1349 this->state = STATE_CERT_SENT;
1350 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1351 return NEED_MORE;
1352}
1353
7fbe2e27
PK
1354/**
1355 * Send Certificate Verify
1356 */
1357static status_t send_certificate_verify(private_tls_server_t *this,
1358 tls_handshake_type_t *type,
1359 bio_writer_t *writer)
1360{
1361 if (!this->crypto->sign_handshake(this->crypto, this->private, writer,
1362 this->hashsig))
1363 {
1364 DBG1(DBG_TLS, "signature generation failed");
1365 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1366 return NEED_MORE;
1367 }
1368
1369 *type = TLS_CERTIFICATE_VERIFY;
1370 this->state = STATE_CERT_VERIFY_SENT;
1371 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1372 return NEED_MORE;
1373}
1374
d2fc9b09
PK
1375/*
1376 * Write all available certificate authorities to output writer
dbb7c030 1377 */
d2fc9b09 1378static void write_certificate_authorities(bio_writer_t *writer)
dbb7c030 1379{
d2fc9b09 1380 bio_writer_t *authorities;
d29a82a9
MW
1381 enumerator_t *enumerator;
1382 certificate_t *cert;
1383 x509_t *x509;
1384 identification_t *id;
dbb7c030 1385
7e432eff 1386 authorities = bio_writer_create(64);
d2fc9b09
PK
1387 enumerator = lib->credmgr->create_cert_enumerator(lib->credmgr, CERT_X509,
1388 KEY_RSA, NULL, TRUE);
400df4ca
MW
1389 while (enumerator->enumerate(enumerator, &cert))
1390 {
714d0bfd
MW
1391 x509 = (x509_t*)cert;
1392 if (x509->get_flags(x509) & X509_CA)
1393 {
1394 id = cert->get_subject(cert);
3c19b346 1395 DBG1(DBG_TLS, "sending TLS cert request for '%Y'", id);
714d0bfd
MW
1396 authorities->write_data16(authorities, id->get_encoding(id));
1397 }
400df4ca
MW
1398 }
1399 enumerator->destroy(enumerator);
1400 writer->write_data16(writer, authorities->get_buf(authorities));
1401 authorities->destroy(authorities);
d2fc9b09
PK
1402}
1403
1404/**
1405 * Send Certificate Request
1406 */
1407static status_t send_certificate_request(private_tls_server_t *this,
1408 tls_handshake_type_t *type,
1409 bio_writer_t *writer)
1410{
1411 bio_writer_t *authorities, *supported, *extensions;
1412
1413 if (this->tls->get_version_max(this->tls) < TLS_1_3)
1414 {
1415 supported = bio_writer_create(4);
1416 /* we propose both RSA and ECDSA */
1417 supported->write_uint8(supported, TLS_RSA_SIGN);
1418 supported->write_uint8(supported, TLS_ECDSA_SIGN);
1419 writer->write_data8(writer, supported->get_buf(supported));
1420 supported->destroy(supported);
1421 if (this->tls->get_version_max(this->tls) >= TLS_1_2)
1422 {
1423 this->crypto->get_signature_algorithms(this->crypto, writer, TRUE);
1424 }
1425
a4a128bd
SL
1426 if (this->send_certreq_authorities)
1427 {
1428 write_certificate_authorities(writer);
1429 }
1430 else
1431 {
1432 writer->write_data16(writer, chunk_empty);
1433 }
d2fc9b09
PK
1434 }
1435 else
1436 {
1437 /* certificate request context as described in RFC 8446, section 4.3.2 */
1438 writer->write_uint8(writer, 0);
1439
1440 extensions = bio_writer_create(32);
a4a128bd
SL
1441
1442 if (this->send_certreq_authorities)
1443 {
1444 DBG2(DBG_TLS, "sending extension: %N",
1445 tls_extension_names, TLS_EXT_CERTIFICATE_AUTHORITIES);
1446 authorities = bio_writer_create(64);
1447 write_certificate_authorities(authorities);
1448 extensions->write_uint16(extensions, TLS_EXT_CERTIFICATE_AUTHORITIES);
1449 extensions->write_data16(extensions, authorities->get_buf(authorities));
1450 authorities->destroy(authorities);
1451 }
d2fc9b09
PK
1452
1453 DBG2(DBG_TLS, "sending extension: %N",
1454 tls_extension_names, TLS_EXT_SIGNATURE_ALGORITHMS);
1455 extensions->write_uint16(extensions, TLS_EXT_SIGNATURE_ALGORITHMS);
1456 supported = bio_writer_create(32);
1457 this->crypto->get_signature_algorithms(this->crypto, supported, TRUE);
1458 extensions->write_data16(extensions, supported->get_buf(supported));
1459 supported->destroy(supported);
1460 writer->write_data16(writer, extensions->get_buf(extensions));
1461 extensions->destroy(extensions);
1462 }
400df4ca
MW
1463
1464 *type = TLS_CERTIFICATE_REQUEST;
1465 this->state = STATE_CERTREQ_SENT;
1466 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1467 return NEED_MORE;
1468}
1469
37a59a8f 1470/**
3f7bb88b 1471 * Try to find a curve supported by both, client and server
37a59a8f 1472 */
3f7bb88b 1473static bool find_supported_curve(private_tls_server_t *this,
7fbe2e27 1474 tls_named_group_t *curve)
37a59a8f 1475{
7a2b0266 1476 tls_named_group_t current;
3f7bb88b
MW
1477 enumerator_t *enumerator;
1478
1479 enumerator = this->crypto->create_ec_enumerator(this->crypto);
1480 while (enumerator->enumerate(enumerator, NULL, &current))
1481 {
1482 if (peer_supports_curve(this, current))
37a59a8f 1483 {
3f7bb88b
MW
1484 *curve = current;
1485 enumerator->destroy(enumerator);
37a59a8f
MW
1486 return TRUE;
1487 }
1488 }
3f7bb88b 1489 enumerator->destroy(enumerator);
37a59a8f
MW
1490 return FALSE;
1491}
1492
f14358a9
MW
1493/**
1494 * Send Server key Exchange
1495 */
1496static status_t send_server_key_exchange(private_tls_server_t *this,
7e432eff 1497 tls_handshake_type_t *type, bio_writer_t *writer,
f14358a9
MW
1498 diffie_hellman_group_t group)
1499{
141d7f7a 1500 diffie_hellman_params_t *params = NULL;
7a2b0266 1501 tls_named_group_t curve;
f14358a9
MW
1502 chunk_t chunk;
1503
f4c98ae6 1504 if (diffie_hellman_group_is_ec(group))
141d7f7a 1505 {
066ac880 1506 curve = tls_ec_group_to_curve(group);
3f7bb88b
MW
1507 if (!curve || (!peer_supports_curve(this, curve) &&
1508 !find_supported_curve(this, &curve)))
f4c98ae6
MW
1509 {
1510 DBG1(DBG_TLS, "no EC group supported by client and server");
1511 this->alert->add(this->alert, TLS_FATAL, TLS_HANDSHAKE_FAILURE);
1512 return NEED_MORE;
1513 }
7a2b0266 1514 DBG2(DBG_TLS, "selected ECDH group %N", tls_named_group_names, curve);
f4c98ae6 1515 writer->write_uint8(writer, TLS_ECC_NAMED_CURVE);
3f7bb88b 1516 writer->write_uint16(writer, curve);
f4c98ae6
MW
1517 }
1518 else
1519 {
1520 params = diffie_hellman_get_params(group);
1521 if (!params)
1522 {
1523 DBG1(DBG_TLS, "no parameters found for DH group %N",
1524 diffie_hellman_group_names, group);
1525 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1526 return NEED_MORE;
1527 }
e4fd2bb4 1528 DBG2(DBG_TLS, "selected DH group %N", diffie_hellman_group_names, group);
f4c98ae6
MW
1529 writer->write_data16(writer, params->prime);
1530 writer->write_data16(writer, params->generator);
141d7f7a 1531 }
37a59a8f
MW
1532 this->dh = lib->crypto->create_dh(lib->crypto, group);
1533 if (!this->dh)
1534 {
1535 DBG1(DBG_TLS, "DH group %N not supported",
1536 diffie_hellman_group_names, group);
1537 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1538 return NEED_MORE;
1539 }
42431690
MW
1540 if (!this->dh->get_my_public_value(this->dh, &chunk))
1541 {
1542 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1543 return NEED_MORE;
1544 }
141d7f7a
MW
1545 if (params)
1546 {
1547 writer->write_data16(writer, chunk);
1548 }
966a26ea
TB
1549 else if (group != CURVE_25519 &&
1550 group != CURVE_448)
e6cce7ff
MW
1551 { /* ECP uses 8bit length header only, but a point format */
1552 writer->write_uint8(writer, chunk.len + 1);
ec7d4e70 1553 writer->write_uint8(writer, TLS_ANSI_UNCOMPRESSED);
e6cce7ff 1554 writer->write_data(writer, chunk);
141d7f7a 1555 }
966a26ea
TB
1556 else
1557 { /* ECPoint uses an 8-bit length header only */
1558 writer->write_data8(writer, chunk);
1559 }
f14358a9
MW
1560 free(chunk.ptr);
1561
1562 chunk = chunk_cat("ccc", chunk_from_thing(this->client_random),
1563 chunk_from_thing(this->server_random), writer->get_buf(writer));
1564 if (!this->private || !this->crypto->sign(this->crypto, this->private,
1565 writer, chunk, this->hashsig))
1566 {
1567 DBG1(DBG_TLS, "signing DH parameters failed");
1568 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1569 free(chunk.ptr);
1570 return NEED_MORE;
1571 }
1572 free(chunk.ptr);
1573 *type = TLS_SERVER_KEY_EXCHANGE;
1574 this->state = STATE_KEY_EXCHANGE_SENT;
1575 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1576 return NEED_MORE;
1577}
1578
400df4ca
MW
1579/**
1580 * Send Hello Done
1581 */
1582static status_t send_hello_done(private_tls_server_t *this,
7e432eff 1583 tls_handshake_type_t *type, bio_writer_t *writer)
400df4ca
MW
1584{
1585 *type = TLS_SERVER_HELLO_DONE;
1586 this->state = STATE_HELLO_DONE;
1587 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1588 return NEED_MORE;
1589}
1590
1591/**
1592 * Send Finished
1593 */
1594static status_t send_finished(private_tls_server_t *this,
7e432eff 1595 tls_handshake_type_t *type, bio_writer_t *writer)
400df4ca 1596{
7fbe2e27 1597 if (this->tls->get_version_max(this->tls) < TLS_1_3)
400df4ca 1598 {
7fbe2e27
PK
1599 char buf[12];
1600
1601 if (!this->crypto->calculate_finished_legacy(this->crypto,
1602 "server finished", buf))
1603 {
1604 DBG1(DBG_TLS, "calculating server finished data failed");
1605 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1606 return FAILED;
1607 }
1608
1609 writer->write_data(writer, chunk_from_thing(buf));
400df4ca 1610 }
7fbe2e27
PK
1611 else
1612 {
1613 chunk_t verify_data;
1614
1615 if (!this->crypto->calculate_finished(this->crypto, TRUE, &verify_data))
1616 {
1617 DBG1(DBG_TLS, "calculating server finished data failed");
1618 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1619 return NEED_MORE;
1620 }
400df4ca 1621
7fbe2e27
PK
1622 writer->write_data(writer, verify_data);
1623 chunk_free(&verify_data);
1624 }
400df4ca
MW
1625
1626 *type = TLS_FINISHED;
1627 this->state = STATE_FINISHED_SENT;
6a5c86b7
MW
1628 this->crypto->append_handshake(this->crypto, *type, writer->get_buf(writer));
1629
4c0c2283
MW
1630 return NEED_MORE;
1631}
1632
ab70f68c
PK
1633/**
1634 * Send KeyUpdate message
1635 */
1636static status_t send_key_update(private_tls_server_t *this,
1637 tls_handshake_type_t *type, bio_writer_t *writer)
1638{
1639 *type = TLS_KEY_UPDATE;
1640
1641 /* we currently only send this as reply, so we never request an update */
1642 writer->write_uint8(writer, 0);
1643
1644 this->state = STATE_KEY_UPDATE_SENT;
1645 return NEED_MORE;
1646}
1647
4c0c2283 1648METHOD(tls_handshake_t, build, status_t,
7e432eff 1649 private_tls_server_t *this, tls_handshake_type_t *type, bio_writer_t *writer)
4c0c2283 1650{
f14358a9
MW
1651 diffie_hellman_group_t group;
1652
7fbe2e27 1653 if (this->tls->get_version_max(this->tls) < TLS_1_3)
400df4ca 1654 {
7fbe2e27
PK
1655 switch (this->state)
1656 {
1657 case STATE_HELLO_RECEIVED:
1658 return send_server_hello(this, type, writer);
1659 case STATE_HELLO_SENT:
1660 return send_certificate(this, type, writer);
1661 case STATE_CERT_SENT:
1662 group = this->crypto->get_dh_group(this->crypto);
1663 if (group)
1664 {
1665 return send_server_key_exchange(this, type, writer, group);
1666 }
1667 /* otherwise fall through to next state */
1668 case STATE_KEY_EXCHANGE_SENT:
4bba89ff
PK
1669 if (this->peer)
1670 {
1671 return send_certificate_request(this, type, writer);
1672 }
1673 /* otherwise fall through to next state */
7fbe2e27
PK
1674 case STATE_CERTREQ_SENT:
1675 return send_hello_done(this, type, writer);
1676 case STATE_CIPHERSPEC_CHANGED_OUT:
1677 return send_finished(this, type, writer);
1678 case STATE_FINISHED_SENT:
1679 return INVALID_STATE;
1680 default:
1681 return INVALID_STATE;
1682 }
1683 }
1684 else
1685 {
1686 switch (this->state)
1687 {
1688 case STATE_HELLO_RECEIVED:
1689 return send_server_hello(this, type, writer);
1690 case STATE_HELLO_SENT:
1691 case STATE_CIPHERSPEC_CHANGED_OUT:
1692 return send_encrypted_extensions(this, type, writer);
1693 case STATE_ENCRYPTED_EXTENSIONS_SENT:
d2fc9b09
PK
1694 if (this->peer)
1695 {
1696 return send_certificate_request(this, type, writer);
1697 }
1698 /* otherwise fall through to next state */
1699 case STATE_CERTREQ_SENT:
7fbe2e27
PK
1700 return send_certificate(this, type, writer);
1701 case STATE_CERT_SENT:
1702 return send_certificate_verify(this, type, writer);
1703 case STATE_CERT_VERIFY_SENT:
1704 return send_finished(this, type, writer);
1705 case STATE_FINISHED_SENT:
d2fc9b09
PK
1706 if (!this->crypto->derive_app_keys(this->crypto))
1707 {
1708 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1709 return NEED_MORE;
1710 }
1711 /* inbound key switches after process client finished message */
1712 this->crypto->change_cipher(this->crypto, FALSE);
1713 this->state = STATE_FINISHED_SENT_KEY_SWITCHED;
7fbe2e27 1714 return INVALID_STATE;
ab70f68c
PK
1715 case STATE_KEY_UPDATE_REQUESTED:
1716 return send_key_update(this, type, writer);
1717 case STATE_KEY_UPDATE_SENT:
1718 if (!this->crypto->update_app_keys(this->crypto, FALSE))
1719 {
1720 this->alert->add(this->alert, TLS_FATAL, TLS_INTERNAL_ERROR);
1721 return NEED_MORE;
1722 }
1723 this->crypto->change_cipher(this->crypto, FALSE);
1724 this->state = STATE_FINISHED_RECEIVED;
7fbe2e27
PK
1725 default:
1726 return INVALID_STATE;
1727 }
400df4ca 1728 }
4c0c2283
MW
1729}
1730
3ddd164e 1731METHOD(tls_handshake_t, cipherspec_changed, bool,
4caa3806 1732 private_tls_server_t *this, bool inbound)
3ddd164e 1733{
7fbe2e27 1734 if (this->tls->get_version_max(this->tls) < TLS_1_3)
400df4ca 1735 {
7fbe2e27 1736 if (inbound)
6a5c86b7 1737 {
7fbe2e27
PK
1738 if (this->resume)
1739 {
1740 return this->state == STATE_FINISHED_SENT;
1741 }
1742 if (this->peer)
1743 {
1744 return this->state == STATE_CERT_VERIFY_RECEIVED;
1745 }
1746 return this->state == STATE_KEY_EXCHANGE_RECEIVED;
6a5c86b7 1747 }
7fbe2e27 1748 else
4caa3806 1749 {
7fbe2e27
PK
1750 if (this->resume)
1751 {
1752 return this->state == STATE_HELLO_SENT;
1753 }
1754 return this->state == STATE_FINISHED_RECEIVED;
4caa3806 1755 }
7fbe2e27 1756 return FALSE;
4caa3806
MW
1757 }
1758 else
1759 {
7fbe2e27 1760 if (inbound)
c3e71324 1761 { /* accept ChangeCipherSpec after ServerFinish or HelloRetryRequest */
d2fc9b09
PK
1762 return this->state == STATE_FINISHED_SENT ||
1763 this->state == STATE_FINISHED_SENT_KEY_SWITCHED ||
1764 retrying(this);
7fbe2e27
PK
1765 }
1766 else
6a5c86b7
MW
1767 {
1768 return this->state == STATE_HELLO_SENT;
1769 }
400df4ca 1770 }
3ddd164e
MW
1771}
1772
4caa3806
MW
1773METHOD(tls_handshake_t, change_cipherspec, void,
1774 private_tls_server_t *this, bool inbound)
3ddd164e 1775{
7fbe2e27
PK
1776 if (this->tls->get_version_max(this->tls) < TLS_1_3)
1777 {
1778 this->crypto->change_cipher(this->crypto, inbound);
1779 }
1780
c3e71324
PK
1781 if (retrying(this))
1782 { /* client might send a ChangeCipherSpec after a HelloRetryRequest and
1783 * before a new ClientHello which should not cause any state changes */
1784 return;
1785 }
1786
4caa3806 1787 if (inbound)
400df4ca 1788 {
400df4ca 1789 this->state = STATE_CIPHERSPEC_CHANGED_IN;
400df4ca 1790 }
4caa3806
MW
1791 else
1792 {
1793 this->state = STATE_CIPHERSPEC_CHANGED_OUT;
1794 }
3ddd164e
MW
1795}
1796
1327839d
AS
1797METHOD(tls_handshake_t, finished, bool,
1798 private_tls_server_t *this)
1799{
7fbe2e27
PK
1800 if (this->tls->get_version_max(this->tls) < TLS_1_3)
1801 {
1802 if (this->resume)
1803 {
1804 return this->state == STATE_FINISHED_RECEIVED;
1805 }
1806 return this->state == STATE_FINISHED_SENT;
1807 }
1808 else
6a5c86b7
MW
1809 {
1810 return this->state == STATE_FINISHED_RECEIVED;
1811 }
1327839d
AS
1812}
1813
2de481e3
MW
1814METHOD(tls_handshake_t, get_peer_id, identification_t*,
1815 private_tls_server_t *this)
1816{
1817 return this->peer;
1818}
1819
1820METHOD(tls_handshake_t, get_server_id, identification_t*,
1821 private_tls_server_t *this)
1822{
1823 return this->server;
1824}
1825
666c5523
MW
1826METHOD(tls_handshake_t, get_auth, auth_cfg_t*,
1827 private_tls_server_t *this)
1828{
1829 return this->peer_auth;
1830}
1831
4c0c2283
MW
1832METHOD(tls_handshake_t, destroy, void,
1833 private_tls_server_t *this)
1834{
400df4ca 1835 DESTROY_IF(this->private);
f14358a9 1836 DESTROY_IF(this->dh);
2de481e3
MW
1837 DESTROY_IF(this->peer);
1838 this->server->destroy(this->server);
400df4ca
MW
1839 this->peer_auth->destroy(this->peer_auth);
1840 this->server_auth->destroy(this->server_auth);
06109c47 1841 free(this->hashsig.ptr);
37a59a8f 1842 free(this->curves.ptr);
6a5c86b7 1843 free(this->session.ptr);
4c0c2283
MW
1844 free(this);
1845}
1846
1847/**
1848 * See header
1849 */
e6f3ef13
MW
1850tls_server_t *tls_server_create(tls_t *tls,
1851 tls_crypto_t *crypto, tls_alert_t *alert,
1852 identification_t *server, identification_t *peer)
4c0c2283
MW
1853{
1854 private_tls_server_t *this;
1855
1856 INIT(this,
ba31fe1f
MW
1857 .public = {
1858 .handshake = {
1859 .process = _process,
1860 .build = _build,
1861 .cipherspec_changed = _cipherspec_changed,
1862 .change_cipherspec = _change_cipherspec,
1863 .finished = _finished,
2de481e3
MW
1864 .get_peer_id = _get_peer_id,
1865 .get_server_id = _get_server_id,
666c5523 1866 .get_auth = _get_auth,
ba31fe1f
MW
1867 .destroy = _destroy,
1868 },
4c0c2283 1869 },
3e962b08 1870 .tls = tls,
536dbc00 1871 .crypto = crypto,
e6f3ef13 1872 .alert = alert,
2de481e3
MW
1873 .server = server->clone(server),
1874 .peer = peer ? peer->clone(peer) : NULL,
400df4ca
MW
1875 .state = STATE_INIT,
1876 .peer_auth = auth_cfg_create(),
1877 .server_auth = auth_cfg_create(),
a4a128bd
SL
1878 .send_certreq_authorities = lib->settings->get_bool(lib->settings,
1879 "%s.tls.send_certreq_authorities",
1880 TRUE, lib->ns),
4c0c2283
MW
1881 );
1882
1883 return &this->public;
1884}