2 * TLS interface functions and an internal TLS implementation
3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file interface functions for hostapd/wpa_supplicant to use the
9 * integrated TLSv1 implementation.
16 #include "tls/tlsv1_client.h"
17 #include "tls/tlsv1_server.h"
20 static int tls_ref_count = 0;
24 struct tlsv1_credentials *server_cred;
27 void (*event_cb)(void *ctx, enum tls_event ev,
28 union tls_event_data *data);
33 struct tls_connection {
34 struct tlsv1_client *client;
35 struct tlsv1_server *server;
36 struct tls_global *global;
40 void * tls_init(const struct tls_config *conf)
42 struct tls_global *global;
44 if (tls_ref_count == 0) {
45 #ifdef CONFIG_TLS_INTERNAL_CLIENT
46 if (tlsv1_client_global_init())
48 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
49 #ifdef CONFIG_TLS_INTERNAL_SERVER
50 if (tlsv1_server_global_init())
52 #endif /* CONFIG_TLS_INTERNAL_SERVER */
56 global = os_zalloc(sizeof(*global));
60 global->event_cb = conf->event_cb;
61 global->cb_ctx = conf->cb_ctx;
62 global->cert_in_cb = conf->cert_in_cb;
68 void tls_deinit(void *ssl_ctx)
70 struct tls_global *global = ssl_ctx;
72 if (tls_ref_count == 0) {
73 #ifdef CONFIG_TLS_INTERNAL_CLIENT
74 tlsv1_client_global_deinit();
75 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
76 #ifdef CONFIG_TLS_INTERNAL_SERVER
77 tlsv1_server_global_deinit();
78 #endif /* CONFIG_TLS_INTERNAL_SERVER */
80 #ifdef CONFIG_TLS_INTERNAL_SERVER
81 tlsv1_cred_free(global->server_cred);
82 #endif /* CONFIG_TLS_INTERNAL_SERVER */
87 int tls_get_errors(void *tls_ctx)
93 struct tls_connection * tls_connection_init(void *tls_ctx)
95 struct tls_connection *conn;
96 struct tls_global *global = tls_ctx;
98 conn = os_zalloc(sizeof(*conn));
101 conn->global = global;
103 #ifdef CONFIG_TLS_INTERNAL_CLIENT
104 if (!global->server) {
105 conn->client = tlsv1_client_init();
106 if (conn->client == NULL) {
110 tlsv1_client_set_cb(conn->client, global->event_cb,
111 global->cb_ctx, global->cert_in_cb);
113 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
114 #ifdef CONFIG_TLS_INTERNAL_SERVER
115 if (global->server) {
116 conn->server = tlsv1_server_init(global->server_cred);
117 if (conn->server == NULL) {
122 #endif /* CONFIG_TLS_INTERNAL_SERVER */
128 #ifdef CONFIG_TESTING_OPTIONS
129 #ifdef CONFIG_TLS_INTERNAL_SERVER
130 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags)
133 tlsv1_server_set_test_flags(conn->server, flags);
135 #endif /* CONFIG_TLS_INTERNAL_SERVER */
136 #endif /* CONFIG_TESTING_OPTIONS */
139 void tls_connection_set_log_cb(struct tls_connection *conn,
140 void (*log_cb)(void *ctx, const char *msg),
143 #ifdef CONFIG_TLS_INTERNAL_SERVER
145 tlsv1_server_set_log_cb(conn->server, log_cb, ctx);
146 #endif /* CONFIG_TLS_INTERNAL_SERVER */
150 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
154 #ifdef CONFIG_TLS_INTERNAL_CLIENT
156 tlsv1_client_deinit(conn->client);
157 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
158 #ifdef CONFIG_TLS_INTERNAL_SERVER
160 tlsv1_server_deinit(conn->server);
161 #endif /* CONFIG_TLS_INTERNAL_SERVER */
166 int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
168 #ifdef CONFIG_TLS_INTERNAL_CLIENT
170 return tlsv1_client_established(conn->client);
171 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
172 #ifdef CONFIG_TLS_INTERNAL_SERVER
174 return tlsv1_server_established(conn->server);
175 #endif /* CONFIG_TLS_INTERNAL_SERVER */
180 char * tls_connection_peer_serial_num(void *tls_ctx,
181 struct tls_connection *conn)
188 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
190 #ifdef CONFIG_TLS_INTERNAL_CLIENT
192 return tlsv1_client_shutdown(conn->client);
193 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
194 #ifdef CONFIG_TLS_INTERNAL_SERVER
196 return tlsv1_server_shutdown(conn->server);
197 #endif /* CONFIG_TLS_INTERNAL_SERVER */
202 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
203 const struct tls_connection_params *params)
205 #ifdef CONFIG_TLS_INTERNAL_CLIENT
206 struct tlsv1_credentials *cred;
208 if (conn->client == NULL)
211 if (params->flags & TLS_CONN_EXT_CERT_CHECK) {
213 "TLS: tls_ext_cert_check=1 not supported");
217 cred = tlsv1_cred_alloc();
221 if (params->subject_match) {
222 wpa_printf(MSG_INFO, "TLS: subject_match not supported");
223 tlsv1_cred_free(cred);
227 if (params->altsubject_match) {
228 wpa_printf(MSG_INFO, "TLS: altsubject_match not supported");
229 tlsv1_cred_free(cred);
233 if (params->suffix_match) {
234 wpa_printf(MSG_INFO, "TLS: suffix_match not supported");
235 tlsv1_cred_free(cred);
239 if (params->domain_match) {
240 wpa_printf(MSG_INFO, "TLS: domain_match not supported");
241 tlsv1_cred_free(cred);
245 if (params->openssl_ciphers) {
246 wpa_printf(MSG_INFO, "TLS: openssl_ciphers not supported");
247 tlsv1_cred_free(cred);
251 if (params->openssl_ecdh_curves) {
252 wpa_printf(MSG_INFO, "TLS: openssl_ecdh_curves not supported");
253 tlsv1_cred_free(cred);
257 if (tlsv1_set_ca_cert(cred, params->ca_cert,
258 params->ca_cert_blob, params->ca_cert_blob_len,
260 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
262 tlsv1_cred_free(cred);
266 if (tlsv1_set_cert(cred, params->client_cert,
267 params->client_cert_blob,
268 params->client_cert_blob_len)) {
269 wpa_printf(MSG_INFO, "TLS: Failed to configure client "
271 tlsv1_cred_free(cred);
275 if (tlsv1_set_private_key(cred, params->private_key,
276 params->private_key_passwd,
277 params->private_key_blob,
278 params->private_key_blob_len)) {
279 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
280 tlsv1_cred_free(cred);
284 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
285 params->dh_blob_len)) {
286 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
287 tlsv1_cred_free(cred);
291 if (tlsv1_client_set_cred(conn->client, cred) < 0) {
292 tlsv1_cred_free(cred);
296 tlsv1_client_set_flags(conn->client, params->flags);
299 #else /* CONFIG_TLS_INTERNAL_CLIENT */
301 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
305 int tls_global_set_params(void *tls_ctx,
306 const struct tls_connection_params *params)
308 #ifdef CONFIG_TLS_INTERNAL_SERVER
309 struct tls_global *global = tls_ctx;
310 struct tlsv1_credentials *cred;
312 if (params->check_cert_subject)
313 return -1; /* not yet supported */
315 /* Currently, global parameters are only set when running in server
318 tlsv1_cred_free(global->server_cred);
319 global->server_cred = cred = tlsv1_cred_alloc();
323 if (tlsv1_set_ca_cert(cred, params->ca_cert, params->ca_cert_blob,
324 params->ca_cert_blob_len, params->ca_path)) {
325 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
330 if (tlsv1_set_cert(cred, params->client_cert, params->client_cert_blob,
331 params->client_cert_blob_len)) {
332 wpa_printf(MSG_INFO, "TLS: Failed to configure server "
337 if (tlsv1_set_private_key(cred, params->private_key,
338 params->private_key_passwd,
339 params->private_key_blob,
340 params->private_key_blob_len)) {
341 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
345 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
346 params->dh_blob_len)) {
347 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
351 if (params->ocsp_stapling_response)
352 cred->ocsp_stapling_response =
353 os_strdup(params->ocsp_stapling_response);
354 if (params->ocsp_stapling_response_multi)
355 cred->ocsp_stapling_response_multi =
356 os_strdup(params->ocsp_stapling_response_multi);
359 #else /* CONFIG_TLS_INTERNAL_SERVER */
361 #endif /* CONFIG_TLS_INTERNAL_SERVER */
365 int tls_global_set_verify(void *tls_ctx, int check_crl, int strict)
367 struct tls_global *global = tls_ctx;
368 global->check_crl = check_crl;
373 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
374 int verify_peer, unsigned int flags,
375 const u8 *session_ctx, size_t session_ctx_len)
377 #ifdef CONFIG_TLS_INTERNAL_SERVER
379 return tlsv1_server_set_verify(conn->server, verify_peer);
380 #endif /* CONFIG_TLS_INTERNAL_SERVER */
385 int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn,
386 struct tls_random *data)
388 #ifdef CONFIG_TLS_INTERNAL_CLIENT
390 return tlsv1_client_get_random(conn->client, data);
391 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
392 #ifdef CONFIG_TLS_INTERNAL_SERVER
394 return tlsv1_server_get_random(conn->server, data);
395 #endif /* CONFIG_TLS_INTERNAL_SERVER */
400 static int tls_get_keyblock_size(struct tls_connection *conn)
402 #ifdef CONFIG_TLS_INTERNAL_CLIENT
404 return tlsv1_client_get_keyblock_size(conn->client);
405 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
406 #ifdef CONFIG_TLS_INTERNAL_SERVER
408 return tlsv1_server_get_keyblock_size(conn->server);
409 #endif /* CONFIG_TLS_INTERNAL_SERVER */
414 static int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
415 const char *label, const u8 *context,
416 size_t context_len, int server_random_first,
417 int skip_keyblock, u8 *out, size_t out_len)
419 int ret = -1, skip = 0;
424 skip = tls_get_keyblock_size(conn);
427 tmp_out = os_malloc(skip + out_len);
433 #ifdef CONFIG_TLS_INTERNAL_CLIENT
435 ret = tlsv1_client_prf(conn->client, label, context,
436 context_len, server_random_first,
437 _out, skip + out_len);
439 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
440 #ifdef CONFIG_TLS_INTERNAL_SERVER
442 ret = tlsv1_server_prf(conn->server, label, context,
443 context_len, server_random_first,
444 _out, skip + out_len);
446 #endif /* CONFIG_TLS_INTERNAL_SERVER */
447 if (ret == 0 && skip_keyblock)
448 os_memcpy(out, _out + skip, out_len);
449 bin_clear_free(tmp_out, skip);
455 int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
456 const char *label, const u8 *context,
457 size_t context_len, u8 *out, size_t out_len)
459 return tls_connection_prf(tls_ctx, conn, label, context, context_len,
464 int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
465 u8 *out, size_t out_len)
467 return tls_connection_prf(tls_ctx, conn, "key expansion", NULL, 0,
472 struct wpabuf * tls_connection_handshake(void *tls_ctx,
473 struct tls_connection *conn,
474 const struct wpabuf *in_data,
475 struct wpabuf **appl_data)
477 return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data,
482 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
483 struct tls_connection *conn,
484 const struct wpabuf *in_data,
485 struct wpabuf **appl_data,
488 #ifdef CONFIG_TLS_INTERNAL_CLIENT
490 size_t res_len, ad_len;
493 if (conn->client == NULL)
497 res = tlsv1_client_handshake(conn->client,
498 in_data ? wpabuf_head(in_data) : NULL,
499 in_data ? wpabuf_len(in_data) : 0,
500 &res_len, &ad, &ad_len, need_more_data);
503 out = wpabuf_alloc_ext_data(res, res_len);
511 *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
512 if (*appl_data == NULL)
520 #else /* CONFIG_TLS_INTERNAL_CLIENT */
522 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
526 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
527 struct tls_connection *conn,
528 const struct wpabuf *in_data,
529 struct wpabuf **appl_data)
531 #ifdef CONFIG_TLS_INTERNAL_SERVER
536 if (conn->server == NULL)
542 res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
543 wpabuf_len(in_data), &res_len);
544 if (res == NULL && tlsv1_server_established(conn->server))
545 return wpabuf_alloc(0);
548 out = wpabuf_alloc_ext_data(res, res_len);
555 #else /* CONFIG_TLS_INTERNAL_SERVER */
557 #endif /* CONFIG_TLS_INTERNAL_SERVER */
561 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
562 struct tls_connection *conn,
563 const struct wpabuf *in_data)
565 #ifdef CONFIG_TLS_INTERNAL_CLIENT
569 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
572 res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
580 wpabuf_put(buf, res);
583 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
584 #ifdef CONFIG_TLS_INTERNAL_SERVER
588 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
591 res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
599 wpabuf_put(buf, res);
602 #endif /* CONFIG_TLS_INTERNAL_SERVER */
607 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
608 struct tls_connection *conn,
609 const struct wpabuf *in_data)
611 return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL);
615 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
616 struct tls_connection *conn,
617 const struct wpabuf *in_data,
623 #ifdef CONFIG_TLS_INTERNAL_CLIENT
625 return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
629 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
630 #ifdef CONFIG_TLS_INTERNAL_SERVER
634 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
637 res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
645 wpabuf_put(buf, res);
648 #endif /* CONFIG_TLS_INTERNAL_SERVER */
653 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
655 #ifdef CONFIG_TLS_INTERNAL_CLIENT
657 return tlsv1_client_resumed(conn->client);
658 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
659 #ifdef CONFIG_TLS_INTERNAL_SERVER
661 return tlsv1_server_resumed(conn->server);
662 #endif /* CONFIG_TLS_INTERNAL_SERVER */
667 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
670 #ifdef CONFIG_TLS_INTERNAL_CLIENT
672 return tlsv1_client_set_cipher_list(conn->client, ciphers);
673 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
674 #ifdef CONFIG_TLS_INTERNAL_SERVER
676 return tlsv1_server_set_cipher_list(conn->server, ciphers);
677 #endif /* CONFIG_TLS_INTERNAL_SERVER */
682 int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
683 char *buf, size_t buflen)
687 #ifdef CONFIG_TLS_INTERNAL_CLIENT
689 return tlsv1_client_get_version(conn->client, buf, buflen);
690 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
695 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
696 char *buf, size_t buflen)
700 #ifdef CONFIG_TLS_INTERNAL_CLIENT
702 return tlsv1_client_get_cipher(conn->client, buf, buflen);
703 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
704 #ifdef CONFIG_TLS_INTERNAL_SERVER
706 return tlsv1_server_get_cipher(conn->server, buf, buflen);
707 #endif /* CONFIG_TLS_INTERNAL_SERVER */
712 int tls_connection_enable_workaround(void *tls_ctx,
713 struct tls_connection *conn)
719 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
720 int ext_type, const u8 *data,
723 #ifdef CONFIG_TLS_INTERNAL_CLIENT
725 return tlsv1_client_hello_ext(conn->client, ext_type,
728 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
733 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
735 #ifdef CONFIG_TLS_INTERNAL_SERVER
737 return tlsv1_server_get_failed(conn->server);
738 #endif /* CONFIG_TLS_INTERNAL_SERVER */
743 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
745 #ifdef CONFIG_TLS_INTERNAL_SERVER
747 return tlsv1_server_get_read_alerts(conn->server);
748 #endif /* CONFIG_TLS_INTERNAL_SERVER */
753 int tls_connection_get_write_alerts(void *tls_ctx,
754 struct tls_connection *conn)
756 #ifdef CONFIG_TLS_INTERNAL_SERVER
758 return tlsv1_server_get_write_alerts(conn->server);
759 #endif /* CONFIG_TLS_INTERNAL_SERVER */
764 int tls_connection_set_session_ticket_cb(void *tls_ctx,
765 struct tls_connection *conn,
766 tls_session_ticket_cb cb,
769 #ifdef CONFIG_TLS_INTERNAL_CLIENT
771 tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx);
774 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
775 #ifdef CONFIG_TLS_INTERNAL_SERVER
777 tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx);
780 #endif /* CONFIG_TLS_INTERNAL_SERVER */
785 int tls_get_library_version(char *buf, size_t buf_len)
787 return os_snprintf(buf, buf_len, "internal");
791 void tls_connection_set_success_data(struct tls_connection *conn,
797 void tls_connection_set_success_data_resumed(struct tls_connection *conn)
802 const struct wpabuf *
803 tls_connection_get_success_data(struct tls_connection *conn)
809 void tls_connection_remove_session(struct tls_connection *conn)