2 * TLS interface functions and an internal TLS implementation
3 * Copyright (c) 2004-2011, 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;
28 struct tls_connection {
29 struct tlsv1_client *client;
30 struct tlsv1_server *server;
31 struct tls_global *global;
35 void * tls_init(const struct tls_config *conf)
37 struct tls_global *global;
39 if (tls_ref_count == 0) {
40 #ifdef CONFIG_TLS_INTERNAL_CLIENT
41 if (tlsv1_client_global_init())
43 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
44 #ifdef CONFIG_TLS_INTERNAL_SERVER
45 if (tlsv1_server_global_init())
47 #endif /* CONFIG_TLS_INTERNAL_SERVER */
51 global = os_zalloc(sizeof(*global));
58 void tls_deinit(void *ssl_ctx)
60 struct tls_global *global = ssl_ctx;
62 if (tls_ref_count == 0) {
63 #ifdef CONFIG_TLS_INTERNAL_CLIENT
64 tlsv1_client_global_deinit();
65 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
66 #ifdef CONFIG_TLS_INTERNAL_SERVER
67 tlsv1_cred_free(global->server_cred);
68 tlsv1_server_global_deinit();
69 #endif /* CONFIG_TLS_INTERNAL_SERVER */
75 int tls_get_errors(void *tls_ctx)
81 struct tls_connection * tls_connection_init(void *tls_ctx)
83 struct tls_connection *conn;
84 struct tls_global *global = tls_ctx;
86 conn = os_zalloc(sizeof(*conn));
89 conn->global = global;
91 #ifdef CONFIG_TLS_INTERNAL_CLIENT
92 if (!global->server) {
93 conn->client = tlsv1_client_init();
94 if (conn->client == NULL) {
99 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
100 #ifdef CONFIG_TLS_INTERNAL_SERVER
101 if (global->server) {
102 conn->server = tlsv1_server_init(global->server_cred);
103 if (conn->server == NULL) {
108 #endif /* CONFIG_TLS_INTERNAL_SERVER */
114 #ifdef CONFIG_TESTING_OPTIONS
115 #ifdef CONFIG_TLS_INTERNAL_SERVER
116 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags)
119 tlsv1_server_set_test_flags(conn->server, flags);
121 #endif /* CONFIG_TLS_INTERNAL_SERVER */
122 #endif /* CONFIG_TESTING_OPTIONS */
125 void tls_connection_set_log_cb(struct tls_connection *conn,
126 void (*log_cb)(void *ctx, const char *msg),
129 #ifdef CONFIG_TLS_INTERNAL_SERVER
131 tlsv1_server_set_log_cb(conn->server, log_cb, ctx);
132 #endif /* CONFIG_TLS_INTERNAL_SERVER */
136 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
140 #ifdef CONFIG_TLS_INTERNAL_CLIENT
142 tlsv1_client_deinit(conn->client);
143 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
144 #ifdef CONFIG_TLS_INTERNAL_SERVER
146 tlsv1_server_deinit(conn->server);
147 #endif /* CONFIG_TLS_INTERNAL_SERVER */
152 int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
154 #ifdef CONFIG_TLS_INTERNAL_CLIENT
156 return tlsv1_client_established(conn->client);
157 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
158 #ifdef CONFIG_TLS_INTERNAL_SERVER
160 return tlsv1_server_established(conn->server);
161 #endif /* CONFIG_TLS_INTERNAL_SERVER */
166 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
168 #ifdef CONFIG_TLS_INTERNAL_CLIENT
170 return tlsv1_client_shutdown(conn->client);
171 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
172 #ifdef CONFIG_TLS_INTERNAL_SERVER
174 return tlsv1_server_shutdown(conn->server);
175 #endif /* CONFIG_TLS_INTERNAL_SERVER */
180 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
181 const struct tls_connection_params *params)
183 #ifdef CONFIG_TLS_INTERNAL_CLIENT
184 struct tlsv1_credentials *cred;
186 if (conn->client == NULL)
189 cred = tlsv1_cred_alloc();
193 if (params->subject_match) {
194 wpa_printf(MSG_INFO, "TLS: subject_match not supported");
198 if (params->altsubject_match) {
199 wpa_printf(MSG_INFO, "TLS: altsubject_match not supported");
203 if (params->suffix_match) {
204 wpa_printf(MSG_INFO, "TLS: suffix_match not supported");
208 if (params->domain_match) {
209 wpa_printf(MSG_INFO, "TLS: domain_match not supported");
213 if (params->openssl_ciphers) {
214 wpa_printf(MSG_INFO, "GnuTLS: openssl_ciphers not supported");
218 if (tlsv1_set_ca_cert(cred, params->ca_cert,
219 params->ca_cert_blob, params->ca_cert_blob_len,
221 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
223 tlsv1_cred_free(cred);
227 if (tlsv1_set_cert(cred, params->client_cert,
228 params->client_cert_blob,
229 params->client_cert_blob_len)) {
230 wpa_printf(MSG_INFO, "TLS: Failed to configure client "
232 tlsv1_cred_free(cred);
236 if (tlsv1_set_private_key(cred, params->private_key,
237 params->private_key_passwd,
238 params->private_key_blob,
239 params->private_key_blob_len)) {
240 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
241 tlsv1_cred_free(cred);
245 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
246 params->dh_blob_len)) {
247 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
248 tlsv1_cred_free(cred);
252 if (tlsv1_client_set_cred(conn->client, cred) < 0) {
253 tlsv1_cred_free(cred);
257 tlsv1_client_set_time_checks(
258 conn->client, !(params->flags & TLS_CONN_DISABLE_TIME_CHECKS));
261 #else /* CONFIG_TLS_INTERNAL_CLIENT */
263 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
267 int tls_global_set_params(void *tls_ctx,
268 const struct tls_connection_params *params)
270 #ifdef CONFIG_TLS_INTERNAL_SERVER
271 struct tls_global *global = tls_ctx;
272 struct tlsv1_credentials *cred;
274 /* Currently, global parameters are only set when running in server
277 tlsv1_cred_free(global->server_cred);
278 global->server_cred = cred = tlsv1_cred_alloc();
282 if (tlsv1_set_ca_cert(cred, params->ca_cert, params->ca_cert_blob,
283 params->ca_cert_blob_len, params->ca_path)) {
284 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
289 if (tlsv1_set_cert(cred, params->client_cert, params->client_cert_blob,
290 params->client_cert_blob_len)) {
291 wpa_printf(MSG_INFO, "TLS: Failed to configure server "
296 if (tlsv1_set_private_key(cred, params->private_key,
297 params->private_key_passwd,
298 params->private_key_blob,
299 params->private_key_blob_len)) {
300 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
304 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
305 params->dh_blob_len)) {
306 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
311 #else /* CONFIG_TLS_INTERNAL_SERVER */
313 #endif /* CONFIG_TLS_INTERNAL_SERVER */
317 int tls_global_set_verify(void *tls_ctx, int check_crl)
319 struct tls_global *global = tls_ctx;
320 global->check_crl = check_crl;
325 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
328 #ifdef CONFIG_TLS_INTERNAL_SERVER
330 return tlsv1_server_set_verify(conn->server, verify_peer);
331 #endif /* CONFIG_TLS_INTERNAL_SERVER */
336 int tls_connection_get_keys(void *tls_ctx, struct tls_connection *conn,
337 struct tls_keys *keys)
339 #ifdef CONFIG_TLS_INTERNAL_CLIENT
341 return tlsv1_client_get_keys(conn->client, keys);
342 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
343 #ifdef CONFIG_TLS_INTERNAL_SERVER
345 return tlsv1_server_get_keys(conn->server, keys);
346 #endif /* CONFIG_TLS_INTERNAL_SERVER */
351 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
352 const char *label, int server_random_first,
353 u8 *out, size_t out_len)
355 #ifdef CONFIG_TLS_INTERNAL_CLIENT
357 return tlsv1_client_prf(conn->client, label,
361 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
362 #ifdef CONFIG_TLS_INTERNAL_SERVER
364 return tlsv1_server_prf(conn->server, label,
368 #endif /* CONFIG_TLS_INTERNAL_SERVER */
373 struct wpabuf * tls_connection_handshake(void *tls_ctx,
374 struct tls_connection *conn,
375 const struct wpabuf *in_data,
376 struct wpabuf **appl_data)
378 return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data,
383 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
384 struct tls_connection *conn,
385 const struct wpabuf *in_data,
386 struct wpabuf **appl_data,
389 #ifdef CONFIG_TLS_INTERNAL_CLIENT
391 size_t res_len, ad_len;
394 if (conn->client == NULL)
398 res = tlsv1_client_handshake(conn->client,
399 in_data ? wpabuf_head(in_data) : NULL,
400 in_data ? wpabuf_len(in_data) : 0,
401 &res_len, &ad, &ad_len, need_more_data);
404 out = wpabuf_alloc_ext_data(res, res_len);
412 *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
413 if (*appl_data == NULL)
421 #else /* CONFIG_TLS_INTERNAL_CLIENT */
423 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
427 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
428 struct tls_connection *conn,
429 const struct wpabuf *in_data,
430 struct wpabuf **appl_data)
432 #ifdef CONFIG_TLS_INTERNAL_SERVER
437 if (conn->server == NULL)
443 res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
444 wpabuf_len(in_data), &res_len);
445 if (res == NULL && tlsv1_server_established(conn->server))
446 return wpabuf_alloc(0);
449 out = wpabuf_alloc_ext_data(res, res_len);
456 #else /* CONFIG_TLS_INTERNAL_SERVER */
458 #endif /* CONFIG_TLS_INTERNAL_SERVER */
462 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
463 struct tls_connection *conn,
464 const struct wpabuf *in_data)
466 #ifdef CONFIG_TLS_INTERNAL_CLIENT
470 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
473 res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
481 wpabuf_put(buf, res);
484 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
485 #ifdef CONFIG_TLS_INTERNAL_SERVER
489 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
492 res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
500 wpabuf_put(buf, res);
503 #endif /* CONFIG_TLS_INTERNAL_SERVER */
508 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
509 struct tls_connection *conn,
510 const struct wpabuf *in_data)
512 return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL);
516 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
517 struct tls_connection *conn,
518 const struct wpabuf *in_data,
524 #ifdef CONFIG_TLS_INTERNAL_CLIENT
526 return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
530 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
531 #ifdef CONFIG_TLS_INTERNAL_SERVER
535 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
538 res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
546 wpabuf_put(buf, res);
549 #endif /* CONFIG_TLS_INTERNAL_SERVER */
554 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
556 #ifdef CONFIG_TLS_INTERNAL_CLIENT
558 return tlsv1_client_resumed(conn->client);
559 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
560 #ifdef CONFIG_TLS_INTERNAL_SERVER
562 return tlsv1_server_resumed(conn->server);
563 #endif /* CONFIG_TLS_INTERNAL_SERVER */
568 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
571 #ifdef CONFIG_TLS_INTERNAL_CLIENT
573 return tlsv1_client_set_cipher_list(conn->client, ciphers);
574 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
575 #ifdef CONFIG_TLS_INTERNAL_SERVER
577 return tlsv1_server_set_cipher_list(conn->server, ciphers);
578 #endif /* CONFIG_TLS_INTERNAL_SERVER */
583 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
584 char *buf, size_t buflen)
588 #ifdef CONFIG_TLS_INTERNAL_CLIENT
590 return tlsv1_client_get_cipher(conn->client, buf, buflen);
591 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
592 #ifdef CONFIG_TLS_INTERNAL_SERVER
594 return tlsv1_server_get_cipher(conn->server, buf, buflen);
595 #endif /* CONFIG_TLS_INTERNAL_SERVER */
600 int tls_connection_enable_workaround(void *tls_ctx,
601 struct tls_connection *conn)
607 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
608 int ext_type, const u8 *data,
611 #ifdef CONFIG_TLS_INTERNAL_CLIENT
613 return tlsv1_client_hello_ext(conn->client, ext_type,
616 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
621 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
627 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
633 int tls_connection_get_write_alerts(void *tls_ctx,
634 struct tls_connection *conn)
640 int tls_connection_get_keyblock_size(void *tls_ctx,
641 struct tls_connection *conn)
643 #ifdef CONFIG_TLS_INTERNAL_CLIENT
645 return tlsv1_client_get_keyblock_size(conn->client);
646 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
647 #ifdef CONFIG_TLS_INTERNAL_SERVER
649 return tlsv1_server_get_keyblock_size(conn->server);
650 #endif /* CONFIG_TLS_INTERNAL_SERVER */
655 unsigned int tls_capabilities(void *tls_ctx)
661 int tls_connection_set_session_ticket_cb(void *tls_ctx,
662 struct tls_connection *conn,
663 tls_session_ticket_cb cb,
666 #ifdef CONFIG_TLS_INTERNAL_CLIENT
668 tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx);
671 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
672 #ifdef CONFIG_TLS_INTERNAL_SERVER
674 tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx);
677 #endif /* CONFIG_TLS_INTERNAL_SERVER */
682 int tls_get_library_version(char *buf, size_t buf_len)
684 return os_snprintf(buf, buf_len, "internal");