blob: 01b17b26dde424e01e1aadeb0f4dbe4965b7de11 [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];
2590 addr[0] = wpabuf_head(cert);
2591 len[0] = wpabuf_len(cert);
2592 if (sha256_vector(1, addr, len, hash) < 0 ||
2593 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2594 err_str = "Server certificate mismatch";
2595 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2596 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002597 } else if (!preverify_ok) {
2598 /*
2599 * Certificate matches pinned certificate, allow
2600 * regardless of other problems.
2601 */
2602 wpa_printf(MSG_DEBUG,
2603 "OpenSSL: Ignore validation issues for a pinned server certificate");
2604 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 }
2606 wpabuf_free(cert);
2607 }
2608 }
2609#endif /* CONFIG_SHA256 */
2610
Hai Shalom81f62d82019-07-22 12:10:00 -07002611 openssl_tls_cert_event(conn, err_cert, depth, buf);
2612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 if (!preverify_ok) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002614 if (depth > 0) {
2615 /* Send cert event for the peer certificate so that
2616 * the upper layers get information about it even if
2617 * validation of a CA certificate fails. */
2618 STACK_OF(X509) *chain;
2619
2620 chain = X509_STORE_CTX_get1_chain(x509_ctx);
2621 if (chain && sk_X509_num(chain) > 0) {
2622 char buf2[256];
2623 X509 *cert;
2624
2625 cert = sk_X509_value(chain, 0);
2626 X509_NAME_oneline(X509_get_subject_name(cert),
2627 buf2, sizeof(buf2));
2628
2629 openssl_tls_cert_event(conn, cert, 0, buf2);
2630 }
2631 if (chain)
2632 sk_X509_pop_free(chain, X509_free);
2633 }
2634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2636 " error %d (%s) depth %d for '%s'", err, err_str,
2637 depth, buf);
2638 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2639 err_str, TLS_FAIL_UNSPECIFIED);
2640 return preverify_ok;
2641 }
2642
2643 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2644 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2645 preverify_ok, err, err_str,
2646 conn->ca_cert_verify, depth, buf);
Hai Shalom021b0b52019-04-10 11:17:58 -07002647 check_cert_subject = conn->check_cert_subject;
2648 if (!check_cert_subject)
2649 check_cert_subject = conn->data->check_cert_subject;
2650 if (check_cert_subject) {
2651 if (depth == 0 &&
2652 !tls_match_dn_field(err_cert, check_cert_subject)) {
2653 preverify_ok = 0;
2654 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2655 "Distinguished Name",
2656 TLS_FAIL_DN_MISMATCH);
2657 }
2658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2660 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2661 "match with '%s'", buf, match);
2662 preverify_ok = 0;
2663 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2664 "Subject mismatch",
2665 TLS_FAIL_SUBJECT_MISMATCH);
2666 } else if (depth == 0 && altmatch &&
2667 !tls_match_altsubject(err_cert, altmatch)) {
2668 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2669 "'%s' not found", altmatch);
2670 preverify_ok = 0;
2671 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2672 "AltSubject mismatch",
2673 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002674 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002675 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002676 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2677 suffix_match);
2678 preverify_ok = 0;
2679 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2680 "Domain suffix mismatch",
2681 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002682 } else if (depth == 0 && domain_match &&
2683 !tls_match_suffix(err_cert, domain_match, 1)) {
2684 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2685 domain_match);
2686 preverify_ok = 0;
2687 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2688 "Domain mismatch",
2689 TLS_FAIL_DOMAIN_MISMATCH);
Hai Shalom81f62d82019-07-22 12:10:00 -07002690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691
2692 if (conn->cert_probe && preverify_ok && depth == 0) {
2693 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2694 "on probe-only run");
2695 preverify_ok = 0;
2696 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2697 "Server certificate chain probe",
2698 TLS_FAIL_SERVER_CHAIN_PROBE);
2699 }
2700
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002701#ifdef CONFIG_SUITEB
2702 if (conn->flags & TLS_CONN_SUITEB) {
2703 EVP_PKEY *pk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002704 int len = -1;
2705
2706 pk = X509_get_pubkey(err_cert);
2707 if (pk) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002708 len = EVP_PKEY_bits(pk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002709 EVP_PKEY_free(pk);
2710 }
2711
2712 if (len >= 0) {
2713 wpa_printf(MSG_DEBUG,
2714 "OpenSSL: RSA modulus size: %d bits", len);
2715 if (len < 3072) {
2716 preverify_ok = 0;
2717 openssl_tls_fail_event(
2718 conn, err_cert, err,
2719 depth, buf,
2720 "Insufficient RSA modulus size",
2721 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2722 }
2723 }
2724 }
2725#endif /* CONFIG_SUITEB */
2726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002727#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002728 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2729 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002730 enum ocsp_result res;
2731
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002732 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2733 conn->peer_issuer,
2734 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002735 if (res == OCSP_REVOKED) {
2736 preverify_ok = 0;
2737 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2738 "certificate revoked",
2739 TLS_FAIL_REVOKED);
2740 if (err == X509_V_OK)
2741 X509_STORE_CTX_set_error(
2742 x509_ctx, X509_V_ERR_CERT_REVOKED);
2743 } else if (res != OCSP_GOOD &&
2744 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2745 preverify_ok = 0;
2746 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2747 "bad certificate status response",
2748 TLS_FAIL_UNSPECIFIED);
2749 }
2750 }
2751#endif /* OPENSSL_IS_BORINGSSL */
2752
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002753 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002754 context->event_cb(context->cb_ctx,
2755 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002756
Hai Shalom899fcc72020-10-19 14:38:18 -07002757 if (depth == 0 && preverify_ok) {
2758 os_free(conn->peer_subject);
2759 conn->peer_subject = os_strdup(buf);
2760 }
2761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 return preverify_ok;
2763}
2764
2765
2766#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002767static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002769 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 X509_LOOKUP *lookup;
2771 int ret = 0;
2772
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002773 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 X509_LOOKUP_file());
2775 if (lookup == NULL) {
2776 tls_show_errors(MSG_WARNING, __func__,
2777 "Failed add lookup for X509 store");
2778 return -1;
2779 }
2780
2781 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2782 unsigned long err = ERR_peek_error();
2783 tls_show_errors(MSG_WARNING, __func__,
2784 "Failed load CA in DER format");
2785 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2786 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2787 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2788 "cert already in hash table error",
2789 __func__);
2790 } else
2791 ret = -1;
2792 }
2793
2794 return ret;
2795}
2796#endif /* OPENSSL_NO_STDIO */
2797
2798
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002799static int tls_connection_ca_cert(struct tls_data *data,
2800 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 const char *ca_cert, const u8 *ca_cert_blob,
2802 size_t ca_cert_blob_len, const char *ca_path)
2803{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002804 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002805 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806
2807 /*
2808 * Remove previously configured trusted CA certificates before adding
2809 * new ones.
2810 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002811 store = X509_STORE_new();
2812 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2814 "certificate store", __func__);
2815 return -1;
2816 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002817 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818
2819 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2820 conn->ca_cert_verify = 1;
2821
2822 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2823 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2824 "chain");
2825 conn->cert_probe = 1;
2826 conn->ca_cert_verify = 0;
2827 return 0;
2828 }
2829
2830 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2831#ifdef CONFIG_SHA256
2832 const char *pos = ca_cert + 7;
2833 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2834 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2835 "hash value '%s'", ca_cert);
2836 return -1;
2837 }
2838 pos += 14;
2839 if (os_strlen(pos) != 32 * 2) {
2840 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2841 "hash length in ca_cert '%s'", ca_cert);
2842 return -1;
2843 }
2844 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2845 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2846 "value in ca_cert '%s'", ca_cert);
2847 return -1;
2848 }
2849 conn->server_cert_only = 1;
2850 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2851 "certificate match");
2852 return 0;
2853#else /* CONFIG_SHA256 */
2854 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2855 "cannot validate server certificate hash");
2856 return -1;
2857#endif /* CONFIG_SHA256 */
2858 }
2859
2860 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002861 X509 *cert = d2i_X509(NULL,
2862 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 ca_cert_blob_len);
2864 if (cert == NULL) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002865 BIO *bio = BIO_new_mem_buf(ca_cert_blob,
2866 ca_cert_blob_len);
2867
2868 if (bio) {
2869 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2870 BIO_free(bio);
2871 }
2872
2873 if (!cert) {
2874 tls_show_errors(MSG_WARNING, __func__,
2875 "Failed to parse ca_cert_blob");
2876 return -1;
2877 }
2878
2879 while (ERR_get_error()) {
2880 /* Ignore errors from DER conversion. */
2881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 }
2883
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002884 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2885 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 unsigned long err = ERR_peek_error();
2887 tls_show_errors(MSG_WARNING, __func__,
2888 "Failed to add ca_cert_blob to "
2889 "certificate store");
2890 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2891 ERR_GET_REASON(err) ==
2892 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2893 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2894 "cert already in hash table error",
2895 __func__);
2896 } else {
2897 X509_free(cert);
2898 return -1;
2899 }
2900 }
2901 X509_free(cert);
2902 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2903 "to certificate store", __func__);
2904 return 0;
2905 }
2906
2907#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002908 /* Single alias */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002909 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert,
2910 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002911 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Gabriel Birenff4f8382023-04-06 20:14:39 +00002912 &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002914 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2915 return 0;
2916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002918 /* Multiple aliases separated by space */
Hai Shalom7ad2a872021-08-02 18:56:55 -07002919 if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert,
2920 ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) {
2921 char *aliases = os_strdup(
2922 &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002923 const char *delim = " ";
2924 int rc = 0;
2925 char *savedptr;
2926 char *alias;
2927
2928 if (!aliases)
2929 return -1;
2930 alias = strtok_r(aliases, delim, &savedptr);
2931 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2932 if (tls_add_ca_from_keystore_encoded(
Gabriel Birenff4f8382023-04-06 20:14:39 +00002933 SSL_CTX_get_cert_store(ssl_ctx), alias, conn)) {
Hai Shalom7ad2a872021-08-02 18:56:55 -07002934 wpa_printf(MSG_ERROR,
2935 "OpenSSL: Failed to add ca_cert %s from keystore",
2936 alias);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002937 rc = -1;
2938 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 }
2940 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002941 os_free(aliases);
2942 if (rc)
2943 return rc;
2944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2946 return 0;
2947 }
2948#endif /* ANDROID */
2949
2950#ifdef CONFIG_NATIVE_WINDOWS
2951 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2952 0) {
2953 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2954 "system certificate store");
2955 return 0;
2956 }
2957#endif /* CONFIG_NATIVE_WINDOWS */
2958
2959 if (ca_cert || ca_path) {
2960#ifndef OPENSSL_NO_STDIO
2961 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2962 1) {
2963 tls_show_errors(MSG_WARNING, __func__,
2964 "Failed to load root certificates");
2965 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002966 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2968 "DER format CA certificate",
2969 __func__);
2970 } else
2971 return -1;
2972 } else {
2973 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2974 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002975 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 }
2977#else /* OPENSSL_NO_STDIO */
2978 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2979 __func__);
2980 return -1;
2981#endif /* OPENSSL_NO_STDIO */
2982 } else {
2983 /* No ca_cert configured - do not try to verify server
2984 * certificate */
2985 conn->ca_cert_verify = 0;
2986 }
2987
2988 return 0;
2989}
2990
2991
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002992static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002994 SSL_CTX *ssl_ctx = data->ssl;
2995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 if (ca_cert) {
2997 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2998 {
2999 tls_show_errors(MSG_WARNING, __func__,
3000 "Failed to load root certificates");
3001 return -1;
3002 }
3003
3004 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
3005 "certificate(s) loaded");
3006
3007#ifndef OPENSSL_NO_STDIO
3008 /* Add the same CAs to the client certificate requests */
3009 SSL_CTX_set_client_CA_list(ssl_ctx,
3010 SSL_load_client_CA_file(ca_cert));
3011#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08003012
3013 os_free(data->ca_cert);
3014 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 }
3016
3017 return 0;
3018}
3019
3020
Hai Shalom74f70d42019-02-11 14:42:39 -08003021int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022{
3023 int flags;
3024
3025 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003026 struct tls_data *data = ssl_ctx;
3027 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 if (cs == NULL) {
3029 tls_show_errors(MSG_INFO, __func__, "Failed to get "
3030 "certificate store when enabling "
3031 "check_crl");
3032 return -1;
3033 }
3034 flags = X509_V_FLAG_CRL_CHECK;
3035 if (check_crl == 2)
3036 flags |= X509_V_FLAG_CRL_CHECK_ALL;
3037 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08003038
3039 data->check_crl = check_crl;
3040 data->check_crl_strict = strict;
3041 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 }
3043 return 0;
3044}
3045
3046
3047static int tls_connection_set_subject_match(struct tls_connection *conn,
3048 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003049 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003050 const char *suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07003051 const char *domain_match,
3052 const char *check_cert_subject)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053{
3054 os_free(conn->subject_match);
3055 conn->subject_match = NULL;
3056 if (subject_match) {
3057 conn->subject_match = os_strdup(subject_match);
3058 if (conn->subject_match == NULL)
3059 return -1;
3060 }
3061
3062 os_free(conn->altsubject_match);
3063 conn->altsubject_match = NULL;
3064 if (altsubject_match) {
3065 conn->altsubject_match = os_strdup(altsubject_match);
3066 if (conn->altsubject_match == NULL)
3067 return -1;
3068 }
3069
Dmitry Shmidt051af732013-10-22 13:52:46 -07003070 os_free(conn->suffix_match);
3071 conn->suffix_match = NULL;
3072 if (suffix_match) {
3073 conn->suffix_match = os_strdup(suffix_match);
3074 if (conn->suffix_match == NULL)
3075 return -1;
3076 }
3077
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003078 os_free(conn->domain_match);
3079 conn->domain_match = NULL;
3080 if (domain_match) {
3081 conn->domain_match = os_strdup(domain_match);
3082 if (conn->domain_match == NULL)
3083 return -1;
3084 }
3085
Hai Shalom021b0b52019-04-10 11:17:58 -07003086 os_free(conn->check_cert_subject);
3087 conn->check_cert_subject = NULL;
3088 if (check_cert_subject) {
3089 conn->check_cert_subject = os_strdup(check_cert_subject);
3090 if (!conn->check_cert_subject)
3091 return -1;
3092 }
3093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 return 0;
3095}
3096
3097
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003098#ifdef CONFIG_SUITEB
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003099static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003100{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003101 struct tls_connection *conn = arg;
3102
3103 /*
3104 * This cert_cb() is not really the best location for doing a
3105 * constraint check for the ServerKeyExchange message, but this seems to
3106 * be the only place where the current OpenSSL sequence can be
3107 * terminated cleanly with an TLS alert going out to the server.
3108 */
3109
3110 if (!(conn->flags & TLS_CONN_SUITEB))
3111 return 1;
3112
3113 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
3114 if (conn->cipher_suite != 0x9f)
3115 return 1;
3116
3117 if (conn->server_dh_prime_len >= 3072)
3118 return 1;
3119
3120 wpa_printf(MSG_DEBUG,
3121 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
3122 conn->server_dh_prime_len);
3123 return 0;
3124}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003125#endif /* CONFIG_SUITEB */
3126
3127
Roshan Pius3a1667e2018-07-03 15:17:14 -07003128static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
3129 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003130{
3131 SSL *ssl = conn->ssl;
3132
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003133#ifdef SSL_OP_NO_TICKET
3134 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
3135 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003136 else
3137 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003138#endif /* SSL_OP_NO_TICKET */
3139
Sunil Ravia04bd252022-05-02 22:54:18 -07003140#ifdef SSL_OP_LEGACY_SERVER_CONNECT
3141 if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION)
3142 SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT);
3143#endif /* SSL_OP_LEGACY_SERVER_CONNECT */
3144
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003145#ifdef SSL_OP_NO_TLSv1
3146 if (flags & TLS_CONN_DISABLE_TLSv1_0)
3147 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3148 else
3149 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
3150#endif /* SSL_OP_NO_TLSv1 */
3151#ifdef SSL_OP_NO_TLSv1_1
3152 if (flags & TLS_CONN_DISABLE_TLSv1_1)
3153 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3154 else
3155 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
3156#endif /* SSL_OP_NO_TLSv1_1 */
3157#ifdef SSL_OP_NO_TLSv1_2
3158 if (flags & TLS_CONN_DISABLE_TLSv1_2)
3159 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
3160 else
3161 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
3162#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003163#ifdef SSL_OP_NO_TLSv1_3
3164 if (flags & TLS_CONN_DISABLE_TLSv1_3)
3165 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
3166 else
3167 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
3168#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08003169#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3170 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
3171 TLS_CONN_ENABLE_TLSv1_1 |
3172 TLS_CONN_ENABLE_TLSv1_2)) {
3173 int version = 0;
3174
3175 /* Explicit request to enable TLS versions even if needing to
3176 * override systemwide policies. */
Hai Shalom899fcc72020-10-19 14:38:18 -07003177 if (flags & TLS_CONN_ENABLE_TLSv1_0)
Hai Shalom74f70d42019-02-11 14:42:39 -08003178 version = TLS1_VERSION;
Hai Shalom899fcc72020-10-19 14:38:18 -07003179 else if (flags & TLS_CONN_ENABLE_TLSv1_1)
3180 version = TLS1_1_VERSION;
3181 else if (flags & TLS_CONN_ENABLE_TLSv1_2)
3182 version = TLS1_2_VERSION;
Hai Shalom74f70d42019-02-11 14:42:39 -08003183 if (!version) {
3184 wpa_printf(MSG_DEBUG,
3185 "OpenSSL: Invalid TLS version configuration");
3186 return -1;
3187 }
3188
3189 if (SSL_set_min_proto_version(ssl, version) != 1) {
3190 wpa_printf(MSG_DEBUG,
3191 "OpenSSL: Failed to set minimum TLS version");
3192 return -1;
3193 }
3194 }
3195#endif /* >= 1.1.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003196#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3197 !defined(LIBRESSL_VERSION_NUMBER) && \
3198 !defined(OPENSSL_IS_BORINGSSL)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003199 {
3200#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3201 int need_level = 0;
3202#else
3203 int need_level = 1;
3204#endif
3205
3206 if ((flags &
3207 (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) &&
3208 SSL_get_security_level(ssl) > need_level) {
3209 /*
3210 * Need to drop to security level 1 (or 0 with OpenSSL
3211 * 3.0) to allow TLS versions older than 1.2 to be used
3212 * when explicitly enabled in configuration.
3213 */
3214 SSL_set_security_level(conn->ssl, need_level);
3215 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003216 }
3217#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08003218
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003219#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003220#ifdef OPENSSL_IS_BORINGSSL
3221 /* Start with defaults from BoringSSL */
Jimmy Chen916e0a72022-01-11 15:19:46 +08003222 SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003223#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003224 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
3225 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
3226
Roshan Pius3a1667e2018-07-03 15:17:14 -07003227 if (openssl_ciphers) {
3228 wpa_printf(MSG_DEBUG,
3229 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
3230 openssl_ciphers);
3231 ciphers = openssl_ciphers;
3232 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003233 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3234 wpa_printf(MSG_INFO,
3235 "OpenSSL: Failed to set Suite B ciphers");
3236 return -1;
3237 }
3238 } else if (flags & TLS_CONN_SUITEB) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003239#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003240 EC_KEY *ecdh;
Sunil Ravia04bd252022-05-02 22:54:18 -07003241#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003242 const char *ciphers =
3243 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07003244 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003245
Roshan Pius3a1667e2018-07-03 15:17:14 -07003246 if (openssl_ciphers) {
3247 wpa_printf(MSG_DEBUG,
3248 "OpenSSL: Override ciphers for Suite B: %s",
3249 openssl_ciphers);
3250 ciphers = openssl_ciphers;
3251 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003252 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
3253 wpa_printf(MSG_INFO,
3254 "OpenSSL: Failed to set Suite B ciphers");
3255 return -1;
3256 }
3257
Sunil Ravia04bd252022-05-02 22:54:18 -07003258#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3259 if (SSL_set1_groups(ssl, nid, 1) != 1) {
3260 wpa_printf(MSG_INFO,
3261 "OpenSSL: Failed to set Suite B groups");
3262 return -1;
3263 }
3264
3265#else
Roshan Pius3a1667e2018-07-03 15:17:14 -07003266 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003267 wpa_printf(MSG_INFO,
3268 "OpenSSL: Failed to set Suite B curves");
3269 return -1;
3270 }
3271
3272 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
3273 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
3274 EC_KEY_free(ecdh);
3275 wpa_printf(MSG_INFO,
3276 "OpenSSL: Failed to set ECDH parameter");
3277 return -1;
3278 }
3279 EC_KEY_free(ecdh);
Sunil Ravia04bd252022-05-02 22:54:18 -07003280#endif
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003281 }
Hai Shalom0f94b7a2023-03-13 13:22:35 -07003282 if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH))
3283#ifdef EAP_TLSV1_3
3284 && (flags & TLS_CONN_DISABLE_TLSv1_3)
3285#endif
3286 ) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003287#ifdef OPENSSL_IS_BORINGSSL
3288 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
3289
Jimmy Chen916e0a72022-01-11 15:19:46 +08003290 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003291 1) != 1) {
3292 wpa_printf(MSG_INFO,
3293 "OpenSSL: Failed to set Suite B sigalgs");
3294 return -1;
3295 }
3296#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003297 /* ECDSA+SHA384 if need to add EC support here */
3298 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
3299 wpa_printf(MSG_INFO,
3300 "OpenSSL: Failed to set Suite B sigalgs");
3301 return -1;
3302 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003303#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003304
3305 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
3306 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
3307 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
3308 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003309
3310#ifdef OPENSSL_IS_BORINGSSL
3311 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
3312 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
3313 int nid[1] = { NID_secp384r1 };
3314
3315 if (SSL_set1_curves(ssl, nid, 1) != 1) {
3316 wpa_printf(MSG_INFO,
3317 "OpenSSL: Failed to set Suite B curves");
3318 return -1;
3319 }
3320
Jimmy Chen916e0a72022-01-11 15:19:46 +08003321 if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003322 1) != 1) {
3323 wpa_printf(MSG_INFO,
3324 "OpenSSL: Failed to set Suite B sigalgs");
3325 return -1;
3326 }
3327 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003328#else /* OPENSSL_IS_BORINGSSL */
3329 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
3330 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3331 wpa_printf(MSG_INFO,
3332 "OpenSSL: Failed to set openssl_ciphers '%s'",
3333 openssl_ciphers);
3334 return -1;
3335 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003336#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08003337#else /* CONFIG_SUITEB */
3338 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
3339 wpa_printf(MSG_INFO,
3340 "OpenSSL: Failed to set openssl_ciphers '%s'",
3341 openssl_ciphers);
3342 return -1;
3343 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003344#endif /* CONFIG_SUITEB */
3345
Hai Shalom81f62d82019-07-22 12:10:00 -07003346 if (flags & TLS_CONN_TEAP_ANON_DH) {
3347#ifndef TEAP_DH_ANON_CS
3348#define TEAP_DH_ANON_CS \
3349 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \
3350 "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \
3351 "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \
3352 "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
3353 "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \
3354 "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \
3355 "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \
3356 "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA"
3357#endif
3358 static const char *cs = TEAP_DH_ANON_CS;
3359
3360#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3361 !defined(LIBRESSL_VERSION_NUMBER) && \
3362 !defined(OPENSSL_IS_BORINGSSL)
3363 /*
3364 * Need to drop to security level 0 to allow anonymous
3365 * cipher suites for EAP-TEAP.
3366 */
3367 SSL_set_security_level(conn->ssl, 0);
3368#endif
3369
3370 wpa_printf(MSG_DEBUG,
3371 "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s",
3372 cs);
3373 if (SSL_set_cipher_list(conn->ssl, cs) != 1) {
3374 tls_show_errors(MSG_INFO, __func__,
3375 "Cipher suite configuration failed");
3376 return -1;
3377 }
3378 }
3379
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003380 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003381}
3382
3383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003385 int verify_peer, unsigned int flags,
3386 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387{
3388 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003389 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390
3391 if (conn == NULL)
3392 return -1;
3393
Hai Shalom899fcc72020-10-19 14:38:18 -07003394 if (verify_peer == 2) {
3395 conn->ca_cert_verify = 1;
3396 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3397 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3398 } else if (verify_peer) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 conn->ca_cert_verify = 1;
3400 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
3401 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
3402 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
3403 } else {
3404 conn->ca_cert_verify = 0;
3405 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
3406 }
3407
Roshan Pius3a1667e2018-07-03 15:17:14 -07003408 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003409 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003410 conn->flags = flags;
3411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412 SSL_set_accept_state(conn->ssl);
3413
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003414 if (data->tls_session_lifetime == 0) {
3415 /*
3416 * Set session id context to a unique value to make sure
3417 * session resumption cannot be used either through session
3418 * caching or TLS ticket extension.
3419 */
3420 counter++;
3421 SSL_set_session_id_context(conn->ssl,
3422 (const unsigned char *) &counter,
3423 sizeof(counter));
3424 } else if (session_ctx) {
3425 SSL_set_session_id_context(conn->ssl, session_ctx,
3426 session_ctx_len);
3427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428
3429 return 0;
3430}
3431
3432
3433static int tls_connection_client_cert(struct tls_connection *conn,
3434 const char *client_cert,
3435 const u8 *client_cert_blob,
3436 size_t client_cert_blob_len)
3437{
3438 if (client_cert == NULL && client_cert_blob == NULL)
3439 return 0;
3440
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003441#ifdef PKCS12_FUNCS
Sunil Ravia04bd252022-05-02 22:54:18 -07003442#ifdef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003443 /*
3444 * Clear previously set extra chain certificates, if any, from PKCS#12
Sunil Ravia04bd252022-05-02 22:54:18 -07003445 * processing in tls_parse_pkcs12() to allow LibreSSL to build a new
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003446 * chain properly.
3447 */
3448 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07003449#endif /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003450#endif /* PKCS12_FUNCS */
3451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 if (client_cert_blob &&
3453 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
3454 client_cert_blob_len) == 1) {
3455 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
3456 "OK");
3457 return 0;
3458 } else if (client_cert_blob) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003459#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 tls_show_errors(MSG_DEBUG, __func__,
3461 "SSL_use_certificate_ASN1 failed");
Hai Shalom899fcc72020-10-19 14:38:18 -07003462#else
3463 BIO *bio;
3464 X509 *x509;
3465
3466 tls_show_errors(MSG_DEBUG, __func__,
3467 "SSL_use_certificate_ASN1 failed");
3468 bio = BIO_new(BIO_s_mem());
3469 if (!bio)
3470 return -1;
3471 BIO_write(bio, client_cert_blob, client_cert_blob_len);
3472 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3473 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
3474 X509_free(x509);
3475 BIO_free(bio);
3476 return -1;
3477 }
3478 X509_free(x509);
3479 wpa_printf(MSG_DEBUG,
3480 "OpenSSL: Found PEM encoded certificate from blob");
3481 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3482 wpa_printf(MSG_DEBUG,
3483 "OpenSSL: Added an additional certificate into the chain");
3484 SSL_add0_chain_cert(conn->ssl, x509);
3485 }
3486 BIO_free(bio);
3487 return 0;
3488#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 }
3490
3491 if (client_cert == NULL)
3492 return -1;
3493
3494#ifdef ANDROID
Hai Shalom7ad2a872021-08-02 18:56:55 -07003495 if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert,
3496 ANDROID_KEYSTORE_PREFIX_LEN) == 0) {
Gabriel Birenff4f8382023-04-06 20:14:39 +00003497 BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 X509 *x509 = NULL;
Hai Shalom7ad2a872021-08-02 18:56:55 -07003499 if (!bio) {
3500 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003501 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003502 // Keystore returns X.509 certificates in PEM encoding
3503 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
3504 if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 X509_free(x509);
Hai Shalom7ad2a872021-08-02 18:56:55 -07003506 BIO_free(bio);
3507 wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding");
3508 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003510 X509_free(x509);
3511 wpa_printf(MSG_DEBUG,
3512 "OpenSSL: Found PEM encoded certificate from keystore: %s",
3513 client_cert);
Paul Stewart50772e82017-01-25 13:59:16 -08003514
Hai Shalom7ad2a872021-08-02 18:56:55 -07003515 // Read additional certificates into the chain
3516 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) {
3517 wpa_printf(MSG_DEBUG,
3518 "OpenSSL: Added an additional certificate into the chain");
3519 // Takes ownership of x509, no need to free it here
3520 SSL_add0_chain_cert(conn->ssl, x509);
Paul Stewart50772e82017-01-25 13:59:16 -08003521 }
Hai Shalom7ad2a872021-08-02 18:56:55 -07003522 BIO_free(bio);
3523 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 }
3525#endif /* ANDROID */
3526
3527#ifndef OPENSSL_NO_STDIO
3528 if (SSL_use_certificate_file(conn->ssl, client_cert,
3529 SSL_FILETYPE_ASN1) == 1) {
3530 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
3531 " --> OK");
3532 return 0;
3533 }
3534
Hai Shalom021b0b52019-04-10 11:17:58 -07003535#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
3536 !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
Hai Shalom74f70d42019-02-11 14:42:39 -08003537 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
3538 ERR_clear_error();
3539 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
3540 " --> OK");
3541 return 0;
3542 }
3543#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 if (SSL_use_certificate_file(conn->ssl, client_cert,
3545 SSL_FILETYPE_PEM) == 1) {
3546 ERR_clear_error();
3547 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
3548 " --> OK");
3549 return 0;
3550 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003551#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552
3553 tls_show_errors(MSG_DEBUG, __func__,
3554 "SSL_use_certificate_file failed");
3555#else /* OPENSSL_NO_STDIO */
3556 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3557#endif /* OPENSSL_NO_STDIO */
3558
3559 return -1;
3560}
3561
3562
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003563static int tls_global_client_cert(struct tls_data *data,
3564 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565{
3566#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003567 SSL_CTX *ssl_ctx = data->ssl;
3568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569 if (client_cert == NULL)
3570 return 0;
3571
3572 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3573 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003574 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
3576 SSL_FILETYPE_PEM) != 1) {
3577 tls_show_errors(MSG_INFO, __func__,
3578 "Failed to load client certificate");
3579 return -1;
3580 }
3581 return 0;
3582#else /* OPENSSL_NO_STDIO */
3583 if (client_cert == NULL)
3584 return 0;
3585 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3586 return -1;
3587#endif /* OPENSSL_NO_STDIO */
3588}
3589
3590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003592static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 const char *passwd)
3594{
3595 EVP_PKEY *pkey;
3596 X509 *cert;
3597 STACK_OF(X509) *certs;
3598 int res = 0;
3599 char buf[256];
3600
3601 pkey = NULL;
3602 cert = NULL;
3603 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003604 if (!passwd)
3605 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
3607 tls_show_errors(MSG_DEBUG, __func__,
3608 "Failed to parse PKCS12 file");
3609 PKCS12_free(p12);
3610 return -1;
3611 }
3612 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
3613
3614 if (cert) {
3615 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3616 sizeof(buf));
3617 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
3618 "subject='%s'", buf);
3619 if (ssl) {
3620 if (SSL_use_certificate(ssl, cert) != 1)
3621 res = -1;
3622 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003623 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 res = -1;
3625 }
3626 X509_free(cert);
3627 }
3628
3629 if (pkey) {
3630 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
3631 if (ssl) {
3632 if (SSL_use_PrivateKey(ssl, pkey) != 1)
3633 res = -1;
3634 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003635 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 res = -1;
3637 }
3638 EVP_PKEY_free(pkey);
3639 }
3640
3641 if (certs) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003642#ifndef LIBRESSL_VERSION_NUMBER
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003643 if (ssl)
3644 SSL_clear_chain_certs(ssl);
3645 else
3646 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003647 while ((cert = sk_X509_pop(certs)) != NULL) {
3648 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3649 sizeof(buf));
3650 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3651 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003652 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3653 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3654 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003655 tls_show_errors(MSG_DEBUG, __func__,
3656 "Failed to add additional certificate");
3657 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003658 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003659 break;
3660 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003661 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003662 }
3663 if (!res) {
3664 /* Try to continue anyway */
3665 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003666 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003667#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003668 if (ssl)
3669 res = SSL_build_cert_chain(
3670 ssl,
3671 SSL_BUILD_CHAIN_FLAG_CHECK |
3672 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3673 else
3674 res = SSL_CTX_build_cert_chain(
3675 data->ssl,
3676 SSL_BUILD_CHAIN_FLAG_CHECK |
3677 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003678 if (!res) {
3679 tls_show_errors(MSG_DEBUG, __func__,
3680 "Failed to build certificate chain");
3681 } else if (res == 2) {
3682 wpa_printf(MSG_DEBUG,
3683 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3684 }
3685#endif /* OPENSSL_IS_BORINGSSL */
3686 /*
3687 * Try to continue regardless of result since it is possible for
3688 * the extra certificates not to be required.
3689 */
3690 res = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07003691#else /* LIBRESSL_VERSION_NUMBER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003692 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 while ((cert = sk_X509_pop(certs)) != NULL) {
3694 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3695 sizeof(buf));
3696 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3697 " from PKCS12: subject='%s'", buf);
3698 /*
3699 * There is no SSL equivalent for the chain cert - so
3700 * always add it to the context...
3701 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003702 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3703 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003704 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705 res = -1;
3706 break;
3707 }
3708 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003709 sk_X509_pop_free(certs, X509_free);
Sunil Ravia04bd252022-05-02 22:54:18 -07003710#endif /* LIBRSESSL_VERSION_NUMBER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 }
3712
3713 PKCS12_free(p12);
3714
3715 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003716 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717
3718 return res;
3719}
3720#endif /* PKCS12_FUNCS */
3721
3722
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003723static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3724 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725{
3726#ifdef PKCS12_FUNCS
3727 FILE *f;
3728 PKCS12 *p12;
3729
3730 f = fopen(private_key, "rb");
3731 if (f == NULL)
3732 return -1;
3733
3734 p12 = d2i_PKCS12_fp(f, NULL);
3735 fclose(f);
3736
3737 if (p12 == NULL) {
3738 tls_show_errors(MSG_INFO, __func__,
3739 "Failed to use PKCS#12 file");
3740 return -1;
3741 }
3742
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003743 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744
3745#else /* PKCS12_FUNCS */
3746 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3747 "p12/pfx files");
3748 return -1;
3749#endif /* PKCS12_FUNCS */
3750}
3751
3752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003753static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 const u8 *blob, size_t len, const char *passwd)
3755{
3756#ifdef PKCS12_FUNCS
3757 PKCS12 *p12;
3758
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003759 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 if (p12 == NULL) {
3761 tls_show_errors(MSG_INFO, __func__,
3762 "Failed to use PKCS#12 blob");
3763 return -1;
3764 }
3765
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003766 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767
3768#else /* PKCS12_FUNCS */
3769 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3770 "p12/pfx blobs");
3771 return -1;
3772#endif /* PKCS12_FUNCS */
3773}
3774
3775
3776#ifndef OPENSSL_NO_ENGINE
3777static int tls_engine_get_cert(struct tls_connection *conn,
3778 const char *cert_id,
3779 X509 **cert)
3780{
3781 /* this runs after the private key is loaded so no PIN is required */
3782 struct {
3783 const char *cert_id;
3784 X509 *cert;
3785 } params;
3786 params.cert_id = cert_id;
3787 params.cert = NULL;
3788
3789 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3790 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003791 unsigned long err = ERR_get_error();
3792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3794 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003795 ERR_error_string(err, NULL));
3796 if (tls_is_pin_error(err))
3797 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3799 }
3800 if (!params.cert) {
3801 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3802 " '%s'", cert_id);
3803 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3804 }
3805 *cert = params.cert;
3806 return 0;
3807}
3808#endif /* OPENSSL_NO_ENGINE */
3809
3810
3811static int tls_connection_engine_client_cert(struct tls_connection *conn,
3812 const char *cert_id)
3813{
3814#ifndef OPENSSL_NO_ENGINE
3815 X509 *cert;
3816
3817 if (tls_engine_get_cert(conn, cert_id, &cert))
3818 return -1;
3819
3820 if (!SSL_use_certificate(conn->ssl, cert)) {
3821 tls_show_errors(MSG_ERROR, __func__,
3822 "SSL_use_certificate failed");
3823 X509_free(cert);
3824 return -1;
3825 }
3826 X509_free(cert);
3827 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3828 "OK");
3829 return 0;
3830
3831#else /* OPENSSL_NO_ENGINE */
3832 return -1;
3833#endif /* OPENSSL_NO_ENGINE */
3834}
3835
3836
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003837static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 struct tls_connection *conn,
3839 const char *ca_cert_id)
3840{
3841#ifndef OPENSSL_NO_ENGINE
3842 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003843 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003844 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845
3846 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3847 return -1;
3848
3849 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003850 store = X509_STORE_new();
3851 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3853 "certificate store", __func__);
3854 X509_free(cert);
3855 return -1;
3856 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003857 SSL_CTX_set_cert_store(ssl_ctx, store);
3858 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 unsigned long err = ERR_peek_error();
3860 tls_show_errors(MSG_WARNING, __func__,
3861 "Failed to add CA certificate from engine "
3862 "to certificate store");
3863 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3864 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3865 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3866 " already in hash table error",
3867 __func__);
3868 } else {
3869 X509_free(cert);
3870 return -1;
3871 }
3872 }
3873 X509_free(cert);
3874 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3875 "to certificate store", __func__);
3876 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003877 conn->ca_cert_verify = 1;
3878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 return 0;
3880
3881#else /* OPENSSL_NO_ENGINE */
3882 return -1;
3883#endif /* OPENSSL_NO_ENGINE */
3884}
3885
3886
3887static int tls_connection_engine_private_key(struct tls_connection *conn)
3888{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003889#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3891 tls_show_errors(MSG_ERROR, __func__,
3892 "ENGINE: cannot use private key for TLS");
3893 return -1;
3894 }
3895 if (!SSL_check_private_key(conn->ssl)) {
3896 tls_show_errors(MSG_INFO, __func__,
3897 "Private key failed verification");
3898 return -1;
3899 }
3900 return 0;
3901#else /* OPENSSL_NO_ENGINE */
3902 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3903 "engine support was not compiled in");
3904 return -1;
3905#endif /* OPENSSL_NO_ENGINE */
3906}
3907
3908
Roshan Pius3a1667e2018-07-03 15:17:14 -07003909#ifndef OPENSSL_NO_STDIO
3910static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003911{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003912 if (!password)
3913 return 0;
3914 os_strlcpy(buf, (const char *) password, size);
3915 return os_strlen(buf);
3916}
3917#endif /* OPENSSL_NO_STDIO */
3918
3919
3920static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3921 const char *private_key,
3922 const char *private_key_passwd)
3923{
3924#ifndef OPENSSL_NO_STDIO
3925 BIO *bio;
3926 EVP_PKEY *pkey;
3927 int ret;
3928
3929 /* First try ASN.1 (DER). */
3930 bio = BIO_new_file(private_key, "r");
3931 if (!bio)
3932 return -1;
3933 pkey = d2i_PrivateKey_bio(bio, NULL);
3934 BIO_free(bio);
3935
3936 if (pkey) {
3937 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3938 } else {
3939 /* Try PEM with the provided password. */
3940 bio = BIO_new_file(private_key, "r");
3941 if (!bio)
3942 return -1;
3943 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3944 (void *) private_key_passwd);
3945 BIO_free(bio);
3946 if (!pkey)
3947 return -1;
3948 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3949 /* Clear errors from the previous failed load. */
3950 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003951 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003952
3953 if (ssl)
3954 ret = SSL_use_PrivateKey(ssl, pkey);
3955 else
3956 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3957
3958 EVP_PKEY_free(pkey);
3959 return ret == 1 ? 0 : -1;
3960#else /* OPENSSL_NO_STDIO */
3961 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3962 return -1;
3963#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003964}
3965
3966
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003967static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 struct tls_connection *conn,
3969 const char *private_key,
3970 const char *private_key_passwd,
3971 const u8 *private_key_blob,
3972 size_t private_key_blob_len)
3973{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003974 BIO *bio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975 int ok;
3976
3977 if (private_key == NULL && private_key_blob == NULL)
3978 return 0;
3979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 ok = 0;
3981 while (private_key_blob) {
3982 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3983 (u8 *) private_key_blob,
3984 private_key_blob_len) == 1) {
3985 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3986 "ASN1(EVP_PKEY_RSA) --> OK");
3987 ok = 1;
3988 break;
3989 }
3990
3991 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3992 (u8 *) private_key_blob,
3993 private_key_blob_len) == 1) {
3994 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3995 "ASN1(EVP_PKEY_DSA) --> OK");
3996 ok = 1;
3997 break;
3998 }
3999
Hai Shalom899fcc72020-10-19 14:38:18 -07004000#ifndef OPENSSL_NO_EC
4001 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl,
4002 (u8 *) private_key_blob,
4003 private_key_blob_len) == 1) {
4004 wpa_printf(MSG_DEBUG,
4005 "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK");
4006 ok = 1;
4007 break;
4008 }
4009#endif /* OPENSSL_NO_EC */
4010
Sunil Ravia04bd252022-05-02 22:54:18 -07004011#if OPENSSL_VERSION_NUMBER < 0x30000000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
4013 (u8 *) private_key_blob,
4014 private_key_blob_len) == 1) {
4015 wpa_printf(MSG_DEBUG, "OpenSSL: "
4016 "SSL_use_RSAPrivateKey_ASN1 --> OK");
4017 ok = 1;
4018 break;
4019 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004020#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021
Hai Shaloma20dcd72022-02-04 13:43:00 -08004022 bio = BIO_new_mem_buf((u8 *) private_key_blob,
4023 private_key_blob_len);
4024 if (bio) {
4025 EVP_PKEY *pkey;
4026
4027 pkey = PEM_read_bio_PrivateKey(
4028 bio, NULL, tls_passwd_cb,
4029 (void *) private_key_passwd);
4030 if (pkey) {
4031 if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) {
4032 wpa_printf(MSG_DEBUG,
4033 "OpenSSL: SSL_use_PrivateKey --> OK");
4034 ok = 1;
4035 EVP_PKEY_free(pkey);
4036 BIO_free(bio);
4037 break;
4038 }
4039 EVP_PKEY_free(pkey);
4040 }
4041 BIO_free(bio);
4042 }
4043
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004044 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004045 private_key_blob_len,
4046 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
4048 "OK");
4049 ok = 1;
4050 break;
4051 }
4052
4053 break;
4054 }
4055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004057 if (tls_use_private_key_file(data, conn->ssl, private_key,
4058 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 ok = 1;
4060 break;
4061 }
4062
Roshan Pius3a1667e2018-07-03 15:17:14 -07004063 if (tls_read_pkcs12(data, conn->ssl, private_key,
4064 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
4066 "--> OK");
4067 ok = 1;
4068 break;
4069 }
4070
4071 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
4072 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
4073 "access certificate store --> OK");
4074 ok = 1;
4075 break;
4076 }
4077
4078 break;
4079 }
4080
4081 if (!ok) {
4082 tls_show_errors(MSG_INFO, __func__,
4083 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084 return -1;
4085 }
4086 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 if (!SSL_check_private_key(conn->ssl)) {
4089 tls_show_errors(MSG_INFO, __func__, "Private key failed "
4090 "verification");
4091 return -1;
4092 }
4093
4094 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
4095 return 0;
4096}
4097
4098
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004099static int tls_global_private_key(struct tls_data *data,
4100 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 const char *private_key_passwd)
4102{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004103 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104
4105 if (private_key == NULL)
4106 return 0;
4107
Roshan Pius3a1667e2018-07-03 15:17:14 -07004108 if (tls_use_private_key_file(data, NULL, private_key,
4109 private_key_passwd) &&
4110 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 tls_show_errors(MSG_INFO, __func__,
4112 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113 ERR_clear_error();
4114 return -1;
4115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 if (!SSL_CTX_check_private_key(ssl_ctx)) {
4119 tls_show_errors(MSG_INFO, __func__,
4120 "Private key failed verification");
4121 return -1;
4122 }
4123
4124 return 0;
4125}
4126
4127
Sunil Ravia04bd252022-05-02 22:54:18 -07004128#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4129#ifndef OPENSSL_NO_DH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130#ifndef OPENSSL_NO_DSA
Sunil Ravia04bd252022-05-02 22:54:18 -07004131/* This is needed to replace the deprecated DSA_dup_DH() function */
4132static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa)
4133{
4134 OSSL_PARAM_BLD *bld = NULL;
4135 OSSL_PARAM *params = NULL;
4136 BIGNUM *p = NULL, *q = NULL, *g = NULL;
4137 EVP_PKEY_CTX *ctx = NULL;
4138 EVP_PKEY *pkey = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139
Sunil Ravia04bd252022-05-02 22:54:18 -07004140 if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) ||
4141 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) ||
4142 !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) ||
4143 !(bld = OSSL_PARAM_BLD_new()) ||
4144 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) ||
4145 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) ||
4146 !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) ||
4147 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
4148 !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) ||
4149 EVP_PKEY_fromdata_init(ctx) != 1 ||
4150 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS,
4151 params) != 1)
4152 wpa_printf(MSG_INFO,
4153 "TLS: Failed to convert DSA parameters to DH parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154
Sunil Ravia04bd252022-05-02 22:54:18 -07004155 EVP_PKEY_CTX_free(ctx);
4156 OSSL_PARAM_free(params);
4157 OSSL_PARAM_BLD_free(bld);
4158 BN_free(p);
4159 BN_free(q);
4160 BN_free(g);
4161 return pkey;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162}
Sunil Ravia04bd252022-05-02 22:54:18 -07004163#endif /* !OPENSSL_NO_DSA */
4164#endif /* OPENSSL_NO_DH */
4165#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004167static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168{
4169#ifdef OPENSSL_NO_DH
4170 if (dh_file == NULL)
4171 return 0;
4172 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
4173 "dh_file specified");
4174 return -1;
4175#else /* OPENSSL_NO_DH */
Sunil Ravia04bd252022-05-02 22:54:18 -07004176#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4177 SSL_CTX *ssl_ctx = data->ssl;
4178 BIO *bio;
4179 OSSL_DECODER_CTX *ctx = NULL;
4180 EVP_PKEY *pkey = NULL, *tmpkey = NULL;
4181 bool dsa = false;
4182
4183 if (!ssl_ctx)
4184 return -1;
4185 if (!dh_file) {
4186 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4187 return 0;
4188 }
4189
4190 bio = BIO_new_file(dh_file, "r");
4191 if (!bio) {
4192 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4193 dh_file, ERR_error_string(ERR_get_error(), NULL));
4194 return -1;
4195 }
4196 ctx = OSSL_DECODER_CTX_new_for_pkey(
4197 &tmpkey, "PEM", NULL, NULL,
4198 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);
4199 if (!ctx ||
4200 OSSL_DECODER_from_bio(ctx, bio) != 1) {
4201 wpa_printf(MSG_INFO,
4202 "TLS: Failed to decode domain parameters from '%s': %s",
4203 dh_file, ERR_error_string(ERR_get_error(), NULL));
4204 BIO_free(bio);
Sunil8cd6f4d2022-06-28 18:40:46 +00004205 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004206 return -1;
4207 }
Sunil8cd6f4d2022-06-28 18:40:46 +00004208 OSSL_DECODER_CTX_free(ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -07004209 BIO_free(bio);
4210
4211 if (!tmpkey) {
4212 wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters");
4213 return -1;
4214 }
4215
4216#ifndef OPENSSL_NO_DSA
4217 if (EVP_PKEY_is_a(tmpkey, "DSA")) {
4218 pkey = openssl_dsa_to_dh(tmpkey);
4219 EVP_PKEY_free(tmpkey);
4220 if (!pkey)
4221 return -1;
4222 dsa = true;
4223 }
4224#endif /* !OPENSSL_NO_DSA */
4225 if (!dsa) {
4226 if (EVP_PKEY_is_a(tmpkey, "DH") ||
4227 EVP_PKEY_is_a(tmpkey, "DHX")) {
4228 } else {
4229 wpa_printf(MSG_INFO,
4230 "TLS: No DH parameters found in %s",
4231 dh_file);
4232 EVP_PKEY_free(tmpkey);
4233 return -1;
4234 }
4235 pkey = tmpkey;
4236 tmpkey = NULL;
4237 }
4238
4239 if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) {
4240 wpa_printf(MSG_INFO,
4241 "TLS: Failed to set DH params from '%s': %s",
4242 dh_file, ERR_error_string(ERR_get_error(), NULL));
4243 EVP_PKEY_free(pkey);
4244 return -1;
4245 }
4246 return 0;
4247#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004248 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 DH *dh;
4250 BIO *bio;
4251
Sunil Ravia04bd252022-05-02 22:54:18 -07004252 if (!ssl_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004254 if (!dh_file) {
4255#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
4256 SSL_CTX_set_dh_auto(ssl_ctx, 1);
4257#endif
4258 return 0;
4259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260
4261 bio = BIO_new_file(dh_file, "r");
4262 if (bio == NULL) {
4263 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
4264 dh_file, ERR_error_string(ERR_get_error(), NULL));
4265 return -1;
4266 }
4267 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4268 BIO_free(bio);
4269#ifndef OPENSSL_NO_DSA
4270 while (dh == NULL) {
4271 DSA *dsa;
4272 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
4273 " trying to parse as DSA params", dh_file,
4274 ERR_error_string(ERR_get_error(), NULL));
4275 bio = BIO_new_file(dh_file, "r");
4276 if (bio == NULL)
4277 break;
4278 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
4279 BIO_free(bio);
4280 if (!dsa) {
4281 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
4282 "'%s': %s", dh_file,
4283 ERR_error_string(ERR_get_error(), NULL));
4284 break;
4285 }
4286
4287 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
4288 dh = DSA_dup_DH(dsa);
4289 DSA_free(dsa);
4290 if (dh == NULL) {
4291 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
4292 "params into DH params");
4293 break;
4294 }
4295 break;
4296 }
4297#endif /* !OPENSSL_NO_DSA */
4298 if (dh == NULL) {
4299 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
4300 "'%s'", dh_file);
4301 return -1;
4302 }
4303
4304 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
4305 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
4306 "%s", dh_file,
4307 ERR_error_string(ERR_get_error(), NULL));
4308 DH_free(dh);
4309 return -1;
4310 }
4311 DH_free(dh);
4312 return 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07004313#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314#endif /* OPENSSL_NO_DH */
4315}
4316
4317
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004318int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
4319 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320{
4321 SSL *ssl;
4322
4323 if (conn == NULL || keys == NULL)
4324 return -1;
4325 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004326 if (ssl == NULL)
4327 return -1;
4328
4329 os_memset(keys, 0, sizeof(*keys));
4330 keys->client_random = conn->client_random;
4331 keys->client_random_len = SSL_get_client_random(
4332 ssl, conn->client_random, sizeof(conn->client_random));
4333 keys->server_random = conn->server_random;
4334 keys->server_random_len = SSL_get_server_random(
4335 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336
4337 return 0;
4338}
4339
4340
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004341#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004342static int openssl_get_keyblock_size(SSL *ssl)
4343{
Sunil Ravia04bd252022-05-02 22:54:18 -07004344#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004345 const EVP_CIPHER *c;
4346 const EVP_MD *h;
4347 int md_size;
4348
4349 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
4350 ssl->read_hash == NULL)
4351 return -1;
4352
4353 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004354 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004355 if (h)
4356 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004357 else if (ssl->s3)
4358 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004359 else
4360 return -1;
4361
4362 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
4363 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
4364 EVP_CIPHER_iv_length(c));
4365 return 2 * (EVP_CIPHER_key_length(c) +
4366 md_size +
4367 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004368#else
4369 const SSL_CIPHER *ssl_cipher;
4370 int cipher, digest;
4371 const EVP_CIPHER *c;
4372 const EVP_MD *h;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004373 int mac_key_len, enc_key_len, fixed_iv_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004374
4375 ssl_cipher = SSL_get_current_cipher(ssl);
4376 if (!ssl_cipher)
4377 return -1;
4378 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
4379 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
4380 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
4381 cipher, digest);
4382 if (cipher < 0 || digest < 0)
4383 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004384 if (cipher == NID_undef) {
4385 wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004386 return -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004387 }
4388 c = EVP_get_cipherbynid(cipher);
4389 if (!c)
4390 return -1;
4391 enc_key_len = EVP_CIPHER_key_length(c);
4392 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
4393 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
4394 fixed_iv_len = 4; /* only part of IV from PRF */
4395 else
4396 fixed_iv_len = EVP_CIPHER_iv_length(c);
4397 if (digest == NID_undef) {
4398 wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)");
4399 mac_key_len = 0;
4400 } else {
4401 h = EVP_get_digestbynid(digest);
4402 if (!h)
4403 return -1;
4404 mac_key_len = EVP_MD_size(h);
4405 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406
4407 wpa_printf(MSG_DEBUG,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004408 "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d",
4409 mac_key_len, enc_key_len, fixed_iv_len);
4410 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004411#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004412}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004413#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004414
4415
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004416int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
Hai Shalom021b0b52019-04-10 11:17:58 -07004417 const char *label, const u8 *context,
4418 size_t context_len, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004419{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004420 if (!conn ||
4421 SSL_export_keying_material(conn->ssl, out, out_len, label,
Hai Shalom021b0b52019-04-10 11:17:58 -07004422 os_strlen(label), context, context_len,
4423 context != NULL) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004424 return -1;
4425 return 0;
4426}
4427
4428
4429int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
4430 u8 *out, size_t out_len)
4431{
4432#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004433 SSL *ssl;
4434 SSL_SESSION *sess;
4435 u8 *rnd;
4436 int ret = -1;
4437 int skip = 0;
4438 u8 *tmp_out = NULL;
4439 u8 *_out = out;
4440 unsigned char client_random[SSL3_RANDOM_SIZE];
4441 unsigned char server_random[SSL3_RANDOM_SIZE];
4442 unsigned char master_key[64];
4443 size_t master_key_len;
4444 const char *ver;
4445
4446 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004447 * TLS library did not support EAP-FAST key generation, so get the
4448 * needed TLS session parameters and use an internal implementation of
4449 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004450 */
4451
4452 if (conn == NULL)
4453 return -1;
4454 ssl = conn->ssl;
4455 if (ssl == NULL)
4456 return -1;
4457 ver = SSL_get_version(ssl);
4458 sess = SSL_get_session(ssl);
4459 if (!ver || !sess)
4460 return -1;
4461
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004462 skip = openssl_get_keyblock_size(ssl);
4463 if (skip < 0)
4464 return -1;
4465 tmp_out = os_malloc(skip + out_len);
4466 if (!tmp_out)
4467 return -1;
4468 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004469
4470 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
4471 if (!rnd) {
4472 os_free(tmp_out);
4473 return -1;
4474 }
4475
4476 SSL_get_client_random(ssl, client_random, sizeof(client_random));
4477 SSL_get_server_random(ssl, server_random, sizeof(server_random));
4478 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
4479 sizeof(master_key));
4480
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004481 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
4482 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004483
4484 if (os_strcmp(ver, "TLSv1.2") == 0) {
4485 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004486 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487 _out, skip + out_len);
4488 ret = 0;
4489 } else if (tls_prf_sha1_md5(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) == 0) {
4492 ret = 0;
4493 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004494 forced_memzero(master_key, sizeof(master_key));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004495 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004496 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004497 os_memcpy(out, _out + skip, out_len);
4498 bin_clear_free(tmp_out, skip);
4499
4500 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004501#else /* OPENSSL_NEED_EAP_FAST_PRF */
4502 wpa_printf(MSG_ERROR,
4503 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
4504 return -1;
4505#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506}
4507
4508
4509static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07004510openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511{
Sunil Ravia04bd252022-05-02 22:54:18 -07004512 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 int res;
4514 struct wpabuf *out_data;
4515
4516 /*
4517 * Give TLS handshake data from the server (if available) to OpenSSL
4518 * for processing.
4519 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004520 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
4522 < 0) {
4523 tls_show_errors(MSG_INFO, __func__,
4524 "Handshake failed - BIO_write");
4525 return NULL;
4526 }
4527
4528 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004529 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 res = SSL_accept(conn->ssl);
4531 else
4532 res = SSL_connect(conn->ssl);
4533 if (res != 1) {
4534 int err = SSL_get_error(conn->ssl, res);
4535 if (err == SSL_ERROR_WANT_READ)
4536 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
4537 "more data");
4538 else if (err == SSL_ERROR_WANT_WRITE)
4539 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
4540 "write");
4541 else {
Sunil Ravia04bd252022-05-02 22:54:18 -07004542 unsigned long error = ERR_peek_last_error();
4543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
Sunil Ravia04bd252022-05-02 22:54:18 -07004545
4546 if (context->event_cb &&
4547 ERR_GET_LIB(error) == ERR_LIB_SSL &&
4548 ERR_GET_REASON(error) ==
4549 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) {
4550 context->event_cb(
4551 context->cb_ctx,
4552 TLS_UNSAFE_RENEGOTIATION_DISABLED,
4553 NULL);
4554 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004556 if (!conn->server && !conn->client_hello_generated) {
4557 /* The server would not understand TLS Alert
4558 * before ClientHello, so simply terminate
4559 * handshake on this type of error case caused
4560 * by a likely internal error like no ciphers
4561 * available. */
4562 wpa_printf(MSG_DEBUG,
4563 "OpenSSL: Could not generate ClientHello");
4564 conn->write_alerts++;
4565 return NULL;
4566 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 }
4568 }
4569
Roshan Pius3a1667e2018-07-03 15:17:14 -07004570 if (!conn->server && !conn->failed)
4571 conn->client_hello_generated = 1;
4572
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004573#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07004574 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004575 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
4576 conn->server_dh_prime_len < 3072) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004577 /*
4578 * This should not be reached since earlier cert_cb should have
4579 * terminated the handshake. Keep this check here for extra
4580 * protection if anything goes wrong with the more low-level
4581 * checks based on having to parse the TLS handshake messages.
4582 */
4583 wpa_printf(MSG_DEBUG,
4584 "OpenSSL: Server DH prime length: %d bits",
4585 conn->server_dh_prime_len);
4586
4587 if (context->event_cb) {
4588 union tls_event_data ev;
4589
4590 os_memset(&ev, 0, sizeof(ev));
4591 ev.alert.is_local = 1;
4592 ev.alert.type = "fatal";
4593 ev.alert.description = "insufficient security";
4594 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
4595 }
4596 /*
4597 * Could send a TLS Alert to the server, but for now, simply
4598 * terminate handshake.
4599 */
4600 conn->failed++;
4601 conn->write_alerts++;
4602 return NULL;
4603 }
4604#endif /* CONFIG_SUITEB */
4605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606 /* Get the TLS handshake data to be sent to the server */
4607 res = BIO_ctrl_pending(conn->ssl_out);
4608 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
4609 out_data = wpabuf_alloc(res);
4610 if (out_data == NULL) {
4611 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
4612 "handshake output (%d bytes)", res);
4613 if (BIO_reset(conn->ssl_out) < 0) {
4614 tls_show_errors(MSG_INFO, __func__,
4615 "BIO_reset failed");
4616 }
4617 return NULL;
4618 }
4619 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
4620 res);
4621 if (res < 0) {
4622 tls_show_errors(MSG_INFO, __func__,
4623 "Handshake failed - BIO_read");
4624 if (BIO_reset(conn->ssl_out) < 0) {
4625 tls_show_errors(MSG_INFO, __func__,
4626 "BIO_reset failed");
4627 }
4628 wpabuf_free(out_data);
4629 return NULL;
4630 }
4631 wpabuf_put(out_data, res);
4632
4633 return out_data;
4634}
4635
4636
4637static struct wpabuf *
4638openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
4639{
4640 struct wpabuf *appl_data;
4641 int res;
4642
4643 appl_data = wpabuf_alloc(max_len + 100);
4644 if (appl_data == NULL)
4645 return NULL;
4646
4647 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
4648 wpabuf_size(appl_data));
4649 if (res < 0) {
4650 int err = SSL_get_error(conn->ssl, res);
4651 if (err == SSL_ERROR_WANT_READ ||
4652 err == SSL_ERROR_WANT_WRITE) {
4653 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
4654 "included");
4655 } else {
4656 tls_show_errors(MSG_INFO, __func__,
4657 "Failed to read possible "
4658 "Application Data");
4659 }
4660 wpabuf_free(appl_data);
4661 return NULL;
4662 }
4663
4664 wpabuf_put(appl_data, res);
4665 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
4666 "message", appl_data);
4667
4668 return appl_data;
4669}
4670
4671
4672static struct wpabuf *
4673openssl_connection_handshake(struct tls_connection *conn,
4674 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004675 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676{
4677 struct wpabuf *out_data;
4678
4679 if (appl_data)
4680 *appl_data = NULL;
4681
Roshan Pius3a1667e2018-07-03 15:17:14 -07004682 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 if (out_data == NULL)
4684 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03004685 if (conn->invalid_hb_used) {
4686 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4687 wpabuf_free(out_data);
4688 return NULL;
4689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004691 if (SSL_is_init_finished(conn->ssl)) {
4692 wpa_printf(MSG_DEBUG,
4693 "OpenSSL: Handshake finished - resumed=%d",
4694 tls_connection_resumed(conn->ssl_ctx, conn));
Hai Shalom81f62d82019-07-22 12:10:00 -07004695 if (conn->server) {
4696 char *buf;
4697 size_t buflen = 2000;
4698
4699 buf = os_malloc(buflen);
4700 if (buf) {
4701 if (SSL_get_shared_ciphers(conn->ssl, buf,
4702 buflen)) {
4703 buf[buflen - 1] = '\0';
4704 wpa_printf(MSG_DEBUG,
4705 "OpenSSL: Shared ciphers: %s",
4706 buf);
4707 }
4708 os_free(buf);
4709 }
4710 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004711 if (appl_data && in_data)
4712 *appl_data = openssl_get_appl_data(conn,
4713 wpabuf_len(in_data));
4714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715
Jouni Malinen26af48b2014-04-09 13:02:53 +03004716 if (conn->invalid_hb_used) {
4717 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4718 if (appl_data) {
4719 wpabuf_free(*appl_data);
4720 *appl_data = NULL;
4721 }
4722 wpabuf_free(out_data);
4723 return NULL;
4724 }
4725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 return out_data;
4727}
4728
4729
4730struct wpabuf *
4731tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
4732 const struct wpabuf *in_data,
4733 struct wpabuf **appl_data)
4734{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004735 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736}
4737
4738
4739struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
4740 struct tls_connection *conn,
4741 const struct wpabuf *in_data,
4742 struct wpabuf **appl_data)
4743{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004744 conn->server = 1;
4745 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746}
4747
4748
4749struct wpabuf * tls_connection_encrypt(void *tls_ctx,
4750 struct tls_connection *conn,
4751 const struct wpabuf *in_data)
4752{
4753 int res;
4754 struct wpabuf *buf;
4755
4756 if (conn == NULL)
4757 return NULL;
4758
4759 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4760 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4761 (res = BIO_reset(conn->ssl_out)) < 0) {
4762 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4763 return NULL;
4764 }
4765 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4766 if (res < 0) {
4767 tls_show_errors(MSG_INFO, __func__,
4768 "Encryption failed - SSL_write");
4769 return NULL;
4770 }
4771
4772 /* Read encrypted data to be sent to the server */
4773 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4774 if (buf == NULL)
4775 return NULL;
4776 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4777 if (res < 0) {
4778 tls_show_errors(MSG_INFO, __func__,
4779 "Encryption failed - BIO_read");
4780 wpabuf_free(buf);
4781 return NULL;
4782 }
4783 wpabuf_put(buf, res);
4784
4785 return buf;
4786}
4787
4788
4789struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4790 struct tls_connection *conn,
4791 const struct wpabuf *in_data)
4792{
4793 int res;
4794 struct wpabuf *buf;
4795
4796 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4797 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4798 wpabuf_len(in_data));
4799 if (res < 0) {
4800 tls_show_errors(MSG_INFO, __func__,
4801 "Decryption failed - BIO_write");
4802 return NULL;
4803 }
4804 if (BIO_reset(conn->ssl_out) < 0) {
4805 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4806 return NULL;
4807 }
4808
4809 /* Read decrypted data for further processing */
4810 /*
4811 * Even though we try to disable TLS compression, it is possible that
4812 * this cannot be done with all TLS libraries. Add extra buffer space
4813 * to handle the possibility of the decrypted data being longer than
4814 * input data.
4815 */
4816 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4817 if (buf == NULL)
4818 return NULL;
4819 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4820 if (res < 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004821 int err = SSL_get_error(conn->ssl, res);
4822
4823 if (err == SSL_ERROR_WANT_READ) {
4824 wpa_printf(MSG_DEBUG,
4825 "SSL: SSL_connect - want more data");
4826 res = 0;
4827 } else {
4828 tls_show_errors(MSG_INFO, __func__,
4829 "Decryption failed - SSL_read");
4830 wpabuf_free(buf);
4831 return NULL;
4832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 }
4834 wpabuf_put(buf, res);
4835
Jouni Malinen26af48b2014-04-09 13:02:53 +03004836 if (conn->invalid_hb_used) {
4837 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4838 wpabuf_free(buf);
4839 return NULL;
4840 }
4841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 return buf;
4843}
4844
4845
4846int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4847{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004848 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849}
4850
4851
4852int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4853 u8 *ciphers)
4854{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004855 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 u8 *c;
4857 int ret;
4858
4859 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4860 return -1;
4861
4862 buf[0] = '\0';
4863 pos = buf;
4864 end = pos + sizeof(buf);
4865
4866 c = ciphers;
4867 while (*c != TLS_CIPHER_NONE) {
4868 const char *suite;
4869
4870 switch (*c) {
4871 case TLS_CIPHER_RC4_SHA:
4872 suite = "RC4-SHA";
4873 break;
4874 case TLS_CIPHER_AES128_SHA:
4875 suite = "AES128-SHA";
4876 break;
4877 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4878 suite = "DHE-RSA-AES128-SHA";
4879 break;
4880 case TLS_CIPHER_ANON_DH_AES128_SHA:
4881 suite = "ADH-AES128-SHA";
4882 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004883 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4884 suite = "DHE-RSA-AES256-SHA";
4885 break;
4886 case TLS_CIPHER_AES256_SHA:
4887 suite = "AES256-SHA";
4888 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 default:
4890 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4891 "cipher selection: %d", *c);
4892 return -1;
4893 }
4894 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004895 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 break;
4897 pos += ret;
4898
4899 c++;
4900 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004901 if (!buf[0]) {
4902 wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed");
4903 return -1;
4904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905
4906 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4907
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004908#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalom81f62d82019-07-22 12:10:00 -07004909#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004910 if (os_strstr(buf, ":ADH-")) {
4911 /*
4912 * Need to drop to security level 0 to allow anonymous
4913 * cipher suites for EAP-FAST.
4914 */
4915 SSL_set_security_level(conn->ssl, 0);
4916 } else if (SSL_get_security_level(conn->ssl) == 0) {
4917 /* Force at least security level 1 */
4918 SSL_set_security_level(conn->ssl, 1);
4919 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004920#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004921#endif
4922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4924 tls_show_errors(MSG_INFO, __func__,
4925 "Cipher suite configuration failed");
4926 return -1;
4927 }
4928
4929 return 0;
4930}
4931
4932
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004933int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4934 char *buf, size_t buflen)
4935{
4936 const char *name;
4937 if (conn == NULL || conn->ssl == NULL)
4938 return -1;
4939
4940 name = SSL_get_version(conn->ssl);
4941 if (name == NULL)
4942 return -1;
4943
4944 os_strlcpy(buf, name, buflen);
4945 return 0;
4946}
4947
4948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4950 char *buf, size_t buflen)
4951{
4952 const char *name;
4953 if (conn == NULL || conn->ssl == NULL)
4954 return -1;
4955
4956 name = SSL_get_cipher(conn->ssl);
4957 if (name == NULL)
4958 return -1;
4959
4960 os_strlcpy(buf, name, buflen);
4961 return 0;
4962}
4963
4964
4965int tls_connection_enable_workaround(void *ssl_ctx,
4966 struct tls_connection *conn)
4967{
4968 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4969
4970 return 0;
4971}
4972
4973
Hai Shalom81f62d82019-07-22 12:10:00 -07004974#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975/* ClientHello TLS extensions require a patch to openssl, so this function is
4976 * commented out unless explicitly needed for EAP-FAST in order to be able to
4977 * build this file with unmodified openssl. */
4978int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4979 int ext_type, const u8 *data,
4980 size_t data_len)
4981{
4982 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4983 return -1;
4984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4986 data_len) != 1)
4987 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988
4989 return 0;
4990}
Hai Shalom81f62d82019-07-22 12:10:00 -07004991#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992
4993
4994int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4995{
4996 if (conn == NULL)
4997 return -1;
4998 return conn->failed;
4999}
5000
5001
5002int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
5003{
5004 if (conn == NULL)
5005 return -1;
5006 return conn->read_alerts;
5007}
5008
5009
5010int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
5011{
5012 if (conn == NULL)
5013 return -1;
5014 return conn->write_alerts;
5015}
5016
5017
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005018#ifdef HAVE_OCSP
5019
5020static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
5021{
5022#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005023 BIO *out;
5024 size_t rlen;
5025 char *txt;
5026 int res;
5027
5028 if (wpa_debug_level > MSG_DEBUG)
5029 return;
5030
5031 out = BIO_new(BIO_s_mem());
5032 if (!out)
5033 return;
5034
5035 OCSP_RESPONSE_print(out, rsp, 0);
5036 rlen = BIO_ctrl_pending(out);
5037 txt = os_malloc(rlen + 1);
5038 if (!txt) {
5039 BIO_free(out);
5040 return;
5041 }
5042
5043 res = BIO_read(out, txt, rlen);
5044 if (res > 0) {
5045 txt[res] = '\0';
5046 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
5047 }
5048 os_free(txt);
5049 BIO_free(out);
5050#endif /* CONFIG_NO_STDOUT_DEBUG */
5051}
5052
5053
5054static int ocsp_resp_cb(SSL *s, void *arg)
5055{
5056 struct tls_connection *conn = arg;
5057 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005058 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005059 OCSP_RESPONSE *rsp;
5060 OCSP_BASICRESP *basic;
5061 OCSP_CERTID *id;
5062 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005063 X509_STORE *store;
5064 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005065
5066 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
5067 if (!p) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005068#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5069#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
5070 if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) {
5071 /* TLS 1.3 sends the OCSP response with the server
5072 * Certificate message. Since that Certificate message
5073 * is not sent when resuming a session, there can be no
5074 * new OCSP response. Allow this since the OCSP response
5075 * was validated when checking the initial certificate
5076 * exchange. */
5077 wpa_printf(MSG_DEBUG,
5078 "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session");
5079 return 1;
5080 }
5081#endif
5082#endif
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005083 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
5084 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5085 }
5086
5087 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
5088
5089 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
5090 if (!rsp) {
5091 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
5092 return 0;
5093 }
5094
5095 ocsp_debug_print_resp(rsp);
5096
5097 status = OCSP_response_status(rsp);
5098 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
5099 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
5100 status, OCSP_response_status_str(status));
5101 return 0;
5102 }
5103
5104 basic = OCSP_response_get1_basic(rsp);
5105 if (!basic) {
5106 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
5107 return 0;
5108 }
5109
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005110 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005111 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07005112 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005113
5114 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
5115 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005116 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005117 }
5118 certs = sk_X509_new_null();
5119 if (certs) {
5120 X509 *cert;
5121 cert = X509_dup(conn->peer_issuer);
5122 if (cert && !sk_X509_push(certs, cert)) {
5123 tls_show_errors(
5124 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005125 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005126 X509_free(cert);
5127 sk_X509_free(certs);
5128 certs = NULL;
5129 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005130 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005131 cert = X509_dup(conn->peer_issuer_issuer);
5132 if (cert && !sk_X509_push(certs, cert)) {
5133 tls_show_errors(
5134 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005135 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005136 X509_free(cert);
5137 }
5138 }
5139 }
5140 }
5141
5142 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
5143 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005144 if (status <= 0) {
5145 tls_show_errors(MSG_INFO, __func__,
5146 "OpenSSL: OCSP response failed verification");
5147 OCSP_BASICRESP_free(basic);
5148 OCSP_RESPONSE_free(rsp);
5149 return 0;
5150 }
5151
5152 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
5153
Dmitry Shmidt56052862013-10-04 10:23:25 -07005154 if (!conn->peer_cert) {
5155 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
5156 OCSP_BASICRESP_free(basic);
5157 OCSP_RESPONSE_free(rsp);
5158 return 0;
5159 }
5160
5161 if (!conn->peer_issuer) {
5162 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005163 OCSP_BASICRESP_free(basic);
5164 OCSP_RESPONSE_free(rsp);
5165 return 0;
5166 }
5167
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005168 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005169 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005170 wpa_printf(MSG_DEBUG,
5171 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005172 OCSP_BASICRESP_free(basic);
5173 OCSP_RESPONSE_free(rsp);
5174 return 0;
5175 }
5176
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005177 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
5178 &this_update, &next_update);
5179 if (!res) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005180 OCSP_CERTID_free(id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005181 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
5182 if (!id) {
5183 wpa_printf(MSG_DEBUG,
5184 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
5185 OCSP_BASICRESP_free(basic);
5186 OCSP_RESPONSE_free(rsp);
5187 return 0;
5188 }
5189
5190 res = OCSP_resp_find_status(basic, id, &status, &reason,
5191 &produced_at, &this_update,
5192 &next_update);
5193 }
5194
5195 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005196 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
5197 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
5198 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005199 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005200 OCSP_BASICRESP_free(basic);
5201 OCSP_RESPONSE_free(rsp);
5202 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
5203 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005204 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005205
5206 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
5207 tls_show_errors(MSG_INFO, __func__,
5208 "OpenSSL: OCSP status times invalid");
5209 OCSP_BASICRESP_free(basic);
5210 OCSP_RESPONSE_free(rsp);
5211 return 0;
5212 }
5213
5214 OCSP_BASICRESP_free(basic);
5215 OCSP_RESPONSE_free(rsp);
5216
5217 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
5218 OCSP_cert_status_str(status));
5219
5220 if (status == V_OCSP_CERTSTATUS_GOOD)
5221 return 1;
5222 if (status == V_OCSP_CERTSTATUS_REVOKED)
5223 return 0;
5224 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
5225 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
5226 return 0;
5227 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07005228 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 -07005229 return 1;
5230}
5231
5232
5233static int ocsp_status_cb(SSL *s, void *arg)
5234{
5235 char *tmp;
5236 char *resp;
5237 size_t len;
5238
5239 if (tls_global->ocsp_stapling_response == NULL) {
5240 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
5241 return SSL_TLSEXT_ERR_OK;
5242 }
5243
5244 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
5245 if (resp == NULL) {
5246 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
5247 /* TODO: Build OCSPResponse with responseStatus = internalError
5248 */
5249 return SSL_TLSEXT_ERR_OK;
5250 }
5251 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
5252 tmp = OPENSSL_malloc(len);
5253 if (tmp == NULL) {
5254 os_free(resp);
5255 return SSL_TLSEXT_ERR_ALERT_FATAL;
5256 }
5257
5258 os_memcpy(tmp, resp, len);
5259 os_free(resp);
5260 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
5261
5262 return SSL_TLSEXT_ERR_OK;
5263}
5264
5265#endif /* HAVE_OCSP */
5266
5267
Hai Shalomfdcde762020-04-02 11:19:20 -07005268static size_t max_str_len(const char **lines)
5269{
5270 const char **p;
5271 size_t max_len = 0;
5272
5273 for (p = lines; *p; p++) {
5274 size_t len = os_strlen(*p);
5275
5276 if (len > max_len)
5277 max_len = len;
5278 }
5279
5280 return max_len;
5281}
5282
5283
5284static int match_lines_in_file(const char *path, const char **lines)
5285{
5286 FILE *f;
5287 char *buf;
5288 size_t bufsize;
5289 int found = 0, is_linestart = 1;
5290
5291 bufsize = max_str_len(lines) + sizeof("\r\n");
5292 buf = os_malloc(bufsize);
5293 if (!buf)
5294 return 0;
5295
5296 f = fopen(path, "r");
5297 if (!f) {
5298 os_free(buf);
5299 return 0;
5300 }
5301
5302 while (!found && fgets(buf, bufsize, f)) {
5303 int is_lineend;
5304 size_t len;
5305 const char **p;
5306
5307 len = strcspn(buf, "\r\n");
5308 is_lineend = buf[len] != '\0';
5309 buf[len] = '\0';
5310
5311 if (is_linestart && is_lineend) {
5312 for (p = lines; !found && *p; p++)
5313 found = os_strcmp(buf, *p) == 0;
5314 }
5315 is_linestart = is_lineend;
5316 }
5317
5318 fclose(f);
5319 bin_clear_free(buf, bufsize);
5320
5321 return found;
5322}
5323
5324
5325static int is_tpm2_key(const char *path)
5326{
5327 /* Check both new and old format of TPM2 PEM guard tag */
5328 static const char *tpm2_tags[] = {
5329 "-----BEGIN TSS2 PRIVATE KEY-----",
5330 "-----BEGIN TSS2 KEY BLOB-----",
5331 NULL
5332 };
5333
5334 return match_lines_in_file(path, tpm2_tags);
5335}
5336
5337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
5339 const struct tls_connection_params *params)
5340{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005341 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 int ret;
5343 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005344 int can_pkcs11 = 0;
5345 const char *key_id = params->key_id;
5346 const char *cert_id = params->cert_id;
5347 const char *ca_cert_id = params->ca_cert_id;
5348 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005349 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350
5351 if (conn == NULL)
5352 return -1;
5353
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08005354 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
5355 wpa_printf(MSG_INFO,
5356 "OpenSSL: ocsp=3 not supported");
5357 return -1;
5358 }
5359
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005360 /*
5361 * If the engine isn't explicitly configured, and any of the
5362 * cert/key fields are actually PKCS#11 URIs, then automatically
5363 * use the PKCS#11 ENGINE.
5364 */
5365 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
5366 can_pkcs11 = 1;
5367
5368 if (!key_id && params->private_key && can_pkcs11 &&
5369 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
5370 can_pkcs11 = 2;
5371 key_id = params->private_key;
5372 }
5373
5374 if (!cert_id && params->client_cert && can_pkcs11 &&
5375 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
5376 can_pkcs11 = 2;
5377 cert_id = params->client_cert;
5378 }
5379
5380 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
5381 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
5382 can_pkcs11 = 2;
5383 ca_cert_id = params->ca_cert;
5384 }
5385
5386 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
5387 if (can_pkcs11 == 2 && !engine_id)
5388 engine_id = "pkcs11";
5389
Hai Shalomfdcde762020-04-02 11:19:20 -07005390 /* If private_key points to a TPM2-wrapped key, automatically enable
5391 * tpm2 engine and use it to unwrap the key. */
5392 if (params->private_key &&
5393 (!engine_id || os_strcmp(engine_id, "tpm2") == 0) &&
5394 is_tpm2_key(params->private_key)) {
5395 wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s",
5396 params->private_key);
5397 key_id = key_id ? key_id : params->private_key;
5398 engine_id = engine_id ? engine_id : "tpm2";
5399 }
5400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005401#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005402#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005403 if (params->flags & TLS_CONN_EAP_FAST) {
5404 wpa_printf(MSG_DEBUG,
5405 "OpenSSL: Use TLSv1_method() for EAP-FAST");
5406 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
5407 tls_show_errors(MSG_INFO, __func__,
5408 "Failed to set TLSv1_method() for EAP-FAST");
5409 return -1;
5410 }
5411 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005412#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07005413#if OPENSSL_VERSION_NUMBER >= 0x10101000L
5414#ifdef SSL_OP_NO_TLSv1_3
5415 if (params->flags & TLS_CONN_EAP_FAST) {
5416 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
5417 * refuses to start the handshake with the modified ciphersuite
5418 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
5419 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
5420 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
5421 }
5422#endif /* SSL_OP_NO_TLSv1_3 */
5423#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005424#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005426 while ((err = ERR_get_error())) {
5427 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5428 __func__, ERR_error_string(err, NULL));
5429 }
5430
Sunil Ravi77d572f2023-01-17 23:58:31 +00005431 if (tls_set_conn_flags(conn, params->flags,
5432 params->openssl_ciphers) < 0) {
5433 wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags");
5434 return -1;
5435 }
5436
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005437 if (engine_id) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005438 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s",
5439 engine_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005440 ret = tls_engine_init(conn, engine_id, params->pin,
5441 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 if (ret)
5443 return ret;
5444 }
5445 if (tls_connection_set_subject_match(conn,
5446 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07005447 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005448 params->suffix_match,
Hai Shalom021b0b52019-04-10 11:17:58 -07005449 params->domain_match,
Hai Shalom22171592021-04-02 16:05:23 -07005450 params->check_cert_subject)) {
5451 wpa_printf(MSG_ERROR, "TLS: Failed to set subject match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005455 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005456 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005458 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 params->ca_cert_blob,
5460 params->ca_cert_blob_len,
Hai Shalom22171592021-04-02 16:05:23 -07005461 params->ca_path)) {
5462 wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005464 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005466 if (engine_id && cert_id) {
5467 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
5469 } else if (tls_connection_client_cert(conn, params->client_cert,
5470 params->client_cert_blob,
Hai Shalom22171592021-04-02 16:05:23 -07005471 params->client_cert_blob_len)) {
5472 wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 return -1;
Hai Shalom22171592021-04-02 16:05:23 -07005474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005476 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
5478 if (tls_connection_engine_private_key(conn))
5479 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005480 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 params->private_key,
5482 params->private_key_passwd,
5483 params->private_key_blob,
5484 params->private_key_blob_len)) {
5485 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
5486 params->private_key);
5487 return -1;
5488 }
5489
Roshan Pius3a1667e2018-07-03 15:17:14 -07005490 ciphers = params->openssl_ciphers;
5491#ifdef CONFIG_SUITEB
5492#ifdef OPENSSL_IS_BORINGSSL
5493 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
5494 /* BoringSSL removed support for SUITEB192, so need to handle
5495 * this with hardcoded ciphersuite and additional checks for
5496 * other parameters. */
5497 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
5498 }
5499#endif /* OPENSSL_IS_BORINGSSL */
5500#endif /* CONFIG_SUITEB */
5501 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005502 wpa_printf(MSG_INFO,
5503 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07005504 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005505 return -1;
5506 }
5507
Hai Shalom74f70d42019-02-11 14:42:39 -08005508 if (!params->openssl_ecdh_curves) {
5509#ifndef OPENSSL_IS_BORINGSSL
5510#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005511#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005512 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
5513 wpa_printf(MSG_INFO,
5514 "OpenSSL: Failed to set ECDH curves to auto");
5515 return -1;
5516 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005517#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005518#endif /* OPENSSL_NO_EC */
5519#endif /* OPENSSL_IS_BORINGSSL */
5520 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005521#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005522 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005523 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005524 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005525#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005526#ifndef OPENSSL_NO_EC
5527 if (SSL_set1_curves_list(conn->ssl,
5528 params->openssl_ecdh_curves) != 1) {
5529 wpa_printf(MSG_INFO,
5530 "OpenSSL: Failed to set ECDH curves '%s'",
5531 params->openssl_ecdh_curves);
5532 return -1;
5533 }
5534#else /* OPENSSL_NO_EC */
5535 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5536 return -1;
5537#endif /* OPENSSL_NO_EC */
5538#endif /* OPENSSL_IS_BORINGSSL */
5539 }
5540
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005541#ifdef OPENSSL_IS_BORINGSSL
5542 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5543 SSL_enable_ocsp_stapling(conn->ssl);
5544 }
5545#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005546#ifdef HAVE_OCSP
5547 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005548 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005549 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
5550 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
5551 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
5552 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005553#else /* HAVE_OCSP */
5554 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
5555 wpa_printf(MSG_INFO,
5556 "OpenSSL: No OCSP support included - reject configuration");
5557 return -1;
5558 }
5559 if (params->flags & TLS_CONN_REQUEST_OCSP) {
5560 wpa_printf(MSG_DEBUG,
5561 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
5562 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005563#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005564#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005565
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005566 conn->flags = params->flags;
5567
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005568 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569
5570 return 0;
5571}
5572
5573
Hai Shalom81f62d82019-07-22 12:10:00 -07005574static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx)
5575{
5576 SSL *ssl;
5577 int i;
5578
5579 ssl = SSL_new(ssl_ctx);
5580 if (!ssl)
5581 return;
5582
5583 wpa_printf(MSG_DEBUG,
5584 "OpenSSL: Enabled cipher suites in priority order");
5585 for (i = 0; ; i++) {
5586 const char *cipher;
5587
5588 cipher = SSL_get_cipher_list(ssl, i);
5589 if (!cipher)
5590 break;
5591 wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher);
5592 }
5593
5594 SSL_free(ssl);
5595}
5596
5597
5598#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5599
5600static const char * openssl_pkey_type_str(const EVP_PKEY *pkey)
5601{
5602 if (!pkey)
5603 return "NULL";
5604 switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) {
5605 case EVP_PKEY_RSA:
5606 return "RSA";
5607 case EVP_PKEY_DSA:
5608 return "DSA";
5609 case EVP_PKEY_DH:
5610 return "DH";
5611 case EVP_PKEY_EC:
5612 return "EC";
Sunil Ravi77d572f2023-01-17 23:58:31 +00005613 default:
5614 return "?";
Hai Shalom81f62d82019-07-22 12:10:00 -07005615 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005616}
5617
5618
5619static void openssl_debug_dump_certificate(int i, X509 *cert)
5620{
5621 char buf[256];
5622 EVP_PKEY *pkey;
5623 ASN1_INTEGER *ser;
5624 char serial_num[128];
5625
Hai Shalom60840252021-02-19 19:02:11 -08005626 if (!cert)
5627 return;
5628
Hai Shalom81f62d82019-07-22 12:10:00 -07005629 X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
5630
5631 ser = X509_get_serialNumber(cert);
5632 if (ser)
5633 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
5634 ASN1_STRING_get0_data(ser),
5635 ASN1_STRING_length(ser));
5636 else
5637 serial_num[0] = '\0';
5638
5639 pkey = X509_get_pubkey(cert);
5640 wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf,
5641 openssl_pkey_type_str(pkey), serial_num);
5642 EVP_PKEY_free(pkey);
5643}
5644
5645
5646static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx)
5647{
5648 STACK_OF(X509) *certs;
5649
5650 wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain");
5651 if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) {
5652 int i;
5653
5654 for (i = sk_X509_num(certs); i > 0; i--)
5655 openssl_debug_dump_certificate(i, sk_X509_value(certs,
5656 i - 1));
5657 }
5658 openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx));
5659}
5660
5661#endif
5662
5663
5664static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx)
5665{
5666#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION)
5667 int res;
5668
5669 for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5670 res == 1;
5671 res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT))
5672 openssl_debug_dump_certificates(ssl_ctx);
5673
5674 SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST);
5675#endif
5676}
5677
5678
5679static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx)
5680{
5681 openssl_debug_dump_cipher_list(ssl_ctx);
5682 openssl_debug_dump_certificate_chains(ssl_ctx);
5683}
5684
5685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686int tls_global_set_params(void *tls_ctx,
5687 const struct tls_connection_params *params)
5688{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005689 struct tls_data *data = tls_ctx;
5690 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691 unsigned long err;
5692
5693 while ((err = ERR_get_error())) {
5694 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
5695 __func__, ERR_error_string(err, NULL));
5696 }
5697
Hai Shalom021b0b52019-04-10 11:17:58 -07005698 os_free(data->check_cert_subject);
5699 data->check_cert_subject = NULL;
5700 if (params->check_cert_subject) {
5701 data->check_cert_subject =
5702 os_strdup(params->check_cert_subject);
5703 if (!data->check_cert_subject)
5704 return -1;
5705 }
5706
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005707 if (tls_global_ca_cert(data, params->ca_cert) ||
5708 tls_global_client_cert(data, params->client_cert) ||
5709 tls_global_private_key(data, params->private_key,
5710 params->private_key_passwd) ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005711 tls_global_client_cert(data, params->client_cert2) ||
5712 tls_global_private_key(data, params->private_key2,
5713 params->private_key_passwd2) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005714 tls_global_dh(data, params->dh_file)) {
5715 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 return -1;
5717 }
5718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005719 if (params->openssl_ciphers &&
5720 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
5721 wpa_printf(MSG_INFO,
5722 "OpenSSL: Failed to set cipher string '%s'",
5723 params->openssl_ciphers);
5724 return -1;
5725 }
5726
Hai Shalom74f70d42019-02-11 14:42:39 -08005727 if (!params->openssl_ecdh_curves) {
5728#ifndef OPENSSL_IS_BORINGSSL
5729#ifndef OPENSSL_NO_EC
Sunil Ravia04bd252022-05-02 22:54:18 -07005730#if OPENSSL_VERSION_NUMBER < 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -08005731 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
5732 wpa_printf(MSG_INFO,
5733 "OpenSSL: Failed to set ECDH curves to auto");
5734 return -1;
5735 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005736#endif /* < 1.1.0 */
Hai Shalom74f70d42019-02-11 14:42:39 -08005737#endif /* OPENSSL_NO_EC */
5738#endif /* OPENSSL_IS_BORINGSSL */
5739 } else if (params->openssl_ecdh_curves[0]) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005740#ifdef OPENSSL_IS_BORINGSSL
Hai Shalom74f70d42019-02-11 14:42:39 -08005741 wpa_printf(MSG_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07005742 "OpenSSL: ECDH configuration not supported");
Hai Shalom74f70d42019-02-11 14:42:39 -08005743 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07005744#else /* !OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08005745#ifndef OPENSSL_NO_EC
Hai Shalom5f92bc92019-04-18 11:54:11 -07005746#if OPENSSL_VERSION_NUMBER < 0x10100000L
5747 SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
5748#endif
Hai Shalom74f70d42019-02-11 14:42:39 -08005749 if (SSL_CTX_set1_curves_list(ssl_ctx,
5750 params->openssl_ecdh_curves) !=
5751 1) {
5752 wpa_printf(MSG_INFO,
5753 "OpenSSL: Failed to set ECDH curves '%s'",
5754 params->openssl_ecdh_curves);
5755 return -1;
5756 }
5757#else /* OPENSSL_NO_EC */
5758 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
5759 return -1;
5760#endif /* OPENSSL_NO_EC */
5761#endif /* OPENSSL_IS_BORINGSSL */
5762 }
5763
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005764#ifdef SSL_OP_NO_TICKET
5765 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
5766 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
5767 else
5768 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
5769#endif /* SSL_OP_NO_TICKET */
5770
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005771#ifdef HAVE_OCSP
5772 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
5773 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
5774 os_free(tls_global->ocsp_stapling_response);
5775 if (params->ocsp_stapling_response)
5776 tls_global->ocsp_stapling_response =
5777 os_strdup(params->ocsp_stapling_response);
5778 else
5779 tls_global->ocsp_stapling_response = NULL;
5780#endif /* HAVE_OCSP */
5781
Hai Shalom81f62d82019-07-22 12:10:00 -07005782 openssl_debug_dump_ctx(ssl_ctx);
5783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784 return 0;
5785}
5786
5787
Hai Shalom81f62d82019-07-22 12:10:00 -07005788#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005789/* Pre-shared secred requires a patch to openssl, so this function is
5790 * commented out unless explicitly needed for EAP-FAST in order to be able to
5791 * build this file with unmodified openssl. */
5792
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005793#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005794static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5795 STACK_OF(SSL_CIPHER) *peer_ciphers,
5796 const SSL_CIPHER **cipher, void *arg)
5797#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
5799 STACK_OF(SSL_CIPHER) *peer_ciphers,
5800 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005801#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802{
5803 struct tls_connection *conn = arg;
5804 int ret;
5805
Sunil Ravia04bd252022-05-02 22:54:18 -07005806#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005807 if (conn == NULL || conn->session_ticket_cb == NULL)
5808 return 0;
5809
5810 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5811 conn->session_ticket,
5812 conn->session_ticket_len,
5813 s->s3->client_random,
5814 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005815#else
5816 unsigned char client_random[SSL3_RANDOM_SIZE];
5817 unsigned char server_random[SSL3_RANDOM_SIZE];
5818
5819 if (conn == NULL || conn->session_ticket_cb == NULL)
5820 return 0;
5821
5822 SSL_get_client_random(s, client_random, sizeof(client_random));
5823 SSL_get_server_random(s, server_random, sizeof(server_random));
5824
5825 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
5826 conn->session_ticket,
5827 conn->session_ticket_len,
5828 client_random,
5829 server_random, secret);
5830#endif
5831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005832 os_free(conn->session_ticket);
5833 conn->session_ticket = NULL;
5834
5835 if (ret <= 0)
5836 return 0;
5837
5838 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
5839 return 1;
5840}
5841
5842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
5844 int len, void *arg)
5845{
5846 struct tls_connection *conn = arg;
5847
5848 if (conn == NULL || conn->session_ticket_cb == NULL)
5849 return 0;
5850
5851 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
5852
5853 os_free(conn->session_ticket);
5854 conn->session_ticket = NULL;
5855
5856 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
5857 "extension", data, len);
5858
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005859 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860 if (conn->session_ticket == NULL)
5861 return 0;
5862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863 conn->session_ticket_len = len;
5864
5865 return 1;
5866}
Hai Shalom81f62d82019-07-22 12:10:00 -07005867#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868
5869
5870int tls_connection_set_session_ticket_cb(void *tls_ctx,
5871 struct tls_connection *conn,
5872 tls_session_ticket_cb cb,
5873 void *ctx)
5874{
Hai Shalom81f62d82019-07-22 12:10:00 -07005875#ifdef EAP_FAST_OR_TEAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 conn->session_ticket_cb = cb;
5877 conn->session_ticket_cb_ctx = ctx;
5878
5879 if (cb) {
5880 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
5881 conn) != 1)
5882 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 SSL_set_session_ticket_ext_cb(conn->ssl,
5884 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005885 } else {
5886 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
5887 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005889 }
5890
5891 return 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07005892#else /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005894#endif /* EAP_FAST_OR_TEAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005896
5897
5898int tls_get_library_version(char *buf, size_t buf_len)
5899{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005900#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005901 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5902 OPENSSL_VERSION_TEXT,
5903 OpenSSL_version(OPENSSL_VERSION));
5904#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005905 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
5906 OPENSSL_VERSION_TEXT,
5907 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005908#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005909}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005910
5911
5912void tls_connection_set_success_data(struct tls_connection *conn,
5913 struct wpabuf *data)
5914{
5915 SSL_SESSION *sess;
5916 struct wpabuf *old;
Sunil Ravia04bd252022-05-02 22:54:18 -07005917 struct tls_session_data *sess_data = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005918
5919 if (tls_ex_idx_session < 0)
5920 goto fail;
5921 sess = SSL_get_session(conn->ssl);
5922 if (!sess)
5923 goto fail;
5924 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5925 if (old) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005926 struct tls_session_data *found;
5927
5928 found = get_session_data(conn->context, old);
5929 wpa_printf(MSG_DEBUG,
5930 "OpenSSL: Replacing old success data %p (sess %p)%s",
5931 old, sess, found ? "" : " (not freeing)");
5932 if (found) {
5933 dl_list_del(&found->list);
5934 os_free(found);
5935 wpabuf_free(old);
5936 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005937 }
Sunil Ravia04bd252022-05-02 22:54:18 -07005938
5939 sess_data = os_zalloc(sizeof(*sess_data));
5940 if (!sess_data ||
5941 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005942 goto fail;
5943
Sunil Ravia04bd252022-05-02 22:54:18 -07005944 sess_data->buf = data;
5945 dl_list_add(&conn->context->sessions, &sess_data->list);
5946 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)",
5947 data, sess);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005948 conn->success_data = 1;
5949 return;
5950
5951fail:
5952 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
5953 wpabuf_free(data);
Sunil Ravia04bd252022-05-02 22:54:18 -07005954 os_free(sess_data);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005955}
5956
5957
5958void tls_connection_set_success_data_resumed(struct tls_connection *conn)
5959{
5960 wpa_printf(MSG_DEBUG,
5961 "OpenSSL: Success data accepted for resumed session");
5962 conn->success_data = 1;
5963}
5964
5965
5966const struct wpabuf *
5967tls_connection_get_success_data(struct tls_connection *conn)
5968{
5969 SSL_SESSION *sess;
5970
5971 if (tls_ex_idx_session < 0 ||
5972 !(sess = SSL_get_session(conn->ssl)))
5973 return NULL;
5974 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5975}
5976
5977
5978void tls_connection_remove_session(struct tls_connection *conn)
5979{
5980 SSL_SESSION *sess;
5981
5982 sess = SSL_get_session(conn->ssl);
5983 if (!sess)
5984 return;
5985
5986 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5987 wpa_printf(MSG_DEBUG,
5988 "OpenSSL: Session was not cached");
5989 else
5990 wpa_printf(MSG_DEBUG,
5991 "OpenSSL: Removed cached session to disable session resumption");
5992}
Hai Shalom81f62d82019-07-22 12:10:00 -07005993
5994
5995int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len)
5996{
5997 size_t len;
5998 int reused;
5999
6000 reused = SSL_session_reused(conn->ssl);
6001 if ((conn->server && !reused) || (!conn->server && reused))
6002 len = SSL_get_peer_finished(conn->ssl, buf, max_len);
6003 else
6004 len = SSL_get_finished(conn->ssl, buf, max_len);
6005
6006 if (len == 0 || len > max_len)
6007 return -1;
6008
6009 return len;
6010}
6011
6012
6013u16 tls_connection_get_cipher_suite(struct tls_connection *conn)
6014{
6015 const SSL_CIPHER *cipher;
6016
6017 cipher = SSL_get_current_cipher(conn->ssl);
6018 if (!cipher)
6019 return 0;
6020#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6021 return SSL_CIPHER_get_protocol_id(cipher);
6022#else
6023 return SSL_CIPHER_get_id(cipher) & 0xFFFF;
6024#endif
6025}
Hai Shalom899fcc72020-10-19 14:38:18 -07006026
6027
6028const char * tls_connection_get_peer_subject(struct tls_connection *conn)
6029{
6030 if (conn)
6031 return conn->peer_subject;
6032 return NULL;
6033}
6034
6035
6036bool tls_connection_get_own_cert_used(struct tls_connection *conn)
6037{
6038 if (conn)
6039 return SSL_get_certificate(conn->ssl) != NULL;
6040 return false;
6041}
Gabriel Birena5bdf372022-12-15 20:54:33 +00006042
6043void tls_register_cert_callback(tls_get_certificate_cb cb)
6044{
6045 certificate_callback_global = cb;
6046}