2 * SSL/TLS interface functions for GnuTLS
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.
10 #include <gnutls/gnutls.h>
11 #include <gnutls/x509.h>
13 #include <gnutls/pkcs12.h>
14 #endif /* PKCS12_FUNCS */
15 #if GNUTLS_VERSION_NUMBER >= 0x030103
16 #include <gnutls/ocsp.h>
20 #include "crypto/crypto.h"
24 static int tls_gnutls_ref_count = 0;
27 /* Data for session resumption */
29 size_t session_data_size;
34 gnutls_certificate_credentials_t xcred;
36 void (*event_cb)(void *ctx, enum tls_event ev,
37 union tls_event_data *data);
42 struct tls_connection {
43 struct tls_global *global;
44 gnutls_session_t session;
45 int read_alerts, write_alerts, failed;
47 u8 *pre_shared_secret;
48 size_t pre_shared_secret_len;
51 unsigned int disable_time_checks:1;
53 struct wpabuf *push_buf;
54 struct wpabuf *pull_buf;
55 const u8 *pull_buf_offset;
58 gnutls_certificate_credentials_t xcred;
66 static int tls_connection_verify_peer(gnutls_session_t session);
69 static void tls_log_func(int level, const char *msg)
72 if (level == 6 || level == 7) {
73 /* These levels seem to be mostly I/O debug and msg dumps */
82 while (*pos != '\0') {
89 wpa_printf(level > 3 ? MSG_MSGDUMP : MSG_DEBUG,
90 "gnutls<%d> %s", level, s);
95 void * tls_init(const struct tls_config *conf)
97 struct tls_global *global;
99 if (tls_gnutls_ref_count == 0) {
100 wpa_printf(MSG_DEBUG,
101 "GnuTLS: Library version %s (runtime) - %s (build)",
102 gnutls_check_version(NULL), GNUTLS_VERSION);
105 global = os_zalloc(sizeof(*global));
109 if (tls_gnutls_ref_count == 0 && gnutls_global_init() < 0) {
113 tls_gnutls_ref_count++;
115 gnutls_global_set_log_function(tls_log_func);
116 if (wpa_debug_show_keys)
117 gnutls_global_set_log_level(11);
120 global->event_cb = conf->event_cb;
121 global->cb_ctx = conf->cb_ctx;
122 global->cert_in_cb = conf->cert_in_cb;
129 void tls_deinit(void *ssl_ctx)
131 struct tls_global *global = ssl_ctx;
133 if (global->params_set)
134 gnutls_certificate_free_credentials(global->xcred);
135 os_free(global->session_data);
139 tls_gnutls_ref_count--;
140 if (tls_gnutls_ref_count == 0)
141 gnutls_global_deinit();
145 int tls_get_errors(void *ssl_ctx)
151 static ssize_t tls_pull_func(gnutls_transport_ptr_t ptr, void *buf,
154 struct tls_connection *conn = (struct tls_connection *) ptr;
156 if (conn->pull_buf == NULL) {
161 end = wpabuf_head_u8(conn->pull_buf) + wpabuf_len(conn->pull_buf);
162 if ((size_t) (end - conn->pull_buf_offset) < len)
163 len = end - conn->pull_buf_offset;
164 os_memcpy(buf, conn->pull_buf_offset, len);
165 conn->pull_buf_offset += len;
166 if (conn->pull_buf_offset == end) {
167 wpa_printf(MSG_DEBUG, "%s - pull_buf consumed", __func__);
168 wpabuf_free(conn->pull_buf);
169 conn->pull_buf = NULL;
170 conn->pull_buf_offset = NULL;
172 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in pull_buf",
174 (unsigned long) (end - conn->pull_buf_offset));
180 static ssize_t tls_push_func(gnutls_transport_ptr_t ptr, const void *buf,
183 struct tls_connection *conn = (struct tls_connection *) ptr;
185 if (wpabuf_resize(&conn->push_buf, len) < 0) {
189 wpabuf_put_data(conn->push_buf, buf, len);
195 static int tls_gnutls_init_session(struct tls_global *global,
196 struct tls_connection *conn)
201 ret = gnutls_init(&conn->session,
202 global->server ? GNUTLS_SERVER : GNUTLS_CLIENT);
204 wpa_printf(MSG_INFO, "TLS: Failed to initialize new TLS "
205 "connection: %s", gnutls_strerror(ret));
209 ret = gnutls_set_default_priority(conn->session);
213 ret = gnutls_priority_set_direct(conn->session, "NORMAL:-VERS-SSL3.0",
216 wpa_printf(MSG_ERROR, "GnuTLS: Priority string failure at "
221 gnutls_transport_set_pull_function(conn->session, tls_pull_func);
222 gnutls_transport_set_push_function(conn->session, tls_push_func);
223 gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr_t) conn);
224 gnutls_session_set_ptr(conn->session, conn);
229 wpa_printf(MSG_INFO, "TLS: Failed to setup new TLS connection: %s",
230 gnutls_strerror(ret));
231 gnutls_deinit(conn->session);
236 struct tls_connection * tls_connection_init(void *ssl_ctx)
238 struct tls_global *global = ssl_ctx;
239 struct tls_connection *conn;
242 conn = os_zalloc(sizeof(*conn));
245 conn->global = global;
247 if (tls_gnutls_init_session(global, conn)) {
252 if (global->params_set) {
253 ret = gnutls_credentials_set(conn->session,
254 GNUTLS_CRD_CERTIFICATE,
257 wpa_printf(MSG_INFO, "Failed to configure "
258 "credentials: %s", gnutls_strerror(ret));
264 if (gnutls_certificate_allocate_credentials(&conn->xcred)) {
273 void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
278 gnutls_certificate_free_credentials(conn->xcred);
279 gnutls_deinit(conn->session);
280 os_free(conn->pre_shared_secret);
281 wpabuf_free(conn->push_buf);
282 wpabuf_free(conn->pull_buf);
283 os_free(conn->suffix_match);
284 os_free(conn->domain_match);
289 int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
291 return conn ? conn->established : 0;
295 int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
297 struct tls_global *global = ssl_ctx;
303 /* Shutdown previous TLS connection without notifying the peer
304 * because the connection was already terminated in practice
305 * and "close notify" shutdown alert would confuse AS. */
306 gnutls_bye(conn->session, GNUTLS_SHUT_RDWR);
307 wpabuf_free(conn->push_buf);
308 conn->push_buf = NULL;
309 conn->established = 0;
311 gnutls_deinit(conn->session);
312 if (tls_gnutls_init_session(global, conn)) {
313 wpa_printf(MSG_INFO, "GnuTLS: Failed to preparare new session "
314 "for session resumption use");
318 ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE,
319 conn->params_set ? conn->xcred :
322 wpa_printf(MSG_INFO, "GnuTLS: Failed to configure credentials "
323 "for session resumption: %s", gnutls_strerror(ret));
327 if (global->session_data) {
328 ret = gnutls_session_set_data(conn->session,
329 global->session_data,
330 global->session_data_size);
332 wpa_printf(MSG_INFO, "GnuTLS: Failed to set session "
333 "data: %s", gnutls_strerror(ret));
342 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
343 const struct tls_connection_params *params)
347 if (conn == NULL || params == NULL)
350 if (params->subject_match) {
351 wpa_printf(MSG_INFO, "GnuTLS: subject_match not supported");
355 if (params->altsubject_match) {
356 wpa_printf(MSG_INFO, "GnuTLS: altsubject_match not supported");
360 os_free(conn->suffix_match);
361 conn->suffix_match = NULL;
362 if (params->suffix_match) {
363 conn->suffix_match = os_strdup(params->suffix_match);
364 if (conn->suffix_match == NULL)
368 #if GNUTLS_VERSION_NUMBER >= 0x030300
369 os_free(conn->domain_match);
370 conn->domain_match = NULL;
371 if (params->domain_match) {
372 conn->domain_match = os_strdup(params->domain_match);
373 if (conn->domain_match == NULL)
377 if (params->domain_match) {
378 wpa_printf(MSG_INFO, "GnuTLS: domain_match not supported");
381 #endif /* >= 3.3.0 */
383 conn->flags = params->flags;
385 if (params->openssl_ciphers) {
386 wpa_printf(MSG_INFO, "GnuTLS: openssl_ciphers not supported");
390 /* TODO: gnutls_certificate_set_verify_flags(xcred, flags);
391 * to force peer validation(?) */
393 if (params->ca_cert) {
394 wpa_printf(MSG_DEBUG, "GnuTLS: Try to parse %s in DER format",
396 ret = gnutls_certificate_set_x509_trust_file(
397 conn->xcred, params->ca_cert, GNUTLS_X509_FMT_DER);
399 wpa_printf(MSG_DEBUG,
400 "GnuTLS: Failed to read CA cert '%s' in DER format (%s) - try in PEM format",
402 gnutls_strerror(ret));
403 ret = gnutls_certificate_set_x509_trust_file(
404 conn->xcred, params->ca_cert,
405 GNUTLS_X509_FMT_PEM);
407 wpa_printf(MSG_DEBUG,
408 "Failed to read CA cert '%s' in PEM format: %s",
410 gnutls_strerror(ret));
414 } else if (params->ca_cert_blob) {
417 ca.data = (unsigned char *) params->ca_cert_blob;
418 ca.size = params->ca_cert_blob_len;
420 ret = gnutls_certificate_set_x509_trust_mem(
421 conn->xcred, &ca, GNUTLS_X509_FMT_DER);
423 wpa_printf(MSG_DEBUG,
424 "Failed to parse CA cert in DER format: %s",
425 gnutls_strerror(ret));
426 ret = gnutls_certificate_set_x509_trust_mem(
427 conn->xcred, &ca, GNUTLS_X509_FMT_PEM);
429 wpa_printf(MSG_DEBUG,
430 "Failed to parse CA cert in PEM format: %s",
431 gnutls_strerror(ret));
435 } else if (params->ca_path) {
436 wpa_printf(MSG_INFO, "GnuTLS: ca_path not supported");
440 conn->disable_time_checks = 0;
441 if (params->ca_cert || params->ca_cert_blob) {
442 conn->verify_peer = 1;
443 gnutls_certificate_set_verify_function(
444 conn->xcred, tls_connection_verify_peer);
446 if (params->flags & TLS_CONN_ALLOW_SIGN_RSA_MD5) {
447 gnutls_certificate_set_verify_flags(
448 conn->xcred, GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
451 if (params->flags & TLS_CONN_DISABLE_TIME_CHECKS) {
452 conn->disable_time_checks = 1;
453 gnutls_certificate_set_verify_flags(
455 GNUTLS_VERIFY_DISABLE_TIME_CHECKS);
459 if (params->client_cert && params->private_key) {
460 #if GNUTLS_VERSION_NUMBER >= 0x03010b
461 ret = gnutls_certificate_set_x509_key_file2(
462 conn->xcred, params->client_cert, params->private_key,
463 GNUTLS_X509_FMT_DER, params->private_key_passwd, 0);
465 /* private_key_passwd not (easily) supported here */
466 ret = gnutls_certificate_set_x509_key_file(
467 conn->xcred, params->client_cert, params->private_key,
468 GNUTLS_X509_FMT_DER);
471 wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
472 "in DER format: %s", gnutls_strerror(ret));
473 #if GNUTLS_VERSION_NUMBER >= 0x03010b
474 ret = gnutls_certificate_set_x509_key_file2(
475 conn->xcred, params->client_cert,
476 params->private_key, GNUTLS_X509_FMT_PEM,
477 params->private_key_passwd, 0);
479 ret = gnutls_certificate_set_x509_key_file(
480 conn->xcred, params->client_cert,
481 params->private_key, GNUTLS_X509_FMT_PEM);
484 wpa_printf(MSG_DEBUG, "Failed to read client "
485 "cert/key in PEM format: %s",
486 gnutls_strerror(ret));
490 } else if (params->private_key) {
493 /* Try to load in PKCS#12 format */
494 ret = gnutls_certificate_set_x509_simple_pkcs12_file(
495 conn->xcred, params->private_key, GNUTLS_X509_FMT_DER,
496 params->private_key_passwd);
498 wpa_printf(MSG_DEBUG, "Failed to load private_key in "
499 "PKCS#12 format: %s", gnutls_strerror(ret));
503 #endif /* PKCS12_FUNCS */
506 wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
510 } else if (params->client_cert_blob && params->private_key_blob) {
511 gnutls_datum_t cert, key;
513 cert.data = (unsigned char *) params->client_cert_blob;
514 cert.size = params->client_cert_blob_len;
515 key.data = (unsigned char *) params->private_key_blob;
516 key.size = params->private_key_blob_len;
518 #if GNUTLS_VERSION_NUMBER >= 0x03010b
519 ret = gnutls_certificate_set_x509_key_mem2(
520 conn->xcred, &cert, &key, GNUTLS_X509_FMT_DER,
521 params->private_key_passwd, 0);
523 /* private_key_passwd not (easily) supported here */
524 ret = gnutls_certificate_set_x509_key_mem(
525 conn->xcred, &cert, &key, GNUTLS_X509_FMT_DER);
528 wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
529 "in DER format: %s", gnutls_strerror(ret));
530 #if GNUTLS_VERSION_NUMBER >= 0x03010b
531 ret = gnutls_certificate_set_x509_key_mem2(
532 conn->xcred, &cert, &key, GNUTLS_X509_FMT_PEM,
533 params->private_key_passwd, 0);
535 /* private_key_passwd not (easily) supported here */
536 ret = gnutls_certificate_set_x509_key_mem(
537 conn->xcred, &cert, &key, GNUTLS_X509_FMT_PEM);
540 wpa_printf(MSG_DEBUG, "Failed to read client "
541 "cert/key in PEM format: %s",
542 gnutls_strerror(ret));
546 } else if (params->private_key_blob) {
550 key.data = (unsigned char *) params->private_key_blob;
551 key.size = params->private_key_blob_len;
553 /* Try to load in PKCS#12 format */
554 ret = gnutls_certificate_set_x509_simple_pkcs12_mem(
555 conn->xcred, &key, GNUTLS_X509_FMT_DER,
556 params->private_key_passwd);
558 wpa_printf(MSG_DEBUG, "Failed to load private_key in "
559 "PKCS#12 format: %s", gnutls_strerror(ret));
562 #else /* PKCS12_FUNCS */
563 wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not included");
565 #endif /* PKCS12_FUNCS */
568 #if GNUTLS_VERSION_NUMBER >= 0x030103
569 if (params->flags & (TLS_CONN_REQUEST_OCSP | TLS_CONN_REQUIRE_OCSP)) {
570 ret = gnutls_ocsp_status_request_enable_client(conn->session,
572 if (ret != GNUTLS_E_SUCCESS) {
574 "GnuTLS: Failed to enable OCSP client");
579 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
581 "GnuTLS: OCSP not supported by this version of GnuTLS");
586 conn->params_set = 1;
588 ret = gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE,
591 wpa_printf(MSG_INFO, "Failed to configure credentials: %s",
592 gnutls_strerror(ret));
599 int tls_global_set_params(void *tls_ctx,
600 const struct tls_connection_params *params)
602 struct tls_global *global = tls_ctx;
605 /* Currently, global parameters are only set when running in server
609 if (global->params_set) {
610 gnutls_certificate_free_credentials(global->xcred);
611 global->params_set = 0;
614 ret = gnutls_certificate_allocate_credentials(&global->xcred);
616 wpa_printf(MSG_DEBUG, "Failed to allocate global credentials "
617 "%s", gnutls_strerror(ret));
621 if (params->ca_cert) {
622 ret = gnutls_certificate_set_x509_trust_file(
623 global->xcred, params->ca_cert, GNUTLS_X509_FMT_DER);
625 wpa_printf(MSG_DEBUG, "Failed to read CA cert '%s' "
626 "in DER format: %s", params->ca_cert,
627 gnutls_strerror(ret));
628 ret = gnutls_certificate_set_x509_trust_file(
629 global->xcred, params->ca_cert,
630 GNUTLS_X509_FMT_PEM);
632 wpa_printf(MSG_DEBUG, "Failed to read CA cert "
633 "'%s' in PEM format: %s",
635 gnutls_strerror(ret));
640 if (params->flags & TLS_CONN_ALLOW_SIGN_RSA_MD5) {
641 gnutls_certificate_set_verify_flags(
643 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
646 if (params->flags & TLS_CONN_DISABLE_TIME_CHECKS) {
647 gnutls_certificate_set_verify_flags(
649 GNUTLS_VERIFY_DISABLE_TIME_CHECKS);
653 if (params->client_cert && params->private_key) {
654 /* TODO: private_key_passwd? */
655 ret = gnutls_certificate_set_x509_key_file(
656 global->xcred, params->client_cert,
657 params->private_key, GNUTLS_X509_FMT_DER);
659 wpa_printf(MSG_DEBUG, "Failed to read client cert/key "
660 "in DER format: %s", gnutls_strerror(ret));
661 ret = gnutls_certificate_set_x509_key_file(
662 global->xcred, params->client_cert,
663 params->private_key, GNUTLS_X509_FMT_PEM);
665 wpa_printf(MSG_DEBUG, "Failed to read client "
666 "cert/key in PEM format: %s",
667 gnutls_strerror(ret));
671 } else if (params->private_key) {
674 /* Try to load in PKCS#12 format */
675 ret = gnutls_certificate_set_x509_simple_pkcs12_file(
676 global->xcred, params->private_key,
677 GNUTLS_X509_FMT_DER, params->private_key_passwd);
679 wpa_printf(MSG_DEBUG, "Failed to load private_key in "
680 "PKCS#12 format: %s", gnutls_strerror(ret));
684 #endif /* PKCS12_FUNCS */
687 wpa_printf(MSG_DEBUG, "GnuTLS: PKCS#12 support not "
693 global->params_set = 1;
698 gnutls_certificate_free_credentials(global->xcred);
703 int tls_global_set_verify(void *ssl_ctx, int check_crl)
710 int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
711 int verify_peer, unsigned int flags,
712 const u8 *session_ctx, size_t session_ctx_len)
714 if (conn == NULL || conn->session == NULL)
717 conn->verify_peer = verify_peer;
718 gnutls_certificate_server_set_request(conn->session,
719 verify_peer ? GNUTLS_CERT_REQUIRE
720 : GNUTLS_CERT_REQUEST);
726 int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
727 struct tls_random *keys)
729 #if GNUTLS_VERSION_NUMBER >= 0x030012
730 gnutls_datum_t client, server;
732 if (conn == NULL || conn->session == NULL || keys == NULL)
735 os_memset(keys, 0, sizeof(*keys));
736 gnutls_session_get_random(conn->session, &client, &server);
737 keys->client_random = client.data;
738 keys->server_random = server.data;
739 keys->client_random_len = client.size;
740 keys->server_random_len = client.size;
749 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
750 const char *label, int server_random_first,
751 int skip_keyblock, u8 *out, size_t out_len)
753 if (conn == NULL || conn->session == NULL || skip_keyblock)
756 return gnutls_prf(conn->session, os_strlen(label), label,
757 server_random_first, 0, NULL, out_len, (char *) out);
761 static void gnutls_tls_fail_event(struct tls_connection *conn,
762 const gnutls_datum_t *cert, int depth,
763 const char *subject, const char *err_str,
764 enum tls_fail_reason reason)
766 union tls_event_data ev;
767 struct tls_global *global = conn->global;
768 struct wpabuf *cert_buf = NULL;
770 if (global->event_cb == NULL)
773 os_memset(&ev, 0, sizeof(ev));
774 ev.cert_fail.depth = depth;
775 ev.cert_fail.subject = subject ? subject : "";
776 ev.cert_fail.reason = reason;
777 ev.cert_fail.reason_txt = err_str;
779 cert_buf = wpabuf_alloc_copy(cert->data, cert->size);
780 ev.cert_fail.cert = cert_buf;
782 global->event_cb(global->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
783 wpabuf_free(cert_buf);
787 #if GNUTLS_VERSION_NUMBER < 0x030300
788 static int server_eku_purpose(gnutls_x509_crt_t cert)
794 size_t oid_size = sizeof(oid);
797 res = gnutls_x509_crt_get_key_purpose_oid(cert, i, oid,
799 if (res == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
801 /* No EKU - assume any use allowed */
808 wpa_printf(MSG_INFO, "GnuTLS: Failed to get EKU");
812 wpa_printf(MSG_DEBUG, "GnuTLS: Certificate purpose: %s", oid);
813 if (os_strcmp(oid, GNUTLS_KP_TLS_WWW_SERVER) == 0 ||
814 os_strcmp(oid, GNUTLS_KP_ANY) == 0)
823 static int check_ocsp(struct tls_connection *conn, gnutls_session_t session,
824 gnutls_alert_description_t *err)
826 #if GNUTLS_VERSION_NUMBER >= 0x030103
827 gnutls_datum_t response, buf;
828 gnutls_ocsp_resp_t resp;
829 unsigned int cert_status;
832 if (!(conn->flags & (TLS_CONN_REQUEST_OCSP | TLS_CONN_REQUIRE_OCSP)))
835 if (!gnutls_ocsp_status_request_is_checked(session, 0)) {
836 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
838 "GnuTLS: No valid OCSP response received");
842 wpa_printf(MSG_DEBUG,
843 "GnuTLS: Valid OCSP response was not received - continue since OCSP was not required");
848 * GnuTLS has already verified the OCSP response in
849 * check_ocsp_response() and rejected handshake if the certificate was
850 * found to be revoked. However, if the response indicates that the
851 * status is unknown, handshake continues and reaches here. We need to
852 * re-import the OCSP response to check for unknown certificate status,
853 * but we do not need to repeat gnutls_ocsp_resp_check_crt() and
854 * gnutls_ocsp_resp_verify_direct() calls.
857 res = gnutls_ocsp_status_request_get(session, &response);
858 if (res != GNUTLS_E_SUCCESS) {
860 "GnuTLS: OCSP response was received, but it was not valid");
864 if (gnutls_ocsp_resp_init(&resp) != GNUTLS_E_SUCCESS)
867 res = gnutls_ocsp_resp_import(resp, &response);
868 if (res != GNUTLS_E_SUCCESS) {
870 "GnuTLS: Could not parse received OCSP response: %s",
871 gnutls_strerror(res));
872 gnutls_ocsp_resp_deinit(resp);
876 res = gnutls_ocsp_resp_print(resp, GNUTLS_OCSP_PRINT_FULL, &buf);
877 if (res == GNUTLS_E_SUCCESS) {
878 wpa_printf(MSG_DEBUG, "GnuTLS: %s", buf.data);
879 gnutls_free(buf.data);
882 res = gnutls_ocsp_resp_get_single(resp, 0, NULL, NULL, NULL,
883 NULL, &cert_status, NULL,
885 gnutls_ocsp_resp_deinit(resp);
886 if (res != GNUTLS_E_SUCCESS) {
888 "GnuTLS: Failed to extract OCSP information: %s",
889 gnutls_strerror(res));
893 if (cert_status == GNUTLS_OCSP_CERT_GOOD) {
894 wpa_printf(MSG_DEBUG, "GnuTLS: OCSP cert status: good");
895 } else if (cert_status == GNUTLS_OCSP_CERT_REVOKED) {
896 wpa_printf(MSG_DEBUG,
897 "GnuTLS: OCSP cert status: revoked");
900 wpa_printf(MSG_DEBUG,
901 "GnuTLS: OCSP cert status: unknown");
902 if (conn->flags & TLS_CONN_REQUIRE_OCSP)
904 wpa_printf(MSG_DEBUG,
905 "GnuTLS: OCSP was not required, so allow connection to continue");
911 gnutls_tls_fail_event(conn, NULL, 0, NULL,
912 "bad certificate status response",
914 *err = GNUTLS_A_CERTIFICATE_REVOKED;
916 #else /* GnuTLS 3.1.3 or newer */
918 #endif /* GnuTLS 3.1.3 or newer */
922 static int tls_connection_verify_peer(gnutls_session_t session)
924 struct tls_connection *conn;
925 unsigned int status, num_certs, i;
927 const gnutls_datum_t *certs;
928 gnutls_x509_crt_t cert;
929 gnutls_alert_description_t err;
932 conn = gnutls_session_get_ptr(session);
933 if (!conn->verify_peer) {
934 wpa_printf(MSG_DEBUG,
935 "GnuTLS: No peer certificate verification enabled");
939 wpa_printf(MSG_DEBUG, "GnuTSL: Verifying peer certificate");
941 #if GNUTLS_VERSION_NUMBER >= 0x030300
943 gnutls_typed_vdata_st data[1];
944 unsigned int elements = 0;
946 os_memset(data, 0, sizeof(data));
947 if (!conn->global->server) {
948 data[elements].type = GNUTLS_DT_KEY_PURPOSE_OID;
949 data[elements].data = (void *) GNUTLS_KP_TLS_WWW_SERVER;
952 res = gnutls_certificate_verify_peers(session, data, 1,
956 res = gnutls_certificate_verify_peers2(session, &status);
959 wpa_printf(MSG_INFO, "TLS: Failed to verify peer "
960 "certificate chain");
961 err = GNUTLS_A_INTERNAL_ERROR;
965 #if GNUTLS_VERSION_NUMBER >= 0x030104
970 type = gnutls_certificate_type_get(session);
971 ret = gnutls_certificate_verification_status_print(status, type,
974 wpa_printf(MSG_DEBUG,
975 "GnuTLS: Failed to print verification status");
976 err = GNUTLS_A_INTERNAL_ERROR;
979 wpa_printf(MSG_DEBUG, "GnuTLS: %s", info.data);
980 gnutls_free(info.data);
982 #endif /* GnuTLS 3.1.4 or newer */
984 certs = gnutls_certificate_get_peers(session, &num_certs);
985 if (certs == NULL || num_certs == 0) {
986 wpa_printf(MSG_INFO, "TLS: No peer certificate chain received");
987 err = GNUTLS_A_UNKNOWN_CA;
991 if (conn->verify_peer && (status & GNUTLS_CERT_INVALID)) {
992 wpa_printf(MSG_INFO, "TLS: Peer certificate not trusted");
993 if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
994 wpa_printf(MSG_INFO, "TLS: Certificate uses insecure "
996 gnutls_tls_fail_event(conn, NULL, 0, NULL,
997 "certificate uses insecure algorithm",
998 TLS_FAIL_BAD_CERTIFICATE);
999 err = GNUTLS_A_INSUFFICIENT_SECURITY;
1002 if (status & GNUTLS_CERT_NOT_ACTIVATED) {
1003 wpa_printf(MSG_INFO, "TLS: Certificate not yet "
1005 gnutls_tls_fail_event(conn, NULL, 0, NULL,
1006 "certificate not yet valid",
1007 TLS_FAIL_NOT_YET_VALID);
1008 err = GNUTLS_A_CERTIFICATE_EXPIRED;
1011 if (status & GNUTLS_CERT_EXPIRED) {
1012 wpa_printf(MSG_INFO, "TLS: Certificate expired");
1013 gnutls_tls_fail_event(conn, NULL, 0, NULL,
1014 "certificate has expired",
1016 err = GNUTLS_A_CERTIFICATE_EXPIRED;
1019 gnutls_tls_fail_event(conn, NULL, 0, NULL,
1020 "untrusted certificate",
1021 TLS_FAIL_UNTRUSTED);
1022 err = GNUTLS_A_INTERNAL_ERROR;
1026 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
1027 wpa_printf(MSG_INFO, "TLS: Peer certificate does not have a "
1029 gnutls_tls_fail_event(conn, NULL, 0, NULL, "signed not found",
1030 TLS_FAIL_UNTRUSTED);
1031 err = GNUTLS_A_UNKNOWN_CA;
1035 if (status & GNUTLS_CERT_REVOKED) {
1036 wpa_printf(MSG_INFO, "TLS: Peer certificate has been revoked");
1037 gnutls_tls_fail_event(conn, NULL, 0, NULL,
1038 "certificate revoked",
1040 err = GNUTLS_A_CERTIFICATE_REVOKED;
1045 wpa_printf(MSG_INFO, "TLS: Unknown verification status: %d",
1047 err = GNUTLS_A_INTERNAL_ERROR;
1051 if (check_ocsp(conn, session, &err))
1056 for (i = 0; i < num_certs; i++) {
1059 if (gnutls_x509_crt_init(&cert) < 0) {
1060 wpa_printf(MSG_INFO, "TLS: Certificate initialization "
1062 err = GNUTLS_A_BAD_CERTIFICATE;
1066 if (gnutls_x509_crt_import(cert, &certs[i],
1067 GNUTLS_X509_FMT_DER) < 0) {
1068 wpa_printf(MSG_INFO, "TLS: Could not parse peer "
1069 "certificate %d/%d", i + 1, num_certs);
1070 gnutls_x509_crt_deinit(cert);
1071 err = GNUTLS_A_BAD_CERTIFICATE;
1075 gnutls_x509_crt_get_dn(cert, NULL, &len);
1077 buf = os_malloc(len + 1);
1079 buf[0] = buf[len] = '\0';
1080 gnutls_x509_crt_get_dn(cert, buf, &len);
1082 wpa_printf(MSG_DEBUG, "TLS: Peer cert chain %d/%d: %s",
1083 i + 1, num_certs, buf);
1085 if (conn->global->event_cb) {
1086 struct wpabuf *cert_buf = NULL;
1087 union tls_event_data ev;
1088 #ifdef CONFIG_SHA256
1092 #endif /* CONFIG_SHA256 */
1094 os_memset(&ev, 0, sizeof(ev));
1095 if (conn->global->cert_in_cb) {
1096 cert_buf = wpabuf_alloc_copy(certs[i].data,
1098 ev.peer_cert.cert = cert_buf;
1100 #ifdef CONFIG_SHA256
1101 _addr[0] = certs[i].data;
1102 _len[0] = certs[i].size;
1103 if (sha256_vector(1, _addr, _len, hash) == 0) {
1104 ev.peer_cert.hash = hash;
1105 ev.peer_cert.hash_len = sizeof(hash);
1107 #endif /* CONFIG_SHA256 */
1108 ev.peer_cert.depth = i;
1109 ev.peer_cert.subject = buf;
1110 conn->global->event_cb(conn->global->cb_ctx,
1111 TLS_PEER_CERTIFICATE, &ev);
1112 wpabuf_free(cert_buf);
1116 if (conn->suffix_match &&
1117 !gnutls_x509_crt_check_hostname(
1118 cert, conn->suffix_match)) {
1119 wpa_printf(MSG_WARNING,
1120 "TLS: Domain suffix match '%s' not found",
1121 conn->suffix_match);
1122 gnutls_tls_fail_event(
1123 conn, &certs[i], i, buf,
1124 "Domain suffix mismatch",
1125 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
1126 err = GNUTLS_A_BAD_CERTIFICATE;
1127 gnutls_x509_crt_deinit(cert);
1132 #if GNUTLS_VERSION_NUMBER >= 0x030300
1133 if (conn->domain_match &&
1134 !gnutls_x509_crt_check_hostname2(
1135 cert, conn->domain_match,
1136 GNUTLS_VERIFY_DO_NOT_ALLOW_WILDCARDS)) {
1137 wpa_printf(MSG_WARNING,
1138 "TLS: Domain match '%s' not found",
1139 conn->domain_match);
1140 gnutls_tls_fail_event(
1141 conn, &certs[i], i, buf,
1143 TLS_FAIL_DOMAIN_MISMATCH);
1144 err = GNUTLS_A_BAD_CERTIFICATE;
1145 gnutls_x509_crt_deinit(cert);
1149 #endif /* >= 3.3.0 */
1151 /* TODO: validate altsubject_match.
1152 * For now, any such configuration is rejected in
1153 * tls_connection_set_params() */
1155 #if GNUTLS_VERSION_NUMBER < 0x030300
1157 * gnutls_certificate_verify_peers() not available, so
1158 * need to check EKU separately.
1160 if (!conn->global->server &&
1161 !server_eku_purpose(cert)) {
1162 wpa_printf(MSG_WARNING,
1163 "GnuTLS: No server EKU");
1164 gnutls_tls_fail_event(
1165 conn, &certs[i], i, buf,
1167 TLS_FAIL_BAD_CERTIFICATE);
1168 err = GNUTLS_A_BAD_CERTIFICATE;
1169 gnutls_x509_crt_deinit(cert);
1173 #endif /* < 3.3.0 */
1176 if (!conn->disable_time_checks &&
1177 (gnutls_x509_crt_get_expiration_time(cert) < now.sec ||
1178 gnutls_x509_crt_get_activation_time(cert) > now.sec)) {
1179 wpa_printf(MSG_INFO, "TLS: Peer certificate %d/%d is "
1180 "not valid at this time",
1182 gnutls_tls_fail_event(
1183 conn, &certs[i], i, buf,
1184 "Certificate is not valid at this time",
1186 gnutls_x509_crt_deinit(cert);
1188 err = GNUTLS_A_CERTIFICATE_EXPIRED;
1194 gnutls_x509_crt_deinit(cert);
1197 if (conn->global->event_cb != NULL)
1198 conn->global->event_cb(conn->global->cb_ctx,
1199 TLS_CERT_CHAIN_SUCCESS, NULL);
1205 gnutls_alert_send(session, GNUTLS_AL_FATAL, err);
1206 return GNUTLS_E_CERTIFICATE_ERROR;
1210 static struct wpabuf * gnutls_get_appl_data(struct tls_connection *conn)
1214 wpa_printf(MSG_DEBUG, "GnuTLS: Check for possible Application Data");
1215 ad = wpabuf_alloc((wpabuf_len(conn->pull_buf) + 500) * 3);
1219 res = gnutls_record_recv(conn->session, wpabuf_mhead(ad),
1221 wpa_printf(MSG_DEBUG, "GnuTLS: gnutls_record_recv: %d", res);
1223 wpa_printf(MSG_DEBUG, "%s - gnutls_record_recv failed: %d "
1224 "(%s)", __func__, (int) res,
1225 gnutls_strerror(res));
1230 wpabuf_put(ad, res);
1231 wpa_printf(MSG_DEBUG, "GnuTLS: Received %d bytes of Application Data",
1237 struct wpabuf * tls_connection_handshake(void *tls_ctx,
1238 struct tls_connection *conn,
1239 const struct wpabuf *in_data,
1240 struct wpabuf **appl_data)
1242 struct tls_global *global = tls_ctx;
1243 struct wpabuf *out_data;
1249 if (in_data && wpabuf_len(in_data) > 0) {
1250 if (conn->pull_buf) {
1251 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
1252 "pull_buf", __func__,
1253 (unsigned long) wpabuf_len(conn->pull_buf));
1254 wpabuf_free(conn->pull_buf);
1256 conn->pull_buf = wpabuf_dup(in_data);
1257 if (conn->pull_buf == NULL)
1259 conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
1262 ret = gnutls_handshake(conn->session);
1264 gnutls_alert_description_t alert;
1267 case GNUTLS_E_AGAIN:
1268 if (global->server && conn->established &&
1269 conn->push_buf == NULL) {
1270 /* Need to return something to trigger
1271 * completion of EAP-TLS. */
1272 conn->push_buf = wpabuf_alloc(0);
1275 case GNUTLS_E_FATAL_ALERT_RECEIVED:
1276 alert = gnutls_alert_get(conn->session);
1277 wpa_printf(MSG_DEBUG, "%s - received fatal '%s' alert",
1278 __func__, gnutls_alert_get_name(alert));
1279 conn->read_alerts++;
1280 if (conn->global->event_cb != NULL) {
1281 union tls_event_data ev;
1283 os_memset(&ev, 0, sizeof(ev));
1284 ev.alert.is_local = 0;
1285 ev.alert.type = gnutls_alert_get_name(alert);
1286 ev.alert.description = ev.alert.type;
1287 conn->global->event_cb(conn->global->cb_ctx,
1292 wpa_printf(MSG_DEBUG, "%s - gnutls_handshake failed "
1293 "-> %s", __func__, gnutls_strerror(ret));
1299 wpa_printf(MSG_DEBUG, "TLS: Handshake completed successfully");
1301 #if GNUTLS_VERSION_NUMBER >= 0x03010a
1305 desc = gnutls_session_get_desc(conn->session);
1307 wpa_printf(MSG_DEBUG, "GnuTLS: %s", desc);
1311 #endif /* GnuTLS 3.1.10 or newer */
1313 conn->established = 1;
1314 if (conn->push_buf == NULL) {
1315 /* Need to return something to get final TLS ACK. */
1316 conn->push_buf = wpabuf_alloc(0);
1319 gnutls_session_get_data(conn->session, NULL, &size);
1320 if (global->session_data == NULL ||
1321 global->session_data_size < size) {
1322 os_free(global->session_data);
1323 global->session_data = os_malloc(size);
1325 if (global->session_data) {
1326 global->session_data_size = size;
1327 gnutls_session_get_data(conn->session,
1328 global->session_data,
1329 &global->session_data_size);
1332 if (conn->pull_buf && appl_data)
1333 *appl_data = gnutls_get_appl_data(conn);
1336 out_data = conn->push_buf;
1337 conn->push_buf = NULL;
1342 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
1343 struct tls_connection *conn,
1344 const struct wpabuf *in_data,
1345 struct wpabuf **appl_data)
1347 return tls_connection_handshake(tls_ctx, conn, in_data, appl_data);
1351 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
1352 struct tls_connection *conn,
1353 const struct wpabuf *in_data)
1358 res = gnutls_record_send(conn->session, wpabuf_head(in_data),
1359 wpabuf_len(in_data));
1361 wpa_printf(MSG_INFO, "%s: Encryption failed: %s",
1362 __func__, gnutls_strerror(res));
1366 buf = conn->push_buf;
1367 conn->push_buf = NULL;
1372 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
1373 struct tls_connection *conn,
1374 const struct wpabuf *in_data)
1379 if (conn->pull_buf) {
1380 wpa_printf(MSG_DEBUG, "%s - %lu bytes remaining in "
1381 "pull_buf", __func__,
1382 (unsigned long) wpabuf_len(conn->pull_buf));
1383 wpabuf_free(conn->pull_buf);
1385 conn->pull_buf = wpabuf_dup(in_data);
1386 if (conn->pull_buf == NULL)
1388 conn->pull_buf_offset = wpabuf_head(conn->pull_buf);
1391 * Even though we try to disable TLS compression, it is possible that
1392 * this cannot be done with all TLS libraries. Add extra buffer space
1393 * to handle the possibility of the decrypted data being longer than
1396 out = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
1400 res = gnutls_record_recv(conn->session, wpabuf_mhead(out),
1403 wpa_printf(MSG_DEBUG, "%s - gnutls_record_recv failed: %d "
1404 "(%s)", __func__, (int) res, gnutls_strerror(res));
1408 wpabuf_put(out, res);
1414 int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
1418 return gnutls_session_is_resumed(conn->session);
1422 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
1430 int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
1431 char *buf, size_t buflen)
1438 int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
1439 char *buf, size_t buflen)
1447 int tls_connection_enable_workaround(void *ssl_ctx,
1448 struct tls_connection *conn)
1450 gnutls_record_disable_padding(conn->session);
1455 int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
1456 int ext_type, const u8 *data,
1464 int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
1468 return conn->failed;
1472 int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
1476 return conn->read_alerts;
1480 int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
1484 return conn->write_alerts;
1488 int tls_connection_set_session_ticket_cb(void *tls_ctx,
1489 struct tls_connection *conn,
1490 tls_session_ticket_cb cb, void *ctx)
1496 int tls_get_library_version(char *buf, size_t buf_len)
1498 return os_snprintf(buf, buf_len, "GnuTLS build=%s run=%s",
1499 GNUTLS_VERSION, gnutls_check_version(NULL));
1503 void tls_connection_set_success_data(struct tls_connection *conn,
1504 struct wpabuf *data)
1509 void tls_connection_set_success_data_resumed(struct tls_connection *conn)
1514 const struct wpabuf *
1515 tls_connection_get_success_data(struct tls_connection *conn)
1521 void tls_connection_remove_session(struct tls_connection *conn)