blob: edb3f0c757554a943545fee6ced40d2c8e315147 [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) {
Gabriel Biren980c48a2023-03-27 21:49:21 +0000175 wpa_printf(MSG_INFO, "Retrieving certificate using callback");
Gabriel Birena5bdf372022-12-15 20:54:33 +0000176 int length = (*certificate_callback_global)(tls_global->cb_ctx, alias, &value);
177 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
178 BIO_write(bio, value, length);
179 }
Dmitry Shmidtff079172013-11-08 14:10:30 -0800180 free(value);
181 return bio;
182}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800183
Hai Shalom7ad2a872021-08-02 18:56:55 -0700184static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800185{
Hai Shalom7ad2a872021-08-02 18:56:55 -0700186 BIO *bio = BIO_from_keystore(alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800187 STACK_OF(X509_INFO) *stack = NULL;
188 stack_index_t i;
Hai Shalom22171592021-04-02 16:05:23 -0700189 int ret = 0;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800190
Hai Shalom22171592021-04-02 16:05:23 -0700191 if (!bio) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700192 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
193 alias);
Hai Shalom22171592021-04-02 16:05:23 -0700194 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800195 }
196
Hai Shalom7ad2a872021-08-02 18:56:55 -0700197 // Keystore returns X.509 certificates in PEM encoding
Hai Shalom22171592021-04-02 16:05:23 -0700198 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
199 BIO_free(bio);
200
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800201 if (!stack) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700202 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
203 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800204 return -1;
205 }
206
207 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
208 X509_INFO *info = sk_X509_INFO_value(stack, i);
209
210 if (info->x509)
Hai Shalom22171592021-04-02 16:05:23 -0700211 if (!X509_STORE_add_cert(ctx, info->x509)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700212 wpa_printf(MSG_ERROR,
213 "OpenSSL: Failed to add Root CA certificate");
Hai Shalom22171592021-04-02 16:05:23 -0700214 ret = -1;
215 break;
216 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800217 if (info->crl)
218 X509_STORE_add_crl(ctx, info->crl);
219 }
220
221 sk_X509_INFO_pop_free(stack, X509_INFO_free);
Hai Shalom22171592021-04-02 16:05:23 -0700222 return ret;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800223}
224
225
226static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
Hai Shalom7ad2a872021-08-02 18:56:55 -0700227 const char *encoded_alias)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800228{
229 int rc = -1;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700230 int len = os_strlen(encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800231 unsigned char *decoded_alias;
232
233 if (len & 1) {
234 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
Hai Shalom7ad2a872021-08-02 18:56:55 -0700235 encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800236 return rc;
237 }
238
239 decoded_alias = os_malloc(len / 2 + 1);
240 if (decoded_alias) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700241 if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800242 decoded_alias[len / 2] = '\0';
243 rc = tls_add_ca_from_keystore(
244 ctx, (const char *) decoded_alias);
245 }
246 os_free(decoded_alias);
247 }
248
249 return rc;
250}
251
Dmitry Shmidtff079172013-11-08 14:10:30 -0800252#endif /* ANDROID */
253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800254struct tls_data {
255 SSL_CTX *ssl;
256 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -0800257 int check_crl;
258 int check_crl_strict;
259 char *ca_cert;
260 unsigned int crl_reload_interval;
261 struct os_reltime crl_last_reload;
Hai Shalom021b0b52019-04-10 11:17:58 -0700262 char *check_cert_subject;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800263};
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700266 struct tls_context *context;
Hai Shalom74f70d42019-02-11 14:42:39 -0800267 struct tls_data *data;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800268 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 SSL *ssl;
270 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700271#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 ENGINE *engine; /* functional reference to the engine */
273 EVP_PKEY *private_key; /* the private key if using engine */
274#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800275 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -0700276 char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 int read_alerts, write_alerts, failed;
278
279 tls_session_ticket_cb session_ticket_cb;
280 void *session_ticket_cb_ctx;
281
282 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
283 u8 *session_ticket;
284 size_t session_ticket_len;
285
286 unsigned int ca_cert_verify:1;
287 unsigned int cert_probe:1;
288 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300289 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800290 unsigned int success_data:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700291 unsigned int client_hello_generated:1;
292 unsigned int server:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293
294 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700295
296 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700297
298 X509 *peer_cert;
299 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 X509 *peer_issuer_issuer;
Hai Shalom899fcc72020-10-19 14:38:18 -0700301 char *peer_subject; /* peer subject info for authenticated peer */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800302
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800303 unsigned char client_random[SSL3_RANDOM_SIZE];
304 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700305
306 u16 cipher_suite;
307 int server_dh_prime_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308};
309
310
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700311static struct tls_context * tls_context_new(const struct tls_config *conf)
312{
313 struct tls_context *context = os_zalloc(sizeof(*context));
314 if (context == NULL)
315 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -0700316 dl_list_init(&context->sessions);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700317 if (conf) {
318 context->event_cb = conf->event_cb;
319 context->cb_ctx = conf->cb_ctx;
320 context->cert_in_cb = conf->cert_in_cb;
321 }
322 return context;
323}
324
325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326#ifdef CONFIG_NO_STDOUT_DEBUG
327
328static void _tls_show_errors(void)
329{
330 unsigned long err;
331
332 while ((err = ERR_get_error())) {
333 /* Just ignore the errors, since stdout is disabled */
334 }
335}
336#define tls_show_errors(l, f, t) _tls_show_errors()
337
338#else /* CONFIG_NO_STDOUT_DEBUG */
339
340static void tls_show_errors(int level, const char *func, const char *txt)
341{
342 unsigned long err;
343
344 wpa_printf(level, "OpenSSL: %s - %s %s",
345 func, txt, ERR_error_string(ERR_get_error(), NULL));
346
347 while ((err = ERR_get_error())) {
348 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
349 ERR_error_string(err, NULL));
350 }
351}
352
353#endif /* CONFIG_NO_STDOUT_DEBUG */
354
355
Hai Shalom74f70d42019-02-11 14:42:39 -0800356static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
357{
358 int flags;
359 X509_STORE *store;
360
361 store = X509_STORE_new();
362 if (!store) {
363 wpa_printf(MSG_DEBUG,
364 "OpenSSL: %s - failed to allocate new certificate store",
365 __func__);
366 return NULL;
367 }
368
369 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
370 tls_show_errors(MSG_WARNING, __func__,
371 "Failed to load root certificates");
372 X509_STORE_free(store);
373 return NULL;
374 }
375
376 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
377 if (check_crl == 2)
378 flags |= X509_V_FLAG_CRL_CHECK_ALL;
379
380 X509_STORE_set_flags(store, flags);
381
382 return store;
383}
384
385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386#ifdef CONFIG_NATIVE_WINDOWS
387
388/* Windows CryptoAPI and access to certificate stores */
389#include <wincrypt.h>
390
391#ifdef __MINGW32_VERSION
392/*
393 * MinGW does not yet include all the needed definitions for CryptoAPI, so
394 * define here whatever extra is needed.
395 */
396#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
397#define CERT_STORE_READONLY_FLAG 0x00008000
398#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
399
400#endif /* __MINGW32_VERSION */
401
402
403struct cryptoapi_rsa_data {
404 const CERT_CONTEXT *cert;
405 HCRYPTPROV crypt_prov;
406 DWORD key_spec;
407 BOOL free_crypt_prov;
408};
409
410
411static void cryptoapi_error(const char *msg)
412{
413 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
414 msg, (unsigned int) GetLastError());
415}
416
417
418static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
419 unsigned char *to, RSA *rsa, int padding)
420{
421 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
422 return 0;
423}
424
425
426static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
427 unsigned char *to, RSA *rsa, int padding)
428{
429 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
430 return 0;
431}
432
433
434static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
435 unsigned char *to, RSA *rsa, int padding)
436{
437 struct cryptoapi_rsa_data *priv =
438 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
439 HCRYPTHASH hash;
440 DWORD hash_size, len, i;
441 unsigned char *buf = NULL;
442 int ret = 0;
443
444 if (priv == NULL) {
445 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
446 ERR_R_PASSED_NULL_PARAMETER);
447 return 0;
448 }
449
450 if (padding != RSA_PKCS1_PADDING) {
451 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
452 RSA_R_UNKNOWN_PADDING_TYPE);
453 return 0;
454 }
455
456 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
457 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
458 __func__);
459 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
460 RSA_R_INVALID_MESSAGE_LENGTH);
461 return 0;
462 }
463
464 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
465 {
466 cryptoapi_error("CryptCreateHash failed");
467 return 0;
468 }
469
470 len = sizeof(hash_size);
471 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
472 0)) {
473 cryptoapi_error("CryptGetHashParam failed");
474 goto err;
475 }
476
477 if ((int) hash_size != flen) {
478 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
479 (unsigned) hash_size, flen);
480 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
481 RSA_R_INVALID_MESSAGE_LENGTH);
482 goto err;
483 }
484 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
485 cryptoapi_error("CryptSetHashParam failed");
486 goto err;
487 }
488
489 len = RSA_size(rsa);
490 buf = os_malloc(len);
491 if (buf == NULL) {
492 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
493 goto err;
494 }
495
496 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
497 cryptoapi_error("CryptSignHash failed");
498 goto err;
499 }
500
501 for (i = 0; i < len; i++)
502 to[i] = buf[len - i - 1];
503 ret = len;
504
505err:
506 os_free(buf);
507 CryptDestroyHash(hash);
508
509 return ret;
510}
511
512
513static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
514 unsigned char *to, RSA *rsa, int padding)
515{
516 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
517 return 0;
518}
519
520
521static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
522{
523 if (priv == NULL)
524 return;
525 if (priv->crypt_prov && priv->free_crypt_prov)
526 CryptReleaseContext(priv->crypt_prov, 0);
527 if (priv->cert)
528 CertFreeCertificateContext(priv->cert);
529 os_free(priv);
530}
531
532
533static int cryptoapi_finish(RSA *rsa)
534{
535 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
536 os_free((void *) rsa->meth);
537 rsa->meth = NULL;
538 return 1;
539}
540
541
542static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
543{
544 HCERTSTORE cs;
545 const CERT_CONTEXT *ret = NULL;
546
547 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
548 store | CERT_STORE_OPEN_EXISTING_FLAG |
549 CERT_STORE_READONLY_FLAG, L"MY");
550 if (cs == NULL) {
551 cryptoapi_error("Failed to open 'My system store'");
552 return NULL;
553 }
554
555 if (strncmp(name, "cert://", 7) == 0) {
556 unsigned short wbuf[255];
557 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
558 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
559 PKCS_7_ASN_ENCODING,
560 0, CERT_FIND_SUBJECT_STR,
561 wbuf, NULL);
562 } else if (strncmp(name, "hash://", 7) == 0) {
563 CRYPT_HASH_BLOB blob;
564 int len;
565 const char *hash = name + 7;
566 unsigned char *buf;
567
568 len = os_strlen(hash) / 2;
569 buf = os_malloc(len);
570 if (buf && hexstr2bin(hash, buf, len) == 0) {
571 blob.cbData = len;
572 blob.pbData = buf;
573 ret = CertFindCertificateInStore(cs,
574 X509_ASN_ENCODING |
575 PKCS_7_ASN_ENCODING,
576 0, CERT_FIND_HASH,
577 &blob, NULL);
578 }
579 os_free(buf);
580 }
581
582 CertCloseStore(cs, 0);
583
584 return ret;
585}
586
587
588static int tls_cryptoapi_cert(SSL *ssl, const char *name)
589{
590 X509 *cert = NULL;
591 RSA *rsa = NULL, *pub_rsa;
592 struct cryptoapi_rsa_data *priv;
593 RSA_METHOD *rsa_meth;
594
595 if (name == NULL ||
596 (strncmp(name, "cert://", 7) != 0 &&
597 strncmp(name, "hash://", 7) != 0))
598 return -1;
599
600 priv = os_zalloc(sizeof(*priv));
601 rsa_meth = os_zalloc(sizeof(*rsa_meth));
602 if (priv == NULL || rsa_meth == NULL) {
603 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
604 "for CryptoAPI RSA method");
605 os_free(priv);
606 os_free(rsa_meth);
607 return -1;
608 }
609
610 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
611 if (priv->cert == NULL) {
612 priv->cert = cryptoapi_find_cert(
613 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
614 }
615 if (priv->cert == NULL) {
616 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
617 "'%s'", name);
618 goto err;
619 }
620
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800621 cert = d2i_X509(NULL,
622 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 priv->cert->cbCertEncoded);
624 if (cert == NULL) {
625 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
626 "encoding");
627 goto err;
628 }
629
630 if (!CryptAcquireCertificatePrivateKey(priv->cert,
631 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
632 NULL, &priv->crypt_prov,
633 &priv->key_spec,
634 &priv->free_crypt_prov)) {
635 cryptoapi_error("Failed to acquire a private key for the "
636 "certificate");
637 goto err;
638 }
639
640 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
641 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
642 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
643 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
644 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
645 rsa_meth->finish = cryptoapi_finish;
646 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
647 rsa_meth->app_data = (char *) priv;
648
649 rsa = RSA_new();
650 if (rsa == NULL) {
651 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
652 ERR_R_MALLOC_FAILURE);
653 goto err;
654 }
655
656 if (!SSL_use_certificate(ssl, cert)) {
657 RSA_free(rsa);
658 rsa = NULL;
659 goto err;
660 }
661 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
662 X509_free(cert);
663 cert = NULL;
664
665 rsa->n = BN_dup(pub_rsa->n);
666 rsa->e = BN_dup(pub_rsa->e);
667 if (!RSA_set_method(rsa, rsa_meth))
668 goto err;
669
670 if (!SSL_use_RSAPrivateKey(ssl, rsa))
671 goto err;
672 RSA_free(rsa);
673
674 return 0;
675
676err:
677 if (cert)
678 X509_free(cert);
679 if (rsa)
680 RSA_free(rsa);
681 else {
682 os_free(rsa_meth);
683 cryptoapi_free_data(priv);
684 }
685 return -1;
686}
687
688
689static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
690{
691 HCERTSTORE cs;
692 PCCERT_CONTEXT ctx = NULL;
693 X509 *cert;
694 char buf[128];
695 const char *store;
696#ifdef UNICODE
697 WCHAR *wstore;
698#endif /* UNICODE */
699
700 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
701 return -1;
702
703 store = name + 13;
704#ifdef UNICODE
705 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
706 if (wstore == NULL)
707 return -1;
708 wsprintf(wstore, L"%S", store);
709 cs = CertOpenSystemStore(0, wstore);
710 os_free(wstore);
711#else /* UNICODE */
712 cs = CertOpenSystemStore(0, store);
713#endif /* UNICODE */
714 if (cs == NULL) {
715 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
716 "'%s': error=%d", __func__, store,
717 (int) GetLastError());
718 return -1;
719 }
720
721 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800722 cert = d2i_X509(NULL,
723 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 ctx->cbCertEncoded);
725 if (cert == NULL) {
726 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
727 "X509 DER encoding for CA cert");
728 continue;
729 }
730
731 X509_NAME_oneline(X509_get_subject_name(cert), buf,
732 sizeof(buf));
733 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
734 "system certificate store: subject='%s'", buf);
735
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700736 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
737 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 tls_show_errors(MSG_WARNING, __func__,
739 "Failed to add ca_cert to OpenSSL "
740 "certificate store");
741 }
742
743 X509_free(cert);
744 }
745
746 if (!CertCloseStore(cs, 0)) {
747 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
748 "'%s': error=%d", __func__, name + 13,
749 (int) GetLastError());
750 }
751
752 return 0;
753}
754
755
756#else /* CONFIG_NATIVE_WINDOWS */
757
758static int tls_cryptoapi_cert(SSL *ssl, const char *name)
759{
760 return -1;
761}
762
763#endif /* CONFIG_NATIVE_WINDOWS */
764
765
766static void ssl_info_cb(const SSL *ssl, int where, int ret)
767{
768 const char *str;
769 int w;
770
771 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
772 w = where & ~SSL_ST_MASK;
773 if (w & SSL_ST_CONNECT)
774 str = "SSL_connect";
775 else if (w & SSL_ST_ACCEPT)
776 str = "SSL_accept";
777 else
778 str = "undefined";
779
780 if (where & SSL_CB_LOOP) {
781 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
782 str, SSL_state_string_long(ssl));
783 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700784 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
786 where & SSL_CB_READ ?
787 "read (remote end reported an error)" :
788 "write (local SSL3 detected an error)",
789 SSL_alert_type_string_long(ret),
790 SSL_alert_desc_string_long(ret));
791 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 if (where & SSL_CB_READ)
793 conn->read_alerts++;
794 else
795 conn->write_alerts++;
796 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700797 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700799 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700800 os_memset(&ev, 0, sizeof(ev));
801 ev.alert.is_local = !(where & SSL_CB_READ);
802 ev.alert.type = SSL_alert_type_string_long(ret);
803 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700804 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 } else if (where & SSL_CB_EXIT && ret <= 0) {
807 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
808 str, ret == 0 ? "failed" : "error",
809 SSL_state_string_long(ssl));
810 }
811}
812
813
814#ifndef OPENSSL_NO_ENGINE
815/**
816 * tls_engine_load_dynamic_generic - load any openssl engine
817 * @pre: an array of commands and values that load an engine initialized
818 * in the engine specific function
819 * @post: an array of commands and values that initialize an already loaded
820 * engine (or %NULL if not required)
821 * @id: the engine id of the engine to load (only required if post is not %NULL
822 *
823 * This function is a generic function that loads any openssl engine.
824 *
825 * Returns: 0 on success, -1 on failure
826 */
827static int tls_engine_load_dynamic_generic(const char *pre[],
828 const char *post[], const char *id)
829{
830 ENGINE *engine;
831 const char *dynamic_id = "dynamic";
832
833 engine = ENGINE_by_id(id);
834 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
836 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700837 /*
838 * If it was auto-loaded by ENGINE_by_id() we might still
839 * need to tell it which PKCS#11 module to use in legacy
840 * (non-p11-kit) environments. Do so now; even if it was
841 * properly initialised before, setting it again will be
842 * harmless.
843 */
844 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 }
846 ERR_clear_error();
847
848 engine = ENGINE_by_id(dynamic_id);
849 if (engine == NULL) {
850 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
851 dynamic_id,
852 ERR_error_string(ERR_get_error(), NULL));
853 return -1;
854 }
855
856 /* Perform the pre commands. This will load the engine. */
857 while (pre && pre[0]) {
858 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
859 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
860 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
861 "%s %s [%s]", pre[0], pre[1],
862 ERR_error_string(ERR_get_error(), NULL));
863 ENGINE_free(engine);
864 return -1;
865 }
866 pre += 2;
867 }
868
869 /*
870 * Free the reference to the "dynamic" engine. The loaded engine can
871 * now be looked up using ENGINE_by_id().
872 */
873 ENGINE_free(engine);
874
875 engine = ENGINE_by_id(id);
876 if (engine == NULL) {
877 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
878 id, ERR_error_string(ERR_get_error(), NULL));
879 return -1;
880 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700881 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 while (post && post[0]) {
883 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
884 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
885 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
886 " %s %s [%s]", post[0], post[1],
887 ERR_error_string(ERR_get_error(), NULL));
888 ENGINE_remove(engine);
889 ENGINE_free(engine);
890 return -1;
891 }
892 post += 2;
893 }
894 ENGINE_free(engine);
895
896 return 0;
897}
898
899
900/**
901 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
902 * @pkcs11_so_path: pksc11_so_path from the configuration
903 * @pcks11_module_path: pkcs11_module_path from the configuration
904 */
905static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
906 const char *pkcs11_module_path)
907{
908 char *engine_id = "pkcs11";
909 const char *pre_cmd[] = {
910 "SO_PATH", NULL /* pkcs11_so_path */,
911 "ID", NULL /* engine_id */,
912 "LIST_ADD", "1",
913 /* "NO_VCHECK", "1", */
914 "LOAD", NULL,
915 NULL, NULL
916 };
917 const char *post_cmd[] = {
918 "MODULE_PATH", NULL /* pkcs11_module_path */,
919 NULL, NULL
920 };
921
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800922 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 return 0;
924
925 pre_cmd[1] = pkcs11_so_path;
926 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800927 if (pkcs11_module_path)
928 post_cmd[1] = pkcs11_module_path;
929 else
930 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931
932 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
933 pkcs11_so_path);
934
935 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
936}
937
938
939/**
940 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
941 * @opensc_so_path: opensc_so_path from the configuration
942 */
943static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
944{
945 char *engine_id = "opensc";
946 const char *pre_cmd[] = {
947 "SO_PATH", NULL /* opensc_so_path */,
948 "ID", NULL /* engine_id */,
949 "LIST_ADD", "1",
950 "LOAD", NULL,
951 NULL, NULL
952 };
953
954 if (!opensc_so_path)
955 return 0;
956
957 pre_cmd[1] = opensc_so_path;
958 pre_cmd[3] = engine_id;
959
960 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
961 opensc_so_path);
962
963 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
964}
965#endif /* OPENSSL_NO_ENGINE */
966
967
Sunil Ravia04bd252022-05-02 22:54:18 -0700968static struct tls_session_data * get_session_data(struct tls_context *context,
969 const struct wpabuf *buf)
970{
971 struct tls_session_data *data;
972
973 dl_list_for_each(data, &context->sessions, struct tls_session_data,
974 list) {
975 if (data->buf == buf)
976 return data;
977 }
978
979 return NULL;
980}
981
982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800983static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
984{
985 struct wpabuf *buf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700986 struct tls_context *context;
987 struct tls_session_data *found;
988
989 wpa_printf(MSG_DEBUG,
990 "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess,
991 tls_ex_idx_session);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800992
993 if (tls_ex_idx_session < 0)
994 return;
995 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
996 if (!buf)
997 return;
Sunil Ravia04bd252022-05-02 22:54:18 -0700998
999 context = SSL_CTX_get_app_data(ctx);
1000 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
1001 found = get_session_data(context, buf);
1002 if (!found) {
1003 wpa_printf(MSG_DEBUG,
1004 "OpenSSL: Do not free application session data %p (sess %p)",
1005 buf, sess);
1006 return;
1007 }
1008
1009 dl_list_del(&found->list);
1010 os_free(found);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001011 wpa_printf(MSG_DEBUG,
1012 "OpenSSL: Free application session data %p (sess %p)",
1013 buf, sess);
1014 wpabuf_free(buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001015}
1016
1017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018void * tls_init(const struct tls_config *conf)
1019{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001020 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001022 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001023 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024
1025 if (tls_openssl_ref_count == 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001026 void openssl_load_legacy_provider(void);
1027
1028 openssl_load_legacy_provider();
1029
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001030 tls_global = context = tls_context_new(conf);
1031 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033#ifdef CONFIG_FIPS
1034#ifdef OPENSSL_FIPS
1035 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001036 static int fips_enabled = 0;
1037
1038 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
1040 "mode");
1041 ERR_load_crypto_strings();
1042 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001043 os_free(tls_global);
1044 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001046 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001048 fips_enabled = 1;
1049 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 }
1051#else /* OPENSSL_FIPS */
1052 if (conf && conf->fips_mode) {
1053 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
1054 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001055 os_free(tls_global);
1056 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 return NULL;
1058 }
1059#endif /* OPENSSL_FIPS */
1060#endif /* CONFIG_FIPS */
Sunil Ravia04bd252022-05-02 22:54:18 -07001061#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 SSL_load_error_strings();
1063 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001064#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 EVP_add_digest(EVP_sha256());
1066#endif /* OPENSSL_NO_SHA256 */
1067 /* TODO: if /dev/urandom is available, PRNG is seeded
1068 * automatically. If this is not the case, random data should
1069 * be added here. */
1070
1071#ifdef PKCS12_FUNCS
1072#ifndef OPENSSL_NO_RC2
1073 /*
1074 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1075 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1076 * versions, but it looks like OpenSSL 1.0.0 does not do that
1077 * anymore.
1078 */
1079 EVP_add_cipher(EVP_rc2_40_cbc());
1080#endif /* OPENSSL_NO_RC2 */
1081 PKCS12_PBE_add();
1082#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001083#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001084 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001085 context = tls_context_new(conf);
1086 if (context == NULL)
1087 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 }
1089 tls_openssl_ref_count++;
1090
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001091 data = os_zalloc(sizeof(*data));
1092 if (data)
1093 ssl = SSL_CTX_new(SSLv23_method());
1094 else
1095 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001096 if (ssl == NULL) {
1097 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001098 if (context != tls_global)
1099 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001100 if (tls_openssl_ref_count == 0) {
1101 os_free(tls_global);
1102 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001103 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001104 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001106 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001107 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001108 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001109 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001110 data->crl_reload_interval = conf->crl_reload_interval;
1111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001113 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1114 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1115
Hai Shalom60840252021-02-19 19:02:11 -08001116 SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY);
1117
Dmitry Shmidt29333592017-01-09 12:27:11 -08001118#ifdef SSL_MODE_NO_AUTO_CHAIN
1119 /* Number of deployed use cases assume the default OpenSSL behavior of
1120 * auto chaining the local certificate is in use. BoringSSL removed this
1121 * functionality by default, so we need to restore it here to avoid
1122 * breaking existing use cases. */
1123 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1124#endif /* SSL_MODE_NO_AUTO_CHAIN */
1125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001127 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001128 if (data->tls_session_lifetime > 0) {
1129 SSL_CTX_set_quiet_shutdown(ssl, 1);
1130 /*
1131 * Set default context here. In practice, this will be replaced
1132 * by the per-EAP method context in tls_connection_set_verify().
1133 */
1134 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1135 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1136 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1137 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
Sunil Ravia04bd252022-05-02 22:54:18 -07001138#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1139 !defined(LIBRESSL_VERSION_NUMBER) && \
1140 !defined(OPENSSL_IS_BORINGSSL)
1141 /* One session ticket is sufficient for EAP-TLS */
1142 SSL_CTX_set_num_tickets(ssl, 1);
1143#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001144 } else {
1145 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
Sunil Ravia04bd252022-05-02 22:54:18 -07001146#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1147 !defined(LIBRESSL_VERSION_NUMBER) && \
1148 !defined(OPENSSL_IS_BORINGSSL)
1149 SSL_CTX_set_num_tickets(ssl, 0);
1150#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001151 }
1152
1153 if (tls_ex_idx_session < 0) {
1154 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1155 0, NULL, NULL, NULL, NULL);
1156 if (tls_ex_idx_session < 0) {
1157 tls_deinit(data);
1158 return NULL;
1159 }
1160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161
1162#ifndef OPENSSL_NO_ENGINE
Hai Shalom81f62d82019-07-22 12:10:00 -07001163 wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines");
1164 ENGINE_load_builtin_engines();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 if (conf &&
1167 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1168 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1170 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1171 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001172 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 return NULL;
1174 }
1175 }
1176#endif /* OPENSSL_NO_ENGINE */
1177
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001178 if (conf && conf->openssl_ciphers)
1179 ciphers = conf->openssl_ciphers;
1180 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001181 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001182 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1183 wpa_printf(MSG_ERROR,
1184 "OpenSSL: Failed to set cipher string '%s'",
1185 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001186 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001187 return NULL;
1188 }
1189
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001190 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191}
1192
1193
1194void tls_deinit(void *ssl_ctx)
1195{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001196 struct tls_data *data = ssl_ctx;
1197 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001198 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Sunil Ravia04bd252022-05-02 22:54:18 -07001199 struct tls_session_data *sess_data;
1200
1201 if (data->tls_session_lifetime > 0) {
1202 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions");
1203 SSL_CTX_flush_sessions(ssl, 0);
1204 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done");
1205 }
1206 while ((sess_data = dl_list_first(&context->sessions,
1207 struct tls_session_data, list))) {
1208 wpa_printf(MSG_DEBUG,
1209 "OpenSSL: Freeing not-flushed session data %p",
1210 sess_data->buf);
1211 wpabuf_free(sess_data->buf);
1212 dl_list_del(&sess_data->list);
1213 os_free(sess_data);
1214 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001215 if (context != tls_global)
1216 os_free(context);
Hai Shalom74f70d42019-02-11 14:42:39 -08001217 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 SSL_CTX_free(ssl);
1219
1220 tls_openssl_ref_count--;
1221 if (tls_openssl_ref_count == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001222#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223#ifndef OPENSSL_NO_ENGINE
1224 ENGINE_cleanup();
1225#endif /* OPENSSL_NO_ENGINE */
1226 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001227 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 ERR_free_strings();
1229 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001230#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001231 os_free(tls_global->ocsp_stapling_response);
1232 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 os_free(tls_global);
1234 tls_global = NULL;
1235 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001236
Hai Shalom021b0b52019-04-10 11:17:58 -07001237 os_free(data->check_cert_subject);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001238 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239}
1240
1241
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001242#ifndef OPENSSL_NO_ENGINE
1243
1244/* Cryptoki return values */
1245#define CKR_PIN_INCORRECT 0x000000a0
1246#define CKR_PIN_INVALID 0x000000a1
1247#define CKR_PIN_LEN_RANGE 0x000000a2
1248
1249/* libp11 */
1250#define ERR_LIB_PKCS11 ERR_LIB_USER
1251
1252static int tls_is_pin_error(unsigned int err)
1253{
1254 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1255 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1256 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1257 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1258}
1259
1260#endif /* OPENSSL_NO_ENGINE */
1261
1262
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001263#ifdef ANDROID
1264/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1265EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1266#endif /* ANDROID */
1267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1269 const char *pin, const char *key_id,
1270 const char *cert_id, const char *ca_cert_id)
1271{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001272#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1273#if !defined(OPENSSL_NO_ENGINE)
1274#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1275#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001276 if (!key_id)
1277 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001278 conn->engine = NULL;
1279 conn->private_key = EVP_PKEY_from_keystore(key_id);
1280 if (!conn->private_key) {
1281 wpa_printf(MSG_ERROR,
1282 "ENGINE: cannot load private key with id '%s' [%s]",
1283 key_id,
1284 ERR_error_string(ERR_get_error(), NULL));
1285 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1286 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001287#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289#ifndef OPENSSL_NO_ENGINE
1290 int ret = -1;
1291 if (engine_id == NULL) {
1292 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1293 return -1;
1294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295
1296 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001297#ifdef ANDROID
1298 ENGINE_load_dynamic();
1299#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 conn->engine = ENGINE_by_id(engine_id);
1301 if (!conn->engine) {
1302 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1303 engine_id, ERR_error_string(ERR_get_error(), NULL));
1304 goto err;
1305 }
1306 if (ENGINE_init(conn->engine) != 1) {
1307 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1308 "(engine: %s) [%s]", engine_id,
1309 ERR_error_string(ERR_get_error(), NULL));
1310 goto err;
1311 }
1312 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1313
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001314#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001315 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1317 ERR_error_string(ERR_get_error(), NULL));
1318 goto err;
1319 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001320#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001321 if (key_id) {
1322 /*
1323 * Ensure that the ENGINE does not attempt to use the OpenSSL
1324 * UI system to obtain a PIN, if we didn't provide one.
1325 */
1326 struct {
1327 const void *password;
1328 const char *prompt_info;
1329 } key_cb = { "", NULL };
1330
1331 /* load private key first in-case PIN is required for cert */
1332 conn->private_key = ENGINE_load_private_key(conn->engine,
1333 key_id, NULL,
1334 &key_cb);
1335 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001336 unsigned long err = ERR_get_error();
1337
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001338 wpa_printf(MSG_ERROR,
1339 "ENGINE: cannot load private key with id '%s' [%s]",
1340 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001341 ERR_error_string(err, NULL));
1342 if (tls_is_pin_error(err))
1343 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1344 else
1345 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001346 goto err;
1347 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 }
1349
1350 /* handle a certificate and/or CA certificate */
1351 if (cert_id || ca_cert_id) {
1352 const char *cmd_name = "LOAD_CERT_CTRL";
1353
1354 /* test if the engine supports a LOAD_CERT_CTRL */
1355 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1356 0, (void *)cmd_name, NULL)) {
1357 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1358 " loading certificates");
1359 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1360 goto err;
1361 }
1362 }
1363
1364 return 0;
1365
1366err:
1367 if (conn->engine) {
1368 ENGINE_free(conn->engine);
1369 conn->engine = NULL;
1370 }
1371
1372 if (conn->private_key) {
1373 EVP_PKEY_free(conn->private_key);
1374 conn->private_key = NULL;
1375 }
1376
1377 return ret;
1378#else /* OPENSSL_NO_ENGINE */
1379 return 0;
1380#endif /* OPENSSL_NO_ENGINE */
1381}
1382
1383
1384static void tls_engine_deinit(struct tls_connection *conn)
1385{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001386#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1388 if (conn->private_key) {
1389 EVP_PKEY_free(conn->private_key);
1390 conn->private_key = NULL;
1391 }
1392 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001393#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001395#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 conn->engine = NULL;
1397 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001398#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399}
1400
1401
1402int tls_get_errors(void *ssl_ctx)
1403{
1404 int count = 0;
1405 unsigned long err;
1406
1407 while ((err = ERR_get_error())) {
1408 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1409 ERR_error_string(err, NULL));
1410 count++;
1411 }
1412
1413 return count;
1414}
1415
Jouni Malinen26af48b2014-04-09 13:02:53 +03001416
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001417static const char * openssl_content_type(int content_type)
1418{
1419 switch (content_type) {
1420 case 20:
1421 return "change cipher spec";
1422 case 21:
1423 return "alert";
1424 case 22:
1425 return "handshake";
1426 case 23:
1427 return "application data";
1428 case 24:
1429 return "heartbeat";
1430 case 256:
1431 return "TLS header info"; /* pseudo content type */
Hai Shalom81f62d82019-07-22 12:10:00 -07001432 case 257:
1433 return "inner content type"; /* pseudo content type */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001434 default:
1435 return "?";
1436 }
1437}
1438
1439
1440static const char * openssl_handshake_type(int content_type, const u8 *buf,
1441 size_t len)
1442{
Hai Shalom81f62d82019-07-22 12:10:00 -07001443 if (content_type == 257 && buf && len == 1)
1444 return openssl_content_type(buf[0]);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 if (content_type != 22 || !buf || len == 0)
1446 return "";
1447 switch (buf[0]) {
1448 case 0:
1449 return "hello request";
1450 case 1:
1451 return "client hello";
1452 case 2:
1453 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001454 case 3:
1455 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001456 case 4:
1457 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001458 case 5:
1459 return "end of early data";
1460 case 6:
1461 return "hello retry request";
1462 case 8:
1463 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001464 case 11:
1465 return "certificate";
1466 case 12:
1467 return "server key exchange";
1468 case 13:
1469 return "certificate request";
1470 case 14:
1471 return "server hello done";
1472 case 15:
1473 return "certificate verify";
1474 case 16:
1475 return "client key exchange";
1476 case 20:
1477 return "finished";
1478 case 21:
1479 return "certificate url";
1480 case 22:
1481 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001482 case 23:
1483 return "supplemental data";
1484 case 24:
1485 return "key update";
1486 case 254:
1487 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001488 default:
1489 return "?";
1490 }
1491}
1492
1493
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001494#ifdef CONFIG_SUITEB
1495
1496static void check_server_hello(struct tls_connection *conn,
1497 const u8 *pos, const u8 *end)
1498{
1499 size_t payload_len, id_len;
1500
1501 /*
1502 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1503 * not make it cleanly available during handshake and we need to know
1504 * whether DHE was selected.
1505 */
1506
1507 if (end - pos < 3)
1508 return;
1509 payload_len = WPA_GET_BE24(pos);
1510 pos += 3;
1511
1512 if ((size_t) (end - pos) < payload_len)
1513 return;
1514 end = pos + payload_len;
1515
1516 /* Skip Version and Random */
1517 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1518 return;
1519 pos += 2 + SSL3_RANDOM_SIZE;
1520
1521 /* Skip Session ID */
1522 if (end - pos < 1)
1523 return;
1524 id_len = *pos++;
1525 if ((size_t) (end - pos) < id_len)
1526 return;
1527 pos += id_len;
1528
1529 if (end - pos < 2)
1530 return;
1531 conn->cipher_suite = WPA_GET_BE16(pos);
1532 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1533 conn->cipher_suite);
1534}
1535
1536
1537static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1538 const u8 *pos, const u8 *end)
1539{
1540 size_t payload_len;
1541 u16 dh_len;
1542 BIGNUM *p;
1543 int bits;
1544
1545 if (!(conn->flags & TLS_CONN_SUITEB))
1546 return;
1547
1548 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1549 if (conn->cipher_suite != 0x9f)
1550 return;
1551
1552 if (end - pos < 3)
1553 return;
1554 payload_len = WPA_GET_BE24(pos);
1555 pos += 3;
1556
1557 if ((size_t) (end - pos) < payload_len)
1558 return;
1559 end = pos + payload_len;
1560
1561 if (end - pos < 2)
1562 return;
1563 dh_len = WPA_GET_BE16(pos);
1564 pos += 2;
1565
1566 if ((size_t) (end - pos) < dh_len)
1567 return;
1568 p = BN_bin2bn(pos, dh_len, NULL);
1569 if (!p)
1570 return;
1571
1572 bits = BN_num_bits(p);
1573 BN_free(p);
1574
1575 conn->server_dh_prime_len = bits;
1576 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1577 conn->server_dh_prime_len);
1578}
1579
1580#endif /* CONFIG_SUITEB */
1581
1582
Jouni Malinen26af48b2014-04-09 13:02:53 +03001583static void tls_msg_cb(int write_p, int version, int content_type,
1584 const void *buf, size_t len, SSL *ssl, void *arg)
1585{
1586 struct tls_connection *conn = arg;
1587 const u8 *pos = buf;
1588
Sunil8cd6f4d2022-06-28 18:40:46 +00001589#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1590 if ((SSL_version(ssl) == TLS1_VERSION ||
1591 SSL_version(ssl) == TLS1_1_VERSION) &&
1592 SSL_get_security_level(ssl) > 0) {
1593 wpa_printf(MSG_DEBUG,
1594 "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm");
1595 SSL_set_security_level(ssl, 0);
1596 }
1597#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001598 if (write_p == 2) {
1599 wpa_printf(MSG_DEBUG,
1600 "OpenSSL: session ver=0x%x content_type=%d",
1601 version, content_type);
1602 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1603 return;
1604 }
1605
1606 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1607 write_p ? "TX" : "RX", version, content_type,
1608 openssl_content_type(content_type),
1609 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001610 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1611 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1612 size_t payload_len = WPA_GET_BE16(pos + 1);
1613 if (payload_len + 3 > len) {
1614 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1615 conn->invalid_hb_used = 1;
1616 }
1617 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001618
1619#ifdef CONFIG_SUITEB
1620 /*
1621 * Need to parse these handshake messages to be able to check DH prime
1622 * length since OpenSSL does not expose the new cipher suite and DH
1623 * parameters during handshake (e.g., for cert_cb() callback).
1624 */
1625 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1626 check_server_hello(conn, pos + 1, pos + len);
1627 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1628 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1629#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001630}
1631
1632
Sunil Ravia04bd252022-05-02 22:54:18 -07001633#ifdef CONFIG_TESTING_OPTIONS
1634#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1635/*
1636 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1637 * material will be exported that you may use with Wireshark to decode any
1638 * TLS flows. Please see the following for more details:
1639 *
1640 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1641 *
1642 * Example logging sessions are (you should delete the file on each run):
1643 *
1644 * rm -f /tmp/sslkey.log
1645 * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ...
1646 *
1647 * rm -f /tmp/sslkey.log
1648 * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ...
1649 *
1650 * rm -f /tmp/sslkey.log
1651 * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ...
1652 */
1653static void tls_keylog_cb(const SSL *ssl, const char *line)
1654{
1655 int fd;
1656 const char *filename;
1657 struct iovec iov[2];
1658
1659 filename = getenv("SSLKEYLOGFILE");
1660 if (!filename)
1661 return;
1662
1663 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1664 if (fd < 0) {
1665 wpa_printf(MSG_ERROR,
1666 "OpenSSL: Failed to open keylog file %s: %s",
1667 filename, strerror(errno));
1668 return;
1669 }
1670
1671 /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed
1672 * to be atomic for O_APPEND. */
1673 iov[0].iov_base = (void *) line;
1674 iov[0].iov_len = os_strlen(line);
1675 iov[1].iov_base = "\n";
1676 iov[1].iov_len = 1;
1677
1678 if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) {
1679 wpa_printf(MSG_DEBUG,
1680 "OpenSSL: Failed to write to keylog file %s: %s",
1681 filename, strerror(errno));
1682 }
1683
1684 close(fd);
1685}
1686#endif
1687#endif /* CONFIG_TESTING_OPTIONS */
1688
1689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690struct tls_connection * tls_connection_init(void *ssl_ctx)
1691{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001692 struct tls_data *data = ssl_ctx;
1693 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 struct tls_connection *conn;
1695 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001696 X509_STORE *new_cert_store;
1697 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001698 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699
Hai Shalom74f70d42019-02-11 14:42:39 -08001700 /* Replace X509 store if it is time to update CRL. */
1701 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1702 os_reltime_expired(&now, &data->crl_last_reload,
1703 data->crl_reload_interval)) {
1704 wpa_printf(MSG_INFO,
1705 "OpenSSL: Flushing X509 store with ca_cert file");
1706 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1707 data->check_crl);
1708 if (!new_cert_store) {
1709 wpa_printf(MSG_ERROR,
1710 "OpenSSL: Error replacing X509 store with ca_cert file");
1711 } else {
1712 /* Replace old store */
1713 SSL_CTX_set_cert_store(ssl, new_cert_store);
1714 data->crl_last_reload = now;
1715 }
1716 }
1717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 conn = os_zalloc(sizeof(*conn));
1719 if (conn == NULL)
1720 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001721 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001722 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 conn->ssl = SSL_new(ssl);
1724 if (conn->ssl == NULL) {
1725 tls_show_errors(MSG_INFO, __func__,
1726 "Failed to initialize new SSL connection");
1727 os_free(conn);
1728 return NULL;
1729 }
1730
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001731 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001733 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1734 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1736 SSL_OP_SINGLE_DH_USE;
1737#ifdef SSL_OP_NO_COMPRESSION
1738 options |= SSL_OP_NO_COMPRESSION;
1739#endif /* SSL_OP_NO_COMPRESSION */
1740 SSL_set_options(conn->ssl, options);
Hai Shalom81f62d82019-07-22 12:10:00 -07001741#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
1742 /* Hopefully there is no need for middlebox compatibility mechanisms
1743 * when going through EAP authentication. */
1744 SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
1745#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746
Sunil Ravia04bd252022-05-02 22:54:18 -07001747#ifdef CONFIG_TESTING_OPTIONS
1748#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1749 /* Set the keylog file if the admin requested it. */
1750 if (getenv("SSLKEYLOGFILE"))
1751 SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb);
1752#endif
1753#endif /* CONFIG_TESTING_OPTIONS */
1754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 conn->ssl_in = BIO_new(BIO_s_mem());
1756 if (!conn->ssl_in) {
1757 tls_show_errors(MSG_INFO, __func__,
1758 "Failed to create a new BIO for ssl_in");
1759 SSL_free(conn->ssl);
1760 os_free(conn);
1761 return NULL;
1762 }
1763
1764 conn->ssl_out = BIO_new(BIO_s_mem());
1765 if (!conn->ssl_out) {
1766 tls_show_errors(MSG_INFO, __func__,
1767 "Failed to create a new BIO for ssl_out");
1768 SSL_free(conn->ssl);
1769 BIO_free(conn->ssl_in);
1770 os_free(conn);
1771 return NULL;
1772 }
1773
1774 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1775
1776 return conn;
1777}
1778
1779
1780void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1781{
1782 if (conn == NULL)
1783 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001784 if (conn->success_data) {
1785 /*
1786 * Make sure ssl_clear_bad_session() does not remove this
1787 * session.
1788 */
1789 SSL_set_quiet_shutdown(conn->ssl, 1);
1790 SSL_shutdown(conn->ssl);
1791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 SSL_free(conn->ssl);
1793 tls_engine_deinit(conn);
1794 os_free(conn->subject_match);
1795 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001796 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001797 os_free(conn->domain_match);
Hai Shalom021b0b52019-04-10 11:17:58 -07001798 os_free(conn->check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 os_free(conn->session_ticket);
Hai Shalom899fcc72020-10-19 14:38:18 -07001800 os_free(conn->peer_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 os_free(conn);
1802}
1803
1804
1805int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1806{
1807 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1808}
1809
1810
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001811char * tls_connection_peer_serial_num(void *tls_ctx,
1812 struct tls_connection *conn)
1813{
1814 ASN1_INTEGER *ser;
1815 char *serial_num;
1816 size_t len;
1817
1818 if (!conn->peer_cert)
1819 return NULL;
1820
1821 ser = X509_get_serialNumber(conn->peer_cert);
1822 if (!ser)
1823 return NULL;
1824
1825 len = ASN1_STRING_length(ser) * 2 + 1;
1826 serial_num = os_malloc(len);
1827 if (!serial_num)
1828 return NULL;
1829 wpa_snprintf_hex_uppercase(serial_num, len,
1830 ASN1_STRING_get0_data(ser),
1831 ASN1_STRING_length(ser));
1832 return serial_num;
1833}
1834
1835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1837{
1838 if (conn == NULL)
1839 return -1;
1840
1841 /* Shutdown previous TLS connection without notifying the peer
1842 * because the connection was already terminated in practice
1843 * and "close notify" shutdown alert would confuse AS. */
1844 SSL_set_quiet_shutdown(conn->ssl, 1);
1845 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001846 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847}
1848
1849
1850static int tls_match_altsubject_component(X509 *cert, int type,
1851 const char *value, size_t len)
1852{
1853 GENERAL_NAME *gen;
1854 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001855 int found = 0;
1856 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857
1858 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1859
1860 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1861 gen = sk_GENERAL_NAME_value(ext, i);
1862 if (gen->type != type)
1863 continue;
1864 if (os_strlen((char *) gen->d.ia5->data) == len &&
1865 os_memcmp(value, gen->d.ia5->data, len) == 0)
1866 found++;
1867 }
1868
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001869 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 return found;
1872}
1873
1874
1875static int tls_match_altsubject(X509 *cert, const char *match)
1876{
1877 int type;
1878 const char *pos, *end;
1879 size_t len;
1880
1881 pos = match;
1882 do {
1883 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1884 type = GEN_EMAIL;
1885 pos += 6;
1886 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1887 type = GEN_DNS;
1888 pos += 4;
1889 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1890 type = GEN_URI;
1891 pos += 4;
1892 } else {
1893 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1894 "match '%s'", pos);
1895 return 0;
1896 }
1897 end = os_strchr(pos, ';');
1898 while (end) {
1899 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1900 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1901 os_strncmp(end + 1, "URI:", 4) == 0)
1902 break;
1903 end = os_strchr(end + 1, ';');
1904 }
1905 if (end)
1906 len = end - pos;
1907 else
1908 len = os_strlen(pos);
1909 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1910 return 1;
1911 pos = end + 1;
1912 } while (end);
1913
1914 return 0;
1915}
1916
1917
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001918#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001919static int domain_suffix_match(const u8 *val, size_t len, const char *match,
Hai Shalom021b0b52019-04-10 11:17:58 -07001920 size_t match_len, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001921{
Hai Shalom021b0b52019-04-10 11:17:58 -07001922 size_t i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001923
1924 /* Check for embedded nuls that could mess up suffix matching */
1925 for (i = 0; i < len; i++) {
1926 if (val[i] == '\0') {
1927 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1928 return 0;
1929 }
1930 }
1931
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001932 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001933 return 0;
1934
1935 if (os_strncasecmp((const char *) val + len - match_len, match,
1936 match_len) != 0)
1937 return 0; /* no match */
1938
1939 if (match_len == len)
1940 return 1; /* exact match */
1941
1942 if (val[len - match_len - 1] == '.')
1943 return 1; /* full label match completes suffix match */
1944
1945 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1946 return 0;
1947}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001948#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001949
1950
Hai Shalom021b0b52019-04-10 11:17:58 -07001951struct tls_dn_field_order_cnt {
1952 u8 cn;
1953 u8 c;
1954 u8 l;
1955 u8 st;
1956 u8 o;
1957 u8 ou;
1958 u8 email;
1959};
1960
1961
1962static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt,
1963 int nid)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001964{
Hai Shalom021b0b52019-04-10 11:17:58 -07001965 switch (nid) {
1966 case NID_commonName:
1967 return dn_cnt->cn;
1968 case NID_countryName:
1969 return dn_cnt->c;
1970 case NID_localityName:
1971 return dn_cnt->l;
1972 case NID_stateOrProvinceName:
1973 return dn_cnt->st;
1974 case NID_organizationName:
1975 return dn_cnt->o;
1976 case NID_organizationalUnitName:
1977 return dn_cnt->ou;
1978 case NID_pkcs9_emailAddress:
1979 return dn_cnt->email;
1980 default:
1981 wpa_printf(MSG_ERROR,
1982 "TLS: Unknown NID '%d' in check_cert_subject",
1983 nid);
1984 return -1;
1985 }
1986}
1987
1988
1989/**
1990 * match_dn_field - Match configuration DN field against Certificate DN field
1991 * @cert: Certificate
1992 * @nid: NID of DN field
1993 * @field: Field name
1994 * @value DN field value which is passed from configuration
1995 * e.g., if configuration have C=US and this argument will point to US.
1996 * @dn_cnt: DN matching context
1997 * Returns: 1 on success and 0 on failure
1998 */
1999static int match_dn_field(const X509 *cert, int nid, const char *field,
2000 const char *value,
2001 const struct tls_dn_field_order_cnt *dn_cnt)
2002{
2003 int i, ret = 0, len, config_dn_field_index, match_index = 0;
2004 X509_NAME *name;
2005
2006 len = os_strlen(value);
2007 name = X509_get_subject_name((X509 *) cert);
2008
2009 /* Assign incremented cnt for every field of DN to check DN field in
2010 * right order */
2011 config_dn_field_index = get_dn_field_index(dn_cnt, nid);
2012 if (config_dn_field_index < 0)
2013 return 0;
2014
2015 /* Fetch value based on NID */
2016 for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) {
2017 X509_NAME_ENTRY *e;
2018 ASN1_STRING *cn;
2019
2020 e = X509_NAME_get_entry(name, i);
2021 if (!e)
2022 continue;
2023
2024 cn = X509_NAME_ENTRY_get_data(e);
2025 if (!cn)
2026 continue;
2027
2028 match_index++;
2029
2030 /* check for more than one DN field with same name */
2031 if (match_index != config_dn_field_index)
2032 continue;
2033
2034 /* Check wildcard at the right end side */
2035 /* E.g., if OU=develop* mentioned in configuration, allow 'OU'
2036 * of the subject in the client certificate to start with
2037 * 'develop' */
2038 if (len > 0 && value[len - 1] == '*') {
2039 /* Compare actual certificate DN field value with
2040 * configuration DN field value up to the specified
2041 * length. */
2042 ret = ASN1_STRING_length(cn) >= len - 1 &&
2043 os_memcmp(ASN1_STRING_get0_data(cn), value,
2044 len - 1) == 0;
2045 } else {
2046 /* Compare actual certificate DN field value with
2047 * configuration DN field value */
2048 ret = ASN1_STRING_length(cn) == len &&
2049 os_memcmp(ASN1_STRING_get0_data(cn), value,
2050 len) == 0;
2051 }
2052 if (!ret) {
2053 wpa_printf(MSG_ERROR,
2054 "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'",
2055 field, value, ASN1_STRING_get0_data(cn));
2056 }
2057 break;
2058 }
2059
2060 return ret;
2061}
2062
2063
2064/**
2065 * get_value_from_field - Get value from DN field
2066 * @cert: Certificate
2067 * @field_str: DN field string which is passed from configuration file (e.g.,
2068 * C=US)
2069 * @dn_cnt: DN matching context
2070 * Returns: 1 on success and 0 on failure
2071 */
2072static int get_value_from_field(const X509 *cert, char *field_str,
2073 struct tls_dn_field_order_cnt *dn_cnt)
2074{
2075 int nid;
2076 char *context = NULL, *name, *value;
2077
2078 if (os_strcmp(field_str, "*") == 0)
2079 return 1; /* wildcard matches everything */
2080
2081 name = str_token(field_str, "=", &context);
2082 if (!name)
2083 return 0;
2084
2085 /* Compare all configured DN fields and assign nid based on that to
2086 * fetch correct value from certificate subject */
2087 if (os_strcmp(name, "CN") == 0) {
2088 nid = NID_commonName;
2089 dn_cnt->cn++;
2090 } else if(os_strcmp(name, "C") == 0) {
2091 nid = NID_countryName;
2092 dn_cnt->c++;
2093 } else if (os_strcmp(name, "L") == 0) {
2094 nid = NID_localityName;
2095 dn_cnt->l++;
2096 } else if (os_strcmp(name, "ST") == 0) {
2097 nid = NID_stateOrProvinceName;
2098 dn_cnt->st++;
2099 } else if (os_strcmp(name, "O") == 0) {
2100 nid = NID_organizationName;
2101 dn_cnt->o++;
2102 } else if (os_strcmp(name, "OU") == 0) {
2103 nid = NID_organizationalUnitName;
2104 dn_cnt->ou++;
2105 } else if (os_strcmp(name, "emailAddress") == 0) {
2106 nid = NID_pkcs9_emailAddress;
2107 dn_cnt->email++;
2108 } else {
2109 wpa_printf(MSG_ERROR,
2110 "TLS: Unknown field '%s' in check_cert_subject", name);
2111 return 0;
2112 }
2113
2114 value = str_token(field_str, "=", &context);
2115 if (!value) {
2116 wpa_printf(MSG_ERROR,
2117 "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject",
2118 name);
2119 return 0;
2120 }
2121
2122 return match_dn_field(cert, nid, name, value, dn_cnt);
2123}
2124
2125
2126/**
2127 * tls_match_dn_field - Match subject DN field with check_cert_subject
2128 * @cert: Certificate
2129 * @match: check_cert_subject string
2130 * Returns: Return 1 on success and 0 on failure
2131*/
2132static int tls_match_dn_field(X509 *cert, const char *match)
2133{
2134 const char *token, *last = NULL;
2135 char field[256];
2136 struct tls_dn_field_order_cnt dn_cnt;
2137
2138 os_memset(&dn_cnt, 0, sizeof(dn_cnt));
2139
2140 /* Maximum length of each DN field is 255 characters */
2141
2142 /* Process each '/' delimited field */
2143 while ((token = cstr_token(match, "/", &last))) {
2144 if (last - token >= (int) sizeof(field)) {
2145 wpa_printf(MSG_ERROR,
2146 "OpenSSL: Too long DN matching field value in '%s'",
2147 match);
2148 return 0;
2149 }
2150 os_memcpy(field, token, last - token);
2151 field[last - token] = '\0';
2152
2153 if (!get_value_from_field(cert, field, &dn_cnt)) {
2154 wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'",
2155 field);
2156 return 0;
2157 }
2158 }
2159
2160 return 1;
2161}
2162
2163
2164#ifndef CONFIG_NATIVE_WINDOWS
2165static int tls_match_suffix_helper(X509 *cert, const char *match,
2166 size_t match_len, int full)
2167{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002168 GENERAL_NAME *gen;
2169 void *ext;
2170 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002171 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002172 int dns_name = 0;
2173 X509_NAME *name;
2174
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002175 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
2176 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002177
2178 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
2179
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002180 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
2181 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002182 if (gen->type != GEN_DNS)
2183 continue;
2184 dns_name++;
2185 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
2186 gen->d.dNSName->data,
2187 gen->d.dNSName->length);
2188 if (domain_suffix_match(gen->d.dNSName->data,
Hai Shalom021b0b52019-04-10 11:17:58 -07002189 gen->d.dNSName->length,
2190 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002191 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
2192 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002193 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002194 return 1;
2195 }
2196 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002197 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002198
2199 if (dns_name) {
2200 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
2201 return 0;
2202 }
2203
2204 name = X509_get_subject_name(cert);
2205 i = -1;
2206 for (;;) {
2207 X509_NAME_ENTRY *e;
2208 ASN1_STRING *cn;
2209
2210 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
2211 if (i == -1)
2212 break;
2213 e = X509_NAME_get_entry(name, i);
2214 if (e == NULL)
2215 continue;
2216 cn = X509_NAME_ENTRY_get_data(e);
2217 if (cn == NULL)
2218 continue;
2219 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
2220 cn->data, cn->length);
Hai Shalom021b0b52019-04-10 11:17:58 -07002221 if (domain_suffix_match(cn->data, cn->length,
2222 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002223 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
2224 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002225 return 1;
2226 }
2227 }
2228
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002229 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
2230 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002231 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07002232}
2233#endif /* CONFIG_NATIVE_WINDOWS */
2234
2235
2236static int tls_match_suffix(X509 *cert, const char *match, int full)
2237{
2238#ifdef CONFIG_NATIVE_WINDOWS
2239 /* wincrypt.h has conflicting X509_NAME definition */
2240 return -1;
2241#else /* CONFIG_NATIVE_WINDOWS */
2242 const char *token, *last = NULL;
2243
2244 /* Process each match alternative separately until a match is found */
2245 while ((token = cstr_token(match, ";", &last))) {
2246 if (tls_match_suffix_helper(cert, token, last - token, full))
2247 return 1;
2248 }
2249
2250 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08002251#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002252}
2253
2254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255static enum tls_fail_reason openssl_tls_fail_reason(int err)
2256{
2257 switch (err) {
2258 case X509_V_ERR_CERT_REVOKED:
2259 return TLS_FAIL_REVOKED;
2260 case X509_V_ERR_CERT_NOT_YET_VALID:
2261 case X509_V_ERR_CRL_NOT_YET_VALID:
2262 return TLS_FAIL_NOT_YET_VALID;
2263 case X509_V_ERR_CERT_HAS_EXPIRED:
2264 case X509_V_ERR_CRL_HAS_EXPIRED:
2265 return TLS_FAIL_EXPIRED;
2266 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
2267 case X509_V_ERR_UNABLE_TO_GET_CRL:
2268 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
2269 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2270 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2271 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2272 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2273 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
2274 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
2275 case X509_V_ERR_INVALID_CA:
2276 return TLS_FAIL_UNTRUSTED;
2277 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
2278 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
2279 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
2280 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
2281 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
2282 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
2283 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
2284 case X509_V_ERR_CERT_UNTRUSTED:
2285 case X509_V_ERR_CERT_REJECTED:
2286 return TLS_FAIL_BAD_CERTIFICATE;
2287 default:
2288 return TLS_FAIL_UNSPECIFIED;
2289 }
2290}
2291
2292
2293static struct wpabuf * get_x509_cert(X509 *cert)
2294{
2295 struct wpabuf *buf;
2296 u8 *tmp;
2297
2298 int cert_len = i2d_X509(cert, NULL);
2299 if (cert_len <= 0)
2300 return NULL;
2301
2302 buf = wpabuf_alloc(cert_len);
2303 if (buf == NULL)
2304 return NULL;
2305
2306 tmp = wpabuf_put(buf, cert_len);
2307 i2d_X509(cert, &tmp);
2308 return buf;
2309}
2310
2311
2312static void openssl_tls_fail_event(struct tls_connection *conn,
2313 X509 *err_cert, int err, int depth,
2314 const char *subject, const char *err_str,
2315 enum tls_fail_reason reason)
2316{
2317 union tls_event_data ev;
2318 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002319 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320
Pavel Grafov4d8552e2018-02-06 11:28:29 +00002321#ifdef ANDROID
2322 log_cert_validation_failure(err_str);
2323#endif
2324
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002325 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 return;
2327
2328 cert = get_x509_cert(err_cert);
2329 os_memset(&ev, 0, sizeof(ev));
2330 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
2331 reason : openssl_tls_fail_reason(err);
2332 ev.cert_fail.depth = depth;
2333 ev.cert_fail.subject = subject;
2334 ev.cert_fail.reason_txt = err_str;
2335 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002336 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 wpabuf_free(cert);
2338}
2339
2340
Hai Shalom81f62d82019-07-22 12:10:00 -07002341static int openssl_cert_tod(X509 *cert)
2342{
2343 CERTIFICATEPOLICIES *ext;
2344 stack_index_t i;
2345 char buf[100];
2346 int res;
2347 int tod = 0;
2348
2349 ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL);
2350 if (!ext)
2351 return 0;
2352
2353 for (i = 0; i < sk_POLICYINFO_num(ext); i++) {
2354 POLICYINFO *policy;
2355
2356 policy = sk_POLICYINFO_value(ext, i);
2357 res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0);
2358 if (res < 0 || (size_t) res >= sizeof(buf))
2359 continue;
2360 wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf);
2361 if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0)
Hai Shalomc3565922019-10-28 11:58:20 -07002362 tod = 1; /* TOD-STRICT */
2363 else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod)
2364 tod = 2; /* TOD-TOFU */
Hai Shalom81f62d82019-07-22 12:10:00 -07002365 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002366 sk_POLICYINFO_pop_free(ext, POLICYINFO_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002367
2368 return tod;
2369}
2370
2371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372static void openssl_tls_cert_event(struct tls_connection *conn,
2373 X509 *err_cert, int depth,
2374 const char *subject)
2375{
2376 struct wpabuf *cert = NULL;
2377 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002378 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002379 char *altsubject[TLS_MAX_ALT_SUBJECT];
2380 int alt, num_altsubject = 0;
2381 GENERAL_NAME *gen;
2382 void *ext;
2383 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002384 ASN1_INTEGER *ser;
2385 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386#ifdef CONFIG_SHA256
2387 u8 hash[32];
2388#endif /* CONFIG_SHA256 */
2389
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002390 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 return;
2392
2393 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002394 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
2395 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396 cert = get_x509_cert(err_cert);
2397 ev.peer_cert.cert = cert;
2398 }
2399#ifdef CONFIG_SHA256
2400 if (cert) {
2401 const u8 *addr[1];
2402 size_t len[1];
2403 addr[0] = wpabuf_head(cert);
2404 len[0] = wpabuf_len(cert);
2405 if (sha256_vector(1, addr, len, hash) == 0) {
2406 ev.peer_cert.hash = hash;
2407 ev.peer_cert.hash_len = sizeof(hash);
2408 }
2409 }
2410#endif /* CONFIG_SHA256 */
2411 ev.peer_cert.depth = depth;
2412 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002413
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002414 ser = X509_get_serialNumber(err_cert);
2415 if (ser) {
2416 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
2417 ASN1_STRING_get0_data(ser),
2418 ASN1_STRING_length(ser));
2419 ev.peer_cert.serial_num = serial_num;
2420 }
2421
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002422 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
2423 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
2424 char *pos;
2425
2426 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
2427 break;
2428 gen = sk_GENERAL_NAME_value(ext, i);
2429 if (gen->type != GEN_EMAIL &&
2430 gen->type != GEN_DNS &&
2431 gen->type != GEN_URI)
2432 continue;
2433
2434 pos = os_malloc(10 + gen->d.ia5->length + 1);
2435 if (pos == NULL)
2436 break;
2437 altsubject[num_altsubject++] = pos;
2438
2439 switch (gen->type) {
2440 case GEN_EMAIL:
2441 os_memcpy(pos, "EMAIL:", 6);
2442 pos += 6;
2443 break;
2444 case GEN_DNS:
2445 os_memcpy(pos, "DNS:", 4);
2446 pos += 4;
2447 break;
2448 case GEN_URI:
2449 os_memcpy(pos, "URI:", 4);
2450 pos += 4;
2451 break;
2452 }
2453
2454 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2455 pos += gen->d.ia5->length;
2456 *pos = '\0';
2457 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002458 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002459
2460 for (alt = 0; alt < num_altsubject; alt++)
2461 ev.peer_cert.altsubject[alt] = altsubject[alt];
2462 ev.peer_cert.num_altsubject = num_altsubject;
2463
Hai Shalom81f62d82019-07-22 12:10:00 -07002464 ev.peer_cert.tod = openssl_cert_tod(err_cert);
2465
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002466 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002468 for (alt = 0; alt < num_altsubject; alt++)
2469 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470}
2471
2472
Hai Shalomc3565922019-10-28 11:58:20 -07002473static void debug_print_cert(X509 *cert, const char *title)
2474{
2475#ifndef CONFIG_NO_STDOUT_DEBUG
2476 BIO *out;
2477 size_t rlen;
2478 char *txt;
2479 int res;
2480
2481 if (wpa_debug_level > MSG_DEBUG)
2482 return;
2483
2484 out = BIO_new(BIO_s_mem());
2485 if (!out)
2486 return;
2487
2488 X509_print(out, cert);
2489 rlen = BIO_ctrl_pending(out);
2490 txt = os_malloc(rlen + 1);
2491 if (txt) {
2492 res = BIO_read(out, txt, rlen);
2493 if (res > 0) {
2494 txt[res] = '\0';
2495 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
2496 }
2497 os_free(txt);
2498 }
2499
2500 BIO_free(out);
2501#endif /* CONFIG_NO_STDOUT_DEBUG */
2502}
2503
2504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2506{
2507 char buf[256];
2508 X509 *err_cert;
2509 int err, depth;
2510 SSL *ssl;
2511 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002512 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002513 char *match, *altmatch, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -07002514 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 const char *err_str;
2516
2517 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002518 if (!err_cert)
2519 return 0;
2520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 err = X509_STORE_CTX_get_error(x509_ctx);
2522 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2523 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2524 SSL_get_ex_data_X509_STORE_CTX_idx());
Hai Shalomc3565922019-10-28 11:58:20 -07002525 os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth);
2526 debug_print_cert(err_cert, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2528
2529 conn = SSL_get_app_data(ssl);
2530 if (conn == NULL)
2531 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002532
2533 if (depth == 0)
2534 conn->peer_cert = err_cert;
2535 else if (depth == 1)
2536 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002537 else if (depth == 2)
2538 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002539
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002540 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 match = conn->subject_match;
2542 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002543 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002544 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
2546 if (!preverify_ok && !conn->ca_cert_verify)
2547 preverify_ok = 1;
2548 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2549 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002550 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2551 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2552 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2553 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2554 "time mismatch");
2555 preverify_ok = 1;
2556 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002557 if (!preverify_ok && !conn->data->check_crl_strict &&
2558 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2559 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2560 wpa_printf(MSG_DEBUG,
2561 "OpenSSL: Ignore certificate validity CRL time mismatch");
2562 preverify_ok = 1;
2563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564
2565 err_str = X509_verify_cert_error_string(err);
2566
2567#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002568 /*
2569 * Do not require preverify_ok so we can explicity allow otherwise
2570 * invalid pinned server certificates.
2571 */
2572 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 struct wpabuf *cert;
2574 cert = get_x509_cert(err_cert);
2575 if (!cert) {
2576 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2577 "server certificate data");
2578 preverify_ok = 0;
2579 } else {
2580 u8 hash[32];
2581 const u8 *addr[1];
2582 size_t len[1];
2583 addr[0] = wpabuf_head(cert);
2584 len[0] = wpabuf_len(cert);
2585 if (sha256_vector(1, addr, len, hash) < 0 ||
2586 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2587 err_str = "Server certificate mismatch";
2588 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2589 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002590 } else if (!preverify_ok) {
2591 /*
2592 * Certificate matches pinned certificate, allow
2593 * regardless of other problems.
2594 */
2595 wpa_printf(MSG_DEBUG,
2596 "OpenSSL: Ignore validation issues for a pinned server certificate");
2597 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 }
2599 wpabuf_free(cert);
2600 }
2601 }
2602#endif /* CONFIG_SHA256 */
2603
Hai Shalom81f62d82019-07-22 12:10:00 -07002604 openssl_tls_cert_event(conn, err_cert, depth, buf);
2605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606 if (!preverify_ok) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002607 if (depth > 0) {
2608 /* Send cert event for the peer certificate so that
2609 * the upper layers get information about it even if
2610 * validation of a CA certificate fails. */
2611 STACK_OF(X509) *chain;
2612
2613 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2614 if (chain && sk_X509_num(chain) > 0) {
2615 char buf2[256];
2616 X509 *cert;
2617
2618 cert = sk_X509_value(chain, 0);
2619 X509_NAME_oneline(X509_get_subject_name(cert),
2620 buf2, sizeof(buf2));
2621
2622 openssl_tls_cert_event(conn, cert, 0, buf2);
2623 }
2624 if (chain)
2625 sk_X509_pop_free(chain, X509_free);
2626 }
2627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2629 " error %d (%s) depth %d for '%s'", err, err_str,
2630 depth, buf);
2631 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2632 err_str, TLS_FAIL_UNSPECIFIED);
2633 return preverify_ok;
2634 }
2635
2636 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2637 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2638 preverify_ok, err, err_str,
2639 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002640 check_cert_subject = conn->check_cert_subject;
2641 if (!check_cert_subject)
2642 check_cert_subject = conn->data->check_cert_subject;
2643 if (check_cert_subject) {
2644 if (depth == 0 &&
2645 !tls_match_dn_field(err_cert, check_cert_subject)) {
2646 preverify_ok = 0;
2647 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2648 "Distinguished Name",
2649 TLS_FAIL_DN_MISMATCH);
2650 }
2651 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2653 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2654 "match with '%s'", buf, match);
2655 preverify_ok = 0;
2656 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2657 "Subject mismatch",
2658 TLS_FAIL_SUBJECT_MISMATCH);
2659 } else if (depth == 0 && altmatch &&
2660 !tls_match_altsubject(err_cert, altmatch)) {
2661 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2662 "'%s' not found", altmatch);
2663 preverify_ok = 0;
2664 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2665 "AltSubject mismatch",
2666 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002667 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002668 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002669 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2670 suffix_match);
2671 preverify_ok = 0;
2672 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2673 "Domain suffix mismatch",
2674 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002675 } else if (depth == 0 && domain_match &&
2676 !tls_match_suffix(err_cert, domain_match, 1)) {
2677 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2678 domain_match);
2679 preverify_ok = 0;
2680 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2681 "Domain mismatch",
2682 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002683 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684
2685 if (conn->cert_probe && preverify_ok && depth == 0) {
2686 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2687 "on probe-only run");
2688 preverify_ok = 0;
2689 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2690 "Server certificate chain probe",
2691 TLS_FAIL_SERVER_CHAIN_PROBE);
2692 }
2693
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002694#ifdef CONFIG_SUITEB
2695 if (conn->flags & TLS_CONN_SUITEB) {
2696 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002697 int len = -1;
2698
2699 pk = X509_get_pubkey(err_cert);
2700 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002701 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002702 EVP_PKEY_free(pk);
2703 }
2704
2705 if (len >= 0) {
2706 wpa_printf(MSG_DEBUG,
2707 "OpenSSL: RSA modulus size: %d bits", len);
2708 if (len < 3072) {
2709 preverify_ok = 0;
2710 openssl_tls_fail_event(
2711 conn, err_cert, err,
2712 depth, buf,
2713 "Insufficient RSA modulus size",
2714 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2715 }
2716 }
2717 }
2718#endif /* CONFIG_SUITEB */
2719
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002720#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002721 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2722 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002723 enum ocsp_result res;
2724
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002725 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2726 conn->peer_issuer,
2727 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002728 if (res == OCSP_REVOKED) {
2729 preverify_ok = 0;
2730 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2731 "certificate revoked",
2732 TLS_FAIL_REVOKED);
2733 if (err == X509_V_OK)
2734 X509_STORE_CTX_set_error(
2735 x509_ctx, X509_V_ERR_CERT_REVOKED);
2736 } else if (res != OCSP_GOOD &&
2737 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2738 preverify_ok = 0;
2739 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2740 "bad certificate status response",
2741 TLS_FAIL_UNSPECIFIED);
2742 }
2743 }
2744#endif /* OPENSSL_IS_BORINGSSL */
2745
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002746 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002747 context->event_cb(context->cb_ctx,
2748 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002749
Hai Shalom899fcc72020-10-19 14:38:18 -07002750 if (depth == 0 && preverify_ok) {
2751 os_free(conn->peer_subject);
2752 conn->peer_subject = os_strdup(buf);
2753 }
2754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002755 return preverify_ok;
2756}
2757
2758
2759#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002760static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002762 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 X509_LOOKUP *lookup;
2764 int ret = 0;
2765
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002766 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 X509_LOOKUP_file());
2768 if (lookup == NULL) {
2769 tls_show_errors(MSG_WARNING, __func__,
2770 "Failed add lookup for X509 store");
2771 return -1;
2772 }
2773
2774 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2775 unsigned long err = ERR_peek_error();
2776 tls_show_errors(MSG_WARNING, __func__,
2777 "Failed load CA in DER format");
2778 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2779 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2780 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2781 "cert already in hash table error",
2782 __func__);
2783 } else
2784 ret = -1;
2785 }
2786
2787 return ret;
2788}
2789#endif /* OPENSSL_NO_STDIO */
2790
2791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002792static int tls_connection_ca_cert(struct tls_data *data,
2793 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 const char *ca_cert, const u8 *ca_cert_blob,
2795 size_t ca_cert_blob_len, const char *ca_path)
2796{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002797 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002798 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799
2800 /*
2801 * Remove previously configured trusted CA certificates before adding
2802 * new ones.
2803 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002804 store = X509_STORE_new();
2805 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2807 "certificate store", __func__);
2808 return -1;
2809 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002810 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811
2812 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2813 conn->ca_cert_verify = 1;
2814
2815 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2816 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2817 "chain");
2818 conn->cert_probe = 1;
2819 conn->ca_cert_verify = 0;
2820 return 0;
2821 }
2822
2823 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2824#ifdef CONFIG_SHA256
2825 const char *pos = ca_cert + 7;
2826 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2827 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2828 "hash value '%s'", ca_cert);
2829 return -1;
2830 }
2831 pos += 14;
2832 if (os_strlen(pos) != 32 * 2) {
2833 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2834 "hash length in ca_cert '%s'", ca_cert);
2835 return -1;
2836 }
2837 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2838 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2839 "value in ca_cert '%s'", ca_cert);
2840 return -1;
2841 }
2842 conn->server_cert_only = 1;
2843 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2844 "certificate match");
2845 return 0;
2846#else /* CONFIG_SHA256 */
2847 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2848 "cannot validate server certificate hash");
2849 return -1;
2850#endif /* CONFIG_SHA256 */
2851 }
2852
2853 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002854 X509 *cert = d2i_X509(NULL,
2855 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 ca_cert_blob_len);
2857 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002858 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2859 ca_cert_blob_len);
2860
2861 if (bio) {
2862 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2863 BIO_free(bio);
2864 }
2865
2866 if (!cert) {
2867 tls_show_errors(MSG_WARNING, __func__,
2868 "Failed to parse ca_cert_blob");
2869 return -1;
2870 }
2871
2872 while (ERR_get_error()) {
2873 /* Ignore errors from DER conversion. */
2874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 }
2876
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002877 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2878 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 unsigned long err = ERR_peek_error();
2880 tls_show_errors(MSG_WARNING, __func__,
2881 "Failed to add ca_cert_blob to "
2882 "certificate store");
2883 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2884 ERR_GET_REASON(err) ==
2885 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2886 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2887 "cert already in hash table error",
2888 __func__);
2889 } else {
2890 X509_free(cert);
2891 return -1;
2892 }
2893 }
2894 X509_free(cert);
2895 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2896 "to certificate store", __func__);
2897 return 0;
2898 }
2899
2900#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002901 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002902 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2903 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002904 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Hai Shalom7ad2a872021-08-02 18:56:55 -07002905 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002907 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2908 return 0;
2909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002911 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002912 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2913 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2914 char *aliases = os_strdup(
2915 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002916 const char *delim = " ";
2917 int rc = 0;
2918 char *savedptr;
2919 char *alias;
2920
2921 if (!aliases)
2922 return -1;
2923 alias = strtok_r(aliases, delim, &savedptr);
2924 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2925 if (tls_add_ca_from_keystore_encoded(
Hai Shalom7ad2a872021-08-02 18:56:55 -07002926 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
2927 wpa_printf(MSG_ERROR,
2928 "OpenSSL: Failed to add ca_cert %s from keystore",
2929 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002930 rc = -1;
2931 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 }
2933 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002934 os_free(aliases);
2935 if (rc)
2936 return rc;
2937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2939 return 0;
2940 }
2941#endif /* ANDROID */
2942
2943#ifdef CONFIG_NATIVE_WINDOWS
2944 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2945 0) {
2946 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2947 "system certificate store");
2948 return 0;
2949 }
2950#endif /* CONFIG_NATIVE_WINDOWS */
2951
2952 if (ca_cert || ca_path) {
2953#ifndef OPENSSL_NO_STDIO
2954 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2955 1) {
2956 tls_show_errors(MSG_WARNING, __func__,
2957 "Failed to load root certificates");
2958 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002959 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2961 "DER format CA certificate",
2962 __func__);
2963 } else
2964 return -1;
2965 } else {
2966 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2967 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002968 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 }
2970#else /* OPENSSL_NO_STDIO */
2971 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2972 __func__);
2973 return -1;
2974#endif /* OPENSSL_NO_STDIO */
2975 } else {
2976 /* No ca_cert configured - do not try to verify server
2977 * certificate */
2978 conn->ca_cert_verify = 0;
2979 }
2980
2981 return 0;
2982}
2983
2984
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002985static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002987 SSL_CTX *ssl_ctx = data->ssl;
2988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 if (ca_cert) {
2990 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2991 {
2992 tls_show_errors(MSG_WARNING, __func__,
2993 "Failed to load root certificates");
2994 return -1;
2995 }
2996
2997 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2998 "certificate(s) loaded");
2999
3000#ifndef OPENSSL_NO_STDIO
3001 /* Add the same CAs to the client certificate requests */
3002 SSL_CTX_set_client_CA_list(ssl_ctx,
3003 SSL_load_client_CA_file(ca_cert));
3004#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003005
3006 os_free(data->ca_cert);
3007 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008 }
3009
3010 return 0;
3011}
3012
3013
Hai Shalom74f70d42019-02-11 14:42:39 -08003014int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015{
3016 int flags;
3017
3018 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003019 struct tls_data *data = ssl_ctx;
3020 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 if (cs == NULL) {
3022 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3023 "certificate store when enabling "
3024 "check_crl");
3025 return -1;
3026 }
3027 flags = X509_V_FLAG_CRL_CHECK;
3028 if (check_crl == 2)
3029 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3030 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003031
3032 data->check_crl = check_crl;
3033 data->check_crl_strict = strict;
3034 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 }
3036 return 0;
3037}
3038
3039
3040static int tls_connection_set_subject_match(struct tls_connection *conn,
3041 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003042 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003043 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003044 const char *domain_match,
3045 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046{
3047 os_free(conn->subject_match);
3048 conn->subject_match = NULL;
3049 if (subject_match) {
3050 conn->subject_match = os_strdup(subject_match);
3051 if (conn->subject_match == NULL)
3052 return -1;
3053 }
3054
3055 os_free(conn->altsubject_match);
3056 conn->altsubject_match = NULL;
3057 if (altsubject_match) {
3058 conn->altsubject_match = os_strdup(altsubject_match);
3059 if (conn->altsubject_match == NULL)
3060 return -1;
3061 }
3062
Dmitry Shmidt051af732013-10-22 13:52:46 -07003063 os_free(conn->suffix_match);
3064 conn->suffix_match = NULL;
3065 if (suffix_match) {
3066 conn->suffix_match = os_strdup(suffix_match);
3067 if (conn->suffix_match == NULL)
3068 return -1;
3069 }
3070
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003071 os_free(conn->domain_match);
3072 conn->domain_match = NULL;
3073 if (domain_match) {
3074 conn->domain_match = os_strdup(domain_match);
3075 if (conn->domain_match == NULL)
3076 return -1;
3077 }
3078
Hai Shalom021b0b52019-04-10 11:17:58 -07003079 os_free(conn->check_cert_subject);
3080 conn->check_cert_subject = NULL;
3081 if (check_cert_subject) {
3082 conn->check_cert_subject = os_strdup(check_cert_subject);
3083 if (!conn->check_cert_subject)
3084 return -1;
3085 }
3086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087 return 0;
3088}
3089
3090
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003091#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003092static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003093{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003094 struct tls_connection *conn = arg;
3095
3096 /*
3097 * This cert_cb() is not really the best location for doing a
3098 * constraint check for the ServerKeyExchange message, but this seems to
3099 * be the only place where the current OpenSSL sequence can be
3100 * terminated cleanly with an TLS alert going out to the server.
3101 */
3102
3103 if (!(conn->flags & TLS_CONN_SUITEB))
3104 return 1;
3105
3106 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3107 if (conn->cipher_suite != 0x9f)
3108 return 1;
3109
3110 if (conn->server_dh_prime_len >= 3072)
3111 return 1;
3112
3113 wpa_printf(MSG_DEBUG,
3114 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3115 conn->server_dh_prime_len);
3116 return 0;
3117}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003118#endif /* CONFIG_SUITEB */
3119
3120
Roshan Pius3a1667e2018-07-03 15:17:14 -07003121static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3122 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003123{
3124 SSL *ssl = conn->ssl;
3125
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003126#ifdef SSL_OP_NO_TICKET
3127 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3128 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003129 else
3130 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003131#endif /* SSL_OP_NO_TICKET */
3132
Sunil Ravia04bd252022-05-02 22:54:18 -07003133#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3134 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3135 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3136#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3137
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003138#ifdef SSL_OP_NO_TLSv1
3139 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3140 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3141 else
3142 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3143#endif /* SSL_OP_NO_TLSv1 */
3144#ifdef SSL_OP_NO_TLSv1_1
3145 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3146 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3147 else
3148 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3149#endif /* SSL_OP_NO_TLSv1_1 */
3150#ifdef SSL_OP_NO_TLSv1_2
3151 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3152 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3153 else
3154 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3155#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003156#ifdef SSL_OP_NO_TLSv1_3
3157 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3158 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3159 else
3160 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3161#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003162#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3163 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3164 TLS_CONN_ENABLE_TLSv1_1 |
3165 TLS_CONN_ENABLE_TLSv1_2)) {
3166 int version = 0;
3167
3168 /* Explicit request to enable TLS versions even if needing to
3169 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003170 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003171 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003172 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3173 version = TLS1_1_VERSION;
3174 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3175 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003176 if (!version) {
3177 wpa_printf(MSG_DEBUG,
3178 "OpenSSL: Invalid TLS version configuration");
3179 return -1;
3180 }
3181
3182 if (SSL_set_min_proto_version(ssl, version) != 1) {
3183 wpa_printf(MSG_DEBUG,
3184 "OpenSSL: Failed to set minimum TLS version");
3185 return -1;
3186 }
3187 }
3188#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003189#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3190 !defined(LIBRESSL_VERSION_NUMBER) && \
3191 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003192 {
3193#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3194 int need_level = 0;
3195#else
3196 int need_level = 1;
3197#endif
3198
3199 if ((flags &
3200 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3201 SSL_get_security_level(ssl) > need_level) {
3202 /*
3203 * Need to drop to security level 1 (or 0 with OpenSSL
3204 * 3.0) to allow TLS versions older than 1.2 to be used
3205 * when explicitly enabled in configuration.
3206 */
3207 SSL_set_security_level(conn->ssl, need_level);
3208 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003209 }
3210#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003211
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003212#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003213#ifdef OPENSSL_IS_BORINGSSL
3214 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003215 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003216#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003217 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3218 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3219
Roshan Pius3a1667e2018-07-03 15:17:14 -07003220 if (openssl_ciphers) {
3221 wpa_printf(MSG_DEBUG,
3222 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3223 openssl_ciphers);
3224 ciphers = openssl_ciphers;
3225 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003226 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3227 wpa_printf(MSG_INFO,
3228 "OpenSSL: Failed to set Suite B ciphers");
3229 return -1;
3230 }
3231 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003232#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003233 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003234#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003235 const char *ciphers =
3236 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003237 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003238
Roshan Pius3a1667e2018-07-03 15:17:14 -07003239 if (openssl_ciphers) {
3240 wpa_printf(MSG_DEBUG,
3241 "OpenSSL: Override ciphers for Suite B: %s",
3242 openssl_ciphers);
3243 ciphers = openssl_ciphers;
3244 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003245 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3246 wpa_printf(MSG_INFO,
3247 "OpenSSL: Failed to set Suite B ciphers");
3248 return -1;
3249 }
3250
Sunil Ravia04bd252022-05-02 22:54:18 -07003251#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3252 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3253 wpa_printf(MSG_INFO,
3254 "OpenSSL: Failed to set Suite B groups");
3255 return -1;
3256 }
3257
3258#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003259 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003260 wpa_printf(MSG_INFO,
3261 "OpenSSL: Failed to set Suite B curves");
3262 return -1;
3263 }
3264
3265 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3266 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3267 EC_KEY_free(ecdh);
3268 wpa_printf(MSG_INFO,
3269 "OpenSSL: Failed to set ECDH parameter");
3270 return -1;
3271 }
3272 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003273#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003274 }
Hai Shalom0f94b7a2023-03-13 13:22:35 -07003275 if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH))
3276#ifdef EAP_TLSV1_3
3277 && (flags & TLS_CONN_DISABLE_TLSv1_3)
3278#endif
3279 ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003280#ifdef OPENSSL_IS_BORINGSSL
3281 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3282
Jimmy Chen916e0a72022-01-11 15:19:46 +08003283 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003284 1) != 1) {
3285 wpa_printf(MSG_INFO,
3286 "OpenSSL: Failed to set Suite B sigalgs");
3287 return -1;
3288 }
3289#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003290 /* ECDSA+SHA384 if need to add EC support here */
3291 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3292 wpa_printf(MSG_INFO,
3293 "OpenSSL: Failed to set Suite B sigalgs");
3294 return -1;
3295 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003296#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003297
3298 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3299 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3300 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3301 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003302
3303#ifdef OPENSSL_IS_BORINGSSL
3304 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3305 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3306 int nid[1] = { NID_secp384r1 };
3307
3308 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3309 wpa_printf(MSG_INFO,
3310 "OpenSSL: Failed to set Suite B curves");
3311 return -1;
3312 }
3313
Jimmy Chen916e0a72022-01-11 15:19:46 +08003314 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003315 1) != 1) {
3316 wpa_printf(MSG_INFO,
3317 "OpenSSL: Failed to set Suite B sigalgs");
3318 return -1;
3319 }
3320 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003321#else /* OPENSSL_IS_BORINGSSL */
3322 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3323 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3324 wpa_printf(MSG_INFO,
3325 "OpenSSL: Failed to set openssl_ciphers '%s'",
3326 openssl_ciphers);
3327 return -1;
3328 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003329#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003330#else /* CONFIG_SUITEB */
3331 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3332 wpa_printf(MSG_INFO,
3333 "OpenSSL: Failed to set openssl_ciphers '%s'",
3334 openssl_ciphers);
3335 return -1;
3336 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003337#endif /* CONFIG_SUITEB */
3338
Hai Shalom81f62d82019-07-22 12:10:00 -07003339 if (flags & TLS_CONN_TEAP_ANON_DH) {
3340#ifndef TEAP_DH_ANON_CS
3341#define TEAP_DH_ANON_CS \
3342 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3343 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3344 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3345 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3346 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3347 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3348 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3349 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3350#endif
3351 static const char *cs = TEAP_DH_ANON_CS;
3352
3353#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3354 !defined(LIBRESSL_VERSION_NUMBER) && \
3355 !defined(OPENSSL_IS_BORINGSSL)
3356 /*
3357 * Need to drop to security level 0 to allow anonymous
3358 * cipher suites for EAP-TEAP.
3359 */
3360 SSL_set_security_level(conn->ssl, 0);
3361#endif
3362
3363 wpa_printf(MSG_DEBUG,
3364 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3365 cs);
3366 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3367 tls_show_errors(MSG_INFO, __func__,
3368 "Cipher suite configuration failed");
3369 return -1;
3370 }
3371 }
3372
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003373 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003374}
3375
3376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003378 int verify_peer, unsigned int flags,
3379 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380{
3381 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003382 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383
3384 if (conn == NULL)
3385 return -1;
3386
Hai Shalom899fcc72020-10-19 14:38:18 -07003387 if (verify_peer == 2) {
3388 conn->ca_cert_verify = 1;
3389 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3390 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3391 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392 conn->ca_cert_verify = 1;
3393 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3394 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3395 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3396 } else {
3397 conn->ca_cert_verify = 0;
3398 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3399 }
3400
Roshan Pius3a1667e2018-07-03 15:17:14 -07003401 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003402 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003403 conn->flags = flags;
3404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 SSL_set_accept_state(conn->ssl);
3406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003407 if (data->tls_session_lifetime == 0) {
3408 /*
3409 * Set session id context to a unique value to make sure
3410 * session resumption cannot be used either through session
3411 * caching or TLS ticket extension.
3412 */
3413 counter++;
3414 SSL_set_session_id_context(conn->ssl,
3415 (const unsigned char *) &counter,
3416 sizeof(counter));
3417 } else if (session_ctx) {
3418 SSL_set_session_id_context(conn->ssl, session_ctx,
3419 session_ctx_len);
3420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421
3422 return 0;
3423}
3424
3425
3426static int tls_connection_client_cert(struct tls_connection *conn,
3427 const char *client_cert,
3428 const u8 *client_cert_blob,
3429 size_t client_cert_blob_len)
3430{
3431 if (client_cert == NULL && client_cert_blob == NULL)
3432 return 0;
3433
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003434#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003435#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003436 /*
3437 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003438 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003439 * chain properly.
3440 */
3441 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003442#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003443#endif /* PKCS12_FUNCS */
3444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003445 if (client_cert_blob &&
3446 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3447 client_cert_blob_len) == 1) {
3448 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3449 "OK");
3450 return 0;
3451 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003452#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 tls_show_errors(MSG_DEBUG, __func__,
3454 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003455#else
3456 BIO *bio;
3457 X509 *x509;
3458
3459 tls_show_errors(MSG_DEBUG, __func__,
3460 "SSL_use_certificate_ASN1 failed");
3461 bio = BIO_new(BIO_s_mem());
3462 if (!bio)
3463 return -1;
3464 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3465 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3466 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3467 X509_free(x509);
3468 BIO_free(bio);
3469 return -1;
3470 }
3471 X509_free(x509);
3472 wpa_printf(MSG_DEBUG,
3473 "OpenSSL: Found PEM encoded certificate from blob");
3474 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3475 wpa_printf(MSG_DEBUG,
3476 "OpenSSL: Added an additional certificate into the chain");
3477 SSL_add0_chain_cert(conn->ssl, x509);
3478 }
3479 BIO_free(bio);
3480 return 0;
3481#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 }
3483
3484 if (client_cert == NULL)
3485 return -1;
3486
3487#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003488 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3489 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
3490 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003492 if (!bio) {
3493 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003494 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003495 // Keystore returns X.509 certificates in PEM encoding
3496 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3497 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003499 BIO_free(bio);
3500 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3501 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003503 X509_free(x509);
3504 wpa_printf(MSG_DEBUG,
3505 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3506 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003507
Hai Shalom7ad2a872021-08-02 18:56:55 -07003508 // Read additional certificates into the chain
3509 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3510 wpa_printf(MSG_DEBUG,
3511 "OpenSSL: Added an additional certificate into the chain");
3512 // Takes ownership of x509, no need to free it here
3513 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003514 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003515 BIO_free(bio);
3516 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517 }
3518#endif /* ANDROID */
3519
3520#ifndef OPENSSL_NO_STDIO
3521 if (SSL_use_certificate_file(conn->ssl, client_cert,
3522 SSL_FILETYPE_ASN1) == 1) {
3523 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3524 " --> OK");
3525 return 0;
3526 }
3527
Hai Shalom021b0b52019-04-10 11:17:58 -07003528#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3529 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003530 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3531 ERR_clear_error();
3532 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3533 " --> OK");
3534 return 0;
3535 }
3536#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537 if (SSL_use_certificate_file(conn->ssl, client_cert,
3538 SSL_FILETYPE_PEM) == 1) {
3539 ERR_clear_error();
3540 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3541 " --> OK");
3542 return 0;
3543 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003544#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545
3546 tls_show_errors(MSG_DEBUG, __func__,
3547 "SSL_use_certificate_file failed");
3548#else /* OPENSSL_NO_STDIO */
3549 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3550#endif /* OPENSSL_NO_STDIO */
3551
3552 return -1;
3553}
3554
3555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003556static int tls_global_client_cert(struct tls_data *data,
3557 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558{
3559#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003560 SSL_CTX *ssl_ctx = data->ssl;
3561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 if (client_cert == NULL)
3563 return 0;
3564
3565 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3566 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003567 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3569 SSL_FILETYPE_PEM) != 1) {
3570 tls_show_errors(MSG_INFO, __func__,
3571 "Failed to load client certificate");
3572 return -1;
3573 }
3574 return 0;
3575#else /* OPENSSL_NO_STDIO */
3576 if (client_cert == NULL)
3577 return 0;
3578 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3579 return -1;
3580#endif /* OPENSSL_NO_STDIO */
3581}
3582
3583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003585static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 const char *passwd)
3587{
3588 EVP_PKEY *pkey;
3589 X509 *cert;
3590 STACK_OF(X509) *certs;
3591 int res = 0;
3592 char buf[256];
3593
3594 pkey = NULL;
3595 cert = NULL;
3596 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003597 if (!passwd)
3598 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3600 tls_show_errors(MSG_DEBUG, __func__,
3601 "Failed to parse PKCS12 file");
3602 PKCS12_free(p12);
3603 return -1;
3604 }
3605 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3606
3607 if (cert) {
3608 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3609 sizeof(buf));
3610 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3611 "subject='%s'", buf);
3612 if (ssl) {
3613 if (SSL_use_certificate(ssl, cert) != 1)
3614 res = -1;
3615 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003616 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 res = -1;
3618 }
3619 X509_free(cert);
3620 }
3621
3622 if (pkey) {
3623 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3624 if (ssl) {
3625 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3626 res = -1;
3627 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003628 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 res = -1;
3630 }
3631 EVP_PKEY_free(pkey);
3632 }
3633
3634 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003635#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003636 if (ssl)
3637 SSL_clear_chain_certs(ssl);
3638 else
3639 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003640 while ((cert = sk_X509_pop(certs)) != NULL) {
3641 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3642 sizeof(buf));
3643 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3644 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003645 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3646 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3647 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003648 tls_show_errors(MSG_DEBUG, __func__,
3649 "Failed to add additional certificate");
3650 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003651 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003652 break;
3653 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003654 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003655 }
3656 if (!res) {
3657 /* Try to continue anyway */
3658 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003659 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003660#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003661 if (ssl)
3662 res = SSL_build_cert_chain(
3663 ssl,
3664 SSL_BUILD_CHAIN_FLAG_CHECK |
3665 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3666 else
3667 res = SSL_CTX_build_cert_chain(
3668 data->ssl,
3669 SSL_BUILD_CHAIN_FLAG_CHECK |
3670 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003671 if (!res) {
3672 tls_show_errors(MSG_DEBUG, __func__,
3673 "Failed to build certificate chain");
3674 } else if (res == 2) {
3675 wpa_printf(MSG_DEBUG,
3676 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3677 }
3678#endif /* OPENSSL_IS_BORINGSSL */
3679 /*
3680 * Try to continue regardless of result since it is possible for
3681 * the extra certificates not to be required.
3682 */
3683 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003684#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003685 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 while ((cert = sk_X509_pop(certs)) != NULL) {
3687 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3688 sizeof(buf));
3689 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3690 " from PKCS12: subject='%s'", buf);
3691 /*
3692 * There is no SSL equivalent for the chain cert - so
3693 * always add it to the context...
3694 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003695 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3696 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003697 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 res = -1;
3699 break;
3700 }
3701 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003702 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003703#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 }
3705
3706 PKCS12_free(p12);
3707
3708 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003709 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710
3711 return res;
3712}
3713#endif /* PKCS12_FUNCS */
3714
3715
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003716static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3717 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718{
3719#ifdef PKCS12_FUNCS
3720 FILE *f;
3721 PKCS12 *p12;
3722
3723 f = fopen(private_key, "rb");
3724 if (f == NULL)
3725 return -1;
3726
3727 p12 = d2i_PKCS12_fp(f, NULL);
3728 fclose(f);
3729
3730 if (p12 == NULL) {
3731 tls_show_errors(MSG_INFO, __func__,
3732 "Failed to use PKCS#12 file");
3733 return -1;
3734 }
3735
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003736 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737
3738#else /* PKCS12_FUNCS */
3739 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3740 "p12/pfx files");
3741 return -1;
3742#endif /* PKCS12_FUNCS */
3743}
3744
3745
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003746static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 const u8 *blob, size_t len, const char *passwd)
3748{
3749#ifdef PKCS12_FUNCS
3750 PKCS12 *p12;
3751
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003752 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 if (p12 == NULL) {
3754 tls_show_errors(MSG_INFO, __func__,
3755 "Failed to use PKCS#12 blob");
3756 return -1;
3757 }
3758
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003759 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760
3761#else /* PKCS12_FUNCS */
3762 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3763 "p12/pfx blobs");
3764 return -1;
3765#endif /* PKCS12_FUNCS */
3766}
3767
3768
3769#ifndef OPENSSL_NO_ENGINE
3770static int tls_engine_get_cert(struct tls_connection *conn,
3771 const char *cert_id,
3772 X509 **cert)
3773{
3774 /* this runs after the private key is loaded so no PIN is required */
3775 struct {
3776 const char *cert_id;
3777 X509 *cert;
3778 } params;
3779 params.cert_id = cert_id;
3780 params.cert = NULL;
3781
3782 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3783 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003784 unsigned long err = ERR_get_error();
3785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3787 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003788 ERR_error_string(err, NULL));
3789 if (tls_is_pin_error(err))
3790 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3792 }
3793 if (!params.cert) {
3794 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3795 " '%s'", cert_id);
3796 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3797 }
3798 *cert = params.cert;
3799 return 0;
3800}
3801#endif /* OPENSSL_NO_ENGINE */
3802
3803
3804static int tls_connection_engine_client_cert(struct tls_connection *conn,
3805 const char *cert_id)
3806{
3807#ifndef OPENSSL_NO_ENGINE
3808 X509 *cert;
3809
3810 if (tls_engine_get_cert(conn, cert_id, &cert))
3811 return -1;
3812
3813 if (!SSL_use_certificate(conn->ssl, cert)) {
3814 tls_show_errors(MSG_ERROR, __func__,
3815 "SSL_use_certificate failed");
3816 X509_free(cert);
3817 return -1;
3818 }
3819 X509_free(cert);
3820 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3821 "OK");
3822 return 0;
3823
3824#else /* OPENSSL_NO_ENGINE */
3825 return -1;
3826#endif /* OPENSSL_NO_ENGINE */
3827}
3828
3829
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003830static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 struct tls_connection *conn,
3832 const char *ca_cert_id)
3833{
3834#ifndef OPENSSL_NO_ENGINE
3835 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003836 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003837 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838
3839 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3840 return -1;
3841
3842 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003843 store = X509_STORE_new();
3844 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3846 "certificate store", __func__);
3847 X509_free(cert);
3848 return -1;
3849 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003850 SSL_CTX_set_cert_store(ssl_ctx, store);
3851 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 unsigned long err = ERR_peek_error();
3853 tls_show_errors(MSG_WARNING, __func__,
3854 "Failed to add CA certificate from engine "
3855 "to certificate store");
3856 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3857 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3858 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3859 " already in hash table error",
3860 __func__);
3861 } else {
3862 X509_free(cert);
3863 return -1;
3864 }
3865 }
3866 X509_free(cert);
3867 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3868 "to certificate store", __func__);
3869 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003870 conn->ca_cert_verify = 1;
3871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 return 0;
3873
3874#else /* OPENSSL_NO_ENGINE */
3875 return -1;
3876#endif /* OPENSSL_NO_ENGINE */
3877}
3878
3879
3880static int tls_connection_engine_private_key(struct tls_connection *conn)
3881{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003882#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3884 tls_show_errors(MSG_ERROR, __func__,
3885 "ENGINE: cannot use private key for TLS");
3886 return -1;
3887 }
3888 if (!SSL_check_private_key(conn->ssl)) {
3889 tls_show_errors(MSG_INFO, __func__,
3890 "Private key failed verification");
3891 return -1;
3892 }
3893 return 0;
3894#else /* OPENSSL_NO_ENGINE */
3895 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3896 "engine support was not compiled in");
3897 return -1;
3898#endif /* OPENSSL_NO_ENGINE */
3899}
3900
3901
Roshan Pius3a1667e2018-07-03 15:17:14 -07003902#ifndef OPENSSL_NO_STDIO
3903static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003904{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003905 if (!password)
3906 return 0;
3907 os_strlcpy(buf, (const char *) password, size);
3908 return os_strlen(buf);
3909}
3910#endif /* OPENSSL_NO_STDIO */
3911
3912
3913static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3914 const char *private_key,
3915 const char *private_key_passwd)
3916{
3917#ifndef OPENSSL_NO_STDIO
3918 BIO *bio;
3919 EVP_PKEY *pkey;
3920 int ret;
3921
3922 /* First try ASN.1 (DER). */
3923 bio = BIO_new_file(private_key, "r");
3924 if (!bio)
3925 return -1;
3926 pkey = d2i_PrivateKey_bio(bio, NULL);
3927 BIO_free(bio);
3928
3929 if (pkey) {
3930 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3931 } else {
3932 /* Try PEM with the provided password. */
3933 bio = BIO_new_file(private_key, "r");
3934 if (!bio)
3935 return -1;
3936 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3937 (void *) private_key_passwd);
3938 BIO_free(bio);
3939 if (!pkey)
3940 return -1;
3941 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3942 /* Clear errors from the previous failed load. */
3943 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003944 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003945
3946 if (ssl)
3947 ret = SSL_use_PrivateKey(ssl, pkey);
3948 else
3949 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3950
3951 EVP_PKEY_free(pkey);
3952 return ret == 1 ? 0 : -1;
3953#else /* OPENSSL_NO_STDIO */
3954 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3955 return -1;
3956#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003957}
3958
3959
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003960static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 struct tls_connection *conn,
3962 const char *private_key,
3963 const char *private_key_passwd,
3964 const u8 *private_key_blob,
3965 size_t private_key_blob_len)
3966{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003967 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 int ok;
3969
3970 if (private_key == NULL && private_key_blob == NULL)
3971 return 0;
3972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 ok = 0;
3974 while (private_key_blob) {
3975 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3976 (u8 *) private_key_blob,
3977 private_key_blob_len) == 1) {
3978 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3979 "ASN1(EVP_PKEY_RSA) --> OK");
3980 ok = 1;
3981 break;
3982 }
3983
3984 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3985 (u8 *) private_key_blob,
3986 private_key_blob_len) == 1) {
3987 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3988 "ASN1(EVP_PKEY_DSA) --> OK");
3989 ok = 1;
3990 break;
3991 }
3992
Hai Shalom899fcc72020-10-19 14:38:18 -07003993#ifndef OPENSSL_NO_EC
3994 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
3995 (u8 *) private_key_blob,
3996 private_key_blob_len) == 1) {
3997 wpa_printf(MSG_DEBUG,
3998 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
3999 ok = 1;
4000 break;
4001 }
4002#endif /* OPENSSL_NO_EC */
4003
Sunil Ravia04bd252022-05-02 22:54:18 -07004004#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4006 (u8 *) private_key_blob,
4007 private_key_blob_len) == 1) {
4008 wpa_printf(MSG_DEBUG, "OpenSSL: "
4009 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4010 ok = 1;
4011 break;
4012 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004013#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014
Hai Shaloma20dcd72022-02-04 13:43:00 -08004015 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4016 private_key_blob_len);
4017 if (bio) {
4018 EVP_PKEY *pkey;
4019
4020 pkey = PEM_read_bio_PrivateKey(
4021 bio, NULL, tls_passwd_cb,
4022 (void *) private_key_passwd);
4023 if (pkey) {
4024 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4025 wpa_printf(MSG_DEBUG,
4026 "OpenSSL: SSL_use_PrivateKey --> OK");
4027 ok = 1;
4028 EVP_PKEY_free(pkey);
4029 BIO_free(bio);
4030 break;
4031 }
4032 EVP_PKEY_free(pkey);
4033 }
4034 BIO_free(bio);
4035 }
4036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004037 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004038 private_key_blob_len,
4039 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4041 "OK");
4042 ok = 1;
4043 break;
4044 }
4045
4046 break;
4047 }
4048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004050 if (tls_use_private_key_file(data, conn->ssl, private_key,
4051 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 ok = 1;
4053 break;
4054 }
4055
Roshan Pius3a1667e2018-07-03 15:17:14 -07004056 if (tls_read_pkcs12(data, conn->ssl, private_key,
4057 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4059 "--> OK");
4060 ok = 1;
4061 break;
4062 }
4063
4064 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4065 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4066 "access certificate store --> OK");
4067 ok = 1;
4068 break;
4069 }
4070
4071 break;
4072 }
4073
4074 if (!ok) {
4075 tls_show_errors(MSG_INFO, __func__,
4076 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 return -1;
4078 }
4079 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 if (!SSL_check_private_key(conn->ssl)) {
4082 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4083 "verification");
4084 return -1;
4085 }
4086
4087 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4088 return 0;
4089}
4090
4091
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004092static int tls_global_private_key(struct tls_data *data,
4093 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 const char *private_key_passwd)
4095{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004096 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
4098 if (private_key == NULL)
4099 return 0;
4100
Roshan Pius3a1667e2018-07-03 15:17:14 -07004101 if (tls_use_private_key_file(data, NULL, private_key,
4102 private_key_passwd) &&
4103 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 tls_show_errors(MSG_INFO, __func__,
4105 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 ERR_clear_error();
4107 return -1;
4108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4112 tls_show_errors(MSG_INFO, __func__,
4113 "Private key failed verification");
4114 return -1;
4115 }
4116
4117 return 0;
4118}
4119
4120
Sunil Ravia04bd252022-05-02 22:54:18 -07004121#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4122#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004124/* This is needed to replace the deprecated DSA_dup_DH() function */
4125static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4126{
4127 OSSL_PARAM_BLD *bld = NULL;
4128 OSSL_PARAM *params = NULL;
4129 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4130 EVP_PKEY_CTX *ctx = NULL;
4131 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132
Sunil Ravia04bd252022-05-02 22:54:18 -07004133 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4134 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4135 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4136 !(bld = OSSL_PARAM_BLD_new()) ||
4137 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4138 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4139 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4140 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4141 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4142 EVP_PKEY_fromdata_init(ctx) != 1 ||
4143 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4144 params) != 1)
4145 wpa_printf(MSG_INFO,
4146 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147
Sunil Ravia04bd252022-05-02 22:54:18 -07004148 EVP_PKEY_CTX_free(ctx);
4149 OSSL_PARAM_free(params);
4150 OSSL_PARAM_BLD_free(bld);
4151 BN_free(p);
4152 BN_free(q);
4153 BN_free(g);
4154 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155}
Sunil Ravia04bd252022-05-02 22:54:18 -07004156#endif /* !OPENSSL_NO_DSA */
4157#endif /* OPENSSL_NO_DH */
4158#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004160static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161{
4162#ifdef OPENSSL_NO_DH
4163 if (dh_file == NULL)
4164 return 0;
4165 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4166 "dh_file specified");
4167 return -1;
4168#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004169#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4170 SSL_CTX *ssl_ctx = data->ssl;
4171 BIO *bio;
4172 OSSL_DECODER_CTX *ctx = NULL;
4173 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4174 bool dsa = false;
4175
4176 if (!ssl_ctx)
4177 return -1;
4178 if (!dh_file) {
4179 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4180 return 0;
4181 }
4182
4183 bio = BIO_new_file(dh_file, "r");
4184 if (!bio) {
4185 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4186 dh_file, ERR_error_string(ERR_get_error(), NULL));
4187 return -1;
4188 }
4189 ctx = OSSL_DECODER_CTX_new_for_pkey(
4190 &tmpkey, "PEM", NULL, NULL,
4191 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4192 if (!ctx ||
4193 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4194 wpa_printf(MSG_INFO,
4195 "TLS: Failed to decode domain parameters from '%s': %s",
4196 dh_file, ERR_error_string(ERR_get_error(), NULL));
4197 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004198 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004199 return -1;
4200 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004201 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004202 BIO_free(bio);
4203
4204 if (!tmpkey) {
4205 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4206 return -1;
4207 }
4208
4209#ifndef OPENSSL_NO_DSA
4210 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4211 pkey = openssl_dsa_to_dh(tmpkey);
4212 EVP_PKEY_free(tmpkey);
4213 if (!pkey)
4214 return -1;
4215 dsa = true;
4216 }
4217#endif /* !OPENSSL_NO_DSA */
4218 if (!dsa) {
4219 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4220 EVP_PKEY_is_a(tmpkey, "DHX")) {
4221 } else {
4222 wpa_printf(MSG_INFO,
4223 "TLS: No DH parameters found in %s",
4224 dh_file);
4225 EVP_PKEY_free(tmpkey);
4226 return -1;
4227 }
4228 pkey = tmpkey;
4229 tmpkey = NULL;
4230 }
4231
4232 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4233 wpa_printf(MSG_INFO,
4234 "TLS: Failed to set DH params from '%s': %s",
4235 dh_file, ERR_error_string(ERR_get_error(), NULL));
4236 EVP_PKEY_free(pkey);
4237 return -1;
4238 }
4239 return 0;
4240#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004241 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242 DH *dh;
4243 BIO *bio;
4244
Sunil Ravia04bd252022-05-02 22:54:18 -07004245 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004247 if (!dh_file) {
4248#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4249 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4250#endif
4251 return 0;
4252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253
4254 bio = BIO_new_file(dh_file, "r");
4255 if (bio == NULL) {
4256 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4257 dh_file, ERR_error_string(ERR_get_error(), NULL));
4258 return -1;
4259 }
4260 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4261 BIO_free(bio);
4262#ifndef OPENSSL_NO_DSA
4263 while (dh == NULL) {
4264 DSA *dsa;
4265 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4266 " trying to parse as DSA params", dh_file,
4267 ERR_error_string(ERR_get_error(), NULL));
4268 bio = BIO_new_file(dh_file, "r");
4269 if (bio == NULL)
4270 break;
4271 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4272 BIO_free(bio);
4273 if (!dsa) {
4274 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4275 "'%s': %s", dh_file,
4276 ERR_error_string(ERR_get_error(), NULL));
4277 break;
4278 }
4279
4280 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4281 dh = DSA_dup_DH(dsa);
4282 DSA_free(dsa);
4283 if (dh == NULL) {
4284 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4285 "params into DH params");
4286 break;
4287 }
4288 break;
4289 }
4290#endif /* !OPENSSL_NO_DSA */
4291 if (dh == NULL) {
4292 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4293 "'%s'", dh_file);
4294 return -1;
4295 }
4296
4297 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4298 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4299 "%s", dh_file,
4300 ERR_error_string(ERR_get_error(), NULL));
4301 DH_free(dh);
4302 return -1;
4303 }
4304 DH_free(dh);
4305 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004306#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307#endif /* OPENSSL_NO_DH */
4308}
4309
4310
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004311int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4312 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313{
4314 SSL *ssl;
4315
4316 if (conn == NULL || keys == NULL)
4317 return -1;
4318 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004319 if (ssl == NULL)
4320 return -1;
4321
4322 os_memset(keys, 0, sizeof(*keys));
4323 keys->client_random = conn->client_random;
4324 keys->client_random_len = SSL_get_client_random(
4325 ssl, conn->client_random, sizeof(conn->client_random));
4326 keys->server_random = conn->server_random;
4327 keys->server_random_len = SSL_get_server_random(
4328 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329
4330 return 0;
4331}
4332
4333
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004334#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004335static int openssl_get_keyblock_size(SSL *ssl)
4336{
Sunil Ravia04bd252022-05-02 22:54:18 -07004337#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004338 const EVP_CIPHER *c;
4339 const EVP_MD *h;
4340 int md_size;
4341
4342 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4343 ssl->read_hash == NULL)
4344 return -1;
4345
4346 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004347 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004348 if (h)
4349 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004350 else if (ssl->s3)
4351 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004352 else
4353 return -1;
4354
4355 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4356 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4357 EVP_CIPHER_iv_length(c));
4358 return 2 * (EVP_CIPHER_key_length(c) +
4359 md_size +
4360 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004361#else
4362 const SSL_CIPHER *ssl_cipher;
4363 int cipher, digest;
4364 const EVP_CIPHER *c;
4365 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004366 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004367
4368 ssl_cipher = SSL_get_current_cipher(ssl);
4369 if (!ssl_cipher)
4370 return -1;
4371 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4372 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4373 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4374 cipher, digest);
4375 if (cipher < 0 || digest < 0)
4376 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004377 if (cipher == NID_undef) {
4378 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004379 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004380 }
4381 c = EVP_get_cipherbynid(cipher);
4382 if (!c)
4383 return -1;
4384 enc_key_len = EVP_CIPHER_key_length(c);
4385 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4386 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4387 fixed_iv_len = 4; /* only part of IV from PRF */
4388 else
4389 fixed_iv_len = EVP_CIPHER_iv_length(c);
4390 if (digest == NID_undef) {
4391 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4392 mac_key_len = 0;
4393 } else {
4394 h = EVP_get_digestbynid(digest);
4395 if (!h)
4396 return -1;
4397 mac_key_len = EVP_MD_size(h);
4398 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004399
4400 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004401 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4402 mac_key_len, enc_key_len, fixed_iv_len);
4403 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004404#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004405}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004406#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004407
4408
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004409int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004410 const char *label, const u8 *context,
4411 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004412{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004413 if (!conn ||
4414 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004415 os_strlen(label), context, context_len,
4416 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004417 return -1;
4418 return 0;
4419}
4420
4421
4422int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4423 u8 *out, size_t out_len)
4424{
4425#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004426 SSL *ssl;
4427 SSL_SESSION *sess;
4428 u8 *rnd;
4429 int ret = -1;
4430 int skip = 0;
4431 u8 *tmp_out = NULL;
4432 u8 *_out = out;
4433 unsigned char client_random[SSL3_RANDOM_SIZE];
4434 unsigned char server_random[SSL3_RANDOM_SIZE];
4435 unsigned char master_key[64];
4436 size_t master_key_len;
4437 const char *ver;
4438
4439 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004440 * TLS library did not support EAP-FAST key generation, so get the
4441 * needed TLS session parameters and use an internal implementation of
4442 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004443 */
4444
4445 if (conn == NULL)
4446 return -1;
4447 ssl = conn->ssl;
4448 if (ssl == NULL)
4449 return -1;
4450 ver = SSL_get_version(ssl);
4451 sess = SSL_get_session(ssl);
4452 if (!ver || !sess)
4453 return -1;
4454
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004455 skip = openssl_get_keyblock_size(ssl);
4456 if (skip < 0)
4457 return -1;
4458 tmp_out = os_malloc(skip + out_len);
4459 if (!tmp_out)
4460 return -1;
4461 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004462
4463 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4464 if (!rnd) {
4465 os_free(tmp_out);
4466 return -1;
4467 }
4468
4469 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4470 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4471 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4472 sizeof(master_key));
4473
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004474 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4475 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004476
4477 if (os_strcmp(ver, "TLSv1.2") == 0) {
4478 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004479 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004480 _out, skip + out_len);
4481 ret = 0;
4482 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004483 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004484 _out, skip + out_len) == 0) {
4485 ret = 0;
4486 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004487 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004488 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004489 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004490 os_memcpy(out, _out + skip, out_len);
4491 bin_clear_free(tmp_out, skip);
4492
4493 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004494#else /* OPENSSL_NEED_EAP_FAST_PRF */
4495 wpa_printf(MSG_ERROR,
4496 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4497 return -1;
4498#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499}
4500
4501
4502static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004503openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504{
Sunil Ravia04bd252022-05-02 22:54:18 -07004505 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 int res;
4507 struct wpabuf *out_data;
4508
4509 /*
4510 * Give TLS handshake data from the server (if available) to OpenSSL
4511 * for processing.
4512 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004513 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4515 < 0) {
4516 tls_show_errors(MSG_INFO, __func__,
4517 "Handshake failed - BIO_write");
4518 return NULL;
4519 }
4520
4521 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004522 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 res = SSL_accept(conn->ssl);
4524 else
4525 res = SSL_connect(conn->ssl);
4526 if (res != 1) {
4527 int err = SSL_get_error(conn->ssl, res);
4528 if (err == SSL_ERROR_WANT_READ)
4529 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4530 "more data");
4531 else if (err == SSL_ERROR_WANT_WRITE)
4532 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4533 "write");
4534 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004535 unsigned long error = ERR_peek_last_error();
4536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004537 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004538
4539 if (context->event_cb &&
4540 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4541 ERR_GET_REASON(error) ==
4542 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4543 context->event_cb(
4544 context->cb_ctx,
4545 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4546 NULL);
4547 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004549 if (!conn->server && !conn->client_hello_generated) {
4550 /* The server would not understand TLS Alert
4551 * before ClientHello, so simply terminate
4552 * handshake on this type of error case caused
4553 * by a likely internal error like no ciphers
4554 * available. */
4555 wpa_printf(MSG_DEBUG,
4556 "OpenSSL: Could not generate ClientHello");
4557 conn->write_alerts++;
4558 return NULL;
4559 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 }
4561 }
4562
Roshan Pius3a1667e2018-07-03 15:17:14 -07004563 if (!conn->server && !conn->failed)
4564 conn->client_hello_generated = 1;
4565
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004566#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004567 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004568 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4569 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004570 /*
4571 * This should not be reached since earlier cert_cb should have
4572 * terminated the handshake. Keep this check here for extra
4573 * protection if anything goes wrong with the more low-level
4574 * checks based on having to parse the TLS handshake messages.
4575 */
4576 wpa_printf(MSG_DEBUG,
4577 "OpenSSL: Server DH prime length: %d bits",
4578 conn->server_dh_prime_len);
4579
4580 if (context->event_cb) {
4581 union tls_event_data ev;
4582
4583 os_memset(&ev, 0, sizeof(ev));
4584 ev.alert.is_local = 1;
4585 ev.alert.type = "fatal";
4586 ev.alert.description = "insufficient security";
4587 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4588 }
4589 /*
4590 * Could send a TLS Alert to the server, but for now, simply
4591 * terminate handshake.
4592 */
4593 conn->failed++;
4594 conn->write_alerts++;
4595 return NULL;
4596 }
4597#endif /* CONFIG_SUITEB */
4598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 /* Get the TLS handshake data to be sent to the server */
4600 res = BIO_ctrl_pending(conn->ssl_out);
4601 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4602 out_data = wpabuf_alloc(res);
4603 if (out_data == NULL) {
4604 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4605 "handshake output (%d bytes)", res);
4606 if (BIO_reset(conn->ssl_out) < 0) {
4607 tls_show_errors(MSG_INFO, __func__,
4608 "BIO_reset failed");
4609 }
4610 return NULL;
4611 }
4612 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4613 res);
4614 if (res < 0) {
4615 tls_show_errors(MSG_INFO, __func__,
4616 "Handshake failed - BIO_read");
4617 if (BIO_reset(conn->ssl_out) < 0) {
4618 tls_show_errors(MSG_INFO, __func__,
4619 "BIO_reset failed");
4620 }
4621 wpabuf_free(out_data);
4622 return NULL;
4623 }
4624 wpabuf_put(out_data, res);
4625
4626 return out_data;
4627}
4628
4629
4630static struct wpabuf *
4631openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4632{
4633 struct wpabuf *appl_data;
4634 int res;
4635
4636 appl_data = wpabuf_alloc(max_len + 100);
4637 if (appl_data == NULL)
4638 return NULL;
4639
4640 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4641 wpabuf_size(appl_data));
4642 if (res < 0) {
4643 int err = SSL_get_error(conn->ssl, res);
4644 if (err == SSL_ERROR_WANT_READ ||
4645 err == SSL_ERROR_WANT_WRITE) {
4646 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4647 "included");
4648 } else {
4649 tls_show_errors(MSG_INFO, __func__,
4650 "Failed to read possible "
4651 "Application Data");
4652 }
4653 wpabuf_free(appl_data);
4654 return NULL;
4655 }
4656
4657 wpabuf_put(appl_data, res);
4658 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4659 "message", appl_data);
4660
4661 return appl_data;
4662}
4663
4664
4665static struct wpabuf *
4666openssl_connection_handshake(struct tls_connection *conn,
4667 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004668 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669{
4670 struct wpabuf *out_data;
4671
4672 if (appl_data)
4673 *appl_data = NULL;
4674
Roshan Pius3a1667e2018-07-03 15:17:14 -07004675 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 if (out_data == NULL)
4677 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004678 if (conn->invalid_hb_used) {
4679 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4680 wpabuf_free(out_data);
4681 return NULL;
4682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004684 if (SSL_is_init_finished(conn->ssl)) {
4685 wpa_printf(MSG_DEBUG,
4686 "OpenSSL: Handshake finished - resumed=%d",
4687 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004688 if (conn->server) {
4689 char *buf;
4690 size_t buflen = 2000;
4691
4692 buf = os_malloc(buflen);
4693 if (buf) {
4694 if (SSL_get_shared_ciphers(conn->ssl, buf,
4695 buflen)) {
4696 buf[buflen - 1] = '\0';
4697 wpa_printf(MSG_DEBUG,
4698 "OpenSSL: Shared ciphers: %s",
4699 buf);
4700 }
4701 os_free(buf);
4702 }
4703 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004704 if (appl_data && in_data)
4705 *appl_data = openssl_get_appl_data(conn,
4706 wpabuf_len(in_data));
4707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708
Jouni Malinen26af48b2014-04-09 13:02:53 +03004709 if (conn->invalid_hb_used) {
4710 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4711 if (appl_data) {
4712 wpabuf_free(*appl_data);
4713 *appl_data = NULL;
4714 }
4715 wpabuf_free(out_data);
4716 return NULL;
4717 }
4718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 return out_data;
4720}
4721
4722
4723struct wpabuf *
4724tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4725 const struct wpabuf *in_data,
4726 struct wpabuf **appl_data)
4727{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004728 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729}
4730
4731
4732struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4733 struct tls_connection *conn,
4734 const struct wpabuf *in_data,
4735 struct wpabuf **appl_data)
4736{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004737 conn->server = 1;
4738 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739}
4740
4741
4742struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4743 struct tls_connection *conn,
4744 const struct wpabuf *in_data)
4745{
4746 int res;
4747 struct wpabuf *buf;
4748
4749 if (conn == NULL)
4750 return NULL;
4751
4752 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4753 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4754 (res = BIO_reset(conn->ssl_out)) < 0) {
4755 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4756 return NULL;
4757 }
4758 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4759 if (res < 0) {
4760 tls_show_errors(MSG_INFO, __func__,
4761 "Encryption failed - SSL_write");
4762 return NULL;
4763 }
4764
4765 /* Read encrypted data to be sent to the server */
4766 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4767 if (buf == NULL)
4768 return NULL;
4769 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4770 if (res < 0) {
4771 tls_show_errors(MSG_INFO, __func__,
4772 "Encryption failed - BIO_read");
4773 wpabuf_free(buf);
4774 return NULL;
4775 }
4776 wpabuf_put(buf, res);
4777
4778 return buf;
4779}
4780
4781
4782struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4783 struct tls_connection *conn,
4784 const struct wpabuf *in_data)
4785{
4786 int res;
4787 struct wpabuf *buf;
4788
4789 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4790 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4791 wpabuf_len(in_data));
4792 if (res < 0) {
4793 tls_show_errors(MSG_INFO, __func__,
4794 "Decryption failed - BIO_write");
4795 return NULL;
4796 }
4797 if (BIO_reset(conn->ssl_out) < 0) {
4798 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4799 return NULL;
4800 }
4801
4802 /* Read decrypted data for further processing */
4803 /*
4804 * Even though we try to disable TLS compression, it is possible that
4805 * this cannot be done with all TLS libraries. Add extra buffer space
4806 * to handle the possibility of the decrypted data being longer than
4807 * input data.
4808 */
4809 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4810 if (buf == NULL)
4811 return NULL;
4812 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4813 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004814 int err = SSL_get_error(conn->ssl, res);
4815
4816 if (err == SSL_ERROR_WANT_READ) {
4817 wpa_printf(MSG_DEBUG,
4818 "SSL: SSL_connect - want more data");
4819 res = 0;
4820 } else {
4821 tls_show_errors(MSG_INFO, __func__,
4822 "Decryption failed - SSL_read");
4823 wpabuf_free(buf);
4824 return NULL;
4825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 }
4827 wpabuf_put(buf, res);
4828
Jouni Malinen26af48b2014-04-09 13:02:53 +03004829 if (conn->invalid_hb_used) {
4830 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4831 wpabuf_free(buf);
4832 return NULL;
4833 }
4834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 return buf;
4836}
4837
4838
4839int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4840{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004841 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842}
4843
4844
4845int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4846 u8 *ciphers)
4847{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004848 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 u8 *c;
4850 int ret;
4851
4852 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4853 return -1;
4854
4855 buf[0] = '\0';
4856 pos = buf;
4857 end = pos + sizeof(buf);
4858
4859 c = ciphers;
4860 while (*c != TLS_CIPHER_NONE) {
4861 const char *suite;
4862
4863 switch (*c) {
4864 case TLS_CIPHER_RC4_SHA:
4865 suite = "RC4-SHA";
4866 break;
4867 case TLS_CIPHER_AES128_SHA:
4868 suite = "AES128-SHA";
4869 break;
4870 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4871 suite = "DHE-RSA-AES128-SHA";
4872 break;
4873 case TLS_CIPHER_ANON_DH_AES128_SHA:
4874 suite = "ADH-AES128-SHA";
4875 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004876 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4877 suite = "DHE-RSA-AES256-SHA";
4878 break;
4879 case TLS_CIPHER_AES256_SHA:
4880 suite = "AES256-SHA";
4881 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 default:
4883 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4884 "cipher selection: %d", *c);
4885 return -1;
4886 }
4887 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004888 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 break;
4890 pos += ret;
4891
4892 c++;
4893 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004894 if (!buf[0]) {
4895 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4896 return -1;
4897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898
4899 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4900
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004901#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004902#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004903 if (os_strstr(buf, ":ADH-")) {
4904 /*
4905 * Need to drop to security level 0 to allow anonymous
4906 * cipher suites for EAP-FAST.
4907 */
4908 SSL_set_security_level(conn->ssl, 0);
4909 } else if (SSL_get_security_level(conn->ssl) == 0) {
4910 /* Force at least security level 1 */
4911 SSL_set_security_level(conn->ssl, 1);
4912 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004913#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004914#endif
4915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4917 tls_show_errors(MSG_INFO, __func__,
4918 "Cipher suite configuration failed");
4919 return -1;
4920 }
4921
4922 return 0;
4923}
4924
4925
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004926int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4927 char *buf, size_t buflen)
4928{
4929 const char *name;
4930 if (conn == NULL || conn->ssl == NULL)
4931 return -1;
4932
4933 name = SSL_get_version(conn->ssl);
4934 if (name == NULL)
4935 return -1;
4936
4937 os_strlcpy(buf, name, buflen);
4938 return 0;
4939}
4940
4941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4943 char *buf, size_t buflen)
4944{
4945 const char *name;
4946 if (conn == NULL || conn->ssl == NULL)
4947 return -1;
4948
4949 name = SSL_get_cipher(conn->ssl);
4950 if (name == NULL)
4951 return -1;
4952
4953 os_strlcpy(buf, name, buflen);
4954 return 0;
4955}
4956
4957
4958int tls_connection_enable_workaround(void *ssl_ctx,
4959 struct tls_connection *conn)
4960{
4961 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4962
4963 return 0;
4964}
4965
4966
Hai Shalom81f62d82019-07-22 12:10:00 -07004967#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968/* ClientHello TLS extensions require a patch to openssl, so this function is
4969 * commented out unless explicitly needed for EAP-FAST in order to be able to
4970 * build this file with unmodified openssl. */
4971int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4972 int ext_type, const u8 *data,
4973 size_t data_len)
4974{
4975 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4976 return -1;
4977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4979 data_len) != 1)
4980 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981
4982 return 0;
4983}
Hai Shalom81f62d82019-07-22 12:10:00 -07004984#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985
4986
4987int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4988{
4989 if (conn == NULL)
4990 return -1;
4991 return conn->failed;
4992}
4993
4994
4995int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4996{
4997 if (conn == NULL)
4998 return -1;
4999 return conn->read_alerts;
5000}
5001
5002
5003int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
5004{
5005 if (conn == NULL)
5006 return -1;
5007 return conn->write_alerts;
5008}
5009
5010
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005011#ifdef HAVE_OCSP
5012
5013static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5014{
5015#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005016 BIO *out;
5017 size_t rlen;
5018 char *txt;
5019 int res;
5020
5021 if (wpa_debug_level > MSG_DEBUG)
5022 return;
5023
5024 out = BIO_new(BIO_s_mem());
5025 if (!out)
5026 return;
5027
5028 OCSP_RESPONSE_print(out, rsp, 0);
5029 rlen = BIO_ctrl_pending(out);
5030 txt = os_malloc(rlen + 1);
5031 if (!txt) {
5032 BIO_free(out);
5033 return;
5034 }
5035
5036 res = BIO_read(out, txt, rlen);
5037 if (res > 0) {
5038 txt[res] = '\0';
5039 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5040 }
5041 os_free(txt);
5042 BIO_free(out);
5043#endif /* CONFIG_NO_STDOUT_DEBUG */
5044}
5045
5046
5047static int ocsp_resp_cb(SSL *s, void *arg)
5048{
5049 struct tls_connection *conn = arg;
5050 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005051 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005052 OCSP_RESPONSE *rsp;
5053 OCSP_BASICRESP *basic;
5054 OCSP_CERTID *id;
5055 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005056 X509_STORE *store;
5057 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005058
5059 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5060 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005061#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5062#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5063 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5064 /* TLS 1.3 sends the OCSP response with the server
5065 * Certificate message. Since that Certificate message
5066 * is not sent when resuming a session, there can be no
5067 * new OCSP response. Allow this since the OCSP response
5068 * was validated when checking the initial certificate
5069 * exchange. */
5070 wpa_printf(MSG_DEBUG,
5071 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5072 return 1;
5073 }
5074#endif
5075#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005076 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5077 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5078 }
5079
5080 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5081
5082 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5083 if (!rsp) {
5084 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5085 return 0;
5086 }
5087
5088 ocsp_debug_print_resp(rsp);
5089
5090 status = OCSP_response_status(rsp);
5091 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5092 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5093 status, OCSP_response_status_str(status));
5094 return 0;
5095 }
5096
5097 basic = OCSP_response_get1_basic(rsp);
5098 if (!basic) {
5099 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5100 return 0;
5101 }
5102
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005103 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005104 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005105 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005106
5107 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5108 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005109 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005110 }
5111 certs = sk_X509_new_null();
5112 if (certs) {
5113 X509 *cert;
5114 cert = X509_dup(conn->peer_issuer);
5115 if (cert && !sk_X509_push(certs, cert)) {
5116 tls_show_errors(
5117 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005118 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005119 X509_free(cert);
5120 sk_X509_free(certs);
5121 certs = NULL;
5122 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005123 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005124 cert = X509_dup(conn->peer_issuer_issuer);
5125 if (cert && !sk_X509_push(certs, cert)) {
5126 tls_show_errors(
5127 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005128 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005129 X509_free(cert);
5130 }
5131 }
5132 }
5133 }
5134
5135 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5136 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005137 if (status <= 0) {
5138 tls_show_errors(MSG_INFO, __func__,
5139 "OpenSSL: OCSP response failed verification");
5140 OCSP_BASICRESP_free(basic);
5141 OCSP_RESPONSE_free(rsp);
5142 return 0;
5143 }
5144
5145 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5146
Dmitry Shmidt56052862013-10-04 10:23:25 -07005147 if (!conn->peer_cert) {
5148 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5149 OCSP_BASICRESP_free(basic);
5150 OCSP_RESPONSE_free(rsp);
5151 return 0;
5152 }
5153
5154 if (!conn->peer_issuer) {
5155 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005156 OCSP_BASICRESP_free(basic);
5157 OCSP_RESPONSE_free(rsp);
5158 return 0;
5159 }
5160
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005161 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005162 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005163 wpa_printf(MSG_DEBUG,
5164 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005165 OCSP_BASICRESP_free(basic);
5166 OCSP_RESPONSE_free(rsp);
5167 return 0;
5168 }
5169
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005170 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5171 &this_update, &next_update);
5172 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005173 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005174 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5175 if (!id) {
5176 wpa_printf(MSG_DEBUG,
5177 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5178 OCSP_BASICRESP_free(basic);
5179 OCSP_RESPONSE_free(rsp);
5180 return 0;
5181 }
5182
5183 res = OCSP_resp_find_status(basic, id, &status, &reason,
5184 &produced_at, &this_update,
5185 &next_update);
5186 }
5187
5188 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005189 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5190 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5191 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005192 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005193 OCSP_BASICRESP_free(basic);
5194 OCSP_RESPONSE_free(rsp);
5195 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5196 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005197 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005198
5199 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5200 tls_show_errors(MSG_INFO, __func__,
5201 "OpenSSL: OCSP status times invalid");
5202 OCSP_BASICRESP_free(basic);
5203 OCSP_RESPONSE_free(rsp);
5204 return 0;
5205 }
5206
5207 OCSP_BASICRESP_free(basic);
5208 OCSP_RESPONSE_free(rsp);
5209
5210 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5211 OCSP_cert_status_str(status));
5212
5213 if (status == V_OCSP_CERTSTATUS_GOOD)
5214 return 1;
5215 if (status == V_OCSP_CERTSTATUS_REVOKED)
5216 return 0;
5217 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5218 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5219 return 0;
5220 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005221 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 -07005222 return 1;
5223}
5224
5225
5226static int ocsp_status_cb(SSL *s, void *arg)
5227{
5228 char *tmp;
5229 char *resp;
5230 size_t len;
5231
5232 if (tls_global->ocsp_stapling_response == NULL) {
5233 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5234 return SSL_TLSEXT_ERR_OK;
5235 }
5236
5237 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5238 if (resp == NULL) {
5239 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5240 /* TODO: Build OCSPResponse with responseStatus = internalError
5241 */
5242 return SSL_TLSEXT_ERR_OK;
5243 }
5244 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5245 tmp = OPENSSL_malloc(len);
5246 if (tmp == NULL) {
5247 os_free(resp);
5248 return SSL_TLSEXT_ERR_ALERT_FATAL;
5249 }
5250
5251 os_memcpy(tmp, resp, len);
5252 os_free(resp);
5253 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5254
5255 return SSL_TLSEXT_ERR_OK;
5256}
5257
5258#endif /* HAVE_OCSP */
5259
5260
Hai Shalomfdcde762020-04-02 11:19:20 -07005261static size_t max_str_len(const char **lines)
5262{
5263 const char **p;
5264 size_t max_len = 0;
5265
5266 for (p = lines; *p; p++) {
5267 size_t len = os_strlen(*p);
5268
5269 if (len > max_len)
5270 max_len = len;
5271 }
5272
5273 return max_len;
5274}
5275
5276
5277static int match_lines_in_file(const char *path, const char **lines)
5278{
5279 FILE *f;
5280 char *buf;
5281 size_t bufsize;
5282 int found = 0, is_linestart = 1;
5283
5284 bufsize = max_str_len(lines) + sizeof("\r\n");
5285 buf = os_malloc(bufsize);
5286 if (!buf)
5287 return 0;
5288
5289 f = fopen(path, "r");
5290 if (!f) {
5291 os_free(buf);
5292 return 0;
5293 }
5294
5295 while (!found && fgets(buf, bufsize, f)) {
5296 int is_lineend;
5297 size_t len;
5298 const char **p;
5299
5300 len = strcspn(buf, "\r\n");
5301 is_lineend = buf[len] != '\0';
5302 buf[len] = '\0';
5303
5304 if (is_linestart && is_lineend) {
5305 for (p = lines; !found && *p; p++)
5306 found = os_strcmp(buf, *p) == 0;
5307 }
5308 is_linestart = is_lineend;
5309 }
5310
5311 fclose(f);
5312 bin_clear_free(buf, bufsize);
5313
5314 return found;
5315}
5316
5317
5318static int is_tpm2_key(const char *path)
5319{
5320 /* Check both new and old format of TPM2 PEM guard tag */
5321 static const char *tpm2_tags[] = {
5322 "-----BEGIN TSS2 PRIVATE KEY-----",
5323 "-----BEGIN TSS2 KEY BLOB-----",
5324 NULL
5325 };
5326
5327 return match_lines_in_file(path, tpm2_tags);
5328}
5329
5330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005331int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5332 const struct tls_connection_params *params)
5333{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005334 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335 int ret;
5336 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005337 int can_pkcs11 = 0;
5338 const char *key_id = params->key_id;
5339 const char *cert_id = params->cert_id;
5340 const char *ca_cert_id = params->ca_cert_id;
5341 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005342 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343
5344 if (conn == NULL)
5345 return -1;
5346
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005347 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5348 wpa_printf(MSG_INFO,
5349 "OpenSSL: ocsp=3 not supported");
5350 return -1;
5351 }
5352
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005353 /*
5354 * If the engine isn't explicitly configured, and any of the
5355 * cert/key fields are actually PKCS#11 URIs, then automatically
5356 * use the PKCS#11 ENGINE.
5357 */
5358 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5359 can_pkcs11 = 1;
5360
5361 if (!key_id && params->private_key && can_pkcs11 &&
5362 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5363 can_pkcs11 = 2;
5364 key_id = params->private_key;
5365 }
5366
5367 if (!cert_id && params->client_cert && can_pkcs11 &&
5368 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5369 can_pkcs11 = 2;
5370 cert_id = params->client_cert;
5371 }
5372
5373 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5374 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5375 can_pkcs11 = 2;
5376 ca_cert_id = params->ca_cert;
5377 }
5378
5379 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5380 if (can_pkcs11 == 2 && !engine_id)
5381 engine_id = "pkcs11";
5382
Hai Shalomfdcde762020-04-02 11:19:20 -07005383 /* If private_key points to a TPM2-wrapped key, automatically enable
5384 * tpm2 engine and use it to unwrap the key. */
5385 if (params->private_key &&
5386 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5387 is_tpm2_key(params->private_key)) {
5388 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5389 params->private_key);
5390 key_id = key_id ? key_id : params->private_key;
5391 engine_id = engine_id ? engine_id : "tpm2";
5392 }
5393
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005394#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005395#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005396 if (params->flags & TLS_CONN_EAP_FAST) {
5397 wpa_printf(MSG_DEBUG,
5398 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5399 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5400 tls_show_errors(MSG_INFO, __func__,
5401 "Failed to set TLSv1_method() for EAP-FAST");
5402 return -1;
5403 }
5404 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005405#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005406#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5407#ifdef SSL_OP_NO_TLSv1_3
5408 if (params->flags & TLS_CONN_EAP_FAST) {
5409 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5410 * refuses to start the handshake with the modified ciphersuite
5411 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5412 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5413 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5414 }
5415#endif /* SSL_OP_NO_TLSv1_3 */
5416#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005417#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 while ((err = ERR_get_error())) {
5420 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5421 __func__, ERR_error_string(err, NULL));
5422 }
5423
Sunil Ravi77d572f2023-01-17 23:58:31 +00005424 if (tls_set_conn_flags(conn, params->flags,
5425 params->openssl_ciphers) < 0) {
5426 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
5427 return -1;
5428 }
5429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005430 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005431 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5432 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005433 ret = tls_engine_init(conn, engine_id, params->pin,
5434 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 if (ret)
5436 return ret;
5437 }
5438 if (tls_connection_set_subject_match(conn,
5439 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005440 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005441 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005442 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005443 params->check_cert_subject)) {
5444 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005448 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005449 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005451 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 params->ca_cert_blob,
5453 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005454 params->ca_path)) {
5455 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005459 if (engine_id && cert_id) {
5460 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5462 } else if (tls_connection_client_cert(conn, params->client_cert,
5463 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005464 params->client_cert_blob_len)) {
5465 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005469 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5471 if (tls_connection_engine_private_key(conn))
5472 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005473 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 params->private_key,
5475 params->private_key_passwd,
5476 params->private_key_blob,
5477 params->private_key_blob_len)) {
5478 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5479 params->private_key);
5480 return -1;
5481 }
5482
Roshan Pius3a1667e2018-07-03 15:17:14 -07005483 ciphers = params->openssl_ciphers;
5484#ifdef CONFIG_SUITEB
5485#ifdef OPENSSL_IS_BORINGSSL
5486 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5487 /* BoringSSL removed support for SUITEB192, so need to handle
5488 * this with hardcoded ciphersuite and additional checks for
5489 * other parameters. */
5490 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5491 }
5492#endif /* OPENSSL_IS_BORINGSSL */
5493#endif /* CONFIG_SUITEB */
5494 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005495 wpa_printf(MSG_INFO,
5496 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005497 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005498 return -1;
5499 }
5500
Hai Shalom74f70d42019-02-11 14:42:39 -08005501 if (!params->openssl_ecdh_curves) {
5502#ifndef OPENSSL_IS_BORINGSSL
5503#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005504#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005505 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5506 wpa_printf(MSG_INFO,
5507 "OpenSSL: Failed to set ECDH curves to auto");
5508 return -1;
5509 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005510#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005511#endif /* OPENSSL_NO_EC */
5512#endif /* OPENSSL_IS_BORINGSSL */
5513 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005514#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005515 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005516 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005517 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005518#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005519#ifndef OPENSSL_NO_EC
5520 if (SSL_set1_curves_list(conn->ssl,
5521 params->openssl_ecdh_curves) != 1) {
5522 wpa_printf(MSG_INFO,
5523 "OpenSSL: Failed to set ECDH curves '%s'",
5524 params->openssl_ecdh_curves);
5525 return -1;
5526 }
5527#else /* OPENSSL_NO_EC */
5528 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5529 return -1;
5530#endif /* OPENSSL_NO_EC */
5531#endif /* OPENSSL_IS_BORINGSSL */
5532 }
5533
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005534#ifdef OPENSSL_IS_BORINGSSL
5535 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5536 SSL_enable_ocsp_stapling(conn->ssl);
5537 }
5538#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005539#ifdef HAVE_OCSP
5540 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005541 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005542 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5543 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5544 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5545 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005546#else /* HAVE_OCSP */
5547 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5548 wpa_printf(MSG_INFO,
5549 "OpenSSL: No OCSP support included - reject configuration");
5550 return -1;
5551 }
5552 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5553 wpa_printf(MSG_DEBUG,
5554 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5555 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005556#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005557#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005558
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005559 conn->flags = params->flags;
5560
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005561 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562
5563 return 0;
5564}
5565
5566
Hai Shalom81f62d82019-07-22 12:10:00 -07005567static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5568{
5569 SSL *ssl;
5570 int i;
5571
5572 ssl = SSL_new(ssl_ctx);
5573 if (!ssl)
5574 return;
5575
5576 wpa_printf(MSG_DEBUG,
5577 "OpenSSL: Enabled cipher suites in priority order");
5578 for (i = 0; ; i++) {
5579 const char *cipher;
5580
5581 cipher = SSL_get_cipher_list(ssl, i);
5582 if (!cipher)
5583 break;
5584 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5585 }
5586
5587 SSL_free(ssl);
5588}
5589
5590
5591#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5592
5593static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5594{
5595 if (!pkey)
5596 return "NULL";
5597 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5598 case EVP_PKEY_RSA:
5599 return "RSA";
5600 case EVP_PKEY_DSA:
5601 return "DSA";
5602 case EVP_PKEY_DH:
5603 return "DH";
5604 case EVP_PKEY_EC:
5605 return "EC";
Sunil Ravi77d572f2023-01-17 23:58:31 +00005606 default:
5607 return "?";
Hai Shalom81f62d82019-07-22 12:10:00 -07005608 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005609}
5610
5611
5612static void openssl_debug_dump_certificate(int i, X509 *cert)
5613{
5614 char buf[256];
5615 EVP_PKEY *pkey;
5616 ASN1_INTEGER *ser;
5617 char serial_num[128];
5618
Hai Shalom60840252021-02-19 19:02:11 -08005619 if (!cert)
5620 return;
5621
Hai Shalom81f62d82019-07-22 12:10:00 -07005622 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5623
5624 ser = X509_get_serialNumber(cert);
5625 if (ser)
5626 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5627 ASN1_STRING_get0_data(ser),
5628 ASN1_STRING_length(ser));
5629 else
5630 serial_num[0] = '\0';
5631
5632 pkey = X509_get_pubkey(cert);
5633 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5634 openssl_pkey_type_str(pkey), serial_num);
5635 EVP_PKEY_free(pkey);
5636}
5637
5638
5639static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5640{
5641 STACK_OF(X509) *certs;
5642
5643 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5644 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5645 int i;
5646
5647 for (i = sk_X509_num(certs); i > 0; i--)
5648 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5649 i - 1));
5650 }
5651 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5652}
5653
5654#endif
5655
5656
5657static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5658{
5659#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5660 int res;
5661
5662 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5663 res == 1;
5664 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5665 openssl_debug_dump_certificates(ssl_ctx);
5666
5667 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5668#endif
5669}
5670
5671
5672static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5673{
5674 openssl_debug_dump_cipher_list(ssl_ctx);
5675 openssl_debug_dump_certificate_chains(ssl_ctx);
5676}
5677
5678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679int tls_global_set_params(void *tls_ctx,
5680 const struct tls_connection_params *params)
5681{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005682 struct tls_data *data = tls_ctx;
5683 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 unsigned long err;
5685
5686 while ((err = ERR_get_error())) {
5687 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5688 __func__, ERR_error_string(err, NULL));
5689 }
5690
Hai Shalom021b0b52019-04-10 11:17:58 -07005691 os_free(data->check_cert_subject);
5692 data->check_cert_subject = NULL;
5693 if (params->check_cert_subject) {
5694 data->check_cert_subject =
5695 os_strdup(params->check_cert_subject);
5696 if (!data->check_cert_subject)
5697 return -1;
5698 }
5699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005700 if (tls_global_ca_cert(data, params->ca_cert) ||
5701 tls_global_client_cert(data, params->client_cert) ||
5702 tls_global_private_key(data, params->private_key,
5703 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005704 tls_global_client_cert(data, params->client_cert2) ||
5705 tls_global_private_key(data, params->private_key2,
5706 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005707 tls_global_dh(data, params->dh_file)) {
5708 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709 return -1;
5710 }
5711
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005712 if (params->openssl_ciphers &&
5713 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5714 wpa_printf(MSG_INFO,
5715 "OpenSSL: Failed to set cipher string '%s'",
5716 params->openssl_ciphers);
5717 return -1;
5718 }
5719
Hai Shalom74f70d42019-02-11 14:42:39 -08005720 if (!params->openssl_ecdh_curves) {
5721#ifndef OPENSSL_IS_BORINGSSL
5722#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005723#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005724 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5725 wpa_printf(MSG_INFO,
5726 "OpenSSL: Failed to set ECDH curves to auto");
5727 return -1;
5728 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005729#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005730#endif /* OPENSSL_NO_EC */
5731#endif /* OPENSSL_IS_BORINGSSL */
5732 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005733#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005734 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005735 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005736 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005737#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005738#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005739#if OPENSSL_VERSION_NUMBER < 0x10100000L
5740 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5741#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005742 if (SSL_CTX_set1_curves_list(ssl_ctx,
5743 params->openssl_ecdh_curves) !=
5744 1) {
5745 wpa_printf(MSG_INFO,
5746 "OpenSSL: Failed to set ECDH curves '%s'",
5747 params->openssl_ecdh_curves);
5748 return -1;
5749 }
5750#else /* OPENSSL_NO_EC */
5751 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5752 return -1;
5753#endif /* OPENSSL_NO_EC */
5754#endif /* OPENSSL_IS_BORINGSSL */
5755 }
5756
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005757#ifdef SSL_OP_NO_TICKET
5758 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5759 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5760 else
5761 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5762#endif /* SSL_OP_NO_TICKET */
5763
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005764#ifdef HAVE_OCSP
5765 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5766 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5767 os_free(tls_global->ocsp_stapling_response);
5768 if (params->ocsp_stapling_response)
5769 tls_global->ocsp_stapling_response =
5770 os_strdup(params->ocsp_stapling_response);
5771 else
5772 tls_global->ocsp_stapling_response = NULL;
5773#endif /* HAVE_OCSP */
5774
Hai Shalom81f62d82019-07-22 12:10:00 -07005775 openssl_debug_dump_ctx(ssl_ctx);
5776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005777 return 0;
5778}
5779
5780
Hai Shalom81f62d82019-07-22 12:10:00 -07005781#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005782/* Pre-shared secred requires a patch to openssl, so this function is
5783 * commented out unless explicitly needed for EAP-FAST in order to be able to
5784 * build this file with unmodified openssl. */
5785
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005786#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005787static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5788 STACK_OF(SSL_CIPHER) *peer_ciphers,
5789 const SSL_CIPHER **cipher, void *arg)
5790#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5792 STACK_OF(SSL_CIPHER) *peer_ciphers,
5793 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005794#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795{
5796 struct tls_connection *conn = arg;
5797 int ret;
5798
Sunil Ravia04bd252022-05-02 22:54:18 -07005799#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 if (conn == NULL || conn->session_ticket_cb == NULL)
5801 return 0;
5802
5803 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5804 conn->session_ticket,
5805 conn->session_ticket_len,
5806 s->s3->client_random,
5807 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005808#else
5809 unsigned char client_random[SSL3_RANDOM_SIZE];
5810 unsigned char server_random[SSL3_RANDOM_SIZE];
5811
5812 if (conn == NULL || conn->session_ticket_cb == NULL)
5813 return 0;
5814
5815 SSL_get_client_random(s, client_random, sizeof(client_random));
5816 SSL_get_server_random(s, server_random, sizeof(server_random));
5817
5818 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5819 conn->session_ticket,
5820 conn->session_ticket_len,
5821 client_random,
5822 server_random, secret);
5823#endif
5824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 os_free(conn->session_ticket);
5826 conn->session_ticket = NULL;
5827
5828 if (ret <= 0)
5829 return 0;
5830
5831 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5832 return 1;
5833}
5834
5835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5837 int len, void *arg)
5838{
5839 struct tls_connection *conn = arg;
5840
5841 if (conn == NULL || conn->session_ticket_cb == NULL)
5842 return 0;
5843
5844 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5845
5846 os_free(conn->session_ticket);
5847 conn->session_ticket = NULL;
5848
5849 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5850 "extension", data, len);
5851
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005852 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (conn->session_ticket == NULL)
5854 return 0;
5855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 conn->session_ticket_len = len;
5857
5858 return 1;
5859}
Hai Shalom81f62d82019-07-22 12:10:00 -07005860#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861
5862
5863int tls_connection_set_session_ticket_cb(void *tls_ctx,
5864 struct tls_connection *conn,
5865 tls_session_ticket_cb cb,
5866 void *ctx)
5867{
Hai Shalom81f62d82019-07-22 12:10:00 -07005868#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869 conn->session_ticket_cb = cb;
5870 conn->session_ticket_cb_ctx = ctx;
5871
5872 if (cb) {
5873 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5874 conn) != 1)
5875 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 SSL_set_session_ticket_ext_cb(conn->ssl,
5877 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878 } else {
5879 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5880 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005881 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882 }
5883
5884 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005885#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005887#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005889
5890
5891int tls_get_library_version(char *buf, size_t buf_len)
5892{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005893#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005894 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5895 OPENSSL_VERSION_TEXT,
5896 OpenSSL_version(OPENSSL_VERSION));
5897#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005898 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5899 OPENSSL_VERSION_TEXT,
5900 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005901#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005902}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005903
5904
5905void tls_connection_set_success_data(struct tls_connection *conn,
5906 struct wpabuf *data)
5907{
5908 SSL_SESSION *sess;
5909 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005910 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005911
5912 if (tls_ex_idx_session < 0)
5913 goto fail;
5914 sess = SSL_get_session(conn->ssl);
5915 if (!sess)
5916 goto fail;
5917 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5918 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005919 struct tls_session_data *found;
5920
5921 found = get_session_data(conn->context, old);
5922 wpa_printf(MSG_DEBUG,
5923 "OpenSSL: Replacing old success data %p (sess %p)%s",
5924 old, sess, found ? "" : " (not freeing)");
5925 if (found) {
5926 dl_list_del(&found->list);
5927 os_free(found);
5928 wpabuf_free(old);
5929 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005930 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005931
5932 sess_data = os_zalloc(sizeof(*sess_data));
5933 if (!sess_data ||
5934 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005935 goto fail;
5936
Sunil Ravia04bd252022-05-02 22:54:18 -07005937 sess_data->buf = data;
5938 dl_list_add(&conn->context->sessions, &sess_data->list);
5939 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5940 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005941 conn->success_data = 1;
5942 return;
5943
5944fail:
5945 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
5946 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07005947 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005948}
5949
5950
5951void tls_connection_set_success_data_resumed(struct tls_connection *conn)
5952{
5953 wpa_printf(MSG_DEBUG,
5954 "OpenSSL: Success data accepted for resumed session");
5955 conn->success_data = 1;
5956}
5957
5958
5959const struct wpabuf *
5960tls_connection_get_success_data(struct tls_connection *conn)
5961{
5962 SSL_SESSION *sess;
5963
5964 if (tls_ex_idx_session < 0 ||
5965 !(sess = SSL_get_session(conn->ssl)))
5966 return NULL;
5967 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5968}
5969
5970
5971void tls_connection_remove_session(struct tls_connection *conn)
5972{
5973 SSL_SESSION *sess;
5974
5975 sess = SSL_get_session(conn->ssl);
5976 if (!sess)
5977 return;
5978
5979 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5980 wpa_printf(MSG_DEBUG,
5981 "OpenSSL: Session was not cached");
5982 else
5983 wpa_printf(MSG_DEBUG,
5984 "OpenSSL: Removed cached session to disable session resumption");
5985}
Hai Shalom81f62d82019-07-22 12:10:00 -07005986
5987
5988int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
5989{
5990 size_t len;
5991 int reused;
5992
5993 reused = SSL_session_reused(conn->ssl);
5994 if ((conn->server && !reused) || (!conn->server && reused))
5995 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
5996 else
5997 len = SSL_get_finished(conn->ssl, buf, max_len);
5998
5999 if (len == 0 || len > max_len)
6000 return -1;
6001
6002 return len;
6003}
6004
6005
6006u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6007{
6008 const SSL_CIPHER *cipher;
6009
6010 cipher = SSL_get_current_cipher(conn->ssl);
6011 if (!cipher)
6012 return 0;
6013#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6014 return SSL_CIPHER_get_protocol_id(cipher);
6015#else
6016 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6017#endif
6018}
Hai Shalom899fcc72020-10-19 14:38:18 -07006019
6020
6021const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6022{
6023 if (conn)
6024 return conn->peer_subject;
6025 return NULL;
6026}
6027
6028
6029bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6030{
6031 if (conn)
6032 return SSL_get_certificate(conn->ssl) != NULL;
6033 return false;
6034}
Gabriel Birena5bdf372022-12-15 20:54:33 +00006035
6036void tls_register_cert_callback(tls_get_certificate_cb cb)
6037{
6038 certificate_callback_global = cb;
6039}