blob: 29f0d189dbc7f4a83b18d5d8954e1b38767c992a [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
Gabriel Birena5bdf372022-12-15 20:54:33 +0000129static int tls_openssl_ref_count = 0;
130static int tls_ex_idx_session = -1;
131
132struct tls_session_data {
133 struct dl_list list;
134 struct wpabuf *buf;
135};
136
137struct tls_context {
138 void (*event_cb)(void *ctx, enum tls_event ev,
139 union tls_event_data *data);
140 void *cb_ctx;
141 int cert_in_cb;
142 char *ocsp_stapling_response;
143 struct dl_list sessions; /* struct tls_session_data */
144};
145
146static struct tls_context *tls_global = NULL;
147static tls_get_certificate_cb certificate_callback_global = NULL;
148
Dmitry Shmidtff079172013-11-08 14:10:30 -0800149#ifdef ANDROID
150#include <openssl/pem.h>
Dmitry Shmidtff079172013-11-08 14:10:30 -0800151
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000152#include <log/log.h>
153#include <log/log_event_list.h>
154
155#define CERT_VALIDATION_FAILURE 210033
Hai Shalom7ad2a872021-08-02 18:56:55 -0700156#define ANDROID_KEYSTORE_PREFIX "keystore://"
157#define ANDROID_KEYSTORE_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_PREFIX)
158#define ANDROID_KEYSTORE_ENCODED_PREFIX "keystores://"
159#define ANDROID_KEYSTORE_ENCODED_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_ENCODED_PREFIX)
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000160
161static void log_cert_validation_failure(const char *reason)
162{
163 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
164 android_log_write_string8(ctx, reason);
165 android_log_write_list(ctx, LOG_ID_SECURITY);
166 android_log_destroy(&ctx);
167}
168
169
Hai Shalom7ad2a872021-08-02 18:56:55 -0700170static BIO* BIO_from_keystore(const char *alias)
Dmitry Shmidtff079172013-11-08 14:10:30 -0800171{
172 BIO *bio = NULL;
173 uint8_t *value = NULL;
Gabriel Birena5bdf372022-12-15 20:54:33 +0000174 if (tls_global != NULL && certificate_callback_global != NULL) {
175 int length = (*certificate_callback_global)(tls_global->cb_ctx, alias, &value);
176 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
177 BIO_write(bio, value, length);
178 }
Dmitry Shmidtff079172013-11-08 14:10:30 -0800179 free(value);
180 return bio;
181}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800182
Hai Shalom7ad2a872021-08-02 18:56:55 -0700183static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800184{
Hai Shalom7ad2a872021-08-02 18:56:55 -0700185 BIO *bio = BIO_from_keystore(alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800186 STACK_OF(X509_INFO) *stack = NULL;
187 stack_index_t i;
Hai Shalom22171592021-04-02 16:05:23 -0700188 int ret = 0;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800189
Hai Shalom22171592021-04-02 16:05:23 -0700190 if (!bio) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700191 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
192 alias);
Hai Shalom22171592021-04-02 16:05:23 -0700193 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800194 }
195
Hai Shalom7ad2a872021-08-02 18:56:55 -0700196 // Keystore returns X.509 certificates in PEM encoding
Hai Shalom22171592021-04-02 16:05:23 -0700197 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
198 BIO_free(bio);
199
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800200 if (!stack) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700201 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
202 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800203 return -1;
204 }
205
206 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
207 X509_INFO *info = sk_X509_INFO_value(stack, i);
208
209 if (info->x509)
Hai Shalom22171592021-04-02 16:05:23 -0700210 if (!X509_STORE_add_cert(ctx, info->x509)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700211 wpa_printf(MSG_ERROR,
212 "OpenSSL: Failed to add Root CA certificate");
Hai Shalom22171592021-04-02 16:05:23 -0700213 ret = -1;
214 break;
215 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800216 if (info->crl)
217 X509_STORE_add_crl(ctx, info->crl);
218 }
219
220 sk_X509_INFO_pop_free(stack, X509_INFO_free);
Hai Shalom22171592021-04-02 16:05:23 -0700221 return ret;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800222}
223
224
225static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
Hai Shalom7ad2a872021-08-02 18:56:55 -0700226 const char *encoded_alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800227{
228 int rc = -1;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700229 int len = os_strlen(encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800230 unsigned char *decoded_alias;
231
232 if (len & 1) {
233 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
Hai Shalom7ad2a872021-08-02 18:56:55 -0700234 encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800235 return rc;
236 }
237
238 decoded_alias = os_malloc(len / 2 + 1);
239 if (decoded_alias) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700240 if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800241 decoded_alias[len / 2] = '\0';
242 rc = tls_add_ca_from_keystore(
243 ctx, (const char *) decoded_alias);
244 }
245 os_free(decoded_alias);
246 }
247
248 return rc;
249}
250
Dmitry Shmidtff079172013-11-08 14:10:30 -0800251#endif /* ANDROID */
252
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800253struct tls_data {
254 SSL_CTX *ssl;
255 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -0800256 int check_crl;
257 int check_crl_strict;
258 char *ca_cert;
259 unsigned int crl_reload_interval;
260 struct os_reltime crl_last_reload;
Hai Shalom021b0b52019-04-10 11:17:58 -0700261 char *check_cert_subject;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800262};
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700265 struct tls_context *context;
Hai Shalom74f70d42019-02-11 14:42:39 -0800266 struct tls_data *data;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800267 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 SSL *ssl;
269 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700270#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 ENGINE *engine; /* functional reference to the engine */
272 EVP_PKEY *private_key; /* the private key if using engine */
273#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800274 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -0700275 char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 int read_alerts, write_alerts, failed;
277
278 tls_session_ticket_cb session_ticket_cb;
279 void *session_ticket_cb_ctx;
280
281 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
282 u8 *session_ticket;
283 size_t session_ticket_len;
284
285 unsigned int ca_cert_verify:1;
286 unsigned int cert_probe:1;
287 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300288 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800289 unsigned int success_data:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700290 unsigned int client_hello_generated:1;
291 unsigned int server:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292
293 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700294
295 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700296
297 X509 *peer_cert;
298 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 X509 *peer_issuer_issuer;
Hai Shalom899fcc72020-10-19 14:38:18 -0700300 char *peer_subject; /* peer subject info for authenticated peer */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800301
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800302 unsigned char client_random[SSL3_RANDOM_SIZE];
303 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700304
305 u16 cipher_suite;
306 int server_dh_prime_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307};
308
309
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700310static struct tls_context * tls_context_new(const struct tls_config *conf)
311{
312 struct tls_context *context = os_zalloc(sizeof(*context));
313 if (context == NULL)
314 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -0700315 dl_list_init(&context->sessions);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700316 if (conf) {
317 context->event_cb = conf->event_cb;
318 context->cb_ctx = conf->cb_ctx;
319 context->cert_in_cb = conf->cert_in_cb;
320 }
321 return context;
322}
323
324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325#ifdef CONFIG_NO_STDOUT_DEBUG
326
327static void _tls_show_errors(void)
328{
329 unsigned long err;
330
331 while ((err = ERR_get_error())) {
332 /* Just ignore the errors, since stdout is disabled */
333 }
334}
335#define tls_show_errors(l, f, t) _tls_show_errors()
336
337#else /* CONFIG_NO_STDOUT_DEBUG */
338
339static void tls_show_errors(int level, const char *func, const char *txt)
340{
341 unsigned long err;
342
343 wpa_printf(level, "OpenSSL: %s - %s %s",
344 func, txt, ERR_error_string(ERR_get_error(), NULL));
345
346 while ((err = ERR_get_error())) {
347 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
348 ERR_error_string(err, NULL));
349 }
350}
351
352#endif /* CONFIG_NO_STDOUT_DEBUG */
353
354
Hai Shalom74f70d42019-02-11 14:42:39 -0800355static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
356{
357 int flags;
358 X509_STORE *store;
359
360 store = X509_STORE_new();
361 if (!store) {
362 wpa_printf(MSG_DEBUG,
363 "OpenSSL: %s - failed to allocate new certificate store",
364 __func__);
365 return NULL;
366 }
367
368 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
369 tls_show_errors(MSG_WARNING, __func__,
370 "Failed to load root certificates");
371 X509_STORE_free(store);
372 return NULL;
373 }
374
375 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
376 if (check_crl == 2)
377 flags |= X509_V_FLAG_CRL_CHECK_ALL;
378
379 X509_STORE_set_flags(store, flags);
380
381 return store;
382}
383
384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385#ifdef CONFIG_NATIVE_WINDOWS
386
387/* Windows CryptoAPI and access to certificate stores */
388#include <wincrypt.h>
389
390#ifdef __MINGW32_VERSION
391/*
392 * MinGW does not yet include all the needed definitions for CryptoAPI, so
393 * define here whatever extra is needed.
394 */
395#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
396#define CERT_STORE_READONLY_FLAG 0x00008000
397#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
398
399#endif /* __MINGW32_VERSION */
400
401
402struct cryptoapi_rsa_data {
403 const CERT_CONTEXT *cert;
404 HCRYPTPROV crypt_prov;
405 DWORD key_spec;
406 BOOL free_crypt_prov;
407};
408
409
410static void cryptoapi_error(const char *msg)
411{
412 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
413 msg, (unsigned int) GetLastError());
414}
415
416
417static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
418 unsigned char *to, RSA *rsa, int padding)
419{
420 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
421 return 0;
422}
423
424
425static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
426 unsigned char *to, RSA *rsa, int padding)
427{
428 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
429 return 0;
430}
431
432
433static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
434 unsigned char *to, RSA *rsa, int padding)
435{
436 struct cryptoapi_rsa_data *priv =
437 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
438 HCRYPTHASH hash;
439 DWORD hash_size, len, i;
440 unsigned char *buf = NULL;
441 int ret = 0;
442
443 if (priv == NULL) {
444 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
445 ERR_R_PASSED_NULL_PARAMETER);
446 return 0;
447 }
448
449 if (padding != RSA_PKCS1_PADDING) {
450 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
451 RSA_R_UNKNOWN_PADDING_TYPE);
452 return 0;
453 }
454
455 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
456 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
457 __func__);
458 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
459 RSA_R_INVALID_MESSAGE_LENGTH);
460 return 0;
461 }
462
463 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
464 {
465 cryptoapi_error("CryptCreateHash failed");
466 return 0;
467 }
468
469 len = sizeof(hash_size);
470 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
471 0)) {
472 cryptoapi_error("CryptGetHashParam failed");
473 goto err;
474 }
475
476 if ((int) hash_size != flen) {
477 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
478 (unsigned) hash_size, flen);
479 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
480 RSA_R_INVALID_MESSAGE_LENGTH);
481 goto err;
482 }
483 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
484 cryptoapi_error("CryptSetHashParam failed");
485 goto err;
486 }
487
488 len = RSA_size(rsa);
489 buf = os_malloc(len);
490 if (buf == NULL) {
491 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
492 goto err;
493 }
494
495 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
496 cryptoapi_error("CryptSignHash failed");
497 goto err;
498 }
499
500 for (i = 0; i < len; i++)
501 to[i] = buf[len - i - 1];
502 ret = len;
503
504err:
505 os_free(buf);
506 CryptDestroyHash(hash);
507
508 return ret;
509}
510
511
512static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
513 unsigned char *to, RSA *rsa, int padding)
514{
515 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
516 return 0;
517}
518
519
520static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
521{
522 if (priv == NULL)
523 return;
524 if (priv->crypt_prov && priv->free_crypt_prov)
525 CryptReleaseContext(priv->crypt_prov, 0);
526 if (priv->cert)
527 CertFreeCertificateContext(priv->cert);
528 os_free(priv);
529}
530
531
532static int cryptoapi_finish(RSA *rsa)
533{
534 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
535 os_free((void *) rsa->meth);
536 rsa->meth = NULL;
537 return 1;
538}
539
540
541static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
542{
543 HCERTSTORE cs;
544 const CERT_CONTEXT *ret = NULL;
545
546 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
547 store | CERT_STORE_OPEN_EXISTING_FLAG |
548 CERT_STORE_READONLY_FLAG, L"MY");
549 if (cs == NULL) {
550 cryptoapi_error("Failed to open 'My system store'");
551 return NULL;
552 }
553
554 if (strncmp(name, "cert://", 7) == 0) {
555 unsigned short wbuf[255];
556 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
557 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
558 PKCS_7_ASN_ENCODING,
559 0, CERT_FIND_SUBJECT_STR,
560 wbuf, NULL);
561 } else if (strncmp(name, "hash://", 7) == 0) {
562 CRYPT_HASH_BLOB blob;
563 int len;
564 const char *hash = name + 7;
565 unsigned char *buf;
566
567 len = os_strlen(hash) / 2;
568 buf = os_malloc(len);
569 if (buf && hexstr2bin(hash, buf, len) == 0) {
570 blob.cbData = len;
571 blob.pbData = buf;
572 ret = CertFindCertificateInStore(cs,
573 X509_ASN_ENCODING |
574 PKCS_7_ASN_ENCODING,
575 0, CERT_FIND_HASH,
576 &blob, NULL);
577 }
578 os_free(buf);
579 }
580
581 CertCloseStore(cs, 0);
582
583 return ret;
584}
585
586
587static int tls_cryptoapi_cert(SSL *ssl, const char *name)
588{
589 X509 *cert = NULL;
590 RSA *rsa = NULL, *pub_rsa;
591 struct cryptoapi_rsa_data *priv;
592 RSA_METHOD *rsa_meth;
593
594 if (name == NULL ||
595 (strncmp(name, "cert://", 7) != 0 &&
596 strncmp(name, "hash://", 7) != 0))
597 return -1;
598
599 priv = os_zalloc(sizeof(*priv));
600 rsa_meth = os_zalloc(sizeof(*rsa_meth));
601 if (priv == NULL || rsa_meth == NULL) {
602 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
603 "for CryptoAPI RSA method");
604 os_free(priv);
605 os_free(rsa_meth);
606 return -1;
607 }
608
609 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
610 if (priv->cert == NULL) {
611 priv->cert = cryptoapi_find_cert(
612 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
613 }
614 if (priv->cert == NULL) {
615 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
616 "'%s'", name);
617 goto err;
618 }
619
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800620 cert = d2i_X509(NULL,
621 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 priv->cert->cbCertEncoded);
623 if (cert == NULL) {
624 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
625 "encoding");
626 goto err;
627 }
628
629 if (!CryptAcquireCertificatePrivateKey(priv->cert,
630 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
631 NULL, &priv->crypt_prov,
632 &priv->key_spec,
633 &priv->free_crypt_prov)) {
634 cryptoapi_error("Failed to acquire a private key for the "
635 "certificate");
636 goto err;
637 }
638
639 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
640 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
641 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
642 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
643 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
644 rsa_meth->finish = cryptoapi_finish;
645 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
646 rsa_meth->app_data = (char *) priv;
647
648 rsa = RSA_new();
649 if (rsa == NULL) {
650 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
651 ERR_R_MALLOC_FAILURE);
652 goto err;
653 }
654
655 if (!SSL_use_certificate(ssl, cert)) {
656 RSA_free(rsa);
657 rsa = NULL;
658 goto err;
659 }
660 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
661 X509_free(cert);
662 cert = NULL;
663
664 rsa->n = BN_dup(pub_rsa->n);
665 rsa->e = BN_dup(pub_rsa->e);
666 if (!RSA_set_method(rsa, rsa_meth))
667 goto err;
668
669 if (!SSL_use_RSAPrivateKey(ssl, rsa))
670 goto err;
671 RSA_free(rsa);
672
673 return 0;
674
675err:
676 if (cert)
677 X509_free(cert);
678 if (rsa)
679 RSA_free(rsa);
680 else {
681 os_free(rsa_meth);
682 cryptoapi_free_data(priv);
683 }
684 return -1;
685}
686
687
688static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
689{
690 HCERTSTORE cs;
691 PCCERT_CONTEXT ctx = NULL;
692 X509 *cert;
693 char buf[128];
694 const char *store;
695#ifdef UNICODE
696 WCHAR *wstore;
697#endif /* UNICODE */
698
699 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
700 return -1;
701
702 store = name + 13;
703#ifdef UNICODE
704 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
705 if (wstore == NULL)
706 return -1;
707 wsprintf(wstore, L"%S", store);
708 cs = CertOpenSystemStore(0, wstore);
709 os_free(wstore);
710#else /* UNICODE */
711 cs = CertOpenSystemStore(0, store);
712#endif /* UNICODE */
713 if (cs == NULL) {
714 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
715 "'%s': error=%d", __func__, store,
716 (int) GetLastError());
717 return -1;
718 }
719
720 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800721 cert = d2i_X509(NULL,
722 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 ctx->cbCertEncoded);
724 if (cert == NULL) {
725 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
726 "X509 DER encoding for CA cert");
727 continue;
728 }
729
730 X509_NAME_oneline(X509_get_subject_name(cert), buf,
731 sizeof(buf));
732 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
733 "system certificate store: subject='%s'", buf);
734
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700735 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
736 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 tls_show_errors(MSG_WARNING, __func__,
738 "Failed to add ca_cert to OpenSSL "
739 "certificate store");
740 }
741
742 X509_free(cert);
743 }
744
745 if (!CertCloseStore(cs, 0)) {
746 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
747 "'%s': error=%d", __func__, name + 13,
748 (int) GetLastError());
749 }
750
751 return 0;
752}
753
754
755#else /* CONFIG_NATIVE_WINDOWS */
756
757static int tls_cryptoapi_cert(SSL *ssl, const char *name)
758{
759 return -1;
760}
761
762#endif /* CONFIG_NATIVE_WINDOWS */
763
764
765static void ssl_info_cb(const SSL *ssl, int where, int ret)
766{
767 const char *str;
768 int w;
769
770 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
771 w = where & ~SSL_ST_MASK;
772 if (w & SSL_ST_CONNECT)
773 str = "SSL_connect";
774 else if (w & SSL_ST_ACCEPT)
775 str = "SSL_accept";
776 else
777 str = "undefined";
778
779 if (where & SSL_CB_LOOP) {
780 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
781 str, SSL_state_string_long(ssl));
782 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700783 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
785 where & SSL_CB_READ ?
786 "read (remote end reported an error)" :
787 "write (local SSL3 detected an error)",
788 SSL_alert_type_string_long(ret),
789 SSL_alert_desc_string_long(ret));
790 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 if (where & SSL_CB_READ)
792 conn->read_alerts++;
793 else
794 conn->write_alerts++;
795 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700796 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700798 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799 os_memset(&ev, 0, sizeof(ev));
800 ev.alert.is_local = !(where & SSL_CB_READ);
801 ev.alert.type = SSL_alert_type_string_long(ret);
802 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700803 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 } else if (where & SSL_CB_EXIT && ret <= 0) {
806 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
807 str, ret == 0 ? "failed" : "error",
808 SSL_state_string_long(ssl));
809 }
810}
811
812
813#ifndef OPENSSL_NO_ENGINE
814/**
815 * tls_engine_load_dynamic_generic - load any openssl engine
816 * @pre: an array of commands and values that load an engine initialized
817 * in the engine specific function
818 * @post: an array of commands and values that initialize an already loaded
819 * engine (or %NULL if not required)
820 * @id: the engine id of the engine to load (only required if post is not %NULL
821 *
822 * This function is a generic function that loads any openssl engine.
823 *
824 * Returns: 0 on success, -1 on failure
825 */
826static int tls_engine_load_dynamic_generic(const char *pre[],
827 const char *post[], const char *id)
828{
829 ENGINE *engine;
830 const char *dynamic_id = "dynamic";
831
832 engine = ENGINE_by_id(id);
833 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
835 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700836 /*
837 * If it was auto-loaded by ENGINE_by_id() we might still
838 * need to tell it which PKCS#11 module to use in legacy
839 * (non-p11-kit) environments. Do so now; even if it was
840 * properly initialised before, setting it again will be
841 * harmless.
842 */
843 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 }
845 ERR_clear_error();
846
847 engine = ENGINE_by_id(dynamic_id);
848 if (engine == NULL) {
849 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
850 dynamic_id,
851 ERR_error_string(ERR_get_error(), NULL));
852 return -1;
853 }
854
855 /* Perform the pre commands. This will load the engine. */
856 while (pre && pre[0]) {
857 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
858 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
859 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
860 "%s %s [%s]", pre[0], pre[1],
861 ERR_error_string(ERR_get_error(), NULL));
862 ENGINE_free(engine);
863 return -1;
864 }
865 pre += 2;
866 }
867
868 /*
869 * Free the reference to the "dynamic" engine. The loaded engine can
870 * now be looked up using ENGINE_by_id().
871 */
872 ENGINE_free(engine);
873
874 engine = ENGINE_by_id(id);
875 if (engine == NULL) {
876 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
877 id, ERR_error_string(ERR_get_error(), NULL));
878 return -1;
879 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700880 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 while (post && post[0]) {
882 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
883 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
884 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
885 " %s %s [%s]", post[0], post[1],
886 ERR_error_string(ERR_get_error(), NULL));
887 ENGINE_remove(engine);
888 ENGINE_free(engine);
889 return -1;
890 }
891 post += 2;
892 }
893 ENGINE_free(engine);
894
895 return 0;
896}
897
898
899/**
900 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
901 * @pkcs11_so_path: pksc11_so_path from the configuration
902 * @pcks11_module_path: pkcs11_module_path from the configuration
903 */
904static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
905 const char *pkcs11_module_path)
906{
907 char *engine_id = "pkcs11";
908 const char *pre_cmd[] = {
909 "SO_PATH", NULL /* pkcs11_so_path */,
910 "ID", NULL /* engine_id */,
911 "LIST_ADD", "1",
912 /* "NO_VCHECK", "1", */
913 "LOAD", NULL,
914 NULL, NULL
915 };
916 const char *post_cmd[] = {
917 "MODULE_PATH", NULL /* pkcs11_module_path */,
918 NULL, NULL
919 };
920
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 return 0;
923
924 pre_cmd[1] = pkcs11_so_path;
925 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800926 if (pkcs11_module_path)
927 post_cmd[1] = pkcs11_module_path;
928 else
929 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930
931 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
932 pkcs11_so_path);
933
934 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
935}
936
937
938/**
939 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
940 * @opensc_so_path: opensc_so_path from the configuration
941 */
942static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
943{
944 char *engine_id = "opensc";
945 const char *pre_cmd[] = {
946 "SO_PATH", NULL /* opensc_so_path */,
947 "ID", NULL /* engine_id */,
948 "LIST_ADD", "1",
949 "LOAD", NULL,
950 NULL, NULL
951 };
952
953 if (!opensc_so_path)
954 return 0;
955
956 pre_cmd[1] = opensc_so_path;
957 pre_cmd[3] = engine_id;
958
959 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
960 opensc_so_path);
961
962 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
963}
964#endif /* OPENSSL_NO_ENGINE */
965
966
Sunil Ravia04bd252022-05-02 22:54:18 -0700967static struct tls_session_data * get_session_data(struct tls_context *context,
968 const struct wpabuf *buf)
969{
970 struct tls_session_data *data;
971
972 dl_list_for_each(data, &context->sessions, struct tls_session_data,
973 list) {
974 if (data->buf == buf)
975 return data;
976 }
977
978 return NULL;
979}
980
981
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
983{
984 struct wpabuf *buf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700985 struct tls_context *context;
986 struct tls_session_data *found;
987
988 wpa_printf(MSG_DEBUG,
989 "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess,
990 tls_ex_idx_session);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800991
992 if (tls_ex_idx_session < 0)
993 return;
994 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
995 if (!buf)
996 return;
Sunil Ravia04bd252022-05-02 22:54:18 -0700997
998 context = SSL_CTX_get_app_data(ctx);
999 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
1000 found = get_session_data(context, buf);
1001 if (!found) {
1002 wpa_printf(MSG_DEBUG,
1003 "OpenSSL: Do not free application session data %p (sess %p)",
1004 buf, sess);
1005 return;
1006 }
1007
1008 dl_list_del(&found->list);
1009 os_free(found);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001010 wpa_printf(MSG_DEBUG,
1011 "OpenSSL: Free application session data %p (sess %p)",
1012 buf, sess);
1013 wpabuf_free(buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001014}
1015
1016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017void * tls_init(const struct tls_config *conf)
1018{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001019 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001021 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001022 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023
1024 if (tls_openssl_ref_count == 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001025 void openssl_load_legacy_provider(void);
1026
1027 openssl_load_legacy_provider();
1028
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001029 tls_global = context = tls_context_new(conf);
1030 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032#ifdef CONFIG_FIPS
1033#ifdef OPENSSL_FIPS
1034 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001035 static int fips_enabled = 0;
1036
1037 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
1039 "mode");
1040 ERR_load_crypto_strings();
1041 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001042 os_free(tls_global);
1043 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001045 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001047 fips_enabled = 1;
1048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 }
1050#else /* OPENSSL_FIPS */
1051 if (conf && conf->fips_mode) {
1052 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
1053 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001054 os_free(tls_global);
1055 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 return NULL;
1057 }
1058#endif /* OPENSSL_FIPS */
1059#endif /* CONFIG_FIPS */
Sunil Ravia04bd252022-05-02 22:54:18 -07001060#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 SSL_load_error_strings();
1062 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001063#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 EVP_add_digest(EVP_sha256());
1065#endif /* OPENSSL_NO_SHA256 */
1066 /* TODO: if /dev/urandom is available, PRNG is seeded
1067 * automatically. If this is not the case, random data should
1068 * be added here. */
1069
1070#ifdef PKCS12_FUNCS
1071#ifndef OPENSSL_NO_RC2
1072 /*
1073 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1074 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1075 * versions, but it looks like OpenSSL 1.0.0 does not do that
1076 * anymore.
1077 */
1078 EVP_add_cipher(EVP_rc2_40_cbc());
1079#endif /* OPENSSL_NO_RC2 */
1080 PKCS12_PBE_add();
1081#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001082#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001083 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001084 context = tls_context_new(conf);
1085 if (context == NULL)
1086 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 }
1088 tls_openssl_ref_count++;
1089
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001090 data = os_zalloc(sizeof(*data));
1091 if (data)
1092 ssl = SSL_CTX_new(SSLv23_method());
1093 else
1094 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001095 if (ssl == NULL) {
1096 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001097 if (context != tls_global)
1098 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001099 if (tls_openssl_ref_count == 0) {
1100 os_free(tls_global);
1101 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001102 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001103 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001105 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001106 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001107 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001108 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001109 data->crl_reload_interval = conf->crl_reload_interval;
1110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001112 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1113 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1114
Hai Shalom60840252021-02-19 19:02:11 -08001115 SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY);
1116
Dmitry Shmidt29333592017-01-09 12:27:11 -08001117#ifdef SSL_MODE_NO_AUTO_CHAIN
1118 /* Number of deployed use cases assume the default OpenSSL behavior of
1119 * auto chaining the local certificate is in use. BoringSSL removed this
1120 * functionality by default, so we need to restore it here to avoid
1121 * breaking existing use cases. */
1122 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1123#endif /* SSL_MODE_NO_AUTO_CHAIN */
1124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001126 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001127 if (data->tls_session_lifetime > 0) {
1128 SSL_CTX_set_quiet_shutdown(ssl, 1);
1129 /*
1130 * Set default context here. In practice, this will be replaced
1131 * by the per-EAP method context in tls_connection_set_verify().
1132 */
1133 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1134 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1135 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1136 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
Sunil Ravia04bd252022-05-02 22:54:18 -07001137#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1138 !defined(LIBRESSL_VERSION_NUMBER) && \
1139 !defined(OPENSSL_IS_BORINGSSL)
1140 /* One session ticket is sufficient for EAP-TLS */
1141 SSL_CTX_set_num_tickets(ssl, 1);
1142#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001143 } else {
1144 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
Sunil Ravia04bd252022-05-02 22:54:18 -07001145#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1146 !defined(LIBRESSL_VERSION_NUMBER) && \
1147 !defined(OPENSSL_IS_BORINGSSL)
1148 SSL_CTX_set_num_tickets(ssl, 0);
1149#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001150 }
1151
1152 if (tls_ex_idx_session < 0) {
1153 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1154 0, NULL, NULL, NULL, NULL);
1155 if (tls_ex_idx_session < 0) {
1156 tls_deinit(data);
1157 return NULL;
1158 }
1159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
1161#ifndef OPENSSL_NO_ENGINE
Hai Shalom81f62d82019-07-22 12:10:00 -07001162 wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines");
1163 ENGINE_load_builtin_engines();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 if (conf &&
1166 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1167 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1169 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1170 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001171 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 return NULL;
1173 }
1174 }
1175#endif /* OPENSSL_NO_ENGINE */
1176
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001177 if (conf && conf->openssl_ciphers)
1178 ciphers = conf->openssl_ciphers;
1179 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001180 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001181 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1182 wpa_printf(MSG_ERROR,
1183 "OpenSSL: Failed to set cipher string '%s'",
1184 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001185 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001186 return NULL;
1187 }
1188
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001189 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190}
1191
1192
1193void tls_deinit(void *ssl_ctx)
1194{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001195 struct tls_data *data = ssl_ctx;
1196 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001197 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Sunil Ravia04bd252022-05-02 22:54:18 -07001198 struct tls_session_data *sess_data;
1199
1200 if (data->tls_session_lifetime > 0) {
1201 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions");
1202 SSL_CTX_flush_sessions(ssl, 0);
1203 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done");
1204 }
1205 while ((sess_data = dl_list_first(&context->sessions,
1206 struct tls_session_data, list))) {
1207 wpa_printf(MSG_DEBUG,
1208 "OpenSSL: Freeing not-flushed session data %p",
1209 sess_data->buf);
1210 wpabuf_free(sess_data->buf);
1211 dl_list_del(&sess_data->list);
1212 os_free(sess_data);
1213 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001214 if (context != tls_global)
1215 os_free(context);
Hai Shalom74f70d42019-02-11 14:42:39 -08001216 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 SSL_CTX_free(ssl);
1218
1219 tls_openssl_ref_count--;
1220 if (tls_openssl_ref_count == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001221#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222#ifndef OPENSSL_NO_ENGINE
1223 ENGINE_cleanup();
1224#endif /* OPENSSL_NO_ENGINE */
1225 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001226 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 ERR_free_strings();
1228 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001229#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001230 os_free(tls_global->ocsp_stapling_response);
1231 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 os_free(tls_global);
1233 tls_global = NULL;
1234 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001235
Hai Shalom021b0b52019-04-10 11:17:58 -07001236 os_free(data->check_cert_subject);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001237 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238}
1239
1240
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001241#ifndef OPENSSL_NO_ENGINE
1242
1243/* Cryptoki return values */
1244#define CKR_PIN_INCORRECT 0x000000a0
1245#define CKR_PIN_INVALID 0x000000a1
1246#define CKR_PIN_LEN_RANGE 0x000000a2
1247
1248/* libp11 */
1249#define ERR_LIB_PKCS11 ERR_LIB_USER
1250
1251static int tls_is_pin_error(unsigned int err)
1252{
1253 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1254 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1255 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1256 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1257}
1258
1259#endif /* OPENSSL_NO_ENGINE */
1260
1261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001262#ifdef ANDROID
1263/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1264EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1265#endif /* ANDROID */
1266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1268 const char *pin, const char *key_id,
1269 const char *cert_id, const char *ca_cert_id)
1270{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001271#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1272#if !defined(OPENSSL_NO_ENGINE)
1273#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1274#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001275 if (!key_id)
1276 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001277 conn->engine = NULL;
1278 conn->private_key = EVP_PKEY_from_keystore(key_id);
1279 if (!conn->private_key) {
1280 wpa_printf(MSG_ERROR,
1281 "ENGINE: cannot load private key with id '%s' [%s]",
1282 key_id,
1283 ERR_error_string(ERR_get_error(), NULL));
1284 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1285 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001286#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288#ifndef OPENSSL_NO_ENGINE
1289 int ret = -1;
1290 if (engine_id == NULL) {
1291 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1292 return -1;
1293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294
1295 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001296#ifdef ANDROID
1297 ENGINE_load_dynamic();
1298#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 conn->engine = ENGINE_by_id(engine_id);
1300 if (!conn->engine) {
1301 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1302 engine_id, ERR_error_string(ERR_get_error(), NULL));
1303 goto err;
1304 }
1305 if (ENGINE_init(conn->engine) != 1) {
1306 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1307 "(engine: %s) [%s]", engine_id,
1308 ERR_error_string(ERR_get_error(), NULL));
1309 goto err;
1310 }
1311 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1312
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001313#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001314 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1316 ERR_error_string(ERR_get_error(), NULL));
1317 goto err;
1318 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001319#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001320 if (key_id) {
1321 /*
1322 * Ensure that the ENGINE does not attempt to use the OpenSSL
1323 * UI system to obtain a PIN, if we didn't provide one.
1324 */
1325 struct {
1326 const void *password;
1327 const char *prompt_info;
1328 } key_cb = { "", NULL };
1329
1330 /* load private key first in-case PIN is required for cert */
1331 conn->private_key = ENGINE_load_private_key(conn->engine,
1332 key_id, NULL,
1333 &key_cb);
1334 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001335 unsigned long err = ERR_get_error();
1336
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001337 wpa_printf(MSG_ERROR,
1338 "ENGINE: cannot load private key with id '%s' [%s]",
1339 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001340 ERR_error_string(err, NULL));
1341 if (tls_is_pin_error(err))
1342 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1343 else
1344 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001345 goto err;
1346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 }
1348
1349 /* handle a certificate and/or CA certificate */
1350 if (cert_id || ca_cert_id) {
1351 const char *cmd_name = "LOAD_CERT_CTRL";
1352
1353 /* test if the engine supports a LOAD_CERT_CTRL */
1354 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1355 0, (void *)cmd_name, NULL)) {
1356 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1357 " loading certificates");
1358 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1359 goto err;
1360 }
1361 }
1362
1363 return 0;
1364
1365err:
1366 if (conn->engine) {
1367 ENGINE_free(conn->engine);
1368 conn->engine = NULL;
1369 }
1370
1371 if (conn->private_key) {
1372 EVP_PKEY_free(conn->private_key);
1373 conn->private_key = NULL;
1374 }
1375
1376 return ret;
1377#else /* OPENSSL_NO_ENGINE */
1378 return 0;
1379#endif /* OPENSSL_NO_ENGINE */
1380}
1381
1382
1383static void tls_engine_deinit(struct tls_connection *conn)
1384{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001385#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1387 if (conn->private_key) {
1388 EVP_PKEY_free(conn->private_key);
1389 conn->private_key = NULL;
1390 }
1391 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001392#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001394#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 conn->engine = NULL;
1396 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001397#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398}
1399
1400
1401int tls_get_errors(void *ssl_ctx)
1402{
1403 int count = 0;
1404 unsigned long err;
1405
1406 while ((err = ERR_get_error())) {
1407 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1408 ERR_error_string(err, NULL));
1409 count++;
1410 }
1411
1412 return count;
1413}
1414
Jouni Malinen26af48b2014-04-09 13:02:53 +03001415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416static const char * openssl_content_type(int content_type)
1417{
1418 switch (content_type) {
1419 case 20:
1420 return "change cipher spec";
1421 case 21:
1422 return "alert";
1423 case 22:
1424 return "handshake";
1425 case 23:
1426 return "application data";
1427 case 24:
1428 return "heartbeat";
1429 case 256:
1430 return "TLS header info"; /* pseudo content type */
Hai Shalom81f62d82019-07-22 12:10:00 -07001431 case 257:
1432 return "inner content type"; /* pseudo content type */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001433 default:
1434 return "?";
1435 }
1436}
1437
1438
1439static const char * openssl_handshake_type(int content_type, const u8 *buf,
1440 size_t len)
1441{
Hai Shalom81f62d82019-07-22 12:10:00 -07001442 if (content_type == 257 && buf && len == 1)
1443 return openssl_content_type(buf[0]);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001444 if (content_type != 22 || !buf || len == 0)
1445 return "";
1446 switch (buf[0]) {
1447 case 0:
1448 return "hello request";
1449 case 1:
1450 return "client hello";
1451 case 2:
1452 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001453 case 3:
1454 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001455 case 4:
1456 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001457 case 5:
1458 return "end of early data";
1459 case 6:
1460 return "hello retry request";
1461 case 8:
1462 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001463 case 11:
1464 return "certificate";
1465 case 12:
1466 return "server key exchange";
1467 case 13:
1468 return "certificate request";
1469 case 14:
1470 return "server hello done";
1471 case 15:
1472 return "certificate verify";
1473 case 16:
1474 return "client key exchange";
1475 case 20:
1476 return "finished";
1477 case 21:
1478 return "certificate url";
1479 case 22:
1480 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001481 case 23:
1482 return "supplemental data";
1483 case 24:
1484 return "key update";
1485 case 254:
1486 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001487 default:
1488 return "?";
1489 }
1490}
1491
1492
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001493#ifdef CONFIG_SUITEB
1494
1495static void check_server_hello(struct tls_connection *conn,
1496 const u8 *pos, const u8 *end)
1497{
1498 size_t payload_len, id_len;
1499
1500 /*
1501 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1502 * not make it cleanly available during handshake and we need to know
1503 * whether DHE was selected.
1504 */
1505
1506 if (end - pos < 3)
1507 return;
1508 payload_len = WPA_GET_BE24(pos);
1509 pos += 3;
1510
1511 if ((size_t) (end - pos) < payload_len)
1512 return;
1513 end = pos + payload_len;
1514
1515 /* Skip Version and Random */
1516 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1517 return;
1518 pos += 2 + SSL3_RANDOM_SIZE;
1519
1520 /* Skip Session ID */
1521 if (end - pos < 1)
1522 return;
1523 id_len = *pos++;
1524 if ((size_t) (end - pos) < id_len)
1525 return;
1526 pos += id_len;
1527
1528 if (end - pos < 2)
1529 return;
1530 conn->cipher_suite = WPA_GET_BE16(pos);
1531 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1532 conn->cipher_suite);
1533}
1534
1535
1536static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1537 const u8 *pos, const u8 *end)
1538{
1539 size_t payload_len;
1540 u16 dh_len;
1541 BIGNUM *p;
1542 int bits;
1543
1544 if (!(conn->flags & TLS_CONN_SUITEB))
1545 return;
1546
1547 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1548 if (conn->cipher_suite != 0x9f)
1549 return;
1550
1551 if (end - pos < 3)
1552 return;
1553 payload_len = WPA_GET_BE24(pos);
1554 pos += 3;
1555
1556 if ((size_t) (end - pos) < payload_len)
1557 return;
1558 end = pos + payload_len;
1559
1560 if (end - pos < 2)
1561 return;
1562 dh_len = WPA_GET_BE16(pos);
1563 pos += 2;
1564
1565 if ((size_t) (end - pos) < dh_len)
1566 return;
1567 p = BN_bin2bn(pos, dh_len, NULL);
1568 if (!p)
1569 return;
1570
1571 bits = BN_num_bits(p);
1572 BN_free(p);
1573
1574 conn->server_dh_prime_len = bits;
1575 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1576 conn->server_dh_prime_len);
1577}
1578
1579#endif /* CONFIG_SUITEB */
1580
1581
Jouni Malinen26af48b2014-04-09 13:02:53 +03001582static void tls_msg_cb(int write_p, int version, int content_type,
1583 const void *buf, size_t len, SSL *ssl, void *arg)
1584{
1585 struct tls_connection *conn = arg;
1586 const u8 *pos = buf;
1587
Sunil8cd6f4d2022-06-28 18:40:46 +00001588#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1589 if ((SSL_version(ssl) == TLS1_VERSION ||
1590 SSL_version(ssl) == TLS1_1_VERSION) &&
1591 SSL_get_security_level(ssl) > 0) {
1592 wpa_printf(MSG_DEBUG,
1593 "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm");
1594 SSL_set_security_level(ssl, 0);
1595 }
1596#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001597 if (write_p == 2) {
1598 wpa_printf(MSG_DEBUG,
1599 "OpenSSL: session ver=0x%x content_type=%d",
1600 version, content_type);
1601 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1602 return;
1603 }
1604
1605 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1606 write_p ? "TX" : "RX", version, content_type,
1607 openssl_content_type(content_type),
1608 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001609 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1610 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1611 size_t payload_len = WPA_GET_BE16(pos + 1);
1612 if (payload_len + 3 > len) {
1613 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1614 conn->invalid_hb_used = 1;
1615 }
1616 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001617
1618#ifdef CONFIG_SUITEB
1619 /*
1620 * Need to parse these handshake messages to be able to check DH prime
1621 * length since OpenSSL does not expose the new cipher suite and DH
1622 * parameters during handshake (e.g., for cert_cb() callback).
1623 */
1624 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1625 check_server_hello(conn, pos + 1, pos + len);
1626 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1627 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1628#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001629}
1630
1631
Sunil Ravia04bd252022-05-02 22:54:18 -07001632#ifdef CONFIG_TESTING_OPTIONS
1633#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1634/*
1635 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1636 * material will be exported that you may use with Wireshark to decode any
1637 * TLS flows. Please see the following for more details:
1638 *
1639 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1640 *
1641 * Example logging sessions are (you should delete the file on each run):
1642 *
1643 * rm -f /tmp/sslkey.log
1644 * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ...
1645 *
1646 * rm -f /tmp/sslkey.log
1647 * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ...
1648 *
1649 * rm -f /tmp/sslkey.log
1650 * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ...
1651 */
1652static void tls_keylog_cb(const SSL *ssl, const char *line)
1653{
1654 int fd;
1655 const char *filename;
1656 struct iovec iov[2];
1657
1658 filename = getenv("SSLKEYLOGFILE");
1659 if (!filename)
1660 return;
1661
1662 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1663 if (fd < 0) {
1664 wpa_printf(MSG_ERROR,
1665 "OpenSSL: Failed to open keylog file %s: %s",
1666 filename, strerror(errno));
1667 return;
1668 }
1669
1670 /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed
1671 * to be atomic for O_APPEND. */
1672 iov[0].iov_base = (void *) line;
1673 iov[0].iov_len = os_strlen(line);
1674 iov[1].iov_base = "\n";
1675 iov[1].iov_len = 1;
1676
1677 if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) {
1678 wpa_printf(MSG_DEBUG,
1679 "OpenSSL: Failed to write to keylog file %s: %s",
1680 filename, strerror(errno));
1681 }
1682
1683 close(fd);
1684}
1685#endif
1686#endif /* CONFIG_TESTING_OPTIONS */
1687
1688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689struct tls_connection * tls_connection_init(void *ssl_ctx)
1690{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001691 struct tls_data *data = ssl_ctx;
1692 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 struct tls_connection *conn;
1694 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001695 X509_STORE *new_cert_store;
1696 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001697 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698
Hai Shalom74f70d42019-02-11 14:42:39 -08001699 /* Replace X509 store if it is time to update CRL. */
1700 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1701 os_reltime_expired(&now, &data->crl_last_reload,
1702 data->crl_reload_interval)) {
1703 wpa_printf(MSG_INFO,
1704 "OpenSSL: Flushing X509 store with ca_cert file");
1705 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1706 data->check_crl);
1707 if (!new_cert_store) {
1708 wpa_printf(MSG_ERROR,
1709 "OpenSSL: Error replacing X509 store with ca_cert file");
1710 } else {
1711 /* Replace old store */
1712 SSL_CTX_set_cert_store(ssl, new_cert_store);
1713 data->crl_last_reload = now;
1714 }
1715 }
1716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 conn = os_zalloc(sizeof(*conn));
1718 if (conn == NULL)
1719 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001720 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001721 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 conn->ssl = SSL_new(ssl);
1723 if (conn->ssl == NULL) {
1724 tls_show_errors(MSG_INFO, __func__,
1725 "Failed to initialize new SSL connection");
1726 os_free(conn);
1727 return NULL;
1728 }
1729
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001730 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001732 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1733 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1735 SSL_OP_SINGLE_DH_USE;
1736#ifdef SSL_OP_NO_COMPRESSION
1737 options |= SSL_OP_NO_COMPRESSION;
1738#endif /* SSL_OP_NO_COMPRESSION */
1739 SSL_set_options(conn->ssl, options);
Hai Shalom81f62d82019-07-22 12:10:00 -07001740#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
1741 /* Hopefully there is no need for middlebox compatibility mechanisms
1742 * when going through EAP authentication. */
1743 SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
1744#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745
Sunil Ravia04bd252022-05-02 22:54:18 -07001746#ifdef CONFIG_TESTING_OPTIONS
1747#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1748 /* Set the keylog file if the admin requested it. */
1749 if (getenv("SSLKEYLOGFILE"))
1750 SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb);
1751#endif
1752#endif /* CONFIG_TESTING_OPTIONS */
1753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 conn->ssl_in = BIO_new(BIO_s_mem());
1755 if (!conn->ssl_in) {
1756 tls_show_errors(MSG_INFO, __func__,
1757 "Failed to create a new BIO for ssl_in");
1758 SSL_free(conn->ssl);
1759 os_free(conn);
1760 return NULL;
1761 }
1762
1763 conn->ssl_out = BIO_new(BIO_s_mem());
1764 if (!conn->ssl_out) {
1765 tls_show_errors(MSG_INFO, __func__,
1766 "Failed to create a new BIO for ssl_out");
1767 SSL_free(conn->ssl);
1768 BIO_free(conn->ssl_in);
1769 os_free(conn);
1770 return NULL;
1771 }
1772
1773 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1774
1775 return conn;
1776}
1777
1778
1779void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1780{
1781 if (conn == NULL)
1782 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001783 if (conn->success_data) {
1784 /*
1785 * Make sure ssl_clear_bad_session() does not remove this
1786 * session.
1787 */
1788 SSL_set_quiet_shutdown(conn->ssl, 1);
1789 SSL_shutdown(conn->ssl);
1790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 SSL_free(conn->ssl);
1792 tls_engine_deinit(conn);
1793 os_free(conn->subject_match);
1794 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001795 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001796 os_free(conn->domain_match);
Hai Shalom021b0b52019-04-10 11:17:58 -07001797 os_free(conn->check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 os_free(conn->session_ticket);
Hai Shalom899fcc72020-10-19 14:38:18 -07001799 os_free(conn->peer_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 os_free(conn);
1801}
1802
1803
1804int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1805{
1806 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1807}
1808
1809
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001810char * tls_connection_peer_serial_num(void *tls_ctx,
1811 struct tls_connection *conn)
1812{
1813 ASN1_INTEGER *ser;
1814 char *serial_num;
1815 size_t len;
1816
1817 if (!conn->peer_cert)
1818 return NULL;
1819
1820 ser = X509_get_serialNumber(conn->peer_cert);
1821 if (!ser)
1822 return NULL;
1823
1824 len = ASN1_STRING_length(ser) * 2 + 1;
1825 serial_num = os_malloc(len);
1826 if (!serial_num)
1827 return NULL;
1828 wpa_snprintf_hex_uppercase(serial_num, len,
1829 ASN1_STRING_get0_data(ser),
1830 ASN1_STRING_length(ser));
1831 return serial_num;
1832}
1833
1834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1836{
1837 if (conn == NULL)
1838 return -1;
1839
1840 /* Shutdown previous TLS connection without notifying the peer
1841 * because the connection was already terminated in practice
1842 * and "close notify" shutdown alert would confuse AS. */
1843 SSL_set_quiet_shutdown(conn->ssl, 1);
1844 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001845 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846}
1847
1848
1849static int tls_match_altsubject_component(X509 *cert, int type,
1850 const char *value, size_t len)
1851{
1852 GENERAL_NAME *gen;
1853 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001854 int found = 0;
1855 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856
1857 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1858
1859 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1860 gen = sk_GENERAL_NAME_value(ext, i);
1861 if (gen->type != type)
1862 continue;
1863 if (os_strlen((char *) gen->d.ia5->data) == len &&
1864 os_memcmp(value, gen->d.ia5->data, len) == 0)
1865 found++;
1866 }
1867
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001868 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return found;
1871}
1872
1873
1874static int tls_match_altsubject(X509 *cert, const char *match)
1875{
1876 int type;
1877 const char *pos, *end;
1878 size_t len;
1879
1880 pos = match;
1881 do {
1882 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1883 type = GEN_EMAIL;
1884 pos += 6;
1885 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1886 type = GEN_DNS;
1887 pos += 4;
1888 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1889 type = GEN_URI;
1890 pos += 4;
1891 } else {
1892 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1893 "match '%s'", pos);
1894 return 0;
1895 }
1896 end = os_strchr(pos, ';');
1897 while (end) {
1898 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1899 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1900 os_strncmp(end + 1, "URI:", 4) == 0)
1901 break;
1902 end = os_strchr(end + 1, ';');
1903 }
1904 if (end)
1905 len = end - pos;
1906 else
1907 len = os_strlen(pos);
1908 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1909 return 1;
1910 pos = end + 1;
1911 } while (end);
1912
1913 return 0;
1914}
1915
1916
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001917#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001918static int domain_suffix_match(const u8 *val, size_t len, const char *match,
Hai Shalom021b0b52019-04-10 11:17:58 -07001919 size_t match_len, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001920{
Hai Shalom021b0b52019-04-10 11:17:58 -07001921 size_t i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001922
1923 /* Check for embedded nuls that could mess up suffix matching */
1924 for (i = 0; i < len; i++) {
1925 if (val[i] == '\0') {
1926 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1927 return 0;
1928 }
1929 }
1930
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001931 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001932 return 0;
1933
1934 if (os_strncasecmp((const char *) val + len - match_len, match,
1935 match_len) != 0)
1936 return 0; /* no match */
1937
1938 if (match_len == len)
1939 return 1; /* exact match */
1940
1941 if (val[len - match_len - 1] == '.')
1942 return 1; /* full label match completes suffix match */
1943
1944 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1945 return 0;
1946}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001947#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001948
1949
Hai Shalom021b0b52019-04-10 11:17:58 -07001950struct tls_dn_field_order_cnt {
1951 u8 cn;
1952 u8 c;
1953 u8 l;
1954 u8 st;
1955 u8 o;
1956 u8 ou;
1957 u8 email;
1958};
1959
1960
1961static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt,
1962 int nid)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001963{
Hai Shalom021b0b52019-04-10 11:17:58 -07001964 switch (nid) {
1965 case NID_commonName:
1966 return dn_cnt->cn;
1967 case NID_countryName:
1968 return dn_cnt->c;
1969 case NID_localityName:
1970 return dn_cnt->l;
1971 case NID_stateOrProvinceName:
1972 return dn_cnt->st;
1973 case NID_organizationName:
1974 return dn_cnt->o;
1975 case NID_organizationalUnitName:
1976 return dn_cnt->ou;
1977 case NID_pkcs9_emailAddress:
1978 return dn_cnt->email;
1979 default:
1980 wpa_printf(MSG_ERROR,
1981 "TLS: Unknown NID '%d' in check_cert_subject",
1982 nid);
1983 return -1;
1984 }
1985}
1986
1987
1988/**
1989 * match_dn_field - Match configuration DN field against Certificate DN field
1990 * @cert: Certificate
1991 * @nid: NID of DN field
1992 * @field: Field name
1993 * @value DN field value which is passed from configuration
1994 * e.g., if configuration have C=US and this argument will point to US.
1995 * @dn_cnt: DN matching context
1996 * Returns: 1 on success and 0 on failure
1997 */
1998static int match_dn_field(const X509 *cert, int nid, const char *field,
1999 const char *value,
2000 const struct tls_dn_field_order_cnt *dn_cnt)
2001{
2002 int i, ret = 0, len, config_dn_field_index, match_index = 0;
2003 X509_NAME *name;
2004
2005 len = os_strlen(value);
2006 name = X509_get_subject_name((X509 *) cert);
2007
2008 /* Assign incremented cnt for every field of DN to check DN field in
2009 * right order */
2010 config_dn_field_index = get_dn_field_index(dn_cnt, nid);
2011 if (config_dn_field_index < 0)
2012 return 0;
2013
2014 /* Fetch value based on NID */
2015 for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) {
2016 X509_NAME_ENTRY *e;
2017 ASN1_STRING *cn;
2018
2019 e = X509_NAME_get_entry(name, i);
2020 if (!e)
2021 continue;
2022
2023 cn = X509_NAME_ENTRY_get_data(e);
2024 if (!cn)
2025 continue;
2026
2027 match_index++;
2028
2029 /* check for more than one DN field with same name */
2030 if (match_index != config_dn_field_index)
2031 continue;
2032
2033 /* Check wildcard at the right end side */
2034 /* E.g., if OU=develop* mentioned in configuration, allow 'OU'
2035 * of the subject in the client certificate to start with
2036 * 'develop' */
2037 if (len > 0 && value[len - 1] == '*') {
2038 /* Compare actual certificate DN field value with
2039 * configuration DN field value up to the specified
2040 * length. */
2041 ret = ASN1_STRING_length(cn) >= len - 1 &&
2042 os_memcmp(ASN1_STRING_get0_data(cn), value,
2043 len - 1) == 0;
2044 } else {
2045 /* Compare actual certificate DN field value with
2046 * configuration DN field value */
2047 ret = ASN1_STRING_length(cn) == len &&
2048 os_memcmp(ASN1_STRING_get0_data(cn), value,
2049 len) == 0;
2050 }
2051 if (!ret) {
2052 wpa_printf(MSG_ERROR,
2053 "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'",
2054 field, value, ASN1_STRING_get0_data(cn));
2055 }
2056 break;
2057 }
2058
2059 return ret;
2060}
2061
2062
2063/**
2064 * get_value_from_field - Get value from DN field
2065 * @cert: Certificate
2066 * @field_str: DN field string which is passed from configuration file (e.g.,
2067 * C=US)
2068 * @dn_cnt: DN matching context
2069 * Returns: 1 on success and 0 on failure
2070 */
2071static int get_value_from_field(const X509 *cert, char *field_str,
2072 struct tls_dn_field_order_cnt *dn_cnt)
2073{
2074 int nid;
2075 char *context = NULL, *name, *value;
2076
2077 if (os_strcmp(field_str, "*") == 0)
2078 return 1; /* wildcard matches everything */
2079
2080 name = str_token(field_str, "=", &context);
2081 if (!name)
2082 return 0;
2083
2084 /* Compare all configured DN fields and assign nid based on that to
2085 * fetch correct value from certificate subject */
2086 if (os_strcmp(name, "CN") == 0) {
2087 nid = NID_commonName;
2088 dn_cnt->cn++;
2089 } else if(os_strcmp(name, "C") == 0) {
2090 nid = NID_countryName;
2091 dn_cnt->c++;
2092 } else if (os_strcmp(name, "L") == 0) {
2093 nid = NID_localityName;
2094 dn_cnt->l++;
2095 } else if (os_strcmp(name, "ST") == 0) {
2096 nid = NID_stateOrProvinceName;
2097 dn_cnt->st++;
2098 } else if (os_strcmp(name, "O") == 0) {
2099 nid = NID_organizationName;
2100 dn_cnt->o++;
2101 } else if (os_strcmp(name, "OU") == 0) {
2102 nid = NID_organizationalUnitName;
2103 dn_cnt->ou++;
2104 } else if (os_strcmp(name, "emailAddress") == 0) {
2105 nid = NID_pkcs9_emailAddress;
2106 dn_cnt->email++;
2107 } else {
2108 wpa_printf(MSG_ERROR,
2109 "TLS: Unknown field '%s' in check_cert_subject", name);
2110 return 0;
2111 }
2112
2113 value = str_token(field_str, "=", &context);
2114 if (!value) {
2115 wpa_printf(MSG_ERROR,
2116 "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject",
2117 name);
2118 return 0;
2119 }
2120
2121 return match_dn_field(cert, nid, name, value, dn_cnt);
2122}
2123
2124
2125/**
2126 * tls_match_dn_field - Match subject DN field with check_cert_subject
2127 * @cert: Certificate
2128 * @match: check_cert_subject string
2129 * Returns: Return 1 on success and 0 on failure
2130*/
2131static int tls_match_dn_field(X509 *cert, const char *match)
2132{
2133 const char *token, *last = NULL;
2134 char field[256];
2135 struct tls_dn_field_order_cnt dn_cnt;
2136
2137 os_memset(&dn_cnt, 0, sizeof(dn_cnt));
2138
2139 /* Maximum length of each DN field is 255 characters */
2140
2141 /* Process each '/' delimited field */
2142 while ((token = cstr_token(match, "/", &last))) {
2143 if (last - token >= (int) sizeof(field)) {
2144 wpa_printf(MSG_ERROR,
2145 "OpenSSL: Too long DN matching field value in '%s'",
2146 match);
2147 return 0;
2148 }
2149 os_memcpy(field, token, last - token);
2150 field[last - token] = '\0';
2151
2152 if (!get_value_from_field(cert, field, &dn_cnt)) {
2153 wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'",
2154 field);
2155 return 0;
2156 }
2157 }
2158
2159 return 1;
2160}
2161
2162
2163#ifndef CONFIG_NATIVE_WINDOWS
2164static int tls_match_suffix_helper(X509 *cert, const char *match,
2165 size_t match_len, int full)
2166{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002167 GENERAL_NAME *gen;
2168 void *ext;
2169 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002170 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002171 int dns_name = 0;
2172 X509_NAME *name;
2173
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002174 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
2175 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002176
2177 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
2178
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002179 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
2180 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002181 if (gen->type != GEN_DNS)
2182 continue;
2183 dns_name++;
2184 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
2185 gen->d.dNSName->data,
2186 gen->d.dNSName->length);
2187 if (domain_suffix_match(gen->d.dNSName->data,
Hai Shalom021b0b52019-04-10 11:17:58 -07002188 gen->d.dNSName->length,
2189 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002190 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
2191 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002192 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002193 return 1;
2194 }
2195 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002196 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002197
2198 if (dns_name) {
2199 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
2200 return 0;
2201 }
2202
2203 name = X509_get_subject_name(cert);
2204 i = -1;
2205 for (;;) {
2206 X509_NAME_ENTRY *e;
2207 ASN1_STRING *cn;
2208
2209 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
2210 if (i == -1)
2211 break;
2212 e = X509_NAME_get_entry(name, i);
2213 if (e == NULL)
2214 continue;
2215 cn = X509_NAME_ENTRY_get_data(e);
2216 if (cn == NULL)
2217 continue;
2218 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
2219 cn->data, cn->length);
Hai Shalom021b0b52019-04-10 11:17:58 -07002220 if (domain_suffix_match(cn->data, cn->length,
2221 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002222 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
2223 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002224 return 1;
2225 }
2226 }
2227
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002228 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
2229 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002230 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07002231}
2232#endif /* CONFIG_NATIVE_WINDOWS */
2233
2234
2235static int tls_match_suffix(X509 *cert, const char *match, int full)
2236{
2237#ifdef CONFIG_NATIVE_WINDOWS
2238 /* wincrypt.h has conflicting X509_NAME definition */
2239 return -1;
2240#else /* CONFIG_NATIVE_WINDOWS */
2241 const char *token, *last = NULL;
2242
2243 /* Process each match alternative separately until a match is found */
2244 while ((token = cstr_token(match, ";", &last))) {
2245 if (tls_match_suffix_helper(cert, token, last - token, full))
2246 return 1;
2247 }
2248
2249 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08002250#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002251}
2252
2253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002254static enum tls_fail_reason openssl_tls_fail_reason(int err)
2255{
2256 switch (err) {
2257 case X509_V_ERR_CERT_REVOKED:
2258 return TLS_FAIL_REVOKED;
2259 case X509_V_ERR_CERT_NOT_YET_VALID:
2260 case X509_V_ERR_CRL_NOT_YET_VALID:
2261 return TLS_FAIL_NOT_YET_VALID;
2262 case X509_V_ERR_CERT_HAS_EXPIRED:
2263 case X509_V_ERR_CRL_HAS_EXPIRED:
2264 return TLS_FAIL_EXPIRED;
2265 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
2266 case X509_V_ERR_UNABLE_TO_GET_CRL:
2267 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
2268 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2269 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2270 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2271 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2272 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
2273 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
2274 case X509_V_ERR_INVALID_CA:
2275 return TLS_FAIL_UNTRUSTED;
2276 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
2277 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
2278 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
2279 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
2280 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
2281 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
2282 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
2283 case X509_V_ERR_CERT_UNTRUSTED:
2284 case X509_V_ERR_CERT_REJECTED:
2285 return TLS_FAIL_BAD_CERTIFICATE;
2286 default:
2287 return TLS_FAIL_UNSPECIFIED;
2288 }
2289}
2290
2291
2292static struct wpabuf * get_x509_cert(X509 *cert)
2293{
2294 struct wpabuf *buf;
2295 u8 *tmp;
2296
2297 int cert_len = i2d_X509(cert, NULL);
2298 if (cert_len <= 0)
2299 return NULL;
2300
2301 buf = wpabuf_alloc(cert_len);
2302 if (buf == NULL)
2303 return NULL;
2304
2305 tmp = wpabuf_put(buf, cert_len);
2306 i2d_X509(cert, &tmp);
2307 return buf;
2308}
2309
2310
2311static void openssl_tls_fail_event(struct tls_connection *conn,
2312 X509 *err_cert, int err, int depth,
2313 const char *subject, const char *err_str,
2314 enum tls_fail_reason reason)
2315{
2316 union tls_event_data ev;
2317 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002318 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319
Pavel Grafov4d8552e2018-02-06 11:28:29 +00002320#ifdef ANDROID
2321 log_cert_validation_failure(err_str);
2322#endif
2323
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002324 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 return;
2326
2327 cert = get_x509_cert(err_cert);
2328 os_memset(&ev, 0, sizeof(ev));
2329 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
2330 reason : openssl_tls_fail_reason(err);
2331 ev.cert_fail.depth = depth;
2332 ev.cert_fail.subject = subject;
2333 ev.cert_fail.reason_txt = err_str;
2334 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002335 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 wpabuf_free(cert);
2337}
2338
2339
Hai Shalom81f62d82019-07-22 12:10:00 -07002340static int openssl_cert_tod(X509 *cert)
2341{
2342 CERTIFICATEPOLICIES *ext;
2343 stack_index_t i;
2344 char buf[100];
2345 int res;
2346 int tod = 0;
2347
2348 ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL);
2349 if (!ext)
2350 return 0;
2351
2352 for (i = 0; i < sk_POLICYINFO_num(ext); i++) {
2353 POLICYINFO *policy;
2354
2355 policy = sk_POLICYINFO_value(ext, i);
2356 res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0);
2357 if (res < 0 || (size_t) res >= sizeof(buf))
2358 continue;
2359 wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf);
2360 if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0)
Hai Shalomc3565922019-10-28 11:58:20 -07002361 tod = 1; /* TOD-STRICT */
2362 else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod)
2363 tod = 2; /* TOD-TOFU */
Hai Shalom81f62d82019-07-22 12:10:00 -07002364 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002365 sk_POLICYINFO_pop_free(ext, POLICYINFO_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002366
2367 return tod;
2368}
2369
2370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371static void openssl_tls_cert_event(struct tls_connection *conn,
2372 X509 *err_cert, int depth,
2373 const char *subject)
2374{
2375 struct wpabuf *cert = NULL;
2376 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002377 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002378 char *altsubject[TLS_MAX_ALT_SUBJECT];
2379 int alt, num_altsubject = 0;
2380 GENERAL_NAME *gen;
2381 void *ext;
2382 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002383 ASN1_INTEGER *ser;
2384 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385#ifdef CONFIG_SHA256
2386 u8 hash[32];
2387#endif /* CONFIG_SHA256 */
2388
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002389 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390 return;
2391
2392 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002393 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
2394 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 cert = get_x509_cert(err_cert);
2396 ev.peer_cert.cert = cert;
2397 }
2398#ifdef CONFIG_SHA256
2399 if (cert) {
2400 const u8 *addr[1];
2401 size_t len[1];
2402 addr[0] = wpabuf_head(cert);
2403 len[0] = wpabuf_len(cert);
2404 if (sha256_vector(1, addr, len, hash) == 0) {
2405 ev.peer_cert.hash = hash;
2406 ev.peer_cert.hash_len = sizeof(hash);
2407 }
2408 }
2409#endif /* CONFIG_SHA256 */
2410 ev.peer_cert.depth = depth;
2411 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002412
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002413 ser = X509_get_serialNumber(err_cert);
2414 if (ser) {
2415 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
2416 ASN1_STRING_get0_data(ser),
2417 ASN1_STRING_length(ser));
2418 ev.peer_cert.serial_num = serial_num;
2419 }
2420
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002421 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
2422 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
2423 char *pos;
2424
2425 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
2426 break;
2427 gen = sk_GENERAL_NAME_value(ext, i);
2428 if (gen->type != GEN_EMAIL &&
2429 gen->type != GEN_DNS &&
2430 gen->type != GEN_URI)
2431 continue;
2432
2433 pos = os_malloc(10 + gen->d.ia5->length + 1);
2434 if (pos == NULL)
2435 break;
2436 altsubject[num_altsubject++] = pos;
2437
2438 switch (gen->type) {
2439 case GEN_EMAIL:
2440 os_memcpy(pos, "EMAIL:", 6);
2441 pos += 6;
2442 break;
2443 case GEN_DNS:
2444 os_memcpy(pos, "DNS:", 4);
2445 pos += 4;
2446 break;
2447 case GEN_URI:
2448 os_memcpy(pos, "URI:", 4);
2449 pos += 4;
2450 break;
2451 }
2452
2453 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2454 pos += gen->d.ia5->length;
2455 *pos = '\0';
2456 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002457 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002458
2459 for (alt = 0; alt < num_altsubject; alt++)
2460 ev.peer_cert.altsubject[alt] = altsubject[alt];
2461 ev.peer_cert.num_altsubject = num_altsubject;
2462
Hai Shalom81f62d82019-07-22 12:10:00 -07002463 ev.peer_cert.tod = openssl_cert_tod(err_cert);
2464
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002465 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002467 for (alt = 0; alt < num_altsubject; alt++)
2468 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469}
2470
2471
Hai Shalomc3565922019-10-28 11:58:20 -07002472static void debug_print_cert(X509 *cert, const char *title)
2473{
2474#ifndef CONFIG_NO_STDOUT_DEBUG
2475 BIO *out;
2476 size_t rlen;
2477 char *txt;
2478 int res;
2479
2480 if (wpa_debug_level > MSG_DEBUG)
2481 return;
2482
2483 out = BIO_new(BIO_s_mem());
2484 if (!out)
2485 return;
2486
2487 X509_print(out, cert);
2488 rlen = BIO_ctrl_pending(out);
2489 txt = os_malloc(rlen + 1);
2490 if (txt) {
2491 res = BIO_read(out, txt, rlen);
2492 if (res > 0) {
2493 txt[res] = '\0';
2494 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
2495 }
2496 os_free(txt);
2497 }
2498
2499 BIO_free(out);
2500#endif /* CONFIG_NO_STDOUT_DEBUG */
2501}
2502
2503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2505{
2506 char buf[256];
2507 X509 *err_cert;
2508 int err, depth;
2509 SSL *ssl;
2510 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002511 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002512 char *match, *altmatch, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -07002513 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 const char *err_str;
2515
2516 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002517 if (!err_cert)
2518 return 0;
2519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 err = X509_STORE_CTX_get_error(x509_ctx);
2521 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2522 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2523 SSL_get_ex_data_X509_STORE_CTX_idx());
Hai Shalomc3565922019-10-28 11:58:20 -07002524 os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth);
2525 debug_print_cert(err_cert, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2527
2528 conn = SSL_get_app_data(ssl);
2529 if (conn == NULL)
2530 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002531
2532 if (depth == 0)
2533 conn->peer_cert = err_cert;
2534 else if (depth == 1)
2535 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 else if (depth == 2)
2537 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002538
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002539 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 match = conn->subject_match;
2541 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002542 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002543 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544
2545 if (!preverify_ok && !conn->ca_cert_verify)
2546 preverify_ok = 1;
2547 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2548 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002549 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2550 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2551 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2552 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2553 "time mismatch");
2554 preverify_ok = 1;
2555 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002556 if (!preverify_ok && !conn->data->check_crl_strict &&
2557 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2558 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2559 wpa_printf(MSG_DEBUG,
2560 "OpenSSL: Ignore certificate validity CRL time mismatch");
2561 preverify_ok = 1;
2562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
2564 err_str = X509_verify_cert_error_string(err);
2565
2566#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002567 /*
2568 * Do not require preverify_ok so we can explicity allow otherwise
2569 * invalid pinned server certificates.
2570 */
2571 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 struct wpabuf *cert;
2573 cert = get_x509_cert(err_cert);
2574 if (!cert) {
2575 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2576 "server certificate data");
2577 preverify_ok = 0;
2578 } else {
2579 u8 hash[32];
2580 const u8 *addr[1];
2581 size_t len[1];
2582 addr[0] = wpabuf_head(cert);
2583 len[0] = wpabuf_len(cert);
2584 if (sha256_vector(1, addr, len, hash) < 0 ||
2585 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2586 err_str = "Server certificate mismatch";
2587 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2588 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002589 } else if (!preverify_ok) {
2590 /*
2591 * Certificate matches pinned certificate, allow
2592 * regardless of other problems.
2593 */
2594 wpa_printf(MSG_DEBUG,
2595 "OpenSSL: Ignore validation issues for a pinned server certificate");
2596 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 }
2598 wpabuf_free(cert);
2599 }
2600 }
2601#endif /* CONFIG_SHA256 */
2602
Hai Shalom81f62d82019-07-22 12:10:00 -07002603 openssl_tls_cert_event(conn, err_cert, depth, buf);
2604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 if (!preverify_ok) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002606 if (depth > 0) {
2607 /* Send cert event for the peer certificate so that
2608 * the upper layers get information about it even if
2609 * validation of a CA certificate fails. */
2610 STACK_OF(X509) *chain;
2611
2612 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2613 if (chain && sk_X509_num(chain) > 0) {
2614 char buf2[256];
2615 X509 *cert;
2616
2617 cert = sk_X509_value(chain, 0);
2618 X509_NAME_oneline(X509_get_subject_name(cert),
2619 buf2, sizeof(buf2));
2620
2621 openssl_tls_cert_event(conn, cert, 0, buf2);
2622 }
2623 if (chain)
2624 sk_X509_pop_free(chain, X509_free);
2625 }
2626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2628 " error %d (%s) depth %d for '%s'", err, err_str,
2629 depth, buf);
2630 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2631 err_str, TLS_FAIL_UNSPECIFIED);
2632 return preverify_ok;
2633 }
2634
2635 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2636 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2637 preverify_ok, err, err_str,
2638 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002639 check_cert_subject = conn->check_cert_subject;
2640 if (!check_cert_subject)
2641 check_cert_subject = conn->data->check_cert_subject;
2642 if (check_cert_subject) {
2643 if (depth == 0 &&
2644 !tls_match_dn_field(err_cert, check_cert_subject)) {
2645 preverify_ok = 0;
2646 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2647 "Distinguished Name",
2648 TLS_FAIL_DN_MISMATCH);
2649 }
2650 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2652 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2653 "match with '%s'", buf, match);
2654 preverify_ok = 0;
2655 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2656 "Subject mismatch",
2657 TLS_FAIL_SUBJECT_MISMATCH);
2658 } else if (depth == 0 && altmatch &&
2659 !tls_match_altsubject(err_cert, altmatch)) {
2660 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2661 "'%s' not found", altmatch);
2662 preverify_ok = 0;
2663 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2664 "AltSubject mismatch",
2665 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002666 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002667 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002668 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2669 suffix_match);
2670 preverify_ok = 0;
2671 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2672 "Domain suffix mismatch",
2673 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002674 } else if (depth == 0 && domain_match &&
2675 !tls_match_suffix(err_cert, domain_match, 1)) {
2676 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2677 domain_match);
2678 preverify_ok = 0;
2679 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2680 "Domain mismatch",
2681 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683
2684 if (conn->cert_probe && preverify_ok && depth == 0) {
2685 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2686 "on probe-only run");
2687 preverify_ok = 0;
2688 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2689 "Server certificate chain probe",
2690 TLS_FAIL_SERVER_CHAIN_PROBE);
2691 }
2692
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002693#ifdef CONFIG_SUITEB
2694 if (conn->flags & TLS_CONN_SUITEB) {
2695 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002696 int len = -1;
2697
2698 pk = X509_get_pubkey(err_cert);
2699 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002700 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002701 EVP_PKEY_free(pk);
2702 }
2703
2704 if (len >= 0) {
2705 wpa_printf(MSG_DEBUG,
2706 "OpenSSL: RSA modulus size: %d bits", len);
2707 if (len < 3072) {
2708 preverify_ok = 0;
2709 openssl_tls_fail_event(
2710 conn, err_cert, err,
2711 depth, buf,
2712 "Insufficient RSA modulus size",
2713 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2714 }
2715 }
2716 }
2717#endif /* CONFIG_SUITEB */
2718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002719#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002720 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2721 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002722 enum ocsp_result res;
2723
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002724 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2725 conn->peer_issuer,
2726 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002727 if (res == OCSP_REVOKED) {
2728 preverify_ok = 0;
2729 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2730 "certificate revoked",
2731 TLS_FAIL_REVOKED);
2732 if (err == X509_V_OK)
2733 X509_STORE_CTX_set_error(
2734 x509_ctx, X509_V_ERR_CERT_REVOKED);
2735 } else if (res != OCSP_GOOD &&
2736 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2737 preverify_ok = 0;
2738 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2739 "bad certificate status response",
2740 TLS_FAIL_UNSPECIFIED);
2741 }
2742 }
2743#endif /* OPENSSL_IS_BORINGSSL */
2744
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002745 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002746 context->event_cb(context->cb_ctx,
2747 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002748
Hai Shalom899fcc72020-10-19 14:38:18 -07002749 if (depth == 0 && preverify_ok) {
2750 os_free(conn->peer_subject);
2751 conn->peer_subject = os_strdup(buf);
2752 }
2753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 return preverify_ok;
2755}
2756
2757
2758#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002759static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002761 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 X509_LOOKUP *lookup;
2763 int ret = 0;
2764
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002765 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766 X509_LOOKUP_file());
2767 if (lookup == NULL) {
2768 tls_show_errors(MSG_WARNING, __func__,
2769 "Failed add lookup for X509 store");
2770 return -1;
2771 }
2772
2773 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2774 unsigned long err = ERR_peek_error();
2775 tls_show_errors(MSG_WARNING, __func__,
2776 "Failed load CA in DER format");
2777 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2778 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2779 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2780 "cert already in hash table error",
2781 __func__);
2782 } else
2783 ret = -1;
2784 }
2785
2786 return ret;
2787}
2788#endif /* OPENSSL_NO_STDIO */
2789
2790
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791static int tls_connection_ca_cert(struct tls_data *data,
2792 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 const char *ca_cert, const u8 *ca_cert_blob,
2794 size_t ca_cert_blob_len, const char *ca_path)
2795{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002796 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002797 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798
2799 /*
2800 * Remove previously configured trusted CA certificates before adding
2801 * new ones.
2802 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002803 store = X509_STORE_new();
2804 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2806 "certificate store", __func__);
2807 return -1;
2808 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002809 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2812 conn->ca_cert_verify = 1;
2813
2814 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2815 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2816 "chain");
2817 conn->cert_probe = 1;
2818 conn->ca_cert_verify = 0;
2819 return 0;
2820 }
2821
2822 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2823#ifdef CONFIG_SHA256
2824 const char *pos = ca_cert + 7;
2825 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2826 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2827 "hash value '%s'", ca_cert);
2828 return -1;
2829 }
2830 pos += 14;
2831 if (os_strlen(pos) != 32 * 2) {
2832 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2833 "hash length in ca_cert '%s'", ca_cert);
2834 return -1;
2835 }
2836 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2837 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2838 "value in ca_cert '%s'", ca_cert);
2839 return -1;
2840 }
2841 conn->server_cert_only = 1;
2842 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2843 "certificate match");
2844 return 0;
2845#else /* CONFIG_SHA256 */
2846 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2847 "cannot validate server certificate hash");
2848 return -1;
2849#endif /* CONFIG_SHA256 */
2850 }
2851
2852 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002853 X509 *cert = d2i_X509(NULL,
2854 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 ca_cert_blob_len);
2856 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002857 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2858 ca_cert_blob_len);
2859
2860 if (bio) {
2861 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2862 BIO_free(bio);
2863 }
2864
2865 if (!cert) {
2866 tls_show_errors(MSG_WARNING, __func__,
2867 "Failed to parse ca_cert_blob");
2868 return -1;
2869 }
2870
2871 while (ERR_get_error()) {
2872 /* Ignore errors from DER conversion. */
2873 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 }
2875
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002876 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2877 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 unsigned long err = ERR_peek_error();
2879 tls_show_errors(MSG_WARNING, __func__,
2880 "Failed to add ca_cert_blob to "
2881 "certificate store");
2882 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2883 ERR_GET_REASON(err) ==
2884 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2885 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2886 "cert already in hash table error",
2887 __func__);
2888 } else {
2889 X509_free(cert);
2890 return -1;
2891 }
2892 }
2893 X509_free(cert);
2894 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2895 "to certificate store", __func__);
2896 return 0;
2897 }
2898
2899#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002900 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002901 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2902 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002903 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Hai Shalom7ad2a872021-08-02 18:56:55 -07002904 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002906 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2907 return 0;
2908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002910 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002911 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2912 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2913 char *aliases = os_strdup(
2914 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002915 const char *delim = " ";
2916 int rc = 0;
2917 char *savedptr;
2918 char *alias;
2919
2920 if (!aliases)
2921 return -1;
2922 alias = strtok_r(aliases, delim, &savedptr);
2923 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2924 if (tls_add_ca_from_keystore_encoded(
Hai Shalom7ad2a872021-08-02 18:56:55 -07002925 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
2926 wpa_printf(MSG_ERROR,
2927 "OpenSSL: Failed to add ca_cert %s from keystore",
2928 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002929 rc = -1;
2930 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 }
2932 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002933 os_free(aliases);
2934 if (rc)
2935 return rc;
2936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2938 return 0;
2939 }
2940#endif /* ANDROID */
2941
2942#ifdef CONFIG_NATIVE_WINDOWS
2943 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2944 0) {
2945 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2946 "system certificate store");
2947 return 0;
2948 }
2949#endif /* CONFIG_NATIVE_WINDOWS */
2950
2951 if (ca_cert || ca_path) {
2952#ifndef OPENSSL_NO_STDIO
2953 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2954 1) {
2955 tls_show_errors(MSG_WARNING, __func__,
2956 "Failed to load root certificates");
2957 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002958 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2960 "DER format CA certificate",
2961 __func__);
2962 } else
2963 return -1;
2964 } else {
2965 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2966 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002967 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 }
2969#else /* OPENSSL_NO_STDIO */
2970 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2971 __func__);
2972 return -1;
2973#endif /* OPENSSL_NO_STDIO */
2974 } else {
2975 /* No ca_cert configured - do not try to verify server
2976 * certificate */
2977 conn->ca_cert_verify = 0;
2978 }
2979
2980 return 0;
2981}
2982
2983
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002984static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002986 SSL_CTX *ssl_ctx = data->ssl;
2987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 if (ca_cert) {
2989 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2990 {
2991 tls_show_errors(MSG_WARNING, __func__,
2992 "Failed to load root certificates");
2993 return -1;
2994 }
2995
2996 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2997 "certificate(s) loaded");
2998
2999#ifndef OPENSSL_NO_STDIO
3000 /* Add the same CAs to the client certificate requests */
3001 SSL_CTX_set_client_CA_list(ssl_ctx,
3002 SSL_load_client_CA_file(ca_cert));
3003#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003004
3005 os_free(data->ca_cert);
3006 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 }
3008
3009 return 0;
3010}
3011
3012
Hai Shalom74f70d42019-02-11 14:42:39 -08003013int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
3015 int flags;
3016
3017 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003018 struct tls_data *data = ssl_ctx;
3019 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 if (cs == NULL) {
3021 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3022 "certificate store when enabling "
3023 "check_crl");
3024 return -1;
3025 }
3026 flags = X509_V_FLAG_CRL_CHECK;
3027 if (check_crl == 2)
3028 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3029 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003030
3031 data->check_crl = check_crl;
3032 data->check_crl_strict = strict;
3033 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 }
3035 return 0;
3036}
3037
3038
3039static int tls_connection_set_subject_match(struct tls_connection *conn,
3040 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003041 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003042 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003043 const char *domain_match,
3044 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045{
3046 os_free(conn->subject_match);
3047 conn->subject_match = NULL;
3048 if (subject_match) {
3049 conn->subject_match = os_strdup(subject_match);
3050 if (conn->subject_match == NULL)
3051 return -1;
3052 }
3053
3054 os_free(conn->altsubject_match);
3055 conn->altsubject_match = NULL;
3056 if (altsubject_match) {
3057 conn->altsubject_match = os_strdup(altsubject_match);
3058 if (conn->altsubject_match == NULL)
3059 return -1;
3060 }
3061
Dmitry Shmidt051af732013-10-22 13:52:46 -07003062 os_free(conn->suffix_match);
3063 conn->suffix_match = NULL;
3064 if (suffix_match) {
3065 conn->suffix_match = os_strdup(suffix_match);
3066 if (conn->suffix_match == NULL)
3067 return -1;
3068 }
3069
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003070 os_free(conn->domain_match);
3071 conn->domain_match = NULL;
3072 if (domain_match) {
3073 conn->domain_match = os_strdup(domain_match);
3074 if (conn->domain_match == NULL)
3075 return -1;
3076 }
3077
Hai Shalom021b0b52019-04-10 11:17:58 -07003078 os_free(conn->check_cert_subject);
3079 conn->check_cert_subject = NULL;
3080 if (check_cert_subject) {
3081 conn->check_cert_subject = os_strdup(check_cert_subject);
3082 if (!conn->check_cert_subject)
3083 return -1;
3084 }
3085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 return 0;
3087}
3088
3089
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003090#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003091static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003092{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003093 struct tls_connection *conn = arg;
3094
3095 /*
3096 * This cert_cb() is not really the best location for doing a
3097 * constraint check for the ServerKeyExchange message, but this seems to
3098 * be the only place where the current OpenSSL sequence can be
3099 * terminated cleanly with an TLS alert going out to the server.
3100 */
3101
3102 if (!(conn->flags & TLS_CONN_SUITEB))
3103 return 1;
3104
3105 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3106 if (conn->cipher_suite != 0x9f)
3107 return 1;
3108
3109 if (conn->server_dh_prime_len >= 3072)
3110 return 1;
3111
3112 wpa_printf(MSG_DEBUG,
3113 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3114 conn->server_dh_prime_len);
3115 return 0;
3116}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003117#endif /* CONFIG_SUITEB */
3118
3119
Roshan Pius3a1667e2018-07-03 15:17:14 -07003120static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3121 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003122{
3123 SSL *ssl = conn->ssl;
3124
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003125#ifdef SSL_OP_NO_TICKET
3126 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3127 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003128 else
3129 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003130#endif /* SSL_OP_NO_TICKET */
3131
Sunil Ravia04bd252022-05-02 22:54:18 -07003132#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3133 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3134 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3135#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3136
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003137#ifdef SSL_OP_NO_TLSv1
3138 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3139 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3140 else
3141 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3142#endif /* SSL_OP_NO_TLSv1 */
3143#ifdef SSL_OP_NO_TLSv1_1
3144 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3145 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3146 else
3147 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3148#endif /* SSL_OP_NO_TLSv1_1 */
3149#ifdef SSL_OP_NO_TLSv1_2
3150 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3151 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3152 else
3153 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3154#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003155#ifdef SSL_OP_NO_TLSv1_3
3156 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3157 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3158 else
3159 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3160#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003161#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3162 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3163 TLS_CONN_ENABLE_TLSv1_1 |
3164 TLS_CONN_ENABLE_TLSv1_2)) {
3165 int version = 0;
3166
3167 /* Explicit request to enable TLS versions even if needing to
3168 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003169 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003170 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003171 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3172 version = TLS1_1_VERSION;
3173 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3174 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003175 if (!version) {
3176 wpa_printf(MSG_DEBUG,
3177 "OpenSSL: Invalid TLS version configuration");
3178 return -1;
3179 }
3180
3181 if (SSL_set_min_proto_version(ssl, version) != 1) {
3182 wpa_printf(MSG_DEBUG,
3183 "OpenSSL: Failed to set minimum TLS version");
3184 return -1;
3185 }
3186 }
3187#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003188#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3189 !defined(LIBRESSL_VERSION_NUMBER) && \
3190 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003191 {
3192#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3193 int need_level = 0;
3194#else
3195 int need_level = 1;
3196#endif
3197
3198 if ((flags &
3199 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3200 SSL_get_security_level(ssl) > need_level) {
3201 /*
3202 * Need to drop to security level 1 (or 0 with OpenSSL
3203 * 3.0) to allow TLS versions older than 1.2 to be used
3204 * when explicitly enabled in configuration.
3205 */
3206 SSL_set_security_level(conn->ssl, need_level);
3207 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003208 }
3209#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003210
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003211#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003212#ifdef OPENSSL_IS_BORINGSSL
3213 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003214 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003215#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003216 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3217 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3218
Roshan Pius3a1667e2018-07-03 15:17:14 -07003219 if (openssl_ciphers) {
3220 wpa_printf(MSG_DEBUG,
3221 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3222 openssl_ciphers);
3223 ciphers = openssl_ciphers;
3224 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003225 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3226 wpa_printf(MSG_INFO,
3227 "OpenSSL: Failed to set Suite B ciphers");
3228 return -1;
3229 }
3230 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003231#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003232 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003233#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003234 const char *ciphers =
3235 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003236 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003237
Roshan Pius3a1667e2018-07-03 15:17:14 -07003238 if (openssl_ciphers) {
3239 wpa_printf(MSG_DEBUG,
3240 "OpenSSL: Override ciphers for Suite B: %s",
3241 openssl_ciphers);
3242 ciphers = openssl_ciphers;
3243 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003244 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3245 wpa_printf(MSG_INFO,
3246 "OpenSSL: Failed to set Suite B ciphers");
3247 return -1;
3248 }
3249
Sunil Ravia04bd252022-05-02 22:54:18 -07003250#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3251 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3252 wpa_printf(MSG_INFO,
3253 "OpenSSL: Failed to set Suite B groups");
3254 return -1;
3255 }
3256
3257#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003258 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003259 wpa_printf(MSG_INFO,
3260 "OpenSSL: Failed to set Suite B curves");
3261 return -1;
3262 }
3263
3264 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3265 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3266 EC_KEY_free(ecdh);
3267 wpa_printf(MSG_INFO,
3268 "OpenSSL: Failed to set ECDH parameter");
3269 return -1;
3270 }
3271 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003272#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003273 }
Hai Shalom0f94b7a2023-03-13 13:22:35 -07003274 if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH))
3275#ifdef EAP_TLSV1_3
3276 && (flags & TLS_CONN_DISABLE_TLSv1_3)
3277#endif
3278 ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003279#ifdef OPENSSL_IS_BORINGSSL
3280 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3281
Jimmy Chen916e0a72022-01-11 15:19:46 +08003282 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003283 1) != 1) {
3284 wpa_printf(MSG_INFO,
3285 "OpenSSL: Failed to set Suite B sigalgs");
3286 return -1;
3287 }
3288#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003289 /* ECDSA+SHA384 if need to add EC support here */
3290 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3291 wpa_printf(MSG_INFO,
3292 "OpenSSL: Failed to set Suite B sigalgs");
3293 return -1;
3294 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003295#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003296
3297 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3298 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3299 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3300 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003301
3302#ifdef OPENSSL_IS_BORINGSSL
3303 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3304 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3305 int nid[1] = { NID_secp384r1 };
3306
3307 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3308 wpa_printf(MSG_INFO,
3309 "OpenSSL: Failed to set Suite B curves");
3310 return -1;
3311 }
3312
Jimmy Chen916e0a72022-01-11 15:19:46 +08003313 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003314 1) != 1) {
3315 wpa_printf(MSG_INFO,
3316 "OpenSSL: Failed to set Suite B sigalgs");
3317 return -1;
3318 }
3319 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003320#else /* OPENSSL_IS_BORINGSSL */
3321 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3322 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3323 wpa_printf(MSG_INFO,
3324 "OpenSSL: Failed to set openssl_ciphers '%s'",
3325 openssl_ciphers);
3326 return -1;
3327 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003328#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003329#else /* CONFIG_SUITEB */
3330 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3331 wpa_printf(MSG_INFO,
3332 "OpenSSL: Failed to set openssl_ciphers '%s'",
3333 openssl_ciphers);
3334 return -1;
3335 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003336#endif /* CONFIG_SUITEB */
3337
Hai Shalom81f62d82019-07-22 12:10:00 -07003338 if (flags & TLS_CONN_TEAP_ANON_DH) {
3339#ifndef TEAP_DH_ANON_CS
3340#define TEAP_DH_ANON_CS \
3341 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3342 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3343 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3344 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3345 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3346 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3347 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3348 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3349#endif
3350 static const char *cs = TEAP_DH_ANON_CS;
3351
3352#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3353 !defined(LIBRESSL_VERSION_NUMBER) && \
3354 !defined(OPENSSL_IS_BORINGSSL)
3355 /*
3356 * Need to drop to security level 0 to allow anonymous
3357 * cipher suites for EAP-TEAP.
3358 */
3359 SSL_set_security_level(conn->ssl, 0);
3360#endif
3361
3362 wpa_printf(MSG_DEBUG,
3363 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3364 cs);
3365 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3366 tls_show_errors(MSG_INFO, __func__,
3367 "Cipher suite configuration failed");
3368 return -1;
3369 }
3370 }
3371
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003372 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003373}
3374
3375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003377 int verify_peer, unsigned int flags,
3378 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003379{
3380 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003381 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382
3383 if (conn == NULL)
3384 return -1;
3385
Hai Shalom899fcc72020-10-19 14:38:18 -07003386 if (verify_peer == 2) {
3387 conn->ca_cert_verify = 1;
3388 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3389 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3390 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391 conn->ca_cert_verify = 1;
3392 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3393 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3394 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3395 } else {
3396 conn->ca_cert_verify = 0;
3397 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3398 }
3399
Roshan Pius3a1667e2018-07-03 15:17:14 -07003400 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003401 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003402 conn->flags = flags;
3403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404 SSL_set_accept_state(conn->ssl);
3405
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003406 if (data->tls_session_lifetime == 0) {
3407 /*
3408 * Set session id context to a unique value to make sure
3409 * session resumption cannot be used either through session
3410 * caching or TLS ticket extension.
3411 */
3412 counter++;
3413 SSL_set_session_id_context(conn->ssl,
3414 (const unsigned char *) &counter,
3415 sizeof(counter));
3416 } else if (session_ctx) {
3417 SSL_set_session_id_context(conn->ssl, session_ctx,
3418 session_ctx_len);
3419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420
3421 return 0;
3422}
3423
3424
3425static int tls_connection_client_cert(struct tls_connection *conn,
3426 const char *client_cert,
3427 const u8 *client_cert_blob,
3428 size_t client_cert_blob_len)
3429{
3430 if (client_cert == NULL && client_cert_blob == NULL)
3431 return 0;
3432
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003433#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003434#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003435 /*
3436 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003437 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003438 * chain properly.
3439 */
3440 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003441#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003442#endif /* PKCS12_FUNCS */
3443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 if (client_cert_blob &&
3445 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3446 client_cert_blob_len) == 1) {
3447 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3448 "OK");
3449 return 0;
3450 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003451#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 tls_show_errors(MSG_DEBUG, __func__,
3453 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003454#else
3455 BIO *bio;
3456 X509 *x509;
3457
3458 tls_show_errors(MSG_DEBUG, __func__,
3459 "SSL_use_certificate_ASN1 failed");
3460 bio = BIO_new(BIO_s_mem());
3461 if (!bio)
3462 return -1;
3463 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3464 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3465 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3466 X509_free(x509);
3467 BIO_free(bio);
3468 return -1;
3469 }
3470 X509_free(x509);
3471 wpa_printf(MSG_DEBUG,
3472 "OpenSSL: Found PEM encoded certificate from blob");
3473 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3474 wpa_printf(MSG_DEBUG,
3475 "OpenSSL: Added an additional certificate into the chain");
3476 SSL_add0_chain_cert(conn->ssl, x509);
3477 }
3478 BIO_free(bio);
3479 return 0;
3480#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481 }
3482
3483 if (client_cert == NULL)
3484 return -1;
3485
3486#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003487 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3488 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
3489 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003491 if (!bio) {
3492 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003493 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003494 // Keystore returns X.509 certificates in PEM encoding
3495 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3496 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003497 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003498 BIO_free(bio);
3499 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3500 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003502 X509_free(x509);
3503 wpa_printf(MSG_DEBUG,
3504 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3505 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003506
Hai Shalom7ad2a872021-08-02 18:56:55 -07003507 // Read additional certificates into the chain
3508 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3509 wpa_printf(MSG_DEBUG,
3510 "OpenSSL: Added an additional certificate into the chain");
3511 // Takes ownership of x509, no need to free it here
3512 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003513 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003514 BIO_free(bio);
3515 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 }
3517#endif /* ANDROID */
3518
3519#ifndef OPENSSL_NO_STDIO
3520 if (SSL_use_certificate_file(conn->ssl, client_cert,
3521 SSL_FILETYPE_ASN1) == 1) {
3522 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3523 " --> OK");
3524 return 0;
3525 }
3526
Hai Shalom021b0b52019-04-10 11:17:58 -07003527#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3528 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003529 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3530 ERR_clear_error();
3531 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3532 " --> OK");
3533 return 0;
3534 }
3535#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536 if (SSL_use_certificate_file(conn->ssl, client_cert,
3537 SSL_FILETYPE_PEM) == 1) {
3538 ERR_clear_error();
3539 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3540 " --> OK");
3541 return 0;
3542 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003543#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544
3545 tls_show_errors(MSG_DEBUG, __func__,
3546 "SSL_use_certificate_file failed");
3547#else /* OPENSSL_NO_STDIO */
3548 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3549#endif /* OPENSSL_NO_STDIO */
3550
3551 return -1;
3552}
3553
3554
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003555static int tls_global_client_cert(struct tls_data *data,
3556 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557{
3558#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003559 SSL_CTX *ssl_ctx = data->ssl;
3560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 if (client_cert == NULL)
3562 return 0;
3563
3564 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3565 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003566 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3568 SSL_FILETYPE_PEM) != 1) {
3569 tls_show_errors(MSG_INFO, __func__,
3570 "Failed to load client certificate");
3571 return -1;
3572 }
3573 return 0;
3574#else /* OPENSSL_NO_STDIO */
3575 if (client_cert == NULL)
3576 return 0;
3577 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3578 return -1;
3579#endif /* OPENSSL_NO_STDIO */
3580}
3581
3582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003584static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585 const char *passwd)
3586{
3587 EVP_PKEY *pkey;
3588 X509 *cert;
3589 STACK_OF(X509) *certs;
3590 int res = 0;
3591 char buf[256];
3592
3593 pkey = NULL;
3594 cert = NULL;
3595 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003596 if (!passwd)
3597 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3599 tls_show_errors(MSG_DEBUG, __func__,
3600 "Failed to parse PKCS12 file");
3601 PKCS12_free(p12);
3602 return -1;
3603 }
3604 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3605
3606 if (cert) {
3607 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3608 sizeof(buf));
3609 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3610 "subject='%s'", buf);
3611 if (ssl) {
3612 if (SSL_use_certificate(ssl, cert) != 1)
3613 res = -1;
3614 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 res = -1;
3617 }
3618 X509_free(cert);
3619 }
3620
3621 if (pkey) {
3622 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3623 if (ssl) {
3624 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3625 res = -1;
3626 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003627 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 res = -1;
3629 }
3630 EVP_PKEY_free(pkey);
3631 }
3632
3633 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003634#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003635 if (ssl)
3636 SSL_clear_chain_certs(ssl);
3637 else
3638 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003639 while ((cert = sk_X509_pop(certs)) != NULL) {
3640 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3641 sizeof(buf));
3642 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3643 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003644 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3645 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3646 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003647 tls_show_errors(MSG_DEBUG, __func__,
3648 "Failed to add additional certificate");
3649 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003650 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003651 break;
3652 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003653 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003654 }
3655 if (!res) {
3656 /* Try to continue anyway */
3657 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003658 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003659#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003660 if (ssl)
3661 res = SSL_build_cert_chain(
3662 ssl,
3663 SSL_BUILD_CHAIN_FLAG_CHECK |
3664 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3665 else
3666 res = SSL_CTX_build_cert_chain(
3667 data->ssl,
3668 SSL_BUILD_CHAIN_FLAG_CHECK |
3669 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003670 if (!res) {
3671 tls_show_errors(MSG_DEBUG, __func__,
3672 "Failed to build certificate chain");
3673 } else if (res == 2) {
3674 wpa_printf(MSG_DEBUG,
3675 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3676 }
3677#endif /* OPENSSL_IS_BORINGSSL */
3678 /*
3679 * Try to continue regardless of result since it is possible for
3680 * the extra certificates not to be required.
3681 */
3682 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003683#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003684 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 while ((cert = sk_X509_pop(certs)) != NULL) {
3686 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3687 sizeof(buf));
3688 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3689 " from PKCS12: subject='%s'", buf);
3690 /*
3691 * There is no SSL equivalent for the chain cert - so
3692 * always add it to the context...
3693 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003694 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3695 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003696 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 res = -1;
3698 break;
3699 }
3700 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003701 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003702#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 }
3704
3705 PKCS12_free(p12);
3706
3707 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003708 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
3710 return res;
3711}
3712#endif /* PKCS12_FUNCS */
3713
3714
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003715static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3716 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717{
3718#ifdef PKCS12_FUNCS
3719 FILE *f;
3720 PKCS12 *p12;
3721
3722 f = fopen(private_key, "rb");
3723 if (f == NULL)
3724 return -1;
3725
3726 p12 = d2i_PKCS12_fp(f, NULL);
3727 fclose(f);
3728
3729 if (p12 == NULL) {
3730 tls_show_errors(MSG_INFO, __func__,
3731 "Failed to use PKCS#12 file");
3732 return -1;
3733 }
3734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003735 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736
3737#else /* PKCS12_FUNCS */
3738 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3739 "p12/pfx files");
3740 return -1;
3741#endif /* PKCS12_FUNCS */
3742}
3743
3744
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003745static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 const u8 *blob, size_t len, const char *passwd)
3747{
3748#ifdef PKCS12_FUNCS
3749 PKCS12 *p12;
3750
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003751 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 if (p12 == NULL) {
3753 tls_show_errors(MSG_INFO, __func__,
3754 "Failed to use PKCS#12 blob");
3755 return -1;
3756 }
3757
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003758 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759
3760#else /* PKCS12_FUNCS */
3761 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3762 "p12/pfx blobs");
3763 return -1;
3764#endif /* PKCS12_FUNCS */
3765}
3766
3767
3768#ifndef OPENSSL_NO_ENGINE
3769static int tls_engine_get_cert(struct tls_connection *conn,
3770 const char *cert_id,
3771 X509 **cert)
3772{
3773 /* this runs after the private key is loaded so no PIN is required */
3774 struct {
3775 const char *cert_id;
3776 X509 *cert;
3777 } params;
3778 params.cert_id = cert_id;
3779 params.cert = NULL;
3780
3781 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3782 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003783 unsigned long err = ERR_get_error();
3784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3786 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003787 ERR_error_string(err, NULL));
3788 if (tls_is_pin_error(err))
3789 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3791 }
3792 if (!params.cert) {
3793 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3794 " '%s'", cert_id);
3795 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3796 }
3797 *cert = params.cert;
3798 return 0;
3799}
3800#endif /* OPENSSL_NO_ENGINE */
3801
3802
3803static int tls_connection_engine_client_cert(struct tls_connection *conn,
3804 const char *cert_id)
3805{
3806#ifndef OPENSSL_NO_ENGINE
3807 X509 *cert;
3808
3809 if (tls_engine_get_cert(conn, cert_id, &cert))
3810 return -1;
3811
3812 if (!SSL_use_certificate(conn->ssl, cert)) {
3813 tls_show_errors(MSG_ERROR, __func__,
3814 "SSL_use_certificate failed");
3815 X509_free(cert);
3816 return -1;
3817 }
3818 X509_free(cert);
3819 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3820 "OK");
3821 return 0;
3822
3823#else /* OPENSSL_NO_ENGINE */
3824 return -1;
3825#endif /* OPENSSL_NO_ENGINE */
3826}
3827
3828
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003829static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830 struct tls_connection *conn,
3831 const char *ca_cert_id)
3832{
3833#ifndef OPENSSL_NO_ENGINE
3834 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003835 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003836 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837
3838 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3839 return -1;
3840
3841 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003842 store = X509_STORE_new();
3843 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3845 "certificate store", __func__);
3846 X509_free(cert);
3847 return -1;
3848 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003849 SSL_CTX_set_cert_store(ssl_ctx, store);
3850 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 unsigned long err = ERR_peek_error();
3852 tls_show_errors(MSG_WARNING, __func__,
3853 "Failed to add CA certificate from engine "
3854 "to certificate store");
3855 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3856 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3857 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3858 " already in hash table error",
3859 __func__);
3860 } else {
3861 X509_free(cert);
3862 return -1;
3863 }
3864 }
3865 X509_free(cert);
3866 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3867 "to certificate store", __func__);
3868 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003869 conn->ca_cert_verify = 1;
3870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 return 0;
3872
3873#else /* OPENSSL_NO_ENGINE */
3874 return -1;
3875#endif /* OPENSSL_NO_ENGINE */
3876}
3877
3878
3879static int tls_connection_engine_private_key(struct tls_connection *conn)
3880{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003881#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3883 tls_show_errors(MSG_ERROR, __func__,
3884 "ENGINE: cannot use private key for TLS");
3885 return -1;
3886 }
3887 if (!SSL_check_private_key(conn->ssl)) {
3888 tls_show_errors(MSG_INFO, __func__,
3889 "Private key failed verification");
3890 return -1;
3891 }
3892 return 0;
3893#else /* OPENSSL_NO_ENGINE */
3894 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3895 "engine support was not compiled in");
3896 return -1;
3897#endif /* OPENSSL_NO_ENGINE */
3898}
3899
3900
Roshan Pius3a1667e2018-07-03 15:17:14 -07003901#ifndef OPENSSL_NO_STDIO
3902static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003903{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003904 if (!password)
3905 return 0;
3906 os_strlcpy(buf, (const char *) password, size);
3907 return os_strlen(buf);
3908}
3909#endif /* OPENSSL_NO_STDIO */
3910
3911
3912static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3913 const char *private_key,
3914 const char *private_key_passwd)
3915{
3916#ifndef OPENSSL_NO_STDIO
3917 BIO *bio;
3918 EVP_PKEY *pkey;
3919 int ret;
3920
3921 /* First try ASN.1 (DER). */
3922 bio = BIO_new_file(private_key, "r");
3923 if (!bio)
3924 return -1;
3925 pkey = d2i_PrivateKey_bio(bio, NULL);
3926 BIO_free(bio);
3927
3928 if (pkey) {
3929 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3930 } else {
3931 /* Try PEM with the provided password. */
3932 bio = BIO_new_file(private_key, "r");
3933 if (!bio)
3934 return -1;
3935 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3936 (void *) private_key_passwd);
3937 BIO_free(bio);
3938 if (!pkey)
3939 return -1;
3940 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3941 /* Clear errors from the previous failed load. */
3942 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003943 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003944
3945 if (ssl)
3946 ret = SSL_use_PrivateKey(ssl, pkey);
3947 else
3948 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3949
3950 EVP_PKEY_free(pkey);
3951 return ret == 1 ? 0 : -1;
3952#else /* OPENSSL_NO_STDIO */
3953 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3954 return -1;
3955#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003956}
3957
3958
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003959static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 struct tls_connection *conn,
3961 const char *private_key,
3962 const char *private_key_passwd,
3963 const u8 *private_key_blob,
3964 size_t private_key_blob_len)
3965{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003966 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 int ok;
3968
3969 if (private_key == NULL && private_key_blob == NULL)
3970 return 0;
3971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 ok = 0;
3973 while (private_key_blob) {
3974 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3975 (u8 *) private_key_blob,
3976 private_key_blob_len) == 1) {
3977 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3978 "ASN1(EVP_PKEY_RSA) --> OK");
3979 ok = 1;
3980 break;
3981 }
3982
3983 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3984 (u8 *) private_key_blob,
3985 private_key_blob_len) == 1) {
3986 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3987 "ASN1(EVP_PKEY_DSA) --> OK");
3988 ok = 1;
3989 break;
3990 }
3991
Hai Shalom899fcc72020-10-19 14:38:18 -07003992#ifndef OPENSSL_NO_EC
3993 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
3994 (u8 *) private_key_blob,
3995 private_key_blob_len) == 1) {
3996 wpa_printf(MSG_DEBUG,
3997 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
3998 ok = 1;
3999 break;
4000 }
4001#endif /* OPENSSL_NO_EC */
4002
Sunil Ravia04bd252022-05-02 22:54:18 -07004003#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4005 (u8 *) private_key_blob,
4006 private_key_blob_len) == 1) {
4007 wpa_printf(MSG_DEBUG, "OpenSSL: "
4008 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4009 ok = 1;
4010 break;
4011 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004012#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013
Hai Shaloma20dcd72022-02-04 13:43:00 -08004014 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4015 private_key_blob_len);
4016 if (bio) {
4017 EVP_PKEY *pkey;
4018
4019 pkey = PEM_read_bio_PrivateKey(
4020 bio, NULL, tls_passwd_cb,
4021 (void *) private_key_passwd);
4022 if (pkey) {
4023 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4024 wpa_printf(MSG_DEBUG,
4025 "OpenSSL: SSL_use_PrivateKey --> OK");
4026 ok = 1;
4027 EVP_PKEY_free(pkey);
4028 BIO_free(bio);
4029 break;
4030 }
4031 EVP_PKEY_free(pkey);
4032 }
4033 BIO_free(bio);
4034 }
4035
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004036 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004037 private_key_blob_len,
4038 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4040 "OK");
4041 ok = 1;
4042 break;
4043 }
4044
4045 break;
4046 }
4047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004049 if (tls_use_private_key_file(data, conn->ssl, private_key,
4050 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051 ok = 1;
4052 break;
4053 }
4054
Roshan Pius3a1667e2018-07-03 15:17:14 -07004055 if (tls_read_pkcs12(data, conn->ssl, private_key,
4056 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4058 "--> OK");
4059 ok = 1;
4060 break;
4061 }
4062
4063 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4064 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4065 "access certificate store --> OK");
4066 ok = 1;
4067 break;
4068 }
4069
4070 break;
4071 }
4072
4073 if (!ok) {
4074 tls_show_errors(MSG_INFO, __func__,
4075 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 return -1;
4077 }
4078 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 if (!SSL_check_private_key(conn->ssl)) {
4081 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4082 "verification");
4083 return -1;
4084 }
4085
4086 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4087 return 0;
4088}
4089
4090
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004091static int tls_global_private_key(struct tls_data *data,
4092 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 const char *private_key_passwd)
4094{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004095 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096
4097 if (private_key == NULL)
4098 return 0;
4099
Roshan Pius3a1667e2018-07-03 15:17:14 -07004100 if (tls_use_private_key_file(data, NULL, private_key,
4101 private_key_passwd) &&
4102 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 tls_show_errors(MSG_INFO, __func__,
4104 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 ERR_clear_error();
4106 return -1;
4107 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4111 tls_show_errors(MSG_INFO, __func__,
4112 "Private key failed verification");
4113 return -1;
4114 }
4115
4116 return 0;
4117}
4118
4119
Sunil Ravia04bd252022-05-02 22:54:18 -07004120#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4121#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004123/* This is needed to replace the deprecated DSA_dup_DH() function */
4124static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4125{
4126 OSSL_PARAM_BLD *bld = NULL;
4127 OSSL_PARAM *params = NULL;
4128 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4129 EVP_PKEY_CTX *ctx = NULL;
4130 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131
Sunil Ravia04bd252022-05-02 22:54:18 -07004132 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4133 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4134 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4135 !(bld = OSSL_PARAM_BLD_new()) ||
4136 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4137 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4138 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4139 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4140 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4141 EVP_PKEY_fromdata_init(ctx) != 1 ||
4142 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4143 params) != 1)
4144 wpa_printf(MSG_INFO,
4145 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146
Sunil Ravia04bd252022-05-02 22:54:18 -07004147 EVP_PKEY_CTX_free(ctx);
4148 OSSL_PARAM_free(params);
4149 OSSL_PARAM_BLD_free(bld);
4150 BN_free(p);
4151 BN_free(q);
4152 BN_free(g);
4153 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154}
Sunil Ravia04bd252022-05-02 22:54:18 -07004155#endif /* !OPENSSL_NO_DSA */
4156#endif /* OPENSSL_NO_DH */
4157#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004159static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160{
4161#ifdef OPENSSL_NO_DH
4162 if (dh_file == NULL)
4163 return 0;
4164 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4165 "dh_file specified");
4166 return -1;
4167#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004168#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4169 SSL_CTX *ssl_ctx = data->ssl;
4170 BIO *bio;
4171 OSSL_DECODER_CTX *ctx = NULL;
4172 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4173 bool dsa = false;
4174
4175 if (!ssl_ctx)
4176 return -1;
4177 if (!dh_file) {
4178 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4179 return 0;
4180 }
4181
4182 bio = BIO_new_file(dh_file, "r");
4183 if (!bio) {
4184 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4185 dh_file, ERR_error_string(ERR_get_error(), NULL));
4186 return -1;
4187 }
4188 ctx = OSSL_DECODER_CTX_new_for_pkey(
4189 &tmpkey, "PEM", NULL, NULL,
4190 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4191 if (!ctx ||
4192 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4193 wpa_printf(MSG_INFO,
4194 "TLS: Failed to decode domain parameters from '%s': %s",
4195 dh_file, ERR_error_string(ERR_get_error(), NULL));
4196 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004197 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004198 return -1;
4199 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004200 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004201 BIO_free(bio);
4202
4203 if (!tmpkey) {
4204 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4205 return -1;
4206 }
4207
4208#ifndef OPENSSL_NO_DSA
4209 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4210 pkey = openssl_dsa_to_dh(tmpkey);
4211 EVP_PKEY_free(tmpkey);
4212 if (!pkey)
4213 return -1;
4214 dsa = true;
4215 }
4216#endif /* !OPENSSL_NO_DSA */
4217 if (!dsa) {
4218 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4219 EVP_PKEY_is_a(tmpkey, "DHX")) {
4220 } else {
4221 wpa_printf(MSG_INFO,
4222 "TLS: No DH parameters found in %s",
4223 dh_file);
4224 EVP_PKEY_free(tmpkey);
4225 return -1;
4226 }
4227 pkey = tmpkey;
4228 tmpkey = NULL;
4229 }
4230
4231 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4232 wpa_printf(MSG_INFO,
4233 "TLS: Failed to set DH params from '%s': %s",
4234 dh_file, ERR_error_string(ERR_get_error(), NULL));
4235 EVP_PKEY_free(pkey);
4236 return -1;
4237 }
4238 return 0;
4239#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 DH *dh;
4242 BIO *bio;
4243
Sunil Ravia04bd252022-05-02 22:54:18 -07004244 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004246 if (!dh_file) {
4247#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4248 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4249#endif
4250 return 0;
4251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252
4253 bio = BIO_new_file(dh_file, "r");
4254 if (bio == NULL) {
4255 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4256 dh_file, ERR_error_string(ERR_get_error(), NULL));
4257 return -1;
4258 }
4259 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4260 BIO_free(bio);
4261#ifndef OPENSSL_NO_DSA
4262 while (dh == NULL) {
4263 DSA *dsa;
4264 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4265 " trying to parse as DSA params", dh_file,
4266 ERR_error_string(ERR_get_error(), NULL));
4267 bio = BIO_new_file(dh_file, "r");
4268 if (bio == NULL)
4269 break;
4270 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4271 BIO_free(bio);
4272 if (!dsa) {
4273 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4274 "'%s': %s", dh_file,
4275 ERR_error_string(ERR_get_error(), NULL));
4276 break;
4277 }
4278
4279 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4280 dh = DSA_dup_DH(dsa);
4281 DSA_free(dsa);
4282 if (dh == NULL) {
4283 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4284 "params into DH params");
4285 break;
4286 }
4287 break;
4288 }
4289#endif /* !OPENSSL_NO_DSA */
4290 if (dh == NULL) {
4291 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4292 "'%s'", dh_file);
4293 return -1;
4294 }
4295
4296 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4297 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4298 "%s", dh_file,
4299 ERR_error_string(ERR_get_error(), NULL));
4300 DH_free(dh);
4301 return -1;
4302 }
4303 DH_free(dh);
4304 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004305#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306#endif /* OPENSSL_NO_DH */
4307}
4308
4309
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004310int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4311 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312{
4313 SSL *ssl;
4314
4315 if (conn == NULL || keys == NULL)
4316 return -1;
4317 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004318 if (ssl == NULL)
4319 return -1;
4320
4321 os_memset(keys, 0, sizeof(*keys));
4322 keys->client_random = conn->client_random;
4323 keys->client_random_len = SSL_get_client_random(
4324 ssl, conn->client_random, sizeof(conn->client_random));
4325 keys->server_random = conn->server_random;
4326 keys->server_random_len = SSL_get_server_random(
4327 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328
4329 return 0;
4330}
4331
4332
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004333#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004334static int openssl_get_keyblock_size(SSL *ssl)
4335{
Sunil Ravia04bd252022-05-02 22:54:18 -07004336#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004337 const EVP_CIPHER *c;
4338 const EVP_MD *h;
4339 int md_size;
4340
4341 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4342 ssl->read_hash == NULL)
4343 return -1;
4344
4345 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004346 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004347 if (h)
4348 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004349 else if (ssl->s3)
4350 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004351 else
4352 return -1;
4353
4354 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4355 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4356 EVP_CIPHER_iv_length(c));
4357 return 2 * (EVP_CIPHER_key_length(c) +
4358 md_size +
4359 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004360#else
4361 const SSL_CIPHER *ssl_cipher;
4362 int cipher, digest;
4363 const EVP_CIPHER *c;
4364 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004365 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004366
4367 ssl_cipher = SSL_get_current_cipher(ssl);
4368 if (!ssl_cipher)
4369 return -1;
4370 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4371 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4372 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4373 cipher, digest);
4374 if (cipher < 0 || digest < 0)
4375 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004376 if (cipher == NID_undef) {
4377 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004379 }
4380 c = EVP_get_cipherbynid(cipher);
4381 if (!c)
4382 return -1;
4383 enc_key_len = EVP_CIPHER_key_length(c);
4384 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4385 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4386 fixed_iv_len = 4; /* only part of IV from PRF */
4387 else
4388 fixed_iv_len = EVP_CIPHER_iv_length(c);
4389 if (digest == NID_undef) {
4390 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4391 mac_key_len = 0;
4392 } else {
4393 h = EVP_get_digestbynid(digest);
4394 if (!h)
4395 return -1;
4396 mac_key_len = EVP_MD_size(h);
4397 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398
4399 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004400 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4401 mac_key_len, enc_key_len, fixed_iv_len);
4402 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004403#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004404}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004405#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004406
4407
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004408int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004409 const char *label, const u8 *context,
4410 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004411{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004412 if (!conn ||
4413 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004414 os_strlen(label), context, context_len,
4415 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004416 return -1;
4417 return 0;
4418}
4419
4420
4421int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4422 u8 *out, size_t out_len)
4423{
4424#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004425 SSL *ssl;
4426 SSL_SESSION *sess;
4427 u8 *rnd;
4428 int ret = -1;
4429 int skip = 0;
4430 u8 *tmp_out = NULL;
4431 u8 *_out = out;
4432 unsigned char client_random[SSL3_RANDOM_SIZE];
4433 unsigned char server_random[SSL3_RANDOM_SIZE];
4434 unsigned char master_key[64];
4435 size_t master_key_len;
4436 const char *ver;
4437
4438 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004439 * TLS library did not support EAP-FAST key generation, so get the
4440 * needed TLS session parameters and use an internal implementation of
4441 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004442 */
4443
4444 if (conn == NULL)
4445 return -1;
4446 ssl = conn->ssl;
4447 if (ssl == NULL)
4448 return -1;
4449 ver = SSL_get_version(ssl);
4450 sess = SSL_get_session(ssl);
4451 if (!ver || !sess)
4452 return -1;
4453
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004454 skip = openssl_get_keyblock_size(ssl);
4455 if (skip < 0)
4456 return -1;
4457 tmp_out = os_malloc(skip + out_len);
4458 if (!tmp_out)
4459 return -1;
4460 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004461
4462 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4463 if (!rnd) {
4464 os_free(tmp_out);
4465 return -1;
4466 }
4467
4468 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4469 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4470 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4471 sizeof(master_key));
4472
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004473 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4474 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475
4476 if (os_strcmp(ver, "TLSv1.2") == 0) {
4477 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004478 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004479 _out, skip + out_len);
4480 ret = 0;
4481 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004482 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004483 _out, skip + out_len) == 0) {
4484 ret = 0;
4485 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004486 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004488 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004489 os_memcpy(out, _out + skip, out_len);
4490 bin_clear_free(tmp_out, skip);
4491
4492 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004493#else /* OPENSSL_NEED_EAP_FAST_PRF */
4494 wpa_printf(MSG_ERROR,
4495 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4496 return -1;
4497#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498}
4499
4500
4501static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004502openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503{
Sunil Ravia04bd252022-05-02 22:54:18 -07004504 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 int res;
4506 struct wpabuf *out_data;
4507
4508 /*
4509 * Give TLS handshake data from the server (if available) to OpenSSL
4510 * for processing.
4511 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004512 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4514 < 0) {
4515 tls_show_errors(MSG_INFO, __func__,
4516 "Handshake failed - BIO_write");
4517 return NULL;
4518 }
4519
4520 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004521 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522 res = SSL_accept(conn->ssl);
4523 else
4524 res = SSL_connect(conn->ssl);
4525 if (res != 1) {
4526 int err = SSL_get_error(conn->ssl, res);
4527 if (err == SSL_ERROR_WANT_READ)
4528 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4529 "more data");
4530 else if (err == SSL_ERROR_WANT_WRITE)
4531 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4532 "write");
4533 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004534 unsigned long error = ERR_peek_last_error();
4535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004537
4538 if (context->event_cb &&
4539 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4540 ERR_GET_REASON(error) ==
4541 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4542 context->event_cb(
4543 context->cb_ctx,
4544 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4545 NULL);
4546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004548 if (!conn->server && !conn->client_hello_generated) {
4549 /* The server would not understand TLS Alert
4550 * before ClientHello, so simply terminate
4551 * handshake on this type of error case caused
4552 * by a likely internal error like no ciphers
4553 * available. */
4554 wpa_printf(MSG_DEBUG,
4555 "OpenSSL: Could not generate ClientHello");
4556 conn->write_alerts++;
4557 return NULL;
4558 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 }
4560 }
4561
Roshan Pius3a1667e2018-07-03 15:17:14 -07004562 if (!conn->server && !conn->failed)
4563 conn->client_hello_generated = 1;
4564
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004565#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004566 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004567 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4568 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004569 /*
4570 * This should not be reached since earlier cert_cb should have
4571 * terminated the handshake. Keep this check here for extra
4572 * protection if anything goes wrong with the more low-level
4573 * checks based on having to parse the TLS handshake messages.
4574 */
4575 wpa_printf(MSG_DEBUG,
4576 "OpenSSL: Server DH prime length: %d bits",
4577 conn->server_dh_prime_len);
4578
4579 if (context->event_cb) {
4580 union tls_event_data ev;
4581
4582 os_memset(&ev, 0, sizeof(ev));
4583 ev.alert.is_local = 1;
4584 ev.alert.type = "fatal";
4585 ev.alert.description = "insufficient security";
4586 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4587 }
4588 /*
4589 * Could send a TLS Alert to the server, but for now, simply
4590 * terminate handshake.
4591 */
4592 conn->failed++;
4593 conn->write_alerts++;
4594 return NULL;
4595 }
4596#endif /* CONFIG_SUITEB */
4597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 /* Get the TLS handshake data to be sent to the server */
4599 res = BIO_ctrl_pending(conn->ssl_out);
4600 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4601 out_data = wpabuf_alloc(res);
4602 if (out_data == NULL) {
4603 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4604 "handshake output (%d bytes)", res);
4605 if (BIO_reset(conn->ssl_out) < 0) {
4606 tls_show_errors(MSG_INFO, __func__,
4607 "BIO_reset failed");
4608 }
4609 return NULL;
4610 }
4611 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4612 res);
4613 if (res < 0) {
4614 tls_show_errors(MSG_INFO, __func__,
4615 "Handshake failed - BIO_read");
4616 if (BIO_reset(conn->ssl_out) < 0) {
4617 tls_show_errors(MSG_INFO, __func__,
4618 "BIO_reset failed");
4619 }
4620 wpabuf_free(out_data);
4621 return NULL;
4622 }
4623 wpabuf_put(out_data, res);
4624
4625 return out_data;
4626}
4627
4628
4629static struct wpabuf *
4630openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4631{
4632 struct wpabuf *appl_data;
4633 int res;
4634
4635 appl_data = wpabuf_alloc(max_len + 100);
4636 if (appl_data == NULL)
4637 return NULL;
4638
4639 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4640 wpabuf_size(appl_data));
4641 if (res < 0) {
4642 int err = SSL_get_error(conn->ssl, res);
4643 if (err == SSL_ERROR_WANT_READ ||
4644 err == SSL_ERROR_WANT_WRITE) {
4645 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4646 "included");
4647 } else {
4648 tls_show_errors(MSG_INFO, __func__,
4649 "Failed to read possible "
4650 "Application Data");
4651 }
4652 wpabuf_free(appl_data);
4653 return NULL;
4654 }
4655
4656 wpabuf_put(appl_data, res);
4657 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4658 "message", appl_data);
4659
4660 return appl_data;
4661}
4662
4663
4664static struct wpabuf *
4665openssl_connection_handshake(struct tls_connection *conn,
4666 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004667 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668{
4669 struct wpabuf *out_data;
4670
4671 if (appl_data)
4672 *appl_data = NULL;
4673
Roshan Pius3a1667e2018-07-03 15:17:14 -07004674 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 if (out_data == NULL)
4676 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004677 if (conn->invalid_hb_used) {
4678 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4679 wpabuf_free(out_data);
4680 return NULL;
4681 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004683 if (SSL_is_init_finished(conn->ssl)) {
4684 wpa_printf(MSG_DEBUG,
4685 "OpenSSL: Handshake finished - resumed=%d",
4686 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004687 if (conn->server) {
4688 char *buf;
4689 size_t buflen = 2000;
4690
4691 buf = os_malloc(buflen);
4692 if (buf) {
4693 if (SSL_get_shared_ciphers(conn->ssl, buf,
4694 buflen)) {
4695 buf[buflen - 1] = '\0';
4696 wpa_printf(MSG_DEBUG,
4697 "OpenSSL: Shared ciphers: %s",
4698 buf);
4699 }
4700 os_free(buf);
4701 }
4702 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004703 if (appl_data && in_data)
4704 *appl_data = openssl_get_appl_data(conn,
4705 wpabuf_len(in_data));
4706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
Jouni Malinen26af48b2014-04-09 13:02:53 +03004708 if (conn->invalid_hb_used) {
4709 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4710 if (appl_data) {
4711 wpabuf_free(*appl_data);
4712 *appl_data = NULL;
4713 }
4714 wpabuf_free(out_data);
4715 return NULL;
4716 }
4717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 return out_data;
4719}
4720
4721
4722struct wpabuf *
4723tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4724 const struct wpabuf *in_data,
4725 struct wpabuf **appl_data)
4726{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004727 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728}
4729
4730
4731struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4732 struct tls_connection *conn,
4733 const struct wpabuf *in_data,
4734 struct wpabuf **appl_data)
4735{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004736 conn->server = 1;
4737 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738}
4739
4740
4741struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4742 struct tls_connection *conn,
4743 const struct wpabuf *in_data)
4744{
4745 int res;
4746 struct wpabuf *buf;
4747
4748 if (conn == NULL)
4749 return NULL;
4750
4751 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4752 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4753 (res = BIO_reset(conn->ssl_out)) < 0) {
4754 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4755 return NULL;
4756 }
4757 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4758 if (res < 0) {
4759 tls_show_errors(MSG_INFO, __func__,
4760 "Encryption failed - SSL_write");
4761 return NULL;
4762 }
4763
4764 /* Read encrypted data to be sent to the server */
4765 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4766 if (buf == NULL)
4767 return NULL;
4768 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4769 if (res < 0) {
4770 tls_show_errors(MSG_INFO, __func__,
4771 "Encryption failed - BIO_read");
4772 wpabuf_free(buf);
4773 return NULL;
4774 }
4775 wpabuf_put(buf, res);
4776
4777 return buf;
4778}
4779
4780
4781struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4782 struct tls_connection *conn,
4783 const struct wpabuf *in_data)
4784{
4785 int res;
4786 struct wpabuf *buf;
4787
4788 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4789 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4790 wpabuf_len(in_data));
4791 if (res < 0) {
4792 tls_show_errors(MSG_INFO, __func__,
4793 "Decryption failed - BIO_write");
4794 return NULL;
4795 }
4796 if (BIO_reset(conn->ssl_out) < 0) {
4797 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4798 return NULL;
4799 }
4800
4801 /* Read decrypted data for further processing */
4802 /*
4803 * Even though we try to disable TLS compression, it is possible that
4804 * this cannot be done with all TLS libraries. Add extra buffer space
4805 * to handle the possibility of the decrypted data being longer than
4806 * input data.
4807 */
4808 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4809 if (buf == NULL)
4810 return NULL;
4811 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4812 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004813 int err = SSL_get_error(conn->ssl, res);
4814
4815 if (err == SSL_ERROR_WANT_READ) {
4816 wpa_printf(MSG_DEBUG,
4817 "SSL: SSL_connect - want more data");
4818 res = 0;
4819 } else {
4820 tls_show_errors(MSG_INFO, __func__,
4821 "Decryption failed - SSL_read");
4822 wpabuf_free(buf);
4823 return NULL;
4824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 }
4826 wpabuf_put(buf, res);
4827
Jouni Malinen26af48b2014-04-09 13:02:53 +03004828 if (conn->invalid_hb_used) {
4829 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4830 wpabuf_free(buf);
4831 return NULL;
4832 }
4833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 return buf;
4835}
4836
4837
4838int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4839{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004840 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841}
4842
4843
4844int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4845 u8 *ciphers)
4846{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004847 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 u8 *c;
4849 int ret;
4850
4851 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4852 return -1;
4853
4854 buf[0] = '\0';
4855 pos = buf;
4856 end = pos + sizeof(buf);
4857
4858 c = ciphers;
4859 while (*c != TLS_CIPHER_NONE) {
4860 const char *suite;
4861
4862 switch (*c) {
4863 case TLS_CIPHER_RC4_SHA:
4864 suite = "RC4-SHA";
4865 break;
4866 case TLS_CIPHER_AES128_SHA:
4867 suite = "AES128-SHA";
4868 break;
4869 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4870 suite = "DHE-RSA-AES128-SHA";
4871 break;
4872 case TLS_CIPHER_ANON_DH_AES128_SHA:
4873 suite = "ADH-AES128-SHA";
4874 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004875 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4876 suite = "DHE-RSA-AES256-SHA";
4877 break;
4878 case TLS_CIPHER_AES256_SHA:
4879 suite = "AES256-SHA";
4880 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 default:
4882 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4883 "cipher selection: %d", *c);
4884 return -1;
4885 }
4886 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004887 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 break;
4889 pos += ret;
4890
4891 c++;
4892 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004893 if (!buf[0]) {
4894 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4895 return -1;
4896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897
4898 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4899
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004900#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004901#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004902 if (os_strstr(buf, ":ADH-")) {
4903 /*
4904 * Need to drop to security level 0 to allow anonymous
4905 * cipher suites for EAP-FAST.
4906 */
4907 SSL_set_security_level(conn->ssl, 0);
4908 } else if (SSL_get_security_level(conn->ssl) == 0) {
4909 /* Force at least security level 1 */
4910 SSL_set_security_level(conn->ssl, 1);
4911 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004912#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004913#endif
4914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4916 tls_show_errors(MSG_INFO, __func__,
4917 "Cipher suite configuration failed");
4918 return -1;
4919 }
4920
4921 return 0;
4922}
4923
4924
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004925int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4926 char *buf, size_t buflen)
4927{
4928 const char *name;
4929 if (conn == NULL || conn->ssl == NULL)
4930 return -1;
4931
4932 name = SSL_get_version(conn->ssl);
4933 if (name == NULL)
4934 return -1;
4935
4936 os_strlcpy(buf, name, buflen);
4937 return 0;
4938}
4939
4940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4942 char *buf, size_t buflen)
4943{
4944 const char *name;
4945 if (conn == NULL || conn->ssl == NULL)
4946 return -1;
4947
4948 name = SSL_get_cipher(conn->ssl);
4949 if (name == NULL)
4950 return -1;
4951
4952 os_strlcpy(buf, name, buflen);
4953 return 0;
4954}
4955
4956
4957int tls_connection_enable_workaround(void *ssl_ctx,
4958 struct tls_connection *conn)
4959{
4960 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4961
4962 return 0;
4963}
4964
4965
Hai Shalom81f62d82019-07-22 12:10:00 -07004966#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967/* ClientHello TLS extensions require a patch to openssl, so this function is
4968 * commented out unless explicitly needed for EAP-FAST in order to be able to
4969 * build this file with unmodified openssl. */
4970int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4971 int ext_type, const u8 *data,
4972 size_t data_len)
4973{
4974 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4975 return -1;
4976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4978 data_len) != 1)
4979 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980
4981 return 0;
4982}
Hai Shalom81f62d82019-07-22 12:10:00 -07004983#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984
4985
4986int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4987{
4988 if (conn == NULL)
4989 return -1;
4990 return conn->failed;
4991}
4992
4993
4994int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4995{
4996 if (conn == NULL)
4997 return -1;
4998 return conn->read_alerts;
4999}
5000
5001
5002int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
5003{
5004 if (conn == NULL)
5005 return -1;
5006 return conn->write_alerts;
5007}
5008
5009
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005010#ifdef HAVE_OCSP
5011
5012static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5013{
5014#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005015 BIO *out;
5016 size_t rlen;
5017 char *txt;
5018 int res;
5019
5020 if (wpa_debug_level > MSG_DEBUG)
5021 return;
5022
5023 out = BIO_new(BIO_s_mem());
5024 if (!out)
5025 return;
5026
5027 OCSP_RESPONSE_print(out, rsp, 0);
5028 rlen = BIO_ctrl_pending(out);
5029 txt = os_malloc(rlen + 1);
5030 if (!txt) {
5031 BIO_free(out);
5032 return;
5033 }
5034
5035 res = BIO_read(out, txt, rlen);
5036 if (res > 0) {
5037 txt[res] = '\0';
5038 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5039 }
5040 os_free(txt);
5041 BIO_free(out);
5042#endif /* CONFIG_NO_STDOUT_DEBUG */
5043}
5044
5045
5046static int ocsp_resp_cb(SSL *s, void *arg)
5047{
5048 struct tls_connection *conn = arg;
5049 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005050 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005051 OCSP_RESPONSE *rsp;
5052 OCSP_BASICRESP *basic;
5053 OCSP_CERTID *id;
5054 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005055 X509_STORE *store;
5056 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005057
5058 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5059 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005060#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5061#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5062 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5063 /* TLS 1.3 sends the OCSP response with the server
5064 * Certificate message. Since that Certificate message
5065 * is not sent when resuming a session, there can be no
5066 * new OCSP response. Allow this since the OCSP response
5067 * was validated when checking the initial certificate
5068 * exchange. */
5069 wpa_printf(MSG_DEBUG,
5070 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5071 return 1;
5072 }
5073#endif
5074#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005075 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5076 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5077 }
5078
5079 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5080
5081 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5082 if (!rsp) {
5083 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5084 return 0;
5085 }
5086
5087 ocsp_debug_print_resp(rsp);
5088
5089 status = OCSP_response_status(rsp);
5090 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5091 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5092 status, OCSP_response_status_str(status));
5093 return 0;
5094 }
5095
5096 basic = OCSP_response_get1_basic(rsp);
5097 if (!basic) {
5098 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5099 return 0;
5100 }
5101
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005102 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005103 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005104 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005105
5106 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5107 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005108 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005109 }
5110 certs = sk_X509_new_null();
5111 if (certs) {
5112 X509 *cert;
5113 cert = X509_dup(conn->peer_issuer);
5114 if (cert && !sk_X509_push(certs, cert)) {
5115 tls_show_errors(
5116 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005117 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005118 X509_free(cert);
5119 sk_X509_free(certs);
5120 certs = NULL;
5121 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005122 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005123 cert = X509_dup(conn->peer_issuer_issuer);
5124 if (cert && !sk_X509_push(certs, cert)) {
5125 tls_show_errors(
5126 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005127 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005128 X509_free(cert);
5129 }
5130 }
5131 }
5132 }
5133
5134 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5135 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005136 if (status <= 0) {
5137 tls_show_errors(MSG_INFO, __func__,
5138 "OpenSSL: OCSP response failed verification");
5139 OCSP_BASICRESP_free(basic);
5140 OCSP_RESPONSE_free(rsp);
5141 return 0;
5142 }
5143
5144 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5145
Dmitry Shmidt56052862013-10-04 10:23:25 -07005146 if (!conn->peer_cert) {
5147 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5148 OCSP_BASICRESP_free(basic);
5149 OCSP_RESPONSE_free(rsp);
5150 return 0;
5151 }
5152
5153 if (!conn->peer_issuer) {
5154 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005155 OCSP_BASICRESP_free(basic);
5156 OCSP_RESPONSE_free(rsp);
5157 return 0;
5158 }
5159
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005160 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005161 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005162 wpa_printf(MSG_DEBUG,
5163 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005164 OCSP_BASICRESP_free(basic);
5165 OCSP_RESPONSE_free(rsp);
5166 return 0;
5167 }
5168
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005169 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5170 &this_update, &next_update);
5171 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005172 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005173 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5174 if (!id) {
5175 wpa_printf(MSG_DEBUG,
5176 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5177 OCSP_BASICRESP_free(basic);
5178 OCSP_RESPONSE_free(rsp);
5179 return 0;
5180 }
5181
5182 res = OCSP_resp_find_status(basic, id, &status, &reason,
5183 &produced_at, &this_update,
5184 &next_update);
5185 }
5186
5187 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005188 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5189 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5190 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005191 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005192 OCSP_BASICRESP_free(basic);
5193 OCSP_RESPONSE_free(rsp);
5194 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5195 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005196 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005197
5198 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5199 tls_show_errors(MSG_INFO, __func__,
5200 "OpenSSL: OCSP status times invalid");
5201 OCSP_BASICRESP_free(basic);
5202 OCSP_RESPONSE_free(rsp);
5203 return 0;
5204 }
5205
5206 OCSP_BASICRESP_free(basic);
5207 OCSP_RESPONSE_free(rsp);
5208
5209 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5210 OCSP_cert_status_str(status));
5211
5212 if (status == V_OCSP_CERTSTATUS_GOOD)
5213 return 1;
5214 if (status == V_OCSP_CERTSTATUS_REVOKED)
5215 return 0;
5216 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5217 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5218 return 0;
5219 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005220 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 -07005221 return 1;
5222}
5223
5224
5225static int ocsp_status_cb(SSL *s, void *arg)
5226{
5227 char *tmp;
5228 char *resp;
5229 size_t len;
5230
5231 if (tls_global->ocsp_stapling_response == NULL) {
5232 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5233 return SSL_TLSEXT_ERR_OK;
5234 }
5235
5236 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5237 if (resp == NULL) {
5238 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5239 /* TODO: Build OCSPResponse with responseStatus = internalError
5240 */
5241 return SSL_TLSEXT_ERR_OK;
5242 }
5243 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5244 tmp = OPENSSL_malloc(len);
5245 if (tmp == NULL) {
5246 os_free(resp);
5247 return SSL_TLSEXT_ERR_ALERT_FATAL;
5248 }
5249
5250 os_memcpy(tmp, resp, len);
5251 os_free(resp);
5252 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5253
5254 return SSL_TLSEXT_ERR_OK;
5255}
5256
5257#endif /* HAVE_OCSP */
5258
5259
Hai Shalomfdcde762020-04-02 11:19:20 -07005260static size_t max_str_len(const char **lines)
5261{
5262 const char **p;
5263 size_t max_len = 0;
5264
5265 for (p = lines; *p; p++) {
5266 size_t len = os_strlen(*p);
5267
5268 if (len > max_len)
5269 max_len = len;
5270 }
5271
5272 return max_len;
5273}
5274
5275
5276static int match_lines_in_file(const char *path, const char **lines)
5277{
5278 FILE *f;
5279 char *buf;
5280 size_t bufsize;
5281 int found = 0, is_linestart = 1;
5282
5283 bufsize = max_str_len(lines) + sizeof("\r\n");
5284 buf = os_malloc(bufsize);
5285 if (!buf)
5286 return 0;
5287
5288 f = fopen(path, "r");
5289 if (!f) {
5290 os_free(buf);
5291 return 0;
5292 }
5293
5294 while (!found && fgets(buf, bufsize, f)) {
5295 int is_lineend;
5296 size_t len;
5297 const char **p;
5298
5299 len = strcspn(buf, "\r\n");
5300 is_lineend = buf[len] != '\0';
5301 buf[len] = '\0';
5302
5303 if (is_linestart && is_lineend) {
5304 for (p = lines; !found && *p; p++)
5305 found = os_strcmp(buf, *p) == 0;
5306 }
5307 is_linestart = is_lineend;
5308 }
5309
5310 fclose(f);
5311 bin_clear_free(buf, bufsize);
5312
5313 return found;
5314}
5315
5316
5317static int is_tpm2_key(const char *path)
5318{
5319 /* Check both new and old format of TPM2 PEM guard tag */
5320 static const char *tpm2_tags[] = {
5321 "-----BEGIN TSS2 PRIVATE KEY-----",
5322 "-----BEGIN TSS2 KEY BLOB-----",
5323 NULL
5324 };
5325
5326 return match_lines_in_file(path, tpm2_tags);
5327}
5328
5329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5331 const struct tls_connection_params *params)
5332{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005333 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334 int ret;
5335 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005336 int can_pkcs11 = 0;
5337 const char *key_id = params->key_id;
5338 const char *cert_id = params->cert_id;
5339 const char *ca_cert_id = params->ca_cert_id;
5340 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005341 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342
5343 if (conn == NULL)
5344 return -1;
5345
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005346 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5347 wpa_printf(MSG_INFO,
5348 "OpenSSL: ocsp=3 not supported");
5349 return -1;
5350 }
5351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005352 /*
5353 * If the engine isn't explicitly configured, and any of the
5354 * cert/key fields are actually PKCS#11 URIs, then automatically
5355 * use the PKCS#11 ENGINE.
5356 */
5357 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5358 can_pkcs11 = 1;
5359
5360 if (!key_id && params->private_key && can_pkcs11 &&
5361 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5362 can_pkcs11 = 2;
5363 key_id = params->private_key;
5364 }
5365
5366 if (!cert_id && params->client_cert && can_pkcs11 &&
5367 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5368 can_pkcs11 = 2;
5369 cert_id = params->client_cert;
5370 }
5371
5372 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5373 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5374 can_pkcs11 = 2;
5375 ca_cert_id = params->ca_cert;
5376 }
5377
5378 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5379 if (can_pkcs11 == 2 && !engine_id)
5380 engine_id = "pkcs11";
5381
Hai Shalomfdcde762020-04-02 11:19:20 -07005382 /* If private_key points to a TPM2-wrapped key, automatically enable
5383 * tpm2 engine and use it to unwrap the key. */
5384 if (params->private_key &&
5385 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5386 is_tpm2_key(params->private_key)) {
5387 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5388 params->private_key);
5389 key_id = key_id ? key_id : params->private_key;
5390 engine_id = engine_id ? engine_id : "tpm2";
5391 }
5392
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005393#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005394#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005395 if (params->flags & TLS_CONN_EAP_FAST) {
5396 wpa_printf(MSG_DEBUG,
5397 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5398 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5399 tls_show_errors(MSG_INFO, __func__,
5400 "Failed to set TLSv1_method() for EAP-FAST");
5401 return -1;
5402 }
5403 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005404#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005405#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5406#ifdef SSL_OP_NO_TLSv1_3
5407 if (params->flags & TLS_CONN_EAP_FAST) {
5408 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5409 * refuses to start the handshake with the modified ciphersuite
5410 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5411 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5412 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5413 }
5414#endif /* SSL_OP_NO_TLSv1_3 */
5415#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005416#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005418 while ((err = ERR_get_error())) {
5419 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5420 __func__, ERR_error_string(err, NULL));
5421 }
5422
Sunil Ravi77d572f2023-01-17 23:58:31 +00005423 if (tls_set_conn_flags(conn, params->flags,
5424 params->openssl_ciphers) < 0) {
5425 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
5426 return -1;
5427 }
5428
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005429 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005430 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5431 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005432 ret = tls_engine_init(conn, engine_id, params->pin,
5433 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 if (ret)
5435 return ret;
5436 }
5437 if (tls_connection_set_subject_match(conn,
5438 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005439 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005440 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005441 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005442 params->check_cert_subject)) {
5443 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
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 && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005448 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005450 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 params->ca_cert_blob,
5452 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005453 params->ca_path)) {
5454 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005458 if (engine_id && cert_id) {
5459 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5461 } else if (tls_connection_client_cert(conn, params->client_cert,
5462 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005463 params->client_cert_blob_len)) {
5464 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005468 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5470 if (tls_connection_engine_private_key(conn))
5471 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005472 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 params->private_key,
5474 params->private_key_passwd,
5475 params->private_key_blob,
5476 params->private_key_blob_len)) {
5477 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5478 params->private_key);
5479 return -1;
5480 }
5481
Roshan Pius3a1667e2018-07-03 15:17:14 -07005482 ciphers = params->openssl_ciphers;
5483#ifdef CONFIG_SUITEB
5484#ifdef OPENSSL_IS_BORINGSSL
5485 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5486 /* BoringSSL removed support for SUITEB192, so need to handle
5487 * this with hardcoded ciphersuite and additional checks for
5488 * other parameters. */
5489 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5490 }
5491#endif /* OPENSSL_IS_BORINGSSL */
5492#endif /* CONFIG_SUITEB */
5493 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005494 wpa_printf(MSG_INFO,
5495 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005496 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005497 return -1;
5498 }
5499
Hai Shalom74f70d42019-02-11 14:42:39 -08005500 if (!params->openssl_ecdh_curves) {
5501#ifndef OPENSSL_IS_BORINGSSL
5502#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005503#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005504 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5505 wpa_printf(MSG_INFO,
5506 "OpenSSL: Failed to set ECDH curves to auto");
5507 return -1;
5508 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005509#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005510#endif /* OPENSSL_NO_EC */
5511#endif /* OPENSSL_IS_BORINGSSL */
5512 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005513#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005514 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005515 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005516 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005517#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005518#ifndef OPENSSL_NO_EC
5519 if (SSL_set1_curves_list(conn->ssl,
5520 params->openssl_ecdh_curves) != 1) {
5521 wpa_printf(MSG_INFO,
5522 "OpenSSL: Failed to set ECDH curves '%s'",
5523 params->openssl_ecdh_curves);
5524 return -1;
5525 }
5526#else /* OPENSSL_NO_EC */
5527 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5528 return -1;
5529#endif /* OPENSSL_NO_EC */
5530#endif /* OPENSSL_IS_BORINGSSL */
5531 }
5532
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005533#ifdef OPENSSL_IS_BORINGSSL
5534 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5535 SSL_enable_ocsp_stapling(conn->ssl);
5536 }
5537#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005538#ifdef HAVE_OCSP
5539 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005540 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005541 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5542 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5543 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5544 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005545#else /* HAVE_OCSP */
5546 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5547 wpa_printf(MSG_INFO,
5548 "OpenSSL: No OCSP support included - reject configuration");
5549 return -1;
5550 }
5551 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5552 wpa_printf(MSG_DEBUG,
5553 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5554 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005555#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005556#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005557
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005558 conn->flags = params->flags;
5559
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005560 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561
5562 return 0;
5563}
5564
5565
Hai Shalom81f62d82019-07-22 12:10:00 -07005566static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5567{
5568 SSL *ssl;
5569 int i;
5570
5571 ssl = SSL_new(ssl_ctx);
5572 if (!ssl)
5573 return;
5574
5575 wpa_printf(MSG_DEBUG,
5576 "OpenSSL: Enabled cipher suites in priority order");
5577 for (i = 0; ; i++) {
5578 const char *cipher;
5579
5580 cipher = SSL_get_cipher_list(ssl, i);
5581 if (!cipher)
5582 break;
5583 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5584 }
5585
5586 SSL_free(ssl);
5587}
5588
5589
5590#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5591
5592static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5593{
5594 if (!pkey)
5595 return "NULL";
5596 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5597 case EVP_PKEY_RSA:
5598 return "RSA";
5599 case EVP_PKEY_DSA:
5600 return "DSA";
5601 case EVP_PKEY_DH:
5602 return "DH";
5603 case EVP_PKEY_EC:
5604 return "EC";
Sunil Ravi77d572f2023-01-17 23:58:31 +00005605 default:
5606 return "?";
Hai Shalom81f62d82019-07-22 12:10:00 -07005607 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005608}
5609
5610
5611static void openssl_debug_dump_certificate(int i, X509 *cert)
5612{
5613 char buf[256];
5614 EVP_PKEY *pkey;
5615 ASN1_INTEGER *ser;
5616 char serial_num[128];
5617
Hai Shalom60840252021-02-19 19:02:11 -08005618 if (!cert)
5619 return;
5620
Hai Shalom81f62d82019-07-22 12:10:00 -07005621 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5622
5623 ser = X509_get_serialNumber(cert);
5624 if (ser)
5625 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5626 ASN1_STRING_get0_data(ser),
5627 ASN1_STRING_length(ser));
5628 else
5629 serial_num[0] = '\0';
5630
5631 pkey = X509_get_pubkey(cert);
5632 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5633 openssl_pkey_type_str(pkey), serial_num);
5634 EVP_PKEY_free(pkey);
5635}
5636
5637
5638static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5639{
5640 STACK_OF(X509) *certs;
5641
5642 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5643 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5644 int i;
5645
5646 for (i = sk_X509_num(certs); i > 0; i--)
5647 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5648 i - 1));
5649 }
5650 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5651}
5652
5653#endif
5654
5655
5656static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5657{
5658#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5659 int res;
5660
5661 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5662 res == 1;
5663 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5664 openssl_debug_dump_certificates(ssl_ctx);
5665
5666 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5667#endif
5668}
5669
5670
5671static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5672{
5673 openssl_debug_dump_cipher_list(ssl_ctx);
5674 openssl_debug_dump_certificate_chains(ssl_ctx);
5675}
5676
5677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678int tls_global_set_params(void *tls_ctx,
5679 const struct tls_connection_params *params)
5680{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005681 struct tls_data *data = tls_ctx;
5682 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683 unsigned long err;
5684
5685 while ((err = ERR_get_error())) {
5686 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5687 __func__, ERR_error_string(err, NULL));
5688 }
5689
Hai Shalom021b0b52019-04-10 11:17:58 -07005690 os_free(data->check_cert_subject);
5691 data->check_cert_subject = NULL;
5692 if (params->check_cert_subject) {
5693 data->check_cert_subject =
5694 os_strdup(params->check_cert_subject);
5695 if (!data->check_cert_subject)
5696 return -1;
5697 }
5698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005699 if (tls_global_ca_cert(data, params->ca_cert) ||
5700 tls_global_client_cert(data, params->client_cert) ||
5701 tls_global_private_key(data, params->private_key,
5702 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005703 tls_global_client_cert(data, params->client_cert2) ||
5704 tls_global_private_key(data, params->private_key2,
5705 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005706 tls_global_dh(data, params->dh_file)) {
5707 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708 return -1;
5709 }
5710
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005711 if (params->openssl_ciphers &&
5712 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5713 wpa_printf(MSG_INFO,
5714 "OpenSSL: Failed to set cipher string '%s'",
5715 params->openssl_ciphers);
5716 return -1;
5717 }
5718
Hai Shalom74f70d42019-02-11 14:42:39 -08005719 if (!params->openssl_ecdh_curves) {
5720#ifndef OPENSSL_IS_BORINGSSL
5721#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005722#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005723 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5724 wpa_printf(MSG_INFO,
5725 "OpenSSL: Failed to set ECDH curves to auto");
5726 return -1;
5727 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005728#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005729#endif /* OPENSSL_NO_EC */
5730#endif /* OPENSSL_IS_BORINGSSL */
5731 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005732#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005733 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005734 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005735 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005736#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005737#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005738#if OPENSSL_VERSION_NUMBER < 0x10100000L
5739 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5740#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005741 if (SSL_CTX_set1_curves_list(ssl_ctx,
5742 params->openssl_ecdh_curves) !=
5743 1) {
5744 wpa_printf(MSG_INFO,
5745 "OpenSSL: Failed to set ECDH curves '%s'",
5746 params->openssl_ecdh_curves);
5747 return -1;
5748 }
5749#else /* OPENSSL_NO_EC */
5750 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5751 return -1;
5752#endif /* OPENSSL_NO_EC */
5753#endif /* OPENSSL_IS_BORINGSSL */
5754 }
5755
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005756#ifdef SSL_OP_NO_TICKET
5757 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5758 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5759 else
5760 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5761#endif /* SSL_OP_NO_TICKET */
5762
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005763#ifdef HAVE_OCSP
5764 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5765 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5766 os_free(tls_global->ocsp_stapling_response);
5767 if (params->ocsp_stapling_response)
5768 tls_global->ocsp_stapling_response =
5769 os_strdup(params->ocsp_stapling_response);
5770 else
5771 tls_global->ocsp_stapling_response = NULL;
5772#endif /* HAVE_OCSP */
5773
Hai Shalom81f62d82019-07-22 12:10:00 -07005774 openssl_debug_dump_ctx(ssl_ctx);
5775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776 return 0;
5777}
5778
5779
Hai Shalom81f62d82019-07-22 12:10:00 -07005780#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781/* Pre-shared secred requires a patch to openssl, so this function is
5782 * commented out unless explicitly needed for EAP-FAST in order to be able to
5783 * build this file with unmodified openssl. */
5784
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005785#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005786static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5787 STACK_OF(SSL_CIPHER) *peer_ciphers,
5788 const SSL_CIPHER **cipher, void *arg)
5789#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5791 STACK_OF(SSL_CIPHER) *peer_ciphers,
5792 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005793#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794{
5795 struct tls_connection *conn = arg;
5796 int ret;
5797
Sunil Ravia04bd252022-05-02 22:54:18 -07005798#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005799 if (conn == NULL || conn->session_ticket_cb == NULL)
5800 return 0;
5801
5802 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5803 conn->session_ticket,
5804 conn->session_ticket_len,
5805 s->s3->client_random,
5806 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005807#else
5808 unsigned char client_random[SSL3_RANDOM_SIZE];
5809 unsigned char server_random[SSL3_RANDOM_SIZE];
5810
5811 if (conn == NULL || conn->session_ticket_cb == NULL)
5812 return 0;
5813
5814 SSL_get_client_random(s, client_random, sizeof(client_random));
5815 SSL_get_server_random(s, server_random, sizeof(server_random));
5816
5817 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5818 conn->session_ticket,
5819 conn->session_ticket_len,
5820 client_random,
5821 server_random, secret);
5822#endif
5823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824 os_free(conn->session_ticket);
5825 conn->session_ticket = NULL;
5826
5827 if (ret <= 0)
5828 return 0;
5829
5830 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5831 return 1;
5832}
5833
5834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5836 int len, void *arg)
5837{
5838 struct tls_connection *conn = arg;
5839
5840 if (conn == NULL || conn->session_ticket_cb == NULL)
5841 return 0;
5842
5843 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5844
5845 os_free(conn->session_ticket);
5846 conn->session_ticket = NULL;
5847
5848 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5849 "extension", data, len);
5850
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005851 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852 if (conn->session_ticket == NULL)
5853 return 0;
5854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855 conn->session_ticket_len = len;
5856
5857 return 1;
5858}
Hai Shalom81f62d82019-07-22 12:10:00 -07005859#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860
5861
5862int tls_connection_set_session_ticket_cb(void *tls_ctx,
5863 struct tls_connection *conn,
5864 tls_session_ticket_cb cb,
5865 void *ctx)
5866{
Hai Shalom81f62d82019-07-22 12:10:00 -07005867#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868 conn->session_ticket_cb = cb;
5869 conn->session_ticket_cb_ctx = ctx;
5870
5871 if (cb) {
5872 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5873 conn) != 1)
5874 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875 SSL_set_session_ticket_ext_cb(conn->ssl,
5876 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877 } else {
5878 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5879 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005881 }
5882
5883 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005884#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005885 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005886#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005887}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005888
5889
5890int tls_get_library_version(char *buf, size_t buf_len)
5891{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005892#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005893 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5894 OPENSSL_VERSION_TEXT,
5895 OpenSSL_version(OPENSSL_VERSION));
5896#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005897 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5898 OPENSSL_VERSION_TEXT,
5899 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005900#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005901}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005902
5903
5904void tls_connection_set_success_data(struct tls_connection *conn,
5905 struct wpabuf *data)
5906{
5907 SSL_SESSION *sess;
5908 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005909 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005910
5911 if (tls_ex_idx_session < 0)
5912 goto fail;
5913 sess = SSL_get_session(conn->ssl);
5914 if (!sess)
5915 goto fail;
5916 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5917 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005918 struct tls_session_data *found;
5919
5920 found = get_session_data(conn->context, old);
5921 wpa_printf(MSG_DEBUG,
5922 "OpenSSL: Replacing old success data %p (sess %p)%s",
5923 old, sess, found ? "" : " (not freeing)");
5924 if (found) {
5925 dl_list_del(&found->list);
5926 os_free(found);
5927 wpabuf_free(old);
5928 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005929 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005930
5931 sess_data = os_zalloc(sizeof(*sess_data));
5932 if (!sess_data ||
5933 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005934 goto fail;
5935
Sunil Ravia04bd252022-05-02 22:54:18 -07005936 sess_data->buf = data;
5937 dl_list_add(&conn->context->sessions, &sess_data->list);
5938 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5939 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005940 conn->success_data = 1;
5941 return;
5942
5943fail:
5944 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
5945 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07005946 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005947}
5948
5949
5950void tls_connection_set_success_data_resumed(struct tls_connection *conn)
5951{
5952 wpa_printf(MSG_DEBUG,
5953 "OpenSSL: Success data accepted for resumed session");
5954 conn->success_data = 1;
5955}
5956
5957
5958const struct wpabuf *
5959tls_connection_get_success_data(struct tls_connection *conn)
5960{
5961 SSL_SESSION *sess;
5962
5963 if (tls_ex_idx_session < 0 ||
5964 !(sess = SSL_get_session(conn->ssl)))
5965 return NULL;
5966 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5967}
5968
5969
5970void tls_connection_remove_session(struct tls_connection *conn)
5971{
5972 SSL_SESSION *sess;
5973
5974 sess = SSL_get_session(conn->ssl);
5975 if (!sess)
5976 return;
5977
5978 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5979 wpa_printf(MSG_DEBUG,
5980 "OpenSSL: Session was not cached");
5981 else
5982 wpa_printf(MSG_DEBUG,
5983 "OpenSSL: Removed cached session to disable session resumption");
5984}
Hai Shalom81f62d82019-07-22 12:10:00 -07005985
5986
5987int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
5988{
5989 size_t len;
5990 int reused;
5991
5992 reused = SSL_session_reused(conn->ssl);
5993 if ((conn->server && !reused) || (!conn->server && reused))
5994 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
5995 else
5996 len = SSL_get_finished(conn->ssl, buf, max_len);
5997
5998 if (len == 0 || len > max_len)
5999 return -1;
6000
6001 return len;
6002}
6003
6004
6005u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6006{
6007 const SSL_CIPHER *cipher;
6008
6009 cipher = SSL_get_current_cipher(conn->ssl);
6010 if (!cipher)
6011 return 0;
6012#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6013 return SSL_CIPHER_get_protocol_id(cipher);
6014#else
6015 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6016#endif
6017}
Hai Shalom899fcc72020-10-19 14:38:18 -07006018
6019
6020const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6021{
6022 if (conn)
6023 return conn->peer_subject;
6024 return NULL;
6025}
6026
6027
6028bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6029{
6030 if (conn)
6031 return SSL_get_certificate(conn->ssl) != NULL;
6032 return false;
6033}
Gabriel Birena5bdf372022-12-15 20:54:33 +00006034
6035void tls_register_cert_callback(tls_get_certificate_cb cb)
6036{
6037 certificate_callback_global = cb;
6038}