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