blob: dc8a1b4c95d252b3fd71f6d0a3fc7d9a53d7d096 [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
22#include <openssl/ssl.h>
23#include <openssl/err.h>
Dmitry Shmidt849734c2016-05-27 09:59:01 -070024#include <openssl/opensslv.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include <openssl/pkcs12.h>
26#include <openssl/x509v3.h>
27#ifndef OPENSSL_NO_ENGINE
28#include <openssl/engine.h>
29#endif /* OPENSSL_NO_ENGINE */
Sunil Ravia04bd252022-05-02 22:54:18 -070030#if OPENSSL_VERSION_NUMBER >= 0x30000000L
31#include <openssl/core_names.h>
32#include <openssl/decoder.h>
33#include <openssl/param_build.h>
34#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080035#ifndef OPENSSL_NO_DSA
36#include <openssl/dsa.h>
37#endif
38#ifndef OPENSSL_NO_DH
39#include <openssl/dh.h>
40#endif
Sunil Ravia04bd252022-05-02 22:54:18 -070041#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "common.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070044#include "utils/list.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070046#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080049#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
Dmitry Shmidt849734c2016-05-27 09:59:01 -070051#if !defined(CONFIG_FIPS) && \
52 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
53 defined(EAP_SERVER_FAST))
54#define OPENSSL_NEED_EAP_FAST_PRF
55#endif
56
Hai Shalom81f62d82019-07-22 12:10:00 -070057#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
58 defined(EAP_SERVER_FAST) || defined(EAP_TEAP) || \
59 defined(EAP_SERVER_TEAP)
60#define EAP_FAST_OR_TEAP
61#endif
62
63
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070064#if defined(OPENSSL_IS_BORINGSSL)
65/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
66typedef size_t stack_index_t;
67#else
68typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069#endif
70
Dmitry Shmidt34af3062013-07-11 10:46:32 -070071#ifdef SSL_set_tlsext_status_type
72#ifndef OPENSSL_NO_TLSEXT
73#define HAVE_OCSP
74#include <openssl/ocsp.h>
75#endif /* OPENSSL_NO_TLSEXT */
76#endif /* SSL_set_tlsext_status_type */
77
Sunil Ravia04bd252022-05-02 22:54:18 -070078#if OPENSSL_VERSION_NUMBER < 0x10100000L && \
Dmitry Shmidt849734c2016-05-27 09:59:01 -070079 !defined(BORINGSSL_API_VERSION)
Dmitry Shmidtde47be72016-01-07 12:52:55 -080080/*
81 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
Dmitry Shmidt849734c2016-05-27 09:59:01 -070082 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
83 * older versions.
Dmitry Shmidtde47be72016-01-07 12:52:55 -080084 */
85
86static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out,
87 size_t outlen)
88{
89 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
90 return 0;
91 os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE);
92 return SSL3_RANDOM_SIZE;
93}
94
95
96static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
97 size_t outlen)
98{
99 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
100 return 0;
101 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
102 return SSL3_RANDOM_SIZE;
103}
104
105
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700106#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800107static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
108 unsigned char *out, size_t outlen)
109{
110 if (!session || session->master_key_length < 0 ||
111 (size_t) session->master_key_length > outlen)
112 return 0;
113 if ((size_t) session->master_key_length < outlen)
114 outlen = session->master_key_length;
115 os_memcpy(out, session->master_key, outlen);
116 return outlen;
117}
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700118#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800119
120#endif
121
Sunil Ravia04bd252022-05-02 22:54:18 -0700122#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800123static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
124{
125 return ASN1_STRING_data((ASN1_STRING *) x);
126}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700127#endif
128
Dmitry Shmidtff079172013-11-08 14:10:30 -0800129#ifdef ANDROID
130#include <openssl/pem.h>
131#include <keystore/keystore_get.h>
132
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000133#include <log/log.h>
134#include <log/log_event_list.h>
135
136#define CERT_VALIDATION_FAILURE 210033
Hai Shalom7ad2a872021-08-02 18:56:55 -0700137#define ANDROID_KEYSTORE_PREFIX "keystore://"
138#define ANDROID_KEYSTORE_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_PREFIX)
139#define ANDROID_KEYSTORE_ENCODED_PREFIX "keystores://"
140#define ANDROID_KEYSTORE_ENCODED_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_ENCODED_PREFIX)
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000141
142static void log_cert_validation_failure(const char *reason)
143{
144 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
145 android_log_write_string8(ctx, reason);
146 android_log_write_list(ctx, LOG_ID_SECURITY);
147 android_log_destroy(&ctx);
148}
149
150
Hai Shalom7ad2a872021-08-02 18:56:55 -0700151static BIO* BIO_from_keystore(const char *alias)
Dmitry Shmidtff079172013-11-08 14:10:30 -0800152{
153 BIO *bio = NULL;
154 uint8_t *value = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700155 int length = keystore_get(alias, strlen(alias), &value);
Dmitry Shmidtff079172013-11-08 14:10:30 -0800156 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
157 BIO_write(bio, value, length);
158 free(value);
159 return bio;
160}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800161
Hai Shalom7ad2a872021-08-02 18:56:55 -0700162static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800163{
Hai Shalom7ad2a872021-08-02 18:56:55 -0700164 BIO *bio = BIO_from_keystore(alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800165 STACK_OF(X509_INFO) *stack = NULL;
166 stack_index_t i;
Hai Shalom22171592021-04-02 16:05:23 -0700167 int ret = 0;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800168
Hai Shalom22171592021-04-02 16:05:23 -0700169 if (!bio) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700170 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
171 alias);
Hai Shalom22171592021-04-02 16:05:23 -0700172 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800173 }
174
Hai Shalom7ad2a872021-08-02 18:56:55 -0700175 // Keystore returns X.509 certificates in PEM encoding
Hai Shalom22171592021-04-02 16:05:23 -0700176 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
177 BIO_free(bio);
178
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800179 if (!stack) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700180 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
181 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800182 return -1;
183 }
184
185 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
186 X509_INFO *info = sk_X509_INFO_value(stack, i);
187
188 if (info->x509)
Hai Shalom22171592021-04-02 16:05:23 -0700189 if (!X509_STORE_add_cert(ctx, info->x509)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700190 wpa_printf(MSG_ERROR,
191 "OpenSSL: Failed to add Root CA certificate");
Hai Shalom22171592021-04-02 16:05:23 -0700192 ret = -1;
193 break;
194 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800195 if (info->crl)
196 X509_STORE_add_crl(ctx, info->crl);
197 }
198
199 sk_X509_INFO_pop_free(stack, X509_INFO_free);
Hai Shalom22171592021-04-02 16:05:23 -0700200 return ret;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800201}
202
203
204static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
Hai Shalom7ad2a872021-08-02 18:56:55 -0700205 const char *encoded_alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800206{
207 int rc = -1;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700208 int len = os_strlen(encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800209 unsigned char *decoded_alias;
210
211 if (len & 1) {
212 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
Hai Shalom7ad2a872021-08-02 18:56:55 -0700213 encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800214 return rc;
215 }
216
217 decoded_alias = os_malloc(len / 2 + 1);
218 if (decoded_alias) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700219 if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800220 decoded_alias[len / 2] = '\0';
221 rc = tls_add_ca_from_keystore(
222 ctx, (const char *) decoded_alias);
223 }
224 os_free(decoded_alias);
225 }
226
227 return rc;
228}
229
Dmitry Shmidtff079172013-11-08 14:10:30 -0800230#endif /* ANDROID */
231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800233static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Sunil Ravia04bd252022-05-02 22:54:18 -0700235struct tls_session_data {
236 struct dl_list list;
237 struct wpabuf *buf;
238};
239
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700240struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 void (*event_cb)(void *ctx, enum tls_event ev,
242 union tls_event_data *data);
243 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700245 char *ocsp_stapling_response;
Sunil Ravia04bd252022-05-02 22:54:18 -0700246 struct dl_list sessions; /* struct tls_session_data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247};
248
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700249static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250
251
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800252struct tls_data {
253 SSL_CTX *ssl;
254 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -0800255 int check_crl;
256 int check_crl_strict;
257 char *ca_cert;
258 unsigned int crl_reload_interval;
259 struct os_reltime crl_last_reload;
Hai Shalom021b0b52019-04-10 11:17:58 -0700260 char *check_cert_subject;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800261};
262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700264 struct tls_context *context;
Hai Shalom74f70d42019-02-11 14:42:39 -0800265 struct tls_data *data;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800266 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 SSL *ssl;
268 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700269#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 ENGINE *engine; /* functional reference to the engine */
271 EVP_PKEY *private_key; /* the private key if using engine */
272#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800273 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -0700274 char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 int read_alerts, write_alerts, failed;
276
277 tls_session_ticket_cb session_ticket_cb;
278 void *session_ticket_cb_ctx;
279
280 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
281 u8 *session_ticket;
282 size_t session_ticket_len;
283
284 unsigned int ca_cert_verify:1;
285 unsigned int cert_probe:1;
286 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300287 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800288 unsigned int success_data:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700289 unsigned int client_hello_generated:1;
290 unsigned int server:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700293
294 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700295
296 X509 *peer_cert;
297 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800298 X509 *peer_issuer_issuer;
Hai Shalom899fcc72020-10-19 14:38:18 -0700299 char *peer_subject; /* peer subject info for authenticated peer */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800300
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800301 unsigned char client_random[SSL3_RANDOM_SIZE];
302 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700303
304 u16 cipher_suite;
305 int server_dh_prime_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306};
307
308
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700309static struct tls_context * tls_context_new(const struct tls_config *conf)
310{
311 struct tls_context *context = os_zalloc(sizeof(*context));
312 if (context == NULL)
313 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -0700314 dl_list_init(&context->sessions);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700315 if (conf) {
316 context->event_cb = conf->event_cb;
317 context->cb_ctx = conf->cb_ctx;
318 context->cert_in_cb = conf->cert_in_cb;
319 }
320 return context;
321}
322
323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324#ifdef CONFIG_NO_STDOUT_DEBUG
325
326static void _tls_show_errors(void)
327{
328 unsigned long err;
329
330 while ((err = ERR_get_error())) {
331 /* Just ignore the errors, since stdout is disabled */
332 }
333}
334#define tls_show_errors(l, f, t) _tls_show_errors()
335
336#else /* CONFIG_NO_STDOUT_DEBUG */
337
338static void tls_show_errors(int level, const char *func, const char *txt)
339{
340 unsigned long err;
341
342 wpa_printf(level, "OpenSSL: %s - %s %s",
343 func, txt, ERR_error_string(ERR_get_error(), NULL));
344
345 while ((err = ERR_get_error())) {
346 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
347 ERR_error_string(err, NULL));
348 }
349}
350
351#endif /* CONFIG_NO_STDOUT_DEBUG */
352
353
Hai Shalom74f70d42019-02-11 14:42:39 -0800354static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
355{
356 int flags;
357 X509_STORE *store;
358
359 store = X509_STORE_new();
360 if (!store) {
361 wpa_printf(MSG_DEBUG,
362 "OpenSSL: %s - failed to allocate new certificate store",
363 __func__);
364 return NULL;
365 }
366
367 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
368 tls_show_errors(MSG_WARNING, __func__,
369 "Failed to load root certificates");
370 X509_STORE_free(store);
371 return NULL;
372 }
373
374 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
375 if (check_crl == 2)
376 flags |= X509_V_FLAG_CRL_CHECK_ALL;
377
378 X509_STORE_set_flags(store, flags);
379
380 return store;
381}
382
383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384#ifdef CONFIG_NATIVE_WINDOWS
385
386/* Windows CryptoAPI and access to certificate stores */
387#include <wincrypt.h>
388
389#ifdef __MINGW32_VERSION
390/*
391 * MinGW does not yet include all the needed definitions for CryptoAPI, so
392 * define here whatever extra is needed.
393 */
394#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
395#define CERT_STORE_READONLY_FLAG 0x00008000
396#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
397
398#endif /* __MINGW32_VERSION */
399
400
401struct cryptoapi_rsa_data {
402 const CERT_CONTEXT *cert;
403 HCRYPTPROV crypt_prov;
404 DWORD key_spec;
405 BOOL free_crypt_prov;
406};
407
408
409static void cryptoapi_error(const char *msg)
410{
411 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
412 msg, (unsigned int) GetLastError());
413}
414
415
416static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
417 unsigned char *to, RSA *rsa, int padding)
418{
419 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
420 return 0;
421}
422
423
424static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
425 unsigned char *to, RSA *rsa, int padding)
426{
427 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
428 return 0;
429}
430
431
432static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
433 unsigned char *to, RSA *rsa, int padding)
434{
435 struct cryptoapi_rsa_data *priv =
436 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
437 HCRYPTHASH hash;
438 DWORD hash_size, len, i;
439 unsigned char *buf = NULL;
440 int ret = 0;
441
442 if (priv == NULL) {
443 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
444 ERR_R_PASSED_NULL_PARAMETER);
445 return 0;
446 }
447
448 if (padding != RSA_PKCS1_PADDING) {
449 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
450 RSA_R_UNKNOWN_PADDING_TYPE);
451 return 0;
452 }
453
454 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
455 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
456 __func__);
457 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
458 RSA_R_INVALID_MESSAGE_LENGTH);
459 return 0;
460 }
461
462 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
463 {
464 cryptoapi_error("CryptCreateHash failed");
465 return 0;
466 }
467
468 len = sizeof(hash_size);
469 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
470 0)) {
471 cryptoapi_error("CryptGetHashParam failed");
472 goto err;
473 }
474
475 if ((int) hash_size != flen) {
476 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
477 (unsigned) hash_size, flen);
478 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
479 RSA_R_INVALID_MESSAGE_LENGTH);
480 goto err;
481 }
482 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
483 cryptoapi_error("CryptSetHashParam failed");
484 goto err;
485 }
486
487 len = RSA_size(rsa);
488 buf = os_malloc(len);
489 if (buf == NULL) {
490 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
491 goto err;
492 }
493
494 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
495 cryptoapi_error("CryptSignHash failed");
496 goto err;
497 }
498
499 for (i = 0; i < len; i++)
500 to[i] = buf[len - i - 1];
501 ret = len;
502
503err:
504 os_free(buf);
505 CryptDestroyHash(hash);
506
507 return ret;
508}
509
510
511static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
512 unsigned char *to, RSA *rsa, int padding)
513{
514 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
515 return 0;
516}
517
518
519static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
520{
521 if (priv == NULL)
522 return;
523 if (priv->crypt_prov && priv->free_crypt_prov)
524 CryptReleaseContext(priv->crypt_prov, 0);
525 if (priv->cert)
526 CertFreeCertificateContext(priv->cert);
527 os_free(priv);
528}
529
530
531static int cryptoapi_finish(RSA *rsa)
532{
533 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
534 os_free((void *) rsa->meth);
535 rsa->meth = NULL;
536 return 1;
537}
538
539
540static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
541{
542 HCERTSTORE cs;
543 const CERT_CONTEXT *ret = NULL;
544
545 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
546 store | CERT_STORE_OPEN_EXISTING_FLAG |
547 CERT_STORE_READONLY_FLAG, L"MY");
548 if (cs == NULL) {
549 cryptoapi_error("Failed to open 'My system store'");
550 return NULL;
551 }
552
553 if (strncmp(name, "cert://", 7) == 0) {
554 unsigned short wbuf[255];
555 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
556 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
557 PKCS_7_ASN_ENCODING,
558 0, CERT_FIND_SUBJECT_STR,
559 wbuf, NULL);
560 } else if (strncmp(name, "hash://", 7) == 0) {
561 CRYPT_HASH_BLOB blob;
562 int len;
563 const char *hash = name + 7;
564 unsigned char *buf;
565
566 len = os_strlen(hash) / 2;
567 buf = os_malloc(len);
568 if (buf && hexstr2bin(hash, buf, len) == 0) {
569 blob.cbData = len;
570 blob.pbData = buf;
571 ret = CertFindCertificateInStore(cs,
572 X509_ASN_ENCODING |
573 PKCS_7_ASN_ENCODING,
574 0, CERT_FIND_HASH,
575 &blob, NULL);
576 }
577 os_free(buf);
578 }
579
580 CertCloseStore(cs, 0);
581
582 return ret;
583}
584
585
586static int tls_cryptoapi_cert(SSL *ssl, const char *name)
587{
588 X509 *cert = NULL;
589 RSA *rsa = NULL, *pub_rsa;
590 struct cryptoapi_rsa_data *priv;
591 RSA_METHOD *rsa_meth;
592
593 if (name == NULL ||
594 (strncmp(name, "cert://", 7) != 0 &&
595 strncmp(name, "hash://", 7) != 0))
596 return -1;
597
598 priv = os_zalloc(sizeof(*priv));
599 rsa_meth = os_zalloc(sizeof(*rsa_meth));
600 if (priv == NULL || rsa_meth == NULL) {
601 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
602 "for CryptoAPI RSA method");
603 os_free(priv);
604 os_free(rsa_meth);
605 return -1;
606 }
607
608 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
609 if (priv->cert == NULL) {
610 priv->cert = cryptoapi_find_cert(
611 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
612 }
613 if (priv->cert == NULL) {
614 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
615 "'%s'", name);
616 goto err;
617 }
618
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619 cert = d2i_X509(NULL,
620 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 priv->cert->cbCertEncoded);
622 if (cert == NULL) {
623 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
624 "encoding");
625 goto err;
626 }
627
628 if (!CryptAcquireCertificatePrivateKey(priv->cert,
629 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
630 NULL, &priv->crypt_prov,
631 &priv->key_spec,
632 &priv->free_crypt_prov)) {
633 cryptoapi_error("Failed to acquire a private key for the "
634 "certificate");
635 goto err;
636 }
637
638 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
639 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
640 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
641 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
642 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
643 rsa_meth->finish = cryptoapi_finish;
644 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
645 rsa_meth->app_data = (char *) priv;
646
647 rsa = RSA_new();
648 if (rsa == NULL) {
649 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
650 ERR_R_MALLOC_FAILURE);
651 goto err;
652 }
653
654 if (!SSL_use_certificate(ssl, cert)) {
655 RSA_free(rsa);
656 rsa = NULL;
657 goto err;
658 }
659 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
660 X509_free(cert);
661 cert = NULL;
662
663 rsa->n = BN_dup(pub_rsa->n);
664 rsa->e = BN_dup(pub_rsa->e);
665 if (!RSA_set_method(rsa, rsa_meth))
666 goto err;
667
668 if (!SSL_use_RSAPrivateKey(ssl, rsa))
669 goto err;
670 RSA_free(rsa);
671
672 return 0;
673
674err:
675 if (cert)
676 X509_free(cert);
677 if (rsa)
678 RSA_free(rsa);
679 else {
680 os_free(rsa_meth);
681 cryptoapi_free_data(priv);
682 }
683 return -1;
684}
685
686
687static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
688{
689 HCERTSTORE cs;
690 PCCERT_CONTEXT ctx = NULL;
691 X509 *cert;
692 char buf[128];
693 const char *store;
694#ifdef UNICODE
695 WCHAR *wstore;
696#endif /* UNICODE */
697
698 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
699 return -1;
700
701 store = name + 13;
702#ifdef UNICODE
703 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
704 if (wstore == NULL)
705 return -1;
706 wsprintf(wstore, L"%S", store);
707 cs = CertOpenSystemStore(0, wstore);
708 os_free(wstore);
709#else /* UNICODE */
710 cs = CertOpenSystemStore(0, store);
711#endif /* UNICODE */
712 if (cs == NULL) {
713 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
714 "'%s': error=%d", __func__, store,
715 (int) GetLastError());
716 return -1;
717 }
718
719 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800720 cert = d2i_X509(NULL,
721 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 ctx->cbCertEncoded);
723 if (cert == NULL) {
724 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
725 "X509 DER encoding for CA cert");
726 continue;
727 }
728
729 X509_NAME_oneline(X509_get_subject_name(cert), buf,
730 sizeof(buf));
731 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
732 "system certificate store: subject='%s'", buf);
733
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700734 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
735 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 tls_show_errors(MSG_WARNING, __func__,
737 "Failed to add ca_cert to OpenSSL "
738 "certificate store");
739 }
740
741 X509_free(cert);
742 }
743
744 if (!CertCloseStore(cs, 0)) {
745 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
746 "'%s': error=%d", __func__, name + 13,
747 (int) GetLastError());
748 }
749
750 return 0;
751}
752
753
754#else /* CONFIG_NATIVE_WINDOWS */
755
756static int tls_cryptoapi_cert(SSL *ssl, const char *name)
757{
758 return -1;
759}
760
761#endif /* CONFIG_NATIVE_WINDOWS */
762
763
764static void ssl_info_cb(const SSL *ssl, int where, int ret)
765{
766 const char *str;
767 int w;
768
769 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
770 w = where & ~SSL_ST_MASK;
771 if (w & SSL_ST_CONNECT)
772 str = "SSL_connect";
773 else if (w & SSL_ST_ACCEPT)
774 str = "SSL_accept";
775 else
776 str = "undefined";
777
778 if (where & SSL_CB_LOOP) {
779 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
780 str, SSL_state_string_long(ssl));
781 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700782 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
784 where & SSL_CB_READ ?
785 "read (remote end reported an error)" :
786 "write (local SSL3 detected an error)",
787 SSL_alert_type_string_long(ret),
788 SSL_alert_desc_string_long(ret));
789 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 if (where & SSL_CB_READ)
791 conn->read_alerts++;
792 else
793 conn->write_alerts++;
794 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700795 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700797 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798 os_memset(&ev, 0, sizeof(ev));
799 ev.alert.is_local = !(where & SSL_CB_READ);
800 ev.alert.type = SSL_alert_type_string_long(ret);
801 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700802 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 } else if (where & SSL_CB_EXIT && ret <= 0) {
805 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
806 str, ret == 0 ? "failed" : "error",
807 SSL_state_string_long(ssl));
808 }
809}
810
811
812#ifndef OPENSSL_NO_ENGINE
813/**
814 * tls_engine_load_dynamic_generic - load any openssl engine
815 * @pre: an array of commands and values that load an engine initialized
816 * in the engine specific function
817 * @post: an array of commands and values that initialize an already loaded
818 * engine (or %NULL if not required)
819 * @id: the engine id of the engine to load (only required if post is not %NULL
820 *
821 * This function is a generic function that loads any openssl engine.
822 *
823 * Returns: 0 on success, -1 on failure
824 */
825static int tls_engine_load_dynamic_generic(const char *pre[],
826 const char *post[], const char *id)
827{
828 ENGINE *engine;
829 const char *dynamic_id = "dynamic";
830
831 engine = ENGINE_by_id(id);
832 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
834 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700835 /*
836 * If it was auto-loaded by ENGINE_by_id() we might still
837 * need to tell it which PKCS#11 module to use in legacy
838 * (non-p11-kit) environments. Do so now; even if it was
839 * properly initialised before, setting it again will be
840 * harmless.
841 */
842 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 }
844 ERR_clear_error();
845
846 engine = ENGINE_by_id(dynamic_id);
847 if (engine == NULL) {
848 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
849 dynamic_id,
850 ERR_error_string(ERR_get_error(), NULL));
851 return -1;
852 }
853
854 /* Perform the pre commands. This will load the engine. */
855 while (pre && pre[0]) {
856 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
857 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
858 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
859 "%s %s [%s]", pre[0], pre[1],
860 ERR_error_string(ERR_get_error(), NULL));
861 ENGINE_free(engine);
862 return -1;
863 }
864 pre += 2;
865 }
866
867 /*
868 * Free the reference to the "dynamic" engine. The loaded engine can
869 * now be looked up using ENGINE_by_id().
870 */
871 ENGINE_free(engine);
872
873 engine = ENGINE_by_id(id);
874 if (engine == NULL) {
875 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
876 id, ERR_error_string(ERR_get_error(), NULL));
877 return -1;
878 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700879 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 while (post && post[0]) {
881 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
882 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
883 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
884 " %s %s [%s]", post[0], post[1],
885 ERR_error_string(ERR_get_error(), NULL));
886 ENGINE_remove(engine);
887 ENGINE_free(engine);
888 return -1;
889 }
890 post += 2;
891 }
892 ENGINE_free(engine);
893
894 return 0;
895}
896
897
898/**
899 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
900 * @pkcs11_so_path: pksc11_so_path from the configuration
901 * @pcks11_module_path: pkcs11_module_path from the configuration
902 */
903static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
904 const char *pkcs11_module_path)
905{
906 char *engine_id = "pkcs11";
907 const char *pre_cmd[] = {
908 "SO_PATH", NULL /* pkcs11_so_path */,
909 "ID", NULL /* engine_id */,
910 "LIST_ADD", "1",
911 /* "NO_VCHECK", "1", */
912 "LOAD", NULL,
913 NULL, NULL
914 };
915 const char *post_cmd[] = {
916 "MODULE_PATH", NULL /* pkcs11_module_path */,
917 NULL, NULL
918 };
919
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800920 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 return 0;
922
923 pre_cmd[1] = pkcs11_so_path;
924 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800925 if (pkcs11_module_path)
926 post_cmd[1] = pkcs11_module_path;
927 else
928 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929
930 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
931 pkcs11_so_path);
932
933 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
934}
935
936
937/**
938 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
939 * @opensc_so_path: opensc_so_path from the configuration
940 */
941static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
942{
943 char *engine_id = "opensc";
944 const char *pre_cmd[] = {
945 "SO_PATH", NULL /* opensc_so_path */,
946 "ID", NULL /* engine_id */,
947 "LIST_ADD", "1",
948 "LOAD", NULL,
949 NULL, NULL
950 };
951
952 if (!opensc_so_path)
953 return 0;
954
955 pre_cmd[1] = opensc_so_path;
956 pre_cmd[3] = engine_id;
957
958 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
959 opensc_so_path);
960
961 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
962}
963#endif /* OPENSSL_NO_ENGINE */
964
965
Sunil Ravia04bd252022-05-02 22:54:18 -0700966static struct tls_session_data * get_session_data(struct tls_context *context,
967 const struct wpabuf *buf)
968{
969 struct tls_session_data *data;
970
971 dl_list_for_each(data, &context->sessions, struct tls_session_data,
972 list) {
973 if (data->buf == buf)
974 return data;
975 }
976
977 return NULL;
978}
979
980
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800981static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
982{
983 struct wpabuf *buf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700984 struct tls_context *context;
985 struct tls_session_data *found;
986
987 wpa_printf(MSG_DEBUG,
988 "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess,
989 tls_ex_idx_session);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800990
991 if (tls_ex_idx_session < 0)
992 return;
993 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
994 if (!buf)
995 return;
Sunil Ravia04bd252022-05-02 22:54:18 -0700996
997 context = SSL_CTX_get_app_data(ctx);
998 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
999 found = get_session_data(context, buf);
1000 if (!found) {
1001 wpa_printf(MSG_DEBUG,
1002 "OpenSSL: Do not free application session data %p (sess %p)",
1003 buf, sess);
1004 return;
1005 }
1006
1007 dl_list_del(&found->list);
1008 os_free(found);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001009 wpa_printf(MSG_DEBUG,
1010 "OpenSSL: Free application session data %p (sess %p)",
1011 buf, sess);
1012 wpabuf_free(buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001013}
1014
1015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016void * tls_init(const struct tls_config *conf)
1017{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001020 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001021 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022
1023 if (tls_openssl_ref_count == 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001024 void openssl_load_legacy_provider(void);
1025
1026 openssl_load_legacy_provider();
1027
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001028 tls_global = context = tls_context_new(conf);
1029 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031#ifdef CONFIG_FIPS
1032#ifdef OPENSSL_FIPS
1033 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001034 static int fips_enabled = 0;
1035
1036 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
1038 "mode");
1039 ERR_load_crypto_strings();
1040 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001041 os_free(tls_global);
1042 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001044 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001046 fips_enabled = 1;
1047 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 }
1049#else /* OPENSSL_FIPS */
1050 if (conf && conf->fips_mode) {
1051 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
1052 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001053 os_free(tls_global);
1054 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 return NULL;
1056 }
1057#endif /* OPENSSL_FIPS */
1058#endif /* CONFIG_FIPS */
Sunil Ravia04bd252022-05-02 22:54:18 -07001059#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 SSL_load_error_strings();
1061 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001062#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 EVP_add_digest(EVP_sha256());
1064#endif /* OPENSSL_NO_SHA256 */
1065 /* TODO: if /dev/urandom is available, PRNG is seeded
1066 * automatically. If this is not the case, random data should
1067 * be added here. */
1068
1069#ifdef PKCS12_FUNCS
1070#ifndef OPENSSL_NO_RC2
1071 /*
1072 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1073 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1074 * versions, but it looks like OpenSSL 1.0.0 does not do that
1075 * anymore.
1076 */
1077 EVP_add_cipher(EVP_rc2_40_cbc());
1078#endif /* OPENSSL_NO_RC2 */
1079 PKCS12_PBE_add();
1080#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001081#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001082 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001083 context = tls_context_new(conf);
1084 if (context == NULL)
1085 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 }
1087 tls_openssl_ref_count++;
1088
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001089 data = os_zalloc(sizeof(*data));
1090 if (data)
1091 ssl = SSL_CTX_new(SSLv23_method());
1092 else
1093 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001094 if (ssl == NULL) {
1095 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001096 if (context != tls_global)
1097 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001098 if (tls_openssl_ref_count == 0) {
1099 os_free(tls_global);
1100 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001101 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001102 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001104 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001105 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001106 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001107 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001108 data->crl_reload_interval = conf->crl_reload_interval;
1109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001111 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1112 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1113
Hai Shalom60840252021-02-19 19:02:11 -08001114 SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY);
1115
Dmitry Shmidt29333592017-01-09 12:27:11 -08001116#ifdef SSL_MODE_NO_AUTO_CHAIN
1117 /* Number of deployed use cases assume the default OpenSSL behavior of
1118 * auto chaining the local certificate is in use. BoringSSL removed this
1119 * functionality by default, so we need to restore it here to avoid
1120 * breaking existing use cases. */
1121 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1122#endif /* SSL_MODE_NO_AUTO_CHAIN */
1123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001125 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001126 if (data->tls_session_lifetime > 0) {
1127 SSL_CTX_set_quiet_shutdown(ssl, 1);
1128 /*
1129 * Set default context here. In practice, this will be replaced
1130 * by the per-EAP method context in tls_connection_set_verify().
1131 */
1132 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1133 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1134 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1135 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
Sunil Ravia04bd252022-05-02 22:54:18 -07001136#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1137 !defined(LIBRESSL_VERSION_NUMBER) && \
1138 !defined(OPENSSL_IS_BORINGSSL)
1139 /* One session ticket is sufficient for EAP-TLS */
1140 SSL_CTX_set_num_tickets(ssl, 1);
1141#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001142 } else {
1143 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
Sunil Ravia04bd252022-05-02 22:54:18 -07001144#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1145 !defined(LIBRESSL_VERSION_NUMBER) && \
1146 !defined(OPENSSL_IS_BORINGSSL)
1147 SSL_CTX_set_num_tickets(ssl, 0);
1148#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001149 }
1150
1151 if (tls_ex_idx_session < 0) {
1152 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1153 0, NULL, NULL, NULL, NULL);
1154 if (tls_ex_idx_session < 0) {
1155 tls_deinit(data);
1156 return NULL;
1157 }
1158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159
1160#ifndef OPENSSL_NO_ENGINE
Hai Shalom81f62d82019-07-22 12:10:00 -07001161 wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines");
1162 ENGINE_load_builtin_engines();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (conf &&
1165 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1166 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1168 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1169 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001170 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 return NULL;
1172 }
1173 }
1174#endif /* OPENSSL_NO_ENGINE */
1175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001176 if (conf && conf->openssl_ciphers)
1177 ciphers = conf->openssl_ciphers;
1178 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001179 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001180 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1181 wpa_printf(MSG_ERROR,
1182 "OpenSSL: Failed to set cipher string '%s'",
1183 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001184 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001185 return NULL;
1186 }
1187
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001188 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189}
1190
1191
1192void tls_deinit(void *ssl_ctx)
1193{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001194 struct tls_data *data = ssl_ctx;
1195 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001196 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Sunil Ravia04bd252022-05-02 22:54:18 -07001197 struct tls_session_data *sess_data;
1198
1199 if (data->tls_session_lifetime > 0) {
1200 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions");
1201 SSL_CTX_flush_sessions(ssl, 0);
1202 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done");
1203 }
1204 while ((sess_data = dl_list_first(&context->sessions,
1205 struct tls_session_data, list))) {
1206 wpa_printf(MSG_DEBUG,
1207 "OpenSSL: Freeing not-flushed session data %p",
1208 sess_data->buf);
1209 wpabuf_free(sess_data->buf);
1210 dl_list_del(&sess_data->list);
1211 os_free(sess_data);
1212 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001213 if (context != tls_global)
1214 os_free(context);
Hai Shalom74f70d42019-02-11 14:42:39 -08001215 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 SSL_CTX_free(ssl);
1217
1218 tls_openssl_ref_count--;
1219 if (tls_openssl_ref_count == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001220#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221#ifndef OPENSSL_NO_ENGINE
1222 ENGINE_cleanup();
1223#endif /* OPENSSL_NO_ENGINE */
1224 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001225 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 ERR_free_strings();
1227 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001228#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001229 os_free(tls_global->ocsp_stapling_response);
1230 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 os_free(tls_global);
1232 tls_global = NULL;
1233 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001234
Hai Shalom021b0b52019-04-10 11:17:58 -07001235 os_free(data->check_cert_subject);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001236 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237}
1238
1239
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001240#ifndef OPENSSL_NO_ENGINE
1241
1242/* Cryptoki return values */
1243#define CKR_PIN_INCORRECT 0x000000a0
1244#define CKR_PIN_INVALID 0x000000a1
1245#define CKR_PIN_LEN_RANGE 0x000000a2
1246
1247/* libp11 */
1248#define ERR_LIB_PKCS11 ERR_LIB_USER
1249
1250static int tls_is_pin_error(unsigned int err)
1251{
1252 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1253 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1254 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1255 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1256}
1257
1258#endif /* OPENSSL_NO_ENGINE */
1259
1260
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001261#ifdef ANDROID
1262/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1263EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1264#endif /* ANDROID */
1265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1267 const char *pin, const char *key_id,
1268 const char *cert_id, const char *ca_cert_id)
1269{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001270#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1271#if !defined(OPENSSL_NO_ENGINE)
1272#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1273#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001274 if (!key_id)
1275 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001276 conn->engine = NULL;
1277 conn->private_key = EVP_PKEY_from_keystore(key_id);
1278 if (!conn->private_key) {
1279 wpa_printf(MSG_ERROR,
1280 "ENGINE: cannot load private key with id '%s' [%s]",
1281 key_id,
1282 ERR_error_string(ERR_get_error(), NULL));
1283 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1284 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001285#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287#ifndef OPENSSL_NO_ENGINE
1288 int ret = -1;
1289 if (engine_id == NULL) {
1290 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1291 return -1;
1292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293
1294 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001295#ifdef ANDROID
1296 ENGINE_load_dynamic();
1297#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 conn->engine = ENGINE_by_id(engine_id);
1299 if (!conn->engine) {
1300 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1301 engine_id, ERR_error_string(ERR_get_error(), NULL));
1302 goto err;
1303 }
1304 if (ENGINE_init(conn->engine) != 1) {
1305 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1306 "(engine: %s) [%s]", engine_id,
1307 ERR_error_string(ERR_get_error(), NULL));
1308 goto err;
1309 }
1310 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1311
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001312#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001313 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1315 ERR_error_string(ERR_get_error(), NULL));
1316 goto err;
1317 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001318#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 if (key_id) {
1320 /*
1321 * Ensure that the ENGINE does not attempt to use the OpenSSL
1322 * UI system to obtain a PIN, if we didn't provide one.
1323 */
1324 struct {
1325 const void *password;
1326 const char *prompt_info;
1327 } key_cb = { "", NULL };
1328
1329 /* load private key first in-case PIN is required for cert */
1330 conn->private_key = ENGINE_load_private_key(conn->engine,
1331 key_id, NULL,
1332 &key_cb);
1333 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001334 unsigned long err = ERR_get_error();
1335
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001336 wpa_printf(MSG_ERROR,
1337 "ENGINE: cannot load private key with id '%s' [%s]",
1338 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001339 ERR_error_string(err, NULL));
1340 if (tls_is_pin_error(err))
1341 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1342 else
1343 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001344 goto err;
1345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 }
1347
1348 /* handle a certificate and/or CA certificate */
1349 if (cert_id || ca_cert_id) {
1350 const char *cmd_name = "LOAD_CERT_CTRL";
1351
1352 /* test if the engine supports a LOAD_CERT_CTRL */
1353 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1354 0, (void *)cmd_name, NULL)) {
1355 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1356 " loading certificates");
1357 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1358 goto err;
1359 }
1360 }
1361
1362 return 0;
1363
1364err:
1365 if (conn->engine) {
1366 ENGINE_free(conn->engine);
1367 conn->engine = NULL;
1368 }
1369
1370 if (conn->private_key) {
1371 EVP_PKEY_free(conn->private_key);
1372 conn->private_key = NULL;
1373 }
1374
1375 return ret;
1376#else /* OPENSSL_NO_ENGINE */
1377 return 0;
1378#endif /* OPENSSL_NO_ENGINE */
1379}
1380
1381
1382static void tls_engine_deinit(struct tls_connection *conn)
1383{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001384#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1386 if (conn->private_key) {
1387 EVP_PKEY_free(conn->private_key);
1388 conn->private_key = NULL;
1389 }
1390 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001391#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001393#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 conn->engine = NULL;
1395 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001396#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397}
1398
1399
1400int tls_get_errors(void *ssl_ctx)
1401{
1402 int count = 0;
1403 unsigned long err;
1404
1405 while ((err = ERR_get_error())) {
1406 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1407 ERR_error_string(err, NULL));
1408 count++;
1409 }
1410
1411 return count;
1412}
1413
Jouni Malinen26af48b2014-04-09 13:02:53 +03001414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001415static const char * openssl_content_type(int content_type)
1416{
1417 switch (content_type) {
1418 case 20:
1419 return "change cipher spec";
1420 case 21:
1421 return "alert";
1422 case 22:
1423 return "handshake";
1424 case 23:
1425 return "application data";
1426 case 24:
1427 return "heartbeat";
1428 case 256:
1429 return "TLS header info"; /* pseudo content type */
Hai Shalom81f62d82019-07-22 12:10:00 -07001430 case 257:
1431 return "inner content type"; /* pseudo content type */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001432 default:
1433 return "?";
1434 }
1435}
1436
1437
1438static const char * openssl_handshake_type(int content_type, const u8 *buf,
1439 size_t len)
1440{
Hai Shalom81f62d82019-07-22 12:10:00 -07001441 if (content_type == 257 && buf && len == 1)
1442 return openssl_content_type(buf[0]);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001443 if (content_type != 22 || !buf || len == 0)
1444 return "";
1445 switch (buf[0]) {
1446 case 0:
1447 return "hello request";
1448 case 1:
1449 return "client hello";
1450 case 2:
1451 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001452 case 3:
1453 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001454 case 4:
1455 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001456 case 5:
1457 return "end of early data";
1458 case 6:
1459 return "hello retry request";
1460 case 8:
1461 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001462 case 11:
1463 return "certificate";
1464 case 12:
1465 return "server key exchange";
1466 case 13:
1467 return "certificate request";
1468 case 14:
1469 return "server hello done";
1470 case 15:
1471 return "certificate verify";
1472 case 16:
1473 return "client key exchange";
1474 case 20:
1475 return "finished";
1476 case 21:
1477 return "certificate url";
1478 case 22:
1479 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001480 case 23:
1481 return "supplemental data";
1482 case 24:
1483 return "key update";
1484 case 254:
1485 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001486 default:
1487 return "?";
1488 }
1489}
1490
1491
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001492#ifdef CONFIG_SUITEB
1493
1494static void check_server_hello(struct tls_connection *conn,
1495 const u8 *pos, const u8 *end)
1496{
1497 size_t payload_len, id_len;
1498
1499 /*
1500 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1501 * not make it cleanly available during handshake and we need to know
1502 * whether DHE was selected.
1503 */
1504
1505 if (end - pos < 3)
1506 return;
1507 payload_len = WPA_GET_BE24(pos);
1508 pos += 3;
1509
1510 if ((size_t) (end - pos) < payload_len)
1511 return;
1512 end = pos + payload_len;
1513
1514 /* Skip Version and Random */
1515 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1516 return;
1517 pos += 2 + SSL3_RANDOM_SIZE;
1518
1519 /* Skip Session ID */
1520 if (end - pos < 1)
1521 return;
1522 id_len = *pos++;
1523 if ((size_t) (end - pos) < id_len)
1524 return;
1525 pos += id_len;
1526
1527 if (end - pos < 2)
1528 return;
1529 conn->cipher_suite = WPA_GET_BE16(pos);
1530 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1531 conn->cipher_suite);
1532}
1533
1534
1535static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1536 const u8 *pos, const u8 *end)
1537{
1538 size_t payload_len;
1539 u16 dh_len;
1540 BIGNUM *p;
1541 int bits;
1542
1543 if (!(conn->flags & TLS_CONN_SUITEB))
1544 return;
1545
1546 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1547 if (conn->cipher_suite != 0x9f)
1548 return;
1549
1550 if (end - pos < 3)
1551 return;
1552 payload_len = WPA_GET_BE24(pos);
1553 pos += 3;
1554
1555 if ((size_t) (end - pos) < payload_len)
1556 return;
1557 end = pos + payload_len;
1558
1559 if (end - pos < 2)
1560 return;
1561 dh_len = WPA_GET_BE16(pos);
1562 pos += 2;
1563
1564 if ((size_t) (end - pos) < dh_len)
1565 return;
1566 p = BN_bin2bn(pos, dh_len, NULL);
1567 if (!p)
1568 return;
1569
1570 bits = BN_num_bits(p);
1571 BN_free(p);
1572
1573 conn->server_dh_prime_len = bits;
1574 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1575 conn->server_dh_prime_len);
1576}
1577
1578#endif /* CONFIG_SUITEB */
1579
1580
Jouni Malinen26af48b2014-04-09 13:02:53 +03001581static void tls_msg_cb(int write_p, int version, int content_type,
1582 const void *buf, size_t len, SSL *ssl, void *arg)
1583{
1584 struct tls_connection *conn = arg;
1585 const u8 *pos = buf;
1586
Sunil8cd6f4d2022-06-28 18:40:46 +00001587#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1588 if ((SSL_version(ssl) == TLS1_VERSION ||
1589 SSL_version(ssl) == TLS1_1_VERSION) &&
1590 SSL_get_security_level(ssl) > 0) {
1591 wpa_printf(MSG_DEBUG,
1592 "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm");
1593 SSL_set_security_level(ssl, 0);
1594 }
1595#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001596 if (write_p == 2) {
1597 wpa_printf(MSG_DEBUG,
1598 "OpenSSL: session ver=0x%x content_type=%d",
1599 version, content_type);
1600 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1601 return;
1602 }
1603
1604 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1605 write_p ? "TX" : "RX", version, content_type,
1606 openssl_content_type(content_type),
1607 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001608 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1609 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1610 size_t payload_len = WPA_GET_BE16(pos + 1);
1611 if (payload_len + 3 > len) {
1612 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1613 conn->invalid_hb_used = 1;
1614 }
1615 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001616
1617#ifdef CONFIG_SUITEB
1618 /*
1619 * Need to parse these handshake messages to be able to check DH prime
1620 * length since OpenSSL does not expose the new cipher suite and DH
1621 * parameters during handshake (e.g., for cert_cb() callback).
1622 */
1623 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1624 check_server_hello(conn, pos + 1, pos + len);
1625 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1626 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1627#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001628}
1629
1630
Sunil Ravia04bd252022-05-02 22:54:18 -07001631#ifdef CONFIG_TESTING_OPTIONS
1632#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1633/*
1634 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1635 * material will be exported that you may use with Wireshark to decode any
1636 * TLS flows. Please see the following for more details:
1637 *
1638 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1639 *
1640 * Example logging sessions are (you should delete the file on each run):
1641 *
1642 * rm -f /tmp/sslkey.log
1643 * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ...
1644 *
1645 * rm -f /tmp/sslkey.log
1646 * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ...
1647 *
1648 * rm -f /tmp/sslkey.log
1649 * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ...
1650 */
1651static void tls_keylog_cb(const SSL *ssl, const char *line)
1652{
1653 int fd;
1654 const char *filename;
1655 struct iovec iov[2];
1656
1657 filename = getenv("SSLKEYLOGFILE");
1658 if (!filename)
1659 return;
1660
1661 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1662 if (fd < 0) {
1663 wpa_printf(MSG_ERROR,
1664 "OpenSSL: Failed to open keylog file %s: %s",
1665 filename, strerror(errno));
1666 return;
1667 }
1668
1669 /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed
1670 * to be atomic for O_APPEND. */
1671 iov[0].iov_base = (void *) line;
1672 iov[0].iov_len = os_strlen(line);
1673 iov[1].iov_base = "\n";
1674 iov[1].iov_len = 1;
1675
1676 if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) {
1677 wpa_printf(MSG_DEBUG,
1678 "OpenSSL: Failed to write to keylog file %s: %s",
1679 filename, strerror(errno));
1680 }
1681
1682 close(fd);
1683}
1684#endif
1685#endif /* CONFIG_TESTING_OPTIONS */
1686
1687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688struct tls_connection * tls_connection_init(void *ssl_ctx)
1689{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001690 struct tls_data *data = ssl_ctx;
1691 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 struct tls_connection *conn;
1693 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001694 X509_STORE *new_cert_store;
1695 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001696 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697
Hai Shalom74f70d42019-02-11 14:42:39 -08001698 /* Replace X509 store if it is time to update CRL. */
1699 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1700 os_reltime_expired(&now, &data->crl_last_reload,
1701 data->crl_reload_interval)) {
1702 wpa_printf(MSG_INFO,
1703 "OpenSSL: Flushing X509 store with ca_cert file");
1704 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1705 data->check_crl);
1706 if (!new_cert_store) {
1707 wpa_printf(MSG_ERROR,
1708 "OpenSSL: Error replacing X509 store with ca_cert file");
1709 } else {
1710 /* Replace old store */
1711 SSL_CTX_set_cert_store(ssl, new_cert_store);
1712 data->crl_last_reload = now;
1713 }
1714 }
1715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716 conn = os_zalloc(sizeof(*conn));
1717 if (conn == NULL)
1718 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001719 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001720 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 conn->ssl = SSL_new(ssl);
1722 if (conn->ssl == NULL) {
1723 tls_show_errors(MSG_INFO, __func__,
1724 "Failed to initialize new SSL connection");
1725 os_free(conn);
1726 return NULL;
1727 }
1728
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001729 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001731 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1732 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1734 SSL_OP_SINGLE_DH_USE;
1735#ifdef SSL_OP_NO_COMPRESSION
1736 options |= SSL_OP_NO_COMPRESSION;
1737#endif /* SSL_OP_NO_COMPRESSION */
1738 SSL_set_options(conn->ssl, options);
Hai Shalom81f62d82019-07-22 12:10:00 -07001739#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
1740 /* Hopefully there is no need for middlebox compatibility mechanisms
1741 * when going through EAP authentication. */
1742 SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
1743#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744
Sunil Ravia04bd252022-05-02 22:54:18 -07001745#ifdef CONFIG_TESTING_OPTIONS
1746#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1747 /* Set the keylog file if the admin requested it. */
1748 if (getenv("SSLKEYLOGFILE"))
1749 SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb);
1750#endif
1751#endif /* CONFIG_TESTING_OPTIONS */
1752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 conn->ssl_in = BIO_new(BIO_s_mem());
1754 if (!conn->ssl_in) {
1755 tls_show_errors(MSG_INFO, __func__,
1756 "Failed to create a new BIO for ssl_in");
1757 SSL_free(conn->ssl);
1758 os_free(conn);
1759 return NULL;
1760 }
1761
1762 conn->ssl_out = BIO_new(BIO_s_mem());
1763 if (!conn->ssl_out) {
1764 tls_show_errors(MSG_INFO, __func__,
1765 "Failed to create a new BIO for ssl_out");
1766 SSL_free(conn->ssl);
1767 BIO_free(conn->ssl_in);
1768 os_free(conn);
1769 return NULL;
1770 }
1771
1772 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1773
1774 return conn;
1775}
1776
1777
1778void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1779{
1780 if (conn == NULL)
1781 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001782 if (conn->success_data) {
1783 /*
1784 * Make sure ssl_clear_bad_session() does not remove this
1785 * session.
1786 */
1787 SSL_set_quiet_shutdown(conn->ssl, 1);
1788 SSL_shutdown(conn->ssl);
1789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 SSL_free(conn->ssl);
1791 tls_engine_deinit(conn);
1792 os_free(conn->subject_match);
1793 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001794 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001795 os_free(conn->domain_match);
Hai Shalom021b0b52019-04-10 11:17:58 -07001796 os_free(conn->check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 os_free(conn->session_ticket);
Hai Shalom899fcc72020-10-19 14:38:18 -07001798 os_free(conn->peer_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 os_free(conn);
1800}
1801
1802
1803int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1804{
1805 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1806}
1807
1808
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001809char * tls_connection_peer_serial_num(void *tls_ctx,
1810 struct tls_connection *conn)
1811{
1812 ASN1_INTEGER *ser;
1813 char *serial_num;
1814 size_t len;
1815
1816 if (!conn->peer_cert)
1817 return NULL;
1818
1819 ser = X509_get_serialNumber(conn->peer_cert);
1820 if (!ser)
1821 return NULL;
1822
1823 len = ASN1_STRING_length(ser) * 2 + 1;
1824 serial_num = os_malloc(len);
1825 if (!serial_num)
1826 return NULL;
1827 wpa_snprintf_hex_uppercase(serial_num, len,
1828 ASN1_STRING_get0_data(ser),
1829 ASN1_STRING_length(ser));
1830 return serial_num;
1831}
1832
1833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1835{
1836 if (conn == NULL)
1837 return -1;
1838
1839 /* Shutdown previous TLS connection without notifying the peer
1840 * because the connection was already terminated in practice
1841 * and "close notify" shutdown alert would confuse AS. */
1842 SSL_set_quiet_shutdown(conn->ssl, 1);
1843 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001844 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845}
1846
1847
1848static int tls_match_altsubject_component(X509 *cert, int type,
1849 const char *value, size_t len)
1850{
1851 GENERAL_NAME *gen;
1852 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001853 int found = 0;
1854 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
1856 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1857
1858 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1859 gen = sk_GENERAL_NAME_value(ext, i);
1860 if (gen->type != type)
1861 continue;
1862 if (os_strlen((char *) gen->d.ia5->data) == len &&
1863 os_memcmp(value, gen->d.ia5->data, len) == 0)
1864 found++;
1865 }
1866
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001867 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 return found;
1870}
1871
1872
1873static int tls_match_altsubject(X509 *cert, const char *match)
1874{
1875 int type;
1876 const char *pos, *end;
1877 size_t len;
1878
1879 pos = match;
1880 do {
1881 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1882 type = GEN_EMAIL;
1883 pos += 6;
1884 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1885 type = GEN_DNS;
1886 pos += 4;
1887 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1888 type = GEN_URI;
1889 pos += 4;
1890 } else {
1891 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1892 "match '%s'", pos);
1893 return 0;
1894 }
1895 end = os_strchr(pos, ';');
1896 while (end) {
1897 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1898 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1899 os_strncmp(end + 1, "URI:", 4) == 0)
1900 break;
1901 end = os_strchr(end + 1, ';');
1902 }
1903 if (end)
1904 len = end - pos;
1905 else
1906 len = os_strlen(pos);
1907 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1908 return 1;
1909 pos = end + 1;
1910 } while (end);
1911
1912 return 0;
1913}
1914
1915
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001916#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001917static int domain_suffix_match(const u8 *val, size_t len, const char *match,
Hai Shalom021b0b52019-04-10 11:17:58 -07001918 size_t match_len, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001919{
Hai Shalom021b0b52019-04-10 11:17:58 -07001920 size_t i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001921
1922 /* Check for embedded nuls that could mess up suffix matching */
1923 for (i = 0; i < len; i++) {
1924 if (val[i] == '\0') {
1925 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1926 return 0;
1927 }
1928 }
1929
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001930 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001931 return 0;
1932
1933 if (os_strncasecmp((const char *) val + len - match_len, match,
1934 match_len) != 0)
1935 return 0; /* no match */
1936
1937 if (match_len == len)
1938 return 1; /* exact match */
1939
1940 if (val[len - match_len - 1] == '.')
1941 return 1; /* full label match completes suffix match */
1942
1943 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1944 return 0;
1945}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001946#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001947
1948
Hai Shalom021b0b52019-04-10 11:17:58 -07001949struct tls_dn_field_order_cnt {
1950 u8 cn;
1951 u8 c;
1952 u8 l;
1953 u8 st;
1954 u8 o;
1955 u8 ou;
1956 u8 email;
1957};
1958
1959
1960static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt,
1961 int nid)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001962{
Hai Shalom021b0b52019-04-10 11:17:58 -07001963 switch (nid) {
1964 case NID_commonName:
1965 return dn_cnt->cn;
1966 case NID_countryName:
1967 return dn_cnt->c;
1968 case NID_localityName:
1969 return dn_cnt->l;
1970 case NID_stateOrProvinceName:
1971 return dn_cnt->st;
1972 case NID_organizationName:
1973 return dn_cnt->o;
1974 case NID_organizationalUnitName:
1975 return dn_cnt->ou;
1976 case NID_pkcs9_emailAddress:
1977 return dn_cnt->email;
1978 default:
1979 wpa_printf(MSG_ERROR,
1980 "TLS: Unknown NID '%d' in check_cert_subject",
1981 nid);
1982 return -1;
1983 }
1984}
1985
1986
1987/**
1988 * match_dn_field - Match configuration DN field against Certificate DN field
1989 * @cert: Certificate
1990 * @nid: NID of DN field
1991 * @field: Field name
1992 * @value DN field value which is passed from configuration
1993 * e.g., if configuration have C=US and this argument will point to US.
1994 * @dn_cnt: DN matching context
1995 * Returns: 1 on success and 0 on failure
1996 */
1997static int match_dn_field(const X509 *cert, int nid, const char *field,
1998 const char *value,
1999 const struct tls_dn_field_order_cnt *dn_cnt)
2000{
2001 int i, ret = 0, len, config_dn_field_index, match_index = 0;
2002 X509_NAME *name;
2003
2004 len = os_strlen(value);
2005 name = X509_get_subject_name((X509 *) cert);
2006
2007 /* Assign incremented cnt for every field of DN to check DN field in
2008 * right order */
2009 config_dn_field_index = get_dn_field_index(dn_cnt, nid);
2010 if (config_dn_field_index < 0)
2011 return 0;
2012
2013 /* Fetch value based on NID */
2014 for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) {
2015 X509_NAME_ENTRY *e;
2016 ASN1_STRING *cn;
2017
2018 e = X509_NAME_get_entry(name, i);
2019 if (!e)
2020 continue;
2021
2022 cn = X509_NAME_ENTRY_get_data(e);
2023 if (!cn)
2024 continue;
2025
2026 match_index++;
2027
2028 /* check for more than one DN field with same name */
2029 if (match_index != config_dn_field_index)
2030 continue;
2031
2032 /* Check wildcard at the right end side */
2033 /* E.g., if OU=develop* mentioned in configuration, allow 'OU'
2034 * of the subject in the client certificate to start with
2035 * 'develop' */
2036 if (len > 0 && value[len - 1] == '*') {
2037 /* Compare actual certificate DN field value with
2038 * configuration DN field value up to the specified
2039 * length. */
2040 ret = ASN1_STRING_length(cn) >= len - 1 &&
2041 os_memcmp(ASN1_STRING_get0_data(cn), value,
2042 len - 1) == 0;
2043 } else {
2044 /* Compare actual certificate DN field value with
2045 * configuration DN field value */
2046 ret = ASN1_STRING_length(cn) == len &&
2047 os_memcmp(ASN1_STRING_get0_data(cn), value,
2048 len) == 0;
2049 }
2050 if (!ret) {
2051 wpa_printf(MSG_ERROR,
2052 "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'",
2053 field, value, ASN1_STRING_get0_data(cn));
2054 }
2055 break;
2056 }
2057
2058 return ret;
2059}
2060
2061
2062/**
2063 * get_value_from_field - Get value from DN field
2064 * @cert: Certificate
2065 * @field_str: DN field string which is passed from configuration file (e.g.,
2066 * C=US)
2067 * @dn_cnt: DN matching context
2068 * Returns: 1 on success and 0 on failure
2069 */
2070static int get_value_from_field(const X509 *cert, char *field_str,
2071 struct tls_dn_field_order_cnt *dn_cnt)
2072{
2073 int nid;
2074 char *context = NULL, *name, *value;
2075
2076 if (os_strcmp(field_str, "*") == 0)
2077 return 1; /* wildcard matches everything */
2078
2079 name = str_token(field_str, "=", &context);
2080 if (!name)
2081 return 0;
2082
2083 /* Compare all configured DN fields and assign nid based on that to
2084 * fetch correct value from certificate subject */
2085 if (os_strcmp(name, "CN") == 0) {
2086 nid = NID_commonName;
2087 dn_cnt->cn++;
2088 } else if(os_strcmp(name, "C") == 0) {
2089 nid = NID_countryName;
2090 dn_cnt->c++;
2091 } else if (os_strcmp(name, "L") == 0) {
2092 nid = NID_localityName;
2093 dn_cnt->l++;
2094 } else if (os_strcmp(name, "ST") == 0) {
2095 nid = NID_stateOrProvinceName;
2096 dn_cnt->st++;
2097 } else if (os_strcmp(name, "O") == 0) {
2098 nid = NID_organizationName;
2099 dn_cnt->o++;
2100 } else if (os_strcmp(name, "OU") == 0) {
2101 nid = NID_organizationalUnitName;
2102 dn_cnt->ou++;
2103 } else if (os_strcmp(name, "emailAddress") == 0) {
2104 nid = NID_pkcs9_emailAddress;
2105 dn_cnt->email++;
2106 } else {
2107 wpa_printf(MSG_ERROR,
2108 "TLS: Unknown field '%s' in check_cert_subject", name);
2109 return 0;
2110 }
2111
2112 value = str_token(field_str, "=", &context);
2113 if (!value) {
2114 wpa_printf(MSG_ERROR,
2115 "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject",
2116 name);
2117 return 0;
2118 }
2119
2120 return match_dn_field(cert, nid, name, value, dn_cnt);
2121}
2122
2123
2124/**
2125 * tls_match_dn_field - Match subject DN field with check_cert_subject
2126 * @cert: Certificate
2127 * @match: check_cert_subject string
2128 * Returns: Return 1 on success and 0 on failure
2129*/
2130static int tls_match_dn_field(X509 *cert, const char *match)
2131{
2132 const char *token, *last = NULL;
2133 char field[256];
2134 struct tls_dn_field_order_cnt dn_cnt;
2135
2136 os_memset(&dn_cnt, 0, sizeof(dn_cnt));
2137
2138 /* Maximum length of each DN field is 255 characters */
2139
2140 /* Process each '/' delimited field */
2141 while ((token = cstr_token(match, "/", &last))) {
2142 if (last - token >= (int) sizeof(field)) {
2143 wpa_printf(MSG_ERROR,
2144 "OpenSSL: Too long DN matching field value in '%s'",
2145 match);
2146 return 0;
2147 }
2148 os_memcpy(field, token, last - token);
2149 field[last - token] = '\0';
2150
2151 if (!get_value_from_field(cert, field, &dn_cnt)) {
2152 wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'",
2153 field);
2154 return 0;
2155 }
2156 }
2157
2158 return 1;
2159}
2160
2161
2162#ifndef CONFIG_NATIVE_WINDOWS
2163static int tls_match_suffix_helper(X509 *cert, const char *match,
2164 size_t match_len, int full)
2165{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002166 GENERAL_NAME *gen;
2167 void *ext;
2168 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002169 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002170 int dns_name = 0;
2171 X509_NAME *name;
2172
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002173 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
2174 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002175
2176 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
2177
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002178 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
2179 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002180 if (gen->type != GEN_DNS)
2181 continue;
2182 dns_name++;
2183 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
2184 gen->d.dNSName->data,
2185 gen->d.dNSName->length);
2186 if (domain_suffix_match(gen->d.dNSName->data,
Hai Shalom021b0b52019-04-10 11:17:58 -07002187 gen->d.dNSName->length,
2188 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002189 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
2190 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002191 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002192 return 1;
2193 }
2194 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002195 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002196
2197 if (dns_name) {
2198 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
2199 return 0;
2200 }
2201
2202 name = X509_get_subject_name(cert);
2203 i = -1;
2204 for (;;) {
2205 X509_NAME_ENTRY *e;
2206 ASN1_STRING *cn;
2207
2208 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
2209 if (i == -1)
2210 break;
2211 e = X509_NAME_get_entry(name, i);
2212 if (e == NULL)
2213 continue;
2214 cn = X509_NAME_ENTRY_get_data(e);
2215 if (cn == NULL)
2216 continue;
2217 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
2218 cn->data, cn->length);
Hai Shalom021b0b52019-04-10 11:17:58 -07002219 if (domain_suffix_match(cn->data, cn->length,
2220 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002221 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
2222 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002223 return 1;
2224 }
2225 }
2226
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002227 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
2228 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002229 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07002230}
2231#endif /* CONFIG_NATIVE_WINDOWS */
2232
2233
2234static int tls_match_suffix(X509 *cert, const char *match, int full)
2235{
2236#ifdef CONFIG_NATIVE_WINDOWS
2237 /* wincrypt.h has conflicting X509_NAME definition */
2238 return -1;
2239#else /* CONFIG_NATIVE_WINDOWS */
2240 const char *token, *last = NULL;
2241
2242 /* Process each match alternative separately until a match is found */
2243 while ((token = cstr_token(match, ";", &last))) {
2244 if (tls_match_suffix_helper(cert, token, last - token, full))
2245 return 1;
2246 }
2247
2248 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08002249#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002250}
2251
2252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253static enum tls_fail_reason openssl_tls_fail_reason(int err)
2254{
2255 switch (err) {
2256 case X509_V_ERR_CERT_REVOKED:
2257 return TLS_FAIL_REVOKED;
2258 case X509_V_ERR_CERT_NOT_YET_VALID:
2259 case X509_V_ERR_CRL_NOT_YET_VALID:
2260 return TLS_FAIL_NOT_YET_VALID;
2261 case X509_V_ERR_CERT_HAS_EXPIRED:
2262 case X509_V_ERR_CRL_HAS_EXPIRED:
2263 return TLS_FAIL_EXPIRED;
2264 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
2265 case X509_V_ERR_UNABLE_TO_GET_CRL:
2266 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
2267 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2268 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2269 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2270 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2271 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
2272 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
2273 case X509_V_ERR_INVALID_CA:
2274 return TLS_FAIL_UNTRUSTED;
2275 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
2276 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
2277 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
2278 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
2279 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
2280 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
2281 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
2282 case X509_V_ERR_CERT_UNTRUSTED:
2283 case X509_V_ERR_CERT_REJECTED:
2284 return TLS_FAIL_BAD_CERTIFICATE;
2285 default:
2286 return TLS_FAIL_UNSPECIFIED;
2287 }
2288}
2289
2290
2291static struct wpabuf * get_x509_cert(X509 *cert)
2292{
2293 struct wpabuf *buf;
2294 u8 *tmp;
2295
2296 int cert_len = i2d_X509(cert, NULL);
2297 if (cert_len <= 0)
2298 return NULL;
2299
2300 buf = wpabuf_alloc(cert_len);
2301 if (buf == NULL)
2302 return NULL;
2303
2304 tmp = wpabuf_put(buf, cert_len);
2305 i2d_X509(cert, &tmp);
2306 return buf;
2307}
2308
2309
2310static void openssl_tls_fail_event(struct tls_connection *conn,
2311 X509 *err_cert, int err, int depth,
2312 const char *subject, const char *err_str,
2313 enum tls_fail_reason reason)
2314{
2315 union tls_event_data ev;
2316 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002317 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318
Pavel Grafov4d8552e2018-02-06 11:28:29 +00002319#ifdef ANDROID
2320 log_cert_validation_failure(err_str);
2321#endif
2322
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002323 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 return;
2325
2326 cert = get_x509_cert(err_cert);
2327 os_memset(&ev, 0, sizeof(ev));
2328 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
2329 reason : openssl_tls_fail_reason(err);
2330 ev.cert_fail.depth = depth;
2331 ev.cert_fail.subject = subject;
2332 ev.cert_fail.reason_txt = err_str;
2333 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002334 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 wpabuf_free(cert);
2336}
2337
2338
Hai Shalom81f62d82019-07-22 12:10:00 -07002339static int openssl_cert_tod(X509 *cert)
2340{
2341 CERTIFICATEPOLICIES *ext;
2342 stack_index_t i;
2343 char buf[100];
2344 int res;
2345 int tod = 0;
2346
2347 ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL);
2348 if (!ext)
2349 return 0;
2350
2351 for (i = 0; i < sk_POLICYINFO_num(ext); i++) {
2352 POLICYINFO *policy;
2353
2354 policy = sk_POLICYINFO_value(ext, i);
2355 res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0);
2356 if (res < 0 || (size_t) res >= sizeof(buf))
2357 continue;
2358 wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf);
2359 if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0)
Hai Shalomc3565922019-10-28 11:58:20 -07002360 tod = 1; /* TOD-STRICT */
2361 else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod)
2362 tod = 2; /* TOD-TOFU */
Hai Shalom81f62d82019-07-22 12:10:00 -07002363 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002364 sk_POLICYINFO_pop_free(ext, POLICYINFO_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002365
2366 return tod;
2367}
2368
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370static void openssl_tls_cert_event(struct tls_connection *conn,
2371 X509 *err_cert, int depth,
2372 const char *subject)
2373{
2374 struct wpabuf *cert = NULL;
2375 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002376 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002377 char *altsubject[TLS_MAX_ALT_SUBJECT];
2378 int alt, num_altsubject = 0;
2379 GENERAL_NAME *gen;
2380 void *ext;
2381 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002382 ASN1_INTEGER *ser;
2383 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384#ifdef CONFIG_SHA256
2385 u8 hash[32];
2386#endif /* CONFIG_SHA256 */
2387
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002388 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 return;
2390
2391 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002392 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
2393 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 cert = get_x509_cert(err_cert);
2395 ev.peer_cert.cert = cert;
2396 }
2397#ifdef CONFIG_SHA256
2398 if (cert) {
2399 const u8 *addr[1];
2400 size_t len[1];
2401 addr[0] = wpabuf_head(cert);
2402 len[0] = wpabuf_len(cert);
2403 if (sha256_vector(1, addr, len, hash) == 0) {
2404 ev.peer_cert.hash = hash;
2405 ev.peer_cert.hash_len = sizeof(hash);
2406 }
2407 }
2408#endif /* CONFIG_SHA256 */
2409 ev.peer_cert.depth = depth;
2410 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002411
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002412 ser = X509_get_serialNumber(err_cert);
2413 if (ser) {
2414 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
2415 ASN1_STRING_get0_data(ser),
2416 ASN1_STRING_length(ser));
2417 ev.peer_cert.serial_num = serial_num;
2418 }
2419
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002420 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
2421 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
2422 char *pos;
2423
2424 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
2425 break;
2426 gen = sk_GENERAL_NAME_value(ext, i);
2427 if (gen->type != GEN_EMAIL &&
2428 gen->type != GEN_DNS &&
2429 gen->type != GEN_URI)
2430 continue;
2431
2432 pos = os_malloc(10 + gen->d.ia5->length + 1);
2433 if (pos == NULL)
2434 break;
2435 altsubject[num_altsubject++] = pos;
2436
2437 switch (gen->type) {
2438 case GEN_EMAIL:
2439 os_memcpy(pos, "EMAIL:", 6);
2440 pos += 6;
2441 break;
2442 case GEN_DNS:
2443 os_memcpy(pos, "DNS:", 4);
2444 pos += 4;
2445 break;
2446 case GEN_URI:
2447 os_memcpy(pos, "URI:", 4);
2448 pos += 4;
2449 break;
2450 }
2451
2452 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2453 pos += gen->d.ia5->length;
2454 *pos = '\0';
2455 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002456 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002457
2458 for (alt = 0; alt < num_altsubject; alt++)
2459 ev.peer_cert.altsubject[alt] = altsubject[alt];
2460 ev.peer_cert.num_altsubject = num_altsubject;
2461
Hai Shalom81f62d82019-07-22 12:10:00 -07002462 ev.peer_cert.tod = openssl_cert_tod(err_cert);
2463
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002464 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002466 for (alt = 0; alt < num_altsubject; alt++)
2467 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468}
2469
2470
Hai Shalomc3565922019-10-28 11:58:20 -07002471static void debug_print_cert(X509 *cert, const char *title)
2472{
2473#ifndef CONFIG_NO_STDOUT_DEBUG
2474 BIO *out;
2475 size_t rlen;
2476 char *txt;
2477 int res;
2478
2479 if (wpa_debug_level > MSG_DEBUG)
2480 return;
2481
2482 out = BIO_new(BIO_s_mem());
2483 if (!out)
2484 return;
2485
2486 X509_print(out, cert);
2487 rlen = BIO_ctrl_pending(out);
2488 txt = os_malloc(rlen + 1);
2489 if (txt) {
2490 res = BIO_read(out, txt, rlen);
2491 if (res > 0) {
2492 txt[res] = '\0';
2493 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
2494 }
2495 os_free(txt);
2496 }
2497
2498 BIO_free(out);
2499#endif /* CONFIG_NO_STDOUT_DEBUG */
2500}
2501
2502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2504{
2505 char buf[256];
2506 X509 *err_cert;
2507 int err, depth;
2508 SSL *ssl;
2509 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002510 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002511 char *match, *altmatch, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -07002512 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 const char *err_str;
2514
2515 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002516 if (!err_cert)
2517 return 0;
2518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519 err = X509_STORE_CTX_get_error(x509_ctx);
2520 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2521 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2522 SSL_get_ex_data_X509_STORE_CTX_idx());
Hai Shalomc3565922019-10-28 11:58:20 -07002523 os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth);
2524 debug_print_cert(err_cert, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2526
2527 conn = SSL_get_app_data(ssl);
2528 if (conn == NULL)
2529 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002530
2531 if (depth == 0)
2532 conn->peer_cert = err_cert;
2533 else if (depth == 1)
2534 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002535 else if (depth == 2)
2536 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002537
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002538 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 match = conn->subject_match;
2540 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002541 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002542 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543
2544 if (!preverify_ok && !conn->ca_cert_verify)
2545 preverify_ok = 1;
2546 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2547 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002548 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2549 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2550 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2551 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2552 "time mismatch");
2553 preverify_ok = 1;
2554 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002555 if (!preverify_ok && !conn->data->check_crl_strict &&
2556 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2557 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2558 wpa_printf(MSG_DEBUG,
2559 "OpenSSL: Ignore certificate validity CRL time mismatch");
2560 preverify_ok = 1;
2561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562
2563 err_str = X509_verify_cert_error_string(err);
2564
2565#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002566 /*
2567 * Do not require preverify_ok so we can explicity allow otherwise
2568 * invalid pinned server certificates.
2569 */
2570 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 struct wpabuf *cert;
2572 cert = get_x509_cert(err_cert);
2573 if (!cert) {
2574 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2575 "server certificate data");
2576 preverify_ok = 0;
2577 } else {
2578 u8 hash[32];
2579 const u8 *addr[1];
2580 size_t len[1];
2581 addr[0] = wpabuf_head(cert);
2582 len[0] = wpabuf_len(cert);
2583 if (sha256_vector(1, addr, len, hash) < 0 ||
2584 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2585 err_str = "Server certificate mismatch";
2586 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2587 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002588 } else if (!preverify_ok) {
2589 /*
2590 * Certificate matches pinned certificate, allow
2591 * regardless of other problems.
2592 */
2593 wpa_printf(MSG_DEBUG,
2594 "OpenSSL: Ignore validation issues for a pinned server certificate");
2595 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 }
2597 wpabuf_free(cert);
2598 }
2599 }
2600#endif /* CONFIG_SHA256 */
2601
Hai Shalom81f62d82019-07-22 12:10:00 -07002602 openssl_tls_cert_event(conn, err_cert, depth, buf);
2603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 if (!preverify_ok) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002605 if (depth > 0) {
2606 /* Send cert event for the peer certificate so that
2607 * the upper layers get information about it even if
2608 * validation of a CA certificate fails. */
2609 STACK_OF(X509) *chain;
2610
2611 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2612 if (chain && sk_X509_num(chain) > 0) {
2613 char buf2[256];
2614 X509 *cert;
2615
2616 cert = sk_X509_value(chain, 0);
2617 X509_NAME_oneline(X509_get_subject_name(cert),
2618 buf2, sizeof(buf2));
2619
2620 openssl_tls_cert_event(conn, cert, 0, buf2);
2621 }
2622 if (chain)
2623 sk_X509_pop_free(chain, X509_free);
2624 }
2625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2627 " error %d (%s) depth %d for '%s'", err, err_str,
2628 depth, buf);
2629 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2630 err_str, TLS_FAIL_UNSPECIFIED);
2631 return preverify_ok;
2632 }
2633
2634 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2635 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2636 preverify_ok, err, err_str,
2637 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002638 check_cert_subject = conn->check_cert_subject;
2639 if (!check_cert_subject)
2640 check_cert_subject = conn->data->check_cert_subject;
2641 if (check_cert_subject) {
2642 if (depth == 0 &&
2643 !tls_match_dn_field(err_cert, check_cert_subject)) {
2644 preverify_ok = 0;
2645 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2646 "Distinguished Name",
2647 TLS_FAIL_DN_MISMATCH);
2648 }
2649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2651 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2652 "match with '%s'", buf, match);
2653 preverify_ok = 0;
2654 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2655 "Subject mismatch",
2656 TLS_FAIL_SUBJECT_MISMATCH);
2657 } else if (depth == 0 && altmatch &&
2658 !tls_match_altsubject(err_cert, altmatch)) {
2659 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2660 "'%s' not found", altmatch);
2661 preverify_ok = 0;
2662 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2663 "AltSubject mismatch",
2664 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002665 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002666 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002667 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2668 suffix_match);
2669 preverify_ok = 0;
2670 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2671 "Domain suffix mismatch",
2672 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002673 } else if (depth == 0 && domain_match &&
2674 !tls_match_suffix(err_cert, domain_match, 1)) {
2675 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2676 domain_match);
2677 preverify_ok = 0;
2678 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2679 "Domain mismatch",
2680 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002681 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682
2683 if (conn->cert_probe && preverify_ok && depth == 0) {
2684 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2685 "on probe-only run");
2686 preverify_ok = 0;
2687 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2688 "Server certificate chain probe",
2689 TLS_FAIL_SERVER_CHAIN_PROBE);
2690 }
2691
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002692#ifdef CONFIG_SUITEB
2693 if (conn->flags & TLS_CONN_SUITEB) {
2694 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002695 int len = -1;
2696
2697 pk = X509_get_pubkey(err_cert);
2698 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002699 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002700 EVP_PKEY_free(pk);
2701 }
2702
2703 if (len >= 0) {
2704 wpa_printf(MSG_DEBUG,
2705 "OpenSSL: RSA modulus size: %d bits", len);
2706 if (len < 3072) {
2707 preverify_ok = 0;
2708 openssl_tls_fail_event(
2709 conn, err_cert, err,
2710 depth, buf,
2711 "Insufficient RSA modulus size",
2712 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2713 }
2714 }
2715 }
2716#endif /* CONFIG_SUITEB */
2717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002718#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002719 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2720 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002721 enum ocsp_result res;
2722
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002723 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2724 conn->peer_issuer,
2725 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002726 if (res == OCSP_REVOKED) {
2727 preverify_ok = 0;
2728 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2729 "certificate revoked",
2730 TLS_FAIL_REVOKED);
2731 if (err == X509_V_OK)
2732 X509_STORE_CTX_set_error(
2733 x509_ctx, X509_V_ERR_CERT_REVOKED);
2734 } else if (res != OCSP_GOOD &&
2735 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2736 preverify_ok = 0;
2737 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2738 "bad certificate status response",
2739 TLS_FAIL_UNSPECIFIED);
2740 }
2741 }
2742#endif /* OPENSSL_IS_BORINGSSL */
2743
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002744 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002745 context->event_cb(context->cb_ctx,
2746 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002747
Hai Shalom899fcc72020-10-19 14:38:18 -07002748 if (depth == 0 && preverify_ok) {
2749 os_free(conn->peer_subject);
2750 conn->peer_subject = os_strdup(buf);
2751 }
2752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753 return preverify_ok;
2754}
2755
2756
2757#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002758static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002760 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 X509_LOOKUP *lookup;
2762 int ret = 0;
2763
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002764 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 X509_LOOKUP_file());
2766 if (lookup == NULL) {
2767 tls_show_errors(MSG_WARNING, __func__,
2768 "Failed add lookup for X509 store");
2769 return -1;
2770 }
2771
2772 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2773 unsigned long err = ERR_peek_error();
2774 tls_show_errors(MSG_WARNING, __func__,
2775 "Failed load CA in DER format");
2776 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2777 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2778 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2779 "cert already in hash table error",
2780 __func__);
2781 } else
2782 ret = -1;
2783 }
2784
2785 return ret;
2786}
2787#endif /* OPENSSL_NO_STDIO */
2788
2789
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002790static int tls_connection_ca_cert(struct tls_data *data,
2791 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792 const char *ca_cert, const u8 *ca_cert_blob,
2793 size_t ca_cert_blob_len, const char *ca_path)
2794{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002795 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002796 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797
2798 /*
2799 * Remove previously configured trusted CA certificates before adding
2800 * new ones.
2801 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002802 store = X509_STORE_new();
2803 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2805 "certificate store", __func__);
2806 return -1;
2807 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002808 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809
2810 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2811 conn->ca_cert_verify = 1;
2812
2813 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2814 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2815 "chain");
2816 conn->cert_probe = 1;
2817 conn->ca_cert_verify = 0;
2818 return 0;
2819 }
2820
2821 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2822#ifdef CONFIG_SHA256
2823 const char *pos = ca_cert + 7;
2824 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2825 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2826 "hash value '%s'", ca_cert);
2827 return -1;
2828 }
2829 pos += 14;
2830 if (os_strlen(pos) != 32 * 2) {
2831 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2832 "hash length in ca_cert '%s'", ca_cert);
2833 return -1;
2834 }
2835 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2836 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2837 "value in ca_cert '%s'", ca_cert);
2838 return -1;
2839 }
2840 conn->server_cert_only = 1;
2841 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2842 "certificate match");
2843 return 0;
2844#else /* CONFIG_SHA256 */
2845 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2846 "cannot validate server certificate hash");
2847 return -1;
2848#endif /* CONFIG_SHA256 */
2849 }
2850
2851 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002852 X509 *cert = d2i_X509(NULL,
2853 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 ca_cert_blob_len);
2855 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002856 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2857 ca_cert_blob_len);
2858
2859 if (bio) {
2860 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2861 BIO_free(bio);
2862 }
2863
2864 if (!cert) {
2865 tls_show_errors(MSG_WARNING, __func__,
2866 "Failed to parse ca_cert_blob");
2867 return -1;
2868 }
2869
2870 while (ERR_get_error()) {
2871 /* Ignore errors from DER conversion. */
2872 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 }
2874
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002875 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2876 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 unsigned long err = ERR_peek_error();
2878 tls_show_errors(MSG_WARNING, __func__,
2879 "Failed to add ca_cert_blob to "
2880 "certificate store");
2881 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2882 ERR_GET_REASON(err) ==
2883 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2884 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2885 "cert already in hash table error",
2886 __func__);
2887 } else {
2888 X509_free(cert);
2889 return -1;
2890 }
2891 }
2892 X509_free(cert);
2893 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2894 "to certificate store", __func__);
2895 return 0;
2896 }
2897
2898#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002899 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002900 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2901 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002902 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Hai Shalom7ad2a872021-08-02 18:56:55 -07002903 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002905 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2906 return 0;
2907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002909 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002910 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2911 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2912 char *aliases = os_strdup(
2913 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002914 const char *delim = " ";
2915 int rc = 0;
2916 char *savedptr;
2917 char *alias;
2918
2919 if (!aliases)
2920 return -1;
2921 alias = strtok_r(aliases, delim, &savedptr);
2922 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2923 if (tls_add_ca_from_keystore_encoded(
Hai Shalom7ad2a872021-08-02 18:56:55 -07002924 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
2925 wpa_printf(MSG_ERROR,
2926 "OpenSSL: Failed to add ca_cert %s from keystore",
2927 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002928 rc = -1;
2929 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 }
2931 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002932 os_free(aliases);
2933 if (rc)
2934 return rc;
2935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2937 return 0;
2938 }
2939#endif /* ANDROID */
2940
2941#ifdef CONFIG_NATIVE_WINDOWS
2942 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2943 0) {
2944 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2945 "system certificate store");
2946 return 0;
2947 }
2948#endif /* CONFIG_NATIVE_WINDOWS */
2949
2950 if (ca_cert || ca_path) {
2951#ifndef OPENSSL_NO_STDIO
2952 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2953 1) {
2954 tls_show_errors(MSG_WARNING, __func__,
2955 "Failed to load root certificates");
2956 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002957 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2959 "DER format CA certificate",
2960 __func__);
2961 } else
2962 return -1;
2963 } else {
2964 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2965 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002966 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 }
2968#else /* OPENSSL_NO_STDIO */
2969 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2970 __func__);
2971 return -1;
2972#endif /* OPENSSL_NO_STDIO */
2973 } else {
2974 /* No ca_cert configured - do not try to verify server
2975 * certificate */
2976 conn->ca_cert_verify = 0;
2977 }
2978
2979 return 0;
2980}
2981
2982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002983static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002985 SSL_CTX *ssl_ctx = data->ssl;
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 if (ca_cert) {
2988 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2989 {
2990 tls_show_errors(MSG_WARNING, __func__,
2991 "Failed to load root certificates");
2992 return -1;
2993 }
2994
2995 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2996 "certificate(s) loaded");
2997
2998#ifndef OPENSSL_NO_STDIO
2999 /* Add the same CAs to the client certificate requests */
3000 SSL_CTX_set_client_CA_list(ssl_ctx,
3001 SSL_load_client_CA_file(ca_cert));
3002#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003003
3004 os_free(data->ca_cert);
3005 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 }
3007
3008 return 0;
3009}
3010
3011
Hai Shalom74f70d42019-02-11 14:42:39 -08003012int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013{
3014 int flags;
3015
3016 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003017 struct tls_data *data = ssl_ctx;
3018 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 if (cs == NULL) {
3020 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3021 "certificate store when enabling "
3022 "check_crl");
3023 return -1;
3024 }
3025 flags = X509_V_FLAG_CRL_CHECK;
3026 if (check_crl == 2)
3027 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3028 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003029
3030 data->check_crl = check_crl;
3031 data->check_crl_strict = strict;
3032 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 }
3034 return 0;
3035}
3036
3037
3038static int tls_connection_set_subject_match(struct tls_connection *conn,
3039 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003040 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003041 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003042 const char *domain_match,
3043 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044{
3045 os_free(conn->subject_match);
3046 conn->subject_match = NULL;
3047 if (subject_match) {
3048 conn->subject_match = os_strdup(subject_match);
3049 if (conn->subject_match == NULL)
3050 return -1;
3051 }
3052
3053 os_free(conn->altsubject_match);
3054 conn->altsubject_match = NULL;
3055 if (altsubject_match) {
3056 conn->altsubject_match = os_strdup(altsubject_match);
3057 if (conn->altsubject_match == NULL)
3058 return -1;
3059 }
3060
Dmitry Shmidt051af732013-10-22 13:52:46 -07003061 os_free(conn->suffix_match);
3062 conn->suffix_match = NULL;
3063 if (suffix_match) {
3064 conn->suffix_match = os_strdup(suffix_match);
3065 if (conn->suffix_match == NULL)
3066 return -1;
3067 }
3068
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003069 os_free(conn->domain_match);
3070 conn->domain_match = NULL;
3071 if (domain_match) {
3072 conn->domain_match = os_strdup(domain_match);
3073 if (conn->domain_match == NULL)
3074 return -1;
3075 }
3076
Hai Shalom021b0b52019-04-10 11:17:58 -07003077 os_free(conn->check_cert_subject);
3078 conn->check_cert_subject = NULL;
3079 if (check_cert_subject) {
3080 conn->check_cert_subject = os_strdup(check_cert_subject);
3081 if (!conn->check_cert_subject)
3082 return -1;
3083 }
3084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 return 0;
3086}
3087
3088
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003089#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003090static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003091{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003092 struct tls_connection *conn = arg;
3093
3094 /*
3095 * This cert_cb() is not really the best location for doing a
3096 * constraint check for the ServerKeyExchange message, but this seems to
3097 * be the only place where the current OpenSSL sequence can be
3098 * terminated cleanly with an TLS alert going out to the server.
3099 */
3100
3101 if (!(conn->flags & TLS_CONN_SUITEB))
3102 return 1;
3103
3104 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3105 if (conn->cipher_suite != 0x9f)
3106 return 1;
3107
3108 if (conn->server_dh_prime_len >= 3072)
3109 return 1;
3110
3111 wpa_printf(MSG_DEBUG,
3112 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3113 conn->server_dh_prime_len);
3114 return 0;
3115}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003116#endif /* CONFIG_SUITEB */
3117
3118
Roshan Pius3a1667e2018-07-03 15:17:14 -07003119static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3120 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003121{
3122 SSL *ssl = conn->ssl;
3123
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003124#ifdef SSL_OP_NO_TICKET
3125 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3126 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003127 else
3128 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003129#endif /* SSL_OP_NO_TICKET */
3130
Sunil Ravia04bd252022-05-02 22:54:18 -07003131#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3132 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3133 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3134#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003136#ifdef SSL_OP_NO_TLSv1
3137 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3138 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3139 else
3140 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3141#endif /* SSL_OP_NO_TLSv1 */
3142#ifdef SSL_OP_NO_TLSv1_1
3143 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3144 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3145 else
3146 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3147#endif /* SSL_OP_NO_TLSv1_1 */
3148#ifdef SSL_OP_NO_TLSv1_2
3149 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3150 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3151 else
3152 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3153#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003154#ifdef SSL_OP_NO_TLSv1_3
3155 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3156 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3157 else
3158 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3159#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003160#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3161 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3162 TLS_CONN_ENABLE_TLSv1_1 |
3163 TLS_CONN_ENABLE_TLSv1_2)) {
3164 int version = 0;
3165
3166 /* Explicit request to enable TLS versions even if needing to
3167 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003168 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003169 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003170 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3171 version = TLS1_1_VERSION;
3172 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3173 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003174 if (!version) {
3175 wpa_printf(MSG_DEBUG,
3176 "OpenSSL: Invalid TLS version configuration");
3177 return -1;
3178 }
3179
3180 if (SSL_set_min_proto_version(ssl, version) != 1) {
3181 wpa_printf(MSG_DEBUG,
3182 "OpenSSL: Failed to set minimum TLS version");
3183 return -1;
3184 }
3185 }
3186#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003187#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3188 !defined(LIBRESSL_VERSION_NUMBER) && \
3189 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003190 {
3191#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3192 int need_level = 0;
3193#else
3194 int need_level = 1;
3195#endif
3196
3197 if ((flags &
3198 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3199 SSL_get_security_level(ssl) > need_level) {
3200 /*
3201 * Need to drop to security level 1 (or 0 with OpenSSL
3202 * 3.0) to allow TLS versions older than 1.2 to be used
3203 * when explicitly enabled in configuration.
3204 */
3205 SSL_set_security_level(conn->ssl, need_level);
3206 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003207 }
3208#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003209
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003210#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003211#ifdef OPENSSL_IS_BORINGSSL
3212 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003213 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003214#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003215 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3216 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3217
Roshan Pius3a1667e2018-07-03 15:17:14 -07003218 if (openssl_ciphers) {
3219 wpa_printf(MSG_DEBUG,
3220 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3221 openssl_ciphers);
3222 ciphers = openssl_ciphers;
3223 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003224 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3225 wpa_printf(MSG_INFO,
3226 "OpenSSL: Failed to set Suite B ciphers");
3227 return -1;
3228 }
3229 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003230#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003231 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003232#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003233 const char *ciphers =
3234 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003235 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003236
Roshan Pius3a1667e2018-07-03 15:17:14 -07003237 if (openssl_ciphers) {
3238 wpa_printf(MSG_DEBUG,
3239 "OpenSSL: Override ciphers for Suite B: %s",
3240 openssl_ciphers);
3241 ciphers = openssl_ciphers;
3242 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003243 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3244 wpa_printf(MSG_INFO,
3245 "OpenSSL: Failed to set Suite B ciphers");
3246 return -1;
3247 }
3248
Sunil Ravia04bd252022-05-02 22:54:18 -07003249#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3250 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3251 wpa_printf(MSG_INFO,
3252 "OpenSSL: Failed to set Suite B groups");
3253 return -1;
3254 }
3255
3256#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003257 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003258 wpa_printf(MSG_INFO,
3259 "OpenSSL: Failed to set Suite B curves");
3260 return -1;
3261 }
3262
3263 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3264 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3265 EC_KEY_free(ecdh);
3266 wpa_printf(MSG_INFO,
3267 "OpenSSL: Failed to set ECDH parameter");
3268 return -1;
3269 }
3270 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003271#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003272 }
3273 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003274#ifdef OPENSSL_IS_BORINGSSL
3275 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3276
Jimmy Chen916e0a72022-01-11 15:19:46 +08003277 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003278 1) != 1) {
3279 wpa_printf(MSG_INFO,
3280 "OpenSSL: Failed to set Suite B sigalgs");
3281 return -1;
3282 }
3283#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003284 /* ECDSA+SHA384 if need to add EC support here */
3285 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3286 wpa_printf(MSG_INFO,
3287 "OpenSSL: Failed to set Suite B sigalgs");
3288 return -1;
3289 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003290#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003291
3292 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3293 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3294 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3295 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003296
3297#ifdef OPENSSL_IS_BORINGSSL
3298 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3299 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3300 int nid[1] = { NID_secp384r1 };
3301
3302 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3303 wpa_printf(MSG_INFO,
3304 "OpenSSL: Failed to set Suite B curves");
3305 return -1;
3306 }
3307
Jimmy Chen916e0a72022-01-11 15:19:46 +08003308 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003309 1) != 1) {
3310 wpa_printf(MSG_INFO,
3311 "OpenSSL: Failed to set Suite B sigalgs");
3312 return -1;
3313 }
3314 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003315#else /* OPENSSL_IS_BORINGSSL */
3316 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3317 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3318 wpa_printf(MSG_INFO,
3319 "OpenSSL: Failed to set openssl_ciphers '%s'",
3320 openssl_ciphers);
3321 return -1;
3322 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003323#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003324#else /* CONFIG_SUITEB */
3325 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3326 wpa_printf(MSG_INFO,
3327 "OpenSSL: Failed to set openssl_ciphers '%s'",
3328 openssl_ciphers);
3329 return -1;
3330 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003331#endif /* CONFIG_SUITEB */
3332
Hai Shalom81f62d82019-07-22 12:10:00 -07003333 if (flags & TLS_CONN_TEAP_ANON_DH) {
3334#ifndef TEAP_DH_ANON_CS
3335#define TEAP_DH_ANON_CS \
3336 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3337 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3338 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3339 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3340 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3341 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3342 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3343 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3344#endif
3345 static const char *cs = TEAP_DH_ANON_CS;
3346
3347#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3348 !defined(LIBRESSL_VERSION_NUMBER) && \
3349 !defined(OPENSSL_IS_BORINGSSL)
3350 /*
3351 * Need to drop to security level 0 to allow anonymous
3352 * cipher suites for EAP-TEAP.
3353 */
3354 SSL_set_security_level(conn->ssl, 0);
3355#endif
3356
3357 wpa_printf(MSG_DEBUG,
3358 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3359 cs);
3360 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3361 tls_show_errors(MSG_INFO, __func__,
3362 "Cipher suite configuration failed");
3363 return -1;
3364 }
3365 }
3366
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003367 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003368}
3369
3370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003372 int verify_peer, unsigned int flags,
3373 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374{
3375 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003376 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377
3378 if (conn == NULL)
3379 return -1;
3380
Hai Shalom899fcc72020-10-19 14:38:18 -07003381 if (verify_peer == 2) {
3382 conn->ca_cert_verify = 1;
3383 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3384 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3385 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 conn->ca_cert_verify = 1;
3387 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3388 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3389 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3390 } else {
3391 conn->ca_cert_verify = 0;
3392 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3393 }
3394
Roshan Pius3a1667e2018-07-03 15:17:14 -07003395 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003396 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003397 conn->flags = flags;
3398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 SSL_set_accept_state(conn->ssl);
3400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003401 if (data->tls_session_lifetime == 0) {
3402 /*
3403 * Set session id context to a unique value to make sure
3404 * session resumption cannot be used either through session
3405 * caching or TLS ticket extension.
3406 */
3407 counter++;
3408 SSL_set_session_id_context(conn->ssl,
3409 (const unsigned char *) &counter,
3410 sizeof(counter));
3411 } else if (session_ctx) {
3412 SSL_set_session_id_context(conn->ssl, session_ctx,
3413 session_ctx_len);
3414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415
3416 return 0;
3417}
3418
3419
3420static int tls_connection_client_cert(struct tls_connection *conn,
3421 const char *client_cert,
3422 const u8 *client_cert_blob,
3423 size_t client_cert_blob_len)
3424{
3425 if (client_cert == NULL && client_cert_blob == NULL)
3426 return 0;
3427
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003428#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003429#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003430 /*
3431 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003432 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003433 * chain properly.
3434 */
3435 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003436#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003437#endif /* PKCS12_FUNCS */
3438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 if (client_cert_blob &&
3440 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3441 client_cert_blob_len) == 1) {
3442 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3443 "OK");
3444 return 0;
3445 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003446#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447 tls_show_errors(MSG_DEBUG, __func__,
3448 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003449#else
3450 BIO *bio;
3451 X509 *x509;
3452
3453 tls_show_errors(MSG_DEBUG, __func__,
3454 "SSL_use_certificate_ASN1 failed");
3455 bio = BIO_new(BIO_s_mem());
3456 if (!bio)
3457 return -1;
3458 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3459 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3460 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3461 X509_free(x509);
3462 BIO_free(bio);
3463 return -1;
3464 }
3465 X509_free(x509);
3466 wpa_printf(MSG_DEBUG,
3467 "OpenSSL: Found PEM encoded certificate from blob");
3468 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3469 wpa_printf(MSG_DEBUG,
3470 "OpenSSL: Added an additional certificate into the chain");
3471 SSL_add0_chain_cert(conn->ssl, x509);
3472 }
3473 BIO_free(bio);
3474 return 0;
3475#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 }
3477
3478 if (client_cert == NULL)
3479 return -1;
3480
3481#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003482 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3483 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
3484 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003486 if (!bio) {
3487 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003488 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003489 // Keystore returns X.509 certificates in PEM encoding
3490 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3491 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003493 BIO_free(bio);
3494 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003497 X509_free(x509);
3498 wpa_printf(MSG_DEBUG,
3499 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3500 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003501
Hai Shalom7ad2a872021-08-02 18:56:55 -07003502 // Read additional certificates into the chain
3503 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3504 wpa_printf(MSG_DEBUG,
3505 "OpenSSL: Added an additional certificate into the chain");
3506 // Takes ownership of x509, no need to free it here
3507 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003508 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003509 BIO_free(bio);
3510 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511 }
3512#endif /* ANDROID */
3513
3514#ifndef OPENSSL_NO_STDIO
3515 if (SSL_use_certificate_file(conn->ssl, client_cert,
3516 SSL_FILETYPE_ASN1) == 1) {
3517 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3518 " --> OK");
3519 return 0;
3520 }
3521
Hai Shalom021b0b52019-04-10 11:17:58 -07003522#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3523 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003524 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3525 ERR_clear_error();
3526 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3527 " --> OK");
3528 return 0;
3529 }
3530#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 if (SSL_use_certificate_file(conn->ssl, client_cert,
3532 SSL_FILETYPE_PEM) == 1) {
3533 ERR_clear_error();
3534 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3535 " --> OK");
3536 return 0;
3537 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003538#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539
3540 tls_show_errors(MSG_DEBUG, __func__,
3541 "SSL_use_certificate_file failed");
3542#else /* OPENSSL_NO_STDIO */
3543 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3544#endif /* OPENSSL_NO_STDIO */
3545
3546 return -1;
3547}
3548
3549
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003550static int tls_global_client_cert(struct tls_data *data,
3551 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552{
3553#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003554 SSL_CTX *ssl_ctx = data->ssl;
3555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556 if (client_cert == NULL)
3557 return 0;
3558
3559 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3560 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003561 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3563 SSL_FILETYPE_PEM) != 1) {
3564 tls_show_errors(MSG_INFO, __func__,
3565 "Failed to load client certificate");
3566 return -1;
3567 }
3568 return 0;
3569#else /* OPENSSL_NO_STDIO */
3570 if (client_cert == NULL)
3571 return 0;
3572 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3573 return -1;
3574#endif /* OPENSSL_NO_STDIO */
3575}
3576
3577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003579static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 const char *passwd)
3581{
3582 EVP_PKEY *pkey;
3583 X509 *cert;
3584 STACK_OF(X509) *certs;
3585 int res = 0;
3586 char buf[256];
3587
3588 pkey = NULL;
3589 cert = NULL;
3590 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003591 if (!passwd)
3592 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3594 tls_show_errors(MSG_DEBUG, __func__,
3595 "Failed to parse PKCS12 file");
3596 PKCS12_free(p12);
3597 return -1;
3598 }
3599 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3600
3601 if (cert) {
3602 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3603 sizeof(buf));
3604 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3605 "subject='%s'", buf);
3606 if (ssl) {
3607 if (SSL_use_certificate(ssl, cert) != 1)
3608 res = -1;
3609 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003610 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 res = -1;
3612 }
3613 X509_free(cert);
3614 }
3615
3616 if (pkey) {
3617 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3618 if (ssl) {
3619 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3620 res = -1;
3621 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003622 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 res = -1;
3624 }
3625 EVP_PKEY_free(pkey);
3626 }
3627
3628 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003629#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003630 if (ssl)
3631 SSL_clear_chain_certs(ssl);
3632 else
3633 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003634 while ((cert = sk_X509_pop(certs)) != NULL) {
3635 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3636 sizeof(buf));
3637 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3638 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003639 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3640 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3641 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003642 tls_show_errors(MSG_DEBUG, __func__,
3643 "Failed to add additional certificate");
3644 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003645 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003646 break;
3647 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003648 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003649 }
3650 if (!res) {
3651 /* Try to continue anyway */
3652 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003653 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003654#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003655 if (ssl)
3656 res = SSL_build_cert_chain(
3657 ssl,
3658 SSL_BUILD_CHAIN_FLAG_CHECK |
3659 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3660 else
3661 res = SSL_CTX_build_cert_chain(
3662 data->ssl,
3663 SSL_BUILD_CHAIN_FLAG_CHECK |
3664 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003665 if (!res) {
3666 tls_show_errors(MSG_DEBUG, __func__,
3667 "Failed to build certificate chain");
3668 } else if (res == 2) {
3669 wpa_printf(MSG_DEBUG,
3670 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3671 }
3672#endif /* OPENSSL_IS_BORINGSSL */
3673 /*
3674 * Try to continue regardless of result since it is possible for
3675 * the extra certificates not to be required.
3676 */
3677 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003678#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003679 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 while ((cert = sk_X509_pop(certs)) != NULL) {
3681 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3682 sizeof(buf));
3683 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3684 " from PKCS12: subject='%s'", buf);
3685 /*
3686 * There is no SSL equivalent for the chain cert - so
3687 * always add it to the context...
3688 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003689 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3690 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003691 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 res = -1;
3693 break;
3694 }
3695 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003696 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003697#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 }
3699
3700 PKCS12_free(p12);
3701
3702 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003703 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704
3705 return res;
3706}
3707#endif /* PKCS12_FUNCS */
3708
3709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003710static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3711 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712{
3713#ifdef PKCS12_FUNCS
3714 FILE *f;
3715 PKCS12 *p12;
3716
3717 f = fopen(private_key, "rb");
3718 if (f == NULL)
3719 return -1;
3720
3721 p12 = d2i_PKCS12_fp(f, NULL);
3722 fclose(f);
3723
3724 if (p12 == NULL) {
3725 tls_show_errors(MSG_INFO, __func__,
3726 "Failed to use PKCS#12 file");
3727 return -1;
3728 }
3729
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003730 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731
3732#else /* PKCS12_FUNCS */
3733 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3734 "p12/pfx files");
3735 return -1;
3736#endif /* PKCS12_FUNCS */
3737}
3738
3739
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003740static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003741 const u8 *blob, size_t len, const char *passwd)
3742{
3743#ifdef PKCS12_FUNCS
3744 PKCS12 *p12;
3745
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003746 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 if (p12 == NULL) {
3748 tls_show_errors(MSG_INFO, __func__,
3749 "Failed to use PKCS#12 blob");
3750 return -1;
3751 }
3752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003753 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754
3755#else /* PKCS12_FUNCS */
3756 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3757 "p12/pfx blobs");
3758 return -1;
3759#endif /* PKCS12_FUNCS */
3760}
3761
3762
3763#ifndef OPENSSL_NO_ENGINE
3764static int tls_engine_get_cert(struct tls_connection *conn,
3765 const char *cert_id,
3766 X509 **cert)
3767{
3768 /* this runs after the private key is loaded so no PIN is required */
3769 struct {
3770 const char *cert_id;
3771 X509 *cert;
3772 } params;
3773 params.cert_id = cert_id;
3774 params.cert = NULL;
3775
3776 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3777 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003778 unsigned long err = ERR_get_error();
3779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3781 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003782 ERR_error_string(err, NULL));
3783 if (tls_is_pin_error(err))
3784 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3786 }
3787 if (!params.cert) {
3788 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3789 " '%s'", cert_id);
3790 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3791 }
3792 *cert = params.cert;
3793 return 0;
3794}
3795#endif /* OPENSSL_NO_ENGINE */
3796
3797
3798static int tls_connection_engine_client_cert(struct tls_connection *conn,
3799 const char *cert_id)
3800{
3801#ifndef OPENSSL_NO_ENGINE
3802 X509 *cert;
3803
3804 if (tls_engine_get_cert(conn, cert_id, &cert))
3805 return -1;
3806
3807 if (!SSL_use_certificate(conn->ssl, cert)) {
3808 tls_show_errors(MSG_ERROR, __func__,
3809 "SSL_use_certificate failed");
3810 X509_free(cert);
3811 return -1;
3812 }
3813 X509_free(cert);
3814 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3815 "OK");
3816 return 0;
3817
3818#else /* OPENSSL_NO_ENGINE */
3819 return -1;
3820#endif /* OPENSSL_NO_ENGINE */
3821}
3822
3823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003824static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825 struct tls_connection *conn,
3826 const char *ca_cert_id)
3827{
3828#ifndef OPENSSL_NO_ENGINE
3829 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003830 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003831 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832
3833 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3834 return -1;
3835
3836 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003837 store = X509_STORE_new();
3838 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3840 "certificate store", __func__);
3841 X509_free(cert);
3842 return -1;
3843 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003844 SSL_CTX_set_cert_store(ssl_ctx, store);
3845 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 unsigned long err = ERR_peek_error();
3847 tls_show_errors(MSG_WARNING, __func__,
3848 "Failed to add CA certificate from engine "
3849 "to certificate store");
3850 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3851 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3852 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3853 " already in hash table error",
3854 __func__);
3855 } else {
3856 X509_free(cert);
3857 return -1;
3858 }
3859 }
3860 X509_free(cert);
3861 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3862 "to certificate store", __func__);
3863 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003864 conn->ca_cert_verify = 1;
3865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 return 0;
3867
3868#else /* OPENSSL_NO_ENGINE */
3869 return -1;
3870#endif /* OPENSSL_NO_ENGINE */
3871}
3872
3873
3874static int tls_connection_engine_private_key(struct tls_connection *conn)
3875{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003876#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3878 tls_show_errors(MSG_ERROR, __func__,
3879 "ENGINE: cannot use private key for TLS");
3880 return -1;
3881 }
3882 if (!SSL_check_private_key(conn->ssl)) {
3883 tls_show_errors(MSG_INFO, __func__,
3884 "Private key failed verification");
3885 return -1;
3886 }
3887 return 0;
3888#else /* OPENSSL_NO_ENGINE */
3889 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3890 "engine support was not compiled in");
3891 return -1;
3892#endif /* OPENSSL_NO_ENGINE */
3893}
3894
3895
Roshan Pius3a1667e2018-07-03 15:17:14 -07003896#ifndef OPENSSL_NO_STDIO
3897static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003898{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003899 if (!password)
3900 return 0;
3901 os_strlcpy(buf, (const char *) password, size);
3902 return os_strlen(buf);
3903}
3904#endif /* OPENSSL_NO_STDIO */
3905
3906
3907static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3908 const char *private_key,
3909 const char *private_key_passwd)
3910{
3911#ifndef OPENSSL_NO_STDIO
3912 BIO *bio;
3913 EVP_PKEY *pkey;
3914 int ret;
3915
3916 /* First try ASN.1 (DER). */
3917 bio = BIO_new_file(private_key, "r");
3918 if (!bio)
3919 return -1;
3920 pkey = d2i_PrivateKey_bio(bio, NULL);
3921 BIO_free(bio);
3922
3923 if (pkey) {
3924 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3925 } else {
3926 /* Try PEM with the provided password. */
3927 bio = BIO_new_file(private_key, "r");
3928 if (!bio)
3929 return -1;
3930 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3931 (void *) private_key_passwd);
3932 BIO_free(bio);
3933 if (!pkey)
3934 return -1;
3935 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3936 /* Clear errors from the previous failed load. */
3937 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003938 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003939
3940 if (ssl)
3941 ret = SSL_use_PrivateKey(ssl, pkey);
3942 else
3943 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3944
3945 EVP_PKEY_free(pkey);
3946 return ret == 1 ? 0 : -1;
3947#else /* OPENSSL_NO_STDIO */
3948 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3949 return -1;
3950#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003951}
3952
3953
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003954static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 struct tls_connection *conn,
3956 const char *private_key,
3957 const char *private_key_passwd,
3958 const u8 *private_key_blob,
3959 size_t private_key_blob_len)
3960{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003961 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 int ok;
3963
3964 if (private_key == NULL && private_key_blob == NULL)
3965 return 0;
3966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 ok = 0;
3968 while (private_key_blob) {
3969 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3970 (u8 *) private_key_blob,
3971 private_key_blob_len) == 1) {
3972 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3973 "ASN1(EVP_PKEY_RSA) --> OK");
3974 ok = 1;
3975 break;
3976 }
3977
3978 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3979 (u8 *) private_key_blob,
3980 private_key_blob_len) == 1) {
3981 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3982 "ASN1(EVP_PKEY_DSA) --> OK");
3983 ok = 1;
3984 break;
3985 }
3986
Hai Shalom899fcc72020-10-19 14:38:18 -07003987#ifndef OPENSSL_NO_EC
3988 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
3989 (u8 *) private_key_blob,
3990 private_key_blob_len) == 1) {
3991 wpa_printf(MSG_DEBUG,
3992 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
3993 ok = 1;
3994 break;
3995 }
3996#endif /* OPENSSL_NO_EC */
3997
Sunil Ravia04bd252022-05-02 22:54:18 -07003998#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4000 (u8 *) private_key_blob,
4001 private_key_blob_len) == 1) {
4002 wpa_printf(MSG_DEBUG, "OpenSSL: "
4003 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4004 ok = 1;
4005 break;
4006 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004007#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008
Hai Shaloma20dcd72022-02-04 13:43:00 -08004009 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4010 private_key_blob_len);
4011 if (bio) {
4012 EVP_PKEY *pkey;
4013
4014 pkey = PEM_read_bio_PrivateKey(
4015 bio, NULL, tls_passwd_cb,
4016 (void *) private_key_passwd);
4017 if (pkey) {
4018 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4019 wpa_printf(MSG_DEBUG,
4020 "OpenSSL: SSL_use_PrivateKey --> OK");
4021 ok = 1;
4022 EVP_PKEY_free(pkey);
4023 BIO_free(bio);
4024 break;
4025 }
4026 EVP_PKEY_free(pkey);
4027 }
4028 BIO_free(bio);
4029 }
4030
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004031 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004032 private_key_blob_len,
4033 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4035 "OK");
4036 ok = 1;
4037 break;
4038 }
4039
4040 break;
4041 }
4042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004044 if (tls_use_private_key_file(data, conn->ssl, private_key,
4045 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004046 ok = 1;
4047 break;
4048 }
4049
Roshan Pius3a1667e2018-07-03 15:17:14 -07004050 if (tls_read_pkcs12(data, conn->ssl, private_key,
4051 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4053 "--> OK");
4054 ok = 1;
4055 break;
4056 }
4057
4058 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4059 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4060 "access certificate store --> OK");
4061 ok = 1;
4062 break;
4063 }
4064
4065 break;
4066 }
4067
4068 if (!ok) {
4069 tls_show_errors(MSG_INFO, __func__,
4070 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 return -1;
4072 }
4073 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 if (!SSL_check_private_key(conn->ssl)) {
4076 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4077 "verification");
4078 return -1;
4079 }
4080
4081 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4082 return 0;
4083}
4084
4085
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004086static int tls_global_private_key(struct tls_data *data,
4087 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 const char *private_key_passwd)
4089{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004090 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091
4092 if (private_key == NULL)
4093 return 0;
4094
Roshan Pius3a1667e2018-07-03 15:17:14 -07004095 if (tls_use_private_key_file(data, NULL, private_key,
4096 private_key_passwd) &&
4097 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 tls_show_errors(MSG_INFO, __func__,
4099 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 ERR_clear_error();
4101 return -1;
4102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4106 tls_show_errors(MSG_INFO, __func__,
4107 "Private key failed verification");
4108 return -1;
4109 }
4110
4111 return 0;
4112}
4113
4114
Sunil Ravia04bd252022-05-02 22:54:18 -07004115#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4116#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004118/* This is needed to replace the deprecated DSA_dup_DH() function */
4119static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4120{
4121 OSSL_PARAM_BLD *bld = NULL;
4122 OSSL_PARAM *params = NULL;
4123 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4124 EVP_PKEY_CTX *ctx = NULL;
4125 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126
Sunil Ravia04bd252022-05-02 22:54:18 -07004127 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4128 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4129 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4130 !(bld = OSSL_PARAM_BLD_new()) ||
4131 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4132 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4133 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4134 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4135 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4136 EVP_PKEY_fromdata_init(ctx) != 1 ||
4137 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4138 params) != 1)
4139 wpa_printf(MSG_INFO,
4140 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141
Sunil Ravia04bd252022-05-02 22:54:18 -07004142 EVP_PKEY_CTX_free(ctx);
4143 OSSL_PARAM_free(params);
4144 OSSL_PARAM_BLD_free(bld);
4145 BN_free(p);
4146 BN_free(q);
4147 BN_free(g);
4148 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149}
Sunil Ravia04bd252022-05-02 22:54:18 -07004150#endif /* !OPENSSL_NO_DSA */
4151#endif /* OPENSSL_NO_DH */
4152#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004154static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155{
4156#ifdef OPENSSL_NO_DH
4157 if (dh_file == NULL)
4158 return 0;
4159 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4160 "dh_file specified");
4161 return -1;
4162#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004163#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4164 SSL_CTX *ssl_ctx = data->ssl;
4165 BIO *bio;
4166 OSSL_DECODER_CTX *ctx = NULL;
4167 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4168 bool dsa = false;
4169
4170 if (!ssl_ctx)
4171 return -1;
4172 if (!dh_file) {
4173 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4174 return 0;
4175 }
4176
4177 bio = BIO_new_file(dh_file, "r");
4178 if (!bio) {
4179 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4180 dh_file, ERR_error_string(ERR_get_error(), NULL));
4181 return -1;
4182 }
4183 ctx = OSSL_DECODER_CTX_new_for_pkey(
4184 &tmpkey, "PEM", NULL, NULL,
4185 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4186 if (!ctx ||
4187 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4188 wpa_printf(MSG_INFO,
4189 "TLS: Failed to decode domain parameters from '%s': %s",
4190 dh_file, ERR_error_string(ERR_get_error(), NULL));
4191 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004192 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004193 return -1;
4194 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004195 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004196 BIO_free(bio);
4197
4198 if (!tmpkey) {
4199 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4200 return -1;
4201 }
4202
4203#ifndef OPENSSL_NO_DSA
4204 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4205 pkey = openssl_dsa_to_dh(tmpkey);
4206 EVP_PKEY_free(tmpkey);
4207 if (!pkey)
4208 return -1;
4209 dsa = true;
4210 }
4211#endif /* !OPENSSL_NO_DSA */
4212 if (!dsa) {
4213 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4214 EVP_PKEY_is_a(tmpkey, "DHX")) {
4215 } else {
4216 wpa_printf(MSG_INFO,
4217 "TLS: No DH parameters found in %s",
4218 dh_file);
4219 EVP_PKEY_free(tmpkey);
4220 return -1;
4221 }
4222 pkey = tmpkey;
4223 tmpkey = NULL;
4224 }
4225
4226 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4227 wpa_printf(MSG_INFO,
4228 "TLS: Failed to set DH params from '%s': %s",
4229 dh_file, ERR_error_string(ERR_get_error(), NULL));
4230 EVP_PKEY_free(pkey);
4231 return -1;
4232 }
4233 return 0;
4234#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004235 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 DH *dh;
4237 BIO *bio;
4238
Sunil Ravia04bd252022-05-02 22:54:18 -07004239 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004241 if (!dh_file) {
4242#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4243 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4244#endif
4245 return 0;
4246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247
4248 bio = BIO_new_file(dh_file, "r");
4249 if (bio == NULL) {
4250 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4251 dh_file, ERR_error_string(ERR_get_error(), NULL));
4252 return -1;
4253 }
4254 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4255 BIO_free(bio);
4256#ifndef OPENSSL_NO_DSA
4257 while (dh == NULL) {
4258 DSA *dsa;
4259 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4260 " trying to parse as DSA params", dh_file,
4261 ERR_error_string(ERR_get_error(), NULL));
4262 bio = BIO_new_file(dh_file, "r");
4263 if (bio == NULL)
4264 break;
4265 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4266 BIO_free(bio);
4267 if (!dsa) {
4268 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4269 "'%s': %s", dh_file,
4270 ERR_error_string(ERR_get_error(), NULL));
4271 break;
4272 }
4273
4274 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4275 dh = DSA_dup_DH(dsa);
4276 DSA_free(dsa);
4277 if (dh == NULL) {
4278 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4279 "params into DH params");
4280 break;
4281 }
4282 break;
4283 }
4284#endif /* !OPENSSL_NO_DSA */
4285 if (dh == NULL) {
4286 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4287 "'%s'", dh_file);
4288 return -1;
4289 }
4290
4291 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4292 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4293 "%s", dh_file,
4294 ERR_error_string(ERR_get_error(), NULL));
4295 DH_free(dh);
4296 return -1;
4297 }
4298 DH_free(dh);
4299 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004300#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301#endif /* OPENSSL_NO_DH */
4302}
4303
4304
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004305int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4306 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307{
4308 SSL *ssl;
4309
4310 if (conn == NULL || keys == NULL)
4311 return -1;
4312 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004313 if (ssl == NULL)
4314 return -1;
4315
4316 os_memset(keys, 0, sizeof(*keys));
4317 keys->client_random = conn->client_random;
4318 keys->client_random_len = SSL_get_client_random(
4319 ssl, conn->client_random, sizeof(conn->client_random));
4320 keys->server_random = conn->server_random;
4321 keys->server_random_len = SSL_get_server_random(
4322 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323
4324 return 0;
4325}
4326
4327
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004328#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004329static int openssl_get_keyblock_size(SSL *ssl)
4330{
Sunil Ravia04bd252022-05-02 22:54:18 -07004331#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004332 const EVP_CIPHER *c;
4333 const EVP_MD *h;
4334 int md_size;
4335
4336 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4337 ssl->read_hash == NULL)
4338 return -1;
4339
4340 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004341 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004342 if (h)
4343 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004344 else if (ssl->s3)
4345 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004346 else
4347 return -1;
4348
4349 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4350 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4351 EVP_CIPHER_iv_length(c));
4352 return 2 * (EVP_CIPHER_key_length(c) +
4353 md_size +
4354 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004355#else
4356 const SSL_CIPHER *ssl_cipher;
4357 int cipher, digest;
4358 const EVP_CIPHER *c;
4359 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004360 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004361
4362 ssl_cipher = SSL_get_current_cipher(ssl);
4363 if (!ssl_cipher)
4364 return -1;
4365 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4366 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4367 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4368 cipher, digest);
4369 if (cipher < 0 || digest < 0)
4370 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004371 if (cipher == NID_undef) {
4372 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004373 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004374 }
4375 c = EVP_get_cipherbynid(cipher);
4376 if (!c)
4377 return -1;
4378 enc_key_len = EVP_CIPHER_key_length(c);
4379 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4380 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4381 fixed_iv_len = 4; /* only part of IV from PRF */
4382 else
4383 fixed_iv_len = EVP_CIPHER_iv_length(c);
4384 if (digest == NID_undef) {
4385 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4386 mac_key_len = 0;
4387 } else {
4388 h = EVP_get_digestbynid(digest);
4389 if (!h)
4390 return -1;
4391 mac_key_len = EVP_MD_size(h);
4392 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004393
4394 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004395 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4396 mac_key_len, enc_key_len, fixed_iv_len);
4397 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004399}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004400#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004401
4402
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004403int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004404 const char *label, const u8 *context,
4405 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004406{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004407 if (!conn ||
4408 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004409 os_strlen(label), context, context_len,
4410 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004411 return -1;
4412 return 0;
4413}
4414
4415
4416int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4417 u8 *out, size_t out_len)
4418{
4419#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004420 SSL *ssl;
4421 SSL_SESSION *sess;
4422 u8 *rnd;
4423 int ret = -1;
4424 int skip = 0;
4425 u8 *tmp_out = NULL;
4426 u8 *_out = out;
4427 unsigned char client_random[SSL3_RANDOM_SIZE];
4428 unsigned char server_random[SSL3_RANDOM_SIZE];
4429 unsigned char master_key[64];
4430 size_t master_key_len;
4431 const char *ver;
4432
4433 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004434 * TLS library did not support EAP-FAST key generation, so get the
4435 * needed TLS session parameters and use an internal implementation of
4436 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004437 */
4438
4439 if (conn == NULL)
4440 return -1;
4441 ssl = conn->ssl;
4442 if (ssl == NULL)
4443 return -1;
4444 ver = SSL_get_version(ssl);
4445 sess = SSL_get_session(ssl);
4446 if (!ver || !sess)
4447 return -1;
4448
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004449 skip = openssl_get_keyblock_size(ssl);
4450 if (skip < 0)
4451 return -1;
4452 tmp_out = os_malloc(skip + out_len);
4453 if (!tmp_out)
4454 return -1;
4455 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004456
4457 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4458 if (!rnd) {
4459 os_free(tmp_out);
4460 return -1;
4461 }
4462
4463 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4464 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4465 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4466 sizeof(master_key));
4467
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004468 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4469 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004470
4471 if (os_strcmp(ver, "TLSv1.2") == 0) {
4472 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004473 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474 _out, skip + out_len);
4475 ret = 0;
4476 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004477 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004478 _out, skip + out_len) == 0) {
4479 ret = 0;
4480 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004481 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004482 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004483 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004484 os_memcpy(out, _out + skip, out_len);
4485 bin_clear_free(tmp_out, skip);
4486
4487 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004488#else /* OPENSSL_NEED_EAP_FAST_PRF */
4489 wpa_printf(MSG_ERROR,
4490 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4491 return -1;
4492#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493}
4494
4495
4496static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004497openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498{
Sunil Ravia04bd252022-05-02 22:54:18 -07004499 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004500 int res;
4501 struct wpabuf *out_data;
4502
4503 /*
4504 * Give TLS handshake data from the server (if available) to OpenSSL
4505 * for processing.
4506 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004507 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4509 < 0) {
4510 tls_show_errors(MSG_INFO, __func__,
4511 "Handshake failed - BIO_write");
4512 return NULL;
4513 }
4514
4515 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004516 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 res = SSL_accept(conn->ssl);
4518 else
4519 res = SSL_connect(conn->ssl);
4520 if (res != 1) {
4521 int err = SSL_get_error(conn->ssl, res);
4522 if (err == SSL_ERROR_WANT_READ)
4523 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4524 "more data");
4525 else if (err == SSL_ERROR_WANT_WRITE)
4526 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4527 "write");
4528 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004529 unsigned long error = ERR_peek_last_error();
4530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004532
4533 if (context->event_cb &&
4534 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4535 ERR_GET_REASON(error) ==
4536 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4537 context->event_cb(
4538 context->cb_ctx,
4539 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4540 NULL);
4541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004543 if (!conn->server && !conn->client_hello_generated) {
4544 /* The server would not understand TLS Alert
4545 * before ClientHello, so simply terminate
4546 * handshake on this type of error case caused
4547 * by a likely internal error like no ciphers
4548 * available. */
4549 wpa_printf(MSG_DEBUG,
4550 "OpenSSL: Could not generate ClientHello");
4551 conn->write_alerts++;
4552 return NULL;
4553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554 }
4555 }
4556
Roshan Pius3a1667e2018-07-03 15:17:14 -07004557 if (!conn->server && !conn->failed)
4558 conn->client_hello_generated = 1;
4559
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004560#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004561 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004562 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4563 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004564 /*
4565 * This should not be reached since earlier cert_cb should have
4566 * terminated the handshake. Keep this check here for extra
4567 * protection if anything goes wrong with the more low-level
4568 * checks based on having to parse the TLS handshake messages.
4569 */
4570 wpa_printf(MSG_DEBUG,
4571 "OpenSSL: Server DH prime length: %d bits",
4572 conn->server_dh_prime_len);
4573
4574 if (context->event_cb) {
4575 union tls_event_data ev;
4576
4577 os_memset(&ev, 0, sizeof(ev));
4578 ev.alert.is_local = 1;
4579 ev.alert.type = "fatal";
4580 ev.alert.description = "insufficient security";
4581 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4582 }
4583 /*
4584 * Could send a TLS Alert to the server, but for now, simply
4585 * terminate handshake.
4586 */
4587 conn->failed++;
4588 conn->write_alerts++;
4589 return NULL;
4590 }
4591#endif /* CONFIG_SUITEB */
4592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 /* Get the TLS handshake data to be sent to the server */
4594 res = BIO_ctrl_pending(conn->ssl_out);
4595 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4596 out_data = wpabuf_alloc(res);
4597 if (out_data == NULL) {
4598 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4599 "handshake output (%d bytes)", res);
4600 if (BIO_reset(conn->ssl_out) < 0) {
4601 tls_show_errors(MSG_INFO, __func__,
4602 "BIO_reset failed");
4603 }
4604 return NULL;
4605 }
4606 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4607 res);
4608 if (res < 0) {
4609 tls_show_errors(MSG_INFO, __func__,
4610 "Handshake failed - BIO_read");
4611 if (BIO_reset(conn->ssl_out) < 0) {
4612 tls_show_errors(MSG_INFO, __func__,
4613 "BIO_reset failed");
4614 }
4615 wpabuf_free(out_data);
4616 return NULL;
4617 }
4618 wpabuf_put(out_data, res);
4619
4620 return out_data;
4621}
4622
4623
4624static struct wpabuf *
4625openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4626{
4627 struct wpabuf *appl_data;
4628 int res;
4629
4630 appl_data = wpabuf_alloc(max_len + 100);
4631 if (appl_data == NULL)
4632 return NULL;
4633
4634 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4635 wpabuf_size(appl_data));
4636 if (res < 0) {
4637 int err = SSL_get_error(conn->ssl, res);
4638 if (err == SSL_ERROR_WANT_READ ||
4639 err == SSL_ERROR_WANT_WRITE) {
4640 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4641 "included");
4642 } else {
4643 tls_show_errors(MSG_INFO, __func__,
4644 "Failed to read possible "
4645 "Application Data");
4646 }
4647 wpabuf_free(appl_data);
4648 return NULL;
4649 }
4650
4651 wpabuf_put(appl_data, res);
4652 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4653 "message", appl_data);
4654
4655 return appl_data;
4656}
4657
4658
4659static struct wpabuf *
4660openssl_connection_handshake(struct tls_connection *conn,
4661 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004662 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663{
4664 struct wpabuf *out_data;
4665
4666 if (appl_data)
4667 *appl_data = NULL;
4668
Roshan Pius3a1667e2018-07-03 15:17:14 -07004669 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 if (out_data == NULL)
4671 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004672 if (conn->invalid_hb_used) {
4673 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4674 wpabuf_free(out_data);
4675 return NULL;
4676 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004678 if (SSL_is_init_finished(conn->ssl)) {
4679 wpa_printf(MSG_DEBUG,
4680 "OpenSSL: Handshake finished - resumed=%d",
4681 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004682 if (conn->server) {
4683 char *buf;
4684 size_t buflen = 2000;
4685
4686 buf = os_malloc(buflen);
4687 if (buf) {
4688 if (SSL_get_shared_ciphers(conn->ssl, buf,
4689 buflen)) {
4690 buf[buflen - 1] = '\0';
4691 wpa_printf(MSG_DEBUG,
4692 "OpenSSL: Shared ciphers: %s",
4693 buf);
4694 }
4695 os_free(buf);
4696 }
4697 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004698 if (appl_data && in_data)
4699 *appl_data = openssl_get_appl_data(conn,
4700 wpabuf_len(in_data));
4701 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702
Jouni Malinen26af48b2014-04-09 13:02:53 +03004703 if (conn->invalid_hb_used) {
4704 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4705 if (appl_data) {
4706 wpabuf_free(*appl_data);
4707 *appl_data = NULL;
4708 }
4709 wpabuf_free(out_data);
4710 return NULL;
4711 }
4712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 return out_data;
4714}
4715
4716
4717struct wpabuf *
4718tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4719 const struct wpabuf *in_data,
4720 struct wpabuf **appl_data)
4721{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004722 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723}
4724
4725
4726struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4727 struct tls_connection *conn,
4728 const struct wpabuf *in_data,
4729 struct wpabuf **appl_data)
4730{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004731 conn->server = 1;
4732 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733}
4734
4735
4736struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4737 struct tls_connection *conn,
4738 const struct wpabuf *in_data)
4739{
4740 int res;
4741 struct wpabuf *buf;
4742
4743 if (conn == NULL)
4744 return NULL;
4745
4746 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4747 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4748 (res = BIO_reset(conn->ssl_out)) < 0) {
4749 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4750 return NULL;
4751 }
4752 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4753 if (res < 0) {
4754 tls_show_errors(MSG_INFO, __func__,
4755 "Encryption failed - SSL_write");
4756 return NULL;
4757 }
4758
4759 /* Read encrypted data to be sent to the server */
4760 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4761 if (buf == NULL)
4762 return NULL;
4763 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4764 if (res < 0) {
4765 tls_show_errors(MSG_INFO, __func__,
4766 "Encryption failed - BIO_read");
4767 wpabuf_free(buf);
4768 return NULL;
4769 }
4770 wpabuf_put(buf, res);
4771
4772 return buf;
4773}
4774
4775
4776struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4777 struct tls_connection *conn,
4778 const struct wpabuf *in_data)
4779{
4780 int res;
4781 struct wpabuf *buf;
4782
4783 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4784 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4785 wpabuf_len(in_data));
4786 if (res < 0) {
4787 tls_show_errors(MSG_INFO, __func__,
4788 "Decryption failed - BIO_write");
4789 return NULL;
4790 }
4791 if (BIO_reset(conn->ssl_out) < 0) {
4792 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4793 return NULL;
4794 }
4795
4796 /* Read decrypted data for further processing */
4797 /*
4798 * Even though we try to disable TLS compression, it is possible that
4799 * this cannot be done with all TLS libraries. Add extra buffer space
4800 * to handle the possibility of the decrypted data being longer than
4801 * input data.
4802 */
4803 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4804 if (buf == NULL)
4805 return NULL;
4806 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4807 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004808 int err = SSL_get_error(conn->ssl, res);
4809
4810 if (err == SSL_ERROR_WANT_READ) {
4811 wpa_printf(MSG_DEBUG,
4812 "SSL: SSL_connect - want more data");
4813 res = 0;
4814 } else {
4815 tls_show_errors(MSG_INFO, __func__,
4816 "Decryption failed - SSL_read");
4817 wpabuf_free(buf);
4818 return NULL;
4819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 }
4821 wpabuf_put(buf, res);
4822
Jouni Malinen26af48b2014-04-09 13:02:53 +03004823 if (conn->invalid_hb_used) {
4824 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4825 wpabuf_free(buf);
4826 return NULL;
4827 }
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 return buf;
4830}
4831
4832
4833int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4834{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004835 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836}
4837
4838
4839int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4840 u8 *ciphers)
4841{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004842 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 u8 *c;
4844 int ret;
4845
4846 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4847 return -1;
4848
4849 buf[0] = '\0';
4850 pos = buf;
4851 end = pos + sizeof(buf);
4852
4853 c = ciphers;
4854 while (*c != TLS_CIPHER_NONE) {
4855 const char *suite;
4856
4857 switch (*c) {
4858 case TLS_CIPHER_RC4_SHA:
4859 suite = "RC4-SHA";
4860 break;
4861 case TLS_CIPHER_AES128_SHA:
4862 suite = "AES128-SHA";
4863 break;
4864 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4865 suite = "DHE-RSA-AES128-SHA";
4866 break;
4867 case TLS_CIPHER_ANON_DH_AES128_SHA:
4868 suite = "ADH-AES128-SHA";
4869 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004870 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4871 suite = "DHE-RSA-AES256-SHA";
4872 break;
4873 case TLS_CIPHER_AES256_SHA:
4874 suite = "AES256-SHA";
4875 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876 default:
4877 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4878 "cipher selection: %d", *c);
4879 return -1;
4880 }
4881 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004882 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 break;
4884 pos += ret;
4885
4886 c++;
4887 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004888 if (!buf[0]) {
4889 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4890 return -1;
4891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892
4893 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4894
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004895#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004896#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004897 if (os_strstr(buf, ":ADH-")) {
4898 /*
4899 * Need to drop to security level 0 to allow anonymous
4900 * cipher suites for EAP-FAST.
4901 */
4902 SSL_set_security_level(conn->ssl, 0);
4903 } else if (SSL_get_security_level(conn->ssl) == 0) {
4904 /* Force at least security level 1 */
4905 SSL_set_security_level(conn->ssl, 1);
4906 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004907#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004908#endif
4909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4911 tls_show_errors(MSG_INFO, __func__,
4912 "Cipher suite configuration failed");
4913 return -1;
4914 }
4915
4916 return 0;
4917}
4918
4919
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004920int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4921 char *buf, size_t buflen)
4922{
4923 const char *name;
4924 if (conn == NULL || conn->ssl == NULL)
4925 return -1;
4926
4927 name = SSL_get_version(conn->ssl);
4928 if (name == NULL)
4929 return -1;
4930
4931 os_strlcpy(buf, name, buflen);
4932 return 0;
4933}
4934
4935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4937 char *buf, size_t buflen)
4938{
4939 const char *name;
4940 if (conn == NULL || conn->ssl == NULL)
4941 return -1;
4942
4943 name = SSL_get_cipher(conn->ssl);
4944 if (name == NULL)
4945 return -1;
4946
4947 os_strlcpy(buf, name, buflen);
4948 return 0;
4949}
4950
4951
4952int tls_connection_enable_workaround(void *ssl_ctx,
4953 struct tls_connection *conn)
4954{
4955 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4956
4957 return 0;
4958}
4959
4960
Hai Shalom81f62d82019-07-22 12:10:00 -07004961#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962/* ClientHello TLS extensions require a patch to openssl, so this function is
4963 * commented out unless explicitly needed for EAP-FAST in order to be able to
4964 * build this file with unmodified openssl. */
4965int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4966 int ext_type, const u8 *data,
4967 size_t data_len)
4968{
4969 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4970 return -1;
4971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4973 data_len) != 1)
4974 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975
4976 return 0;
4977}
Hai Shalom81f62d82019-07-22 12:10:00 -07004978#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979
4980
4981int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4982{
4983 if (conn == NULL)
4984 return -1;
4985 return conn->failed;
4986}
4987
4988
4989int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4990{
4991 if (conn == NULL)
4992 return -1;
4993 return conn->read_alerts;
4994}
4995
4996
4997int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
4998{
4999 if (conn == NULL)
5000 return -1;
5001 return conn->write_alerts;
5002}
5003
5004
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005005#ifdef HAVE_OCSP
5006
5007static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5008{
5009#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005010 BIO *out;
5011 size_t rlen;
5012 char *txt;
5013 int res;
5014
5015 if (wpa_debug_level > MSG_DEBUG)
5016 return;
5017
5018 out = BIO_new(BIO_s_mem());
5019 if (!out)
5020 return;
5021
5022 OCSP_RESPONSE_print(out, rsp, 0);
5023 rlen = BIO_ctrl_pending(out);
5024 txt = os_malloc(rlen + 1);
5025 if (!txt) {
5026 BIO_free(out);
5027 return;
5028 }
5029
5030 res = BIO_read(out, txt, rlen);
5031 if (res > 0) {
5032 txt[res] = '\0';
5033 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5034 }
5035 os_free(txt);
5036 BIO_free(out);
5037#endif /* CONFIG_NO_STDOUT_DEBUG */
5038}
5039
5040
5041static int ocsp_resp_cb(SSL *s, void *arg)
5042{
5043 struct tls_connection *conn = arg;
5044 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005045 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005046 OCSP_RESPONSE *rsp;
5047 OCSP_BASICRESP *basic;
5048 OCSP_CERTID *id;
5049 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005050 X509_STORE *store;
5051 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005052
5053 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5054 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005055#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5056#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5057 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5058 /* TLS 1.3 sends the OCSP response with the server
5059 * Certificate message. Since that Certificate message
5060 * is not sent when resuming a session, there can be no
5061 * new OCSP response. Allow this since the OCSP response
5062 * was validated when checking the initial certificate
5063 * exchange. */
5064 wpa_printf(MSG_DEBUG,
5065 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5066 return 1;
5067 }
5068#endif
5069#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005070 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5071 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5072 }
5073
5074 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5075
5076 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5077 if (!rsp) {
5078 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5079 return 0;
5080 }
5081
5082 ocsp_debug_print_resp(rsp);
5083
5084 status = OCSP_response_status(rsp);
5085 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5086 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5087 status, OCSP_response_status_str(status));
5088 return 0;
5089 }
5090
5091 basic = OCSP_response_get1_basic(rsp);
5092 if (!basic) {
5093 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5094 return 0;
5095 }
5096
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005097 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005098 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005099 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005100
5101 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5102 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005103 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005104 }
5105 certs = sk_X509_new_null();
5106 if (certs) {
5107 X509 *cert;
5108 cert = X509_dup(conn->peer_issuer);
5109 if (cert && !sk_X509_push(certs, cert)) {
5110 tls_show_errors(
5111 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005112 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005113 X509_free(cert);
5114 sk_X509_free(certs);
5115 certs = NULL;
5116 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005117 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005118 cert = X509_dup(conn->peer_issuer_issuer);
5119 if (cert && !sk_X509_push(certs, cert)) {
5120 tls_show_errors(
5121 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005122 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005123 X509_free(cert);
5124 }
5125 }
5126 }
5127 }
5128
5129 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5130 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005131 if (status <= 0) {
5132 tls_show_errors(MSG_INFO, __func__,
5133 "OpenSSL: OCSP response failed verification");
5134 OCSP_BASICRESP_free(basic);
5135 OCSP_RESPONSE_free(rsp);
5136 return 0;
5137 }
5138
5139 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5140
Dmitry Shmidt56052862013-10-04 10:23:25 -07005141 if (!conn->peer_cert) {
5142 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5143 OCSP_BASICRESP_free(basic);
5144 OCSP_RESPONSE_free(rsp);
5145 return 0;
5146 }
5147
5148 if (!conn->peer_issuer) {
5149 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005150 OCSP_BASICRESP_free(basic);
5151 OCSP_RESPONSE_free(rsp);
5152 return 0;
5153 }
5154
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005155 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005156 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005157 wpa_printf(MSG_DEBUG,
5158 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005159 OCSP_BASICRESP_free(basic);
5160 OCSP_RESPONSE_free(rsp);
5161 return 0;
5162 }
5163
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005164 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5165 &this_update, &next_update);
5166 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005167 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005168 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5169 if (!id) {
5170 wpa_printf(MSG_DEBUG,
5171 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5172 OCSP_BASICRESP_free(basic);
5173 OCSP_RESPONSE_free(rsp);
5174 return 0;
5175 }
5176
5177 res = OCSP_resp_find_status(basic, id, &status, &reason,
5178 &produced_at, &this_update,
5179 &next_update);
5180 }
5181
5182 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005183 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5184 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5185 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005186 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005187 OCSP_BASICRESP_free(basic);
5188 OCSP_RESPONSE_free(rsp);
5189 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5190 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005191 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005192
5193 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5194 tls_show_errors(MSG_INFO, __func__,
5195 "OpenSSL: OCSP status times invalid");
5196 OCSP_BASICRESP_free(basic);
5197 OCSP_RESPONSE_free(rsp);
5198 return 0;
5199 }
5200
5201 OCSP_BASICRESP_free(basic);
5202 OCSP_RESPONSE_free(rsp);
5203
5204 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5205 OCSP_cert_status_str(status));
5206
5207 if (status == V_OCSP_CERTSTATUS_GOOD)
5208 return 1;
5209 if (status == V_OCSP_CERTSTATUS_REVOKED)
5210 return 0;
5211 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5212 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5213 return 0;
5214 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005215 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 -07005216 return 1;
5217}
5218
5219
5220static int ocsp_status_cb(SSL *s, void *arg)
5221{
5222 char *tmp;
5223 char *resp;
5224 size_t len;
5225
5226 if (tls_global->ocsp_stapling_response == NULL) {
5227 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5228 return SSL_TLSEXT_ERR_OK;
5229 }
5230
5231 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5232 if (resp == NULL) {
5233 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5234 /* TODO: Build OCSPResponse with responseStatus = internalError
5235 */
5236 return SSL_TLSEXT_ERR_OK;
5237 }
5238 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5239 tmp = OPENSSL_malloc(len);
5240 if (tmp == NULL) {
5241 os_free(resp);
5242 return SSL_TLSEXT_ERR_ALERT_FATAL;
5243 }
5244
5245 os_memcpy(tmp, resp, len);
5246 os_free(resp);
5247 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5248
5249 return SSL_TLSEXT_ERR_OK;
5250}
5251
5252#endif /* HAVE_OCSP */
5253
5254
Hai Shalomfdcde762020-04-02 11:19:20 -07005255static size_t max_str_len(const char **lines)
5256{
5257 const char **p;
5258 size_t max_len = 0;
5259
5260 for (p = lines; *p; p++) {
5261 size_t len = os_strlen(*p);
5262
5263 if (len > max_len)
5264 max_len = len;
5265 }
5266
5267 return max_len;
5268}
5269
5270
5271static int match_lines_in_file(const char *path, const char **lines)
5272{
5273 FILE *f;
5274 char *buf;
5275 size_t bufsize;
5276 int found = 0, is_linestart = 1;
5277
5278 bufsize = max_str_len(lines) + sizeof("\r\n");
5279 buf = os_malloc(bufsize);
5280 if (!buf)
5281 return 0;
5282
5283 f = fopen(path, "r");
5284 if (!f) {
5285 os_free(buf);
5286 return 0;
5287 }
5288
5289 while (!found && fgets(buf, bufsize, f)) {
5290 int is_lineend;
5291 size_t len;
5292 const char **p;
5293
5294 len = strcspn(buf, "\r\n");
5295 is_lineend = buf[len] != '\0';
5296 buf[len] = '\0';
5297
5298 if (is_linestart && is_lineend) {
5299 for (p = lines; !found && *p; p++)
5300 found = os_strcmp(buf, *p) == 0;
5301 }
5302 is_linestart = is_lineend;
5303 }
5304
5305 fclose(f);
5306 bin_clear_free(buf, bufsize);
5307
5308 return found;
5309}
5310
5311
5312static int is_tpm2_key(const char *path)
5313{
5314 /* Check both new and old format of TPM2 PEM guard tag */
5315 static const char *tpm2_tags[] = {
5316 "-----BEGIN TSS2 PRIVATE KEY-----",
5317 "-----BEGIN TSS2 KEY BLOB-----",
5318 NULL
5319 };
5320
5321 return match_lines_in_file(path, tpm2_tags);
5322}
5323
5324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5326 const struct tls_connection_params *params)
5327{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005328 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329 int ret;
5330 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005331 int can_pkcs11 = 0;
5332 const char *key_id = params->key_id;
5333 const char *cert_id = params->cert_id;
5334 const char *ca_cert_id = params->ca_cert_id;
5335 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005336 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337
5338 if (conn == NULL)
5339 return -1;
5340
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005341 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5342 wpa_printf(MSG_INFO,
5343 "OpenSSL: ocsp=3 not supported");
5344 return -1;
5345 }
5346
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005347 /*
5348 * If the engine isn't explicitly configured, and any of the
5349 * cert/key fields are actually PKCS#11 URIs, then automatically
5350 * use the PKCS#11 ENGINE.
5351 */
5352 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5353 can_pkcs11 = 1;
5354
5355 if (!key_id && params->private_key && can_pkcs11 &&
5356 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5357 can_pkcs11 = 2;
5358 key_id = params->private_key;
5359 }
5360
5361 if (!cert_id && params->client_cert && can_pkcs11 &&
5362 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5363 can_pkcs11 = 2;
5364 cert_id = params->client_cert;
5365 }
5366
5367 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5368 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5369 can_pkcs11 = 2;
5370 ca_cert_id = params->ca_cert;
5371 }
5372
5373 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5374 if (can_pkcs11 == 2 && !engine_id)
5375 engine_id = "pkcs11";
5376
Hai Shalomfdcde762020-04-02 11:19:20 -07005377 /* If private_key points to a TPM2-wrapped key, automatically enable
5378 * tpm2 engine and use it to unwrap the key. */
5379 if (params->private_key &&
5380 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5381 is_tpm2_key(params->private_key)) {
5382 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5383 params->private_key);
5384 key_id = key_id ? key_id : params->private_key;
5385 engine_id = engine_id ? engine_id : "tpm2";
5386 }
5387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005388#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005389#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005390 if (params->flags & TLS_CONN_EAP_FAST) {
5391 wpa_printf(MSG_DEBUG,
5392 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5393 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5394 tls_show_errors(MSG_INFO, __func__,
5395 "Failed to set TLSv1_method() for EAP-FAST");
5396 return -1;
5397 }
5398 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005399#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005400#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5401#ifdef SSL_OP_NO_TLSv1_3
5402 if (params->flags & TLS_CONN_EAP_FAST) {
5403 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5404 * refuses to start the handshake with the modified ciphersuite
5405 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5406 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5407 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5408 }
5409#endif /* SSL_OP_NO_TLSv1_3 */
5410#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005411#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005413 while ((err = ERR_get_error())) {
5414 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5415 __func__, ERR_error_string(err, NULL));
5416 }
5417
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005418 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005419 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5420 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005421 ret = tls_engine_init(conn, engine_id, params->pin,
5422 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 if (ret)
5424 return ret;
5425 }
5426 if (tls_connection_set_subject_match(conn,
5427 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005428 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005429 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005430 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005431 params->check_cert_subject)) {
5432 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005436 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005437 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005439 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 params->ca_cert_blob,
5441 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005442 params->ca_path)) {
5443 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005447 if (engine_id && cert_id) {
5448 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5450 } else if (tls_connection_client_cert(conn, params->client_cert,
5451 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005452 params->client_cert_blob_len)) {
5453 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005457 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5459 if (tls_connection_engine_private_key(conn))
5460 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005461 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 params->private_key,
5463 params->private_key_passwd,
5464 params->private_key_blob,
5465 params->private_key_blob_len)) {
5466 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5467 params->private_key);
5468 return -1;
5469 }
5470
Roshan Pius3a1667e2018-07-03 15:17:14 -07005471 ciphers = params->openssl_ciphers;
5472#ifdef CONFIG_SUITEB
5473#ifdef OPENSSL_IS_BORINGSSL
5474 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5475 /* BoringSSL removed support for SUITEB192, so need to handle
5476 * this with hardcoded ciphersuite and additional checks for
5477 * other parameters. */
5478 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5479 }
5480#endif /* OPENSSL_IS_BORINGSSL */
5481#endif /* CONFIG_SUITEB */
5482 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005483 wpa_printf(MSG_INFO,
5484 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005485 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005486 return -1;
5487 }
5488
Hai Shalom74f70d42019-02-11 14:42:39 -08005489 if (!params->openssl_ecdh_curves) {
5490#ifndef OPENSSL_IS_BORINGSSL
5491#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005492#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005493 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5494 wpa_printf(MSG_INFO,
5495 "OpenSSL: Failed to set ECDH curves to auto");
5496 return -1;
5497 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005498#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005499#endif /* OPENSSL_NO_EC */
5500#endif /* OPENSSL_IS_BORINGSSL */
5501 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005502#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005503 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005504 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005505 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005506#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005507#ifndef OPENSSL_NO_EC
5508 if (SSL_set1_curves_list(conn->ssl,
5509 params->openssl_ecdh_curves) != 1) {
5510 wpa_printf(MSG_INFO,
5511 "OpenSSL: Failed to set ECDH curves '%s'",
5512 params->openssl_ecdh_curves);
5513 return -1;
5514 }
5515#else /* OPENSSL_NO_EC */
5516 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5517 return -1;
5518#endif /* OPENSSL_NO_EC */
5519#endif /* OPENSSL_IS_BORINGSSL */
5520 }
5521
Roshan Pius3a1667e2018-07-03 15:17:14 -07005522 if (tls_set_conn_flags(conn, params->flags,
Hai Shalom22171592021-04-02 16:05:23 -07005523 params->openssl_ciphers) < 0) {
5524 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005525 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005526 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005527
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005528#ifdef OPENSSL_IS_BORINGSSL
5529 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5530 SSL_enable_ocsp_stapling(conn->ssl);
5531 }
5532#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005533#ifdef HAVE_OCSP
5534 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005535 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005536 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5537 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5538 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5539 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005540#else /* HAVE_OCSP */
5541 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5542 wpa_printf(MSG_INFO,
5543 "OpenSSL: No OCSP support included - reject configuration");
5544 return -1;
5545 }
5546 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5547 wpa_printf(MSG_DEBUG,
5548 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5549 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005550#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005551#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005552
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005553 conn->flags = params->flags;
5554
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005555 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556
5557 return 0;
5558}
5559
5560
Hai Shalom81f62d82019-07-22 12:10:00 -07005561static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5562{
5563 SSL *ssl;
5564 int i;
5565
5566 ssl = SSL_new(ssl_ctx);
5567 if (!ssl)
5568 return;
5569
5570 wpa_printf(MSG_DEBUG,
5571 "OpenSSL: Enabled cipher suites in priority order");
5572 for (i = 0; ; i++) {
5573 const char *cipher;
5574
5575 cipher = SSL_get_cipher_list(ssl, i);
5576 if (!cipher)
5577 break;
5578 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5579 }
5580
5581 SSL_free(ssl);
5582}
5583
5584
5585#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5586
5587static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5588{
5589 if (!pkey)
5590 return "NULL";
5591 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5592 case EVP_PKEY_RSA:
5593 return "RSA";
5594 case EVP_PKEY_DSA:
5595 return "DSA";
5596 case EVP_PKEY_DH:
5597 return "DH";
5598 case EVP_PKEY_EC:
5599 return "EC";
5600 }
5601 return "?";
5602}
5603
5604
5605static void openssl_debug_dump_certificate(int i, X509 *cert)
5606{
5607 char buf[256];
5608 EVP_PKEY *pkey;
5609 ASN1_INTEGER *ser;
5610 char serial_num[128];
5611
Hai Shalom60840252021-02-19 19:02:11 -08005612 if (!cert)
5613 return;
5614
Hai Shalom81f62d82019-07-22 12:10:00 -07005615 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5616
5617 ser = X509_get_serialNumber(cert);
5618 if (ser)
5619 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5620 ASN1_STRING_get0_data(ser),
5621 ASN1_STRING_length(ser));
5622 else
5623 serial_num[0] = '\0';
5624
5625 pkey = X509_get_pubkey(cert);
5626 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5627 openssl_pkey_type_str(pkey), serial_num);
5628 EVP_PKEY_free(pkey);
5629}
5630
5631
5632static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5633{
5634 STACK_OF(X509) *certs;
5635
5636 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5637 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5638 int i;
5639
5640 for (i = sk_X509_num(certs); i > 0; i--)
5641 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5642 i - 1));
5643 }
5644 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5645}
5646
5647#endif
5648
5649
5650static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5651{
5652#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5653 int res;
5654
5655 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5656 res == 1;
5657 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5658 openssl_debug_dump_certificates(ssl_ctx);
5659
5660 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5661#endif
5662}
5663
5664
5665static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5666{
5667 openssl_debug_dump_cipher_list(ssl_ctx);
5668 openssl_debug_dump_certificate_chains(ssl_ctx);
5669}
5670
5671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672int tls_global_set_params(void *tls_ctx,
5673 const struct tls_connection_params *params)
5674{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005675 struct tls_data *data = tls_ctx;
5676 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677 unsigned long err;
5678
5679 while ((err = ERR_get_error())) {
5680 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5681 __func__, ERR_error_string(err, NULL));
5682 }
5683
Hai Shalom021b0b52019-04-10 11:17:58 -07005684 os_free(data->check_cert_subject);
5685 data->check_cert_subject = NULL;
5686 if (params->check_cert_subject) {
5687 data->check_cert_subject =
5688 os_strdup(params->check_cert_subject);
5689 if (!data->check_cert_subject)
5690 return -1;
5691 }
5692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005693 if (tls_global_ca_cert(data, params->ca_cert) ||
5694 tls_global_client_cert(data, params->client_cert) ||
5695 tls_global_private_key(data, params->private_key,
5696 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005697 tls_global_client_cert(data, params->client_cert2) ||
5698 tls_global_private_key(data, params->private_key2,
5699 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005700 tls_global_dh(data, params->dh_file)) {
5701 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005702 return -1;
5703 }
5704
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005705 if (params->openssl_ciphers &&
5706 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5707 wpa_printf(MSG_INFO,
5708 "OpenSSL: Failed to set cipher string '%s'",
5709 params->openssl_ciphers);
5710 return -1;
5711 }
5712
Hai Shalom74f70d42019-02-11 14:42:39 -08005713 if (!params->openssl_ecdh_curves) {
5714#ifndef OPENSSL_IS_BORINGSSL
5715#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005716#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005717 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5718 wpa_printf(MSG_INFO,
5719 "OpenSSL: Failed to set ECDH curves to auto");
5720 return -1;
5721 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005722#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005723#endif /* OPENSSL_NO_EC */
5724#endif /* OPENSSL_IS_BORINGSSL */
5725 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005726#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005727 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005728 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005729 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005730#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005731#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005732#if OPENSSL_VERSION_NUMBER < 0x10100000L
5733 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5734#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005735 if (SSL_CTX_set1_curves_list(ssl_ctx,
5736 params->openssl_ecdh_curves) !=
5737 1) {
5738 wpa_printf(MSG_INFO,
5739 "OpenSSL: Failed to set ECDH curves '%s'",
5740 params->openssl_ecdh_curves);
5741 return -1;
5742 }
5743#else /* OPENSSL_NO_EC */
5744 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5745 return -1;
5746#endif /* OPENSSL_NO_EC */
5747#endif /* OPENSSL_IS_BORINGSSL */
5748 }
5749
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005750#ifdef SSL_OP_NO_TICKET
5751 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5752 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5753 else
5754 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5755#endif /* SSL_OP_NO_TICKET */
5756
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005757#ifdef HAVE_OCSP
5758 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5759 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5760 os_free(tls_global->ocsp_stapling_response);
5761 if (params->ocsp_stapling_response)
5762 tls_global->ocsp_stapling_response =
5763 os_strdup(params->ocsp_stapling_response);
5764 else
5765 tls_global->ocsp_stapling_response = NULL;
5766#endif /* HAVE_OCSP */
5767
Hai Shalom81f62d82019-07-22 12:10:00 -07005768 openssl_debug_dump_ctx(ssl_ctx);
5769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 return 0;
5771}
5772
5773
Hai Shalom81f62d82019-07-22 12:10:00 -07005774#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775/* Pre-shared secred requires a patch to openssl, so this function is
5776 * commented out unless explicitly needed for EAP-FAST in order to be able to
5777 * build this file with unmodified openssl. */
5778
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005779#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005780static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5781 STACK_OF(SSL_CIPHER) *peer_ciphers,
5782 const SSL_CIPHER **cipher, void *arg)
5783#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5785 STACK_OF(SSL_CIPHER) *peer_ciphers,
5786 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005787#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788{
5789 struct tls_connection *conn = arg;
5790 int ret;
5791
Sunil Ravia04bd252022-05-02 22:54:18 -07005792#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 if (conn == NULL || conn->session_ticket_cb == NULL)
5794 return 0;
5795
5796 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5797 conn->session_ticket,
5798 conn->session_ticket_len,
5799 s->s3->client_random,
5800 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005801#else
5802 unsigned char client_random[SSL3_RANDOM_SIZE];
5803 unsigned char server_random[SSL3_RANDOM_SIZE];
5804
5805 if (conn == NULL || conn->session_ticket_cb == NULL)
5806 return 0;
5807
5808 SSL_get_client_random(s, client_random, sizeof(client_random));
5809 SSL_get_server_random(s, server_random, sizeof(server_random));
5810
5811 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5812 conn->session_ticket,
5813 conn->session_ticket_len,
5814 client_random,
5815 server_random, secret);
5816#endif
5817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 os_free(conn->session_ticket);
5819 conn->session_ticket = NULL;
5820
5821 if (ret <= 0)
5822 return 0;
5823
5824 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5825 return 1;
5826}
5827
5828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5830 int len, void *arg)
5831{
5832 struct tls_connection *conn = arg;
5833
5834 if (conn == NULL || conn->session_ticket_cb == NULL)
5835 return 0;
5836
5837 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5838
5839 os_free(conn->session_ticket);
5840 conn->session_ticket = NULL;
5841
5842 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5843 "extension", data, len);
5844
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005845 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846 if (conn->session_ticket == NULL)
5847 return 0;
5848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849 conn->session_ticket_len = len;
5850
5851 return 1;
5852}
Hai Shalom81f62d82019-07-22 12:10:00 -07005853#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005854
5855
5856int tls_connection_set_session_ticket_cb(void *tls_ctx,
5857 struct tls_connection *conn,
5858 tls_session_ticket_cb cb,
5859 void *ctx)
5860{
Hai Shalom81f62d82019-07-22 12:10:00 -07005861#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862 conn->session_ticket_cb = cb;
5863 conn->session_ticket_cb_ctx = ctx;
5864
5865 if (cb) {
5866 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5867 conn) != 1)
5868 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869 SSL_set_session_ticket_ext_cb(conn->ssl,
5870 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871 } else {
5872 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5873 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875 }
5876
5877 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005878#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005880#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005881}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005882
5883
5884int tls_get_library_version(char *buf, size_t buf_len)
5885{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005886#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005887 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5888 OPENSSL_VERSION_TEXT,
5889 OpenSSL_version(OPENSSL_VERSION));
5890#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005891 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5892 OPENSSL_VERSION_TEXT,
5893 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005894#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005895}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005896
5897
5898void tls_connection_set_success_data(struct tls_connection *conn,
5899 struct wpabuf *data)
5900{
5901 SSL_SESSION *sess;
5902 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005903 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005904
5905 if (tls_ex_idx_session < 0)
5906 goto fail;
5907 sess = SSL_get_session(conn->ssl);
5908 if (!sess)
5909 goto fail;
5910 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5911 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005912 struct tls_session_data *found;
5913
5914 found = get_session_data(conn->context, old);
5915 wpa_printf(MSG_DEBUG,
5916 "OpenSSL: Replacing old success data %p (sess %p)%s",
5917 old, sess, found ? "" : " (not freeing)");
5918 if (found) {
5919 dl_list_del(&found->list);
5920 os_free(found);
5921 wpabuf_free(old);
5922 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005923 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005924
5925 sess_data = os_zalloc(sizeof(*sess_data));
5926 if (!sess_data ||
5927 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005928 goto fail;
5929
Sunil Ravia04bd252022-05-02 22:54:18 -07005930 sess_data->buf = data;
5931 dl_list_add(&conn->context->sessions, &sess_data->list);
5932 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5933 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005934 conn->success_data = 1;
5935 return;
5936
5937fail:
5938 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
5939 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07005940 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005941}
5942
5943
5944void tls_connection_set_success_data_resumed(struct tls_connection *conn)
5945{
5946 wpa_printf(MSG_DEBUG,
5947 "OpenSSL: Success data accepted for resumed session");
5948 conn->success_data = 1;
5949}
5950
5951
5952const struct wpabuf *
5953tls_connection_get_success_data(struct tls_connection *conn)
5954{
5955 SSL_SESSION *sess;
5956
5957 if (tls_ex_idx_session < 0 ||
5958 !(sess = SSL_get_session(conn->ssl)))
5959 return NULL;
5960 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5961}
5962
5963
5964void tls_connection_remove_session(struct tls_connection *conn)
5965{
5966 SSL_SESSION *sess;
5967
5968 sess = SSL_get_session(conn->ssl);
5969 if (!sess)
5970 return;
5971
5972 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5973 wpa_printf(MSG_DEBUG,
5974 "OpenSSL: Session was not cached");
5975 else
5976 wpa_printf(MSG_DEBUG,
5977 "OpenSSL: Removed cached session to disable session resumption");
5978}
Hai Shalom81f62d82019-07-22 12:10:00 -07005979
5980
5981int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
5982{
5983 size_t len;
5984 int reused;
5985
5986 reused = SSL_session_reused(conn->ssl);
5987 if ((conn->server && !reused) || (!conn->server && reused))
5988 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
5989 else
5990 len = SSL_get_finished(conn->ssl, buf, max_len);
5991
5992 if (len == 0 || len > max_len)
5993 return -1;
5994
5995 return len;
5996}
5997
5998
5999u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6000{
6001 const SSL_CIPHER *cipher;
6002
6003 cipher = SSL_get_current_cipher(conn->ssl);
6004 if (!cipher)
6005 return 0;
6006#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6007 return SSL_CIPHER_get_protocol_id(cipher);
6008#else
6009 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6010#endif
6011}
Hai Shalom899fcc72020-10-19 14:38:18 -07006012
6013
6014const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6015{
6016 if (conn)
6017 return conn->peer_subject;
6018 return NULL;
6019}
6020
6021
6022bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6023{
6024 if (conn)
6025 return SSL_get_certificate(conn->ssl) != NULL;
6026 return false;
6027}