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;
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 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
182 #ifdef CONFIG_TLS_INTERNAL_CLIENT
184 return tlsv1_client_shutdown(conn->client);
185 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
186 #ifdef CONFIG_TLS_INTERNAL_SERVER
188 return tlsv1_server_shutdown(conn->server);
189 #endif /* CONFIG_TLS_INTERNAL_SERVER */
194 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
195 const struct tls_connection_params *params)
197 #ifdef CONFIG_TLS_INTERNAL_CLIENT
198 struct tlsv1_credentials *cred;
200 if (conn->client == NULL)
203 if (params->flags & TLS_CONN_EXT_CERT_CHECK) {
205 "TLS: tls_ext_cert_check=1 not supported");
209 cred = tlsv1_cred_alloc();
213 if (params->subject_match) {
214 wpa_printf(MSG_INFO, "TLS: subject_match not supported");
215 tlsv1_cred_free(cred);
219 if (params->altsubject_match) {
220 wpa_printf(MSG_INFO, "TLS: altsubject_match not supported");
221 tlsv1_cred_free(cred);
225 if (params->suffix_match) {
226 wpa_printf(MSG_INFO, "TLS: suffix_match not supported");
227 tlsv1_cred_free(cred);
231 if (params->domain_match) {
232 wpa_printf(MSG_INFO, "TLS: domain_match not supported");
233 tlsv1_cred_free(cred);
237 if (params->openssl_ciphers) {
238 wpa_printf(MSG_INFO, "TLS: openssl_ciphers not supported");
239 tlsv1_cred_free(cred);
243 if (tlsv1_set_ca_cert(cred, params->ca_cert,
244 params->ca_cert_blob, params->ca_cert_blob_len,
246 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
248 tlsv1_cred_free(cred);
252 if (tlsv1_set_cert(cred, params->client_cert,
253 params->client_cert_blob,
254 params->client_cert_blob_len)) {
255 wpa_printf(MSG_INFO, "TLS: Failed to configure client "
257 tlsv1_cred_free(cred);
261 if (tlsv1_set_private_key(cred, params->private_key,
262 params->private_key_passwd,
263 params->private_key_blob,
264 params->private_key_blob_len)) {
265 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
266 tlsv1_cred_free(cred);
270 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
271 params->dh_blob_len)) {
272 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
273 tlsv1_cred_free(cred);
277 if (tlsv1_client_set_cred(conn->client, cred) < 0) {
278 tlsv1_cred_free(cred);
282 tlsv1_client_set_flags(conn->client, params->flags);
285 #else /* CONFIG_TLS_INTERNAL_CLIENT */
287 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
291 int tls_global_set_params(void *tls_ctx,
292 const struct tls_connection_params *params)
294 #ifdef CONFIG_TLS_INTERNAL_SERVER
295 struct tls_global *global = tls_ctx;
296 struct tlsv1_credentials *cred;
298 /* Currently, global parameters are only set when running in server
301 tlsv1_cred_free(global->server_cred);
302 global->server_cred = cred = tlsv1_cred_alloc();
306 if (tlsv1_set_ca_cert(cred, params->ca_cert, params->ca_cert_blob,
307 params->ca_cert_blob_len, params->ca_path)) {
308 wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA "
313 if (tlsv1_set_cert(cred, params->client_cert, params->client_cert_blob,
314 params->client_cert_blob_len)) {
315 wpa_printf(MSG_INFO, "TLS: Failed to configure server "
320 if (tlsv1_set_private_key(cred, params->private_key,
321 params->private_key_passwd,
322 params->private_key_blob,
323 params->private_key_blob_len)) {
324 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
328 if (tlsv1_set_dhparams(cred, params->dh_file, params->dh_blob,
329 params->dh_blob_len)) {
330 wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters");
334 if (params->ocsp_stapling_response)
335 cred->ocsp_stapling_response =
336 os_strdup(params->ocsp_stapling_response);
337 if (params->ocsp_stapling_response_multi)
338 cred->ocsp_stapling_response_multi =
339 os_strdup(params->ocsp_stapling_response_multi);
342 #else /* CONFIG_TLS_INTERNAL_SERVER */
344 #endif /* CONFIG_TLS_INTERNAL_SERVER */
348 int tls_global_set_verify(void *tls_ctx, int check_crl)
350 struct tls_global *global = tls_ctx;
351 global->check_crl = check_crl;
356 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
357 int verify_peer, unsigned int flags,
358 const u8 *session_ctx, size_t session_ctx_len)
360 #ifdef CONFIG_TLS_INTERNAL_SERVER
362 return tlsv1_server_set_verify(conn->server, verify_peer);
363 #endif /* CONFIG_TLS_INTERNAL_SERVER */
368 int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn,
369 struct tls_random *data)
371 #ifdef CONFIG_TLS_INTERNAL_CLIENT
373 return tlsv1_client_get_random(conn->client, data);
374 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
375 #ifdef CONFIG_TLS_INTERNAL_SERVER
377 return tlsv1_server_get_random(conn->server, data);
378 #endif /* CONFIG_TLS_INTERNAL_SERVER */
383 static int tls_get_keyblock_size(struct tls_connection *conn)
385 #ifdef CONFIG_TLS_INTERNAL_CLIENT
387 return tlsv1_client_get_keyblock_size(conn->client);
388 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
389 #ifdef CONFIG_TLS_INTERNAL_SERVER
391 return tlsv1_server_get_keyblock_size(conn->server);
392 #endif /* CONFIG_TLS_INTERNAL_SERVER */
397 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
398 const char *label, int server_random_first,
399 int skip_keyblock, u8 *out, size_t out_len)
401 int ret = -1, skip = 0;
406 skip = tls_get_keyblock_size(conn);
409 tmp_out = os_malloc(skip + out_len);
415 #ifdef CONFIG_TLS_INTERNAL_CLIENT
417 ret = tlsv1_client_prf(conn->client, label,
419 _out, skip + out_len);
421 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
422 #ifdef CONFIG_TLS_INTERNAL_SERVER
424 ret = tlsv1_server_prf(conn->server, label,
426 _out, skip + out_len);
428 #endif /* CONFIG_TLS_INTERNAL_SERVER */
429 if (ret == 0 && skip_keyblock)
430 os_memcpy(out, _out + skip, out_len);
431 bin_clear_free(tmp_out, skip);
437 struct wpabuf * tls_connection_handshake(void *tls_ctx,
438 struct tls_connection *conn,
439 const struct wpabuf *in_data,
440 struct wpabuf **appl_data)
442 return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data,
447 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
448 struct tls_connection *conn,
449 const struct wpabuf *in_data,
450 struct wpabuf **appl_data,
453 #ifdef CONFIG_TLS_INTERNAL_CLIENT
455 size_t res_len, ad_len;
458 if (conn->client == NULL)
462 res = tlsv1_client_handshake(conn->client,
463 in_data ? wpabuf_head(in_data) : NULL,
464 in_data ? wpabuf_len(in_data) : 0,
465 &res_len, &ad, &ad_len, need_more_data);
468 out = wpabuf_alloc_ext_data(res, res_len);
476 *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
477 if (*appl_data == NULL)
485 #else /* CONFIG_TLS_INTERNAL_CLIENT */
487 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
491 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
492 struct tls_connection *conn,
493 const struct wpabuf *in_data,
494 struct wpabuf **appl_data)
496 #ifdef CONFIG_TLS_INTERNAL_SERVER
501 if (conn->server == NULL)
507 res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
508 wpabuf_len(in_data), &res_len);
509 if (res == NULL && tlsv1_server_established(conn->server))
510 return wpabuf_alloc(0);
513 out = wpabuf_alloc_ext_data(res, res_len);
520 #else /* CONFIG_TLS_INTERNAL_SERVER */
522 #endif /* CONFIG_TLS_INTERNAL_SERVER */
526 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
527 struct tls_connection *conn,
528 const struct wpabuf *in_data)
530 #ifdef CONFIG_TLS_INTERNAL_CLIENT
534 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
537 res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
545 wpabuf_put(buf, res);
548 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
549 #ifdef CONFIG_TLS_INTERNAL_SERVER
553 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
556 res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
564 wpabuf_put(buf, res);
567 #endif /* CONFIG_TLS_INTERNAL_SERVER */
572 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
573 struct tls_connection *conn,
574 const struct wpabuf *in_data)
576 return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL);
580 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
581 struct tls_connection *conn,
582 const struct wpabuf *in_data,
588 #ifdef CONFIG_TLS_INTERNAL_CLIENT
590 return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
594 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
595 #ifdef CONFIG_TLS_INTERNAL_SERVER
599 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
602 res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
610 wpabuf_put(buf, res);
613 #endif /* CONFIG_TLS_INTERNAL_SERVER */
618 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
620 #ifdef CONFIG_TLS_INTERNAL_CLIENT
622 return tlsv1_client_resumed(conn->client);
623 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
624 #ifdef CONFIG_TLS_INTERNAL_SERVER
626 return tlsv1_server_resumed(conn->server);
627 #endif /* CONFIG_TLS_INTERNAL_SERVER */
632 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
635 #ifdef CONFIG_TLS_INTERNAL_CLIENT
637 return tlsv1_client_set_cipher_list(conn->client, ciphers);
638 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
639 #ifdef CONFIG_TLS_INTERNAL_SERVER
641 return tlsv1_server_set_cipher_list(conn->server, ciphers);
642 #endif /* CONFIG_TLS_INTERNAL_SERVER */
647 int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
648 char *buf, size_t buflen)
652 #ifdef CONFIG_TLS_INTERNAL_CLIENT
654 return tlsv1_client_get_version(conn->client, buf, buflen);
655 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
660 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
661 char *buf, size_t buflen)
665 #ifdef CONFIG_TLS_INTERNAL_CLIENT
667 return tlsv1_client_get_cipher(conn->client, buf, buflen);
668 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
669 #ifdef CONFIG_TLS_INTERNAL_SERVER
671 return tlsv1_server_get_cipher(conn->server, buf, buflen);
672 #endif /* CONFIG_TLS_INTERNAL_SERVER */
677 int tls_connection_enable_workaround(void *tls_ctx,
678 struct tls_connection *conn)
684 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
685 int ext_type, const u8 *data,
688 #ifdef CONFIG_TLS_INTERNAL_CLIENT
690 return tlsv1_client_hello_ext(conn->client, ext_type,
693 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
698 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
704 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
710 int tls_connection_get_write_alerts(void *tls_ctx,
711 struct tls_connection *conn)
717 int tls_connection_set_session_ticket_cb(void *tls_ctx,
718 struct tls_connection *conn,
719 tls_session_ticket_cb cb,
722 #ifdef CONFIG_TLS_INTERNAL_CLIENT
724 tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx);
727 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
728 #ifdef CONFIG_TLS_INTERNAL_SERVER
730 tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx);
733 #endif /* CONFIG_TLS_INTERNAL_SERVER */
738 int tls_get_library_version(char *buf, size_t buf_len)
740 return os_snprintf(buf, buf_len, "internal");
744 void tls_connection_set_success_data(struct tls_connection *conn,
750 void tls_connection_set_success_data_resumed(struct tls_connection *conn)
755 const struct wpabuf *
756 tls_connection_get_success_data(struct tls_connection *conn)
762 void tls_connection_remove_session(struct tls_connection *conn)