blob: 23bbe6879c811ec6234828f7e0616cb418e86ae5 [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
Gabriel Birencd0cb1c2023-04-17 18:16:23 +0000146struct tls_data {
147 SSL_CTX *ssl;
148 unsigned int tls_session_lifetime;
149 int check_crl;
150 int check_crl_strict;
151 char *ca_cert;
152 unsigned int crl_reload_interval;
153 struct os_reltime crl_last_reload;
154 char *check_cert_subject;
155};
156
157struct tls_connection {
158 struct tls_context *context;
159 struct tls_data *data;
160 SSL_CTX *ssl_ctx;
161 SSL *ssl;
162 BIO *ssl_in, *ssl_out;
163#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
164 ENGINE *engine; /* functional reference to the engine */
165 EVP_PKEY *private_key; /* the private key if using engine */
166#endif /* OPENSSL_NO_ENGINE */
167 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
168 char *check_cert_subject;
169 int read_alerts, write_alerts, failed;
170
171 tls_session_ticket_cb session_ticket_cb;
172 void *session_ticket_cb_ctx;
173
174 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
175 u8 *session_ticket;
176 size_t session_ticket_len;
177
178 unsigned int ca_cert_verify:1;
179 unsigned int cert_probe:1;
180 unsigned int server_cert_only:1;
181 unsigned int invalid_hb_used:1;
182 unsigned int success_data:1;
183 unsigned int client_hello_generated:1;
184 unsigned int server:1;
185
186 u8 srv_cert_hash[32];
187
188 unsigned int flags;
189
190 X509 *peer_cert;
191 X509 *peer_issuer;
192 X509 *peer_issuer_issuer;
193 char *peer_subject; /* peer subject info for authenticated peer */
194
195 unsigned char client_random[SSL3_RANDOM_SIZE];
196 unsigned char server_random[SSL3_RANDOM_SIZE];
197
198 u16 cipher_suite;
199 int server_dh_prime_len;
200};
201
Gabriel Birena5bdf372022-12-15 20:54:33 +0000202static struct tls_context *tls_global = NULL;
203static tls_get_certificate_cb certificate_callback_global = NULL;
204
Dmitry Shmidtff079172013-11-08 14:10:30 -0800205#ifdef ANDROID
206#include <openssl/pem.h>
Dmitry Shmidtff079172013-11-08 14:10:30 -0800207
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000208#include <log/log.h>
209#include <log/log_event_list.h>
210
211#define CERT_VALIDATION_FAILURE 210033
Hai Shalom7ad2a872021-08-02 18:56:55 -0700212#define ANDROID_KEYSTORE_PREFIX "keystore://"
213#define ANDROID_KEYSTORE_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_PREFIX)
214#define ANDROID_KEYSTORE_ENCODED_PREFIX "keystores://"
215#define ANDROID_KEYSTORE_ENCODED_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_ENCODED_PREFIX)
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000216
217static void log_cert_validation_failure(const char *reason)
218{
219 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
220 android_log_write_string8(ctx, reason);
221 android_log_write_list(ctx, LOG_ID_SECURITY);
222 android_log_destroy(&ctx);
223}
224
225
Gabriel Birenff4f8382023-04-06 20:14:39 +0000226static BIO* BIO_from_keystore(const char *alias, struct tls_connection *conn)
Dmitry Shmidtff079172013-11-08 14:10:30 -0800227{
228 BIO *bio = NULL;
229 uint8_t *value = NULL;
Gabriel Birenff4f8382023-04-06 20:14:39 +0000230
231 void *cb_ctx = NULL;
232 if (conn != NULL && conn->context != NULL) {
233 cb_ctx = conn->context->cb_ctx;
234 }
235
236 if (cb_ctx != NULL && certificate_callback_global != NULL) {
Gabriel Biren980c48a2023-03-27 21:49:21 +0000237 wpa_printf(MSG_INFO, "Retrieving certificate using callback");
Gabriel Birenff4f8382023-04-06 20:14:39 +0000238 int length = (*certificate_callback_global)(cb_ctx, alias, &value);
Gabriel Birena5bdf372022-12-15 20:54:33 +0000239 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
240 BIO_write(bio, value, length);
Gabriel Birenff4f8382023-04-06 20:14:39 +0000241 free(value);
Gabriel Birena5bdf372022-12-15 20:54:33 +0000242 }
Dmitry Shmidtff079172013-11-08 14:10:30 -0800243 return bio;
244}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800245
Gabriel Birenff4f8382023-04-06 20:14:39 +0000246static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias, struct tls_connection *conn)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800247{
Gabriel Birenff4f8382023-04-06 20:14:39 +0000248 BIO *bio = BIO_from_keystore(alias, conn);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800249 STACK_OF(X509_INFO) *stack = NULL;
250 stack_index_t i;
Hai Shalom22171592021-04-02 16:05:23 -0700251 int ret = 0;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800252
Hai Shalom22171592021-04-02 16:05:23 -0700253 if (!bio) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700254 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
255 alias);
Hai Shalom22171592021-04-02 16:05:23 -0700256 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800257 }
258
Hai Shalom7ad2a872021-08-02 18:56:55 -0700259 // Keystore returns X.509 certificates in PEM encoding
Hai Shalom22171592021-04-02 16:05:23 -0700260 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
261 BIO_free(bio);
262
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800263 if (!stack) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700264 wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s",
265 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800266 return -1;
267 }
268
269 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
270 X509_INFO *info = sk_X509_INFO_value(stack, i);
271
272 if (info->x509)
Hai Shalom22171592021-04-02 16:05:23 -0700273 if (!X509_STORE_add_cert(ctx, info->x509)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700274 wpa_printf(MSG_ERROR,
275 "OpenSSL: Failed to add Root CA certificate");
Hai Shalom22171592021-04-02 16:05:23 -0700276 ret = -1;
277 break;
278 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800279 if (info->crl)
280 X509_STORE_add_crl(ctx, info->crl);
281 }
282
283 sk_X509_INFO_pop_free(stack, X509_INFO_free);
Hai Shalom22171592021-04-02 16:05:23 -0700284 return ret;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800285}
286
287
288static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
Gabriel Birenff4f8382023-04-06 20:14:39 +0000289 const char *encoded_alias,
290 struct tls_connection *conn)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800291{
292 int rc = -1;
Hai Shalom7ad2a872021-08-02 18:56:55 -0700293 int len = os_strlen(encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800294 unsigned char *decoded_alias;
295
296 if (len & 1) {
297 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
Hai Shalom7ad2a872021-08-02 18:56:55 -0700298 encoded_alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800299 return rc;
300 }
301
302 decoded_alias = os_malloc(len / 2 + 1);
303 if (decoded_alias) {
Hai Shalom7ad2a872021-08-02 18:56:55 -0700304 if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800305 decoded_alias[len / 2] = '\0';
306 rc = tls_add_ca_from_keystore(
Gabriel Birenff4f8382023-04-06 20:14:39 +0000307 ctx, (const char *) decoded_alias, conn);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800308 }
309 os_free(decoded_alias);
310 }
311
312 return rc;
313}
314
Dmitry Shmidtff079172013-11-08 14:10:30 -0800315#endif /* ANDROID */
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700318static struct tls_context * tls_context_new(const struct tls_config *conf)
319{
320 struct tls_context *context = os_zalloc(sizeof(*context));
321 if (context == NULL)
322 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -0700323 dl_list_init(&context->sessions);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700324 if (conf) {
325 context->event_cb = conf->event_cb;
326 context->cb_ctx = conf->cb_ctx;
327 context->cert_in_cb = conf->cert_in_cb;
328 }
329 return context;
330}
331
332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333#ifdef CONFIG_NO_STDOUT_DEBUG
334
335static void _tls_show_errors(void)
336{
337 unsigned long err;
338
339 while ((err = ERR_get_error())) {
340 /* Just ignore the errors, since stdout is disabled */
341 }
342}
343#define tls_show_errors(l, f, t) _tls_show_errors()
344
345#else /* CONFIG_NO_STDOUT_DEBUG */
346
347static void tls_show_errors(int level, const char *func, const char *txt)
348{
349 unsigned long err;
350
351 wpa_printf(level, "OpenSSL: %s - %s %s",
352 func, txt, ERR_error_string(ERR_get_error(), NULL));
353
354 while ((err = ERR_get_error())) {
355 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
356 ERR_error_string(err, NULL));
357 }
358}
359
360#endif /* CONFIG_NO_STDOUT_DEBUG */
361
362
Hai Shalom74f70d42019-02-11 14:42:39 -0800363static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
364{
365 int flags;
366 X509_STORE *store;
367
368 store = X509_STORE_new();
369 if (!store) {
370 wpa_printf(MSG_DEBUG,
371 "OpenSSL: %s - failed to allocate new certificate store",
372 __func__);
373 return NULL;
374 }
375
376 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
377 tls_show_errors(MSG_WARNING, __func__,
378 "Failed to load root certificates");
379 X509_STORE_free(store);
380 return NULL;
381 }
382
383 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
384 if (check_crl == 2)
385 flags |= X509_V_FLAG_CRL_CHECK_ALL;
386
387 X509_STORE_set_flags(store, flags);
388
389 return store;
390}
391
392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393#ifdef CONFIG_NATIVE_WINDOWS
394
395/* Windows CryptoAPI and access to certificate stores */
396#include <wincrypt.h>
397
398#ifdef __MINGW32_VERSION
399/*
400 * MinGW does not yet include all the needed definitions for CryptoAPI, so
401 * define here whatever extra is needed.
402 */
403#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
404#define CERT_STORE_READONLY_FLAG 0x00008000
405#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
406
407#endif /* __MINGW32_VERSION */
408
409
410struct cryptoapi_rsa_data {
411 const CERT_CONTEXT *cert;
412 HCRYPTPROV crypt_prov;
413 DWORD key_spec;
414 BOOL free_crypt_prov;
415};
416
417
418static void cryptoapi_error(const char *msg)
419{
420 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
421 msg, (unsigned int) GetLastError());
422}
423
424
425static int cryptoapi_rsa_pub_enc(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_pub_dec(int flen, const unsigned char *from,
434 unsigned char *to, RSA *rsa, int padding)
435{
436 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
437 return 0;
438}
439
440
441static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
442 unsigned char *to, RSA *rsa, int padding)
443{
444 struct cryptoapi_rsa_data *priv =
445 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
446 HCRYPTHASH hash;
447 DWORD hash_size, len, i;
448 unsigned char *buf = NULL;
449 int ret = 0;
450
451 if (priv == NULL) {
452 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
453 ERR_R_PASSED_NULL_PARAMETER);
454 return 0;
455 }
456
457 if (padding != RSA_PKCS1_PADDING) {
458 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
459 RSA_R_UNKNOWN_PADDING_TYPE);
460 return 0;
461 }
462
463 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
464 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
465 __func__);
466 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
467 RSA_R_INVALID_MESSAGE_LENGTH);
468 return 0;
469 }
470
471 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
472 {
473 cryptoapi_error("CryptCreateHash failed");
474 return 0;
475 }
476
477 len = sizeof(hash_size);
478 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
479 0)) {
480 cryptoapi_error("CryptGetHashParam failed");
481 goto err;
482 }
483
484 if ((int) hash_size != flen) {
485 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
486 (unsigned) hash_size, flen);
487 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
488 RSA_R_INVALID_MESSAGE_LENGTH);
489 goto err;
490 }
491 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
492 cryptoapi_error("CryptSetHashParam failed");
493 goto err;
494 }
495
496 len = RSA_size(rsa);
497 buf = os_malloc(len);
498 if (buf == NULL) {
499 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
500 goto err;
501 }
502
503 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
504 cryptoapi_error("CryptSignHash failed");
505 goto err;
506 }
507
508 for (i = 0; i < len; i++)
509 to[i] = buf[len - i - 1];
510 ret = len;
511
512err:
513 os_free(buf);
514 CryptDestroyHash(hash);
515
516 return ret;
517}
518
519
520static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
521 unsigned char *to, RSA *rsa, int padding)
522{
523 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
524 return 0;
525}
526
527
528static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
529{
530 if (priv == NULL)
531 return;
532 if (priv->crypt_prov && priv->free_crypt_prov)
533 CryptReleaseContext(priv->crypt_prov, 0);
534 if (priv->cert)
535 CertFreeCertificateContext(priv->cert);
536 os_free(priv);
537}
538
539
540static int cryptoapi_finish(RSA *rsa)
541{
542 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
543 os_free((void *) rsa->meth);
544 rsa->meth = NULL;
545 return 1;
546}
547
548
549static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
550{
551 HCERTSTORE cs;
552 const CERT_CONTEXT *ret = NULL;
553
554 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
555 store | CERT_STORE_OPEN_EXISTING_FLAG |
556 CERT_STORE_READONLY_FLAG, L"MY");
557 if (cs == NULL) {
558 cryptoapi_error("Failed to open 'My system store'");
559 return NULL;
560 }
561
562 if (strncmp(name, "cert://", 7) == 0) {
563 unsigned short wbuf[255];
564 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
565 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
566 PKCS_7_ASN_ENCODING,
567 0, CERT_FIND_SUBJECT_STR,
568 wbuf, NULL);
569 } else if (strncmp(name, "hash://", 7) == 0) {
570 CRYPT_HASH_BLOB blob;
571 int len;
572 const char *hash = name + 7;
573 unsigned char *buf;
574
575 len = os_strlen(hash) / 2;
576 buf = os_malloc(len);
577 if (buf && hexstr2bin(hash, buf, len) == 0) {
578 blob.cbData = len;
579 blob.pbData = buf;
580 ret = CertFindCertificateInStore(cs,
581 X509_ASN_ENCODING |
582 PKCS_7_ASN_ENCODING,
583 0, CERT_FIND_HASH,
584 &blob, NULL);
585 }
586 os_free(buf);
587 }
588
589 CertCloseStore(cs, 0);
590
591 return ret;
592}
593
594
595static int tls_cryptoapi_cert(SSL *ssl, const char *name)
596{
597 X509 *cert = NULL;
598 RSA *rsa = NULL, *pub_rsa;
599 struct cryptoapi_rsa_data *priv;
600 RSA_METHOD *rsa_meth;
601
602 if (name == NULL ||
603 (strncmp(name, "cert://", 7) != 0 &&
604 strncmp(name, "hash://", 7) != 0))
605 return -1;
606
607 priv = os_zalloc(sizeof(*priv));
608 rsa_meth = os_zalloc(sizeof(*rsa_meth));
609 if (priv == NULL || rsa_meth == NULL) {
610 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
611 "for CryptoAPI RSA method");
612 os_free(priv);
613 os_free(rsa_meth);
614 return -1;
615 }
616
617 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
618 if (priv->cert == NULL) {
619 priv->cert = cryptoapi_find_cert(
620 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
621 }
622 if (priv->cert == NULL) {
623 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
624 "'%s'", name);
625 goto err;
626 }
627
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800628 cert = d2i_X509(NULL,
629 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 priv->cert->cbCertEncoded);
631 if (cert == NULL) {
632 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
633 "encoding");
634 goto err;
635 }
636
637 if (!CryptAcquireCertificatePrivateKey(priv->cert,
638 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
639 NULL, &priv->crypt_prov,
640 &priv->key_spec,
641 &priv->free_crypt_prov)) {
642 cryptoapi_error("Failed to acquire a private key for the "
643 "certificate");
644 goto err;
645 }
646
647 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
648 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
649 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
650 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
651 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
652 rsa_meth->finish = cryptoapi_finish;
653 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
654 rsa_meth->app_data = (char *) priv;
655
656 rsa = RSA_new();
657 if (rsa == NULL) {
658 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
659 ERR_R_MALLOC_FAILURE);
660 goto err;
661 }
662
663 if (!SSL_use_certificate(ssl, cert)) {
664 RSA_free(rsa);
665 rsa = NULL;
666 goto err;
667 }
668 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
669 X509_free(cert);
670 cert = NULL;
671
672 rsa->n = BN_dup(pub_rsa->n);
673 rsa->e = BN_dup(pub_rsa->e);
674 if (!RSA_set_method(rsa, rsa_meth))
675 goto err;
676
677 if (!SSL_use_RSAPrivateKey(ssl, rsa))
678 goto err;
679 RSA_free(rsa);
680
681 return 0;
682
683err:
684 if (cert)
685 X509_free(cert);
686 if (rsa)
687 RSA_free(rsa);
688 else {
689 os_free(rsa_meth);
690 cryptoapi_free_data(priv);
691 }
692 return -1;
693}
694
695
696static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
697{
698 HCERTSTORE cs;
699 PCCERT_CONTEXT ctx = NULL;
700 X509 *cert;
701 char buf[128];
702 const char *store;
703#ifdef UNICODE
704 WCHAR *wstore;
705#endif /* UNICODE */
706
707 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
708 return -1;
709
710 store = name + 13;
711#ifdef UNICODE
712 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
713 if (wstore == NULL)
714 return -1;
715 wsprintf(wstore, L"%S", store);
716 cs = CertOpenSystemStore(0, wstore);
717 os_free(wstore);
718#else /* UNICODE */
719 cs = CertOpenSystemStore(0, store);
720#endif /* UNICODE */
721 if (cs == NULL) {
722 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
723 "'%s': error=%d", __func__, store,
724 (int) GetLastError());
725 return -1;
726 }
727
728 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800729 cert = d2i_X509(NULL,
730 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 ctx->cbCertEncoded);
732 if (cert == NULL) {
733 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
734 "X509 DER encoding for CA cert");
735 continue;
736 }
737
738 X509_NAME_oneline(X509_get_subject_name(cert), buf,
739 sizeof(buf));
740 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
741 "system certificate store: subject='%s'", buf);
742
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700743 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
744 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745 tls_show_errors(MSG_WARNING, __func__,
746 "Failed to add ca_cert to OpenSSL "
747 "certificate store");
748 }
749
750 X509_free(cert);
751 }
752
753 if (!CertCloseStore(cs, 0)) {
754 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
755 "'%s': error=%d", __func__, name + 13,
756 (int) GetLastError());
757 }
758
759 return 0;
760}
761
762
763#else /* CONFIG_NATIVE_WINDOWS */
764
765static int tls_cryptoapi_cert(SSL *ssl, const char *name)
766{
767 return -1;
768}
769
770#endif /* CONFIG_NATIVE_WINDOWS */
771
772
773static void ssl_info_cb(const SSL *ssl, int where, int ret)
774{
775 const char *str;
776 int w;
777
778 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
779 w = where & ~SSL_ST_MASK;
780 if (w & SSL_ST_CONNECT)
781 str = "SSL_connect";
782 else if (w & SSL_ST_ACCEPT)
783 str = "SSL_accept";
784 else
785 str = "undefined";
786
787 if (where & SSL_CB_LOOP) {
788 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
789 str, SSL_state_string_long(ssl));
790 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700791 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
793 where & SSL_CB_READ ?
794 "read (remote end reported an error)" :
795 "write (local SSL3 detected an error)",
796 SSL_alert_type_string_long(ret),
797 SSL_alert_desc_string_long(ret));
798 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 if (where & SSL_CB_READ)
800 conn->read_alerts++;
801 else
802 conn->write_alerts++;
803 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700804 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700806 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700807 os_memset(&ev, 0, sizeof(ev));
808 ev.alert.is_local = !(where & SSL_CB_READ);
809 ev.alert.type = SSL_alert_type_string_long(ret);
810 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700811 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 } else if (where & SSL_CB_EXIT && ret <= 0) {
814 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
815 str, ret == 0 ? "failed" : "error",
816 SSL_state_string_long(ssl));
817 }
818}
819
820
821#ifndef OPENSSL_NO_ENGINE
822/**
823 * tls_engine_load_dynamic_generic - load any openssl engine
824 * @pre: an array of commands and values that load an engine initialized
825 * in the engine specific function
826 * @post: an array of commands and values that initialize an already loaded
827 * engine (or %NULL if not required)
828 * @id: the engine id of the engine to load (only required if post is not %NULL
829 *
830 * This function is a generic function that loads any openssl engine.
831 *
832 * Returns: 0 on success, -1 on failure
833 */
834static int tls_engine_load_dynamic_generic(const char *pre[],
835 const char *post[], const char *id)
836{
837 ENGINE *engine;
838 const char *dynamic_id = "dynamic";
839
840 engine = ENGINE_by_id(id);
841 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
843 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700844 /*
845 * If it was auto-loaded by ENGINE_by_id() we might still
846 * need to tell it which PKCS#11 module to use in legacy
847 * (non-p11-kit) environments. Do so now; even if it was
848 * properly initialised before, setting it again will be
849 * harmless.
850 */
851 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 }
853 ERR_clear_error();
854
855 engine = ENGINE_by_id(dynamic_id);
856 if (engine == NULL) {
857 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
858 dynamic_id,
859 ERR_error_string(ERR_get_error(), NULL));
860 return -1;
861 }
862
863 /* Perform the pre commands. This will load the engine. */
864 while (pre && pre[0]) {
865 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
866 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
867 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
868 "%s %s [%s]", pre[0], pre[1],
869 ERR_error_string(ERR_get_error(), NULL));
870 ENGINE_free(engine);
871 return -1;
872 }
873 pre += 2;
874 }
875
876 /*
877 * Free the reference to the "dynamic" engine. The loaded engine can
878 * now be looked up using ENGINE_by_id().
879 */
880 ENGINE_free(engine);
881
882 engine = ENGINE_by_id(id);
883 if (engine == NULL) {
884 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
885 id, ERR_error_string(ERR_get_error(), NULL));
886 return -1;
887 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700888 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 while (post && post[0]) {
890 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
891 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
892 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
893 " %s %s [%s]", post[0], post[1],
894 ERR_error_string(ERR_get_error(), NULL));
895 ENGINE_remove(engine);
896 ENGINE_free(engine);
897 return -1;
898 }
899 post += 2;
900 }
901 ENGINE_free(engine);
902
903 return 0;
904}
905
906
907/**
908 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
909 * @pkcs11_so_path: pksc11_so_path from the configuration
910 * @pcks11_module_path: pkcs11_module_path from the configuration
911 */
912static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
913 const char *pkcs11_module_path)
914{
915 char *engine_id = "pkcs11";
916 const char *pre_cmd[] = {
917 "SO_PATH", NULL /* pkcs11_so_path */,
918 "ID", NULL /* engine_id */,
919 "LIST_ADD", "1",
920 /* "NO_VCHECK", "1", */
921 "LOAD", NULL,
922 NULL, NULL
923 };
924 const char *post_cmd[] = {
925 "MODULE_PATH", NULL /* pkcs11_module_path */,
926 NULL, NULL
927 };
928
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800929 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 return 0;
931
932 pre_cmd[1] = pkcs11_so_path;
933 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800934 if (pkcs11_module_path)
935 post_cmd[1] = pkcs11_module_path;
936 else
937 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938
939 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
940 pkcs11_so_path);
941
942 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
943}
944
945
946/**
947 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
948 * @opensc_so_path: opensc_so_path from the configuration
949 */
950static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
951{
952 char *engine_id = "opensc";
953 const char *pre_cmd[] = {
954 "SO_PATH", NULL /* opensc_so_path */,
955 "ID", NULL /* engine_id */,
956 "LIST_ADD", "1",
957 "LOAD", NULL,
958 NULL, NULL
959 };
960
961 if (!opensc_so_path)
962 return 0;
963
964 pre_cmd[1] = opensc_so_path;
965 pre_cmd[3] = engine_id;
966
967 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
968 opensc_so_path);
969
970 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
971}
972#endif /* OPENSSL_NO_ENGINE */
973
974
Sunil Ravia04bd252022-05-02 22:54:18 -0700975static struct tls_session_data * get_session_data(struct tls_context *context,
976 const struct wpabuf *buf)
977{
978 struct tls_session_data *data;
979
980 dl_list_for_each(data, &context->sessions, struct tls_session_data,
981 list) {
982 if (data->buf == buf)
983 return data;
984 }
985
986 return NULL;
987}
988
989
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800990static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
991{
992 struct wpabuf *buf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700993 struct tls_context *context;
994 struct tls_session_data *found;
995
996 wpa_printf(MSG_DEBUG,
997 "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess,
998 tls_ex_idx_session);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800999
1000 if (tls_ex_idx_session < 0)
1001 return;
1002 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
1003 if (!buf)
1004 return;
Sunil Ravia04bd252022-05-02 22:54:18 -07001005
1006 context = SSL_CTX_get_app_data(ctx);
1007 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
1008 found = get_session_data(context, buf);
1009 if (!found) {
1010 wpa_printf(MSG_DEBUG,
1011 "OpenSSL: Do not free application session data %p (sess %p)",
1012 buf, sess);
1013 return;
1014 }
1015
1016 dl_list_del(&found->list);
1017 os_free(found);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 wpa_printf(MSG_DEBUG,
1019 "OpenSSL: Free application session data %p (sess %p)",
1020 buf, sess);
1021 wpabuf_free(buf);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022}
1023
1024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025void * tls_init(const struct tls_config *conf)
1026{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001027 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001029 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001030 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031
1032 if (tls_openssl_ref_count == 0) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001033 void openssl_load_legacy_provider(void);
1034
1035 openssl_load_legacy_provider();
1036
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001037 tls_global = context = tls_context_new(conf);
1038 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040#ifdef CONFIG_FIPS
1041#ifdef OPENSSL_FIPS
1042 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001043 static int fips_enabled = 0;
1044
1045 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
1047 "mode");
1048 ERR_load_crypto_strings();
1049 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001050 os_free(tls_global);
1051 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001053 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001055 fips_enabled = 1;
1056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 }
1058#else /* OPENSSL_FIPS */
1059 if (conf && conf->fips_mode) {
1060 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
1061 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001062 os_free(tls_global);
1063 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 return NULL;
1065 }
1066#endif /* OPENSSL_FIPS */
1067#endif /* CONFIG_FIPS */
Sunil Ravia04bd252022-05-02 22:54:18 -07001068#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 SSL_load_error_strings();
1070 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001071#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 EVP_add_digest(EVP_sha256());
1073#endif /* OPENSSL_NO_SHA256 */
1074 /* TODO: if /dev/urandom is available, PRNG is seeded
1075 * automatically. If this is not the case, random data should
1076 * be added here. */
1077
1078#ifdef PKCS12_FUNCS
1079#ifndef OPENSSL_NO_RC2
1080 /*
1081 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1082 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1083 * versions, but it looks like OpenSSL 1.0.0 does not do that
1084 * anymore.
1085 */
1086 EVP_add_cipher(EVP_rc2_40_cbc());
1087#endif /* OPENSSL_NO_RC2 */
1088 PKCS12_PBE_add();
1089#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001090#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001091 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001092 context = tls_context_new(conf);
1093 if (context == NULL)
1094 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 }
1096 tls_openssl_ref_count++;
1097
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001098 data = os_zalloc(sizeof(*data));
1099 if (data)
1100 ssl = SSL_CTX_new(SSLv23_method());
1101 else
1102 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001103 if (ssl == NULL) {
1104 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001105 if (context != tls_global)
1106 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001107 if (tls_openssl_ref_count == 0) {
1108 os_free(tls_global);
1109 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001110 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001111 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001113 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001114 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001115 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001116 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001117 data->crl_reload_interval = conf->crl_reload_interval;
1118 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001120 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1121 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1122
Hai Shalom60840252021-02-19 19:02:11 -08001123 SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY);
1124
Dmitry Shmidt29333592017-01-09 12:27:11 -08001125#ifdef SSL_MODE_NO_AUTO_CHAIN
1126 /* Number of deployed use cases assume the default OpenSSL behavior of
1127 * auto chaining the local certificate is in use. BoringSSL removed this
1128 * functionality by default, so we need to restore it here to avoid
1129 * breaking existing use cases. */
1130 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1131#endif /* SSL_MODE_NO_AUTO_CHAIN */
1132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001134 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001135 if (data->tls_session_lifetime > 0) {
1136 SSL_CTX_set_quiet_shutdown(ssl, 1);
1137 /*
1138 * Set default context here. In practice, this will be replaced
1139 * by the per-EAP method context in tls_connection_set_verify().
1140 */
1141 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1142 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1143 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1144 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
Sunil Ravia04bd252022-05-02 22:54:18 -07001145#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1146 !defined(LIBRESSL_VERSION_NUMBER) && \
1147 !defined(OPENSSL_IS_BORINGSSL)
1148 /* One session ticket is sufficient for EAP-TLS */
1149 SSL_CTX_set_num_tickets(ssl, 1);
1150#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001151 } else {
1152 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
Sunil Ravia04bd252022-05-02 22:54:18 -07001153#if OPENSSL_VERSION_NUMBER >= 0x10101000L && \
1154 !defined(LIBRESSL_VERSION_NUMBER) && \
1155 !defined(OPENSSL_IS_BORINGSSL)
1156 SSL_CTX_set_num_tickets(ssl, 0);
1157#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001158 }
1159
1160 if (tls_ex_idx_session < 0) {
1161 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1162 0, NULL, NULL, NULL, NULL);
1163 if (tls_ex_idx_session < 0) {
1164 tls_deinit(data);
1165 return NULL;
1166 }
1167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168
1169#ifndef OPENSSL_NO_ENGINE
Hai Shalom81f62d82019-07-22 12:10:00 -07001170 wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines");
1171 ENGINE_load_builtin_engines();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 if (conf &&
1174 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1175 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1177 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1178 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001179 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 return NULL;
1181 }
1182 }
1183#endif /* OPENSSL_NO_ENGINE */
1184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001185 if (conf && conf->openssl_ciphers)
1186 ciphers = conf->openssl_ciphers;
1187 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001188 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001189 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1190 wpa_printf(MSG_ERROR,
1191 "OpenSSL: Failed to set cipher string '%s'",
1192 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001193 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001194 return NULL;
1195 }
1196
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001197 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198}
1199
1200
1201void tls_deinit(void *ssl_ctx)
1202{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001203 struct tls_data *data = ssl_ctx;
1204 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001205 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Sunil Ravia04bd252022-05-02 22:54:18 -07001206 struct tls_session_data *sess_data;
1207
1208 if (data->tls_session_lifetime > 0) {
1209 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions");
1210 SSL_CTX_flush_sessions(ssl, 0);
1211 wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done");
1212 }
1213 while ((sess_data = dl_list_first(&context->sessions,
1214 struct tls_session_data, list))) {
1215 wpa_printf(MSG_DEBUG,
1216 "OpenSSL: Freeing not-flushed session data %p",
1217 sess_data->buf);
1218 wpabuf_free(sess_data->buf);
1219 dl_list_del(&sess_data->list);
1220 os_free(sess_data);
1221 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001222 if (context != tls_global)
1223 os_free(context);
Hai Shalom74f70d42019-02-11 14:42:39 -08001224 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 SSL_CTX_free(ssl);
1226
1227 tls_openssl_ref_count--;
1228 if (tls_openssl_ref_count == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001229#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230#ifndef OPENSSL_NO_ENGINE
1231 ENGINE_cleanup();
1232#endif /* OPENSSL_NO_ENGINE */
1233 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001234 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 ERR_free_strings();
1236 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001237#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001238 os_free(tls_global->ocsp_stapling_response);
1239 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 os_free(tls_global);
1241 tls_global = NULL;
1242 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001243
Hai Shalom021b0b52019-04-10 11:17:58 -07001244 os_free(data->check_cert_subject);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001245 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246}
1247
1248
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001249#ifndef OPENSSL_NO_ENGINE
1250
1251/* Cryptoki return values */
1252#define CKR_PIN_INCORRECT 0x000000a0
1253#define CKR_PIN_INVALID 0x000000a1
1254#define CKR_PIN_LEN_RANGE 0x000000a2
1255
1256/* libp11 */
1257#define ERR_LIB_PKCS11 ERR_LIB_USER
1258
1259static int tls_is_pin_error(unsigned int err)
1260{
1261 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1262 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1263 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1264 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1265}
1266
1267#endif /* OPENSSL_NO_ENGINE */
1268
1269
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001270#ifdef ANDROID
1271/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1272EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1273#endif /* ANDROID */
1274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1276 const char *pin, const char *key_id,
1277 const char *cert_id, const char *ca_cert_id)
1278{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001279#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1280#if !defined(OPENSSL_NO_ENGINE)
1281#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1282#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001283 if (!key_id)
1284 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001285 conn->engine = NULL;
1286 conn->private_key = EVP_PKEY_from_keystore(key_id);
1287 if (!conn->private_key) {
1288 wpa_printf(MSG_ERROR,
1289 "ENGINE: cannot load private key with id '%s' [%s]",
1290 key_id,
1291 ERR_error_string(ERR_get_error(), NULL));
1292 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1293 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001294#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296#ifndef OPENSSL_NO_ENGINE
1297 int ret = -1;
1298 if (engine_id == NULL) {
1299 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1300 return -1;
1301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302
1303 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001304#ifdef ANDROID
1305 ENGINE_load_dynamic();
1306#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 conn->engine = ENGINE_by_id(engine_id);
1308 if (!conn->engine) {
1309 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1310 engine_id, ERR_error_string(ERR_get_error(), NULL));
1311 goto err;
1312 }
1313 if (ENGINE_init(conn->engine) != 1) {
1314 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1315 "(engine: %s) [%s]", engine_id,
1316 ERR_error_string(ERR_get_error(), NULL));
1317 goto err;
1318 }
1319 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1320
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001321#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001322 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1324 ERR_error_string(ERR_get_error(), NULL));
1325 goto err;
1326 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001327#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001328 if (key_id) {
1329 /*
1330 * Ensure that the ENGINE does not attempt to use the OpenSSL
1331 * UI system to obtain a PIN, if we didn't provide one.
1332 */
1333 struct {
1334 const void *password;
1335 const char *prompt_info;
1336 } key_cb = { "", NULL };
1337
1338 /* load private key first in-case PIN is required for cert */
1339 conn->private_key = ENGINE_load_private_key(conn->engine,
1340 key_id, NULL,
1341 &key_cb);
1342 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001343 unsigned long err = ERR_get_error();
1344
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001345 wpa_printf(MSG_ERROR,
1346 "ENGINE: cannot load private key with id '%s' [%s]",
1347 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001348 ERR_error_string(err, NULL));
1349 if (tls_is_pin_error(err))
1350 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1351 else
1352 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001353 goto err;
1354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356
1357 /* handle a certificate and/or CA certificate */
1358 if (cert_id || ca_cert_id) {
1359 const char *cmd_name = "LOAD_CERT_CTRL";
1360
1361 /* test if the engine supports a LOAD_CERT_CTRL */
1362 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1363 0, (void *)cmd_name, NULL)) {
1364 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1365 " loading certificates");
1366 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1367 goto err;
1368 }
1369 }
1370
1371 return 0;
1372
1373err:
1374 if (conn->engine) {
1375 ENGINE_free(conn->engine);
1376 conn->engine = NULL;
1377 }
1378
1379 if (conn->private_key) {
1380 EVP_PKEY_free(conn->private_key);
1381 conn->private_key = NULL;
1382 }
1383
1384 return ret;
1385#else /* OPENSSL_NO_ENGINE */
1386 return 0;
1387#endif /* OPENSSL_NO_ENGINE */
1388}
1389
1390
1391static void tls_engine_deinit(struct tls_connection *conn)
1392{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001393#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1395 if (conn->private_key) {
1396 EVP_PKEY_free(conn->private_key);
1397 conn->private_key = NULL;
1398 }
1399 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001400#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001402#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 conn->engine = NULL;
1404 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406}
1407
1408
1409int tls_get_errors(void *ssl_ctx)
1410{
1411 int count = 0;
1412 unsigned long err;
1413
1414 while ((err = ERR_get_error())) {
1415 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1416 ERR_error_string(err, NULL));
1417 count++;
1418 }
1419
1420 return count;
1421}
1422
Jouni Malinen26af48b2014-04-09 13:02:53 +03001423
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001424static const char * openssl_content_type(int content_type)
1425{
1426 switch (content_type) {
1427 case 20:
1428 return "change cipher spec";
1429 case 21:
1430 return "alert";
1431 case 22:
1432 return "handshake";
1433 case 23:
1434 return "application data";
1435 case 24:
1436 return "heartbeat";
1437 case 256:
1438 return "TLS header info"; /* pseudo content type */
Hai Shalom81f62d82019-07-22 12:10:00 -07001439 case 257:
1440 return "inner content type"; /* pseudo content type */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001441 default:
1442 return "?";
1443 }
1444}
1445
1446
1447static const char * openssl_handshake_type(int content_type, const u8 *buf,
1448 size_t len)
1449{
Hai Shalom81f62d82019-07-22 12:10:00 -07001450 if (content_type == 257 && buf && len == 1)
1451 return openssl_content_type(buf[0]);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001452 if (content_type != 22 || !buf || len == 0)
1453 return "";
1454 switch (buf[0]) {
1455 case 0:
1456 return "hello request";
1457 case 1:
1458 return "client hello";
1459 case 2:
1460 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001461 case 3:
1462 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001463 case 4:
1464 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001465 case 5:
1466 return "end of early data";
1467 case 6:
1468 return "hello retry request";
1469 case 8:
1470 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001471 case 11:
1472 return "certificate";
1473 case 12:
1474 return "server key exchange";
1475 case 13:
1476 return "certificate request";
1477 case 14:
1478 return "server hello done";
1479 case 15:
1480 return "certificate verify";
1481 case 16:
1482 return "client key exchange";
1483 case 20:
1484 return "finished";
1485 case 21:
1486 return "certificate url";
1487 case 22:
1488 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001489 case 23:
1490 return "supplemental data";
1491 case 24:
1492 return "key update";
1493 case 254:
1494 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001495 default:
1496 return "?";
1497 }
1498}
1499
1500
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001501#ifdef CONFIG_SUITEB
1502
1503static void check_server_hello(struct tls_connection *conn,
1504 const u8 *pos, const u8 *end)
1505{
1506 size_t payload_len, id_len;
1507
1508 /*
1509 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1510 * not make it cleanly available during handshake and we need to know
1511 * whether DHE was selected.
1512 */
1513
1514 if (end - pos < 3)
1515 return;
1516 payload_len = WPA_GET_BE24(pos);
1517 pos += 3;
1518
1519 if ((size_t) (end - pos) < payload_len)
1520 return;
1521 end = pos + payload_len;
1522
1523 /* Skip Version and Random */
1524 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1525 return;
1526 pos += 2 + SSL3_RANDOM_SIZE;
1527
1528 /* Skip Session ID */
1529 if (end - pos < 1)
1530 return;
1531 id_len = *pos++;
1532 if ((size_t) (end - pos) < id_len)
1533 return;
1534 pos += id_len;
1535
1536 if (end - pos < 2)
1537 return;
1538 conn->cipher_suite = WPA_GET_BE16(pos);
1539 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1540 conn->cipher_suite);
1541}
1542
1543
1544static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1545 const u8 *pos, const u8 *end)
1546{
1547 size_t payload_len;
1548 u16 dh_len;
1549 BIGNUM *p;
1550 int bits;
1551
1552 if (!(conn->flags & TLS_CONN_SUITEB))
1553 return;
1554
1555 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1556 if (conn->cipher_suite != 0x9f)
1557 return;
1558
1559 if (end - pos < 3)
1560 return;
1561 payload_len = WPA_GET_BE24(pos);
1562 pos += 3;
1563
1564 if ((size_t) (end - pos) < payload_len)
1565 return;
1566 end = pos + payload_len;
1567
1568 if (end - pos < 2)
1569 return;
1570 dh_len = WPA_GET_BE16(pos);
1571 pos += 2;
1572
1573 if ((size_t) (end - pos) < dh_len)
1574 return;
1575 p = BN_bin2bn(pos, dh_len, NULL);
1576 if (!p)
1577 return;
1578
1579 bits = BN_num_bits(p);
1580 BN_free(p);
1581
1582 conn->server_dh_prime_len = bits;
1583 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1584 conn->server_dh_prime_len);
1585}
1586
1587#endif /* CONFIG_SUITEB */
1588
1589
Jouni Malinen26af48b2014-04-09 13:02:53 +03001590static void tls_msg_cb(int write_p, int version, int content_type,
1591 const void *buf, size_t len, SSL *ssl, void *arg)
1592{
1593 struct tls_connection *conn = arg;
1594 const u8 *pos = buf;
1595
Sunil8cd6f4d2022-06-28 18:40:46 +00001596#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1597 if ((SSL_version(ssl) == TLS1_VERSION ||
1598 SSL_version(ssl) == TLS1_1_VERSION) &&
1599 SSL_get_security_level(ssl) > 0) {
1600 wpa_printf(MSG_DEBUG,
1601 "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm");
1602 SSL_set_security_level(ssl, 0);
1603 }
1604#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001605 if (write_p == 2) {
1606 wpa_printf(MSG_DEBUG,
1607 "OpenSSL: session ver=0x%x content_type=%d",
1608 version, content_type);
1609 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1610 return;
1611 }
1612
1613 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1614 write_p ? "TX" : "RX", version, content_type,
1615 openssl_content_type(content_type),
1616 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001617 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1618 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1619 size_t payload_len = WPA_GET_BE16(pos + 1);
1620 if (payload_len + 3 > len) {
1621 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1622 conn->invalid_hb_used = 1;
1623 }
1624 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001625
1626#ifdef CONFIG_SUITEB
1627 /*
1628 * Need to parse these handshake messages to be able to check DH prime
1629 * length since OpenSSL does not expose the new cipher suite and DH
1630 * parameters during handshake (e.g., for cert_cb() callback).
1631 */
1632 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1633 check_server_hello(conn, pos + 1, pos + len);
1634 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1635 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1636#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001637}
1638
1639
Sunil Ravia04bd252022-05-02 22:54:18 -07001640#ifdef CONFIG_TESTING_OPTIONS
1641#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1642/*
1643 * By setting the environment variable SSLKEYLOGFILE to a filename keying
1644 * material will be exported that you may use with Wireshark to decode any
1645 * TLS flows. Please see the following for more details:
1646 *
1647 * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption
1648 *
1649 * Example logging sessions are (you should delete the file on each run):
1650 *
1651 * rm -f /tmp/sslkey.log
1652 * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ...
1653 *
1654 * rm -f /tmp/sslkey.log
1655 * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ...
1656 *
1657 * rm -f /tmp/sslkey.log
1658 * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ...
1659 */
1660static void tls_keylog_cb(const SSL *ssl, const char *line)
1661{
1662 int fd;
1663 const char *filename;
1664 struct iovec iov[2];
1665
1666 filename = getenv("SSLKEYLOGFILE");
1667 if (!filename)
1668 return;
1669
1670 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
1671 if (fd < 0) {
1672 wpa_printf(MSG_ERROR,
1673 "OpenSSL: Failed to open keylog file %s: %s",
1674 filename, strerror(errno));
1675 return;
1676 }
1677
1678 /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed
1679 * to be atomic for O_APPEND. */
1680 iov[0].iov_base = (void *) line;
1681 iov[0].iov_len = os_strlen(line);
1682 iov[1].iov_base = "\n";
1683 iov[1].iov_len = 1;
1684
1685 if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) {
1686 wpa_printf(MSG_DEBUG,
1687 "OpenSSL: Failed to write to keylog file %s: %s",
1688 filename, strerror(errno));
1689 }
1690
1691 close(fd);
1692}
1693#endif
1694#endif /* CONFIG_TESTING_OPTIONS */
1695
1696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697struct tls_connection * tls_connection_init(void *ssl_ctx)
1698{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001699 struct tls_data *data = ssl_ctx;
1700 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 struct tls_connection *conn;
1702 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001703 X509_STORE *new_cert_store;
1704 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001705 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706
Hai Shalom74f70d42019-02-11 14:42:39 -08001707 /* Replace X509 store if it is time to update CRL. */
1708 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1709 os_reltime_expired(&now, &data->crl_last_reload,
1710 data->crl_reload_interval)) {
1711 wpa_printf(MSG_INFO,
1712 "OpenSSL: Flushing X509 store with ca_cert file");
1713 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1714 data->check_crl);
1715 if (!new_cert_store) {
1716 wpa_printf(MSG_ERROR,
1717 "OpenSSL: Error replacing X509 store with ca_cert file");
1718 } else {
1719 /* Replace old store */
1720 SSL_CTX_set_cert_store(ssl, new_cert_store);
1721 data->crl_last_reload = now;
1722 }
1723 }
1724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 conn = os_zalloc(sizeof(*conn));
1726 if (conn == NULL)
1727 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001728 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001729 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 conn->ssl = SSL_new(ssl);
1731 if (conn->ssl == NULL) {
1732 tls_show_errors(MSG_INFO, __func__,
1733 "Failed to initialize new SSL connection");
1734 os_free(conn);
1735 return NULL;
1736 }
1737
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001738 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001740 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1741 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1743 SSL_OP_SINGLE_DH_USE;
1744#ifdef SSL_OP_NO_COMPRESSION
1745 options |= SSL_OP_NO_COMPRESSION;
1746#endif /* SSL_OP_NO_COMPRESSION */
1747 SSL_set_options(conn->ssl, options);
Hai Shalom81f62d82019-07-22 12:10:00 -07001748#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
1749 /* Hopefully there is no need for middlebox compatibility mechanisms
1750 * when going through EAP authentication. */
1751 SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
1752#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753
Sunil Ravia04bd252022-05-02 22:54:18 -07001754#ifdef CONFIG_TESTING_OPTIONS
1755#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1756 /* Set the keylog file if the admin requested it. */
1757 if (getenv("SSLKEYLOGFILE"))
1758 SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb);
1759#endif
1760#endif /* CONFIG_TESTING_OPTIONS */
1761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 conn->ssl_in = BIO_new(BIO_s_mem());
1763 if (!conn->ssl_in) {
1764 tls_show_errors(MSG_INFO, __func__,
1765 "Failed to create a new BIO for ssl_in");
1766 SSL_free(conn->ssl);
1767 os_free(conn);
1768 return NULL;
1769 }
1770
1771 conn->ssl_out = BIO_new(BIO_s_mem());
1772 if (!conn->ssl_out) {
1773 tls_show_errors(MSG_INFO, __func__,
1774 "Failed to create a new BIO for ssl_out");
1775 SSL_free(conn->ssl);
1776 BIO_free(conn->ssl_in);
1777 os_free(conn);
1778 return NULL;
1779 }
1780
1781 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1782
1783 return conn;
1784}
1785
1786
1787void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1788{
1789 if (conn == NULL)
1790 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001791 if (conn->success_data) {
1792 /*
1793 * Make sure ssl_clear_bad_session() does not remove this
1794 * session.
1795 */
1796 SSL_set_quiet_shutdown(conn->ssl, 1);
1797 SSL_shutdown(conn->ssl);
1798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 SSL_free(conn->ssl);
1800 tls_engine_deinit(conn);
1801 os_free(conn->subject_match);
1802 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001803 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001804 os_free(conn->domain_match);
Hai Shalom021b0b52019-04-10 11:17:58 -07001805 os_free(conn->check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 os_free(conn->session_ticket);
Hai Shalom899fcc72020-10-19 14:38:18 -07001807 os_free(conn->peer_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 os_free(conn);
1809}
1810
1811
1812int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1813{
1814 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1815}
1816
1817
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001818char * tls_connection_peer_serial_num(void *tls_ctx,
1819 struct tls_connection *conn)
1820{
1821 ASN1_INTEGER *ser;
1822 char *serial_num;
1823 size_t len;
1824
1825 if (!conn->peer_cert)
1826 return NULL;
1827
1828 ser = X509_get_serialNumber(conn->peer_cert);
1829 if (!ser)
1830 return NULL;
1831
1832 len = ASN1_STRING_length(ser) * 2 + 1;
1833 serial_num = os_malloc(len);
1834 if (!serial_num)
1835 return NULL;
1836 wpa_snprintf_hex_uppercase(serial_num, len,
1837 ASN1_STRING_get0_data(ser),
1838 ASN1_STRING_length(ser));
1839 return serial_num;
1840}
1841
1842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1844{
1845 if (conn == NULL)
1846 return -1;
1847
1848 /* Shutdown previous TLS connection without notifying the peer
1849 * because the connection was already terminated in practice
1850 * and "close notify" shutdown alert would confuse AS. */
1851 SSL_set_quiet_shutdown(conn->ssl, 1);
1852 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001853 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854}
1855
1856
1857static int tls_match_altsubject_component(X509 *cert, int type,
1858 const char *value, size_t len)
1859{
1860 GENERAL_NAME *gen;
1861 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001862 int found = 0;
1863 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864
1865 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1866
1867 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1868 gen = sk_GENERAL_NAME_value(ext, i);
1869 if (gen->type != type)
1870 continue;
1871 if (os_strlen((char *) gen->d.ia5->data) == len &&
1872 os_memcmp(value, gen->d.ia5->data, len) == 0)
1873 found++;
1874 }
1875
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001876 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 return found;
1879}
1880
1881
1882static int tls_match_altsubject(X509 *cert, const char *match)
1883{
1884 int type;
1885 const char *pos, *end;
1886 size_t len;
1887
1888 pos = match;
1889 do {
1890 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1891 type = GEN_EMAIL;
1892 pos += 6;
1893 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1894 type = GEN_DNS;
1895 pos += 4;
1896 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1897 type = GEN_URI;
1898 pos += 4;
1899 } else {
1900 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1901 "match '%s'", pos);
1902 return 0;
1903 }
1904 end = os_strchr(pos, ';');
1905 while (end) {
1906 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1907 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1908 os_strncmp(end + 1, "URI:", 4) == 0)
1909 break;
1910 end = os_strchr(end + 1, ';');
1911 }
1912 if (end)
1913 len = end - pos;
1914 else
1915 len = os_strlen(pos);
1916 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1917 return 1;
1918 pos = end + 1;
1919 } while (end);
1920
1921 return 0;
1922}
1923
1924
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001925#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001926static int domain_suffix_match(const u8 *val, size_t len, const char *match,
Hai Shalom021b0b52019-04-10 11:17:58 -07001927 size_t match_len, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001928{
Hai Shalom021b0b52019-04-10 11:17:58 -07001929 size_t i;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001930
1931 /* Check for embedded nuls that could mess up suffix matching */
1932 for (i = 0; i < len; i++) {
1933 if (val[i] == '\0') {
1934 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1935 return 0;
1936 }
1937 }
1938
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001939 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001940 return 0;
1941
1942 if (os_strncasecmp((const char *) val + len - match_len, match,
1943 match_len) != 0)
1944 return 0; /* no match */
1945
1946 if (match_len == len)
1947 return 1; /* exact match */
1948
1949 if (val[len - match_len - 1] == '.')
1950 return 1; /* full label match completes suffix match */
1951
1952 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1953 return 0;
1954}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001955#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001956
1957
Hai Shalom021b0b52019-04-10 11:17:58 -07001958struct tls_dn_field_order_cnt {
1959 u8 cn;
1960 u8 c;
1961 u8 l;
1962 u8 st;
1963 u8 o;
1964 u8 ou;
1965 u8 email;
1966};
1967
1968
1969static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt,
1970 int nid)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001971{
Hai Shalom021b0b52019-04-10 11:17:58 -07001972 switch (nid) {
1973 case NID_commonName:
1974 return dn_cnt->cn;
1975 case NID_countryName:
1976 return dn_cnt->c;
1977 case NID_localityName:
1978 return dn_cnt->l;
1979 case NID_stateOrProvinceName:
1980 return dn_cnt->st;
1981 case NID_organizationName:
1982 return dn_cnt->o;
1983 case NID_organizationalUnitName:
1984 return dn_cnt->ou;
1985 case NID_pkcs9_emailAddress:
1986 return dn_cnt->email;
1987 default:
1988 wpa_printf(MSG_ERROR,
1989 "TLS: Unknown NID '%d' in check_cert_subject",
1990 nid);
1991 return -1;
1992 }
1993}
1994
1995
1996/**
1997 * match_dn_field - Match configuration DN field against Certificate DN field
1998 * @cert: Certificate
1999 * @nid: NID of DN field
2000 * @field: Field name
2001 * @value DN field value which is passed from configuration
2002 * e.g., if configuration have C=US and this argument will point to US.
2003 * @dn_cnt: DN matching context
2004 * Returns: 1 on success and 0 on failure
2005 */
2006static int match_dn_field(const X509 *cert, int nid, const char *field,
2007 const char *value,
2008 const struct tls_dn_field_order_cnt *dn_cnt)
2009{
2010 int i, ret = 0, len, config_dn_field_index, match_index = 0;
2011 X509_NAME *name;
2012
2013 len = os_strlen(value);
2014 name = X509_get_subject_name((X509 *) cert);
2015
2016 /* Assign incremented cnt for every field of DN to check DN field in
2017 * right order */
2018 config_dn_field_index = get_dn_field_index(dn_cnt, nid);
2019 if (config_dn_field_index < 0)
2020 return 0;
2021
2022 /* Fetch value based on NID */
2023 for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) {
2024 X509_NAME_ENTRY *e;
2025 ASN1_STRING *cn;
2026
2027 e = X509_NAME_get_entry(name, i);
2028 if (!e)
2029 continue;
2030
2031 cn = X509_NAME_ENTRY_get_data(e);
2032 if (!cn)
2033 continue;
2034
2035 match_index++;
2036
2037 /* check for more than one DN field with same name */
2038 if (match_index != config_dn_field_index)
2039 continue;
2040
2041 /* Check wildcard at the right end side */
2042 /* E.g., if OU=develop* mentioned in configuration, allow 'OU'
2043 * of the subject in the client certificate to start with
2044 * 'develop' */
2045 if (len > 0 && value[len - 1] == '*') {
2046 /* Compare actual certificate DN field value with
2047 * configuration DN field value up to the specified
2048 * length. */
2049 ret = ASN1_STRING_length(cn) >= len - 1 &&
2050 os_memcmp(ASN1_STRING_get0_data(cn), value,
2051 len - 1) == 0;
2052 } else {
2053 /* Compare actual certificate DN field value with
2054 * configuration DN field value */
2055 ret = ASN1_STRING_length(cn) == len &&
2056 os_memcmp(ASN1_STRING_get0_data(cn), value,
2057 len) == 0;
2058 }
2059 if (!ret) {
2060 wpa_printf(MSG_ERROR,
2061 "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'",
2062 field, value, ASN1_STRING_get0_data(cn));
2063 }
2064 break;
2065 }
2066
2067 return ret;
2068}
2069
2070
2071/**
2072 * get_value_from_field - Get value from DN field
2073 * @cert: Certificate
2074 * @field_str: DN field string which is passed from configuration file (e.g.,
2075 * C=US)
2076 * @dn_cnt: DN matching context
2077 * Returns: 1 on success and 0 on failure
2078 */
2079static int get_value_from_field(const X509 *cert, char *field_str,
2080 struct tls_dn_field_order_cnt *dn_cnt)
2081{
2082 int nid;
2083 char *context = NULL, *name, *value;
2084
2085 if (os_strcmp(field_str, "*") == 0)
2086 return 1; /* wildcard matches everything */
2087
2088 name = str_token(field_str, "=", &context);
2089 if (!name)
2090 return 0;
2091
2092 /* Compare all configured DN fields and assign nid based on that to
2093 * fetch correct value from certificate subject */
2094 if (os_strcmp(name, "CN") == 0) {
2095 nid = NID_commonName;
2096 dn_cnt->cn++;
2097 } else if(os_strcmp(name, "C") == 0) {
2098 nid = NID_countryName;
2099 dn_cnt->c++;
2100 } else if (os_strcmp(name, "L") == 0) {
2101 nid = NID_localityName;
2102 dn_cnt->l++;
2103 } else if (os_strcmp(name, "ST") == 0) {
2104 nid = NID_stateOrProvinceName;
2105 dn_cnt->st++;
2106 } else if (os_strcmp(name, "O") == 0) {
2107 nid = NID_organizationName;
2108 dn_cnt->o++;
2109 } else if (os_strcmp(name, "OU") == 0) {
2110 nid = NID_organizationalUnitName;
2111 dn_cnt->ou++;
2112 } else if (os_strcmp(name, "emailAddress") == 0) {
2113 nid = NID_pkcs9_emailAddress;
2114 dn_cnt->email++;
2115 } else {
2116 wpa_printf(MSG_ERROR,
2117 "TLS: Unknown field '%s' in check_cert_subject", name);
2118 return 0;
2119 }
2120
2121 value = str_token(field_str, "=", &context);
2122 if (!value) {
2123 wpa_printf(MSG_ERROR,
2124 "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject",
2125 name);
2126 return 0;
2127 }
2128
2129 return match_dn_field(cert, nid, name, value, dn_cnt);
2130}
2131
2132
2133/**
2134 * tls_match_dn_field - Match subject DN field with check_cert_subject
2135 * @cert: Certificate
2136 * @match: check_cert_subject string
2137 * Returns: Return 1 on success and 0 on failure
2138*/
2139static int tls_match_dn_field(X509 *cert, const char *match)
2140{
2141 const char *token, *last = NULL;
2142 char field[256];
2143 struct tls_dn_field_order_cnt dn_cnt;
2144
2145 os_memset(&dn_cnt, 0, sizeof(dn_cnt));
2146
2147 /* Maximum length of each DN field is 255 characters */
2148
2149 /* Process each '/' delimited field */
2150 while ((token = cstr_token(match, "/", &last))) {
2151 if (last - token >= (int) sizeof(field)) {
2152 wpa_printf(MSG_ERROR,
2153 "OpenSSL: Too long DN matching field value in '%s'",
2154 match);
2155 return 0;
2156 }
2157 os_memcpy(field, token, last - token);
2158 field[last - token] = '\0';
2159
2160 if (!get_value_from_field(cert, field, &dn_cnt)) {
2161 wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'",
2162 field);
2163 return 0;
2164 }
2165 }
2166
2167 return 1;
2168}
2169
2170
2171#ifndef CONFIG_NATIVE_WINDOWS
2172static int tls_match_suffix_helper(X509 *cert, const char *match,
2173 size_t match_len, int full)
2174{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002175 GENERAL_NAME *gen;
2176 void *ext;
2177 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002178 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002179 int dns_name = 0;
2180 X509_NAME *name;
2181
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002182 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
2183 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002184
2185 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
2186
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002187 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
2188 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002189 if (gen->type != GEN_DNS)
2190 continue;
2191 dns_name++;
2192 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
2193 gen->d.dNSName->data,
2194 gen->d.dNSName->length);
2195 if (domain_suffix_match(gen->d.dNSName->data,
Hai Shalom021b0b52019-04-10 11:17:58 -07002196 gen->d.dNSName->length,
2197 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002198 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
2199 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002200 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002201 return 1;
2202 }
2203 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002204 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002205
2206 if (dns_name) {
2207 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
2208 return 0;
2209 }
2210
2211 name = X509_get_subject_name(cert);
2212 i = -1;
2213 for (;;) {
2214 X509_NAME_ENTRY *e;
2215 ASN1_STRING *cn;
2216
2217 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
2218 if (i == -1)
2219 break;
2220 e = X509_NAME_get_entry(name, i);
2221 if (e == NULL)
2222 continue;
2223 cn = X509_NAME_ENTRY_get_data(e);
2224 if (cn == NULL)
2225 continue;
2226 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
2227 cn->data, cn->length);
Hai Shalom021b0b52019-04-10 11:17:58 -07002228 if (domain_suffix_match(cn->data, cn->length,
2229 match, match_len, full) == 1) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002230 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
2231 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002232 return 1;
2233 }
2234 }
2235
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002236 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
2237 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07002238 return 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07002239}
2240#endif /* CONFIG_NATIVE_WINDOWS */
2241
2242
2243static int tls_match_suffix(X509 *cert, const char *match, int full)
2244{
2245#ifdef CONFIG_NATIVE_WINDOWS
2246 /* wincrypt.h has conflicting X509_NAME definition */
2247 return -1;
2248#else /* CONFIG_NATIVE_WINDOWS */
2249 const char *token, *last = NULL;
2250
2251 /* Process each match alternative separately until a match is found */
2252 while ((token = cstr_token(match, ";", &last))) {
2253 if (tls_match_suffix_helper(cert, token, last - token, full))
2254 return 1;
2255 }
2256
2257 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08002258#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002259}
2260
2261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262static enum tls_fail_reason openssl_tls_fail_reason(int err)
2263{
2264 switch (err) {
2265 case X509_V_ERR_CERT_REVOKED:
2266 return TLS_FAIL_REVOKED;
2267 case X509_V_ERR_CERT_NOT_YET_VALID:
2268 case X509_V_ERR_CRL_NOT_YET_VALID:
2269 return TLS_FAIL_NOT_YET_VALID;
2270 case X509_V_ERR_CERT_HAS_EXPIRED:
2271 case X509_V_ERR_CRL_HAS_EXPIRED:
2272 return TLS_FAIL_EXPIRED;
2273 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
2274 case X509_V_ERR_UNABLE_TO_GET_CRL:
2275 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
2276 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
2277 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
2278 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2279 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
2280 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
2281 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
2282 case X509_V_ERR_INVALID_CA:
2283 return TLS_FAIL_UNTRUSTED;
2284 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
2285 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
2286 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
2287 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
2288 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
2289 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
2290 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
2291 case X509_V_ERR_CERT_UNTRUSTED:
2292 case X509_V_ERR_CERT_REJECTED:
2293 return TLS_FAIL_BAD_CERTIFICATE;
2294 default:
2295 return TLS_FAIL_UNSPECIFIED;
2296 }
2297}
2298
2299
2300static struct wpabuf * get_x509_cert(X509 *cert)
2301{
2302 struct wpabuf *buf;
2303 u8 *tmp;
2304
2305 int cert_len = i2d_X509(cert, NULL);
2306 if (cert_len <= 0)
2307 return NULL;
2308
2309 buf = wpabuf_alloc(cert_len);
2310 if (buf == NULL)
2311 return NULL;
2312
2313 tmp = wpabuf_put(buf, cert_len);
2314 i2d_X509(cert, &tmp);
2315 return buf;
2316}
2317
2318
2319static void openssl_tls_fail_event(struct tls_connection *conn,
2320 X509 *err_cert, int err, int depth,
2321 const char *subject, const char *err_str,
2322 enum tls_fail_reason reason)
2323{
2324 union tls_event_data ev;
2325 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002326 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327
Pavel Grafov4d8552e2018-02-06 11:28:29 +00002328#ifdef ANDROID
2329 log_cert_validation_failure(err_str);
2330#endif
2331
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002332 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 return;
2334
2335 cert = get_x509_cert(err_cert);
2336 os_memset(&ev, 0, sizeof(ev));
2337 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
2338 reason : openssl_tls_fail_reason(err);
2339 ev.cert_fail.depth = depth;
2340 ev.cert_fail.subject = subject;
2341 ev.cert_fail.reason_txt = err_str;
2342 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002343 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 wpabuf_free(cert);
2345}
2346
2347
Hai Shalom81f62d82019-07-22 12:10:00 -07002348static int openssl_cert_tod(X509 *cert)
2349{
2350 CERTIFICATEPOLICIES *ext;
2351 stack_index_t i;
2352 char buf[100];
2353 int res;
2354 int tod = 0;
2355
2356 ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL);
2357 if (!ext)
2358 return 0;
2359
2360 for (i = 0; i < sk_POLICYINFO_num(ext); i++) {
2361 POLICYINFO *policy;
2362
2363 policy = sk_POLICYINFO_value(ext, i);
2364 res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0);
2365 if (res < 0 || (size_t) res >= sizeof(buf))
2366 continue;
2367 wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf);
2368 if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0)
Hai Shalomc3565922019-10-28 11:58:20 -07002369 tod = 1; /* TOD-STRICT */
2370 else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod)
2371 tod = 2; /* TOD-TOFU */
Hai Shalom81f62d82019-07-22 12:10:00 -07002372 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002373 sk_POLICYINFO_pop_free(ext, POLICYINFO_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002374
2375 return tod;
2376}
2377
2378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379static void openssl_tls_cert_event(struct tls_connection *conn,
2380 X509 *err_cert, int depth,
2381 const char *subject)
2382{
2383 struct wpabuf *cert = NULL;
2384 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002385 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002386 char *altsubject[TLS_MAX_ALT_SUBJECT];
2387 int alt, num_altsubject = 0;
2388 GENERAL_NAME *gen;
2389 void *ext;
2390 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002391 ASN1_INTEGER *ser;
2392 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393#ifdef CONFIG_SHA256
2394 u8 hash[32];
2395#endif /* CONFIG_SHA256 */
2396
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002397 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398 return;
2399
2400 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002401 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
2402 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 cert = get_x509_cert(err_cert);
2404 ev.peer_cert.cert = cert;
2405 }
2406#ifdef CONFIG_SHA256
2407 if (cert) {
2408 const u8 *addr[1];
2409 size_t len[1];
2410 addr[0] = wpabuf_head(cert);
2411 len[0] = wpabuf_len(cert);
2412 if (sha256_vector(1, addr, len, hash) == 0) {
2413 ev.peer_cert.hash = hash;
2414 ev.peer_cert.hash_len = sizeof(hash);
2415 }
2416 }
2417#endif /* CONFIG_SHA256 */
2418 ev.peer_cert.depth = depth;
2419 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002420
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002421 ser = X509_get_serialNumber(err_cert);
2422 if (ser) {
2423 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
2424 ASN1_STRING_get0_data(ser),
2425 ASN1_STRING_length(ser));
2426 ev.peer_cert.serial_num = serial_num;
2427 }
2428
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002429 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
2430 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
2431 char *pos;
2432
2433 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
2434 break;
2435 gen = sk_GENERAL_NAME_value(ext, i);
2436 if (gen->type != GEN_EMAIL &&
2437 gen->type != GEN_DNS &&
2438 gen->type != GEN_URI)
2439 continue;
2440
2441 pos = os_malloc(10 + gen->d.ia5->length + 1);
2442 if (pos == NULL)
2443 break;
2444 altsubject[num_altsubject++] = pos;
2445
2446 switch (gen->type) {
2447 case GEN_EMAIL:
2448 os_memcpy(pos, "EMAIL:", 6);
2449 pos += 6;
2450 break;
2451 case GEN_DNS:
2452 os_memcpy(pos, "DNS:", 4);
2453 pos += 4;
2454 break;
2455 case GEN_URI:
2456 os_memcpy(pos, "URI:", 4);
2457 pos += 4;
2458 break;
2459 }
2460
2461 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2462 pos += gen->d.ia5->length;
2463 *pos = '\0';
2464 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002465 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002466
2467 for (alt = 0; alt < num_altsubject; alt++)
2468 ev.peer_cert.altsubject[alt] = altsubject[alt];
2469 ev.peer_cert.num_altsubject = num_altsubject;
2470
Hai Shalom81f62d82019-07-22 12:10:00 -07002471 ev.peer_cert.tod = openssl_cert_tod(err_cert);
2472
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002473 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002475 for (alt = 0; alt < num_altsubject; alt++)
2476 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477}
2478
2479
Hai Shalomc3565922019-10-28 11:58:20 -07002480static void debug_print_cert(X509 *cert, const char *title)
2481{
2482#ifndef CONFIG_NO_STDOUT_DEBUG
2483 BIO *out;
2484 size_t rlen;
2485 char *txt;
2486 int res;
2487
2488 if (wpa_debug_level > MSG_DEBUG)
2489 return;
2490
2491 out = BIO_new(BIO_s_mem());
2492 if (!out)
2493 return;
2494
2495 X509_print(out, cert);
2496 rlen = BIO_ctrl_pending(out);
2497 txt = os_malloc(rlen + 1);
2498 if (txt) {
2499 res = BIO_read(out, txt, rlen);
2500 if (res > 0) {
2501 txt[res] = '\0';
2502 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
2503 }
2504 os_free(txt);
2505 }
2506
2507 BIO_free(out);
2508#endif /* CONFIG_NO_STDOUT_DEBUG */
2509}
2510
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2513{
2514 char buf[256];
2515 X509 *err_cert;
2516 int err, depth;
2517 SSL *ssl;
2518 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002519 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002520 char *match, *altmatch, *suffix_match, *domain_match;
Hai Shalom021b0b52019-04-10 11:17:58 -07002521 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 const char *err_str;
2523
2524 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002525 if (!err_cert)
2526 return 0;
2527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 err = X509_STORE_CTX_get_error(x509_ctx);
2529 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2530 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2531 SSL_get_ex_data_X509_STORE_CTX_idx());
Hai Shalomc3565922019-10-28 11:58:20 -07002532 os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth);
2533 debug_print_cert(err_cert, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2535
2536 conn = SSL_get_app_data(ssl);
2537 if (conn == NULL)
2538 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002539
2540 if (depth == 0)
2541 conn->peer_cert = err_cert;
2542 else if (depth == 1)
2543 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002544 else if (depth == 2)
2545 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002546
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002547 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 match = conn->subject_match;
2549 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002550 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002551 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552
2553 if (!preverify_ok && !conn->ca_cert_verify)
2554 preverify_ok = 1;
2555 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2556 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002557 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2558 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2559 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2560 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2561 "time mismatch");
2562 preverify_ok = 1;
2563 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002564 if (!preverify_ok && !conn->data->check_crl_strict &&
2565 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2566 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2567 wpa_printf(MSG_DEBUG,
2568 "OpenSSL: Ignore certificate validity CRL time mismatch");
2569 preverify_ok = 1;
2570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571
2572 err_str = X509_verify_cert_error_string(err);
2573
2574#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002575 /*
2576 * Do not require preverify_ok so we can explicity allow otherwise
2577 * invalid pinned server certificates.
2578 */
2579 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 struct wpabuf *cert;
2581 cert = get_x509_cert(err_cert);
2582 if (!cert) {
2583 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2584 "server certificate data");
2585 preverify_ok = 0;
2586 } else {
2587 u8 hash[32];
2588 const u8 *addr[1];
2589 size_t len[1];
Hai Shalomae89c832023-04-19 15:42:17 -07002590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 addr[0] = wpabuf_head(cert);
2592 len[0] = wpabuf_len(cert);
2593 if (sha256_vector(1, addr, len, hash) < 0 ||
2594 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2595 err_str = "Server certificate mismatch";
2596 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2597 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002598 } else if (!preverify_ok) {
2599 /*
2600 * Certificate matches pinned certificate, allow
2601 * regardless of other problems.
2602 */
2603 wpa_printf(MSG_DEBUG,
2604 "OpenSSL: Ignore validation issues for a pinned server certificate");
2605 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606 }
2607 wpabuf_free(cert);
2608 }
2609 }
2610#endif /* CONFIG_SHA256 */
2611
2612 if (!preverify_ok) {
Hai Shalomae89c832023-04-19 15:42:17 -07002613 /* Send cert events for the peer certificate chain so that
2614 * the upper layers get information about it even if
2615 * validation of a CA certificate fails. */
2616 STACK_OF(X509) *chain;
2617 int num_of_certs;
Hai Shalom81f62d82019-07-22 12:10:00 -07002618
Hai Shalomae89c832023-04-19 15:42:17 -07002619 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2620 num_of_certs = sk_X509_num(chain);
2621 if (chain && num_of_certs > 0) {
2622 char buf2[256];
2623 X509 *cert;
2624 int cur_depth;
Hai Shalom81f62d82019-07-22 12:10:00 -07002625
Hai Shalomae89c832023-04-19 15:42:17 -07002626 for (cur_depth = num_of_certs - 1; cur_depth >= 0; cur_depth--) {
2627 cert = sk_X509_value(chain, cur_depth);
Hai Shalom81f62d82019-07-22 12:10:00 -07002628 X509_NAME_oneline(X509_get_subject_name(cert),
2629 buf2, sizeof(buf2));
2630
Hai Shalomae89c832023-04-19 15:42:17 -07002631 openssl_tls_cert_event(conn, cert, cur_depth, buf2);
Hai Shalom81f62d82019-07-22 12:10:00 -07002632 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002633 }
Hai Shalomae89c832023-04-19 15:42:17 -07002634 if (chain)
2635 sk_X509_pop_free(chain, X509_free);
Hai Shalom81f62d82019-07-22 12:10:00 -07002636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2638 " error %d (%s) depth %d for '%s'", err, err_str,
2639 depth, buf);
2640 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2641 err_str, TLS_FAIL_UNSPECIFIED);
2642 return preverify_ok;
2643 }
2644
Hai Shalomae89c832023-04-19 15:42:17 -07002645 openssl_tls_cert_event(conn, err_cert, depth, buf);
2646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2648 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2649 preverify_ok, err, err_str,
2650 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002651 check_cert_subject = conn->check_cert_subject;
2652 if (!check_cert_subject)
2653 check_cert_subject = conn->data->check_cert_subject;
2654 if (check_cert_subject) {
2655 if (depth == 0 &&
2656 !tls_match_dn_field(err_cert, check_cert_subject)) {
2657 preverify_ok = 0;
2658 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2659 "Distinguished Name",
2660 TLS_FAIL_DN_MISMATCH);
2661 }
2662 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2664 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2665 "match with '%s'", buf, match);
2666 preverify_ok = 0;
2667 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2668 "Subject mismatch",
2669 TLS_FAIL_SUBJECT_MISMATCH);
2670 } else if (depth == 0 && altmatch &&
2671 !tls_match_altsubject(err_cert, altmatch)) {
2672 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2673 "'%s' not found", altmatch);
2674 preverify_ok = 0;
2675 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2676 "AltSubject mismatch",
2677 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002678 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002679 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002680 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2681 suffix_match);
2682 preverify_ok = 0;
2683 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2684 "Domain suffix mismatch",
2685 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002686 } else if (depth == 0 && domain_match &&
2687 !tls_match_suffix(err_cert, domain_match, 1)) {
2688 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2689 domain_match);
2690 preverify_ok = 0;
2691 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2692 "Domain mismatch",
2693 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695
2696 if (conn->cert_probe && preverify_ok && depth == 0) {
2697 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2698 "on probe-only run");
2699 preverify_ok = 0;
2700 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2701 "Server certificate chain probe",
2702 TLS_FAIL_SERVER_CHAIN_PROBE);
2703 }
2704
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002705#ifdef CONFIG_SUITEB
2706 if (conn->flags & TLS_CONN_SUITEB) {
2707 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002708 int len = -1;
2709
2710 pk = X509_get_pubkey(err_cert);
2711 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002712 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002713 EVP_PKEY_free(pk);
2714 }
2715
2716 if (len >= 0) {
2717 wpa_printf(MSG_DEBUG,
2718 "OpenSSL: RSA modulus size: %d bits", len);
2719 if (len < 3072) {
2720 preverify_ok = 0;
2721 openssl_tls_fail_event(
2722 conn, err_cert, err,
2723 depth, buf,
2724 "Insufficient RSA modulus size",
2725 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2726 }
2727 }
2728 }
2729#endif /* CONFIG_SUITEB */
2730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002731#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002732 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2733 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002734 enum ocsp_result res;
2735
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002736 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2737 conn->peer_issuer,
2738 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002739 if (res == OCSP_REVOKED) {
2740 preverify_ok = 0;
2741 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2742 "certificate revoked",
2743 TLS_FAIL_REVOKED);
2744 if (err == X509_V_OK)
2745 X509_STORE_CTX_set_error(
2746 x509_ctx, X509_V_ERR_CERT_REVOKED);
2747 } else if (res != OCSP_GOOD &&
2748 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2749 preverify_ok = 0;
2750 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2751 "bad certificate status response",
2752 TLS_FAIL_UNSPECIFIED);
2753 }
2754 }
2755#endif /* OPENSSL_IS_BORINGSSL */
2756
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002757 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002758 context->event_cb(context->cb_ctx,
2759 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002760
Hai Shalom899fcc72020-10-19 14:38:18 -07002761 if (depth == 0 && preverify_ok) {
2762 os_free(conn->peer_subject);
2763 conn->peer_subject = os_strdup(buf);
2764 }
2765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766 return preverify_ok;
2767}
2768
2769
2770#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002771static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002773 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 X509_LOOKUP *lookup;
2775 int ret = 0;
2776
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002777 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 X509_LOOKUP_file());
2779 if (lookup == NULL) {
2780 tls_show_errors(MSG_WARNING, __func__,
2781 "Failed add lookup for X509 store");
2782 return -1;
2783 }
2784
2785 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2786 unsigned long err = ERR_peek_error();
2787 tls_show_errors(MSG_WARNING, __func__,
2788 "Failed load CA in DER format");
2789 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2790 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2791 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2792 "cert already in hash table error",
2793 __func__);
2794 } else
2795 ret = -1;
2796 }
2797
2798 return ret;
2799}
2800#endif /* OPENSSL_NO_STDIO */
2801
2802
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002803static int tls_connection_ca_cert(struct tls_data *data,
2804 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 const char *ca_cert, const u8 *ca_cert_blob,
2806 size_t ca_cert_blob_len, const char *ca_path)
2807{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002808 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002809 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 /*
2812 * Remove previously configured trusted CA certificates before adding
2813 * new ones.
2814 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002815 store = X509_STORE_new();
2816 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2818 "certificate store", __func__);
2819 return -1;
2820 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002821 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822
2823 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2824 conn->ca_cert_verify = 1;
2825
2826 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2827 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2828 "chain");
2829 conn->cert_probe = 1;
2830 conn->ca_cert_verify = 0;
2831 return 0;
2832 }
2833
2834 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2835#ifdef CONFIG_SHA256
2836 const char *pos = ca_cert + 7;
2837 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2838 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2839 "hash value '%s'", ca_cert);
2840 return -1;
2841 }
2842 pos += 14;
2843 if (os_strlen(pos) != 32 * 2) {
2844 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2845 "hash length in ca_cert '%s'", ca_cert);
2846 return -1;
2847 }
2848 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2849 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2850 "value in ca_cert '%s'", ca_cert);
2851 return -1;
2852 }
2853 conn->server_cert_only = 1;
2854 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2855 "certificate match");
2856 return 0;
2857#else /* CONFIG_SHA256 */
2858 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2859 "cannot validate server certificate hash");
2860 return -1;
2861#endif /* CONFIG_SHA256 */
2862 }
2863
2864 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002865 X509 *cert = d2i_X509(NULL,
2866 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 ca_cert_blob_len);
2868 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002869 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2870 ca_cert_blob_len);
2871
2872 if (bio) {
2873 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2874 BIO_free(bio);
2875 }
2876
2877 if (!cert) {
2878 tls_show_errors(MSG_WARNING, __func__,
2879 "Failed to parse ca_cert_blob");
2880 return -1;
2881 }
2882
2883 while (ERR_get_error()) {
2884 /* Ignore errors from DER conversion. */
2885 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 }
2887
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002888 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2889 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 unsigned long err = ERR_peek_error();
2891 tls_show_errors(MSG_WARNING, __func__,
2892 "Failed to add ca_cert_blob to "
2893 "certificate store");
2894 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2895 ERR_GET_REASON(err) ==
2896 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2897 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2898 "cert already in hash table error",
2899 __func__);
2900 } else {
2901 X509_free(cert);
2902 return -1;
2903 }
2904 }
2905 X509_free(cert);
2906 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2907 "to certificate store", __func__);
2908 return 0;
2909 }
2910
2911#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002912 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002913 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2914 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002915 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Gabriel Birenff4f8382023-04-06 20:14:39 +00002916 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002918 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2919 return 0;
2920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002922 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002923 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2924 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2925 char *aliases = os_strdup(
2926 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002927 const char *delim = " ";
2928 int rc = 0;
2929 char *savedptr;
2930 char *alias;
2931
2932 if (!aliases)
2933 return -1;
2934 alias = strtok_r(aliases, delim, &savedptr);
2935 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2936 if (tls_add_ca_from_keystore_encoded(
Gabriel Birenff4f8382023-04-06 20:14:39 +00002937 SSL_CTX_get_cert_store(ssl_ctx), alias, conn)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -07002938 wpa_printf(MSG_ERROR,
2939 "OpenSSL: Failed to add ca_cert %s from keystore",
2940 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002941 rc = -1;
2942 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 }
2944 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002945 os_free(aliases);
2946 if (rc)
2947 return rc;
2948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2950 return 0;
2951 }
2952#endif /* ANDROID */
2953
2954#ifdef CONFIG_NATIVE_WINDOWS
2955 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2956 0) {
2957 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2958 "system certificate store");
2959 return 0;
2960 }
2961#endif /* CONFIG_NATIVE_WINDOWS */
2962
2963 if (ca_cert || ca_path) {
2964#ifndef OPENSSL_NO_STDIO
2965 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2966 1) {
2967 tls_show_errors(MSG_WARNING, __func__,
2968 "Failed to load root certificates");
2969 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002970 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2972 "DER format CA certificate",
2973 __func__);
2974 } else
2975 return -1;
2976 } else {
2977 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2978 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002979 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 }
2981#else /* OPENSSL_NO_STDIO */
2982 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2983 __func__);
2984 return -1;
2985#endif /* OPENSSL_NO_STDIO */
2986 } else {
2987 /* No ca_cert configured - do not try to verify server
2988 * certificate */
2989 conn->ca_cert_verify = 0;
2990 }
2991
2992 return 0;
2993}
2994
2995
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002996static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002998 SSL_CTX *ssl_ctx = data->ssl;
2999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 if (ca_cert) {
3001 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
3002 {
3003 tls_show_errors(MSG_WARNING, __func__,
3004 "Failed to load root certificates");
3005 return -1;
3006 }
3007
3008 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
3009 "certificate(s) loaded");
3010
3011#ifndef OPENSSL_NO_STDIO
3012 /* Add the same CAs to the client certificate requests */
3013 SSL_CTX_set_client_CA_list(ssl_ctx,
3014 SSL_load_client_CA_file(ca_cert));
3015#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003016
3017 os_free(data->ca_cert);
3018 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 }
3020
3021 return 0;
3022}
3023
3024
Hai Shalom74f70d42019-02-11 14:42:39 -08003025int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026{
3027 int flags;
3028
3029 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003030 struct tls_data *data = ssl_ctx;
3031 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 if (cs == NULL) {
3033 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3034 "certificate store when enabling "
3035 "check_crl");
3036 return -1;
3037 }
3038 flags = X509_V_FLAG_CRL_CHECK;
3039 if (check_crl == 2)
3040 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3041 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003042
3043 data->check_crl = check_crl;
3044 data->check_crl_strict = strict;
3045 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 }
3047 return 0;
3048}
3049
3050
3051static int tls_connection_set_subject_match(struct tls_connection *conn,
3052 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003053 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003054 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003055 const char *domain_match,
3056 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057{
3058 os_free(conn->subject_match);
3059 conn->subject_match = NULL;
3060 if (subject_match) {
3061 conn->subject_match = os_strdup(subject_match);
3062 if (conn->subject_match == NULL)
3063 return -1;
3064 }
3065
3066 os_free(conn->altsubject_match);
3067 conn->altsubject_match = NULL;
3068 if (altsubject_match) {
3069 conn->altsubject_match = os_strdup(altsubject_match);
3070 if (conn->altsubject_match == NULL)
3071 return -1;
3072 }
3073
Dmitry Shmidt051af732013-10-22 13:52:46 -07003074 os_free(conn->suffix_match);
3075 conn->suffix_match = NULL;
3076 if (suffix_match) {
3077 conn->suffix_match = os_strdup(suffix_match);
3078 if (conn->suffix_match == NULL)
3079 return -1;
3080 }
3081
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003082 os_free(conn->domain_match);
3083 conn->domain_match = NULL;
3084 if (domain_match) {
3085 conn->domain_match = os_strdup(domain_match);
3086 if (conn->domain_match == NULL)
3087 return -1;
3088 }
3089
Hai Shalom021b0b52019-04-10 11:17:58 -07003090 os_free(conn->check_cert_subject);
3091 conn->check_cert_subject = NULL;
3092 if (check_cert_subject) {
3093 conn->check_cert_subject = os_strdup(check_cert_subject);
3094 if (!conn->check_cert_subject)
3095 return -1;
3096 }
3097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 return 0;
3099}
3100
3101
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003102#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003103static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003104{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003105 struct tls_connection *conn = arg;
3106
3107 /*
3108 * This cert_cb() is not really the best location for doing a
3109 * constraint check for the ServerKeyExchange message, but this seems to
3110 * be the only place where the current OpenSSL sequence can be
3111 * terminated cleanly with an TLS alert going out to the server.
3112 */
3113
3114 if (!(conn->flags & TLS_CONN_SUITEB))
3115 return 1;
3116
3117 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3118 if (conn->cipher_suite != 0x9f)
3119 return 1;
3120
3121 if (conn->server_dh_prime_len >= 3072)
3122 return 1;
3123
3124 wpa_printf(MSG_DEBUG,
3125 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3126 conn->server_dh_prime_len);
3127 return 0;
3128}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003129#endif /* CONFIG_SUITEB */
3130
3131
Roshan Pius3a1667e2018-07-03 15:17:14 -07003132static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3133 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003134{
3135 SSL *ssl = conn->ssl;
3136
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003137#ifdef SSL_OP_NO_TICKET
3138 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3139 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003140 else
3141 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003142#endif /* SSL_OP_NO_TICKET */
3143
Sunil Ravia04bd252022-05-02 22:54:18 -07003144#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3145 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3146 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3147#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3148
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003149#ifdef SSL_OP_NO_TLSv1
3150 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3151 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3152 else
3153 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3154#endif /* SSL_OP_NO_TLSv1 */
3155#ifdef SSL_OP_NO_TLSv1_1
3156 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3157 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3158 else
3159 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3160#endif /* SSL_OP_NO_TLSv1_1 */
3161#ifdef SSL_OP_NO_TLSv1_2
3162 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3163 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3164 else
3165 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3166#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003167#ifdef SSL_OP_NO_TLSv1_3
3168 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3169 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3170 else
3171 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3172#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003173#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3174 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3175 TLS_CONN_ENABLE_TLSv1_1 |
3176 TLS_CONN_ENABLE_TLSv1_2)) {
3177 int version = 0;
3178
3179 /* Explicit request to enable TLS versions even if needing to
3180 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003181 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003182 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003183 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3184 version = TLS1_1_VERSION;
3185 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3186 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003187 if (!version) {
3188 wpa_printf(MSG_DEBUG,
3189 "OpenSSL: Invalid TLS version configuration");
3190 return -1;
3191 }
3192
3193 if (SSL_set_min_proto_version(ssl, version) != 1) {
3194 wpa_printf(MSG_DEBUG,
3195 "OpenSSL: Failed to set minimum TLS version");
3196 return -1;
3197 }
3198 }
3199#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003200#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3201 !defined(LIBRESSL_VERSION_NUMBER) && \
3202 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003203 {
3204#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3205 int need_level = 0;
3206#else
3207 int need_level = 1;
3208#endif
3209
3210 if ((flags &
3211 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3212 SSL_get_security_level(ssl) > need_level) {
3213 /*
3214 * Need to drop to security level 1 (or 0 with OpenSSL
3215 * 3.0) to allow TLS versions older than 1.2 to be used
3216 * when explicitly enabled in configuration.
3217 */
3218 SSL_set_security_level(conn->ssl, need_level);
3219 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003220 }
3221#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003222
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003223#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003224#ifdef OPENSSL_IS_BORINGSSL
3225 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003226 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003227#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003228 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3229 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3230
Roshan Pius3a1667e2018-07-03 15:17:14 -07003231 if (openssl_ciphers) {
3232 wpa_printf(MSG_DEBUG,
3233 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3234 openssl_ciphers);
3235 ciphers = openssl_ciphers;
3236 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003237 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3238 wpa_printf(MSG_INFO,
3239 "OpenSSL: Failed to set Suite B ciphers");
3240 return -1;
3241 }
3242 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003243#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003244 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003245#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003246 const char *ciphers =
3247 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003248 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003249
Roshan Pius3a1667e2018-07-03 15:17:14 -07003250 if (openssl_ciphers) {
3251 wpa_printf(MSG_DEBUG,
3252 "OpenSSL: Override ciphers for Suite B: %s",
3253 openssl_ciphers);
3254 ciphers = openssl_ciphers;
3255 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003256 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3257 wpa_printf(MSG_INFO,
3258 "OpenSSL: Failed to set Suite B ciphers");
3259 return -1;
3260 }
3261
Sunil Ravia04bd252022-05-02 22:54:18 -07003262#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3263 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3264 wpa_printf(MSG_INFO,
3265 "OpenSSL: Failed to set Suite B groups");
3266 return -1;
3267 }
3268
3269#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003270 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003271 wpa_printf(MSG_INFO,
3272 "OpenSSL: Failed to set Suite B curves");
3273 return -1;
3274 }
3275
3276 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3277 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3278 EC_KEY_free(ecdh);
3279 wpa_printf(MSG_INFO,
3280 "OpenSSL: Failed to set ECDH parameter");
3281 return -1;
3282 }
3283 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003284#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003285 }
Hai Shalom0f94b7a2023-03-13 13:22:35 -07003286 if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH))
3287#ifdef EAP_TLSV1_3
3288 && (flags & TLS_CONN_DISABLE_TLSv1_3)
3289#endif
3290 ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003291#ifdef OPENSSL_IS_BORINGSSL
3292 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3293
Jimmy Chen916e0a72022-01-11 15:19:46 +08003294 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003295 1) != 1) {
3296 wpa_printf(MSG_INFO,
3297 "OpenSSL: Failed to set Suite B sigalgs");
3298 return -1;
3299 }
3300#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003301 /* ECDSA+SHA384 if need to add EC support here */
3302 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3303 wpa_printf(MSG_INFO,
3304 "OpenSSL: Failed to set Suite B sigalgs");
3305 return -1;
3306 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003307#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003308
3309 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3310 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3311 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3312 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003313
3314#ifdef OPENSSL_IS_BORINGSSL
3315 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3316 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3317 int nid[1] = { NID_secp384r1 };
3318
3319 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3320 wpa_printf(MSG_INFO,
3321 "OpenSSL: Failed to set Suite B curves");
3322 return -1;
3323 }
3324
Jimmy Chen916e0a72022-01-11 15:19:46 +08003325 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003326 1) != 1) {
3327 wpa_printf(MSG_INFO,
3328 "OpenSSL: Failed to set Suite B sigalgs");
3329 return -1;
3330 }
3331 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003332#else /* OPENSSL_IS_BORINGSSL */
3333 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3334 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3335 wpa_printf(MSG_INFO,
3336 "OpenSSL: Failed to set openssl_ciphers '%s'",
3337 openssl_ciphers);
3338 return -1;
3339 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003340#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003341#else /* CONFIG_SUITEB */
3342 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3343 wpa_printf(MSG_INFO,
3344 "OpenSSL: Failed to set openssl_ciphers '%s'",
3345 openssl_ciphers);
3346 return -1;
3347 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003348#endif /* CONFIG_SUITEB */
3349
Hai Shalom81f62d82019-07-22 12:10:00 -07003350 if (flags & TLS_CONN_TEAP_ANON_DH) {
3351#ifndef TEAP_DH_ANON_CS
3352#define TEAP_DH_ANON_CS \
3353 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3354 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3355 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3356 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3357 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3358 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3359 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3360 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3361#endif
3362 static const char *cs = TEAP_DH_ANON_CS;
3363
3364#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3365 !defined(LIBRESSL_VERSION_NUMBER) && \
3366 !defined(OPENSSL_IS_BORINGSSL)
3367 /*
3368 * Need to drop to security level 0 to allow anonymous
3369 * cipher suites for EAP-TEAP.
3370 */
3371 SSL_set_security_level(conn->ssl, 0);
3372#endif
3373
3374 wpa_printf(MSG_DEBUG,
3375 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3376 cs);
3377 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3378 tls_show_errors(MSG_INFO, __func__,
3379 "Cipher suite configuration failed");
3380 return -1;
3381 }
3382 }
3383
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003384 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003385}
3386
3387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003389 int verify_peer, unsigned int flags,
3390 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391{
3392 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003393 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394
3395 if (conn == NULL)
3396 return -1;
3397
Hai Shalom899fcc72020-10-19 14:38:18 -07003398 if (verify_peer == 2) {
3399 conn->ca_cert_verify = 1;
3400 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3401 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3402 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403 conn->ca_cert_verify = 1;
3404 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3405 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3406 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3407 } else {
3408 conn->ca_cert_verify = 0;
3409 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3410 }
3411
Roshan Pius3a1667e2018-07-03 15:17:14 -07003412 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003413 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003414 conn->flags = flags;
3415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416 SSL_set_accept_state(conn->ssl);
3417
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003418 if (data->tls_session_lifetime == 0) {
3419 /*
3420 * Set session id context to a unique value to make sure
3421 * session resumption cannot be used either through session
3422 * caching or TLS ticket extension.
3423 */
3424 counter++;
3425 SSL_set_session_id_context(conn->ssl,
3426 (const unsigned char *) &counter,
3427 sizeof(counter));
3428 } else if (session_ctx) {
3429 SSL_set_session_id_context(conn->ssl, session_ctx,
3430 session_ctx_len);
3431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432
3433 return 0;
3434}
3435
3436
3437static int tls_connection_client_cert(struct tls_connection *conn,
3438 const char *client_cert,
3439 const u8 *client_cert_blob,
3440 size_t client_cert_blob_len)
3441{
3442 if (client_cert == NULL && client_cert_blob == NULL)
3443 return 0;
3444
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003445#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003446#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003447 /*
3448 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003449 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003450 * chain properly.
3451 */
3452 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003453#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003454#endif /* PKCS12_FUNCS */
3455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 if (client_cert_blob &&
3457 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3458 client_cert_blob_len) == 1) {
3459 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3460 "OK");
3461 return 0;
3462 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003463#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 tls_show_errors(MSG_DEBUG, __func__,
3465 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003466#else
3467 BIO *bio;
3468 X509 *x509;
3469
3470 tls_show_errors(MSG_DEBUG, __func__,
3471 "SSL_use_certificate_ASN1 failed");
3472 bio = BIO_new(BIO_s_mem());
3473 if (!bio)
3474 return -1;
3475 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3476 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3477 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3478 X509_free(x509);
3479 BIO_free(bio);
3480 return -1;
3481 }
3482 X509_free(x509);
3483 wpa_printf(MSG_DEBUG,
3484 "OpenSSL: Found PEM encoded certificate from blob");
3485 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3486 wpa_printf(MSG_DEBUG,
3487 "OpenSSL: Added an additional certificate into the chain");
3488 SSL_add0_chain_cert(conn->ssl, x509);
3489 }
3490 BIO_free(bio);
3491 return 0;
3492#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493 }
3494
3495 if (client_cert == NULL)
3496 return -1;
3497
3498#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003499 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3500 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Gabriel Birenff4f8382023-04-06 20:14:39 +00003501 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003503 if (!bio) {
3504 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003505 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003506 // Keystore returns X.509 certificates in PEM encoding
3507 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3508 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003510 BIO_free(bio);
3511 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3512 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003513 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003514 X509_free(x509);
3515 wpa_printf(MSG_DEBUG,
3516 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3517 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003518
Hai Shalom7ad2a872021-08-02 18:56:55 -07003519 // Read additional certificates into the chain
3520 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3521 wpa_printf(MSG_DEBUG,
3522 "OpenSSL: Added an additional certificate into the chain");
3523 // Takes ownership of x509, no need to free it here
3524 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003525 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003526 BIO_free(bio);
3527 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 }
3529#endif /* ANDROID */
3530
3531#ifndef OPENSSL_NO_STDIO
3532 if (SSL_use_certificate_file(conn->ssl, client_cert,
3533 SSL_FILETYPE_ASN1) == 1) {
3534 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3535 " --> OK");
3536 return 0;
3537 }
3538
Hai Shalom021b0b52019-04-10 11:17:58 -07003539#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3540 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003541 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3542 ERR_clear_error();
3543 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3544 " --> OK");
3545 return 0;
3546 }
3547#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 if (SSL_use_certificate_file(conn->ssl, client_cert,
3549 SSL_FILETYPE_PEM) == 1) {
3550 ERR_clear_error();
3551 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3552 " --> OK");
3553 return 0;
3554 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003555#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556
3557 tls_show_errors(MSG_DEBUG, __func__,
3558 "SSL_use_certificate_file failed");
3559#else /* OPENSSL_NO_STDIO */
3560 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3561#endif /* OPENSSL_NO_STDIO */
3562
3563 return -1;
3564}
3565
3566
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003567static int tls_global_client_cert(struct tls_data *data,
3568 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569{
3570#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003571 SSL_CTX *ssl_ctx = data->ssl;
3572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573 if (client_cert == NULL)
3574 return 0;
3575
3576 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3577 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003578 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3580 SSL_FILETYPE_PEM) != 1) {
3581 tls_show_errors(MSG_INFO, __func__,
3582 "Failed to load client certificate");
3583 return -1;
3584 }
3585 return 0;
3586#else /* OPENSSL_NO_STDIO */
3587 if (client_cert == NULL)
3588 return 0;
3589 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3590 return -1;
3591#endif /* OPENSSL_NO_STDIO */
3592}
3593
3594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003596static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597 const char *passwd)
3598{
3599 EVP_PKEY *pkey;
3600 X509 *cert;
3601 STACK_OF(X509) *certs;
3602 int res = 0;
3603 char buf[256];
3604
3605 pkey = NULL;
3606 cert = NULL;
3607 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003608 if (!passwd)
3609 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3611 tls_show_errors(MSG_DEBUG, __func__,
3612 "Failed to parse PKCS12 file");
3613 PKCS12_free(p12);
3614 return -1;
3615 }
3616 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3617
3618 if (cert) {
3619 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3620 sizeof(buf));
3621 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3622 "subject='%s'", buf);
3623 if (ssl) {
3624 if (SSL_use_certificate(ssl, cert) != 1)
3625 res = -1;
3626 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003627 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 res = -1;
3629 }
3630 X509_free(cert);
3631 }
3632
3633 if (pkey) {
3634 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3635 if (ssl) {
3636 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3637 res = -1;
3638 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003639 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 res = -1;
3641 }
3642 EVP_PKEY_free(pkey);
3643 }
3644
3645 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003646#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003647 if (ssl)
3648 SSL_clear_chain_certs(ssl);
3649 else
3650 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003651 while ((cert = sk_X509_pop(certs)) != NULL) {
3652 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3653 sizeof(buf));
3654 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3655 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003656 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3657 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3658 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003659 tls_show_errors(MSG_DEBUG, __func__,
3660 "Failed to add additional certificate");
3661 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003662 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003663 break;
3664 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003665 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003666 }
3667 if (!res) {
3668 /* Try to continue anyway */
3669 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003670 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003671#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003672 if (ssl)
3673 res = SSL_build_cert_chain(
3674 ssl,
3675 SSL_BUILD_CHAIN_FLAG_CHECK |
3676 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3677 else
3678 res = SSL_CTX_build_cert_chain(
3679 data->ssl,
3680 SSL_BUILD_CHAIN_FLAG_CHECK |
3681 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003682 if (!res) {
3683 tls_show_errors(MSG_DEBUG, __func__,
3684 "Failed to build certificate chain");
3685 } else if (res == 2) {
3686 wpa_printf(MSG_DEBUG,
3687 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3688 }
3689#endif /* OPENSSL_IS_BORINGSSL */
3690 /*
3691 * Try to continue regardless of result since it is possible for
3692 * the extra certificates not to be required.
3693 */
3694 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003695#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003696 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 while ((cert = sk_X509_pop(certs)) != NULL) {
3698 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3699 sizeof(buf));
3700 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3701 " from PKCS12: subject='%s'", buf);
3702 /*
3703 * There is no SSL equivalent for the chain cert - so
3704 * always add it to the context...
3705 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003706 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3707 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003708 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709 res = -1;
3710 break;
3711 }
3712 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003713 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003714#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 }
3716
3717 PKCS12_free(p12);
3718
3719 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003720 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721
3722 return res;
3723}
3724#endif /* PKCS12_FUNCS */
3725
3726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003727static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3728 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729{
3730#ifdef PKCS12_FUNCS
3731 FILE *f;
3732 PKCS12 *p12;
3733
3734 f = fopen(private_key, "rb");
3735 if (f == NULL)
3736 return -1;
3737
3738 p12 = d2i_PKCS12_fp(f, NULL);
3739 fclose(f);
3740
3741 if (p12 == NULL) {
3742 tls_show_errors(MSG_INFO, __func__,
3743 "Failed to use PKCS#12 file");
3744 return -1;
3745 }
3746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003747 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748
3749#else /* PKCS12_FUNCS */
3750 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3751 "p12/pfx files");
3752 return -1;
3753#endif /* PKCS12_FUNCS */
3754}
3755
3756
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003757static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 const u8 *blob, size_t len, const char *passwd)
3759{
3760#ifdef PKCS12_FUNCS
3761 PKCS12 *p12;
3762
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003763 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 if (p12 == NULL) {
3765 tls_show_errors(MSG_INFO, __func__,
3766 "Failed to use PKCS#12 blob");
3767 return -1;
3768 }
3769
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003770 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
3772#else /* PKCS12_FUNCS */
3773 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3774 "p12/pfx blobs");
3775 return -1;
3776#endif /* PKCS12_FUNCS */
3777}
3778
3779
3780#ifndef OPENSSL_NO_ENGINE
3781static int tls_engine_get_cert(struct tls_connection *conn,
3782 const char *cert_id,
3783 X509 **cert)
3784{
3785 /* this runs after the private key is loaded so no PIN is required */
3786 struct {
3787 const char *cert_id;
3788 X509 *cert;
3789 } params;
3790 params.cert_id = cert_id;
3791 params.cert = NULL;
3792
3793 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3794 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003795 unsigned long err = ERR_get_error();
3796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3798 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003799 ERR_error_string(err, NULL));
3800 if (tls_is_pin_error(err))
3801 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3803 }
3804 if (!params.cert) {
3805 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3806 " '%s'", cert_id);
3807 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3808 }
3809 *cert = params.cert;
3810 return 0;
3811}
3812#endif /* OPENSSL_NO_ENGINE */
3813
3814
3815static int tls_connection_engine_client_cert(struct tls_connection *conn,
3816 const char *cert_id)
3817{
3818#ifndef OPENSSL_NO_ENGINE
3819 X509 *cert;
3820
3821 if (tls_engine_get_cert(conn, cert_id, &cert))
3822 return -1;
3823
3824 if (!SSL_use_certificate(conn->ssl, cert)) {
3825 tls_show_errors(MSG_ERROR, __func__,
3826 "SSL_use_certificate failed");
3827 X509_free(cert);
3828 return -1;
3829 }
3830 X509_free(cert);
3831 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3832 "OK");
3833 return 0;
3834
3835#else /* OPENSSL_NO_ENGINE */
3836 return -1;
3837#endif /* OPENSSL_NO_ENGINE */
3838}
3839
3840
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003841static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 struct tls_connection *conn,
3843 const char *ca_cert_id)
3844{
3845#ifndef OPENSSL_NO_ENGINE
3846 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003847 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003848 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849
3850 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3851 return -1;
3852
3853 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003854 store = X509_STORE_new();
3855 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3857 "certificate store", __func__);
3858 X509_free(cert);
3859 return -1;
3860 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003861 SSL_CTX_set_cert_store(ssl_ctx, store);
3862 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 unsigned long err = ERR_peek_error();
3864 tls_show_errors(MSG_WARNING, __func__,
3865 "Failed to add CA certificate from engine "
3866 "to certificate store");
3867 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3868 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3869 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3870 " already in hash table error",
3871 __func__);
3872 } else {
3873 X509_free(cert);
3874 return -1;
3875 }
3876 }
3877 X509_free(cert);
3878 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3879 "to certificate store", __func__);
3880 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881 conn->ca_cert_verify = 1;
3882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883 return 0;
3884
3885#else /* OPENSSL_NO_ENGINE */
3886 return -1;
3887#endif /* OPENSSL_NO_ENGINE */
3888}
3889
3890
3891static int tls_connection_engine_private_key(struct tls_connection *conn)
3892{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003893#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3895 tls_show_errors(MSG_ERROR, __func__,
3896 "ENGINE: cannot use private key for TLS");
3897 return -1;
3898 }
3899 if (!SSL_check_private_key(conn->ssl)) {
3900 tls_show_errors(MSG_INFO, __func__,
3901 "Private key failed verification");
3902 return -1;
3903 }
3904 return 0;
3905#else /* OPENSSL_NO_ENGINE */
3906 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3907 "engine support was not compiled in");
3908 return -1;
3909#endif /* OPENSSL_NO_ENGINE */
3910}
3911
3912
Roshan Pius3a1667e2018-07-03 15:17:14 -07003913#ifndef OPENSSL_NO_STDIO
3914static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003915{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003916 if (!password)
3917 return 0;
3918 os_strlcpy(buf, (const char *) password, size);
3919 return os_strlen(buf);
3920}
3921#endif /* OPENSSL_NO_STDIO */
3922
3923
3924static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3925 const char *private_key,
3926 const char *private_key_passwd)
3927{
3928#ifndef OPENSSL_NO_STDIO
3929 BIO *bio;
3930 EVP_PKEY *pkey;
3931 int ret;
3932
3933 /* First try ASN.1 (DER). */
3934 bio = BIO_new_file(private_key, "r");
3935 if (!bio)
3936 return -1;
3937 pkey = d2i_PrivateKey_bio(bio, NULL);
3938 BIO_free(bio);
3939
3940 if (pkey) {
3941 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3942 } else {
3943 /* Try PEM with the provided password. */
3944 bio = BIO_new_file(private_key, "r");
3945 if (!bio)
3946 return -1;
3947 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3948 (void *) private_key_passwd);
3949 BIO_free(bio);
3950 if (!pkey)
3951 return -1;
3952 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3953 /* Clear errors from the previous failed load. */
3954 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003955 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003956
3957 if (ssl)
3958 ret = SSL_use_PrivateKey(ssl, pkey);
3959 else
3960 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3961
3962 EVP_PKEY_free(pkey);
3963 return ret == 1 ? 0 : -1;
3964#else /* OPENSSL_NO_STDIO */
3965 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3966 return -1;
3967#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003968}
3969
3970
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003971static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 struct tls_connection *conn,
3973 const char *private_key,
3974 const char *private_key_passwd,
3975 const u8 *private_key_blob,
3976 size_t private_key_blob_len)
3977{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003978 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 int ok;
3980
3981 if (private_key == NULL && private_key_blob == NULL)
3982 return 0;
3983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 ok = 0;
3985 while (private_key_blob) {
3986 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3987 (u8 *) private_key_blob,
3988 private_key_blob_len) == 1) {
3989 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3990 "ASN1(EVP_PKEY_RSA) --> OK");
3991 ok = 1;
3992 break;
3993 }
3994
3995 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3996 (u8 *) private_key_blob,
3997 private_key_blob_len) == 1) {
3998 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3999 "ASN1(EVP_PKEY_DSA) --> OK");
4000 ok = 1;
4001 break;
4002 }
4003
Hai Shalom899fcc72020-10-19 14:38:18 -07004004#ifndef OPENSSL_NO_EC
4005 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
4006 (u8 *) private_key_blob,
4007 private_key_blob_len) == 1) {
4008 wpa_printf(MSG_DEBUG,
4009 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
4010 ok = 1;
4011 break;
4012 }
4013#endif /* OPENSSL_NO_EC */
4014
Sunil Ravia04bd252022-05-02 22:54:18 -07004015#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4017 (u8 *) private_key_blob,
4018 private_key_blob_len) == 1) {
4019 wpa_printf(MSG_DEBUG, "OpenSSL: "
4020 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4021 ok = 1;
4022 break;
4023 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004024#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025
Hai Shaloma20dcd72022-02-04 13:43:00 -08004026 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4027 private_key_blob_len);
4028 if (bio) {
4029 EVP_PKEY *pkey;
4030
4031 pkey = PEM_read_bio_PrivateKey(
4032 bio, NULL, tls_passwd_cb,
4033 (void *) private_key_passwd);
4034 if (pkey) {
4035 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4036 wpa_printf(MSG_DEBUG,
4037 "OpenSSL: SSL_use_PrivateKey --> OK");
4038 ok = 1;
4039 EVP_PKEY_free(pkey);
4040 BIO_free(bio);
4041 break;
4042 }
4043 EVP_PKEY_free(pkey);
4044 }
4045 BIO_free(bio);
4046 }
4047
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004048 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004049 private_key_blob_len,
4050 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4052 "OK");
4053 ok = 1;
4054 break;
4055 }
4056
4057 break;
4058 }
4059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004061 if (tls_use_private_key_file(data, conn->ssl, private_key,
4062 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 ok = 1;
4064 break;
4065 }
4066
Roshan Pius3a1667e2018-07-03 15:17:14 -07004067 if (tls_read_pkcs12(data, conn->ssl, private_key,
4068 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4070 "--> OK");
4071 ok = 1;
4072 break;
4073 }
4074
4075 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4076 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4077 "access certificate store --> OK");
4078 ok = 1;
4079 break;
4080 }
4081
4082 break;
4083 }
4084
4085 if (!ok) {
4086 tls_show_errors(MSG_INFO, __func__,
4087 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 return -1;
4089 }
4090 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 if (!SSL_check_private_key(conn->ssl)) {
4093 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4094 "verification");
4095 return -1;
4096 }
4097
4098 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4099 return 0;
4100}
4101
4102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004103static int tls_global_private_key(struct tls_data *data,
4104 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 const char *private_key_passwd)
4106{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004107 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108
4109 if (private_key == NULL)
4110 return 0;
4111
Roshan Pius3a1667e2018-07-03 15:17:14 -07004112 if (tls_use_private_key_file(data, NULL, private_key,
4113 private_key_passwd) &&
4114 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 tls_show_errors(MSG_INFO, __func__,
4116 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 ERR_clear_error();
4118 return -1;
4119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4123 tls_show_errors(MSG_INFO, __func__,
4124 "Private key failed verification");
4125 return -1;
4126 }
4127
4128 return 0;
4129}
4130
4131
Sunil Ravia04bd252022-05-02 22:54:18 -07004132#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4133#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004135/* This is needed to replace the deprecated DSA_dup_DH() function */
4136static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4137{
4138 OSSL_PARAM_BLD *bld = NULL;
4139 OSSL_PARAM *params = NULL;
4140 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4141 EVP_PKEY_CTX *ctx = NULL;
4142 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143
Sunil Ravia04bd252022-05-02 22:54:18 -07004144 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4145 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4146 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4147 !(bld = OSSL_PARAM_BLD_new()) ||
4148 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4149 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4150 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4151 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4152 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4153 EVP_PKEY_fromdata_init(ctx) != 1 ||
4154 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4155 params) != 1)
4156 wpa_printf(MSG_INFO,
4157 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158
Sunil Ravia04bd252022-05-02 22:54:18 -07004159 EVP_PKEY_CTX_free(ctx);
4160 OSSL_PARAM_free(params);
4161 OSSL_PARAM_BLD_free(bld);
4162 BN_free(p);
4163 BN_free(q);
4164 BN_free(g);
4165 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166}
Sunil Ravia04bd252022-05-02 22:54:18 -07004167#endif /* !OPENSSL_NO_DSA */
4168#endif /* OPENSSL_NO_DH */
4169#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004171static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172{
4173#ifdef OPENSSL_NO_DH
4174 if (dh_file == NULL)
4175 return 0;
4176 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4177 "dh_file specified");
4178 return -1;
4179#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004180#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4181 SSL_CTX *ssl_ctx = data->ssl;
4182 BIO *bio;
4183 OSSL_DECODER_CTX *ctx = NULL;
4184 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4185 bool dsa = false;
4186
4187 if (!ssl_ctx)
4188 return -1;
4189 if (!dh_file) {
4190 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4191 return 0;
4192 }
4193
4194 bio = BIO_new_file(dh_file, "r");
4195 if (!bio) {
4196 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4197 dh_file, ERR_error_string(ERR_get_error(), NULL));
4198 return -1;
4199 }
4200 ctx = OSSL_DECODER_CTX_new_for_pkey(
4201 &tmpkey, "PEM", NULL, NULL,
4202 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4203 if (!ctx ||
4204 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4205 wpa_printf(MSG_INFO,
4206 "TLS: Failed to decode domain parameters from '%s': %s",
4207 dh_file, ERR_error_string(ERR_get_error(), NULL));
4208 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004209 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004210 return -1;
4211 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004212 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004213 BIO_free(bio);
4214
4215 if (!tmpkey) {
4216 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4217 return -1;
4218 }
4219
4220#ifndef OPENSSL_NO_DSA
4221 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4222 pkey = openssl_dsa_to_dh(tmpkey);
4223 EVP_PKEY_free(tmpkey);
4224 if (!pkey)
4225 return -1;
4226 dsa = true;
4227 }
4228#endif /* !OPENSSL_NO_DSA */
4229 if (!dsa) {
4230 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4231 EVP_PKEY_is_a(tmpkey, "DHX")) {
4232 } else {
4233 wpa_printf(MSG_INFO,
4234 "TLS: No DH parameters found in %s",
4235 dh_file);
4236 EVP_PKEY_free(tmpkey);
4237 return -1;
4238 }
4239 pkey = tmpkey;
4240 tmpkey = NULL;
4241 }
4242
4243 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4244 wpa_printf(MSG_INFO,
4245 "TLS: Failed to set DH params from '%s': %s",
4246 dh_file, ERR_error_string(ERR_get_error(), NULL));
4247 EVP_PKEY_free(pkey);
4248 return -1;
4249 }
4250 return 0;
4251#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004252 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 DH *dh;
4254 BIO *bio;
4255
Sunil Ravia04bd252022-05-02 22:54:18 -07004256 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004258 if (!dh_file) {
4259#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4260 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4261#endif
4262 return 0;
4263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264
4265 bio = BIO_new_file(dh_file, "r");
4266 if (bio == NULL) {
4267 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4268 dh_file, ERR_error_string(ERR_get_error(), NULL));
4269 return -1;
4270 }
4271 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4272 BIO_free(bio);
4273#ifndef OPENSSL_NO_DSA
4274 while (dh == NULL) {
4275 DSA *dsa;
4276 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4277 " trying to parse as DSA params", dh_file,
4278 ERR_error_string(ERR_get_error(), NULL));
4279 bio = BIO_new_file(dh_file, "r");
4280 if (bio == NULL)
4281 break;
4282 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4283 BIO_free(bio);
4284 if (!dsa) {
4285 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4286 "'%s': %s", dh_file,
4287 ERR_error_string(ERR_get_error(), NULL));
4288 break;
4289 }
4290
4291 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4292 dh = DSA_dup_DH(dsa);
4293 DSA_free(dsa);
4294 if (dh == NULL) {
4295 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4296 "params into DH params");
4297 break;
4298 }
4299 break;
4300 }
4301#endif /* !OPENSSL_NO_DSA */
4302 if (dh == NULL) {
4303 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4304 "'%s'", dh_file);
4305 return -1;
4306 }
4307
4308 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4309 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4310 "%s", dh_file,
4311 ERR_error_string(ERR_get_error(), NULL));
4312 DH_free(dh);
4313 return -1;
4314 }
4315 DH_free(dh);
4316 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004317#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318#endif /* OPENSSL_NO_DH */
4319}
4320
4321
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004322int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4323 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324{
4325 SSL *ssl;
4326
4327 if (conn == NULL || keys == NULL)
4328 return -1;
4329 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004330 if (ssl == NULL)
4331 return -1;
4332
4333 os_memset(keys, 0, sizeof(*keys));
4334 keys->client_random = conn->client_random;
4335 keys->client_random_len = SSL_get_client_random(
4336 ssl, conn->client_random, sizeof(conn->client_random));
4337 keys->server_random = conn->server_random;
4338 keys->server_random_len = SSL_get_server_random(
4339 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340
4341 return 0;
4342}
4343
4344
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004345#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004346static int openssl_get_keyblock_size(SSL *ssl)
4347{
Sunil Ravia04bd252022-05-02 22:54:18 -07004348#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004349 const EVP_CIPHER *c;
4350 const EVP_MD *h;
4351 int md_size;
4352
4353 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4354 ssl->read_hash == NULL)
4355 return -1;
4356
4357 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004358 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004359 if (h)
4360 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004361 else if (ssl->s3)
4362 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004363 else
4364 return -1;
4365
4366 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4367 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4368 EVP_CIPHER_iv_length(c));
4369 return 2 * (EVP_CIPHER_key_length(c) +
4370 md_size +
4371 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004372#else
4373 const SSL_CIPHER *ssl_cipher;
4374 int cipher, digest;
4375 const EVP_CIPHER *c;
4376 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004377 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378
4379 ssl_cipher = SSL_get_current_cipher(ssl);
4380 if (!ssl_cipher)
4381 return -1;
4382 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4383 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4384 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4385 cipher, digest);
4386 if (cipher < 0 || digest < 0)
4387 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004388 if (cipher == NID_undef) {
4389 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004390 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004391 }
4392 c = EVP_get_cipherbynid(cipher);
4393 if (!c)
4394 return -1;
4395 enc_key_len = EVP_CIPHER_key_length(c);
4396 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4397 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4398 fixed_iv_len = 4; /* only part of IV from PRF */
4399 else
4400 fixed_iv_len = EVP_CIPHER_iv_length(c);
4401 if (digest == NID_undef) {
4402 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4403 mac_key_len = 0;
4404 } else {
4405 h = EVP_get_digestbynid(digest);
4406 if (!h)
4407 return -1;
4408 mac_key_len = EVP_MD_size(h);
4409 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004410
4411 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004412 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4413 mac_key_len, enc_key_len, fixed_iv_len);
4414 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004416}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004417#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004418
4419
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004420int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004421 const char *label, const u8 *context,
4422 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004423{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004424 if (!conn ||
4425 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004426 os_strlen(label), context, context_len,
4427 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004428 return -1;
4429 return 0;
4430}
4431
4432
4433int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4434 u8 *out, size_t out_len)
4435{
4436#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004437 SSL *ssl;
4438 SSL_SESSION *sess;
4439 u8 *rnd;
4440 int ret = -1;
4441 int skip = 0;
4442 u8 *tmp_out = NULL;
4443 u8 *_out = out;
4444 unsigned char client_random[SSL3_RANDOM_SIZE];
4445 unsigned char server_random[SSL3_RANDOM_SIZE];
4446 unsigned char master_key[64];
4447 size_t master_key_len;
4448 const char *ver;
4449
4450 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004451 * TLS library did not support EAP-FAST key generation, so get the
4452 * needed TLS session parameters and use an internal implementation of
4453 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004454 */
4455
4456 if (conn == NULL)
4457 return -1;
4458 ssl = conn->ssl;
4459 if (ssl == NULL)
4460 return -1;
4461 ver = SSL_get_version(ssl);
4462 sess = SSL_get_session(ssl);
4463 if (!ver || !sess)
4464 return -1;
4465
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004466 skip = openssl_get_keyblock_size(ssl);
4467 if (skip < 0)
4468 return -1;
4469 tmp_out = os_malloc(skip + out_len);
4470 if (!tmp_out)
4471 return -1;
4472 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004473
4474 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4475 if (!rnd) {
4476 os_free(tmp_out);
4477 return -1;
4478 }
4479
4480 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4481 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4482 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4483 sizeof(master_key));
4484
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004485 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4486 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487
4488 if (os_strcmp(ver, "TLSv1.2") == 0) {
4489 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004490 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004491 _out, skip + out_len);
4492 ret = 0;
4493 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004494 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004495 _out, skip + out_len) == 0) {
4496 ret = 0;
4497 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004498 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004499 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004500 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004501 os_memcpy(out, _out + skip, out_len);
4502 bin_clear_free(tmp_out, skip);
4503
4504 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004505#else /* OPENSSL_NEED_EAP_FAST_PRF */
4506 wpa_printf(MSG_ERROR,
4507 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4508 return -1;
4509#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510}
4511
4512
4513static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004514openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515{
Sunil Ravia04bd252022-05-02 22:54:18 -07004516 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 int res;
4518 struct wpabuf *out_data;
4519
4520 /*
4521 * Give TLS handshake data from the server (if available) to OpenSSL
4522 * for processing.
4523 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004524 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4526 < 0) {
4527 tls_show_errors(MSG_INFO, __func__,
4528 "Handshake failed - BIO_write");
4529 return NULL;
4530 }
4531
4532 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004533 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534 res = SSL_accept(conn->ssl);
4535 else
4536 res = SSL_connect(conn->ssl);
4537 if (res != 1) {
4538 int err = SSL_get_error(conn->ssl, res);
4539 if (err == SSL_ERROR_WANT_READ)
4540 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4541 "more data");
4542 else if (err == SSL_ERROR_WANT_WRITE)
4543 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4544 "write");
4545 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004546 unsigned long error = ERR_peek_last_error();
4547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004549
4550 if (context->event_cb &&
4551 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4552 ERR_GET_REASON(error) ==
4553 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4554 context->event_cb(
4555 context->cb_ctx,
4556 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4557 NULL);
4558 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004560 if (!conn->server && !conn->client_hello_generated) {
4561 /* The server would not understand TLS Alert
4562 * before ClientHello, so simply terminate
4563 * handshake on this type of error case caused
4564 * by a likely internal error like no ciphers
4565 * available. */
4566 wpa_printf(MSG_DEBUG,
4567 "OpenSSL: Could not generate ClientHello");
4568 conn->write_alerts++;
4569 return NULL;
4570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 }
4572 }
4573
Roshan Pius3a1667e2018-07-03 15:17:14 -07004574 if (!conn->server && !conn->failed)
4575 conn->client_hello_generated = 1;
4576
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004577#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004578 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004579 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4580 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004581 /*
4582 * This should not be reached since earlier cert_cb should have
4583 * terminated the handshake. Keep this check here for extra
4584 * protection if anything goes wrong with the more low-level
4585 * checks based on having to parse the TLS handshake messages.
4586 */
4587 wpa_printf(MSG_DEBUG,
4588 "OpenSSL: Server DH prime length: %d bits",
4589 conn->server_dh_prime_len);
4590
4591 if (context->event_cb) {
4592 union tls_event_data ev;
4593
4594 os_memset(&ev, 0, sizeof(ev));
4595 ev.alert.is_local = 1;
4596 ev.alert.type = "fatal";
4597 ev.alert.description = "insufficient security";
4598 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4599 }
4600 /*
4601 * Could send a TLS Alert to the server, but for now, simply
4602 * terminate handshake.
4603 */
4604 conn->failed++;
4605 conn->write_alerts++;
4606 return NULL;
4607 }
4608#endif /* CONFIG_SUITEB */
4609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 /* Get the TLS handshake data to be sent to the server */
4611 res = BIO_ctrl_pending(conn->ssl_out);
4612 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4613 out_data = wpabuf_alloc(res);
4614 if (out_data == NULL) {
4615 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4616 "handshake output (%d bytes)", res);
4617 if (BIO_reset(conn->ssl_out) < 0) {
4618 tls_show_errors(MSG_INFO, __func__,
4619 "BIO_reset failed");
4620 }
4621 return NULL;
4622 }
4623 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4624 res);
4625 if (res < 0) {
4626 tls_show_errors(MSG_INFO, __func__,
4627 "Handshake failed - BIO_read");
4628 if (BIO_reset(conn->ssl_out) < 0) {
4629 tls_show_errors(MSG_INFO, __func__,
4630 "BIO_reset failed");
4631 }
4632 wpabuf_free(out_data);
4633 return NULL;
4634 }
4635 wpabuf_put(out_data, res);
4636
4637 return out_data;
4638}
4639
4640
4641static struct wpabuf *
4642openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4643{
4644 struct wpabuf *appl_data;
4645 int res;
4646
4647 appl_data = wpabuf_alloc(max_len + 100);
4648 if (appl_data == NULL)
4649 return NULL;
4650
4651 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4652 wpabuf_size(appl_data));
4653 if (res < 0) {
4654 int err = SSL_get_error(conn->ssl, res);
4655 if (err == SSL_ERROR_WANT_READ ||
4656 err == SSL_ERROR_WANT_WRITE) {
4657 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4658 "included");
4659 } else {
4660 tls_show_errors(MSG_INFO, __func__,
4661 "Failed to read possible "
4662 "Application Data");
4663 }
4664 wpabuf_free(appl_data);
4665 return NULL;
4666 }
4667
4668 wpabuf_put(appl_data, res);
4669 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4670 "message", appl_data);
4671
4672 return appl_data;
4673}
4674
4675
4676static struct wpabuf *
4677openssl_connection_handshake(struct tls_connection *conn,
4678 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004679 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680{
4681 struct wpabuf *out_data;
4682
4683 if (appl_data)
4684 *appl_data = NULL;
4685
Roshan Pius3a1667e2018-07-03 15:17:14 -07004686 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 if (out_data == NULL)
4688 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004689 if (conn->invalid_hb_used) {
4690 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4691 wpabuf_free(out_data);
4692 return NULL;
4693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004695 if (SSL_is_init_finished(conn->ssl)) {
4696 wpa_printf(MSG_DEBUG,
4697 "OpenSSL: Handshake finished - resumed=%d",
4698 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004699 if (conn->server) {
4700 char *buf;
4701 size_t buflen = 2000;
4702
4703 buf = os_malloc(buflen);
4704 if (buf) {
4705 if (SSL_get_shared_ciphers(conn->ssl, buf,
4706 buflen)) {
4707 buf[buflen - 1] = '\0';
4708 wpa_printf(MSG_DEBUG,
4709 "OpenSSL: Shared ciphers: %s",
4710 buf);
4711 }
4712 os_free(buf);
4713 }
4714 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004715 if (appl_data && in_data)
4716 *appl_data = openssl_get_appl_data(conn,
4717 wpabuf_len(in_data));
4718 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719
Jouni Malinen26af48b2014-04-09 13:02:53 +03004720 if (conn->invalid_hb_used) {
4721 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4722 if (appl_data) {
4723 wpabuf_free(*appl_data);
4724 *appl_data = NULL;
4725 }
4726 wpabuf_free(out_data);
4727 return NULL;
4728 }
4729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 return out_data;
4731}
4732
4733
4734struct wpabuf *
4735tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4736 const struct wpabuf *in_data,
4737 struct wpabuf **appl_data)
4738{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004739 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740}
4741
4742
4743struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4744 struct tls_connection *conn,
4745 const struct wpabuf *in_data,
4746 struct wpabuf **appl_data)
4747{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004748 conn->server = 1;
4749 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750}
4751
4752
4753struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4754 struct tls_connection *conn,
4755 const struct wpabuf *in_data)
4756{
4757 int res;
4758 struct wpabuf *buf;
4759
4760 if (conn == NULL)
4761 return NULL;
4762
4763 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4764 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4765 (res = BIO_reset(conn->ssl_out)) < 0) {
4766 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4767 return NULL;
4768 }
4769 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4770 if (res < 0) {
4771 tls_show_errors(MSG_INFO, __func__,
4772 "Encryption failed - SSL_write");
4773 return NULL;
4774 }
4775
4776 /* Read encrypted data to be sent to the server */
4777 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4778 if (buf == NULL)
4779 return NULL;
4780 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4781 if (res < 0) {
4782 tls_show_errors(MSG_INFO, __func__,
4783 "Encryption failed - BIO_read");
4784 wpabuf_free(buf);
4785 return NULL;
4786 }
4787 wpabuf_put(buf, res);
4788
4789 return buf;
4790}
4791
4792
4793struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4794 struct tls_connection *conn,
4795 const struct wpabuf *in_data)
4796{
4797 int res;
4798 struct wpabuf *buf;
4799
4800 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4801 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4802 wpabuf_len(in_data));
4803 if (res < 0) {
4804 tls_show_errors(MSG_INFO, __func__,
4805 "Decryption failed - BIO_write");
4806 return NULL;
4807 }
4808 if (BIO_reset(conn->ssl_out) < 0) {
4809 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4810 return NULL;
4811 }
4812
4813 /* Read decrypted data for further processing */
4814 /*
4815 * Even though we try to disable TLS compression, it is possible that
4816 * this cannot be done with all TLS libraries. Add extra buffer space
4817 * to handle the possibility of the decrypted data being longer than
4818 * input data.
4819 */
4820 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4821 if (buf == NULL)
4822 return NULL;
4823 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4824 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004825 int err = SSL_get_error(conn->ssl, res);
4826
4827 if (err == SSL_ERROR_WANT_READ) {
4828 wpa_printf(MSG_DEBUG,
4829 "SSL: SSL_connect - want more data");
4830 res = 0;
4831 } else {
4832 tls_show_errors(MSG_INFO, __func__,
4833 "Decryption failed - SSL_read");
4834 wpabuf_free(buf);
4835 return NULL;
4836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 }
4838 wpabuf_put(buf, res);
4839
Jouni Malinen26af48b2014-04-09 13:02:53 +03004840 if (conn->invalid_hb_used) {
4841 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4842 wpabuf_free(buf);
4843 return NULL;
4844 }
4845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 return buf;
4847}
4848
4849
4850int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4851{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004852 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853}
4854
4855
4856int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4857 u8 *ciphers)
4858{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004859 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 u8 *c;
4861 int ret;
4862
4863 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4864 return -1;
4865
4866 buf[0] = '\0';
4867 pos = buf;
4868 end = pos + sizeof(buf);
4869
4870 c = ciphers;
4871 while (*c != TLS_CIPHER_NONE) {
4872 const char *suite;
4873
4874 switch (*c) {
4875 case TLS_CIPHER_RC4_SHA:
4876 suite = "RC4-SHA";
4877 break;
4878 case TLS_CIPHER_AES128_SHA:
4879 suite = "AES128-SHA";
4880 break;
4881 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4882 suite = "DHE-RSA-AES128-SHA";
4883 break;
4884 case TLS_CIPHER_ANON_DH_AES128_SHA:
4885 suite = "ADH-AES128-SHA";
4886 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004887 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4888 suite = "DHE-RSA-AES256-SHA";
4889 break;
4890 case TLS_CIPHER_AES256_SHA:
4891 suite = "AES256-SHA";
4892 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 default:
4894 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4895 "cipher selection: %d", *c);
4896 return -1;
4897 }
4898 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004899 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 break;
4901 pos += ret;
4902
4903 c++;
4904 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004905 if (!buf[0]) {
4906 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4907 return -1;
4908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909
4910 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4911
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004912#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004913#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004914 if (os_strstr(buf, ":ADH-")) {
4915 /*
4916 * Need to drop to security level 0 to allow anonymous
4917 * cipher suites for EAP-FAST.
4918 */
4919 SSL_set_security_level(conn->ssl, 0);
4920 } else if (SSL_get_security_level(conn->ssl) == 0) {
4921 /* Force at least security level 1 */
4922 SSL_set_security_level(conn->ssl, 1);
4923 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004924#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004925#endif
4926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4928 tls_show_errors(MSG_INFO, __func__,
4929 "Cipher suite configuration failed");
4930 return -1;
4931 }
4932
4933 return 0;
4934}
4935
4936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004937int tls_get_version(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_version(conn->ssl);
4945 if (name == NULL)
4946 return -1;
4947
4948 os_strlcpy(buf, name, buflen);
4949 return 0;
4950}
4951
4952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4954 char *buf, size_t buflen)
4955{
4956 const char *name;
4957 if (conn == NULL || conn->ssl == NULL)
4958 return -1;
4959
4960 name = SSL_get_cipher(conn->ssl);
4961 if (name == NULL)
4962 return -1;
4963
4964 os_strlcpy(buf, name, buflen);
4965 return 0;
4966}
4967
4968
4969int tls_connection_enable_workaround(void *ssl_ctx,
4970 struct tls_connection *conn)
4971{
4972 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4973
4974 return 0;
4975}
4976
4977
Hai Shalom81f62d82019-07-22 12:10:00 -07004978#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979/* ClientHello TLS extensions require a patch to openssl, so this function is
4980 * commented out unless explicitly needed for EAP-FAST in order to be able to
4981 * build this file with unmodified openssl. */
4982int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4983 int ext_type, const u8 *data,
4984 size_t data_len)
4985{
4986 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4987 return -1;
4988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4990 data_len) != 1)
4991 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992
4993 return 0;
4994}
Hai Shalom81f62d82019-07-22 12:10:00 -07004995#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996
4997
4998int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4999{
5000 if (conn == NULL)
5001 return -1;
5002 return conn->failed;
5003}
5004
5005
5006int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
5007{
5008 if (conn == NULL)
5009 return -1;
5010 return conn->read_alerts;
5011}
5012
5013
5014int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
5015{
5016 if (conn == NULL)
5017 return -1;
5018 return conn->write_alerts;
5019}
5020
5021
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005022#ifdef HAVE_OCSP
5023
5024static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5025{
5026#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005027 BIO *out;
5028 size_t rlen;
5029 char *txt;
5030 int res;
5031
5032 if (wpa_debug_level > MSG_DEBUG)
5033 return;
5034
5035 out = BIO_new(BIO_s_mem());
5036 if (!out)
5037 return;
5038
5039 OCSP_RESPONSE_print(out, rsp, 0);
5040 rlen = BIO_ctrl_pending(out);
5041 txt = os_malloc(rlen + 1);
5042 if (!txt) {
5043 BIO_free(out);
5044 return;
5045 }
5046
5047 res = BIO_read(out, txt, rlen);
5048 if (res > 0) {
5049 txt[res] = '\0';
5050 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5051 }
5052 os_free(txt);
5053 BIO_free(out);
5054#endif /* CONFIG_NO_STDOUT_DEBUG */
5055}
5056
5057
5058static int ocsp_resp_cb(SSL *s, void *arg)
5059{
5060 struct tls_connection *conn = arg;
5061 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005062 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005063 OCSP_RESPONSE *rsp;
5064 OCSP_BASICRESP *basic;
5065 OCSP_CERTID *id;
5066 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005067 X509_STORE *store;
5068 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005069
5070 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5071 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005072#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5073#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5074 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5075 /* TLS 1.3 sends the OCSP response with the server
5076 * Certificate message. Since that Certificate message
5077 * is not sent when resuming a session, there can be no
5078 * new OCSP response. Allow this since the OCSP response
5079 * was validated when checking the initial certificate
5080 * exchange. */
5081 wpa_printf(MSG_DEBUG,
5082 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5083 return 1;
5084 }
5085#endif
5086#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005087 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5088 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5089 }
5090
5091 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5092
5093 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5094 if (!rsp) {
5095 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5096 return 0;
5097 }
5098
5099 ocsp_debug_print_resp(rsp);
5100
5101 status = OCSP_response_status(rsp);
5102 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5103 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5104 status, OCSP_response_status_str(status));
5105 return 0;
5106 }
5107
5108 basic = OCSP_response_get1_basic(rsp);
5109 if (!basic) {
5110 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5111 return 0;
5112 }
5113
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005114 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005115 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005116 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005117
5118 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5119 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005120 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005121 }
5122 certs = sk_X509_new_null();
5123 if (certs) {
5124 X509 *cert;
5125 cert = X509_dup(conn->peer_issuer);
5126 if (cert && !sk_X509_push(certs, cert)) {
5127 tls_show_errors(
5128 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005129 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005130 X509_free(cert);
5131 sk_X509_free(certs);
5132 certs = NULL;
5133 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005134 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005135 cert = X509_dup(conn->peer_issuer_issuer);
5136 if (cert && !sk_X509_push(certs, cert)) {
5137 tls_show_errors(
5138 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005139 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005140 X509_free(cert);
5141 }
5142 }
5143 }
5144 }
5145
5146 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5147 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005148 if (status <= 0) {
5149 tls_show_errors(MSG_INFO, __func__,
5150 "OpenSSL: OCSP response failed verification");
5151 OCSP_BASICRESP_free(basic);
5152 OCSP_RESPONSE_free(rsp);
5153 return 0;
5154 }
5155
5156 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5157
Dmitry Shmidt56052862013-10-04 10:23:25 -07005158 if (!conn->peer_cert) {
5159 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5160 OCSP_BASICRESP_free(basic);
5161 OCSP_RESPONSE_free(rsp);
5162 return 0;
5163 }
5164
5165 if (!conn->peer_issuer) {
5166 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005167 OCSP_BASICRESP_free(basic);
5168 OCSP_RESPONSE_free(rsp);
5169 return 0;
5170 }
5171
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005172 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005173 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005174 wpa_printf(MSG_DEBUG,
5175 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005176 OCSP_BASICRESP_free(basic);
5177 OCSP_RESPONSE_free(rsp);
5178 return 0;
5179 }
5180
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005181 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5182 &this_update, &next_update);
5183 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005184 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005185 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5186 if (!id) {
5187 wpa_printf(MSG_DEBUG,
5188 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5189 OCSP_BASICRESP_free(basic);
5190 OCSP_RESPONSE_free(rsp);
5191 return 0;
5192 }
5193
5194 res = OCSP_resp_find_status(basic, id, &status, &reason,
5195 &produced_at, &this_update,
5196 &next_update);
5197 }
5198
5199 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005200 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5201 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5202 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005203 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005204 OCSP_BASICRESP_free(basic);
5205 OCSP_RESPONSE_free(rsp);
5206 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5207 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005208 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005209
5210 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5211 tls_show_errors(MSG_INFO, __func__,
5212 "OpenSSL: OCSP status times invalid");
5213 OCSP_BASICRESP_free(basic);
5214 OCSP_RESPONSE_free(rsp);
5215 return 0;
5216 }
5217
5218 OCSP_BASICRESP_free(basic);
5219 OCSP_RESPONSE_free(rsp);
5220
5221 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5222 OCSP_cert_status_str(status));
5223
5224 if (status == V_OCSP_CERTSTATUS_GOOD)
5225 return 1;
5226 if (status == V_OCSP_CERTSTATUS_REVOKED)
5227 return 0;
5228 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5229 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5230 return 0;
5231 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005232 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 -07005233 return 1;
5234}
5235
5236
5237static int ocsp_status_cb(SSL *s, void *arg)
5238{
5239 char *tmp;
5240 char *resp;
5241 size_t len;
5242
5243 if (tls_global->ocsp_stapling_response == NULL) {
5244 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5245 return SSL_TLSEXT_ERR_OK;
5246 }
5247
5248 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5249 if (resp == NULL) {
5250 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5251 /* TODO: Build OCSPResponse with responseStatus = internalError
5252 */
5253 return SSL_TLSEXT_ERR_OK;
5254 }
5255 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5256 tmp = OPENSSL_malloc(len);
5257 if (tmp == NULL) {
5258 os_free(resp);
5259 return SSL_TLSEXT_ERR_ALERT_FATAL;
5260 }
5261
5262 os_memcpy(tmp, resp, len);
5263 os_free(resp);
5264 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5265
5266 return SSL_TLSEXT_ERR_OK;
5267}
5268
5269#endif /* HAVE_OCSP */
5270
5271
Hai Shalomfdcde762020-04-02 11:19:20 -07005272static size_t max_str_len(const char **lines)
5273{
5274 const char **p;
5275 size_t max_len = 0;
5276
5277 for (p = lines; *p; p++) {
5278 size_t len = os_strlen(*p);
5279
5280 if (len > max_len)
5281 max_len = len;
5282 }
5283
5284 return max_len;
5285}
5286
5287
5288static int match_lines_in_file(const char *path, const char **lines)
5289{
5290 FILE *f;
5291 char *buf;
5292 size_t bufsize;
5293 int found = 0, is_linestart = 1;
5294
5295 bufsize = max_str_len(lines) + sizeof("\r\n");
5296 buf = os_malloc(bufsize);
5297 if (!buf)
5298 return 0;
5299
5300 f = fopen(path, "r");
5301 if (!f) {
5302 os_free(buf);
5303 return 0;
5304 }
5305
5306 while (!found && fgets(buf, bufsize, f)) {
5307 int is_lineend;
5308 size_t len;
5309 const char **p;
5310
5311 len = strcspn(buf, "\r\n");
5312 is_lineend = buf[len] != '\0';
5313 buf[len] = '\0';
5314
5315 if (is_linestart && is_lineend) {
5316 for (p = lines; !found && *p; p++)
5317 found = os_strcmp(buf, *p) == 0;
5318 }
5319 is_linestart = is_lineend;
5320 }
5321
5322 fclose(f);
5323 bin_clear_free(buf, bufsize);
5324
5325 return found;
5326}
5327
5328
5329static int is_tpm2_key(const char *path)
5330{
5331 /* Check both new and old format of TPM2 PEM guard tag */
5332 static const char *tpm2_tags[] = {
5333 "-----BEGIN TSS2 PRIVATE KEY-----",
5334 "-----BEGIN TSS2 KEY BLOB-----",
5335 NULL
5336 };
5337
5338 return match_lines_in_file(path, tpm2_tags);
5339}
5340
5341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5343 const struct tls_connection_params *params)
5344{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005345 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 int ret;
5347 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005348 int can_pkcs11 = 0;
5349 const char *key_id = params->key_id;
5350 const char *cert_id = params->cert_id;
5351 const char *ca_cert_id = params->ca_cert_id;
5352 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005353 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354
5355 if (conn == NULL)
5356 return -1;
5357
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005358 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5359 wpa_printf(MSG_INFO,
5360 "OpenSSL: ocsp=3 not supported");
5361 return -1;
5362 }
5363
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005364 /*
5365 * If the engine isn't explicitly configured, and any of the
5366 * cert/key fields are actually PKCS#11 URIs, then automatically
5367 * use the PKCS#11 ENGINE.
5368 */
5369 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5370 can_pkcs11 = 1;
5371
5372 if (!key_id && params->private_key && can_pkcs11 &&
5373 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5374 can_pkcs11 = 2;
5375 key_id = params->private_key;
5376 }
5377
5378 if (!cert_id && params->client_cert && can_pkcs11 &&
5379 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5380 can_pkcs11 = 2;
5381 cert_id = params->client_cert;
5382 }
5383
5384 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5385 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5386 can_pkcs11 = 2;
5387 ca_cert_id = params->ca_cert;
5388 }
5389
5390 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5391 if (can_pkcs11 == 2 && !engine_id)
5392 engine_id = "pkcs11";
5393
Hai Shalomfdcde762020-04-02 11:19:20 -07005394 /* If private_key points to a TPM2-wrapped key, automatically enable
5395 * tpm2 engine and use it to unwrap the key. */
5396 if (params->private_key &&
5397 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5398 is_tpm2_key(params->private_key)) {
5399 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5400 params->private_key);
5401 key_id = key_id ? key_id : params->private_key;
5402 engine_id = engine_id ? engine_id : "tpm2";
5403 }
5404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005405#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005406#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005407 if (params->flags & TLS_CONN_EAP_FAST) {
5408 wpa_printf(MSG_DEBUG,
5409 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5410 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5411 tls_show_errors(MSG_INFO, __func__,
5412 "Failed to set TLSv1_method() for EAP-FAST");
5413 return -1;
5414 }
5415 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005416#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005417#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5418#ifdef SSL_OP_NO_TLSv1_3
5419 if (params->flags & TLS_CONN_EAP_FAST) {
5420 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5421 * refuses to start the handshake with the modified ciphersuite
5422 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5423 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5424 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5425 }
5426#endif /* SSL_OP_NO_TLSv1_3 */
5427#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005428#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430 while ((err = ERR_get_error())) {
5431 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5432 __func__, ERR_error_string(err, NULL));
5433 }
5434
Sunil Ravi77d572f2023-01-17 23:58:31 +00005435 if (tls_set_conn_flags(conn, params->flags,
5436 params->openssl_ciphers) < 0) {
5437 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
5438 return -1;
5439 }
5440
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005441 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005442 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5443 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005444 ret = tls_engine_init(conn, engine_id, params->pin,
5445 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446 if (ret)
5447 return ret;
5448 }
5449 if (tls_connection_set_subject_match(conn,
5450 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005451 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005452 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005453 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005454 params->check_cert_subject)) {
5455 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005459 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005460 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005462 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 params->ca_cert_blob,
5464 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005465 params->ca_path)) {
5466 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005470 if (engine_id && cert_id) {
5471 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5473 } else if (tls_connection_client_cert(conn, params->client_cert,
5474 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005475 params->client_cert_blob_len)) {
5476 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005480 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5482 if (tls_connection_engine_private_key(conn))
5483 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005484 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485 params->private_key,
5486 params->private_key_passwd,
5487 params->private_key_blob,
5488 params->private_key_blob_len)) {
5489 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5490 params->private_key);
5491 return -1;
5492 }
5493
Roshan Pius3a1667e2018-07-03 15:17:14 -07005494 ciphers = params->openssl_ciphers;
5495#ifdef CONFIG_SUITEB
5496#ifdef OPENSSL_IS_BORINGSSL
5497 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5498 /* BoringSSL removed support for SUITEB192, so need to handle
5499 * this with hardcoded ciphersuite and additional checks for
5500 * other parameters. */
5501 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5502 }
5503#endif /* OPENSSL_IS_BORINGSSL */
5504#endif /* CONFIG_SUITEB */
5505 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005506 wpa_printf(MSG_INFO,
5507 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005508 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005509 return -1;
5510 }
5511
Hai Shalom74f70d42019-02-11 14:42:39 -08005512 if (!params->openssl_ecdh_curves) {
5513#ifndef OPENSSL_IS_BORINGSSL
5514#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005515#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005516 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5517 wpa_printf(MSG_INFO,
5518 "OpenSSL: Failed to set ECDH curves to auto");
5519 return -1;
5520 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005521#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005522#endif /* OPENSSL_NO_EC */
5523#endif /* OPENSSL_IS_BORINGSSL */
5524 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005525#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005526 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005527 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005528 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005529#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005530#ifndef OPENSSL_NO_EC
5531 if (SSL_set1_curves_list(conn->ssl,
5532 params->openssl_ecdh_curves) != 1) {
5533 wpa_printf(MSG_INFO,
5534 "OpenSSL: Failed to set ECDH curves '%s'",
5535 params->openssl_ecdh_curves);
5536 return -1;
5537 }
5538#else /* OPENSSL_NO_EC */
5539 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5540 return -1;
5541#endif /* OPENSSL_NO_EC */
5542#endif /* OPENSSL_IS_BORINGSSL */
5543 }
5544
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005545#ifdef OPENSSL_IS_BORINGSSL
5546 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5547 SSL_enable_ocsp_stapling(conn->ssl);
5548 }
5549#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005550#ifdef HAVE_OCSP
5551 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005552 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005553 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5554 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5555 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5556 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005557#else /* HAVE_OCSP */
5558 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5559 wpa_printf(MSG_INFO,
5560 "OpenSSL: No OCSP support included - reject configuration");
5561 return -1;
5562 }
5563 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5564 wpa_printf(MSG_DEBUG,
5565 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5566 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005567#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005568#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005569
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005570 conn->flags = params->flags;
5571
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005572 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573
5574 return 0;
5575}
5576
5577
Hai Shalom81f62d82019-07-22 12:10:00 -07005578static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5579{
5580 SSL *ssl;
5581 int i;
5582
5583 ssl = SSL_new(ssl_ctx);
5584 if (!ssl)
5585 return;
5586
5587 wpa_printf(MSG_DEBUG,
5588 "OpenSSL: Enabled cipher suites in priority order");
5589 for (i = 0; ; i++) {
5590 const char *cipher;
5591
5592 cipher = SSL_get_cipher_list(ssl, i);
5593 if (!cipher)
5594 break;
5595 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5596 }
5597
5598 SSL_free(ssl);
5599}
5600
5601
5602#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5603
5604static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5605{
5606 if (!pkey)
5607 return "NULL";
5608 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5609 case EVP_PKEY_RSA:
5610 return "RSA";
5611 case EVP_PKEY_DSA:
5612 return "DSA";
5613 case EVP_PKEY_DH:
5614 return "DH";
5615 case EVP_PKEY_EC:
5616 return "EC";
Sunil Ravi77d572f2023-01-17 23:58:31 +00005617 default:
5618 return "?";
Hai Shalom81f62d82019-07-22 12:10:00 -07005619 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005620}
5621
5622
5623static void openssl_debug_dump_certificate(int i, X509 *cert)
5624{
5625 char buf[256];
5626 EVP_PKEY *pkey;
5627 ASN1_INTEGER *ser;
5628 char serial_num[128];
5629
Hai Shalom60840252021-02-19 19:02:11 -08005630 if (!cert)
5631 return;
5632
Hai Shalom81f62d82019-07-22 12:10:00 -07005633 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5634
5635 ser = X509_get_serialNumber(cert);
5636 if (ser)
5637 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5638 ASN1_STRING_get0_data(ser),
5639 ASN1_STRING_length(ser));
5640 else
5641 serial_num[0] = '\0';
5642
5643 pkey = X509_get_pubkey(cert);
5644 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5645 openssl_pkey_type_str(pkey), serial_num);
5646 EVP_PKEY_free(pkey);
5647}
5648
5649
5650static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5651{
5652 STACK_OF(X509) *certs;
5653
5654 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5655 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5656 int i;
5657
5658 for (i = sk_X509_num(certs); i > 0; i--)
5659 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5660 i - 1));
5661 }
5662 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5663}
5664
5665#endif
5666
5667
5668static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5669{
5670#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5671 int res;
5672
5673 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5674 res == 1;
5675 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5676 openssl_debug_dump_certificates(ssl_ctx);
5677
5678 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5679#endif
5680}
5681
5682
5683static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5684{
5685 openssl_debug_dump_cipher_list(ssl_ctx);
5686 openssl_debug_dump_certificate_chains(ssl_ctx);
5687}
5688
5689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690int tls_global_set_params(void *tls_ctx,
5691 const struct tls_connection_params *params)
5692{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005693 struct tls_data *data = tls_ctx;
5694 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 unsigned long err;
5696
5697 while ((err = ERR_get_error())) {
5698 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5699 __func__, ERR_error_string(err, NULL));
5700 }
5701
Hai Shalom021b0b52019-04-10 11:17:58 -07005702 os_free(data->check_cert_subject);
5703 data->check_cert_subject = NULL;
5704 if (params->check_cert_subject) {
5705 data->check_cert_subject =
5706 os_strdup(params->check_cert_subject);
5707 if (!data->check_cert_subject)
5708 return -1;
5709 }
5710
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005711 if (tls_global_ca_cert(data, params->ca_cert) ||
5712 tls_global_client_cert(data, params->client_cert) ||
5713 tls_global_private_key(data, params->private_key,
5714 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005715 tls_global_client_cert(data, params->client_cert2) ||
5716 tls_global_private_key(data, params->private_key2,
5717 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005718 tls_global_dh(data, params->dh_file)) {
5719 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 return -1;
5721 }
5722
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005723 if (params->openssl_ciphers &&
5724 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5725 wpa_printf(MSG_INFO,
5726 "OpenSSL: Failed to set cipher string '%s'",
5727 params->openssl_ciphers);
5728 return -1;
5729 }
5730
Hai Shalom74f70d42019-02-11 14:42:39 -08005731 if (!params->openssl_ecdh_curves) {
5732#ifndef OPENSSL_IS_BORINGSSL
5733#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005734#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005735 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5736 wpa_printf(MSG_INFO,
5737 "OpenSSL: Failed to set ECDH curves to auto");
5738 return -1;
5739 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005740#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005741#endif /* OPENSSL_NO_EC */
5742#endif /* OPENSSL_IS_BORINGSSL */
5743 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005744#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005745 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005746 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005747 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005748#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005749#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005750#if OPENSSL_VERSION_NUMBER < 0x10100000L
5751 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5752#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005753 if (SSL_CTX_set1_curves_list(ssl_ctx,
5754 params->openssl_ecdh_curves) !=
5755 1) {
5756 wpa_printf(MSG_INFO,
5757 "OpenSSL: Failed to set ECDH curves '%s'",
5758 params->openssl_ecdh_curves);
5759 return -1;
5760 }
5761#else /* OPENSSL_NO_EC */
5762 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5763 return -1;
5764#endif /* OPENSSL_NO_EC */
5765#endif /* OPENSSL_IS_BORINGSSL */
5766 }
5767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005768#ifdef SSL_OP_NO_TICKET
5769 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5770 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5771 else
5772 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5773#endif /* SSL_OP_NO_TICKET */
5774
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005775#ifdef HAVE_OCSP
5776 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5777 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5778 os_free(tls_global->ocsp_stapling_response);
5779 if (params->ocsp_stapling_response)
5780 tls_global->ocsp_stapling_response =
5781 os_strdup(params->ocsp_stapling_response);
5782 else
5783 tls_global->ocsp_stapling_response = NULL;
5784#endif /* HAVE_OCSP */
5785
Hai Shalom81f62d82019-07-22 12:10:00 -07005786 openssl_debug_dump_ctx(ssl_ctx);
5787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 return 0;
5789}
5790
5791
Hai Shalom81f62d82019-07-22 12:10:00 -07005792#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793/* Pre-shared secred requires a patch to openssl, so this function is
5794 * commented out unless explicitly needed for EAP-FAST in order to be able to
5795 * build this file with unmodified openssl. */
5796
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005797#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005798static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5799 STACK_OF(SSL_CIPHER) *peer_ciphers,
5800 const SSL_CIPHER **cipher, void *arg)
5801#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5803 STACK_OF(SSL_CIPHER) *peer_ciphers,
5804 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005805#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005806{
5807 struct tls_connection *conn = arg;
5808 int ret;
5809
Sunil Ravia04bd252022-05-02 22:54:18 -07005810#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005811 if (conn == NULL || conn->session_ticket_cb == NULL)
5812 return 0;
5813
5814 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5815 conn->session_ticket,
5816 conn->session_ticket_len,
5817 s->s3->client_random,
5818 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005819#else
5820 unsigned char client_random[SSL3_RANDOM_SIZE];
5821 unsigned char server_random[SSL3_RANDOM_SIZE];
5822
5823 if (conn == NULL || conn->session_ticket_cb == NULL)
5824 return 0;
5825
5826 SSL_get_client_random(s, client_random, sizeof(client_random));
5827 SSL_get_server_random(s, server_random, sizeof(server_random));
5828
5829 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5830 conn->session_ticket,
5831 conn->session_ticket_len,
5832 client_random,
5833 server_random, secret);
5834#endif
5835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 os_free(conn->session_ticket);
5837 conn->session_ticket = NULL;
5838
5839 if (ret <= 0)
5840 return 0;
5841
5842 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5843 return 1;
5844}
5845
5846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5848 int len, void *arg)
5849{
5850 struct tls_connection *conn = arg;
5851
5852 if (conn == NULL || conn->session_ticket_cb == NULL)
5853 return 0;
5854
5855 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5856
5857 os_free(conn->session_ticket);
5858 conn->session_ticket = NULL;
5859
5860 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5861 "extension", data, len);
5862
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005863 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 if (conn->session_ticket == NULL)
5865 return 0;
5866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867 conn->session_ticket_len = len;
5868
5869 return 1;
5870}
Hai Shalom81f62d82019-07-22 12:10:00 -07005871#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872
5873
5874int tls_connection_set_session_ticket_cb(void *tls_ctx,
5875 struct tls_connection *conn,
5876 tls_session_ticket_cb cb,
5877 void *ctx)
5878{
Hai Shalom81f62d82019-07-22 12:10:00 -07005879#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880 conn->session_ticket_cb = cb;
5881 conn->session_ticket_cb_ctx = ctx;
5882
5883 if (cb) {
5884 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5885 conn) != 1)
5886 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005887 SSL_set_session_ticket_ext_cb(conn->ssl,
5888 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005889 } else {
5890 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5891 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893 }
5894
5895 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005896#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005898#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005900
5901
5902int tls_get_library_version(char *buf, size_t buf_len)
5903{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005904#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005905 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5906 OPENSSL_VERSION_TEXT,
5907 OpenSSL_version(OPENSSL_VERSION));
5908#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005909 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5910 OPENSSL_VERSION_TEXT,
5911 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005912#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005913}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005914
5915
5916void tls_connection_set_success_data(struct tls_connection *conn,
5917 struct wpabuf *data)
5918{
5919 SSL_SESSION *sess;
5920 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005921 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005922
5923 if (tls_ex_idx_session < 0)
5924 goto fail;
5925 sess = SSL_get_session(conn->ssl);
5926 if (!sess)
5927 goto fail;
5928 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5929 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005930 struct tls_session_data *found;
5931
5932 found = get_session_data(conn->context, old);
5933 wpa_printf(MSG_DEBUG,
5934 "OpenSSL: Replacing old success data %p (sess %p)%s",
5935 old, sess, found ? "" : " (not freeing)");
5936 if (found) {
5937 dl_list_del(&found->list);
5938 os_free(found);
5939 wpabuf_free(old);
5940 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005941 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005942
5943 sess_data = os_zalloc(sizeof(*sess_data));
5944 if (!sess_data ||
5945 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005946 goto fail;
5947
Sunil Ravia04bd252022-05-02 22:54:18 -07005948 sess_data->buf = data;
5949 dl_list_add(&conn->context->sessions, &sess_data->list);
5950 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5951 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005952 conn->success_data = 1;
5953 return;
5954
5955fail:
5956 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
5957 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07005958 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005959}
5960
5961
5962void tls_connection_set_success_data_resumed(struct tls_connection *conn)
5963{
5964 wpa_printf(MSG_DEBUG,
5965 "OpenSSL: Success data accepted for resumed session");
5966 conn->success_data = 1;
5967}
5968
5969
5970const struct wpabuf *
5971tls_connection_get_success_data(struct tls_connection *conn)
5972{
5973 SSL_SESSION *sess;
5974
5975 if (tls_ex_idx_session < 0 ||
5976 !(sess = SSL_get_session(conn->ssl)))
5977 return NULL;
5978 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5979}
5980
5981
5982void tls_connection_remove_session(struct tls_connection *conn)
5983{
5984 SSL_SESSION *sess;
5985
5986 sess = SSL_get_session(conn->ssl);
5987 if (!sess)
5988 return;
5989
5990 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5991 wpa_printf(MSG_DEBUG,
5992 "OpenSSL: Session was not cached");
5993 else
5994 wpa_printf(MSG_DEBUG,
5995 "OpenSSL: Removed cached session to disable session resumption");
5996}
Hai Shalom81f62d82019-07-22 12:10:00 -07005997
5998
5999int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
6000{
6001 size_t len;
6002 int reused;
6003
6004 reused = SSL_session_reused(conn->ssl);
6005 if ((conn->server && !reused) || (!conn->server && reused))
6006 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
6007 else
6008 len = SSL_get_finished(conn->ssl, buf, max_len);
6009
6010 if (len == 0 || len > max_len)
6011 return -1;
6012
6013 return len;
6014}
6015
6016
6017u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6018{
6019 const SSL_CIPHER *cipher;
6020
6021 cipher = SSL_get_current_cipher(conn->ssl);
6022 if (!cipher)
6023 return 0;
6024#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6025 return SSL_CIPHER_get_protocol_id(cipher);
6026#else
6027 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6028#endif
6029}
Hai Shalom899fcc72020-10-19 14:38:18 -07006030
6031
6032const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6033{
6034 if (conn)
6035 return conn->peer_subject;
6036 return NULL;
6037}
6038
6039
6040bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6041{
6042 if (conn)
6043 return SSL_get_certificate(conn->ssl) != NULL;
6044 return false;
6045}
Gabriel Birena5bdf372022-12-15 20:54:33 +00006046
6047void tls_register_cert_callback(tls_get_certificate_cb cb)
6048{
6049 certificate_callback_global = cb;
6050}