blob: d8499330d950a1e2e7688e77b4d17082db3f928c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SSL/TLS interface functions for OpenSSL
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070010#ifdef CONFIG_TESTING_OPTIONS
11#include <fcntl.h>
12#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
14#ifndef CONFIG_SMARTCARD
15#ifndef OPENSSL_NO_ENGINE
Kenny Rootdb3c5a42012-03-20 17:00:47 -070016#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#define OPENSSL_NO_ENGINE
18#endif
19#endif
Kenny Rootdb3c5a42012-03-20 17:00:47 -070020#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021
Sunil Ravib0ac25f2024-07-12 01:42:03 +000022#ifndef OPENSSL_NO_ENGINE
23/* OpenSSL 3.0 has moved away from the engine API */
24#define OPENSSL_SUPPRESS_DEPRECATED
25#include <openssl/engine.h>
26#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include <openssl/ssl.h>
28#include <openssl/err.h>
Dmitry Shmidt849734c2016-05-27 09:59:01 -070029#include <openssl/opensslv.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include <openssl/pkcs12.h>
31#include <openssl/x509v3.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070032#if OPENSSL_VERSION_NUMBER >= 0x30000000L
33#include <openssl/core_names.h>
34#include <openssl/decoder.h>
35#include <openssl/param_build.h>
36#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080037#ifndef OPENSSL_NO_DSA
38#include <openssl/dsa.h>
39#endif
40#ifndef OPENSSL_NO_DH
41#include <openssl/dh.h>
42#endif
Sunil Ravia04bd252022-05-02 22:54:18 -070043#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "common.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070046#include "utils/list.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070048#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080049#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080051#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt849734c2016-05-27 09:59:01 -070053#if !defined(CONFIG_FIPS) && \
54 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
55 defined(EAP_SERVER_FAST))
56#define OPENSSL_NEED_EAP_FAST_PRF
57#endif
58
Hai Shalom81f62d82019-07-22 12:10:00 -070059#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
60 defined(EAP_SERVER_FAST) || defined(EAP_TEAP) || \
61 defined(EAP_SERVER_TEAP)
62#define EAP_FAST_OR_TEAP
63#endif
64
65
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070066#if defined(OPENSSL_IS_BORINGSSL)
67/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
68typedef size_t stack_index_t;
69#else
70typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071#endif
72
Dmitry Shmidt34af3062013-07-11 10:46:32 -070073#ifdef SSL_set_tlsext_status_type
74#ifndef OPENSSL_NO_TLSEXT
75#define HAVE_OCSP
76#include <openssl/ocsp.h>
77#endif /* OPENSSL_NO_TLSEXT */
78#endif /* SSL_set_tlsext_status_type */
79
Sunil Ravia04bd252022-05-02 22:54:18 -070080#if OPENSSL_VERSION_NUMBER < 0x10100000L && \
Dmitry Shmidt849734c2016-05-27 09:59:01 -070081 !defined(BORINGSSL_API_VERSION)
Dmitry Shmidtde47be72016-01-07 12:52:55 -080082/*
83 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
Dmitry Shmidt849734c2016-05-27 09:59:01 -070084 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
85 * older versions.
Dmitry Shmidtde47be72016-01-07 12:52:55 -080086 */
87
88static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out,
89 size_t outlen)
90{
91 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
92 return 0;
93 os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE);
94 return SSL3_RANDOM_SIZE;
95}
96
97
98static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
99 size_t outlen)
100{
101 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
102 return 0;
103 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
104 return SSL3_RANDOM_SIZE;
105}
106
107
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700108#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800109static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
110 unsigned char *out, size_t outlen)
111{
112 if (!session || session->master_key_length < 0 ||
113 (size_t) session->master_key_length > outlen)
114 return 0;
115 if ((size_t) session->master_key_length < outlen)
116 outlen = session->master_key_length;
117 os_memcpy(out, session->master_key, outlen);
118 return outlen;
119}
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700120#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800121
122#endif
123
Sunil Ravia04bd252022-05-02 22:54:18 -0700124#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800125static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
126{
127 return ASN1_STRING_data((ASN1_STRING *) x);
128}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700129#endif
130
Gabriel Birena5bdf372022-12-15 20:54:33 +0000131static int tls_openssl_ref_count = 0;
132static int tls_ex_idx_session = -1;
133
134struct tls_session_data {
135 struct dl_list list;
136 struct wpabuf *buf;
137};
138
139struct tls_context {
140 void (*event_cb)(void *ctx, enum tls_event ev,
141 union tls_event_data *data);
142 void *cb_ctx;
143 int cert_in_cb;
144 char *ocsp_stapling_response;
145 struct dl_list sessions; /* struct tls_session_data */
146};
147
Gabriel Birencd0cb1c2023-04-17 18:16:23 +0000148struct tls_data {
149 SSL_CTX *ssl;
150 unsigned int tls_session_lifetime;
151 int check_crl;
152 int check_crl_strict;
153 char *ca_cert;
154 unsigned int crl_reload_interval;
155 struct os_reltime crl_last_reload;
156 char *check_cert_subject;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000157 char *openssl_ciphers;
Gabriel Birencd0cb1c2023-04-17 18:16:23 +0000158};
159
160struct tls_connection {
161 struct tls_context *context;
162 struct tls_data *data;
163 SSL_CTX *ssl_ctx;
164 SSL *ssl;
165 BIO *ssl_in, *ssl_out;
166#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
167 ENGINE *engine; /* functional reference to the engine */
168 EVP_PKEY *private_key; /* the private key if using engine */
169#endif /* OPENSSL_NO_ENGINE */
170 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
171 char *check_cert_subject;
172 int read_alerts, write_alerts, failed;
173
174 tls_session_ticket_cb session_ticket_cb;
175 void *session_ticket_cb_ctx;
176
177 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
178 u8 *session_ticket;
179 size_t session_ticket_len;
180
181 unsigned int ca_cert_verify:1;
182 unsigned int cert_probe:1;
183 unsigned int server_cert_only:1;
184 unsigned int invalid_hb_used:1;
185 unsigned int success_data:1;
186 unsigned int client_hello_generated:1;
187 unsigned int server:1;
188
189 u8 srv_cert_hash[32];
190
191 unsigned int flags;
192
193 X509 *peer_cert;
194 X509 *peer_issuer;
195 X509 *peer_issuer_issuer;
196 char *peer_subject; /* peer subject info for authenticated peer */
197
198 unsigned char client_random[SSL3_RANDOM_SIZE];
199 unsigned char server_random[SSL3_RANDOM_SIZE];
200
201 u16 cipher_suite;
202 int server_dh_prime_len;
203};
204
Gabriel Birena5bdf372022-12-15 20:54:33 +0000205static struct tls_context *tls_global = NULL;
206static tls_get_certificate_cb certificate_callback_global = NULL;
Gabriel Biren60ae0682023-11-01 22:04:12 +0000207static tls_openssl_failure_cb openssl_failure_callback_global = NULL;
Gabriel Birena5bdf372022-12-15 20:54:33 +0000208
Dmitry Shmidtff079172013-11-08 14:10:30 -0800209#ifdef ANDROID
210#include <openssl/pem.h>
Dmitry Shmidtff079172013-11-08 14:10:30 -0800211
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000212#include <log/log.h>
213#include <log/log_event_list.h>
214
215#define CERT_VALIDATION_FAILURE 210033
Hai Shalom7ad2a872021-08-02 18:56:55 -0700216#define ANDROID_KEYSTORE_PREFIX "keystore://"
217#define ANDROID_KEYSTORE_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_PREFIX)
218#define ANDROID_KEYSTORE_ENCODED_PREFIX "keystores://"
219#define ANDROID_KEYSTORE_ENCODED_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_ENCODED_PREFIX)
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000220
221static void log_cert_validation_failure(const char *reason)
222{
223 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
224 android_log_write_string8(ctx, reason);
225 android_log_write_list(ctx, LOG_ID_SECURITY);
226 android_log_destroy(&ctx);
227}
228
229
Gabriel Birenff4f8382023-04-06 20:14:39 +0000230static BIO* BIO_from_keystore(const char *alias, struct tls_connection *conn)
Dmitry Shmidtff079172013-11-08 14:10:30 -0800231{
232 BIO *bio = NULL;
233 uint8_t *value = NULL;
Gabriel Birenff4f8382023-04-06 20:14:39 +0000234
235 void *cb_ctx = NULL;
236 if (conn != NULL && conn->context != NULL) {
237 cb_ctx = conn->context->cb_ctx;
238 }
239
240 if (cb_ctx != NULL && certificate_callback_global != NULL) {
Gabriel Biren980c48a2023-03-27 21:49:21 +0000241 wpa_printf(MSG_INFO, "Retrieving certificate using callback");
Gabriel Birenff4f8382023-04-06 20:14:39 +0000242 int length = (*certificate_callback_global)(cb_ctx, alias, &value);
Gabriel Birena5bdf372022-12-15 20:54:33 +0000243 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
244 BIO_write(bio, value, length);
Gabriel Birenff4f8382023-04-06 20:14:39 +0000245 free(value);
Gabriel Birena5bdf372022-12-15 20:54:33 +0000246 }
Dmitry Shmidtff079172013-11-08 14:10:30 -0800247 return bio;
248}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800249
Gabriel Birenff4f8382023-04-06 20:14:39 +0000250static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias, struct tls_connection *conn)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800251{
Gabriel Birenff4f8382023-04-06 20:14:39 +0000252 BIO *bio = BIO_from_keystore(alias, conn);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800253 STACK_OF(X509_INFO) *stack = NULL;
254 stack_index_t i;
Hai Shalom22171592021-04-02 16:05:23 -0700255 int ret = 0;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800256
Hai Shalom22171592021-04-02 16:05:23 -0700257 if (!bio) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700258 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
259 alias);
Hai Shalom22171592021-04-02 16:05:23 -0700260 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800261 }
262
Hai Shalom7ad2a872021-08-02 18:56:55 -0700263 // Keystore returns X.509 certificates in PEM encoding
Hai Shalom22171592021-04-02 16:05:23 -0700264 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
265 BIO_free(bio);
266
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800267 if (!stack) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700268 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
269 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800270 return -1;
271 }
272
273 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
274 X509_INFO *info = sk_X509_INFO_value(stack, i);
275
276 if (info->x509)
Hai Shalom22171592021-04-02 16:05:23 -0700277 if (!X509_STORE_add_cert(ctx, info->x509)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700278 wpa_printf(MSG_ERROR,
279 "OpenSSL: Failed to add Root CA certificate");
Hai Shalom22171592021-04-02 16:05:23 -0700280 ret = -1;
281 break;
282 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800283 if (info->crl)
284 X509_STORE_add_crl(ctx, info->crl);
285 }
286
287 sk_X509_INFO_pop_free(stack, X509_INFO_free);
Hai Shalom22171592021-04-02 16:05:23 -0700288 return ret;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800289}
290
291
292static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
Gabriel Birenff4f8382023-04-06 20:14:39 +0000293 const char *encoded_alias,
294 struct tls_connection *conn)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800295{
296 int rc = -1;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700297 int len = os_strlen(encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800298 unsigned char *decoded_alias;
299
300 if (len & 1) {
301 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
Hai Shalom7ad2a872021-08-02 18:56:55 -0700302 encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800303 return rc;
304 }
305
306 decoded_alias = os_malloc(len / 2 + 1);
307 if (decoded_alias) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700308 if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800309 decoded_alias[len / 2] = '\0';
310 rc = tls_add_ca_from_keystore(
Gabriel Birenff4f8382023-04-06 20:14:39 +0000311 ctx, (const char *) decoded_alias, conn);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800312 }
313 os_free(decoded_alias);
314 }
315
316 return rc;
317}
318
Dmitry Shmidtff079172013-11-08 14:10:30 -0800319#endif /* ANDROID */
320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700322static struct tls_context * tls_context_new(const struct tls_config *conf)
323{
324 struct tls_context *context = os_zalloc(sizeof(*context));
325 if (context == NULL)
326 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -0700327 dl_list_init(&context->sessions);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700328 if (conf) {
329 context->event_cb = conf->event_cb;
330 context->cb_ctx = conf->cb_ctx;
331 context->cert_in_cb = conf->cert_in_cb;
332 }
333 return context;
334}
335
336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337#ifdef CONFIG_NO_STDOUT_DEBUG
338
339static void _tls_show_errors(void)
340{
341 unsigned long err;
342
343 while ((err = ERR_get_error())) {
344 /* Just ignore the errors, since stdout is disabled */
345 }
346}
347#define tls_show_errors(l, f, t) _tls_show_errors()
348
349#else /* CONFIG_NO_STDOUT_DEBUG */
350
351static void tls_show_errors(int level, const char *func, const char *txt)
352{
353 unsigned long err;
354
355 wpa_printf(level, "OpenSSL: %s - %s %s",
356 func, txt, ERR_error_string(ERR_get_error(), NULL));
357
358 while ((err = ERR_get_error())) {
359 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
360 ERR_error_string(err, NULL));
361 }
362}
363
364#endif /* CONFIG_NO_STDOUT_DEBUG */
365
366
Hai Shalom74f70d42019-02-11 14:42:39 -0800367static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
368{
369 int flags;
370 X509_STORE *store;
371
372 store = X509_STORE_new();
373 if (!store) {
374 wpa_printf(MSG_DEBUG,
375 "OpenSSL: %s - failed to allocate new certificate store",
376 __func__);
377 return NULL;
378 }
379
380 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
381 tls_show_errors(MSG_WARNING, __func__,
382 "Failed to load root certificates");
383 X509_STORE_free(store);
384 return NULL;
385 }
386
387 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
388 if (check_crl == 2)
389 flags |= X509_V_FLAG_CRL_CHECK_ALL;
390
391 X509_STORE_set_flags(store, flags);
392
393 return store;
394}
395
396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397#ifdef CONFIG_NATIVE_WINDOWS
398
399/* Windows CryptoAPI and access to certificate stores */
400#include <wincrypt.h>
401
402#ifdef __MINGW32_VERSION
403/*
404 * MinGW does not yet include all the needed definitions for CryptoAPI, so
405 * define here whatever extra is needed.
406 */
407#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
408#define CERT_STORE_READONLY_FLAG 0x00008000
409#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
410
411#endif /* __MINGW32_VERSION */
412
413
414struct cryptoapi_rsa_data {
415 const CERT_CONTEXT *cert;
416 HCRYPTPROV crypt_prov;
417 DWORD key_spec;
418 BOOL free_crypt_prov;
419};
420
421
422static void cryptoapi_error(const char *msg)
423{
424 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
425 msg, (unsigned int) GetLastError());
426}
427
428
429static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
430 unsigned char *to, RSA *rsa, int padding)
431{
432 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
433 return 0;
434}
435
436
437static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
438 unsigned char *to, RSA *rsa, int padding)
439{
440 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
441 return 0;
442}
443
444
445static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
446 unsigned char *to, RSA *rsa, int padding)
447{
448 struct cryptoapi_rsa_data *priv =
449 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
450 HCRYPTHASH hash;
451 DWORD hash_size, len, i;
452 unsigned char *buf = NULL;
453 int ret = 0;
454
455 if (priv == NULL) {
456 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
457 ERR_R_PASSED_NULL_PARAMETER);
458 return 0;
459 }
460
461 if (padding != RSA_PKCS1_PADDING) {
462 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
463 RSA_R_UNKNOWN_PADDING_TYPE);
464 return 0;
465 }
466
467 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
468 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
469 __func__);
470 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
471 RSA_R_INVALID_MESSAGE_LENGTH);
472 return 0;
473 }
474
475 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
476 {
477 cryptoapi_error("CryptCreateHash failed");
478 return 0;
479 }
480
481 len = sizeof(hash_size);
482 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
483 0)) {
484 cryptoapi_error("CryptGetHashParam failed");
485 goto err;
486 }
487
488 if ((int) hash_size != flen) {
489 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
490 (unsigned) hash_size, flen);
491 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
492 RSA_R_INVALID_MESSAGE_LENGTH);
493 goto err;
494 }
495 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
496 cryptoapi_error("CryptSetHashParam failed");
497 goto err;
498 }
499
500 len = RSA_size(rsa);
501 buf = os_malloc(len);
502 if (buf == NULL) {
503 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
504 goto err;
505 }
506
507 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
508 cryptoapi_error("CryptSignHash failed");
509 goto err;
510 }
511
512 for (i = 0; i < len; i++)
513 to[i] = buf[len - i - 1];
514 ret = len;
515
516err:
517 os_free(buf);
518 CryptDestroyHash(hash);
519
520 return ret;
521}
522
523
524static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
525 unsigned char *to, RSA *rsa, int padding)
526{
527 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
528 return 0;
529}
530
531
532static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
533{
534 if (priv == NULL)
535 return;
536 if (priv->crypt_prov && priv->free_crypt_prov)
537 CryptReleaseContext(priv->crypt_prov, 0);
538 if (priv->cert)
539 CertFreeCertificateContext(priv->cert);
540 os_free(priv);
541}
542
543
544static int cryptoapi_finish(RSA *rsa)
545{
546 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
547 os_free((void *) rsa->meth);
548 rsa->meth = NULL;
549 return 1;
550}
551
552
553static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
554{
555 HCERTSTORE cs;
556 const CERT_CONTEXT *ret = NULL;
557
558 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
559 store | CERT_STORE_OPEN_EXISTING_FLAG |
560 CERT_STORE_READONLY_FLAG, L"MY");
561 if (cs == NULL) {
562 cryptoapi_error("Failed to open 'My system store'");
563 return NULL;
564 }
565
566 if (strncmp(name, "cert://", 7) == 0) {
567 unsigned short wbuf[255];
568 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
569 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
570 PKCS_7_ASN_ENCODING,
571 0, CERT_FIND_SUBJECT_STR,
572 wbuf, NULL);
573 } else if (strncmp(name, "hash://", 7) == 0) {
574 CRYPT_HASH_BLOB blob;
575 int len;
576 const char *hash = name + 7;
577 unsigned char *buf;
578
579 len = os_strlen(hash) / 2;
580 buf = os_malloc(len);
581 if (buf && hexstr2bin(hash, buf, len) == 0) {
582 blob.cbData = len;
583 blob.pbData = buf;
584 ret = CertFindCertificateInStore(cs,
585 X509_ASN_ENCODING |
586 PKCS_7_ASN_ENCODING,
587 0, CERT_FIND_HASH,
588 &blob, NULL);
589 }
590 os_free(buf);
591 }
592
593 CertCloseStore(cs, 0);
594
595 return ret;
596}
597
598
599static int tls_cryptoapi_cert(SSL *ssl, const char *name)
600{
601 X509 *cert = NULL;
602 RSA *rsa = NULL, *pub_rsa;
603 struct cryptoapi_rsa_data *priv;
604 RSA_METHOD *rsa_meth;
605
606 if (name == NULL ||
607 (strncmp(name, "cert://", 7) != 0 &&
608 strncmp(name, "hash://", 7) != 0))
609 return -1;
610
611 priv = os_zalloc(sizeof(*priv));
612 rsa_meth = os_zalloc(sizeof(*rsa_meth));
613 if (priv == NULL || rsa_meth == NULL) {
614 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
615 "for CryptoAPI RSA method");
616 os_free(priv);
617 os_free(rsa_meth);
618 return -1;
619 }
620
621 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
622 if (priv->cert == NULL) {
623 priv->cert = cryptoapi_find_cert(
624 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
625 }
626 if (priv->cert == NULL) {
627 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
628 "'%s'", name);
629 goto err;
630 }
631
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800632 cert = d2i_X509(NULL,
633 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 priv->cert->cbCertEncoded);
635 if (cert == NULL) {
636 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
637 "encoding");
638 goto err;
639 }
640
641 if (!CryptAcquireCertificatePrivateKey(priv->cert,
642 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
643 NULL, &priv->crypt_prov,
644 &priv->key_spec,
645 &priv->free_crypt_prov)) {
646 cryptoapi_error("Failed to acquire a private key for the "
647 "certificate");
648 goto err;
649 }
650
651 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
652 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
653 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
654 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
655 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
656 rsa_meth->finish = cryptoapi_finish;
657 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
658 rsa_meth->app_data = (char *) priv;
659
660 rsa = RSA_new();
661 if (rsa == NULL) {
662 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
663 ERR_R_MALLOC_FAILURE);
664 goto err;
665 }
666
667 if (!SSL_use_certificate(ssl, cert)) {
668 RSA_free(rsa);
669 rsa = NULL;
670 goto err;
671 }
672 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
673 X509_free(cert);
674 cert = NULL;
675
676 rsa->n = BN_dup(pub_rsa->n);
677 rsa->e = BN_dup(pub_rsa->e);
678 if (!RSA_set_method(rsa, rsa_meth))
679 goto err;
680
681 if (!SSL_use_RSAPrivateKey(ssl, rsa))
682 goto err;
683 RSA_free(rsa);
684
685 return 0;
686
687err:
688 if (cert)
689 X509_free(cert);
690 if (rsa)
691 RSA_free(rsa);
692 else {
693 os_free(rsa_meth);
694 cryptoapi_free_data(priv);
695 }
696 return -1;
697}
698
699
700static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
701{
702 HCERTSTORE cs;
703 PCCERT_CONTEXT ctx = NULL;
704 X509 *cert;
705 char buf[128];
706 const char *store;
707#ifdef UNICODE
708 WCHAR *wstore;
709#endif /* UNICODE */
710
711 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
712 return -1;
713
714 store = name + 13;
715#ifdef UNICODE
716 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
717 if (wstore == NULL)
718 return -1;
719 wsprintf(wstore, L"%S", store);
720 cs = CertOpenSystemStore(0, wstore);
721 os_free(wstore);
722#else /* UNICODE */
723 cs = CertOpenSystemStore(0, store);
724#endif /* UNICODE */
725 if (cs == NULL) {
726 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
727 "'%s': error=%d", __func__, store,
728 (int) GetLastError());
729 return -1;
730 }
731
732 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800733 cert = d2i_X509(NULL,
734 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 ctx->cbCertEncoded);
736 if (cert == NULL) {
737 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
738 "X509 DER encoding for CA cert");
739 continue;
740 }
741
742 X509_NAME_oneline(X509_get_subject_name(cert), buf,
743 sizeof(buf));
744 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
745 "system certificate store: subject='%s'", buf);
746
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700747 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
748 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 tls_show_errors(MSG_WARNING, __func__,
750 "Failed to add ca_cert to OpenSSL "
751 "certificate store");
752 }
753
754 X509_free(cert);
755 }
756
757 if (!CertCloseStore(cs, 0)) {
758 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
759 "'%s': error=%d", __func__, name + 13,
760 (int) GetLastError());
761 }
762
763 return 0;
764}
765
766
767#else /* CONFIG_NATIVE_WINDOWS */
768
769static int tls_cryptoapi_cert(SSL *ssl, const char *name)
770{
771 return -1;
772}
773
774#endif /* CONFIG_NATIVE_WINDOWS */
775
776
777static void ssl_info_cb(const SSL *ssl, int where, int ret)
778{
779 const char *str;
780 int w;
781
782 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
783 w = where & ~SSL_ST_MASK;
784 if (w & SSL_ST_CONNECT)
785 str = "SSL_connect";
786 else if (w & SSL_ST_ACCEPT)
787 str = "SSL_accept";
788 else
789 str = "undefined";
790
791 if (where & SSL_CB_LOOP) {
792 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
793 str, SSL_state_string_long(ssl));
794 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700795 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
797 where & SSL_CB_READ ?
798 "read (remote end reported an error)" :
799 "write (local SSL3 detected an error)",
800 SSL_alert_type_string_long(ret),
801 SSL_alert_desc_string_long(ret));
802 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 if (where & SSL_CB_READ)
804 conn->read_alerts++;
805 else
806 conn->write_alerts++;
807 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700808 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700810 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 os_memset(&ev, 0, sizeof(ev));
812 ev.alert.is_local = !(where & SSL_CB_READ);
813 ev.alert.type = SSL_alert_type_string_long(ret);
814 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700815 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 } else if (where & SSL_CB_EXIT && ret <= 0) {
818 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
819 str, ret == 0 ? "failed" : "error",
820 SSL_state_string_long(ssl));
821 }
822}
823
824
825#ifndef OPENSSL_NO_ENGINE
826/**
827 * tls_engine_load_dynamic_generic - load any openssl engine
828 * @pre: an array of commands and values that load an engine initialized
829 * in the engine specific function
830 * @post: an array of commands and values that initialize an already loaded
831 * engine (or %NULL if not required)
832 * @id: the engine id of the engine to load (only required if post is not %NULL
833 *
834 * This function is a generic function that loads any openssl engine.
835 *
836 * Returns: 0 on success, -1 on failure
837 */
838static int tls_engine_load_dynamic_generic(const char *pre[],
839 const char *post[], const char *id)
840{
841 ENGINE *engine;
842 const char *dynamic_id = "dynamic";
843
844 engine = ENGINE_by_id(id);
845 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
847 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700848 /*
849 * If it was auto-loaded by ENGINE_by_id() we might still
850 * need to tell it which PKCS#11 module to use in legacy
851 * (non-p11-kit) environments. Do so now; even if it was
852 * properly initialised before, setting it again will be
853 * harmless.
854 */
855 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 }
857 ERR_clear_error();
858
859 engine = ENGINE_by_id(dynamic_id);
860 if (engine == NULL) {
861 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
862 dynamic_id,
863 ERR_error_string(ERR_get_error(), NULL));
864 return -1;
865 }
866
867 /* Perform the pre commands. This will load the engine. */
868 while (pre && pre[0]) {
869 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
870 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
871 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
872 "%s %s [%s]", pre[0], pre[1],
873 ERR_error_string(ERR_get_error(), NULL));
874 ENGINE_free(engine);
875 return -1;
876 }
877 pre += 2;
878 }
879
880 /*
881 * Free the reference to the "dynamic" engine. The loaded engine can
882 * now be looked up using ENGINE_by_id().
883 */
884 ENGINE_free(engine);
885
886 engine = ENGINE_by_id(id);
887 if (engine == NULL) {
888 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
889 id, ERR_error_string(ERR_get_error(), NULL));
890 return -1;
891 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700892 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 while (post && post[0]) {
894 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
895 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
896 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
897 " %s %s [%s]", post[0], post[1],
898 ERR_error_string(ERR_get_error(), NULL));
899 ENGINE_remove(engine);
900 ENGINE_free(engine);
901 return -1;
902 }
903 post += 2;
904 }
905 ENGINE_free(engine);
906
907 return 0;
908}
909
910
911/**
912 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
913 * @pkcs11_so_path: pksc11_so_path from the configuration
914 * @pcks11_module_path: pkcs11_module_path from the configuration
915 */
916static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
917 const char *pkcs11_module_path)
918{
919 char *engine_id = "pkcs11";
920 const char *pre_cmd[] = {
921 "SO_PATH", NULL /* pkcs11_so_path */,
922 "ID", NULL /* engine_id */,
923 "LIST_ADD", "1",
924 /* "NO_VCHECK", "1", */
925 "LOAD", NULL,
926 NULL, NULL
927 };
928 const char *post_cmd[] = {
929 "MODULE_PATH", NULL /* pkcs11_module_path */,
930 NULL, NULL
931 };
932
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800933 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 return 0;
935
936 pre_cmd[1] = pkcs11_so_path;
937 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 if (pkcs11_module_path)
939 post_cmd[1] = pkcs11_module_path;
940 else
941 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942
943 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
944 pkcs11_so_path);
945
946 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
947}
948
949
950/**
951 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
952 * @opensc_so_path: opensc_so_path from the configuration
953 */
954static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
955{
956 char *engine_id = "opensc";
957 const char *pre_cmd[] = {
958 "SO_PATH", NULL /* opensc_so_path */,
959 "ID", NULL /* engine_id */,
960 "LIST_ADD", "1",
961 "LOAD", NULL,
962 NULL, NULL
963 };
964
965 if (!opensc_so_path)
966 return 0;
967
968 pre_cmd[1] = opensc_so_path;
969 pre_cmd[3] = engine_id;
970
971 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
972 opensc_so_path);
973
974 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
975}
976#endif /* OPENSSL_NO_ENGINE */
977
978
Sunil Ravia04bd252022-05-02 22:54:18 -0700979static struct tls_session_data * get_session_data(struct tls_context *context,
980 const struct wpabuf *buf)
981{
982 struct tls_session_data *data;
983
984 dl_list_for_each(data, &context->sessions, struct tls_session_data,
985 list) {
986 if (data->buf == buf)
987 return data;
988 }
989
990 return NULL;
991}
992
993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800994static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
995{
996 struct wpabuf *buf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700997 struct tls_context *context;
998 struct tls_session_data *found;
999
1000 wpa_printf(MSG_DEBUG,
1001 "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess,
1002 tls_ex_idx_session);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001003
1004 if (tls_ex_idx_session < 0)
1005 return;
1006 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
1007 if (!buf)
1008 return;
Sunil Ravia04bd252022-05-02 22:54:18 -07001009
1010 context = SSL_CTX_get_app_data(ctx);
1011 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
1012 found = get_session_data(context, buf);
1013 if (!found) {
1014 wpa_printf(MSG_DEBUG,
1015 "OpenSSL: Do not free application session data %p (sess %p)",
1016 buf, sess);
1017 return;
1018 }
1019
1020 dl_list_del(&found->list);
1021 os_free(found);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 wpa_printf(MSG_DEBUG,
1023 "OpenSSL: Free application session data %p (sess %p)",
1024 buf, sess);
1025 wpabuf_free(buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001026}
1027
1028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029void * tls_init(const struct tls_config *conf)
1030{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001031 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001033 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034 const char *ciphers;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001035#ifndef OPENSSL_NO_ENGINE
1036#ifdef CONFIG_OPENSC_ENGINE_PATH
1037 char const * const opensc_engine_path = CONFIG_OPENSC_ENGINE_PATH;
1038#else /* CONFIG_OPENSC_ENGINE_PATH */
1039 char const * const opensc_engine_path =
1040 conf ? conf->opensc_engine_path : NULL;
1041#endif /* CONFIG_OPENSC_ENGINE_PATH */
1042#ifdef CONFIG_PKCS11_ENGINE_PATH
1043 char const * const pkcs11_engine_path = CONFIG_PKCS11_ENGINE_PATH;
1044#else /* CONFIG_PKCS11_ENGINE_PATH */
1045 char const * const pkcs11_engine_path =
1046 conf ? conf->pkcs11_engine_path : NULL;
1047#endif /* CONFIG_PKCS11_ENGINE_PATH */
1048#ifdef CONFIG_PKCS11_MODULE_PATH
1049 char const * const pkcs11_module_path = CONFIG_PKCS11_MODULE_PATH;
1050#else /* CONFIG_PKCS11_MODULE_PATH */
1051 char const * const pkcs11_module_path =
1052 conf ? conf->pkcs11_module_path : NULL;
1053#endif /* CONFIG_PKCS11_MODULE_PATH */
1054#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055
1056 if (tls_openssl_ref_count == 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001057 void openssl_load_legacy_provider(void);
1058
1059 openssl_load_legacy_provider();
1060
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001061 tls_global = context = tls_context_new(conf);
1062 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064#ifdef CONFIG_FIPS
1065#ifdef OPENSSL_FIPS
1066 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001067 static int fips_enabled = 0;
1068
1069 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
1071 "mode");
1072 ERR_load_crypto_strings();
1073 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001074 os_free(tls_global);
1075 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001077 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001079 fips_enabled = 1;
1080 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 }
1082#else /* OPENSSL_FIPS */
1083 if (conf && conf->fips_mode) {
1084 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
1085 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001086 os_free(tls_global);
1087 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 return NULL;
1089 }
1090#endif /* OPENSSL_FIPS */
1091#endif /* CONFIG_FIPS */
Sunil Ravia04bd252022-05-02 22:54:18 -07001092#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 SSL_load_error_strings();
1094 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001095#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 EVP_add_digest(EVP_sha256());
1097#endif /* OPENSSL_NO_SHA256 */
1098 /* TODO: if /dev/urandom is available, PRNG is seeded
1099 * automatically. If this is not the case, random data should
1100 * be added here. */
1101
1102#ifdef PKCS12_FUNCS
1103#ifndef OPENSSL_NO_RC2
1104 /*
1105 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1106 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1107 * versions, but it looks like OpenSSL 1.0.0 does not do that
1108 * anymore.
1109 */
1110 EVP_add_cipher(EVP_rc2_40_cbc());
1111#endif /* OPENSSL_NO_RC2 */
1112 PKCS12_PBE_add();
1113#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001114#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001115 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001116 context = tls_context_new(conf);
1117 if (context == NULL)
1118 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 }
1120 tls_openssl_ref_count++;
1121
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001122 data = os_zalloc(sizeof(*data));
1123 if (data)
1124 ssl = SSL_CTX_new(SSLv23_method());
1125 else
1126 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001127 if (ssl == NULL) {
1128 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001129 if (context != tls_global)
1130 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001131 if (tls_openssl_ref_count == 0) {
1132 os_free(tls_global);
1133 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001134 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001135 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001137 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001138 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001139 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001140 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001141 data->crl_reload_interval = conf->crl_reload_interval;
1142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001144 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1145 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1146
Hai Shalom60840252021-02-19 19:02:11 -08001147 SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY);
1148
Dmitry Shmidt29333592017-01-09 12:27:11 -08001149#ifdef SSL_MODE_NO_AUTO_CHAIN
1150 /* Number of deployed use cases assume the default OpenSSL behavior of
1151 * auto chaining the local certificate is in use. BoringSSL removed this
1152 * functionality by default, so we need to restore it here to avoid
1153 * breaking existing use cases. */
1154 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1155#endif /* SSL_MODE_NO_AUTO_CHAIN */
1156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001158 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001159 if (data->tls_session_lifetime > 0) {
1160 SSL_CTX_set_quiet_shutdown(ssl, 1);
1161 /*
1162 * Set default context here. In practice, this will be replaced
1163 * by the per-EAP method context in tls_connection_set_verify().
1164 */
1165 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1166 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1167 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1168 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
Sunil Ravia04bd252022-05-02 22:54:18 -07001169#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1170 !defined(LIBRESSL_VERSION_NUMBER) && \
1171 !defined(OPENSSL_IS_BORINGSSL)
1172 /* One session ticket is sufficient for EAP-TLS */
1173 SSL_CTX_set_num_tickets(ssl, 1);
1174#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001175 } else {
1176 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
Sunil Ravia04bd252022-05-02 22:54:18 -07001177#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1178 !defined(LIBRESSL_VERSION_NUMBER) && \
1179 !defined(OPENSSL_IS_BORINGSSL)
1180 SSL_CTX_set_num_tickets(ssl, 0);
1181#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001182 }
1183
1184 if (tls_ex_idx_session < 0) {
1185 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1186 0, NULL, NULL, NULL, NULL);
1187 if (tls_ex_idx_session < 0) {
1188 tls_deinit(data);
1189 return NULL;
1190 }
1191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192
1193#ifndef OPENSSL_NO_ENGINE
Hai Shalom81f62d82019-07-22 12:10:00 -07001194 wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines");
1195 ENGINE_load_builtin_engines();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001196
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001197 if (opensc_engine_path || pkcs11_engine_path || pkcs11_module_path) {
1198 if (tls_engine_load_dynamic_opensc(opensc_engine_path) ||
1199 tls_engine_load_dynamic_pkcs11(pkcs11_engine_path,
1200 pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001201 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 return NULL;
1203 }
1204 }
1205#endif /* OPENSSL_NO_ENGINE */
1206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001207 if (conf && conf->openssl_ciphers)
1208 ciphers = conf->openssl_ciphers;
1209 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001210 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001211 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1212 wpa_printf(MSG_ERROR,
1213 "OpenSSL: Failed to set cipher string '%s'",
1214 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001215 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001216 return NULL;
1217 }
1218
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001219 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220}
1221
1222
1223void tls_deinit(void *ssl_ctx)
1224{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001225 struct tls_data *data = ssl_ctx;
1226 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001227 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Sunil Ravia04bd252022-05-02 22:54:18 -07001228 struct tls_session_data *sess_data;
1229
1230 if (data->tls_session_lifetime > 0) {
1231 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions");
1232 SSL_CTX_flush_sessions(ssl, 0);
1233 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done");
1234 }
1235 while ((sess_data = dl_list_first(&context->sessions,
1236 struct tls_session_data, list))) {
1237 wpa_printf(MSG_DEBUG,
1238 "OpenSSL: Freeing not-flushed session data %p",
1239 sess_data->buf);
1240 wpabuf_free(sess_data->buf);
1241 dl_list_del(&sess_data->list);
1242 os_free(sess_data);
1243 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001244 if (context != tls_global)
1245 os_free(context);
Hai Shalom74f70d42019-02-11 14:42:39 -08001246 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 SSL_CTX_free(ssl);
1248
1249 tls_openssl_ref_count--;
1250 if (tls_openssl_ref_count == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001251#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252#ifndef OPENSSL_NO_ENGINE
1253 ENGINE_cleanup();
1254#endif /* OPENSSL_NO_ENGINE */
1255 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001256 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 ERR_free_strings();
1258 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001259#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001260 os_free(tls_global->ocsp_stapling_response);
1261 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 os_free(tls_global);
1263 tls_global = NULL;
1264 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001265
Hai Shalom021b0b52019-04-10 11:17:58 -07001266 os_free(data->check_cert_subject);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001267 os_free(data->openssl_ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001268 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269}
1270
1271
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001272#ifndef OPENSSL_NO_ENGINE
1273
1274/* Cryptoki return values */
1275#define CKR_PIN_INCORRECT 0x000000a0
1276#define CKR_PIN_INVALID 0x000000a1
1277#define CKR_PIN_LEN_RANGE 0x000000a2
1278
1279/* libp11 */
1280#define ERR_LIB_PKCS11 ERR_LIB_USER
1281
1282static int tls_is_pin_error(unsigned int err)
1283{
1284 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1285 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1286 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1287 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1288}
1289
1290#endif /* OPENSSL_NO_ENGINE */
1291
1292
Gabriel Biren2fc93732024-10-10 00:24:33 +00001293// Imported from system/security/keystore-engine. This method
1294// is not used by the mainline supplicant.
1295#if defined(ANDROID) && !defined(MAINLINE_SUPPLICANT)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001296EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1297#endif /* ANDROID */
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1300 const char *pin, const char *key_id,
1301 const char *cert_id, const char *ca_cert_id)
1302{
Gabriel Biren2fc93732024-10-10 00:24:33 +00001303#if defined(ANDROID) && !defined(MAINLINE_SUPPLICANT) && defined(OPENSSL_IS_BORINGSSL)
Adam Langley1eb02ed2015-04-21 19:00:05 -07001304#if !defined(OPENSSL_NO_ENGINE)
1305#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1306#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001307 if (!key_id)
1308 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001309 conn->engine = NULL;
1310 conn->private_key = EVP_PKEY_from_keystore(key_id);
Gabriel Biren2fc93732024-10-10 00:24:33 +00001311
Adam Langley1eb02ed2015-04-21 19:00:05 -07001312 if (!conn->private_key) {
1313 wpa_printf(MSG_ERROR,
1314 "ENGINE: cannot load private key with id '%s' [%s]",
1315 key_id,
1316 ERR_error_string(ERR_get_error(), NULL));
1317 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1318 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001319#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321#ifndef OPENSSL_NO_ENGINE
1322 int ret = -1;
1323 if (engine_id == NULL) {
1324 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1325 return -1;
1326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327
1328 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001329#ifdef ANDROID
1330 ENGINE_load_dynamic();
1331#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 conn->engine = ENGINE_by_id(engine_id);
1333 if (!conn->engine) {
1334 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1335 engine_id, ERR_error_string(ERR_get_error(), NULL));
1336 goto err;
1337 }
1338 if (ENGINE_init(conn->engine) != 1) {
1339 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1340 "(engine: %s) [%s]", engine_id,
1341 ERR_error_string(ERR_get_error(), NULL));
1342 goto err;
1343 }
1344 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1345
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001346#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001347 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1349 ERR_error_string(ERR_get_error(), NULL));
1350 goto err;
1351 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001352#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001353 if (key_id) {
1354 /*
1355 * Ensure that the ENGINE does not attempt to use the OpenSSL
1356 * UI system to obtain a PIN, if we didn't provide one.
1357 */
1358 struct {
1359 const void *password;
1360 const char *prompt_info;
1361 } key_cb = { "", NULL };
1362
1363 /* load private key first in-case PIN is required for cert */
1364 conn->private_key = ENGINE_load_private_key(conn->engine,
1365 key_id, NULL,
1366 &key_cb);
1367 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001368 unsigned long err = ERR_get_error();
1369
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001370 wpa_printf(MSG_ERROR,
1371 "ENGINE: cannot load private key with id '%s' [%s]",
1372 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001373 ERR_error_string(err, NULL));
1374 if (tls_is_pin_error(err))
1375 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1376 else
1377 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001378 goto err;
1379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 }
1381
1382 /* handle a certificate and/or CA certificate */
1383 if (cert_id || ca_cert_id) {
1384 const char *cmd_name = "LOAD_CERT_CTRL";
1385
1386 /* test if the engine supports a LOAD_CERT_CTRL */
1387 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1388 0, (void *)cmd_name, NULL)) {
1389 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1390 " loading certificates");
1391 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1392 goto err;
1393 }
1394 }
1395
1396 return 0;
1397
1398err:
1399 if (conn->engine) {
1400 ENGINE_free(conn->engine);
1401 conn->engine = NULL;
1402 }
1403
1404 if (conn->private_key) {
1405 EVP_PKEY_free(conn->private_key);
1406 conn->private_key = NULL;
1407 }
1408
1409 return ret;
1410#else /* OPENSSL_NO_ENGINE */
1411 return 0;
1412#endif /* OPENSSL_NO_ENGINE */
1413}
1414
1415
1416static void tls_engine_deinit(struct tls_connection *conn)
1417{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001418#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1420 if (conn->private_key) {
1421 EVP_PKEY_free(conn->private_key);
1422 conn->private_key = NULL;
1423 }
1424 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001425#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001427#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 conn->engine = NULL;
1429 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001430#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431}
1432
1433
1434int tls_get_errors(void *ssl_ctx)
1435{
1436 int count = 0;
1437 unsigned long err;
1438
1439 while ((err = ERR_get_error())) {
1440 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1441 ERR_error_string(err, NULL));
1442 count++;
1443 }
1444
1445 return count;
1446}
1447
Jouni Malinen26af48b2014-04-09 13:02:53 +03001448
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001449static const char * openssl_content_type(int content_type)
1450{
1451 switch (content_type) {
1452 case 20:
1453 return "change cipher spec";
1454 case 21:
1455 return "alert";
1456 case 22:
1457 return "handshake";
1458 case 23:
1459 return "application data";
1460 case 24:
1461 return "heartbeat";
1462 case 256:
1463 return "TLS header info"; /* pseudo content type */
Hai Shalom81f62d82019-07-22 12:10:00 -07001464 case 257:
1465 return "inner content type"; /* pseudo content type */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001466 default:
1467 return "?";
1468 }
1469}
1470
1471
1472static const char * openssl_handshake_type(int content_type, const u8 *buf,
1473 size_t len)
1474{
Hai Shalom81f62d82019-07-22 12:10:00 -07001475 if (content_type == 257 && buf && len == 1)
1476 return openssl_content_type(buf[0]);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001477 if (content_type != 22 || !buf || len == 0)
1478 return "";
1479 switch (buf[0]) {
1480 case 0:
1481 return "hello request";
1482 case 1:
1483 return "client hello";
1484 case 2:
1485 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001486 case 3:
1487 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001488 case 4:
1489 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001490 case 5:
1491 return "end of early data";
1492 case 6:
1493 return "hello retry request";
1494 case 8:
1495 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001496 case 11:
1497 return "certificate";
1498 case 12:
1499 return "server key exchange";
1500 case 13:
1501 return "certificate request";
1502 case 14:
1503 return "server hello done";
1504 case 15:
1505 return "certificate verify";
1506 case 16:
1507 return "client key exchange";
1508 case 20:
1509 return "finished";
1510 case 21:
1511 return "certificate url";
1512 case 22:
1513 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001514 case 23:
1515 return "supplemental data";
1516 case 24:
1517 return "key update";
1518 case 254:
1519 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001520 default:
1521 return "?";
1522 }
1523}
1524
1525
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001526#ifdef CONFIG_SUITEB
1527
1528static void check_server_hello(struct tls_connection *conn,
1529 const u8 *pos, const u8 *end)
1530{
1531 size_t payload_len, id_len;
1532
1533 /*
1534 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1535 * not make it cleanly available during handshake and we need to know
1536 * whether DHE was selected.
1537 */
1538
1539 if (end - pos < 3)
1540 return;
1541 payload_len = WPA_GET_BE24(pos);
1542 pos += 3;
1543
1544 if ((size_t) (end - pos) < payload_len)
1545 return;
1546 end = pos + payload_len;
1547
1548 /* Skip Version and Random */
1549 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1550 return;
1551 pos += 2 + SSL3_RANDOM_SIZE;
1552
1553 /* Skip Session ID */
1554 if (end - pos < 1)
1555 return;
1556 id_len = *pos++;
1557 if ((size_t) (end - pos) < id_len)
1558 return;
1559 pos += id_len;
1560
1561 if (end - pos < 2)
1562 return;
1563 conn->cipher_suite = WPA_GET_BE16(pos);
1564 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1565 conn->cipher_suite);
1566}
1567
1568
1569static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1570 const u8 *pos, const u8 *end)
1571{
1572 size_t payload_len;
1573 u16 dh_len;
1574 BIGNUM *p;
1575 int bits;
1576
1577 if (!(conn->flags & TLS_CONN_SUITEB))
1578 return;
1579
1580 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1581 if (conn->cipher_suite != 0x9f)
1582 return;
1583
1584 if (end - pos < 3)
1585 return;
1586 payload_len = WPA_GET_BE24(pos);
1587 pos += 3;
1588
1589 if ((size_t) (end - pos) < payload_len)
1590 return;
1591 end = pos + payload_len;
1592
1593 if (end - pos < 2)
1594 return;
1595 dh_len = WPA_GET_BE16(pos);
1596 pos += 2;
1597
1598 if ((size_t) (end - pos) < dh_len)
1599 return;
1600 p = BN_bin2bn(pos, dh_len, NULL);
1601 if (!p)
1602 return;
1603
1604 bits = BN_num_bits(p);
1605 BN_free(p);
1606
1607 conn->server_dh_prime_len = bits;
1608 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1609 conn->server_dh_prime_len);
1610}
1611
1612#endif /* CONFIG_SUITEB */
1613
1614
Jouni Malinen26af48b2014-04-09 13:02:53 +03001615static void tls_msg_cb(int write_p, int version, int content_type,
1616 const void *buf, size_t len, SSL *ssl, void *arg)
1617{
1618 struct tls_connection *conn = arg;
1619 const u8 *pos = buf;
1620
Sunil8cd6f4d2022-06-28 18:40:46 +00001621#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1622 if ((SSL_version(ssl) == TLS1_VERSION ||
1623 SSL_version(ssl) == TLS1_1_VERSION) &&
1624 SSL_get_security_level(ssl) > 0) {
1625 wpa_printf(MSG_DEBUG,
1626 "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm");
1627 SSL_set_security_level(ssl, 0);
1628 }
1629#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001630 if (write_p == 2) {
1631 wpa_printf(MSG_DEBUG,
1632 "OpenSSL: session ver=0x%x content_type=%d",
1633 version, content_type);
1634 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1635 return;
1636 }
1637
1638 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1639 write_p ? "TX" : "RX", version, content_type,
1640 openssl_content_type(content_type),
1641 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001642 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1643 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1644 size_t payload_len = WPA_GET_BE16(pos + 1);
1645 if (payload_len + 3 > len) {
1646 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1647 conn->invalid_hb_used = 1;
1648 }
1649 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001650
1651#ifdef CONFIG_SUITEB
1652 /*
1653 * Need to parse these handshake messages to be able to check DH prime
1654 * length since OpenSSL does not expose the new cipher suite and DH
1655 * parameters during handshake (e.g., for cert_cb() callback).
1656 */
1657 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1658 check_server_hello(conn, pos + 1, pos + len);
1659 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1660 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1661#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001662}
1663
1664
Sunil Ravia04bd252022-05-02 22:54:18 -07001665#ifdef CONFIG_TESTING_OPTIONS
1666#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1667/*
1668 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1669 * material will be exported that you may use with Wireshark to decode any
1670 * TLS flows. Please see the following for more details:
1671 *
1672 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1673 *
1674 * Example logging sessions are (you should delete the file on each run):
1675 *
1676 * rm -f /tmp/sslkey.log
1677 * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ...
1678 *
1679 * rm -f /tmp/sslkey.log
1680 * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ...
1681 *
1682 * rm -f /tmp/sslkey.log
1683 * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ...
1684 */
1685static void tls_keylog_cb(const SSL *ssl, const char *line)
1686{
1687 int fd;
1688 const char *filename;
1689 struct iovec iov[2];
1690
1691 filename = getenv("SSLKEYLOGFILE");
1692 if (!filename)
1693 return;
1694
1695 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1696 if (fd < 0) {
1697 wpa_printf(MSG_ERROR,
1698 "OpenSSL: Failed to open keylog file %s: %s",
1699 filename, strerror(errno));
1700 return;
1701 }
1702
1703 /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed
1704 * to be atomic for O_APPEND. */
1705 iov[0].iov_base = (void *) line;
1706 iov[0].iov_len = os_strlen(line);
1707 iov[1].iov_base = "\n";
1708 iov[1].iov_len = 1;
1709
1710 if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) {
1711 wpa_printf(MSG_DEBUG,
1712 "OpenSSL: Failed to write to keylog file %s: %s",
1713 filename, strerror(errno));
1714 }
1715
1716 close(fd);
1717}
1718#endif
1719#endif /* CONFIG_TESTING_OPTIONS */
1720
1721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722struct tls_connection * tls_connection_init(void *ssl_ctx)
1723{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001724 struct tls_data *data = ssl_ctx;
1725 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 struct tls_connection *conn;
1727 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001728 X509_STORE *new_cert_store;
1729 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001730 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731
Hai Shalom74f70d42019-02-11 14:42:39 -08001732 /* Replace X509 store if it is time to update CRL. */
1733 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1734 os_reltime_expired(&now, &data->crl_last_reload,
1735 data->crl_reload_interval)) {
1736 wpa_printf(MSG_INFO,
1737 "OpenSSL: Flushing X509 store with ca_cert file");
1738 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1739 data->check_crl);
1740 if (!new_cert_store) {
1741 wpa_printf(MSG_ERROR,
1742 "OpenSSL: Error replacing X509 store with ca_cert file");
1743 } else {
1744 /* Replace old store */
1745 SSL_CTX_set_cert_store(ssl, new_cert_store);
1746 data->crl_last_reload = now;
1747 }
1748 }
1749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 conn = os_zalloc(sizeof(*conn));
1751 if (conn == NULL)
1752 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001753 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001754 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 conn->ssl = SSL_new(ssl);
1756 if (conn->ssl == NULL) {
1757 tls_show_errors(MSG_INFO, __func__,
1758 "Failed to initialize new SSL connection");
1759 os_free(conn);
1760 return NULL;
1761 }
1762
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001763 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001765 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1766 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1768 SSL_OP_SINGLE_DH_USE;
1769#ifdef SSL_OP_NO_COMPRESSION
1770 options |= SSL_OP_NO_COMPRESSION;
1771#endif /* SSL_OP_NO_COMPRESSION */
1772 SSL_set_options(conn->ssl, options);
Hai Shalom81f62d82019-07-22 12:10:00 -07001773#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
1774 /* Hopefully there is no need for middlebox compatibility mechanisms
1775 * when going through EAP authentication. */
1776 SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
1777#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778
Sunil Ravia04bd252022-05-02 22:54:18 -07001779#ifdef CONFIG_TESTING_OPTIONS
1780#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1781 /* Set the keylog file if the admin requested it. */
1782 if (getenv("SSLKEYLOGFILE"))
1783 SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb);
1784#endif
1785#endif /* CONFIG_TESTING_OPTIONS */
1786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 conn->ssl_in = BIO_new(BIO_s_mem());
1788 if (!conn->ssl_in) {
1789 tls_show_errors(MSG_INFO, __func__,
1790 "Failed to create a new BIO for ssl_in");
1791 SSL_free(conn->ssl);
1792 os_free(conn);
1793 return NULL;
1794 }
1795
1796 conn->ssl_out = BIO_new(BIO_s_mem());
1797 if (!conn->ssl_out) {
1798 tls_show_errors(MSG_INFO, __func__,
1799 "Failed to create a new BIO for ssl_out");
1800 SSL_free(conn->ssl);
1801 BIO_free(conn->ssl_in);
1802 os_free(conn);
1803 return NULL;
1804 }
1805
1806 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1807
1808 return conn;
1809}
1810
1811
1812void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1813{
1814 if (conn == NULL)
1815 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001816 if (conn->success_data) {
1817 /*
1818 * Make sure ssl_clear_bad_session() does not remove this
1819 * session.
1820 */
1821 SSL_set_quiet_shutdown(conn->ssl, 1);
1822 SSL_shutdown(conn->ssl);
1823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 SSL_free(conn->ssl);
1825 tls_engine_deinit(conn);
1826 os_free(conn->subject_match);
1827 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001828 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001829 os_free(conn->domain_match);
Hai Shalom021b0b52019-04-10 11:17:58 -07001830 os_free(conn->check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831 os_free(conn->session_ticket);
Hai Shalom899fcc72020-10-19 14:38:18 -07001832 os_free(conn->peer_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 os_free(conn);
1834}
1835
1836
1837int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1838{
1839 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1840}
1841
1842
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001843char * tls_connection_peer_serial_num(void *tls_ctx,
1844 struct tls_connection *conn)
1845{
1846 ASN1_INTEGER *ser;
1847 char *serial_num;
1848 size_t len;
1849
1850 if (!conn->peer_cert)
1851 return NULL;
1852
1853 ser = X509_get_serialNumber(conn->peer_cert);
1854 if (!ser)
1855 return NULL;
1856
1857 len = ASN1_STRING_length(ser) * 2 + 1;
1858 serial_num = os_malloc(len);
1859 if (!serial_num)
1860 return NULL;
1861 wpa_snprintf_hex_uppercase(serial_num, len,
1862 ASN1_STRING_get0_data(ser),
1863 ASN1_STRING_length(ser));
1864 return serial_num;
1865}
1866
1867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1869{
1870 if (conn == NULL)
1871 return -1;
1872
1873 /* Shutdown previous TLS connection without notifying the peer
1874 * because the connection was already terminated in practice
1875 * and "close notify" shutdown alert would confuse AS. */
1876 SSL_set_quiet_shutdown(conn->ssl, 1);
1877 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001878 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879}
1880
1881
1882static int tls_match_altsubject_component(X509 *cert, int type,
1883 const char *value, size_t len)
1884{
1885 GENERAL_NAME *gen;
1886 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001887 int found = 0;
1888 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889
1890 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1891
1892 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1893 gen = sk_GENERAL_NAME_value(ext, i);
1894 if (gen->type != type)
1895 continue;
1896 if (os_strlen((char *) gen->d.ia5->data) == len &&
1897 os_memcmp(value, gen->d.ia5->data, len) == 0)
1898 found++;
1899 }
1900
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001901 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903 return found;
1904}
1905
1906
1907static int tls_match_altsubject(X509 *cert, const char *match)
1908{
1909 int type;
1910 const char *pos, *end;
1911 size_t len;
1912
1913 pos = match;
1914 do {
1915 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1916 type = GEN_EMAIL;
1917 pos += 6;
1918 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1919 type = GEN_DNS;
1920 pos += 4;
1921 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1922 type = GEN_URI;
1923 pos += 4;
1924 } else {
1925 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1926 "match '%s'", pos);
1927 return 0;
1928 }
1929 end = os_strchr(pos, ';');
1930 while (end) {
1931 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1932 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1933 os_strncmp(end + 1, "URI:", 4) == 0)
1934 break;
1935 end = os_strchr(end + 1, ';');
1936 }
1937 if (end)
1938 len = end - pos;
1939 else
1940 len = os_strlen(pos);
1941 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1942 return 1;
1943 pos = end + 1;
1944 } while (end);
1945
1946 return 0;
1947}
1948
1949
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001950#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001951static int domain_suffix_match(const u8 *val, size_t len, const char *match,
Hai Shalom021b0b52019-04-10 11:17:58 -07001952 size_t match_len, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001953{
Hai Shalom021b0b52019-04-10 11:17:58 -07001954 size_t i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001955
1956 /* Check for embedded nuls that could mess up suffix matching */
1957 for (i = 0; i < len; i++) {
1958 if (val[i] == '\0') {
1959 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1960 return 0;
1961 }
1962 }
1963
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001964 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001965 return 0;
1966
1967 if (os_strncasecmp((const char *) val + len - match_len, match,
1968 match_len) != 0)
1969 return 0; /* no match */
1970
1971 if (match_len == len)
1972 return 1; /* exact match */
1973
1974 if (val[len - match_len - 1] == '.')
1975 return 1; /* full label match completes suffix match */
1976
1977 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1978 return 0;
1979}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001980#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001981
1982
Hai Shalom021b0b52019-04-10 11:17:58 -07001983struct tls_dn_field_order_cnt {
1984 u8 cn;
1985 u8 c;
1986 u8 l;
1987 u8 st;
1988 u8 o;
1989 u8 ou;
1990 u8 email;
1991};
1992
1993
1994static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt,
1995 int nid)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001996{
Hai Shalom021b0b52019-04-10 11:17:58 -07001997 switch (nid) {
1998 case NID_commonName:
1999 return dn_cnt->cn;
2000 case NID_countryName:
2001 return dn_cnt->c;
2002 case NID_localityName:
2003 return dn_cnt->l;
2004 case NID_stateOrProvinceName:
2005 return dn_cnt->st;
2006 case NID_organizationName:
2007 return dn_cnt->o;
2008 case NID_organizationalUnitName:
2009 return dn_cnt->ou;
2010 case NID_pkcs9_emailAddress:
2011 return dn_cnt->email;
2012 default:
2013 wpa_printf(MSG_ERROR,
2014 "TLS: Unknown NID '%d' in check_cert_subject",
2015 nid);
2016 return -1;
2017 }
2018}
2019
2020
2021/**
2022 * match_dn_field - Match configuration DN field against Certificate DN field
2023 * @cert: Certificate
2024 * @nid: NID of DN field
2025 * @field: Field name
2026 * @value DN field value which is passed from configuration
2027 * e.g., if configuration have C=US and this argument will point to US.
2028 * @dn_cnt: DN matching context
2029 * Returns: 1 on success and 0 on failure
2030 */
2031static int match_dn_field(const X509 *cert, int nid, const char *field,
2032 const char *value,
2033 const struct tls_dn_field_order_cnt *dn_cnt)
2034{
2035 int i, ret = 0, len, config_dn_field_index, match_index = 0;
2036 X509_NAME *name;
2037
2038 len = os_strlen(value);
2039 name = X509_get_subject_name((X509 *) cert);
2040
2041 /* Assign incremented cnt for every field of DN to check DN field in
2042 * right order */
2043 config_dn_field_index = get_dn_field_index(dn_cnt, nid);
2044 if (config_dn_field_index < 0)
2045 return 0;
2046
2047 /* Fetch value based on NID */
2048 for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) {
2049 X509_NAME_ENTRY *e;
2050 ASN1_STRING *cn;
2051
2052 e = X509_NAME_get_entry(name, i);
2053 if (!e)
2054 continue;
2055
2056 cn = X509_NAME_ENTRY_get_data(e);
2057 if (!cn)
2058 continue;
2059
2060 match_index++;
2061
2062 /* check for more than one DN field with same name */
2063 if (match_index != config_dn_field_index)
2064 continue;
2065
2066 /* Check wildcard at the right end side */
2067 /* E.g., if OU=develop* mentioned in configuration, allow 'OU'
2068 * of the subject in the client certificate to start with
2069 * 'develop' */
2070 if (len > 0 && value[len - 1] == '*') {
2071 /* Compare actual certificate DN field value with
2072 * configuration DN field value up to the specified
2073 * length. */
2074 ret = ASN1_STRING_length(cn) >= len - 1 &&
2075 os_memcmp(ASN1_STRING_get0_data(cn), value,
2076 len - 1) == 0;
2077 } else {
2078 /* Compare actual certificate DN field value with
2079 * configuration DN field value */
2080 ret = ASN1_STRING_length(cn) == len &&
2081 os_memcmp(ASN1_STRING_get0_data(cn), value,
2082 len) == 0;
2083 }
2084 if (!ret) {
2085 wpa_printf(MSG_ERROR,
2086 "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'",
2087 field, value, ASN1_STRING_get0_data(cn));
2088 }
2089 break;
2090 }
2091
2092 return ret;
2093}
2094
2095
2096/**
2097 * get_value_from_field - Get value from DN field
2098 * @cert: Certificate
2099 * @field_str: DN field string which is passed from configuration file (e.g.,
2100 * C=US)
2101 * @dn_cnt: DN matching context
2102 * Returns: 1 on success and 0 on failure
2103 */
2104static int get_value_from_field(const X509 *cert, char *field_str,
2105 struct tls_dn_field_order_cnt *dn_cnt)
2106{
2107 int nid;
2108 char *context = NULL, *name, *value;
2109
2110 if (os_strcmp(field_str, "*") == 0)
2111 return 1; /* wildcard matches everything */
2112
2113 name = str_token(field_str, "=", &context);
2114 if (!name)
2115 return 0;
2116
2117 /* Compare all configured DN fields and assign nid based on that to
2118 * fetch correct value from certificate subject */
2119 if (os_strcmp(name, "CN") == 0) {
2120 nid = NID_commonName;
2121 dn_cnt->cn++;
2122 } else if(os_strcmp(name, "C") == 0) {
2123 nid = NID_countryName;
2124 dn_cnt->c++;
2125 } else if (os_strcmp(name, "L") == 0) {
2126 nid = NID_localityName;
2127 dn_cnt->l++;
2128 } else if (os_strcmp(name, "ST") == 0) {
2129 nid = NID_stateOrProvinceName;
2130 dn_cnt->st++;
2131 } else if (os_strcmp(name, "O") == 0) {
2132 nid = NID_organizationName;
2133 dn_cnt->o++;
2134 } else if (os_strcmp(name, "OU") == 0) {
2135 nid = NID_organizationalUnitName;
2136 dn_cnt->ou++;
2137 } else if (os_strcmp(name, "emailAddress") == 0) {
2138 nid = NID_pkcs9_emailAddress;
2139 dn_cnt->email++;
2140 } else {
2141 wpa_printf(MSG_ERROR,
2142 "TLS: Unknown field '%s' in check_cert_subject", name);
2143 return 0;
2144 }
2145
2146 value = str_token(field_str, "=", &context);
2147 if (!value) {
2148 wpa_printf(MSG_ERROR,
2149 "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject",
2150 name);
2151 return 0;
2152 }
2153
2154 return match_dn_field(cert, nid, name, value, dn_cnt);
2155}
2156
2157
2158/**
2159 * tls_match_dn_field - Match subject DN field with check_cert_subject
2160 * @cert: Certificate
2161 * @match: check_cert_subject string
2162 * Returns: Return 1 on success and 0 on failure
2163*/
2164static int tls_match_dn_field(X509 *cert, const char *match)
2165{
2166 const char *token, *last = NULL;
2167 char field[256];
2168 struct tls_dn_field_order_cnt dn_cnt;
2169
2170 os_memset(&dn_cnt, 0, sizeof(dn_cnt));
2171
2172 /* Maximum length of each DN field is 255 characters */
2173
2174 /* Process each '/' delimited field */
2175 while ((token = cstr_token(match, "/", &last))) {
2176 if (last - token >= (int) sizeof(field)) {
2177 wpa_printf(MSG_ERROR,
2178 "OpenSSL: Too long DN matching field value in '%s'",
2179 match);
2180 return 0;
2181 }
2182 os_memcpy(field, token, last - token);
2183 field[last - token] = '\0';
2184
2185 if (!get_value_from_field(cert, field, &dn_cnt)) {
2186 wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'",
2187 field);
2188 return 0;
2189 }
2190 }
2191
2192 return 1;
2193}
2194
2195
2196#ifndef CONFIG_NATIVE_WINDOWS
2197static int tls_match_suffix_helper(X509 *cert, const char *match,
2198 size_t match_len, int full)
2199{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002200 GENERAL_NAME *gen;
2201 void *ext;
2202 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002203 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002204 int dns_name = 0;
2205 X509_NAME *name;
2206
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002207 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
2208 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002209
2210 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
2211
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002212 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
2213 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002214 if (gen->type != GEN_DNS)
2215 continue;
2216 dns_name++;
2217 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
2218 gen->d.dNSName->data,
2219 gen->d.dNSName->length);
2220 if (domain_suffix_match(gen->d.dNSName->data,
Hai Shalom021b0b52019-04-10 11:17:58 -07002221 gen->d.dNSName->length,
2222 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002223 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
2224 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002225 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002226 return 1;
2227 }
2228 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002229 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002230
2231 if (dns_name) {
2232 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
2233 return 0;
2234 }
2235
2236 name = X509_get_subject_name(cert);
2237 i = -1;
2238 for (;;) {
2239 X509_NAME_ENTRY *e;
2240 ASN1_STRING *cn;
2241
2242 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
2243 if (i == -1)
2244 break;
2245 e = X509_NAME_get_entry(name, i);
2246 if (e == NULL)
2247 continue;
2248 cn = X509_NAME_ENTRY_get_data(e);
2249 if (cn == NULL)
2250 continue;
2251 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
2252 cn->data, cn->length);
Hai Shalom021b0b52019-04-10 11:17:58 -07002253 if (domain_suffix_match(cn->data, cn->length,
2254 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002255 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
2256 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002257 return 1;
2258 }
2259 }
2260
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002261 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
2262 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002263 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07002264}
2265#endif /* CONFIG_NATIVE_WINDOWS */
2266
2267
2268static int tls_match_suffix(X509 *cert, const char *match, int full)
2269{
2270#ifdef CONFIG_NATIVE_WINDOWS
2271 /* wincrypt.h has conflicting X509_NAME definition */
2272 return -1;
2273#else /* CONFIG_NATIVE_WINDOWS */
2274 const char *token, *last = NULL;
2275
2276 /* Process each match alternative separately until a match is found */
2277 while ((token = cstr_token(match, ";", &last))) {
2278 if (tls_match_suffix_helper(cert, token, last - token, full))
2279 return 1;
2280 }
2281
2282 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08002283#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002284}
2285
2286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287static enum tls_fail_reason openssl_tls_fail_reason(int err)
2288{
2289 switch (err) {
2290 case X509_V_ERR_CERT_REVOKED:
2291 return TLS_FAIL_REVOKED;
2292 case X509_V_ERR_CERT_NOT_YET_VALID:
2293 case X509_V_ERR_CRL_NOT_YET_VALID:
2294 return TLS_FAIL_NOT_YET_VALID;
2295 case X509_V_ERR_CERT_HAS_EXPIRED:
2296 case X509_V_ERR_CRL_HAS_EXPIRED:
2297 return TLS_FAIL_EXPIRED;
2298 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
2299 case X509_V_ERR_UNABLE_TO_GET_CRL:
2300 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
2301 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2302 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2303 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2304 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2305 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
2306 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
2307 case X509_V_ERR_INVALID_CA:
2308 return TLS_FAIL_UNTRUSTED;
2309 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
2310 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
2311 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
2312 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
2313 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
2314 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
2315 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
2316 case X509_V_ERR_CERT_UNTRUSTED:
2317 case X509_V_ERR_CERT_REJECTED:
2318 return TLS_FAIL_BAD_CERTIFICATE;
2319 default:
2320 return TLS_FAIL_UNSPECIFIED;
2321 }
2322}
2323
2324
2325static struct wpabuf * get_x509_cert(X509 *cert)
2326{
2327 struct wpabuf *buf;
2328 u8 *tmp;
2329
2330 int cert_len = i2d_X509(cert, NULL);
2331 if (cert_len <= 0)
2332 return NULL;
2333
2334 buf = wpabuf_alloc(cert_len);
2335 if (buf == NULL)
2336 return NULL;
2337
2338 tmp = wpabuf_put(buf, cert_len);
2339 i2d_X509(cert, &tmp);
2340 return buf;
2341}
2342
2343
2344static void openssl_tls_fail_event(struct tls_connection *conn,
2345 X509 *err_cert, int err, int depth,
2346 const char *subject, const char *err_str,
2347 enum tls_fail_reason reason)
2348{
2349 union tls_event_data ev;
2350 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002351 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352
Pavel Grafov4d8552e2018-02-06 11:28:29 +00002353#ifdef ANDROID
2354 log_cert_validation_failure(err_str);
2355#endif
2356
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002357 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 return;
2359
2360 cert = get_x509_cert(err_cert);
2361 os_memset(&ev, 0, sizeof(ev));
2362 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
2363 reason : openssl_tls_fail_reason(err);
2364 ev.cert_fail.depth = depth;
2365 ev.cert_fail.subject = subject;
2366 ev.cert_fail.reason_txt = err_str;
2367 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002368 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 wpabuf_free(cert);
2370}
2371
2372
Hai Shalom81f62d82019-07-22 12:10:00 -07002373static int openssl_cert_tod(X509 *cert)
2374{
2375 CERTIFICATEPOLICIES *ext;
2376 stack_index_t i;
2377 char buf[100];
2378 int res;
2379 int tod = 0;
2380
2381 ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL);
2382 if (!ext)
2383 return 0;
2384
2385 for (i = 0; i < sk_POLICYINFO_num(ext); i++) {
2386 POLICYINFO *policy;
2387
2388 policy = sk_POLICYINFO_value(ext, i);
2389 res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0);
2390 if (res < 0 || (size_t) res >= sizeof(buf))
2391 continue;
2392 wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf);
2393 if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0)
Hai Shalomc3565922019-10-28 11:58:20 -07002394 tod = 1; /* TOD-STRICT */
2395 else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod)
2396 tod = 2; /* TOD-TOFU */
Hai Shalom81f62d82019-07-22 12:10:00 -07002397 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002398 sk_POLICYINFO_pop_free(ext, POLICYINFO_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002399
2400 return tod;
2401}
2402
2403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404static void openssl_tls_cert_event(struct tls_connection *conn,
2405 X509 *err_cert, int depth,
2406 const char *subject)
2407{
2408 struct wpabuf *cert = NULL;
2409 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002410 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002411 char *altsubject[TLS_MAX_ALT_SUBJECT];
2412 int alt, num_altsubject = 0;
2413 GENERAL_NAME *gen;
2414 void *ext;
2415 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002416 ASN1_INTEGER *ser;
2417 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418#ifdef CONFIG_SHA256
2419 u8 hash[32];
2420#endif /* CONFIG_SHA256 */
2421
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002422 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 return;
2424
2425 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002426 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
2427 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428 cert = get_x509_cert(err_cert);
2429 ev.peer_cert.cert = cert;
2430 }
2431#ifdef CONFIG_SHA256
2432 if (cert) {
2433 const u8 *addr[1];
2434 size_t len[1];
2435 addr[0] = wpabuf_head(cert);
2436 len[0] = wpabuf_len(cert);
2437 if (sha256_vector(1, addr, len, hash) == 0) {
2438 ev.peer_cert.hash = hash;
2439 ev.peer_cert.hash_len = sizeof(hash);
2440 }
2441 }
2442#endif /* CONFIG_SHA256 */
2443 ev.peer_cert.depth = depth;
2444 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002445
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002446 ser = X509_get_serialNumber(err_cert);
2447 if (ser) {
2448 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
2449 ASN1_STRING_get0_data(ser),
2450 ASN1_STRING_length(ser));
2451 ev.peer_cert.serial_num = serial_num;
2452 }
2453
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002454 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
2455 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
2456 char *pos;
2457
2458 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
2459 break;
2460 gen = sk_GENERAL_NAME_value(ext, i);
2461 if (gen->type != GEN_EMAIL &&
2462 gen->type != GEN_DNS &&
2463 gen->type != GEN_URI)
2464 continue;
2465
2466 pos = os_malloc(10 + gen->d.ia5->length + 1);
2467 if (pos == NULL)
2468 break;
2469 altsubject[num_altsubject++] = pos;
2470
2471 switch (gen->type) {
2472 case GEN_EMAIL:
2473 os_memcpy(pos, "EMAIL:", 6);
2474 pos += 6;
2475 break;
2476 case GEN_DNS:
2477 os_memcpy(pos, "DNS:", 4);
2478 pos += 4;
2479 break;
2480 case GEN_URI:
2481 os_memcpy(pos, "URI:", 4);
2482 pos += 4;
2483 break;
2484 }
2485
2486 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2487 pos += gen->d.ia5->length;
2488 *pos = '\0';
2489 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002490 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002491
2492 for (alt = 0; alt < num_altsubject; alt++)
2493 ev.peer_cert.altsubject[alt] = altsubject[alt];
2494 ev.peer_cert.num_altsubject = num_altsubject;
2495
Hai Shalom81f62d82019-07-22 12:10:00 -07002496 ev.peer_cert.tod = openssl_cert_tod(err_cert);
2497
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002498 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002500 for (alt = 0; alt < num_altsubject; alt++)
2501 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502}
2503
2504
Hai Shalomc3565922019-10-28 11:58:20 -07002505static void debug_print_cert(X509 *cert, const char *title)
2506{
2507#ifndef CONFIG_NO_STDOUT_DEBUG
2508 BIO *out;
2509 size_t rlen;
2510 char *txt;
2511 int res;
2512
2513 if (wpa_debug_level > MSG_DEBUG)
2514 return;
2515
2516 out = BIO_new(BIO_s_mem());
2517 if (!out)
2518 return;
2519
2520 X509_print(out, cert);
2521 rlen = BIO_ctrl_pending(out);
2522 txt = os_malloc(rlen + 1);
2523 if (txt) {
2524 res = BIO_read(out, txt, rlen);
2525 if (res > 0) {
2526 txt[res] = '\0';
2527 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
2528 }
2529 os_free(txt);
2530 }
2531
2532 BIO_free(out);
2533#endif /* CONFIG_NO_STDOUT_DEBUG */
2534}
2535
2536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2538{
2539 char buf[256];
2540 X509 *err_cert;
2541 int err, depth;
2542 SSL *ssl;
2543 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002544 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002545 char *match, *altmatch, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -07002546 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 const char *err_str;
2548
2549 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002550 if (!err_cert)
2551 return 0;
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 err = X509_STORE_CTX_get_error(x509_ctx);
2554 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2555 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2556 SSL_get_ex_data_X509_STORE_CTX_idx());
Hai Shalomc3565922019-10-28 11:58:20 -07002557 os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth);
2558 debug_print_cert(err_cert, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2560
2561 conn = SSL_get_app_data(ssl);
2562 if (conn == NULL)
2563 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002564
2565 if (depth == 0)
2566 conn->peer_cert = err_cert;
2567 else if (depth == 1)
2568 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002569 else if (depth == 2)
2570 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002571
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002572 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 match = conn->subject_match;
2574 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002575 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002576 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577
2578 if (!preverify_ok && !conn->ca_cert_verify)
2579 preverify_ok = 1;
2580 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2581 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002582 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2583 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2584 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2585 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2586 "time mismatch");
2587 preverify_ok = 1;
2588 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002589 if (!preverify_ok && !conn->data->check_crl_strict &&
2590 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2591 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2592 wpa_printf(MSG_DEBUG,
2593 "OpenSSL: Ignore certificate validity CRL time mismatch");
2594 preverify_ok = 1;
2595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596
2597 err_str = X509_verify_cert_error_string(err);
2598
2599#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002600 /*
2601 * Do not require preverify_ok so we can explicity allow otherwise
2602 * invalid pinned server certificates.
2603 */
2604 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 struct wpabuf *cert;
2606 cert = get_x509_cert(err_cert);
2607 if (!cert) {
2608 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2609 "server certificate data");
2610 preverify_ok = 0;
2611 } else {
2612 u8 hash[32];
2613 const u8 *addr[1];
2614 size_t len[1];
Hai Shalomae89c832023-04-19 15:42:17 -07002615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 addr[0] = wpabuf_head(cert);
2617 len[0] = wpabuf_len(cert);
2618 if (sha256_vector(1, addr, len, hash) < 0 ||
2619 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2620 err_str = "Server certificate mismatch";
2621 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2622 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002623 } else if (!preverify_ok) {
2624 /*
2625 * Certificate matches pinned certificate, allow
2626 * regardless of other problems.
2627 */
2628 wpa_printf(MSG_DEBUG,
2629 "OpenSSL: Ignore validation issues for a pinned server certificate");
2630 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 }
2632 wpabuf_free(cert);
2633 }
2634 }
2635#endif /* CONFIG_SHA256 */
2636
2637 if (!preverify_ok) {
Hai Shalomae89c832023-04-19 15:42:17 -07002638 /* Send cert events for the peer certificate chain so that
2639 * the upper layers get information about it even if
2640 * validation of a CA certificate fails. */
2641 STACK_OF(X509) *chain;
2642 int num_of_certs;
Hai Shalom81f62d82019-07-22 12:10:00 -07002643
Hai Shalomae89c832023-04-19 15:42:17 -07002644 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2645 num_of_certs = sk_X509_num(chain);
2646 if (chain && num_of_certs > 0) {
2647 char buf2[256];
2648 X509 *cert;
2649 int cur_depth;
Hai Shalom81f62d82019-07-22 12:10:00 -07002650
Hai Shalomae89c832023-04-19 15:42:17 -07002651 for (cur_depth = num_of_certs - 1; cur_depth >= 0; cur_depth--) {
2652 cert = sk_X509_value(chain, cur_depth);
Hai Shalom81f62d82019-07-22 12:10:00 -07002653 X509_NAME_oneline(X509_get_subject_name(cert),
2654 buf2, sizeof(buf2));
2655
Hai Shalomae89c832023-04-19 15:42:17 -07002656 openssl_tls_cert_event(conn, cert, cur_depth, buf2);
Hai Shalom81f62d82019-07-22 12:10:00 -07002657 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002658 }
Hai Shalomae89c832023-04-19 15:42:17 -07002659 if (chain)
2660 sk_X509_pop_free(chain, X509_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002661
Gabriel Biren60ae0682023-11-01 22:04:12 +00002662 char *format_str = "TLS: Certificate verification failed,"
2663 " error %d (%s) depth %d for '%s'";
2664 int msg_len = snprintf(NULL, 0, format_str, err, err_str, depth, buf) + 1;
2665 char *msg = os_malloc(msg_len);
2666 snprintf(msg, msg_len, format_str, err, err_str, depth, buf);
2667
2668 wpa_printf(MSG_WARNING, "%s", msg);
2669 if (conn != NULL && conn->context != NULL
2670 && openssl_failure_callback_global != NULL) {
2671 (*openssl_failure_callback_global)(conn->context->cb_ctx, msg);
2672 }
2673 os_free(msg);
2674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2676 err_str, TLS_FAIL_UNSPECIFIED);
2677 return preverify_ok;
2678 }
2679
Hai Shalomae89c832023-04-19 15:42:17 -07002680 openssl_tls_cert_event(conn, err_cert, depth, buf);
2681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2683 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2684 preverify_ok, err, err_str,
2685 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002686 check_cert_subject = conn->check_cert_subject;
2687 if (!check_cert_subject)
2688 check_cert_subject = conn->data->check_cert_subject;
2689 if (check_cert_subject) {
2690 if (depth == 0 &&
2691 !tls_match_dn_field(err_cert, check_cert_subject)) {
2692 preverify_ok = 0;
2693 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2694 "Distinguished Name",
2695 TLS_FAIL_DN_MISMATCH);
2696 }
2697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2699 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2700 "match with '%s'", buf, match);
2701 preverify_ok = 0;
2702 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2703 "Subject mismatch",
2704 TLS_FAIL_SUBJECT_MISMATCH);
2705 } else if (depth == 0 && altmatch &&
2706 !tls_match_altsubject(err_cert, altmatch)) {
2707 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2708 "'%s' not found", altmatch);
2709 preverify_ok = 0;
2710 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2711 "AltSubject mismatch",
2712 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002713 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002714 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002715 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2716 suffix_match);
2717 preverify_ok = 0;
2718 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2719 "Domain suffix mismatch",
2720 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002721 } else if (depth == 0 && domain_match &&
2722 !tls_match_suffix(err_cert, domain_match, 1)) {
2723 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2724 domain_match);
2725 preverify_ok = 0;
2726 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2727 "Domain mismatch",
2728 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002729 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730
2731 if (conn->cert_probe && preverify_ok && depth == 0) {
2732 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2733 "on probe-only run");
2734 preverify_ok = 0;
2735 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2736 "Server certificate chain probe",
2737 TLS_FAIL_SERVER_CHAIN_PROBE);
2738 }
2739
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002740#ifdef CONFIG_SUITEB
2741 if (conn->flags & TLS_CONN_SUITEB) {
2742 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002743 int len = -1;
2744
2745 pk = X509_get_pubkey(err_cert);
2746 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002747 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002748 EVP_PKEY_free(pk);
2749 }
2750
2751 if (len >= 0) {
2752 wpa_printf(MSG_DEBUG,
2753 "OpenSSL: RSA modulus size: %d bits", len);
2754 if (len < 3072) {
2755 preverify_ok = 0;
2756 openssl_tls_fail_event(
2757 conn, err_cert, err,
2758 depth, buf,
2759 "Insufficient RSA modulus size",
2760 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2761 }
2762 }
2763 }
2764#endif /* CONFIG_SUITEB */
2765
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002766#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002767 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2768 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002769 enum ocsp_result res;
2770
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002771 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2772 conn->peer_issuer,
2773 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002774 if (res == OCSP_REVOKED) {
2775 preverify_ok = 0;
2776 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2777 "certificate revoked",
2778 TLS_FAIL_REVOKED);
2779 if (err == X509_V_OK)
2780 X509_STORE_CTX_set_error(
2781 x509_ctx, X509_V_ERR_CERT_REVOKED);
2782 } else if (res != OCSP_GOOD &&
2783 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2784 preverify_ok = 0;
2785 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2786 "bad certificate status response",
2787 TLS_FAIL_UNSPECIFIED);
2788 }
2789 }
2790#endif /* OPENSSL_IS_BORINGSSL */
2791
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002792 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002793 context->event_cb(context->cb_ctx,
2794 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002795
Hai Shalom899fcc72020-10-19 14:38:18 -07002796 if (depth == 0 && preverify_ok) {
2797 os_free(conn->peer_subject);
2798 conn->peer_subject = os_strdup(buf);
2799 }
2800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 return preverify_ok;
2802}
2803
2804
2805#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002806static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002808 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 X509_LOOKUP *lookup;
2810 int ret = 0;
2811
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002812 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 X509_LOOKUP_file());
2814 if (lookup == NULL) {
2815 tls_show_errors(MSG_WARNING, __func__,
2816 "Failed add lookup for X509 store");
2817 return -1;
2818 }
2819
2820 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2821 unsigned long err = ERR_peek_error();
2822 tls_show_errors(MSG_WARNING, __func__,
2823 "Failed load CA in DER format");
2824 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2825 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2826 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2827 "cert already in hash table error",
2828 __func__);
2829 } else
2830 ret = -1;
2831 }
2832
2833 return ret;
2834}
2835#endif /* OPENSSL_NO_STDIO */
2836
2837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002838static int tls_connection_ca_cert(struct tls_data *data,
2839 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840 const char *ca_cert, const u8 *ca_cert_blob,
2841 size_t ca_cert_blob_len, const char *ca_path)
2842{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002843 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002844 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845
2846 /*
2847 * Remove previously configured trusted CA certificates before adding
2848 * new ones.
2849 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002850 store = X509_STORE_new();
2851 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2853 "certificate store", __func__);
2854 return -1;
2855 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002856 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857
2858 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2859 conn->ca_cert_verify = 1;
2860
2861 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2862 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2863 "chain");
2864 conn->cert_probe = 1;
2865 conn->ca_cert_verify = 0;
2866 return 0;
2867 }
2868
2869 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2870#ifdef CONFIG_SHA256
2871 const char *pos = ca_cert + 7;
2872 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2873 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2874 "hash value '%s'", ca_cert);
2875 return -1;
2876 }
2877 pos += 14;
2878 if (os_strlen(pos) != 32 * 2) {
2879 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2880 "hash length in ca_cert '%s'", ca_cert);
2881 return -1;
2882 }
2883 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2884 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2885 "value in ca_cert '%s'", ca_cert);
2886 return -1;
2887 }
2888 conn->server_cert_only = 1;
2889 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2890 "certificate match");
2891 return 0;
2892#else /* CONFIG_SHA256 */
2893 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2894 "cannot validate server certificate hash");
2895 return -1;
2896#endif /* CONFIG_SHA256 */
2897 }
2898
2899 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002900 X509 *cert = d2i_X509(NULL,
2901 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 ca_cert_blob_len);
2903 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002904 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2905 ca_cert_blob_len);
2906
2907 if (bio) {
2908 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2909 BIO_free(bio);
2910 }
2911
2912 if (!cert) {
2913 tls_show_errors(MSG_WARNING, __func__,
2914 "Failed to parse ca_cert_blob");
2915 return -1;
2916 }
2917
2918 while (ERR_get_error()) {
2919 /* Ignore errors from DER conversion. */
2920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 }
2922
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002923 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2924 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 unsigned long err = ERR_peek_error();
2926 tls_show_errors(MSG_WARNING, __func__,
2927 "Failed to add ca_cert_blob to "
2928 "certificate store");
2929 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2930 ERR_GET_REASON(err) ==
2931 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2932 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2933 "cert already in hash table error",
2934 __func__);
2935 } else {
2936 X509_free(cert);
2937 return -1;
2938 }
2939 }
2940 X509_free(cert);
2941 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2942 "to certificate store", __func__);
2943 return 0;
2944 }
2945
2946#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002947 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002948 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2949 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002950 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Gabriel Birenff4f8382023-04-06 20:14:39 +00002951 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002953 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2954 return 0;
2955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002957 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002958 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2959 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2960 char *aliases = os_strdup(
2961 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002962 const char *delim = " ";
2963 int rc = 0;
2964 char *savedptr;
2965 char *alias;
2966
2967 if (!aliases)
2968 return -1;
2969 alias = strtok_r(aliases, delim, &savedptr);
2970 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2971 if (tls_add_ca_from_keystore_encoded(
Gabriel Birenff4f8382023-04-06 20:14:39 +00002972 SSL_CTX_get_cert_store(ssl_ctx), alias, conn)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -07002973 wpa_printf(MSG_ERROR,
2974 "OpenSSL: Failed to add ca_cert %s from keystore",
2975 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002976 rc = -1;
2977 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 }
2979 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002980 os_free(aliases);
2981 if (rc)
2982 return rc;
2983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2985 return 0;
2986 }
2987#endif /* ANDROID */
2988
2989#ifdef CONFIG_NATIVE_WINDOWS
2990 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2991 0) {
2992 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2993 "system certificate store");
2994 return 0;
2995 }
2996#endif /* CONFIG_NATIVE_WINDOWS */
2997
2998 if (ca_cert || ca_path) {
2999#ifndef OPENSSL_NO_STDIO
3000 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
3001 1) {
3002 tls_show_errors(MSG_WARNING, __func__,
3003 "Failed to load root certificates");
3004 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003005 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
3007 "DER format CA certificate",
3008 __func__);
3009 } else
3010 return -1;
3011 } else {
3012 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
3013 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003014 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 }
3016#else /* OPENSSL_NO_STDIO */
3017 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
3018 __func__);
3019 return -1;
3020#endif /* OPENSSL_NO_STDIO */
3021 } else {
3022 /* No ca_cert configured - do not try to verify server
3023 * certificate */
3024 conn->ca_cert_verify = 0;
3025 }
3026
3027 return 0;
3028}
3029
3030
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003031static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003033 SSL_CTX *ssl_ctx = data->ssl;
3034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 if (ca_cert) {
3036 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
3037 {
3038 tls_show_errors(MSG_WARNING, __func__,
3039 "Failed to load root certificates");
3040 return -1;
3041 }
3042
3043 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
3044 "certificate(s) loaded");
3045
3046#ifndef OPENSSL_NO_STDIO
3047 /* Add the same CAs to the client certificate requests */
3048 SSL_CTX_set_client_CA_list(ssl_ctx,
3049 SSL_load_client_CA_file(ca_cert));
3050#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003051
3052 os_free(data->ca_cert);
3053 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 }
3055
3056 return 0;
3057}
3058
3059
Hai Shalom74f70d42019-02-11 14:42:39 -08003060int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061{
3062 int flags;
3063
3064 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065 struct tls_data *data = ssl_ctx;
3066 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 if (cs == NULL) {
3068 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3069 "certificate store when enabling "
3070 "check_crl");
3071 return -1;
3072 }
3073 flags = X509_V_FLAG_CRL_CHECK;
3074 if (check_crl == 2)
3075 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3076 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003077
3078 data->check_crl = check_crl;
3079 data->check_crl_strict = strict;
3080 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 }
3082 return 0;
3083}
3084
3085
3086static int tls_connection_set_subject_match(struct tls_connection *conn,
3087 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003088 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003089 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003090 const char *domain_match,
3091 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092{
3093 os_free(conn->subject_match);
3094 conn->subject_match = NULL;
3095 if (subject_match) {
3096 conn->subject_match = os_strdup(subject_match);
3097 if (conn->subject_match == NULL)
3098 return -1;
3099 }
3100
3101 os_free(conn->altsubject_match);
3102 conn->altsubject_match = NULL;
3103 if (altsubject_match) {
3104 conn->altsubject_match = os_strdup(altsubject_match);
3105 if (conn->altsubject_match == NULL)
3106 return -1;
3107 }
3108
Dmitry Shmidt051af732013-10-22 13:52:46 -07003109 os_free(conn->suffix_match);
3110 conn->suffix_match = NULL;
3111 if (suffix_match) {
3112 conn->suffix_match = os_strdup(suffix_match);
3113 if (conn->suffix_match == NULL)
3114 return -1;
3115 }
3116
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003117 os_free(conn->domain_match);
3118 conn->domain_match = NULL;
3119 if (domain_match) {
3120 conn->domain_match = os_strdup(domain_match);
3121 if (conn->domain_match == NULL)
3122 return -1;
3123 }
3124
Hai Shalom021b0b52019-04-10 11:17:58 -07003125 os_free(conn->check_cert_subject);
3126 conn->check_cert_subject = NULL;
3127 if (check_cert_subject) {
3128 conn->check_cert_subject = os_strdup(check_cert_subject);
3129 if (!conn->check_cert_subject)
3130 return -1;
3131 }
3132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 return 0;
3134}
3135
3136
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003137#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003138static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003139{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003140 struct tls_connection *conn = arg;
3141
3142 /*
3143 * This cert_cb() is not really the best location for doing a
3144 * constraint check for the ServerKeyExchange message, but this seems to
3145 * be the only place where the current OpenSSL sequence can be
3146 * terminated cleanly with an TLS alert going out to the server.
3147 */
3148
3149 if (!(conn->flags & TLS_CONN_SUITEB))
3150 return 1;
3151
3152 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3153 if (conn->cipher_suite != 0x9f)
3154 return 1;
3155
3156 if (conn->server_dh_prime_len >= 3072)
3157 return 1;
3158
3159 wpa_printf(MSG_DEBUG,
3160 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3161 conn->server_dh_prime_len);
3162 return 0;
3163}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003164#endif /* CONFIG_SUITEB */
3165
3166
Roshan Pius3a1667e2018-07-03 15:17:14 -07003167static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3168 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003169{
3170 SSL *ssl = conn->ssl;
3171
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003172#ifdef SSL_OP_NO_TICKET
3173 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3174 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003175 else
3176 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003177#endif /* SSL_OP_NO_TICKET */
3178
Sunil Ravia04bd252022-05-02 22:54:18 -07003179#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3180 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3181 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3182#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3183
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003184#ifdef SSL_OP_NO_TLSv1
3185 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3186 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3187 else
3188 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3189#endif /* SSL_OP_NO_TLSv1 */
3190#ifdef SSL_OP_NO_TLSv1_1
3191 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3192 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3193 else
3194 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3195#endif /* SSL_OP_NO_TLSv1_1 */
3196#ifdef SSL_OP_NO_TLSv1_2
3197 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3198 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3199 else
3200 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3201#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003202#ifdef SSL_OP_NO_TLSv1_3
3203 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3204 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3205 else
3206 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3207#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003208#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3209 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3210 TLS_CONN_ENABLE_TLSv1_1 |
3211 TLS_CONN_ENABLE_TLSv1_2)) {
3212 int version = 0;
3213
3214 /* Explicit request to enable TLS versions even if needing to
3215 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003216 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003217 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003218 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3219 version = TLS1_1_VERSION;
3220 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3221 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003222 if (!version) {
3223 wpa_printf(MSG_DEBUG,
3224 "OpenSSL: Invalid TLS version configuration");
3225 return -1;
3226 }
3227
3228 if (SSL_set_min_proto_version(ssl, version) != 1) {
3229 wpa_printf(MSG_DEBUG,
3230 "OpenSSL: Failed to set minimum TLS version");
3231 return -1;
3232 }
3233 }
3234#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003235#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3236 !defined(LIBRESSL_VERSION_NUMBER) && \
3237 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003238 {
3239#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3240 int need_level = 0;
3241#else
3242 int need_level = 1;
3243#endif
3244
3245 if ((flags &
3246 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3247 SSL_get_security_level(ssl) > need_level) {
3248 /*
3249 * Need to drop to security level 1 (or 0 with OpenSSL
3250 * 3.0) to allow TLS versions older than 1.2 to be used
3251 * when explicitly enabled in configuration.
3252 */
3253 SSL_set_security_level(conn->ssl, need_level);
3254 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003255 }
3256#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003257
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003258 if (!openssl_ciphers)
3259 openssl_ciphers = conn->data->openssl_ciphers;
3260
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003261#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003262#ifdef OPENSSL_IS_BORINGSSL
3263 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003264 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003265#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003266 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3267 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3268
Roshan Pius3a1667e2018-07-03 15:17:14 -07003269 if (openssl_ciphers) {
3270 wpa_printf(MSG_DEBUG,
3271 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3272 openssl_ciphers);
3273 ciphers = openssl_ciphers;
3274 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003275 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3276 wpa_printf(MSG_INFO,
3277 "OpenSSL: Failed to set Suite B ciphers");
3278 return -1;
3279 }
3280 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003281#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003282 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003283#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003284 const char *ciphers =
3285 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003286 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003287
Roshan Pius3a1667e2018-07-03 15:17:14 -07003288 if (openssl_ciphers) {
3289 wpa_printf(MSG_DEBUG,
3290 "OpenSSL: Override ciphers for Suite B: %s",
3291 openssl_ciphers);
3292 ciphers = openssl_ciphers;
3293 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003294 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3295 wpa_printf(MSG_INFO,
3296 "OpenSSL: Failed to set Suite B ciphers");
3297 return -1;
3298 }
3299
Sunil Ravia04bd252022-05-02 22:54:18 -07003300#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3301 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3302 wpa_printf(MSG_INFO,
3303 "OpenSSL: Failed to set Suite B groups");
3304 return -1;
3305 }
3306
3307#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003308 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003309 wpa_printf(MSG_INFO,
3310 "OpenSSL: Failed to set Suite B curves");
3311 return -1;
3312 }
3313
3314 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3315 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3316 EC_KEY_free(ecdh);
3317 wpa_printf(MSG_INFO,
3318 "OpenSSL: Failed to set ECDH parameter");
3319 return -1;
3320 }
3321 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003322#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003323 }
Hai Shalom0f94b7a2023-03-13 13:22:35 -07003324 if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH))
3325#ifdef EAP_TLSV1_3
3326 && (flags & TLS_CONN_DISABLE_TLSv1_3)
3327#endif
3328 ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003329#ifdef OPENSSL_IS_BORINGSSL
3330 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3331
Jimmy Chen916e0a72022-01-11 15:19:46 +08003332 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003333 1) != 1) {
3334 wpa_printf(MSG_INFO,
3335 "OpenSSL: Failed to set Suite B sigalgs");
3336 return -1;
3337 }
3338#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003339 /* ECDSA+SHA384 if need to add EC support here */
3340 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3341 wpa_printf(MSG_INFO,
3342 "OpenSSL: Failed to set Suite B sigalgs");
3343 return -1;
3344 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003345#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003346
3347 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3348 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3349 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3350 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003351
3352#ifdef OPENSSL_IS_BORINGSSL
3353 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3354 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3355 int nid[1] = { NID_secp384r1 };
3356
3357 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3358 wpa_printf(MSG_INFO,
3359 "OpenSSL: Failed to set Suite B curves");
3360 return -1;
3361 }
3362
Jimmy Chen916e0a72022-01-11 15:19:46 +08003363 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003364 1) != 1) {
3365 wpa_printf(MSG_INFO,
3366 "OpenSSL: Failed to set Suite B sigalgs");
3367 return -1;
3368 }
3369 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003370#else /* OPENSSL_IS_BORINGSSL */
3371 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3372 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3373 wpa_printf(MSG_INFO,
3374 "OpenSSL: Failed to set openssl_ciphers '%s'",
3375 openssl_ciphers);
3376 return -1;
3377 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003378#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003379#else /* CONFIG_SUITEB */
3380 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3381 wpa_printf(MSG_INFO,
3382 "OpenSSL: Failed to set openssl_ciphers '%s'",
3383 openssl_ciphers);
3384 return -1;
3385 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003386#endif /* CONFIG_SUITEB */
3387
Hai Shalom81f62d82019-07-22 12:10:00 -07003388 if (flags & TLS_CONN_TEAP_ANON_DH) {
3389#ifndef TEAP_DH_ANON_CS
3390#define TEAP_DH_ANON_CS \
3391 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3392 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3393 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3394 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3395 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3396 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3397 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3398 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3399#endif
3400 static const char *cs = TEAP_DH_ANON_CS;
3401
3402#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3403 !defined(LIBRESSL_VERSION_NUMBER) && \
3404 !defined(OPENSSL_IS_BORINGSSL)
3405 /*
3406 * Need to drop to security level 0 to allow anonymous
3407 * cipher suites for EAP-TEAP.
3408 */
3409 SSL_set_security_level(conn->ssl, 0);
3410#endif
3411
3412 wpa_printf(MSG_DEBUG,
3413 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3414 cs);
3415 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3416 tls_show_errors(MSG_INFO, __func__,
3417 "Cipher suite configuration failed");
3418 return -1;
3419 }
3420 }
3421
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003422 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003423}
3424
3425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003427 int verify_peer, unsigned int flags,
3428 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429{
3430 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003431 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432
3433 if (conn == NULL)
3434 return -1;
3435
Hai Shalom899fcc72020-10-19 14:38:18 -07003436 if (verify_peer == 2) {
3437 conn->ca_cert_verify = 1;
3438 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3439 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3440 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 conn->ca_cert_verify = 1;
3442 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3443 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3444 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3445 } else {
3446 conn->ca_cert_verify = 0;
3447 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3448 }
3449
Roshan Pius3a1667e2018-07-03 15:17:14 -07003450 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003451 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003452 conn->flags = flags;
3453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 SSL_set_accept_state(conn->ssl);
3455
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 if (data->tls_session_lifetime == 0) {
3457 /*
3458 * Set session id context to a unique value to make sure
3459 * session resumption cannot be used either through session
3460 * caching or TLS ticket extension.
3461 */
3462 counter++;
3463 SSL_set_session_id_context(conn->ssl,
3464 (const unsigned char *) &counter,
3465 sizeof(counter));
3466 } else if (session_ctx) {
3467 SSL_set_session_id_context(conn->ssl, session_ctx,
3468 session_ctx_len);
3469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003470
3471 return 0;
3472}
3473
3474
3475static int tls_connection_client_cert(struct tls_connection *conn,
3476 const char *client_cert,
3477 const u8 *client_cert_blob,
3478 size_t client_cert_blob_len)
3479{
3480 if (client_cert == NULL && client_cert_blob == NULL)
3481 return 0;
3482
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003483#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003484#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003485 /*
3486 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003487 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003488 * chain properly.
3489 */
3490 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003491#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003492#endif /* PKCS12_FUNCS */
3493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 if (client_cert_blob &&
3495 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3496 client_cert_blob_len) == 1) {
3497 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3498 "OK");
3499 return 0;
3500 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003501#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 tls_show_errors(MSG_DEBUG, __func__,
3503 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003504#else
3505 BIO *bio;
3506 X509 *x509;
3507
3508 tls_show_errors(MSG_DEBUG, __func__,
3509 "SSL_use_certificate_ASN1 failed");
3510 bio = BIO_new(BIO_s_mem());
3511 if (!bio)
3512 return -1;
3513 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3514 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3515 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3516 X509_free(x509);
3517 BIO_free(bio);
3518 return -1;
3519 }
3520 X509_free(x509);
3521 wpa_printf(MSG_DEBUG,
3522 "OpenSSL: Found PEM encoded certificate from blob");
3523 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3524 wpa_printf(MSG_DEBUG,
3525 "OpenSSL: Added an additional certificate into the chain");
3526 SSL_add0_chain_cert(conn->ssl, x509);
3527 }
3528 BIO_free(bio);
3529 return 0;
3530#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 }
3532
3533 if (client_cert == NULL)
3534 return -1;
3535
3536#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003537 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3538 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Gabriel Birenff4f8382023-04-06 20:14:39 +00003539 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003541 if (!bio) {
3542 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003543 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003544 // Keystore returns X.509 certificates in PEM encoding
3545 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3546 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003548 BIO_free(bio);
3549 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3550 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003552 X509_free(x509);
3553 wpa_printf(MSG_DEBUG,
3554 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3555 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003556
Hai Shalom7ad2a872021-08-02 18:56:55 -07003557 // Read additional certificates into the chain
3558 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3559 wpa_printf(MSG_DEBUG,
3560 "OpenSSL: Added an additional certificate into the chain");
3561 // Takes ownership of x509, no need to free it here
3562 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003563 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003564 BIO_free(bio);
3565 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 }
3567#endif /* ANDROID */
3568
3569#ifndef OPENSSL_NO_STDIO
3570 if (SSL_use_certificate_file(conn->ssl, client_cert,
3571 SSL_FILETYPE_ASN1) == 1) {
3572 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3573 " --> OK");
3574 return 0;
3575 }
3576
Hai Shalom021b0b52019-04-10 11:17:58 -07003577#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3578 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003579 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3580 ERR_clear_error();
3581 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3582 " --> OK");
3583 return 0;
3584 }
3585#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (SSL_use_certificate_file(conn->ssl, client_cert,
3587 SSL_FILETYPE_PEM) == 1) {
3588 ERR_clear_error();
3589 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3590 " --> OK");
3591 return 0;
3592 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003593#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594
3595 tls_show_errors(MSG_DEBUG, __func__,
3596 "SSL_use_certificate_file failed");
3597#else /* OPENSSL_NO_STDIO */
3598 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3599#endif /* OPENSSL_NO_STDIO */
3600
3601 return -1;
3602}
3603
3604
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003605static int tls_global_client_cert(struct tls_data *data,
3606 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607{
3608#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003609 SSL_CTX *ssl_ctx = data->ssl;
3610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 if (client_cert == NULL)
3612 return 0;
3613
3614 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3615 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003616 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3618 SSL_FILETYPE_PEM) != 1) {
3619 tls_show_errors(MSG_INFO, __func__,
3620 "Failed to load client certificate");
3621 return -1;
3622 }
3623 return 0;
3624#else /* OPENSSL_NO_STDIO */
3625 if (client_cert == NULL)
3626 return 0;
3627 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3628 return -1;
3629#endif /* OPENSSL_NO_STDIO */
3630}
3631
3632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003634static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 const char *passwd)
3636{
3637 EVP_PKEY *pkey;
3638 X509 *cert;
3639 STACK_OF(X509) *certs;
3640 int res = 0;
3641 char buf[256];
3642
3643 pkey = NULL;
3644 cert = NULL;
3645 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003646 if (!passwd)
3647 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3649 tls_show_errors(MSG_DEBUG, __func__,
3650 "Failed to parse PKCS12 file");
3651 PKCS12_free(p12);
3652 return -1;
3653 }
3654 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3655
3656 if (cert) {
3657 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3658 sizeof(buf));
3659 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3660 "subject='%s'", buf);
3661 if (ssl) {
3662 if (SSL_use_certificate(ssl, cert) != 1)
3663 res = -1;
3664 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003665 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 res = -1;
3667 }
3668 X509_free(cert);
3669 }
3670
3671 if (pkey) {
3672 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3673 if (ssl) {
3674 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3675 res = -1;
3676 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003677 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 res = -1;
3679 }
3680 EVP_PKEY_free(pkey);
3681 }
3682
3683 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003684#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003685 if (ssl)
3686 SSL_clear_chain_certs(ssl);
3687 else
3688 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003689 while ((cert = sk_X509_pop(certs)) != NULL) {
3690 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3691 sizeof(buf));
3692 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3693 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003694 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3695 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3696 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003697 tls_show_errors(MSG_DEBUG, __func__,
3698 "Failed to add additional certificate");
3699 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003700 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003701 break;
3702 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003703 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003704 }
3705 if (!res) {
3706 /* Try to continue anyway */
3707 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003708 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003709#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003710 if (ssl)
3711 res = SSL_build_cert_chain(
3712 ssl,
3713 SSL_BUILD_CHAIN_FLAG_CHECK |
3714 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3715 else
3716 res = SSL_CTX_build_cert_chain(
3717 data->ssl,
3718 SSL_BUILD_CHAIN_FLAG_CHECK |
3719 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003720 if (!res) {
3721 tls_show_errors(MSG_DEBUG, __func__,
3722 "Failed to build certificate chain");
3723 } else if (res == 2) {
3724 wpa_printf(MSG_DEBUG,
3725 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3726 }
3727#endif /* OPENSSL_IS_BORINGSSL */
3728 /*
3729 * Try to continue regardless of result since it is possible for
3730 * the extra certificates not to be required.
3731 */
3732 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003733#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003734 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735 while ((cert = sk_X509_pop(certs)) != NULL) {
3736 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3737 sizeof(buf));
3738 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3739 " from PKCS12: subject='%s'", buf);
3740 /*
3741 * There is no SSL equivalent for the chain cert - so
3742 * always add it to the context...
3743 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003744 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3745 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003746 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 res = -1;
3748 break;
3749 }
3750 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003751 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003752#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 }
3754
3755 PKCS12_free(p12);
3756
3757 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003758 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759
3760 return res;
3761}
3762#endif /* PKCS12_FUNCS */
3763
3764
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003765static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3766 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767{
3768#ifdef PKCS12_FUNCS
3769 FILE *f;
3770 PKCS12 *p12;
3771
3772 f = fopen(private_key, "rb");
3773 if (f == NULL)
3774 return -1;
3775
3776 p12 = d2i_PKCS12_fp(f, NULL);
3777 fclose(f);
3778
3779 if (p12 == NULL) {
3780 tls_show_errors(MSG_INFO, __func__,
3781 "Failed to use PKCS#12 file");
3782 return -1;
3783 }
3784
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003785 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786
3787#else /* PKCS12_FUNCS */
3788 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3789 "p12/pfx files");
3790 return -1;
3791#endif /* PKCS12_FUNCS */
3792}
3793
3794
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003795static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 const u8 *blob, size_t len, const char *passwd)
3797{
3798#ifdef PKCS12_FUNCS
3799 PKCS12 *p12;
3800
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003801 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 if (p12 == NULL) {
3803 tls_show_errors(MSG_INFO, __func__,
3804 "Failed to use PKCS#12 blob");
3805 return -1;
3806 }
3807
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003808 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809
3810#else /* PKCS12_FUNCS */
3811 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3812 "p12/pfx blobs");
3813 return -1;
3814#endif /* PKCS12_FUNCS */
3815}
3816
3817
3818#ifndef OPENSSL_NO_ENGINE
3819static int tls_engine_get_cert(struct tls_connection *conn,
3820 const char *cert_id,
3821 X509 **cert)
3822{
3823 /* this runs after the private key is loaded so no PIN is required */
3824 struct {
3825 const char *cert_id;
3826 X509 *cert;
3827 } params;
3828 params.cert_id = cert_id;
3829 params.cert = NULL;
3830
3831 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3832 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003833 unsigned long err = ERR_get_error();
3834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3836 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003837 ERR_error_string(err, NULL));
3838 if (tls_is_pin_error(err))
3839 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3841 }
3842 if (!params.cert) {
3843 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3844 " '%s'", cert_id);
3845 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3846 }
3847 *cert = params.cert;
3848 return 0;
3849}
3850#endif /* OPENSSL_NO_ENGINE */
3851
3852
3853static int tls_connection_engine_client_cert(struct tls_connection *conn,
3854 const char *cert_id)
3855{
3856#ifndef OPENSSL_NO_ENGINE
3857 X509 *cert;
3858
3859 if (tls_engine_get_cert(conn, cert_id, &cert))
3860 return -1;
3861
3862 if (!SSL_use_certificate(conn->ssl, cert)) {
3863 tls_show_errors(MSG_ERROR, __func__,
3864 "SSL_use_certificate failed");
3865 X509_free(cert);
3866 return -1;
3867 }
3868 X509_free(cert);
3869 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3870 "OK");
3871 return 0;
3872
3873#else /* OPENSSL_NO_ENGINE */
3874 return -1;
3875#endif /* OPENSSL_NO_ENGINE */
3876}
3877
3878
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003879static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 struct tls_connection *conn,
3881 const char *ca_cert_id)
3882{
3883#ifndef OPENSSL_NO_ENGINE
3884 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003885 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003886 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887
3888 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3889 return -1;
3890
3891 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003892 store = X509_STORE_new();
3893 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3895 "certificate store", __func__);
3896 X509_free(cert);
3897 return -1;
3898 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003899 SSL_CTX_set_cert_store(ssl_ctx, store);
3900 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 unsigned long err = ERR_peek_error();
3902 tls_show_errors(MSG_WARNING, __func__,
3903 "Failed to add CA certificate from engine "
3904 "to certificate store");
3905 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3906 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3907 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3908 " already in hash table error",
3909 __func__);
3910 } else {
3911 X509_free(cert);
3912 return -1;
3913 }
3914 }
3915 X509_free(cert);
3916 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3917 "to certificate store", __func__);
3918 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003919 conn->ca_cert_verify = 1;
3920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 return 0;
3922
3923#else /* OPENSSL_NO_ENGINE */
3924 return -1;
3925#endif /* OPENSSL_NO_ENGINE */
3926}
3927
3928
3929static int tls_connection_engine_private_key(struct tls_connection *conn)
3930{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003931#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3933 tls_show_errors(MSG_ERROR, __func__,
3934 "ENGINE: cannot use private key for TLS");
3935 return -1;
3936 }
3937 if (!SSL_check_private_key(conn->ssl)) {
3938 tls_show_errors(MSG_INFO, __func__,
3939 "Private key failed verification");
3940 return -1;
3941 }
3942 return 0;
3943#else /* OPENSSL_NO_ENGINE */
3944 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3945 "engine support was not compiled in");
3946 return -1;
3947#endif /* OPENSSL_NO_ENGINE */
3948}
3949
3950
Roshan Pius3a1667e2018-07-03 15:17:14 -07003951#ifndef OPENSSL_NO_STDIO
3952static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003953{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003954 if (!password)
3955 return 0;
3956 os_strlcpy(buf, (const char *) password, size);
3957 return os_strlen(buf);
3958}
3959#endif /* OPENSSL_NO_STDIO */
3960
3961
3962static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3963 const char *private_key,
3964 const char *private_key_passwd)
3965{
3966#ifndef OPENSSL_NO_STDIO
3967 BIO *bio;
3968 EVP_PKEY *pkey;
3969 int ret;
3970
3971 /* First try ASN.1 (DER). */
3972 bio = BIO_new_file(private_key, "r");
3973 if (!bio)
3974 return -1;
3975 pkey = d2i_PrivateKey_bio(bio, NULL);
3976 BIO_free(bio);
3977
3978 if (pkey) {
3979 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3980 } else {
3981 /* Try PEM with the provided password. */
3982 bio = BIO_new_file(private_key, "r");
3983 if (!bio)
3984 return -1;
3985 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3986 (void *) private_key_passwd);
3987 BIO_free(bio);
3988 if (!pkey)
3989 return -1;
3990 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3991 /* Clear errors from the previous failed load. */
3992 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003993 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003994
3995 if (ssl)
3996 ret = SSL_use_PrivateKey(ssl, pkey);
3997 else
3998 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3999
4000 EVP_PKEY_free(pkey);
4001 return ret == 1 ? 0 : -1;
4002#else /* OPENSSL_NO_STDIO */
4003 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
4004 return -1;
4005#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004006}
4007
4008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004009static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 struct tls_connection *conn,
4011 const char *private_key,
4012 const char *private_key_passwd,
4013 const u8 *private_key_blob,
4014 size_t private_key_blob_len)
4015{
Hai Shaloma20dcd72022-02-04 13:43:00 -08004016 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 int ok;
4018
4019 if (private_key == NULL && private_key_blob == NULL)
4020 return 0;
4021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 ok = 0;
4023 while (private_key_blob) {
4024 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
4025 (u8 *) private_key_blob,
4026 private_key_blob_len) == 1) {
4027 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
4028 "ASN1(EVP_PKEY_RSA) --> OK");
4029 ok = 1;
4030 break;
4031 }
4032
4033 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
4034 (u8 *) private_key_blob,
4035 private_key_blob_len) == 1) {
4036 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
4037 "ASN1(EVP_PKEY_DSA) --> OK");
4038 ok = 1;
4039 break;
4040 }
4041
Hai Shalom899fcc72020-10-19 14:38:18 -07004042#ifndef OPENSSL_NO_EC
4043 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
4044 (u8 *) private_key_blob,
4045 private_key_blob_len) == 1) {
4046 wpa_printf(MSG_DEBUG,
4047 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
4048 ok = 1;
4049 break;
4050 }
4051#endif /* OPENSSL_NO_EC */
4052
Sunil Ravia04bd252022-05-02 22:54:18 -07004053#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4055 (u8 *) private_key_blob,
4056 private_key_blob_len) == 1) {
4057 wpa_printf(MSG_DEBUG, "OpenSSL: "
4058 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4059 ok = 1;
4060 break;
4061 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004062#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063
Hai Shaloma20dcd72022-02-04 13:43:00 -08004064 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4065 private_key_blob_len);
4066 if (bio) {
4067 EVP_PKEY *pkey;
4068
4069 pkey = PEM_read_bio_PrivateKey(
4070 bio, NULL, tls_passwd_cb,
4071 (void *) private_key_passwd);
4072 if (pkey) {
4073 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4074 wpa_printf(MSG_DEBUG,
4075 "OpenSSL: SSL_use_PrivateKey --> OK");
4076 ok = 1;
4077 EVP_PKEY_free(pkey);
4078 BIO_free(bio);
4079 break;
4080 }
4081 EVP_PKEY_free(pkey);
4082 }
4083 BIO_free(bio);
4084 }
4085
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004086 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004087 private_key_blob_len,
4088 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4090 "OK");
4091 ok = 1;
4092 break;
4093 }
4094
4095 break;
4096 }
4097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004099 if (tls_use_private_key_file(data, conn->ssl, private_key,
4100 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 ok = 1;
4102 break;
4103 }
4104
Roshan Pius3a1667e2018-07-03 15:17:14 -07004105 if (tls_read_pkcs12(data, conn->ssl, private_key,
4106 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4108 "--> OK");
4109 ok = 1;
4110 break;
4111 }
4112
4113 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4114 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4115 "access certificate store --> OK");
4116 ok = 1;
4117 break;
4118 }
4119
4120 break;
4121 }
4122
4123 if (!ok) {
4124 tls_show_errors(MSG_INFO, __func__,
4125 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 return -1;
4127 }
4128 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 if (!SSL_check_private_key(conn->ssl)) {
4131 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4132 "verification");
4133 return -1;
4134 }
4135
4136 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4137 return 0;
4138}
4139
4140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004141static int tls_global_private_key(struct tls_data *data,
4142 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 const char *private_key_passwd)
4144{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004145 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146
4147 if (private_key == NULL)
4148 return 0;
4149
Roshan Pius3a1667e2018-07-03 15:17:14 -07004150 if (tls_use_private_key_file(data, NULL, private_key,
4151 private_key_passwd) &&
4152 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153 tls_show_errors(MSG_INFO, __func__,
4154 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155 ERR_clear_error();
4156 return -1;
4157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4161 tls_show_errors(MSG_INFO, __func__,
4162 "Private key failed verification");
4163 return -1;
4164 }
4165
4166 return 0;
4167}
4168
4169
Sunil Ravia04bd252022-05-02 22:54:18 -07004170#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4171#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004173/* This is needed to replace the deprecated DSA_dup_DH() function */
4174static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4175{
4176 OSSL_PARAM_BLD *bld = NULL;
4177 OSSL_PARAM *params = NULL;
4178 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4179 EVP_PKEY_CTX *ctx = NULL;
4180 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181
Sunil Ravia04bd252022-05-02 22:54:18 -07004182 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4183 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4184 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4185 !(bld = OSSL_PARAM_BLD_new()) ||
4186 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4187 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4188 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4189 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4190 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4191 EVP_PKEY_fromdata_init(ctx) != 1 ||
4192 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4193 params) != 1)
4194 wpa_printf(MSG_INFO,
4195 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196
Sunil Ravia04bd252022-05-02 22:54:18 -07004197 EVP_PKEY_CTX_free(ctx);
4198 OSSL_PARAM_free(params);
4199 OSSL_PARAM_BLD_free(bld);
4200 BN_free(p);
4201 BN_free(q);
4202 BN_free(g);
4203 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204}
Sunil Ravia04bd252022-05-02 22:54:18 -07004205#endif /* !OPENSSL_NO_DSA */
4206#endif /* OPENSSL_NO_DH */
4207#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004209static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210{
4211#ifdef OPENSSL_NO_DH
4212 if (dh_file == NULL)
4213 return 0;
4214 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4215 "dh_file specified");
4216 return -1;
4217#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004218#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4219 SSL_CTX *ssl_ctx = data->ssl;
4220 BIO *bio;
4221 OSSL_DECODER_CTX *ctx = NULL;
4222 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4223 bool dsa = false;
4224
4225 if (!ssl_ctx)
4226 return -1;
4227 if (!dh_file) {
4228 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4229 return 0;
4230 }
4231
4232 bio = BIO_new_file(dh_file, "r");
4233 if (!bio) {
4234 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4235 dh_file, ERR_error_string(ERR_get_error(), NULL));
4236 return -1;
4237 }
4238 ctx = OSSL_DECODER_CTX_new_for_pkey(
4239 &tmpkey, "PEM", NULL, NULL,
4240 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4241 if (!ctx ||
4242 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4243 wpa_printf(MSG_INFO,
4244 "TLS: Failed to decode domain parameters from '%s': %s",
4245 dh_file, ERR_error_string(ERR_get_error(), NULL));
4246 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004247 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004248 return -1;
4249 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004250 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004251 BIO_free(bio);
4252
4253 if (!tmpkey) {
4254 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4255 return -1;
4256 }
4257
4258#ifndef OPENSSL_NO_DSA
4259 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4260 pkey = openssl_dsa_to_dh(tmpkey);
4261 EVP_PKEY_free(tmpkey);
4262 if (!pkey)
4263 return -1;
4264 dsa = true;
4265 }
4266#endif /* !OPENSSL_NO_DSA */
4267 if (!dsa) {
4268 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4269 EVP_PKEY_is_a(tmpkey, "DHX")) {
4270 } else {
4271 wpa_printf(MSG_INFO,
4272 "TLS: No DH parameters found in %s",
4273 dh_file);
4274 EVP_PKEY_free(tmpkey);
4275 return -1;
4276 }
4277 pkey = tmpkey;
4278 tmpkey = NULL;
4279 }
4280
4281 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4282 wpa_printf(MSG_INFO,
4283 "TLS: Failed to set DH params from '%s': %s",
4284 dh_file, ERR_error_string(ERR_get_error(), NULL));
4285 EVP_PKEY_free(pkey);
4286 return -1;
4287 }
4288 return 0;
4289#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004290 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 DH *dh;
4292 BIO *bio;
4293
Sunil Ravia04bd252022-05-02 22:54:18 -07004294 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004296 if (!dh_file) {
4297#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4298 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4299#endif
4300 return 0;
4301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302
4303 bio = BIO_new_file(dh_file, "r");
4304 if (bio == NULL) {
4305 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4306 dh_file, ERR_error_string(ERR_get_error(), NULL));
4307 return -1;
4308 }
4309 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4310 BIO_free(bio);
4311#ifndef OPENSSL_NO_DSA
4312 while (dh == NULL) {
4313 DSA *dsa;
4314 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4315 " trying to parse as DSA params", dh_file,
4316 ERR_error_string(ERR_get_error(), NULL));
4317 bio = BIO_new_file(dh_file, "r");
4318 if (bio == NULL)
4319 break;
4320 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4321 BIO_free(bio);
4322 if (!dsa) {
4323 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4324 "'%s': %s", dh_file,
4325 ERR_error_string(ERR_get_error(), NULL));
4326 break;
4327 }
4328
4329 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4330 dh = DSA_dup_DH(dsa);
4331 DSA_free(dsa);
4332 if (dh == NULL) {
4333 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4334 "params into DH params");
4335 break;
4336 }
4337 break;
4338 }
4339#endif /* !OPENSSL_NO_DSA */
4340 if (dh == NULL) {
4341 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4342 "'%s'", dh_file);
4343 return -1;
4344 }
4345
4346 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4347 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4348 "%s", dh_file,
4349 ERR_error_string(ERR_get_error(), NULL));
4350 DH_free(dh);
4351 return -1;
4352 }
4353 DH_free(dh);
4354 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004355#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356#endif /* OPENSSL_NO_DH */
4357}
4358
4359
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004360int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4361 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362{
4363 SSL *ssl;
4364
4365 if (conn == NULL || keys == NULL)
4366 return -1;
4367 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004368 if (ssl == NULL)
4369 return -1;
4370
4371 os_memset(keys, 0, sizeof(*keys));
4372 keys->client_random = conn->client_random;
4373 keys->client_random_len = SSL_get_client_random(
4374 ssl, conn->client_random, sizeof(conn->client_random));
4375 keys->server_random = conn->server_random;
4376 keys->server_random_len = SSL_get_server_random(
4377 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378
4379 return 0;
4380}
4381
4382
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004383#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004384static int openssl_get_keyblock_size(SSL *ssl)
4385{
Sunil Ravia04bd252022-05-02 22:54:18 -07004386#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004387 const EVP_CIPHER *c;
4388 const EVP_MD *h;
4389 int md_size;
4390
4391 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4392 ssl->read_hash == NULL)
4393 return -1;
4394
4395 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004396 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004397 if (h)
4398 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004399 else if (ssl->s3)
4400 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004401 else
4402 return -1;
4403
4404 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4405 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4406 EVP_CIPHER_iv_length(c));
4407 return 2 * (EVP_CIPHER_key_length(c) +
4408 md_size +
4409 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004410#else
4411 const SSL_CIPHER *ssl_cipher;
4412 int cipher, digest;
4413 const EVP_CIPHER *c;
4414 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004415 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004416
4417 ssl_cipher = SSL_get_current_cipher(ssl);
4418 if (!ssl_cipher)
4419 return -1;
4420 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4421 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4422 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4423 cipher, digest);
4424 if (cipher < 0 || digest < 0)
4425 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004426 if (cipher == NID_undef) {
4427 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004428 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004429 }
4430 c = EVP_get_cipherbynid(cipher);
4431 if (!c)
4432 return -1;
4433 enc_key_len = EVP_CIPHER_key_length(c);
4434 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4435 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4436 fixed_iv_len = 4; /* only part of IV from PRF */
4437 else
4438 fixed_iv_len = EVP_CIPHER_iv_length(c);
4439 if (digest == NID_undef) {
4440 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4441 mac_key_len = 0;
4442 } else {
4443 h = EVP_get_digestbynid(digest);
4444 if (!h)
4445 return -1;
4446 mac_key_len = EVP_MD_size(h);
4447 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004448
4449 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004450 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4451 mac_key_len, enc_key_len, fixed_iv_len);
4452 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004453#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004454}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004455#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004456
4457
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004458int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004459 const char *label, const u8 *context,
4460 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004461{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004462 if (!conn ||
4463 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004464 os_strlen(label), context, context_len,
4465 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004466 return -1;
4467 return 0;
4468}
4469
4470
4471int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4472 u8 *out, size_t out_len)
4473{
4474#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475 SSL *ssl;
4476 SSL_SESSION *sess;
4477 u8 *rnd;
4478 int ret = -1;
4479 int skip = 0;
4480 u8 *tmp_out = NULL;
4481 u8 *_out = out;
4482 unsigned char client_random[SSL3_RANDOM_SIZE];
4483 unsigned char server_random[SSL3_RANDOM_SIZE];
4484 unsigned char master_key[64];
4485 size_t master_key_len;
4486 const char *ver;
4487
4488 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004489 * TLS library did not support EAP-FAST key generation, so get the
4490 * needed TLS session parameters and use an internal implementation of
4491 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004492 */
4493
4494 if (conn == NULL)
4495 return -1;
4496 ssl = conn->ssl;
4497 if (ssl == NULL)
4498 return -1;
4499 ver = SSL_get_version(ssl);
4500 sess = SSL_get_session(ssl);
4501 if (!ver || !sess)
4502 return -1;
4503
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004504 skip = openssl_get_keyblock_size(ssl);
4505 if (skip < 0)
4506 return -1;
4507 tmp_out = os_malloc(skip + out_len);
4508 if (!tmp_out)
4509 return -1;
4510 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004511
4512 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4513 if (!rnd) {
4514 os_free(tmp_out);
4515 return -1;
4516 }
4517
4518 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4519 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4520 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4521 sizeof(master_key));
4522
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004523 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4524 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004525
4526 if (os_strcmp(ver, "TLSv1.2") == 0) {
4527 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004528 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004529 _out, skip + out_len);
4530 ret = 0;
4531 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004532 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004533 _out, skip + out_len) == 0) {
4534 ret = 0;
4535 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004536 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004537 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004538 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004539 os_memcpy(out, _out + skip, out_len);
4540 bin_clear_free(tmp_out, skip);
4541
4542 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004543#else /* OPENSSL_NEED_EAP_FAST_PRF */
4544 wpa_printf(MSG_ERROR,
4545 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4546 return -1;
4547#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548}
4549
4550
4551static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004552openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553{
Sunil Ravia04bd252022-05-02 22:54:18 -07004554 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 int res;
4556 struct wpabuf *out_data;
4557
4558 /*
4559 * Give TLS handshake data from the server (if available) to OpenSSL
4560 * for processing.
4561 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004562 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4564 < 0) {
4565 tls_show_errors(MSG_INFO, __func__,
4566 "Handshake failed - BIO_write");
4567 return NULL;
4568 }
4569
4570 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004571 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 res = SSL_accept(conn->ssl);
4573 else
4574 res = SSL_connect(conn->ssl);
4575 if (res != 1) {
4576 int err = SSL_get_error(conn->ssl, res);
4577 if (err == SSL_ERROR_WANT_READ)
4578 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4579 "more data");
4580 else if (err == SSL_ERROR_WANT_WRITE)
4581 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4582 "write");
4583 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004584 unsigned long error = ERR_peek_last_error();
4585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004587
4588 if (context->event_cb &&
4589 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4590 ERR_GET_REASON(error) ==
4591 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4592 context->event_cb(
4593 context->cb_ctx,
4594 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4595 NULL);
4596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004598 if (!conn->server && !conn->client_hello_generated) {
4599 /* The server would not understand TLS Alert
4600 * before ClientHello, so simply terminate
4601 * handshake on this type of error case caused
4602 * by a likely internal error like no ciphers
4603 * available. */
4604 wpa_printf(MSG_DEBUG,
4605 "OpenSSL: Could not generate ClientHello");
4606 conn->write_alerts++;
4607 return NULL;
4608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 }
4610 }
4611
Roshan Pius3a1667e2018-07-03 15:17:14 -07004612 if (!conn->server && !conn->failed)
4613 conn->client_hello_generated = 1;
4614
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004615#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004616 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004617 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4618 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004619 /*
4620 * This should not be reached since earlier cert_cb should have
4621 * terminated the handshake. Keep this check here for extra
4622 * protection if anything goes wrong with the more low-level
4623 * checks based on having to parse the TLS handshake messages.
4624 */
4625 wpa_printf(MSG_DEBUG,
4626 "OpenSSL: Server DH prime length: %d bits",
4627 conn->server_dh_prime_len);
4628
4629 if (context->event_cb) {
4630 union tls_event_data ev;
4631
4632 os_memset(&ev, 0, sizeof(ev));
4633 ev.alert.is_local = 1;
4634 ev.alert.type = "fatal";
4635 ev.alert.description = "insufficient security";
4636 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4637 }
4638 /*
4639 * Could send a TLS Alert to the server, but for now, simply
4640 * terminate handshake.
4641 */
4642 conn->failed++;
4643 conn->write_alerts++;
4644 return NULL;
4645 }
4646#endif /* CONFIG_SUITEB */
4647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 /* Get the TLS handshake data to be sent to the server */
4649 res = BIO_ctrl_pending(conn->ssl_out);
4650 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4651 out_data = wpabuf_alloc(res);
4652 if (out_data == NULL) {
4653 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4654 "handshake output (%d bytes)", res);
4655 if (BIO_reset(conn->ssl_out) < 0) {
4656 tls_show_errors(MSG_INFO, __func__,
4657 "BIO_reset failed");
4658 }
4659 return NULL;
4660 }
4661 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4662 res);
4663 if (res < 0) {
4664 tls_show_errors(MSG_INFO, __func__,
4665 "Handshake failed - BIO_read");
4666 if (BIO_reset(conn->ssl_out) < 0) {
4667 tls_show_errors(MSG_INFO, __func__,
4668 "BIO_reset failed");
4669 }
4670 wpabuf_free(out_data);
4671 return NULL;
4672 }
4673 wpabuf_put(out_data, res);
4674
4675 return out_data;
4676}
4677
4678
4679static struct wpabuf *
4680openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4681{
4682 struct wpabuf *appl_data;
4683 int res;
4684
4685 appl_data = wpabuf_alloc(max_len + 100);
4686 if (appl_data == NULL)
4687 return NULL;
4688
4689 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4690 wpabuf_size(appl_data));
4691 if (res < 0) {
4692 int err = SSL_get_error(conn->ssl, res);
4693 if (err == SSL_ERROR_WANT_READ ||
4694 err == SSL_ERROR_WANT_WRITE) {
4695 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4696 "included");
4697 } else {
4698 tls_show_errors(MSG_INFO, __func__,
4699 "Failed to read possible "
4700 "Application Data");
4701 }
4702 wpabuf_free(appl_data);
4703 return NULL;
4704 }
4705
4706 wpabuf_put(appl_data, res);
4707 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4708 "message", appl_data);
4709
4710 return appl_data;
4711}
4712
4713
4714static struct wpabuf *
4715openssl_connection_handshake(struct tls_connection *conn,
4716 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004717 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718{
4719 struct wpabuf *out_data;
4720
4721 if (appl_data)
4722 *appl_data = NULL;
4723
Roshan Pius3a1667e2018-07-03 15:17:14 -07004724 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 if (out_data == NULL)
4726 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004727 if (conn->invalid_hb_used) {
4728 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4729 wpabuf_free(out_data);
4730 return NULL;
4731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004733 if (SSL_is_init_finished(conn->ssl)) {
4734 wpa_printf(MSG_DEBUG,
4735 "OpenSSL: Handshake finished - resumed=%d",
4736 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004737 if (conn->server) {
4738 char *buf;
4739 size_t buflen = 2000;
4740
4741 buf = os_malloc(buflen);
4742 if (buf) {
4743 if (SSL_get_shared_ciphers(conn->ssl, buf,
4744 buflen)) {
4745 buf[buflen - 1] = '\0';
4746 wpa_printf(MSG_DEBUG,
4747 "OpenSSL: Shared ciphers: %s",
4748 buf);
4749 }
4750 os_free(buf);
4751 }
4752 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004753 if (appl_data && in_data)
4754 *appl_data = openssl_get_appl_data(conn,
4755 wpabuf_len(in_data));
4756 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757
Jouni Malinen26af48b2014-04-09 13:02:53 +03004758 if (conn->invalid_hb_used) {
4759 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4760 if (appl_data) {
4761 wpabuf_free(*appl_data);
4762 *appl_data = NULL;
4763 }
4764 wpabuf_free(out_data);
4765 return NULL;
4766 }
4767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768 return out_data;
4769}
4770
4771
4772struct wpabuf *
4773tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4774 const struct wpabuf *in_data,
4775 struct wpabuf **appl_data)
4776{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004777 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778}
4779
4780
4781struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4782 struct tls_connection *conn,
4783 const struct wpabuf *in_data,
4784 struct wpabuf **appl_data)
4785{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004786 conn->server = 1;
4787 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788}
4789
4790
4791struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4792 struct tls_connection *conn,
4793 const struct wpabuf *in_data)
4794{
4795 int res;
4796 struct wpabuf *buf;
4797
4798 if (conn == NULL)
4799 return NULL;
4800
4801 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4802 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4803 (res = BIO_reset(conn->ssl_out)) < 0) {
4804 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4805 return NULL;
4806 }
4807 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4808 if (res < 0) {
4809 tls_show_errors(MSG_INFO, __func__,
4810 "Encryption failed - SSL_write");
4811 return NULL;
4812 }
4813
4814 /* Read encrypted data to be sent to the server */
4815 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4816 if (buf == NULL)
4817 return NULL;
4818 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4819 if (res < 0) {
4820 tls_show_errors(MSG_INFO, __func__,
4821 "Encryption failed - BIO_read");
4822 wpabuf_free(buf);
4823 return NULL;
4824 }
4825 wpabuf_put(buf, res);
4826
4827 return buf;
4828}
4829
4830
4831struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4832 struct tls_connection *conn,
4833 const struct wpabuf *in_data)
4834{
4835 int res;
4836 struct wpabuf *buf;
4837
4838 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4839 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4840 wpabuf_len(in_data));
4841 if (res < 0) {
4842 tls_show_errors(MSG_INFO, __func__,
4843 "Decryption failed - BIO_write");
4844 return NULL;
4845 }
4846 if (BIO_reset(conn->ssl_out) < 0) {
4847 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4848 return NULL;
4849 }
4850
4851 /* Read decrypted data for further processing */
4852 /*
4853 * Even though we try to disable TLS compression, it is possible that
4854 * this cannot be done with all TLS libraries. Add extra buffer space
4855 * to handle the possibility of the decrypted data being longer than
4856 * input data.
4857 */
4858 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4859 if (buf == NULL)
4860 return NULL;
4861 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4862 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004863 int err = SSL_get_error(conn->ssl, res);
4864
4865 if (err == SSL_ERROR_WANT_READ) {
4866 wpa_printf(MSG_DEBUG,
4867 "SSL: SSL_connect - want more data");
4868 res = 0;
4869 } else {
4870 tls_show_errors(MSG_INFO, __func__,
4871 "Decryption failed - SSL_read");
4872 wpabuf_free(buf);
4873 return NULL;
4874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 }
4876 wpabuf_put(buf, res);
4877
Jouni Malinen26af48b2014-04-09 13:02:53 +03004878 if (conn->invalid_hb_used) {
4879 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4880 wpabuf_free(buf);
4881 return NULL;
4882 }
4883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 return buf;
4885}
4886
4887
4888int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4889{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004890 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891}
4892
4893
4894int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4895 u8 *ciphers)
4896{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004897 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898 u8 *c;
4899 int ret;
4900
4901 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4902 return -1;
4903
4904 buf[0] = '\0';
4905 pos = buf;
4906 end = pos + sizeof(buf);
4907
4908 c = ciphers;
4909 while (*c != TLS_CIPHER_NONE) {
4910 const char *suite;
4911
4912 switch (*c) {
4913 case TLS_CIPHER_RC4_SHA:
4914 suite = "RC4-SHA";
4915 break;
4916 case TLS_CIPHER_AES128_SHA:
4917 suite = "AES128-SHA";
4918 break;
4919 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4920 suite = "DHE-RSA-AES128-SHA";
4921 break;
4922 case TLS_CIPHER_ANON_DH_AES128_SHA:
4923 suite = "ADH-AES128-SHA";
4924 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004925 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4926 suite = "DHE-RSA-AES256-SHA";
4927 break;
4928 case TLS_CIPHER_AES256_SHA:
4929 suite = "AES256-SHA";
4930 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 default:
4932 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4933 "cipher selection: %d", *c);
4934 return -1;
4935 }
4936 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004937 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 break;
4939 pos += ret;
4940
4941 c++;
4942 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004943 if (!buf[0]) {
4944 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4945 return -1;
4946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947
4948 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4949
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004950#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004951#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004952 if (os_strstr(buf, ":ADH-")) {
4953 /*
4954 * Need to drop to security level 0 to allow anonymous
4955 * cipher suites for EAP-FAST.
4956 */
4957 SSL_set_security_level(conn->ssl, 0);
4958 } else if (SSL_get_security_level(conn->ssl) == 0) {
4959 /* Force at least security level 1 */
4960 SSL_set_security_level(conn->ssl, 1);
4961 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004962#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004963#endif
4964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4966 tls_show_errors(MSG_INFO, __func__,
4967 "Cipher suite configuration failed");
4968 return -1;
4969 }
4970
4971 return 0;
4972}
4973
4974
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004975int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4976 char *buf, size_t buflen)
4977{
4978 const char *name;
4979 if (conn == NULL || conn->ssl == NULL)
4980 return -1;
4981
4982 name = SSL_get_version(conn->ssl);
4983 if (name == NULL)
4984 return -1;
4985
4986 os_strlcpy(buf, name, buflen);
4987 return 0;
4988}
4989
4990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4992 char *buf, size_t buflen)
4993{
4994 const char *name;
4995 if (conn == NULL || conn->ssl == NULL)
4996 return -1;
4997
4998 name = SSL_get_cipher(conn->ssl);
4999 if (name == NULL)
5000 return -1;
5001
5002 os_strlcpy(buf, name, buflen);
5003 return 0;
5004}
5005
5006
5007int tls_connection_enable_workaround(void *ssl_ctx,
5008 struct tls_connection *conn)
5009{
5010 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
5011
5012 return 0;
5013}
5014
5015
Hai Shalom81f62d82019-07-22 12:10:00 -07005016#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017/* ClientHello TLS extensions require a patch to openssl, so this function is
5018 * commented out unless explicitly needed for EAP-FAST in order to be able to
5019 * build this file with unmodified openssl. */
5020int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
5021 int ext_type, const u8 *data,
5022 size_t data_len)
5023{
5024 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
5025 return -1;
5026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
5028 data_len) != 1)
5029 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030
5031 return 0;
5032}
Hai Shalom81f62d82019-07-22 12:10:00 -07005033#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034
5035
5036int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
5037{
5038 if (conn == NULL)
5039 return -1;
5040 return conn->failed;
5041}
5042
5043
5044int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
5045{
5046 if (conn == NULL)
5047 return -1;
5048 return conn->read_alerts;
5049}
5050
5051
5052int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
5053{
5054 if (conn == NULL)
5055 return -1;
5056 return conn->write_alerts;
5057}
5058
5059
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005060#ifdef HAVE_OCSP
5061
5062static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5063{
5064#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005065 BIO *out;
5066 size_t rlen;
5067 char *txt;
5068 int res;
5069
5070 if (wpa_debug_level > MSG_DEBUG)
5071 return;
5072
5073 out = BIO_new(BIO_s_mem());
5074 if (!out)
5075 return;
5076
5077 OCSP_RESPONSE_print(out, rsp, 0);
5078 rlen = BIO_ctrl_pending(out);
5079 txt = os_malloc(rlen + 1);
5080 if (!txt) {
5081 BIO_free(out);
5082 return;
5083 }
5084
5085 res = BIO_read(out, txt, rlen);
5086 if (res > 0) {
5087 txt[res] = '\0';
5088 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5089 }
5090 os_free(txt);
5091 BIO_free(out);
5092#endif /* CONFIG_NO_STDOUT_DEBUG */
5093}
5094
5095
5096static int ocsp_resp_cb(SSL *s, void *arg)
5097{
5098 struct tls_connection *conn = arg;
5099 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005100 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005101 OCSP_RESPONSE *rsp;
5102 OCSP_BASICRESP *basic;
5103 OCSP_CERTID *id;
5104 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005105 X509_STORE *store;
5106 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005107
5108 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5109 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005110#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5111#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5112 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5113 /* TLS 1.3 sends the OCSP response with the server
5114 * Certificate message. Since that Certificate message
5115 * is not sent when resuming a session, there can be no
5116 * new OCSP response. Allow this since the OCSP response
5117 * was validated when checking the initial certificate
5118 * exchange. */
5119 wpa_printf(MSG_DEBUG,
5120 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5121 return 1;
5122 }
5123#endif
5124#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005125 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5126 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5127 }
5128
5129 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5130
5131 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5132 if (!rsp) {
5133 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5134 return 0;
5135 }
5136
5137 ocsp_debug_print_resp(rsp);
5138
5139 status = OCSP_response_status(rsp);
5140 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5141 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5142 status, OCSP_response_status_str(status));
5143 return 0;
5144 }
5145
5146 basic = OCSP_response_get1_basic(rsp);
5147 if (!basic) {
5148 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5149 return 0;
5150 }
5151
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005152 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005153 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005154 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005155
5156 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5157 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005158 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005159 }
5160 certs = sk_X509_new_null();
5161 if (certs) {
5162 X509 *cert;
5163 cert = X509_dup(conn->peer_issuer);
5164 if (cert && !sk_X509_push(certs, cert)) {
5165 tls_show_errors(
5166 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005167 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005168 X509_free(cert);
5169 sk_X509_free(certs);
5170 certs = NULL;
5171 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005172 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005173 cert = X509_dup(conn->peer_issuer_issuer);
5174 if (cert && !sk_X509_push(certs, cert)) {
5175 tls_show_errors(
5176 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005177 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005178 X509_free(cert);
5179 }
5180 }
5181 }
5182 }
5183
5184 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5185 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005186 if (status <= 0) {
5187 tls_show_errors(MSG_INFO, __func__,
5188 "OpenSSL: OCSP response failed verification");
5189 OCSP_BASICRESP_free(basic);
5190 OCSP_RESPONSE_free(rsp);
5191 return 0;
5192 }
5193
5194 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5195
Dmitry Shmidt56052862013-10-04 10:23:25 -07005196 if (!conn->peer_cert) {
5197 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5198 OCSP_BASICRESP_free(basic);
5199 OCSP_RESPONSE_free(rsp);
5200 return 0;
5201 }
5202
5203 if (!conn->peer_issuer) {
5204 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005205 OCSP_BASICRESP_free(basic);
5206 OCSP_RESPONSE_free(rsp);
5207 return 0;
5208 }
5209
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005210 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005211 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005212 wpa_printf(MSG_DEBUG,
5213 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005214 OCSP_BASICRESP_free(basic);
5215 OCSP_RESPONSE_free(rsp);
5216 return 0;
5217 }
5218
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005219 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5220 &this_update, &next_update);
5221 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005222 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005223 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5224 if (!id) {
5225 wpa_printf(MSG_DEBUG,
5226 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5227 OCSP_BASICRESP_free(basic);
5228 OCSP_RESPONSE_free(rsp);
5229 return 0;
5230 }
5231
5232 res = OCSP_resp_find_status(basic, id, &status, &reason,
5233 &produced_at, &this_update,
5234 &next_update);
5235 }
5236
5237 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005238 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5239 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5240 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005241 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005242 OCSP_BASICRESP_free(basic);
5243 OCSP_RESPONSE_free(rsp);
5244 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5245 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005246 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005247
5248 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5249 tls_show_errors(MSG_INFO, __func__,
5250 "OpenSSL: OCSP status times invalid");
5251 OCSP_BASICRESP_free(basic);
5252 OCSP_RESPONSE_free(rsp);
5253 return 0;
5254 }
5255
5256 OCSP_BASICRESP_free(basic);
5257 OCSP_RESPONSE_free(rsp);
5258
5259 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5260 OCSP_cert_status_str(status));
5261
5262 if (status == V_OCSP_CERTSTATUS_GOOD)
5263 return 1;
5264 if (status == V_OCSP_CERTSTATUS_REVOKED)
5265 return 0;
5266 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5267 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5268 return 0;
5269 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005270 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005271 return 1;
5272}
5273
5274
5275static int ocsp_status_cb(SSL *s, void *arg)
5276{
5277 char *tmp;
5278 char *resp;
5279 size_t len;
5280
5281 if (tls_global->ocsp_stapling_response == NULL) {
5282 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5283 return SSL_TLSEXT_ERR_OK;
5284 }
5285
5286 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5287 if (resp == NULL) {
5288 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5289 /* TODO: Build OCSPResponse with responseStatus = internalError
5290 */
5291 return SSL_TLSEXT_ERR_OK;
5292 }
5293 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5294 tmp = OPENSSL_malloc(len);
5295 if (tmp == NULL) {
5296 os_free(resp);
5297 return SSL_TLSEXT_ERR_ALERT_FATAL;
5298 }
5299
5300 os_memcpy(tmp, resp, len);
5301 os_free(resp);
5302 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5303
5304 return SSL_TLSEXT_ERR_OK;
5305}
5306
5307#endif /* HAVE_OCSP */
5308
5309
Hai Shalomfdcde762020-04-02 11:19:20 -07005310static size_t max_str_len(const char **lines)
5311{
5312 const char **p;
5313 size_t max_len = 0;
5314
5315 for (p = lines; *p; p++) {
5316 size_t len = os_strlen(*p);
5317
5318 if (len > max_len)
5319 max_len = len;
5320 }
5321
5322 return max_len;
5323}
5324
5325
5326static int match_lines_in_file(const char *path, const char **lines)
5327{
5328 FILE *f;
5329 char *buf;
5330 size_t bufsize;
5331 int found = 0, is_linestart = 1;
5332
5333 bufsize = max_str_len(lines) + sizeof("\r\n");
5334 buf = os_malloc(bufsize);
5335 if (!buf)
5336 return 0;
5337
5338 f = fopen(path, "r");
5339 if (!f) {
5340 os_free(buf);
5341 return 0;
5342 }
5343
5344 while (!found && fgets(buf, bufsize, f)) {
5345 int is_lineend;
5346 size_t len;
5347 const char **p;
5348
5349 len = strcspn(buf, "\r\n");
5350 is_lineend = buf[len] != '\0';
5351 buf[len] = '\0';
5352
5353 if (is_linestart && is_lineend) {
5354 for (p = lines; !found && *p; p++)
5355 found = os_strcmp(buf, *p) == 0;
5356 }
5357 is_linestart = is_lineend;
5358 }
5359
5360 fclose(f);
5361 bin_clear_free(buf, bufsize);
5362
5363 return found;
5364}
5365
5366
5367static int is_tpm2_key(const char *path)
5368{
5369 /* Check both new and old format of TPM2 PEM guard tag */
5370 static const char *tpm2_tags[] = {
5371 "-----BEGIN TSS2 PRIVATE KEY-----",
5372 "-----BEGIN TSS2 KEY BLOB-----",
5373 NULL
5374 };
5375
5376 return match_lines_in_file(path, tpm2_tags);
5377}
5378
5379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5381 const struct tls_connection_params *params)
5382{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005383 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 int ret;
5385 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005386 int can_pkcs11 = 0;
5387 const char *key_id = params->key_id;
5388 const char *cert_id = params->cert_id;
5389 const char *ca_cert_id = params->ca_cert_id;
5390 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005391 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392
5393 if (conn == NULL)
5394 return -1;
5395
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005396 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5397 wpa_printf(MSG_INFO,
5398 "OpenSSL: ocsp=3 not supported");
5399 return -1;
5400 }
5401
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005402 /*
5403 * If the engine isn't explicitly configured, and any of the
5404 * cert/key fields are actually PKCS#11 URIs, then automatically
5405 * use the PKCS#11 ENGINE.
5406 */
5407 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5408 can_pkcs11 = 1;
5409
5410 if (!key_id && params->private_key && can_pkcs11 &&
5411 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5412 can_pkcs11 = 2;
5413 key_id = params->private_key;
5414 }
5415
5416 if (!cert_id && params->client_cert && can_pkcs11 &&
5417 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5418 can_pkcs11 = 2;
5419 cert_id = params->client_cert;
5420 }
5421
5422 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5423 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5424 can_pkcs11 = 2;
5425 ca_cert_id = params->ca_cert;
5426 }
5427
5428 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5429 if (can_pkcs11 == 2 && !engine_id)
5430 engine_id = "pkcs11";
5431
Hai Shalomfdcde762020-04-02 11:19:20 -07005432 /* If private_key points to a TPM2-wrapped key, automatically enable
5433 * tpm2 engine and use it to unwrap the key. */
5434 if (params->private_key &&
5435 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5436 is_tpm2_key(params->private_key)) {
5437 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5438 params->private_key);
5439 key_id = key_id ? key_id : params->private_key;
5440 engine_id = engine_id ? engine_id : "tpm2";
5441 }
5442
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005443#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005444#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005445 if (params->flags & TLS_CONN_EAP_FAST) {
5446 wpa_printf(MSG_DEBUG,
5447 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5448 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5449 tls_show_errors(MSG_INFO, __func__,
5450 "Failed to set TLSv1_method() for EAP-FAST");
5451 return -1;
5452 }
5453 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005454#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005455#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5456#ifdef SSL_OP_NO_TLSv1_3
5457 if (params->flags & TLS_CONN_EAP_FAST) {
5458 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5459 * refuses to start the handshake with the modified ciphersuite
5460 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5461 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5462 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5463 }
5464#endif /* SSL_OP_NO_TLSv1_3 */
5465#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005466#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 while ((err = ERR_get_error())) {
5469 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5470 __func__, ERR_error_string(err, NULL));
5471 }
5472
Sunil Ravi77d572f2023-01-17 23:58:31 +00005473 if (tls_set_conn_flags(conn, params->flags,
5474 params->openssl_ciphers) < 0) {
5475 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
5476 return -1;
5477 }
5478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005479 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005480 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5481 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005482 ret = tls_engine_init(conn, engine_id, params->pin,
5483 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484 if (ret)
5485 return ret;
5486 }
5487 if (tls_connection_set_subject_match(conn,
5488 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005489 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005490 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005491 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005492 params->check_cert_subject)) {
5493 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005495 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005497 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005498 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005500 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 params->ca_cert_blob,
5502 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005503 params->ca_path)) {
5504 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005508 if (engine_id && cert_id) {
5509 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5511 } else if (tls_connection_client_cert(conn, params->client_cert,
5512 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005513 params->client_cert_blob_len)) {
5514 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005518 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5520 if (tls_connection_engine_private_key(conn))
5521 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005522 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 params->private_key,
5524 params->private_key_passwd,
5525 params->private_key_blob,
5526 params->private_key_blob_len)) {
5527 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5528 params->private_key);
5529 return -1;
5530 }
5531
Roshan Pius3a1667e2018-07-03 15:17:14 -07005532 ciphers = params->openssl_ciphers;
5533#ifdef CONFIG_SUITEB
5534#ifdef OPENSSL_IS_BORINGSSL
5535 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5536 /* BoringSSL removed support for SUITEB192, so need to handle
5537 * this with hardcoded ciphersuite and additional checks for
5538 * other parameters. */
5539 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5540 }
5541#endif /* OPENSSL_IS_BORINGSSL */
5542#endif /* CONFIG_SUITEB */
5543 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005544 wpa_printf(MSG_INFO,
5545 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005546 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005547 return -1;
5548 }
5549
Hai Shalom74f70d42019-02-11 14:42:39 -08005550 if (!params->openssl_ecdh_curves) {
5551#ifndef OPENSSL_IS_BORINGSSL
5552#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005553#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005554 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5555 wpa_printf(MSG_INFO,
5556 "OpenSSL: Failed to set ECDH curves to auto");
5557 return -1;
5558 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005559#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005560#endif /* OPENSSL_NO_EC */
5561#endif /* OPENSSL_IS_BORINGSSL */
5562 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005563#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005564 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005565 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005566 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005567#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005568#ifndef OPENSSL_NO_EC
5569 if (SSL_set1_curves_list(conn->ssl,
5570 params->openssl_ecdh_curves) != 1) {
5571 wpa_printf(MSG_INFO,
5572 "OpenSSL: Failed to set ECDH curves '%s'",
5573 params->openssl_ecdh_curves);
5574 return -1;
5575 }
5576#else /* OPENSSL_NO_EC */
5577 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5578 return -1;
5579#endif /* OPENSSL_NO_EC */
5580#endif /* OPENSSL_IS_BORINGSSL */
5581 }
5582
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005583#ifdef OPENSSL_IS_BORINGSSL
5584 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5585 SSL_enable_ocsp_stapling(conn->ssl);
5586 }
5587#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005588#ifdef HAVE_OCSP
5589 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005590 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005591 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5592 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5593 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5594 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005595#else /* HAVE_OCSP */
5596 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5597 wpa_printf(MSG_INFO,
5598 "OpenSSL: No OCSP support included - reject configuration");
5599 return -1;
5600 }
5601 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5602 wpa_printf(MSG_DEBUG,
5603 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5604 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005605#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005606#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005607
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005608 conn->flags = params->flags;
5609
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005610 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
5612 return 0;
5613}
5614
5615
Hai Shalom81f62d82019-07-22 12:10:00 -07005616static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5617{
5618 SSL *ssl;
5619 int i;
5620
5621 ssl = SSL_new(ssl_ctx);
5622 if (!ssl)
5623 return;
5624
5625 wpa_printf(MSG_DEBUG,
5626 "OpenSSL: Enabled cipher suites in priority order");
5627 for (i = 0; ; i++) {
5628 const char *cipher;
5629
5630 cipher = SSL_get_cipher_list(ssl, i);
5631 if (!cipher)
5632 break;
5633 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5634 }
5635
5636 SSL_free(ssl);
5637}
5638
5639
5640#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5641
5642static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5643{
5644 if (!pkey)
5645 return "NULL";
5646 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5647 case EVP_PKEY_RSA:
5648 return "RSA";
5649 case EVP_PKEY_DSA:
5650 return "DSA";
5651 case EVP_PKEY_DH:
5652 return "DH";
5653 case EVP_PKEY_EC:
5654 return "EC";
Sunil Ravi77d572f2023-01-17 23:58:31 +00005655 default:
5656 return "?";
Hai Shalom81f62d82019-07-22 12:10:00 -07005657 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005658}
5659
5660
5661static void openssl_debug_dump_certificate(int i, X509 *cert)
5662{
5663 char buf[256];
5664 EVP_PKEY *pkey;
5665 ASN1_INTEGER *ser;
5666 char serial_num[128];
5667
Hai Shalom60840252021-02-19 19:02:11 -08005668 if (!cert)
5669 return;
5670
Hai Shalom81f62d82019-07-22 12:10:00 -07005671 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5672
5673 ser = X509_get_serialNumber(cert);
5674 if (ser)
5675 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5676 ASN1_STRING_get0_data(ser),
5677 ASN1_STRING_length(ser));
5678 else
5679 serial_num[0] = '\0';
5680
5681 pkey = X509_get_pubkey(cert);
5682 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5683 openssl_pkey_type_str(pkey), serial_num);
5684 EVP_PKEY_free(pkey);
5685}
5686
5687
5688static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5689{
5690 STACK_OF(X509) *certs;
5691
5692 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5693 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5694 int i;
5695
5696 for (i = sk_X509_num(certs); i > 0; i--)
5697 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5698 i - 1));
5699 }
5700 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5701}
5702
5703#endif
5704
5705
5706static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5707{
5708#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5709 int res;
5710
5711 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5712 res == 1;
5713 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5714 openssl_debug_dump_certificates(ssl_ctx);
5715
5716 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5717#endif
5718}
5719
5720
5721static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5722{
5723 openssl_debug_dump_cipher_list(ssl_ctx);
5724 openssl_debug_dump_certificate_chains(ssl_ctx);
5725}
5726
5727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728int tls_global_set_params(void *tls_ctx,
5729 const struct tls_connection_params *params)
5730{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005731 struct tls_data *data = tls_ctx;
5732 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733 unsigned long err;
5734
5735 while ((err = ERR_get_error())) {
5736 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5737 __func__, ERR_error_string(err, NULL));
5738 }
5739
Hai Shalom021b0b52019-04-10 11:17:58 -07005740 os_free(data->check_cert_subject);
5741 data->check_cert_subject = NULL;
5742 if (params->check_cert_subject) {
5743 data->check_cert_subject =
5744 os_strdup(params->check_cert_subject);
5745 if (!data->check_cert_subject)
5746 return -1;
5747 }
5748
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005749 if (tls_global_ca_cert(data, params->ca_cert) ||
5750 tls_global_client_cert(data, params->client_cert) ||
5751 tls_global_private_key(data, params->private_key,
5752 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005753 tls_global_client_cert(data, params->client_cert2) ||
5754 tls_global_private_key(data, params->private_key2,
5755 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005756 tls_global_dh(data, params->dh_file)) {
5757 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 return -1;
5759 }
5760
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005761 os_free(data->openssl_ciphers);
5762 if (params->openssl_ciphers) {
5763 data->openssl_ciphers = os_strdup(params->openssl_ciphers);
5764 if (!data->openssl_ciphers)
5765 return -1;
5766 } else {
5767 data->openssl_ciphers = NULL;
5768 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005769 if (params->openssl_ciphers &&
5770 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5771 wpa_printf(MSG_INFO,
5772 "OpenSSL: Failed to set cipher string '%s'",
5773 params->openssl_ciphers);
5774 return -1;
5775 }
5776
Hai Shalom74f70d42019-02-11 14:42:39 -08005777 if (!params->openssl_ecdh_curves) {
5778#ifndef OPENSSL_IS_BORINGSSL
5779#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005780#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005781 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5782 wpa_printf(MSG_INFO,
5783 "OpenSSL: Failed to set ECDH curves to auto");
5784 return -1;
5785 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005786#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005787#endif /* OPENSSL_NO_EC */
5788#endif /* OPENSSL_IS_BORINGSSL */
5789 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005790#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005791 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005792 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005793 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005794#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005795#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005796#if OPENSSL_VERSION_NUMBER < 0x10100000L
5797 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5798#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005799 if (SSL_CTX_set1_curves_list(ssl_ctx,
5800 params->openssl_ecdh_curves) !=
5801 1) {
5802 wpa_printf(MSG_INFO,
5803 "OpenSSL: Failed to set ECDH curves '%s'",
5804 params->openssl_ecdh_curves);
5805 return -1;
5806 }
5807#else /* OPENSSL_NO_EC */
5808 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5809 return -1;
5810#endif /* OPENSSL_NO_EC */
5811#endif /* OPENSSL_IS_BORINGSSL */
5812 }
5813
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005814#ifdef SSL_OP_NO_TICKET
5815 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5816 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5817 else
5818 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5819#endif /* SSL_OP_NO_TICKET */
5820
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005821#ifdef HAVE_OCSP
5822 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5823 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5824 os_free(tls_global->ocsp_stapling_response);
5825 if (params->ocsp_stapling_response)
5826 tls_global->ocsp_stapling_response =
5827 os_strdup(params->ocsp_stapling_response);
5828 else
5829 tls_global->ocsp_stapling_response = NULL;
5830#endif /* HAVE_OCSP */
5831
Hai Shalom81f62d82019-07-22 12:10:00 -07005832 openssl_debug_dump_ctx(ssl_ctx);
5833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834 return 0;
5835}
5836
5837
Hai Shalom81f62d82019-07-22 12:10:00 -07005838#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839/* Pre-shared secred requires a patch to openssl, so this function is
5840 * commented out unless explicitly needed for EAP-FAST in order to be able to
5841 * build this file with unmodified openssl. */
5842
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005843#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005844static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5845 STACK_OF(SSL_CIPHER) *peer_ciphers,
5846 const SSL_CIPHER **cipher, void *arg)
5847#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005848static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5849 STACK_OF(SSL_CIPHER) *peer_ciphers,
5850 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005851#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852{
5853 struct tls_connection *conn = arg;
5854 int ret;
5855
Sunil Ravia04bd252022-05-02 22:54:18 -07005856#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 if (conn == NULL || conn->session_ticket_cb == NULL)
5858 return 0;
5859
5860 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5861 conn->session_ticket,
5862 conn->session_ticket_len,
5863 s->s3->client_random,
5864 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005865#else
5866 unsigned char client_random[SSL3_RANDOM_SIZE];
5867 unsigned char server_random[SSL3_RANDOM_SIZE];
5868
5869 if (conn == NULL || conn->session_ticket_cb == NULL)
5870 return 0;
5871
5872 SSL_get_client_random(s, client_random, sizeof(client_random));
5873 SSL_get_server_random(s, server_random, sizeof(server_random));
5874
5875 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5876 conn->session_ticket,
5877 conn->session_ticket_len,
5878 client_random,
5879 server_random, secret);
5880#endif
5881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882 os_free(conn->session_ticket);
5883 conn->session_ticket = NULL;
5884
5885 if (ret <= 0)
5886 return 0;
5887
5888 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5889 return 1;
5890}
5891
5892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5894 int len, void *arg)
5895{
5896 struct tls_connection *conn = arg;
5897
5898 if (conn == NULL || conn->session_ticket_cb == NULL)
5899 return 0;
5900
5901 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5902
5903 os_free(conn->session_ticket);
5904 conn->session_ticket = NULL;
5905
5906 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5907 "extension", data, len);
5908
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005909 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910 if (conn->session_ticket == NULL)
5911 return 0;
5912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913 conn->session_ticket_len = len;
5914
5915 return 1;
5916}
Hai Shalom81f62d82019-07-22 12:10:00 -07005917#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918
5919
5920int tls_connection_set_session_ticket_cb(void *tls_ctx,
5921 struct tls_connection *conn,
5922 tls_session_ticket_cb cb,
5923 void *ctx)
5924{
Hai Shalom81f62d82019-07-22 12:10:00 -07005925#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926 conn->session_ticket_cb = cb;
5927 conn->session_ticket_cb_ctx = ctx;
5928
5929 if (cb) {
5930 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5931 conn) != 1)
5932 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 SSL_set_session_ticket_ext_cb(conn->ssl,
5934 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 } else {
5936 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5937 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005939 }
5940
5941 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005942#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005944#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005946
5947
5948int tls_get_library_version(char *buf, size_t buf_len)
5949{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005950#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005951 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5952 OPENSSL_VERSION_TEXT,
5953 OpenSSL_version(OPENSSL_VERSION));
5954#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005955 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5956 OPENSSL_VERSION_TEXT,
5957 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005958#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005959}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005960
5961
5962void tls_connection_set_success_data(struct tls_connection *conn,
5963 struct wpabuf *data)
5964{
5965 SSL_SESSION *sess;
5966 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005967 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005968
5969 if (tls_ex_idx_session < 0)
5970 goto fail;
5971 sess = SSL_get_session(conn->ssl);
5972 if (!sess)
5973 goto fail;
5974 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5975 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005976 struct tls_session_data *found;
5977
5978 found = get_session_data(conn->context, old);
5979 wpa_printf(MSG_DEBUG,
5980 "OpenSSL: Replacing old success data %p (sess %p)%s",
5981 old, sess, found ? "" : " (not freeing)");
5982 if (found) {
5983 dl_list_del(&found->list);
5984 os_free(found);
5985 wpabuf_free(old);
5986 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005987 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005988
5989 sess_data = os_zalloc(sizeof(*sess_data));
5990 if (!sess_data ||
5991 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005992 goto fail;
5993
Sunil Ravia04bd252022-05-02 22:54:18 -07005994 sess_data->buf = data;
5995 dl_list_add(&conn->context->sessions, &sess_data->list);
5996 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5997 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005998 conn->success_data = 1;
5999 return;
6000
6001fail:
6002 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
6003 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07006004 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006005}
6006
6007
6008void tls_connection_set_success_data_resumed(struct tls_connection *conn)
6009{
6010 wpa_printf(MSG_DEBUG,
6011 "OpenSSL: Success data accepted for resumed session");
6012 conn->success_data = 1;
6013}
6014
6015
6016const struct wpabuf *
6017tls_connection_get_success_data(struct tls_connection *conn)
6018{
6019 SSL_SESSION *sess;
6020
6021 if (tls_ex_idx_session < 0 ||
6022 !(sess = SSL_get_session(conn->ssl)))
6023 return NULL;
6024 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
6025}
6026
6027
6028void tls_connection_remove_session(struct tls_connection *conn)
6029{
6030 SSL_SESSION *sess;
6031
6032 sess = SSL_get_session(conn->ssl);
6033 if (!sess)
6034 return;
6035
6036 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
6037 wpa_printf(MSG_DEBUG,
6038 "OpenSSL: Session was not cached");
6039 else
6040 wpa_printf(MSG_DEBUG,
6041 "OpenSSL: Removed cached session to disable session resumption");
6042}
Hai Shalom81f62d82019-07-22 12:10:00 -07006043
6044
6045int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
6046{
6047 size_t len;
6048 int reused;
6049
6050 reused = SSL_session_reused(conn->ssl);
6051 if ((conn->server && !reused) || (!conn->server && reused))
6052 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
6053 else
6054 len = SSL_get_finished(conn->ssl, buf, max_len);
6055
6056 if (len == 0 || len > max_len)
6057 return -1;
6058
6059 return len;
6060}
6061
6062
6063u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6064{
6065 const SSL_CIPHER *cipher;
6066
6067 cipher = SSL_get_current_cipher(conn->ssl);
6068 if (!cipher)
6069 return 0;
6070#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6071 return SSL_CIPHER_get_protocol_id(cipher);
6072#else
6073 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6074#endif
6075}
Hai Shalom899fcc72020-10-19 14:38:18 -07006076
6077
6078const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6079{
6080 if (conn)
6081 return conn->peer_subject;
6082 return NULL;
6083}
6084
6085
6086bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6087{
6088 if (conn)
6089 return SSL_get_certificate(conn->ssl) != NULL;
6090 return false;
6091}
Gabriel Birena5bdf372022-12-15 20:54:33 +00006092
6093void tls_register_cert_callback(tls_get_certificate_cb cb)
6094{
6095 certificate_callback_global = cb;
6096}
Gabriel Biren60ae0682023-11-01 22:04:12 +00006097
6098void tls_register_openssl_failure_callback(tls_openssl_failure_cb cb)
6099{
6100 openssl_failure_callback_global = cb;
6101}