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 static 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 int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
438 const char *label, u8 *out, size_t out_len)
440 return tls_connection_prf(tls_ctx, conn, label, 0, 0, out, out_len);
444 int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
445 u8 *out, size_t out_len)
447 return tls_connection_prf(tls_ctx, conn, "key expansion", 1, 1, out,
452 struct wpabuf * tls_connection_handshake(void *tls_ctx,
453 struct tls_connection *conn,
454 const struct wpabuf *in_data,
455 struct wpabuf **appl_data)
457 return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data,
462 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
463 struct tls_connection *conn,
464 const struct wpabuf *in_data,
465 struct wpabuf **appl_data,
468 #ifdef CONFIG_TLS_INTERNAL_CLIENT
470 size_t res_len, ad_len;
473 if (conn->client == NULL)
477 res = tlsv1_client_handshake(conn->client,
478 in_data ? wpabuf_head(in_data) : NULL,
479 in_data ? wpabuf_len(in_data) : 0,
480 &res_len, &ad, &ad_len, need_more_data);
483 out = wpabuf_alloc_ext_data(res, res_len);
491 *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
492 if (*appl_data == NULL)
500 #else /* CONFIG_TLS_INTERNAL_CLIENT */
502 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
506 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
507 struct tls_connection *conn,
508 const struct wpabuf *in_data,
509 struct wpabuf **appl_data)
511 #ifdef CONFIG_TLS_INTERNAL_SERVER
516 if (conn->server == NULL)
522 res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
523 wpabuf_len(in_data), &res_len);
524 if (res == NULL && tlsv1_server_established(conn->server))
525 return wpabuf_alloc(0);
528 out = wpabuf_alloc_ext_data(res, res_len);
535 #else /* CONFIG_TLS_INTERNAL_SERVER */
537 #endif /* CONFIG_TLS_INTERNAL_SERVER */
541 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
542 struct tls_connection *conn,
543 const struct wpabuf *in_data)
545 #ifdef CONFIG_TLS_INTERNAL_CLIENT
549 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
552 res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
560 wpabuf_put(buf, res);
563 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
564 #ifdef CONFIG_TLS_INTERNAL_SERVER
568 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
571 res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
579 wpabuf_put(buf, res);
582 #endif /* CONFIG_TLS_INTERNAL_SERVER */
587 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
588 struct tls_connection *conn,
589 const struct wpabuf *in_data)
591 return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL);
595 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
596 struct tls_connection *conn,
597 const struct wpabuf *in_data,
603 #ifdef CONFIG_TLS_INTERNAL_CLIENT
605 return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
609 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
610 #ifdef CONFIG_TLS_INTERNAL_SERVER
614 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
617 res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
625 wpabuf_put(buf, res);
628 #endif /* CONFIG_TLS_INTERNAL_SERVER */
633 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
635 #ifdef CONFIG_TLS_INTERNAL_CLIENT
637 return tlsv1_client_resumed(conn->client);
638 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
639 #ifdef CONFIG_TLS_INTERNAL_SERVER
641 return tlsv1_server_resumed(conn->server);
642 #endif /* CONFIG_TLS_INTERNAL_SERVER */
647 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
650 #ifdef CONFIG_TLS_INTERNAL_CLIENT
652 return tlsv1_client_set_cipher_list(conn->client, ciphers);
653 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
654 #ifdef CONFIG_TLS_INTERNAL_SERVER
656 return tlsv1_server_set_cipher_list(conn->server, ciphers);
657 #endif /* CONFIG_TLS_INTERNAL_SERVER */
662 int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
663 char *buf, size_t buflen)
667 #ifdef CONFIG_TLS_INTERNAL_CLIENT
669 return tlsv1_client_get_version(conn->client, buf, buflen);
670 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
675 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
676 char *buf, size_t buflen)
680 #ifdef CONFIG_TLS_INTERNAL_CLIENT
682 return tlsv1_client_get_cipher(conn->client, buf, buflen);
683 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
684 #ifdef CONFIG_TLS_INTERNAL_SERVER
686 return tlsv1_server_get_cipher(conn->server, buf, buflen);
687 #endif /* CONFIG_TLS_INTERNAL_SERVER */
692 int tls_connection_enable_workaround(void *tls_ctx,
693 struct tls_connection *conn)
699 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
700 int ext_type, const u8 *data,
703 #ifdef CONFIG_TLS_INTERNAL_CLIENT
705 return tlsv1_client_hello_ext(conn->client, ext_type,
708 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
713 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
719 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
725 int tls_connection_get_write_alerts(void *tls_ctx,
726 struct tls_connection *conn)
732 int tls_connection_set_session_ticket_cb(void *tls_ctx,
733 struct tls_connection *conn,
734 tls_session_ticket_cb cb,
737 #ifdef CONFIG_TLS_INTERNAL_CLIENT
739 tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx);
742 #endif /* CONFIG_TLS_INTERNAL_CLIENT */
743 #ifdef CONFIG_TLS_INTERNAL_SERVER
745 tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx);
748 #endif /* CONFIG_TLS_INTERNAL_SERVER */
753 int tls_get_library_version(char *buf, size_t buf_len)
755 return os_snprintf(buf, buf_len, "internal");
759 void tls_connection_set_success_data(struct tls_connection *conn,
765 void tls_connection_set_success_data_resumed(struct tls_connection *conn)
770 const struct wpabuf *
771 tls_connection_get_success_data(struct tls_connection *conn)
777 void tls_connection_remove_session(struct tls_connection *conn)